Skip to content

Commit

Permalink
fix(reports): correct date calculation in AccountReport
Browse files Browse the repository at this point in the history
fyo.getValue(*, 'fiscalYear{Start,End}') returns a javascript Date object
that encodes the filcal year as local time (01-01 and 12-31 at midnight).
This date must not be converted to UTC else east timezones will return
the day before (12-31 and 12-30). Use locale time values instead.

When computing the monthly/quaterly/half yearly time periods, correct the
dates in the particular case where the last day of the month is selected
as reference date (yields series 12-31 11-30 10-31 09-30 ...) instead of
(12-31 11-30 10-30 09-30 ...).
  • Loading branch information
mildred committed Dec 5, 2023
1 parent 82a2c5e commit d04b156
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions reports/AccountReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DateRange[]> {
const endpoints = await this._getFromAndToDates();
const fromDate = DateTime.fromISO(endpoints.fromDate);
Expand All @@ -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;
Expand All @@ -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 })),
});
}

Expand Down Expand Up @@ -445,14 +456,15 @@ export async function getFiscalEndpoints(

const fromDate = [
fromYear,
fys.toISOString().split('T')[0].split('-').slice(1),
]
.flat()
.join('-');

const toDate = [toYear, fye.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.getMonth() + 1).toString().padStart(2, '0'),
fye.getDate().toString().padStart(2, '0')
].join('-');

return { fromDate, toDate };
}
Expand Down

0 comments on commit d04b156

Please sign in to comment.