Skip to content

Commit c1b5b38

Browse files
Refactor SQL query for improved date range logic (#68)
- Updated the date range logic in the TARGET_UNITS_QUERY to calculate the start date of the quarter dynamically, ensuring accurate reporting for target units. - Adjusted the `composeWeeklyReportTitle` method to set the period start based on the current quarter, enhancing the clarity and accuracy of weekly financial reports. These changes improve the accuracy of financial data retrieval and reporting in the application.
1 parent 7825d40 commit c1b5b38

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

workers/main/src/services/TargetUnit/queries.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,15 @@ export const TARGET_UNITS_QUERY = `SELECT
2525
JOIN users AS u ON u.id = te.user_id
2626
WHERE g.type = 'Group'
2727
AND cv.customized_type = 'Principal' and cv.value = ?
28-
AND te.spent_on BETWEEN DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) + 7 DAY)
29-
AND DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) + 1 DAY)
28+
AND te.spent_on BETWEEN
29+
DATE_FORMAT(
30+
DATE_SUB(
31+
DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) + 1 DAY),
32+
INTERVAL (MONTH(DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) + 1 DAY))-1)%3 MONTH
33+
),
34+
'%Y-%m-01'
35+
)
36+
AND DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) + 1 DAY)
3037
) t
3138
GROUP BY
3239
group_id,

workers/main/src/services/WeeklyFinancialReport/WeeklyFinancialReportRepository.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,8 @@ export class WeeklyFinancialReportRepository
151151
}
152152

153153
private composeWeeklyReportTitle(currentDate: Date): string {
154-
const periodStart = new Date(
155-
currentDate.getFullYear(),
156-
currentDate.getMonth(),
157-
currentDate.getDate() - ((currentDate.getDay() + 6) % 7) - 7,
158-
)
154+
const quarter = Math.floor(currentDate.getMonth() / 3);
155+
const periodStart = new Date(currentDate.getFullYear(), quarter * 3, 1)
159156
.toISOString()
160157
.slice(0, 10);
161158
const periodEnd = new Date(

0 commit comments

Comments
 (0)