Skip to content

Commit

Permalink
Added mathround for delay period, small changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jajoho committed Feb 6, 2022
1 parent 112d5c1 commit edcad83
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions CalculateInterest.grandtotalplugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
Return a JSON with the values you like to replace
Keys:
delayEnd -> Delay period end date (date, formatted)
delayStart -> Delay period start date (date, formatted)
delayEnd -> Delay period end date (date, formatted)
outstandingDebt -> Outstanding debt (number)
interestRate -> Interest rate (number)
Expand Down Expand Up @@ -36,23 +36,31 @@ function isLeapYear(year) {
var currentTime = new Date();
var currentYear = currentTime.getFullYear();

///Calculate delay period in number of days
delayEnd = new Date(delayEnd);
delayStart = new Date(delayStart);

update();
// 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

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

///Calculate delay period in number of days
delayEnd = new Date(delayEnd);
delayStart = new Date(delayStart);
var delayInDays = ((delayEnd - delayStart) / (1000 * 3600 * 24)) + 1; // +1 to count first and last day
// Calculate interest and round the result of the calculation
var sumInterest = originalClaimAmount * calculatedInterestRate / 100 / daysOfYear(currentYear) * delayInDays;
var interestRounded = Math.round((sumInterest + Number.EPSILON) * 100) / 100;

// Calculate final interest rate with interest rate entered by user and prime rate from bundesbank.de
var calculatedInterestRate = parseInt(interestRate) + +getXML();
optionsLocaleDate = {
year: "numeric",
month: "short",
day: "numeric"
};

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

function update() {
var result = {};

// Create and change notes and provide GrandTotal interest sum for the document
var aNotes = valueForKeyPath("notes");
Expand All @@ -61,15 +69,7 @@ function update() {

aNotes = removePrevious(aNotes);

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


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>";

Expand All @@ -78,13 +78,12 @@ function update() {
else
aNewNotes = aLine;

result["notes"] = aNewNotes;
result["unitPrice"] = interestRounded;
result.notes = aNewNotes;
result.unitPrice = interestRounded;

return result;
}


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

0 comments on commit edcad83

Please sign in to comment.