Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jajoho committed Feb 6, 2022
1 parent 29ac453 commit e632883
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 46 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

.DS_Store
.vscode/extensions.json
.markdownlint.yaml
.trunk/trunk.yaml
.trunk/.gitignore
95 changes: 50 additions & 45 deletions CalculateInterest.grandtotalplugin/index.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
/*
Return a JSON with the values you like to replace
Keys:
delayStart -> Delay period start date (date, formatted)
delayEnd -> Delay period end date (date, formatted)
outstandingDebt -> Outstanding debt (number)
interestRate -> Interest rate (number)
// Return a JSON with the values you like to replace

*/
// Keys:
// delayStart -> Delay period start date (date, formatted)
// delayEnd -> Delay period end date (date, formatted)
// outstandingDebt -> Outstanding debt (number)
// interestRate -> Interest rate (number)

//

// Function to fetch via API the prime rate from bundesbank.de
function getXML() {
string = loadURL("GET", "https://api.statistiken.bundesbank.de/rest/data/BBK01/SU0115?detail=dataonly&lastNObservations=1");
if (string.length == 0) {
return null;
}
getString();
return result;

function getString() {
var regExp = /-[0-9]*\.[0-9]+/m;
result = string.match(regExp);
}
string = loadURL(
"GET",
"https://api.statistiken.bundesbank.de/rest/data/BBK01/SU0115?detail=dataonly&lastNObservations=1"
);
if (string.length == 0) {
return null;
}
getString();
return result;

function getString() {
var regExp = /-[0-9]*\.[0-9]+/m;
result = string.match(regExp);
}
}

// Check if year is leap (366 days) year or regular year (365 days) for interest calculation
function daysOfYear(year) {
return isLeapYear(year) ? 366 : 365;
return isLeapYear(year) ? 366 : 365;
}

function isLeapYear(year) {
return year % 400 === 0 || (year % 100 !== 0 && year % 4 === 0);
return year % 400 === 0 || (year % 100 !== 0 && year % 4 === 0);
}

var currentTime = new Date();
Expand All @@ -41,50 +43,53 @@ delayEnd = new Date(delayEnd);
delayStart = new Date(delayStart);

// Math.round to still have delayInDays without decimals (can occur when date strings are not unified)
var delayInDays = Math.round(((delayEnd - delayStart) / (1000 * 3600 * 24)) + 1); // +1 to count first and last day
var delayInDays = Math.round((delayEnd - delayStart) / (1000 * 3600 * 24) + 1); // +1 to count first and last day

// Calculate final interest rate with interest rate entered by user and prime rate from bundesbank.de
// Calculate final interest rate with interest rate entered by user and prime rate from bundesbank.de
var calculatedInterestRate = parseInt(interestRate) + +getXML();

// Calculate interest and round the result of the calculation
var sumInterest = originalClaimAmount * calculatedInterestRate / 100 / daysOfYear(currentYear) * delayInDays;
var sumInterest =
((originalClaimAmount * calculatedInterestRate) /
100 /
daysOfYear(currentYear)) *
delayInDays;
var interestRounded = Math.round((sumInterest + Number.EPSILON) * 100) / 100;

optionsLocaleDate = {
year: "numeric",
month: "short",
day: "numeric"
year: "numeric",
month: "short",
day: "numeric",
};

// Update unit price and notes
update();

function update() {
var result = {};
var result = {};

// Create and change notes and provide GrandTotal interest sum for the document
var aNotes = valueForKeyPath("notes");
if (!aNotes)
aNotes = "";
// Create and change notes and provide GrandTotal interest sum for the document
var aNotes = valueForKeyPath("notes");
if (!aNotes) aNotes = "";

aNotes = removePrevious(aNotes);
aNotes = removePrevious(aNotes);

aLine = `${localize("Delay Period")}: ${delayInDays} ${localize("Days")} (${localize("from")} ${delayStart.toLocaleDateString("de-De", optionsLocaleDate)} ${localize("until")} ${delayEnd.toLocaleDateString("de-De", optionsLocaleDate)})\n${localize("Original claim amount")}: ${currency} ${formattedNumber(originalClaimAmount)}\n${localize("Interest rate")}: ${formattedNumber(calculatedInterestRate)} %`;
aLine = `${localize("Delay Period")}: ${delayInDays} ${localize("Days")} (${localize("from")} ${delayStart.toLocaleDateString("de-De",optionsLocaleDate)} ${localize("until")} ${delayEnd.toLocaleDateString("de-De",optionsLocaleDate
)})\n${localize("Original claim amount")}: ${currency} ${formattedNumber(originalClaimAmount
)}\n${localize("Interest rate")}: ${formattedNumber(calculatedInterestRate)} %`;

aLine = "<i>" + aLine + "</i>";
aLine = "<i>" + aLine + "</i>";

if (aNotes.length > 0)
aNewNotes = aNotes + aLine;
else
aNewNotes = aLine;
if (aNotes.length > 0) aNewNotes = aNotes + aLine;
else aNewNotes = aLine;

result.notes = aNewNotes;
result.unitPrice = interestRounded;
result.notes = aNewNotes;
result.unitPrice = interestRounded;

return result;
return result;
}

function removePrevious(s) {
var regExp = /(\<i)\s*[^\>]*\>([^\<]*\<\/i>)?/gi;
return s.replace(regExp, "");
var regExp = /(\<i)\s*[^\>]*\>([^\<]*\<\/i>)?/gi;
return s.replace(regExp, "");
}

0 comments on commit e632883

Please sign in to comment.