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 de1a206..28cca86 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 @@ -397,7 +397,26 @@ open class DashboardService( + " from team t" + " left join project p on t.teamLead = p.teamLead" + " left join customer c on p.customerId = c.id" - + " left join invoice i on p.code = i.projectCode" + + " left join (" + + " select" + + " c3.id as cid," + + " sum(i3.issueAmount) as issueAmount," + + " sum(i3.paidAmount) as paidAmount" + + " from team t3" + + " left join project p3 on t3.teamLead = p3.teamLead" + + " left join customer c3 on p3.customerId = c3.id" + + " left join invoice i3 on p3.code = i3.projectCode" + + " where t3.deleted = 0" + + " and p3.status = 'On-going'" + + ) + + if (args != null) { + if (args.containsKey("teamId")) + sql.append(" AND t3.id = :teamId"); + } + sql.append( " group by c3.id" + + " ) as i on i.cid = c.id" + " left join (" + " select" + " r.teamId as teamId," @@ -425,8 +444,8 @@ open class DashboardService( + " group by r.teamId, r.customerId" + " ) as expenditure on expenditure.teamId = t.id and expenditure.customerId = c.id" + " where t.deleted = 0" - + " and p.status = 'On-going'" - ) + + " and p.status = 'On-going'") + if (args != null) { if (args.containsKey("teamId")) sql.append(" AND t.id = :teamId"); @@ -635,7 +654,7 @@ open class DashboardService( + " coalesce (sum(i.paidAmount),0) as totalReceived," + " coalesce (sum(i.issueAmount) - sum(i.paidAmount),0) as receivable," + " coalesce (round(sum(p.expectedTotalFee)*0.8,2),0) as totalBudget," - + " coalesce (expenditure.expenditure) as totalExpenditure," + + " coalesce (expenditure.expenditure,0) as totalExpenditure," + " coalesce (sum(p.expectedTotalFee)*0.8 - expenditure.expenditure,0) as expenditureReceivable" + " from project p" + " left join invoice i on p.code = i.projectCode" @@ -737,60 +756,62 @@ open class DashboardService( } fun CashFlowLedger(args: Map): List> { val sql = StringBuilder("select" - + " ROW_NUMBER() OVER (ORDER BY date, income, expenditure) AS id," - + " 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 date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), 2) AS balance," - + " CASE" - + " WHEN income > 0 THEN paymentMilestone" - + " ELSE 'Monthly Manpower Expenditure'" - + " END AS remarks" - + " FROM" - + " (" - + " SELECT" - + " date_format(i.receiptDate, '%b %y') AS date," - + " sum(i.paidAmount) AS income," - + " NULL AS expenditure," - + " i.paymentMilestone AS paymentMilestone" - + " FROM" - + " project p" - + " LEFT JOIN invoice i ON p.code = i.projectCode" - + " WHERE" - + " p.id IN (:projectIds)" - + " AND i.paidAmount IS NOT NULL" - + " GROUP BY" - + " date_format(i.receiptDate, '%b %y')," - + " i.paymentMilestone" - + " UNION" - + " SELECT" - + " date_format(r.date, '%b %y') AS date," - + " NULL AS income," - + " sum(r.expenditure) AS expenditure," - + " NULL AS paymentMilestone" - + " FROM" - + " (" - + " SELECT" - + " t.recordDate AS date," - + " (COALESCE(SUM(t.normalConsumed), 0) * s2.hourlyRate) + (COALESCE(SUM(t.otConsumed), 0) * s2.hourlyRate * 1.0) AS expenditure" - + " FROM" - + " project p" - + " LEFT JOIN project_task pt ON p.id = pt.project_id" - + " LEFT JOIN timesheet t ON pt.id = t.projectTaskId" - + " LEFT JOIN staff s ON t.staffId = s.id" - + " LEFT JOIN salary s2 ON s.salaryId = s2.salaryPoint" - + " WHERE" - + " t.id IS NOT NULL" - + " AND p.id IN (:projectIds)" - + " GROUP BY" - + " s2.hourlyRate," - + " t.recordDate" - + " ) AS r" - + " GROUP BY" - + " date_format(r.date, '%b %y')" - + " ) AS combined_data" - + " ORDER BY" - + " date" + + " 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 paymentMilestone" + + " 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.paymentMilestone AS paymentMilestone" + + " FROM" + + " project p" + + " LEFT JOIN invoice i ON p.code = i.projectCode" + + " WHERE" + + " p.id IN (:projectIds)" + + " AND i.paidAmount IS NOT NULL" + + " GROUP BY" + + " parsed_date," + + " formatted_date," + + " i.paymentMilestone" + + " 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" + + " t.recordDate AS date," + + " (COALESCE(SUM(t.normalConsumed), 0) * s2.hourlyRate) + (COALESCE(SUM(t.otConsumed), 0) * s2.hourlyRate * 1.0) AS expenditure" + + " FROM" + + " project p" + + " LEFT JOIN project_task pt ON p.id = pt.project_id" + + " LEFT JOIN timesheet t ON pt.id = t.projectTaskId" + + " LEFT JOIN staff s ON t.staffId = s.id" + + " LEFT JOIN salary s2 ON s.salaryId = s2.salaryPoint" + + " WHERE" + + " t.id IS NOT NULL" + + " AND p.id IN (:projectIds)" + + " GROUP BY" + + " s2.hourlyRate," + + " t.recordDate" + + " ) AS r" + + " GROUP BY" + + " parsed_date," + + " formatted_date" + + " ) AS combined_data" + + " ORDER BY" + + " parsed_date;" ) return jdbcDao.queryForList(sql.toString(), args)