diff --git a/src/reports/IncomeStatementReport.vue b/src/reports/IncomeStatementReport.vue index d6fcc7b..f3d135a 100644 --- a/src/reports/IncomeStatementReport.vue +++ b/src/reports/IncomeStatementReport.vue @@ -27,7 +27,7 @@
- @@ -69,22 +69,25 @@ } async function updateReport() { + const dayjsDt = dayjs(dt.value!); + const dayjsDtStart = dayjs(dtStart.value!); const reportDates = []; + for (let i = 0; i < comparePeriods.value; i++) { let thisReportDt, thisReportDtStart; // Get period start and end dates if (compareUnit.value === 'years') { - thisReportDt = dayjs(dt.value!).subtract(i, 'year'); - thisReportDtStart = dayjs(dtStart.value!).subtract(i, 'year'); + thisReportDt = dayjsDt.subtract(i, 'year'); + thisReportDtStart = dayjsDtStart.subtract(i, 'year'); } else if (compareUnit.value === 'months') { - if (dayjs(dt.value!).add(1, 'day').isSame(dayjs(dt.value!).set('date', 1).add(1, 'month'))) { + if (dayjsDt.add(1, 'day').isSame(dayjsDt.set('date', 1).add(1, 'month'))) { // If dt is the end of a calendar month, then fix each prior dt to be the end of the calendar month - thisReportDt = dayjs(dt.value!).subtract(i, 'month').set('date', 1).add(1, 'month').subtract(1, 'day'); - thisReportDtStart = dayjs(dtStart.value!).subtract(i, 'month'); + thisReportDt = dayjsDt.subtract(i, 'month').set('date', 1).add(1, 'month').subtract(1, 'day'); + thisReportDtStart = dayjsDtStart.subtract(i, 'month'); } else { - thisReportDt = dayjs(dt.value!).subtract(i, 'month'); - thisReportDtStart = dayjs(dtStart.value!).subtract(i, 'month'); + thisReportDt = dayjsDt.subtract(i, 'month'); + thisReportDtStart = dayjsDtStart.subtract(i, 'month'); } } else { throw new Error('Unexpected compareUnit'); @@ -97,4 +100,25 @@ } load(); + + function onCompareUnitChange() { + const dayjsDt = dayjs(dt.value!); + const dayjsDtStart = dayjs(dtStart.value!); + + if (compareUnit.value === 'years') { + if (dayjsDt.add(1, 'day').subtract(1, 'month').isSame(dayjsDtStart)) { + // Dates were previously set to one month - now compareUnit changed to years + // Automatically change dates to one year + dtStart.value = dayjsDt.add(1, 'day').subtract(1, 'year').format('YYYY-MM-DD'); + } + } else if (compareUnit.value === 'months') { + if (dayjsDt.add(1, 'day').subtract(1, 'year').isSame(dayjsDtStart)) { + // Dates were previously set to one year - now compareUnit changed to months + // Automatically change dates to one month + dtStart.value = dayjsDt.add(1, 'day').subtract(1, 'month').format('YYYY-MM-DD'); + } + } else { + throw new Error('Unexpected compareUnit'); + } + }