Skip to content

Commit

Permalink
[AccxountReport] Fix date calculation
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 1, 2023
1 parent e86778f commit 098a9a9
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})

Check warning on line 244 in reports/AccountReport.ts

View workflow job for this annotation

GitHub Actions / setup_and_lint

Replace `day:·date.daysInMonth})` with `·day:·date.daysInMonth·});`
} else {
return date

Check warning on line 246 in reports/AccountReport.ts

View workflow job for this annotation

GitHub Actions / setup_and_lint

Insert `;`
}
}

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 })) },

Check warning on line 266 in reports/AccountReport.ts

View workflow job for this annotation

GitHub Actions / setup_and_lint

Replace `·toDate,·fromDate:·this._fixMonthsJump(toDate,·toDate.minus({·months·}))` with `⏎········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 })),

Check warning on line 278 in reports/AccountReport.ts

View workflow job for this annotation

GitHub Actions / setup_and_lint

Replace `toDate,·lastRange.fromDate.minus({·months·})` with `⏎··········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')

Check warning on line 460 in reports/AccountReport.ts

View workflow job for this annotation

GitHub Actions / setup_and_lint

Insert `,`
].join('-');

const toDate = [
toYear,
(fye.getMonth() + 1).toString().padStart(2, '0'),
fye.getDate().toString().padStart(2, '0')

Check warning on line 466 in reports/AccountReport.ts

View workflow job for this annotation

GitHub Actions / setup_and_lint

Insert `,`
].join('-');

return { fromDate, toDate };
}
Expand Down

0 comments on commit 098a9a9

Please sign in to comment.