diff --git a/src/main/java/com/ffii/tsms/modules/data/service/DashboardService.kt b/src/main/java/com/ffii/tsms/modules/data/service/DashboardService.kt index 91af2fa..d76813c 100644 --- a/src/main/java/com/ffii/tsms/modules/data/service/DashboardService.kt +++ b/src/main/java/com/ffii/tsms/modules/data/service/DashboardService.kt @@ -1813,75 +1813,86 @@ open class DashboardService( // + " ) AS combined_data" // + " ORDER BY" // + " parsed_date;" - " select" - + " ROW_NUMBER() OVER (ORDER BY parsed_date, income, expenditure) AS id," - + " formatted_date AS date," - + " COALESCE(ROUND(income, 2), 0) AS income," - + " COALESCE(ROUND(expenditure, 2), 0) AS expenditure," - + " ROUND(SUM(COALESCE(income, 0) - COALESCE(expenditure, 0)) OVER (ORDER BY parsed_date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), 2) AS balance," - + " CASE" - + " WHEN income > 0 THEN concat('Invoice Receipt: ',invoiceNo)" - + " ELSE 'Monthly Manpower Expenditure'" - + " END AS remarks" - + " FROM (" - + " SELECT" - + " DATE_FORMAT(i.receiptDate, '%Y-%m') AS parsed_date," - + " DATE_FORMAT(i.receiptDate, '%m/%y') AS formatted_date," - + " SUM(i.paidAmount) AS income," - + " NULL AS expenditure," - + " i.invoiceNo AS invoiceNo" - + " FROM" - + " project p" - + " LEFT JOIN invoice i ON p.code = i.projectCode and i.deleted = 0" //update invoice deletable - + " WHERE" - + " p.id IN (:projectIds)" - + " AND i.paidAmount IS NOT NULL" - + " GROUP BY" - + " parsed_date," - + " formatted_date," - + " i.invoiceNo" - + " UNION" - + " SELECT" - + " DATE_FORMAT(r.date, '%Y-%m') AS parsed_date," - + " DATE_FORMAT(r.date, '%m/%y') AS formatted_date," - + " NULL AS income," - + " SUM(r.expenditure) AS expenditure," - + " NULL AS paymentMilestone" - + " FROM (" - + " SELECT" - + " se.timesheet_record_date AS date," - + " (COALESCE(SUM(t3.normalConsumed), 0) * se.hourlyRate) + (COALESCE(SUM(t3.otConsumed), 0) * se.hourlyRate * 1.0) AS expenditure" - + " from (" - + " select" - + " t2.id as tid," - + " t2.recordDate as timesheet_record_date," - + " s.id as staff_id," - + " se.id as salary_effective_id," - + " se.salaryId," - + " se.startdate as salary_effective_start_date," - + " coalesce(se.enddate,NOW()) as salary_effective_end_date," - + " s2.hourlyRate as hourlyRate" - + " from project p2" - + " left join project_task pt ON p2.id = pt.project_id" - + " left join timesheet t2 on pt.id = t2.projectTaskId" - + " left join staff s on t2.staffId = s.id" - + " left join salary_effective se on t2.recordDate >= se.`date` and s.id = se.staffId" - + " left join salary s2 on se.salaryId = s2.salaryPoint" - + " where p2.status = 'On-going'" - + " and t2.id is not null" - + " AND p2.id IN (:projectIds)" - + " group by t2.id, s.id, se.id, se.salaryId,se.date, s2.hourlyRate" - + " ) as se" - + " left join timesheet t3 on se.tid = t3.id" - + " where t3.recordDate >= se.salary_effective_start_date and t3.recordDate <= se.salary_effective_end_date" - + " group by se.timesheet_record_date, se.hourlyRate" - + " ) AS r" - + " GROUP BY" - + " parsed_date," - + " formatted_date" - + " ) AS combined_data" - + " ORDER BY" - + " parsed_date" + "select" + + " ROW_NUMBER() OVER (ORDER BY parsed_date, income, expenditure, expense) AS id, " + + " formatted_date AS date, " + + " COALESCE(ROUND(income, 2), 0) AS income, " + + " COALESCE(ROUND(expenditure, 2), 0) AS expenditure, " + + " COALESCE(ROUND(expense, 2), 0) as expense, " + + " ROUND(SUM(COALESCE(income, 0) - COALESCE(expenditure, 0) - COALESCE(expense, 0)) OVER (ORDER BY parsed_date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), 2) AS balance, " + + " CASE " + + " WHEN income > 0 THEN concat('Invoice Receipt: ',invoiceNo) " + + " when expense > 0 THEN concat('Project Expense: ',invoiceNo) " + + " ELSE 'Monthly Manpower Expenditure' " + + " END AS remarks " + + " FROM ( " + + " SELECT " + + " DATE_FORMAT(pe.receiptDate, '%Y-%m') AS parsed_date, " + + " DATE_FORMAT(pe.receiptDate, '%m/%y') AS formatted_date, " + + " NULL AS income, " + + " NULL AS expenditure, " + + " SUM(pe.amount) AS expense, " + + " COALESCE(CAST(pe.expenseNo AS CHAR), 'no expenseNo') AS invoiceNo " + + " FROM project_expense pe " + + " WHERE pe.projectId IN (:projectIds) " + + " AND pe.deleted = false " + + " GROUP BY parsed_date, formatted_date, pe.expenseNo " + + " UNION " + + " SELECT " + + " DATE_FORMAT(i.receiptDate, '%Y-%m') AS parsed_date, " + + " DATE_FORMAT(i.receiptDate, '%m/%y') AS formatted_date, " + + " SUM(i.paidAmount) AS income, " + + " NULL AS expenditure, " + + " null AS expense, " + + " COALESCE(CAST(i.invoiceNo AS CHAR), 'No Invoice') AS invoiceNo " + + " FROM project p " + + " LEFT JOIN invoice i ON p.code = i.projectCode AND i.deleted = 0 " + + " WHERE p.id IN (:projectIds) " + + " AND i.paidAmount IS NOT NULL " + + " GROUP BY parsed_date, formatted_date, i.invoiceNo " + + " UNION " + + " SELECT " + + " DATE_FORMAT(r.date, '%Y-%m') AS parsed_date, " + + " DATE_FORMAT(r.date, '%m/%y') AS formatted_date, " + + " NULL AS income, " + + " SUM(r.expenditure) AS expenditure, " + + " null as expense " + + " , NULL AS invoiceNo " + + " FROM ( " + + " SELECT " + + " se.timesheet_record_date AS date, " + + " (COALESCE(SUM(t3.normalConsumed), 0) * se.hourlyRate) + (COALESCE(SUM(t3.otConsumed), 0) * se.hourlyRate * 1.0) AS expenditure " + + " from ( " + + " select " + + " t2.id as tid, " + + " t2.recordDate as timesheet_record_date, " + + " s.id as staff_id, " + + " se.id as salary_effective_id, " + + " se.salaryId, " + + " se.startdate as salary_effective_start_date, " + + " coalesce(se.enddate,NOW()) as salary_effective_end_date, " + + " s2.hourlyRate as hourlyRate " + + " from project p2 " + + " left join project_task pt ON p2.id = pt.project_id " + + " left join timesheet t2 on pt.id = t2.projectTaskId " + + " left join staff s on t2.staffId = s.id " + + " left join salary_effective se on t2.recordDate >= se.`date` and s.id = se.staffId " + + " left join salary s2 on se.salaryId = s2.salaryPoint " + + " where p2.status = 'On-going' " + + " and t2.id is not null " + + " AND p2.id IN (:projectIds) " + + " group by t2.id, s.id, se.id, se.salaryId,se.date, s2.hourlyRate " + + " ) as se " + + " left join timesheet t3 on se.tid = t3.id " + + " where t3.recordDate >= se.salary_effective_start_date and t3.recordDate <= se.salary_effective_end_date " + + " group by se.timesheet_record_date, se.hourlyRate " + + " ) AS r " + + " GROUP BY " + + " parsed_date, " + + " formatted_date " + + " ) AS combined_data " + + " ORDER BY " + + " parsed_date " ) return jdbcDao.queryForList(sql.toString(), args) @@ -1986,81 +1997,101 @@ open class DashboardService( // + " left join staff s on t.staffId = s.id" // + " left join salary s2 on s.salaryId = s2.salaryPoint" // + " where t.id is not null" - " select" - + " months.month as monthExpenditure," - + " coalesce (expenditure.recordMonth,'-') as recordMonth," - + " coalesce (expenditure.expenditure,0) as expenditure," - + " SUM(COALESCE(expenditure.expenditure, 0)) OVER (ORDER BY months.month) AS cumulativeExpenditure" - + " FROM (" - + " SELECT 01 AS month" - + " UNION" - + " SELECT 02" - + " UNION" - + " SELECT 03" - + " UNION" - + " SELECT 04" - + " UNION" - + " SELECT 05" - + " UNION" - + " SELECT 06" - + " UNION" - + " SELECT 07" - + " UNION" - + " SELECT 08" - + " UNION" - + " SELECT 09" - + " UNION" - + " SELECT 10" - + " UNION" - + " SELECT 11" - + " union" - + " select 12" - + " ) AS months" - + " left join(" - + " SELECT" - + " r.recordMonth as recordMonth," - + " sum(r.cumulativeExpenditure) as expenditure" - + " from(" - + " select" - + " se.recordMonth," - + " t3.id," - + " coalesce(sum(t3.normalConsumed),0) as normalConsumed," - + " coalesce(sum(t3.otConsumed),0) as otConsumed," - + " se.hourlyRate as hourlyRate," - + " (coalesce(sum(t3.normalConsumed),0) * se.hourlyRate) + (coalesce(sum(t3.otConsumed),0) * se.hourlyRate * 1.0) as cumulativeExpenditure" - + " from (" - + " select" - + " month(t2.recordDate) as recordMonth," - + " t2.id as tid," - + " t2.recordDate as timesheet_record_date," - + " s.id as staff_id," - + " se.id as salary_effective_id," - + " se.salaryId," - + " se.startdate as salary_effective_start_date," - + " coalesce(se.enddate,now()) as salary_effective_end_date," - + " s2.hourlyRate as hourlyRate" - + " from project p" - + " left join team t on p.teamLead = t.teamLead" - + " left join project_task pt on p.id = pt.project_id" - + " left join timesheet t2 on pt.id = t2.projectTaskId" - + " left join staff s on t2.staffId = s.id" - + " left join salary_effective se on t2.recordDate >= se.`date` and s.id = se.staffId" - + " left join salary s2 on se.salaryId = s2.salaryPoint" - + " where t.id is not null" - ) + "with e_cte as (" + + " select" + + " pe.projectId," + + " month(pe.receiptDate) as recordMonth," + + " sum(pe.amount) as expense" + + " from project_expense pe" + + " where pe.deleted = false" + + " and year(pe.receiptDate) = :year" + + " group by month(pe.receiptDate), pe.projectId" + + " )" + + " select" + + " months.month as monthExpenditure," + + " coalesce (expenditure.recordMonth,'-') as recordMonth," + + " coalesce (expenditure.expenditure,0) as expenditure," + + " SUM(COALESCE(expenditure.expenditure, 0)) OVER (ORDER BY months.month) AS cumulativeExpenditure" + + " FROM (" + + " SELECT 01 AS month" + + " UNION" + + " SELECT 02" + + " UNION" + + " SELECT 03" + + " UNION" + + " SELECT 04" + + " UNION" + + " SELECT 05" + + " UNION" + + " SELECT 06" + + " UNION" + + " SELECT 07" + + " UNION" + + " SELECT 08" + + " UNION" + + " SELECT 09" + + " UNION" + + " SELECT 10" + + " UNION" + + " SELECT 11" + + " union" + + " select 12" + + " ) AS months" + + " left join(" + + " select" + + " anotherResult.recordMonth," + + " anotherResult.expenditure + coalesce(ec.expense, 0) as expenditure," + + " coalesce(ec.expense, 0) as expense" + + " from (" + + " select" + + " group_concat(r.projectId) as projectId," + + " r.recordMonth as recordMonth," + + " sum(r.cumulativeExpenditure) as expenditure" + + " from(" + + " select" + + " se.projectId," + + " se.recordMonth," + + " t3.id," + + " coalesce(sum(t3.normalConsumed),0) as normalConsumed," + + " coalesce(sum(t3.otConsumed),0) as otConsumed," + + " se.hourlyRate as hourlyRate," + + " (coalesce(sum(t3.normalConsumed),0) * se.hourlyRate) + (coalesce(sum(t3.otConsumed),0) * se.hourlyRate * 1.0) as cumulativeExpenditure" + + " from (" + + " select" + + " p.id as projectId," + + " month(t2.recordDate) as recordMonth," + + " t2.id as tid," + + " t2.recordDate as timesheet_record_date," + + " s.id as staff_id," + + " se.id as salary_effective_id," + + " se.salaryId," + + " se.startdate as salary_effective_start_date," + + " coalesce(se.enddate,now()) as salary_effective_end_date," + + " s2.hourlyRate as hourlyRate" + + " from project p" + + " left join team t on p.teamLead = t.teamLead" + + " left join project_task pt on p.id = pt.project_id" + + " left join timesheet t2 on pt.id = t2.projectTaskId" + + " left join staff s on t2.staffId = s.id" + + " left join salary_effective se on t2.recordDate >= se.`date` and s.id = se.staffId" + + " left join salary s2 on se.salaryId = s2.salaryPoint" + + " where t.id is not null" + ) if (args != null) { if (args.containsKey("teamId")) sql.append(" AND t.id = :teamId") } - sql.append(" and year(t2.recordDate) = :year" - + " group by month(t2.recordDate),t2.id, s.id, se.id, se.salaryId,se.date, s2.hourlyRate" - + " ) as se" - + " left join timesheet t3 on se.tid = t3.id" - + " where t3.recordDate >= se.salary_effective_start_date and t3.recordDate <= se.salary_effective_end_date" - + " group by se.recordMonth, t3.id, se.hourlyRate" - + " ) as r" - + " group by r.recordMonth" - + " ) as expenditure on months.month = expenditure.recordMonth" + sql.append(" and year(t2.recordDate) = :year " + + " group by month(t2.recordDate),t2.id, s.id, se.id, se.salaryId,se.date, s2.hourlyRate " + + " ) as se " + + " left join timesheet t3 on se.tid = t3.id " + + " where t3.recordDate >= se.salary_effective_start_date and t3.recordDate <= se.salary_effective_end_date " + + " group by se.recordMonth, t3.id, se.hourlyRate " + + " ) as r " + + " group by r.recordMonth " + + " ) as anotherResult " + + " left join e_cte ec on FIND_IN_SET(ec.projectId, anotherResult.projectId) > 0 and ec.recordMonth = anotherResult.recordMonth " + + " ) as expenditure on months.month = expenditure.recordMonth " ) return jdbcDao.queryForList(sql.toString(), args)