Skip to content

Commit

Permalink
new version 2.5 (calculation method changed)
Browse files Browse the repository at this point in the history
  • Loading branch information
jajoho committed Feb 7, 2022
1 parent 39ed267 commit 3c71683
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 23 deletions.
2 changes: 1 addition & 1 deletion CalculateInterest.grandtotalplugin/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
<key>copyright</key>
<string>CC0 v1.0 2022 Jatayu Holznagel</string>
<key>CFBundleVersion</key>
<string>2.2.3</string>
<string>2.5</string>
<key>width</key>
<real>500</real>
<key>GrandTotalMinimumVersion</key>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
"until" = "bis zum";
"Check Internet Connection" = "Internet Verbindung prüfen, um den Basiszinssatz abzurufen.";
"Own interest rate" = "Eigener Zinssatz";
"ErrorDayCount" = "Fehler: Anzahl der Tage muss eine positive Zahl sein";
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
"until" = "until";
"Check Internet Connection" = "Check internet connection to retrieve prime rate.";
"Own interest rate" = "Own interest rate";
"ErrorDayCount" = "Error: Number of days must be a positive number";
37 changes: 15 additions & 22 deletions CalculateInterest.grandtotalplugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
// interestRate -> Interest rate (number)
// ownInterestRate -> Own interest rate, if the automatically calculated one is to be overwritten (number)

// Interest rate calculated with actual/360 method (EZB): https://en.wikipedia.org/wiki/Day_count_convention#Actual/360
var actEZB = parseInt(360.00);

// Function to fetch via API the prime rate from bundesbank.de
function getXML() {
const URL = "https://api.statistiken.bundesbank.de/rest/data/";
Expand All @@ -29,24 +32,12 @@ var primeRate = getXML();
var primeRate = parseFloat(primeRate);
var primeInterest = interestRate + primeRate;

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

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

var currentTime = new Date();
var currentYear = currentTime.getFullYear();

///Calculate delay period in number of days
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
// Calculate difference between start and end of default period
var delayInDays = ((delayEnd - delayStart) / (1000 * 3600 * 24)) + 1; // +1 to count first and last day

// Calculate final interest rate with own interest rate or prime rate from bundesbank.de
function calculatedInterestRate() {
Expand All @@ -58,11 +49,10 @@ function calculatedInterestRate() {
return result;
}
}
var finalInterestRate = calculatedInterestRate();
var interestRate = calculatedInterestRate();

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

optionsLocaleDate = {
year: "numeric",
Expand All @@ -72,10 +62,13 @@ optionsLocaleDate = {

// Distinction between 1 and several days for notes
function localizeDay() {
if (delayInDays < 0) {
return `${localize("ErrorDayCount")}`;
}
if (delayInDays == 1) {
return `${localize("Day")} (${delayStart.toLocaleDateString("de-De", optionsLocaleDate)})`;
return `${localize("DelayPeriod")}: ${new Intl.NumberFormat("de-DE").format(delayInDays)} ${localize("Day")} (${delayStart.toLocaleDateString("de-De", optionsLocaleDate)})`;
} else {
return `${localize("Days")} (${localize("from")} ${delayStart.toLocaleDateString("de-De", optionsLocaleDate)} ${localize("until")} ${delayEnd.toLocaleDateString("de-De", optionsLocaleDate)})`;
return `${localize("DelayPeriod")}: ${new Intl.NumberFormat("de-DE").format(delayInDays)} ${localize("Day")} ${localize("Days")} (${localize("from")} ${delayStart.toLocaleDateString("de-De", optionsLocaleDate)} ${localize("until")} ${delayEnd.toLocaleDateString("de-De", optionsLocaleDate)})`;
}
}

Expand All @@ -91,15 +84,15 @@ function update() {

aNotes = removePrevious(aNotes);

aLine = `${localize("DelayPeriod")}: ${new Intl.NumberFormat("de-DE").format(delayInDays)} ${localizeDay()}\n${localize("OriginalClaimAmount")}: ${new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR', currencyDisplay: 'code'}).format(originalClaimAmount)}\n${localize("InterestRate")}: ${formattedNumber(finalInterestRate)} %`;
aLine = `${localizeDay()}\n${localize("OriginalClaimAmount")}: ${new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR', currencyDisplay: 'code'}).format(originalClaimAmount)}\n${localize("InterestRate")}: ${new Intl.NumberFormat("de-DE").format((interestRate))} %`;

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

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

result.notes = aNewNotes;
result.unitPrice = interestRounded;
result.unitPrice = parseFloat(sumInterest);

return result;
}
Expand Down

0 comments on commit 3c71683

Please sign in to comment.