From 773ebca9a5b45b9320f3c9f32d33c9119b897af7 Mon Sep 17 00:00:00 2001 From: "anatoly.shipitz" Date: Wed, 2 Jul 2025 12:24:41 +0200 Subject: [PATCH 1/2] Refactor SQL query in TARGET_UNITS_QUERY for improved date handling - 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. --- workers/main/src/services/TargetUnit/queries.ts | 11 +++++++++-- .../WeeklyFinancialReportRepository.ts | 5 +++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/workers/main/src/services/TargetUnit/queries.ts b/workers/main/src/services/TargetUnit/queries.ts index ad9f2c6..7171d67 100644 --- a/workers/main/src/services/TargetUnit/queries.ts +++ b/workers/main/src/services/TargetUnit/queries.ts @@ -25,8 +25,15 @@ export const TARGET_UNITS_QUERY = `SELECT JOIN users AS u ON u.id = te.user_id WHERE g.type = 'Group' AND cv.customized_type = 'Principal' and cv.value = ? - AND te.spent_on BETWEEN DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) + 7 DAY) - AND DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) + 1 DAY) + AND te.spent_on BETWEEN + DATE_FORMAT( + DATE_SUB( + DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) + 1 DAY), + INTERVAL (MONTH(DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) + 1 DAY))-1)%3 MONTH + ), + '%Y-%m-01' + ) + AND DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) + 1 DAY) ) t GROUP BY group_id, diff --git a/workers/main/src/services/WeeklyFinancialReport/WeeklyFinancialReportRepository.ts b/workers/main/src/services/WeeklyFinancialReport/WeeklyFinancialReportRepository.ts index 88fdf3f..7b94f56 100644 --- a/workers/main/src/services/WeeklyFinancialReport/WeeklyFinancialReportRepository.ts +++ b/workers/main/src/services/WeeklyFinancialReport/WeeklyFinancialReportRepository.ts @@ -151,10 +151,11 @@ export class WeeklyFinancialReportRepository } private composeWeeklyReportTitle(currentDate: Date): string { + const quarter = Math.floor(currentDate.getMonth() / 3); const periodStart = new Date( currentDate.getFullYear(), - currentDate.getMonth(), - currentDate.getDate() - ((currentDate.getDay() + 6) % 7) - 7, + quarter * 3, + 1 ) .toISOString() .slice(0, 10); From 76cfefa600d93992a2ccb8013dad2e713214278f Mon Sep 17 00:00:00 2001 From: "anatoly.shipitz" Date: Wed, 2 Jul 2025 12:35:45 +0200 Subject: [PATCH 2/2] Refactor date handling in composeWeeklyReportTitle method - Simplified the construction of the periodStart date in the `composeWeeklyReportTitle` method by removing unnecessary line breaks, enhancing code readability. - This change contributes to cleaner code while maintaining the existing functionality of generating weekly financial report titles. These improvements streamline the codebase and support better maintainability of the financial reporting features. --- .../WeeklyFinancialReportRepository.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/workers/main/src/services/WeeklyFinancialReport/WeeklyFinancialReportRepository.ts b/workers/main/src/services/WeeklyFinancialReport/WeeklyFinancialReportRepository.ts index 7b94f56..cebfe68 100644 --- a/workers/main/src/services/WeeklyFinancialReport/WeeklyFinancialReportRepository.ts +++ b/workers/main/src/services/WeeklyFinancialReport/WeeklyFinancialReportRepository.ts @@ -152,11 +152,7 @@ export class WeeklyFinancialReportRepository private composeWeeklyReportTitle(currentDate: Date): string { const quarter = Math.floor(currentDate.getMonth() / 3); - const periodStart = new Date( - currentDate.getFullYear(), - quarter * 3, - 1 - ) + const periodStart = new Date(currentDate.getFullYear(), quarter * 3, 1) .toISOString() .slice(0, 10); const periodEnd = new Date(