diff --git a/reports/AccountReport.ts b/reports/AccountReport.ts index 2f0bba60..093bdeee 100644 --- a/reports/AccountReport.ts +++ b/reports/AccountReport.ts @@ -236,6 +236,17 @@ export abstract class AccountReport extends LedgerReport { return null; } + // Fix arythmetic on dates when adding or substracting months. If the + // reference date was the last day in month, ensure that the resulting date is + // also the last day. + _fixMonthsJump(refDate: DateTime, date: DateTime): DateTime { + if (refDate.day == refDate.daysInMonth && date.day != date.daysInMonth) { + return date.set({day: date.daysInMonth}) + } else { + return date + } + } + async _getDateRanges(): Promise { const endpoints = await this._getFromAndToDates(); const fromDate = DateTime.fromISO(endpoints.fromDate); @@ -252,7 +263,7 @@ export abstract class AccountReport extends LedgerReport { const months: number = monthsMap[this.periodicity]; const dateRanges: DateRange[] = [ - { toDate, fromDate: toDate.minus({ months }) }, + { toDate, fromDate: this._fixMonthsJump(toDate, toDate.minus({ months })) }, ]; let count = this.count ?? 1; @@ -264,7 +275,7 @@ export abstract class AccountReport extends LedgerReport { const lastRange = dateRanges.at(-1)!; dateRanges.push({ toDate: lastRange.fromDate, - fromDate: lastRange.fromDate.minus({ months }), + fromDate: this._fixMonthsJump(toDate, lastRange.fromDate.minus({ months })), }); } @@ -445,14 +456,15 @@ export async function getFiscalEndpoints( const fromDate = [ fromYear, - fys.toISOString().split('T')[0].split('-').slice(1), - ] - .flat() - .join('-'); + (fys.getMonth() + 1).toString().padStart(2, '0'), + fys.getDate().toString().padStart(2, '0') + ].join('-'); - const toDate = [toYear, fye.toISOString().split('T')[0].split('-').slice(1)] - .flat() - .join('-'); + const toDate = [ + toYear, + (fye.getMonth() + 1).toString().padStart(2, '0'), + fye.getDate().toString().padStart(2, '0') + ].join('-'); return { fromDate, toDate }; }