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 7a89093..18541d9 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 @@ -1983,6 +1983,78 @@ open class DashboardService( return jdbcDao.queryForList(sql.toString(), args) } + fun CompanyTeamCashFlowSQL(args: Map): List> { + val sql = StringBuilder("with projectExpense as (" + + " select " + + " month(pe.issueDate) as month, " + + " sum(pe.amount) as projectExpense " + + " from project_expense pe " + + " left join project p on p.code = pe.projectId " + + " left join staff s on s.id = p.teamLead " + + " where pe.deleted = false " + + " and year(pe.issueDate) = :year " + + (if (args["teamId"] != null) " and s.teamId = :teamId " else "") + + " group by month " + + " ) " + + " , invoice as ( " + + " select " + + " month(receiptDate) as month, " + + " sum(issueAmount) as income " + + " from invoice i " + + " left join project p on p.code = i.projectCode " + + " left join staff s on s.id = p.teamLead " + + " where i.deleted = false " + + " and year(i.receiptDate) = :year " + + (if (args["teamId"] != null) " and s.teamId = :teamId " else "") + + " group by month " + + " ) " + + " , manpowerExpense as ( " + + " with p_cte as ( " + + " select " + + " p.*, " + + " s.teamId " + + " from project p " + + " left join staff s on s.id = p.teamLead " + + " where p.deleted = false " + + (if (args["teamId"] != null) " and s.teamId = :teamId " else "") + + " ) " + + " select " + + " month(t.recordDate) as month, " + + " SUM( " + + " (COALESCE(t.normalConsumed, 0) + COALESCE(t.otConsumed, 0)) * " + + " COALESCE(sal.hourlyRate, sal2.hourlyRate) * " + + " CASE " + + " WHEN tl.teamId != p.teamId THEN 1.15 " + + " ELSE 1 " + + " END " + + " ) AS manpowerExpense " + + " from ( " + + " SELECT * " + + " FROM timesheet t " + + " where t.deleted = false " + + " and t.projectId is not null " + + " and year(t.recordDate) = :year " + + " ) t " + + " inner join p_cte p on p.id = t.projectId " + + " left join team_log tl on tl.staffId = t.staffId and t.recordDate >= tl.`from` AND (t.recordDate <= tl.`to` or tl.`to` is null) " + + " left JOIN salary_effective se ON se.staffId = t.staffId and t.recordDate between se.startDate AND se.endDate " + + " left join salary sal on sal.salaryPoint = se.salaryId " + + " left join staff s on s.id = t.staffId and se.id is null " + + " left join salary sal2 on sal2.salaryPoint = s.salaryId " + + " group by month " + + " ) " + + " select " + + " me.month, " + + " coalesce(me.manpowerExpense, 0) + coalesce(pe.projectExpense, 0) as expenditure, " + + " coalesce(i.income,0) as income, " + + " SUM(COALESCE(me.manpowerExpense, 0)) OVER (ORDER BY me.month) AS cumulativeExpenditure, " + + " SUM(COALESCE(i.income, 0)) OVER (ORDER BY me.month) AS cumulativeIncome " + + " from manpowerExpense me " + + " left join projectExpense pe on pe.month = me.month " + + " left join invoice i on i.month = me.month; " + ) + return jdbcDao.queryForList(sql.toString(), args) + } fun TeamCashFlowIncome(args: Map): List> { val sql = StringBuilder("select" + " months.month as monthInvoice," @@ -2844,7 +2916,7 @@ open class DashboardService( + " from staff s" + " left join staff_allocation sa on sa.staff_id = s.id" + " left join project p on sa.project_id = p.id" - + " left join timesheet t on p.id = t.projectId and s.id = t.staffId" + + " left join timesheet t on s.id = t.staffId" + " where s.id = :staffId" + " and t.recordDate >= :startdate" + " and t.recordDate <= last_day(:startdate)" @@ -3444,13 +3516,18 @@ open class DashboardService( + " from project p " + " left join staff s on s.id = p.teamLead " + " where p.deleted = false " + + " and p.status = 'On-going' " + " ) " + " select " + " projectId, " - + " CASE WHEN tl.teamId = p.teamId " - + " then sum((coalesce(t.normalConsumed, 0) + coalesce(t.otConsumed, 0)) * coalesce(sal.hourlyRate, sal2.hourlyRate)) " - + " else sum((coalesce(t.normalConsumed, 0) + coalesce(t.otConsumed, 0)) * coalesce(sal.hourlyRate, sal2.hourlyRate)) " - + " end as manhourExpense " + + " SUM( " + + " (COALESCE(t.normalConsumed, 0) + COALESCE(t.otConsumed, 0)) * " + + " COALESCE(sal.hourlyRate, sal2.hourlyRate) * " + + " CASE " + + " WHEN tl.teamId != p.teamId THEN 1 " + + " ELSE 1 " + + " END " + + " ) AS manhourExpense " + " from ( " + " SELECT * " + " FROM timesheet t " @@ -3461,12 +3538,12 @@ open class DashboardService( + " and t.projectId is not null " + " ) t " + " left join p_cte p on p.id = t.projectId " - + " left join team_log tl on tl.staffId = t.staffId and t.recordDate between tl.`from` AND tl.`to` " + + " left join team_log tl on tl.staffId = t.staffId and t.recordDate >= tl.`from` AND (t.recordDate <= tl.`to` or tl.`to` is null) " + " left JOIN salary_effective se ON se.staffId = t.staffId and t.recordDate between se.startDate AND se.endDate " + " left join salary sal on sal.salaryPoint = se.salaryId " + " left join staff s on s.id = t.staffId and se.id is null " + " left join salary sal2 on sal2.salaryPoint = s.salaryId " - + " GROUP BY t.projectId, tl.teamId, p.teamId " + + " GROUP BY t.projectId " + " ) " // invoice data + " , invoice_data as ( " @@ -3475,7 +3552,7 @@ open class DashboardService( + " sum(coalesce(i.issueAmount, 0)) as invoicedAmount, " + " sum(coalesce(i.paidAmount, 0)) as paidAmount " + " from invoice i " - + " left join project p on p.code = i.projectCode " + + " inner join project p on p.code = i.projectCode and p.status = 'On-going' " + " left join staff s on s.id = p.teamlead " + " where i.deleted = false " + (if (args.containsKey("startDate") && args.containsKey("endDate")) " and i.invoiceDate between :startDate AND :endDate " @@ -3489,7 +3566,7 @@ open class DashboardService( + " pe.projectId, " + " sum(amount) as projectExpense " + " from project_expense pe " - + " left join project p on p.id = pe.projectId " + + " left join project p on p.id = pe.projectId and p.status = 'On-going' " + " left join staff s on s.id = p.teamlead " + " where pe.deleted = false " + (if (args.containsKey("startDate") && args.containsKey("endDate")) " and pe.issueDate between :startDate AND :endDate " @@ -3525,7 +3602,7 @@ open class DashboardService( + " left join invoice_data id on id.projectId = p.id " + " left join project_expense pe on pe.projectId = p.id " + " where p.status = 'On-going' " - + (if (args.containsKey("teamId")) "where s.teamId = :teamId" else "") + + (if (args.containsKey("teamId")) "and s.teamId = :teamId" else "") + " order by p.id " + " ) result ") return jdbcDao.queryForList(sql.toString(), args) diff --git a/src/main/java/com/ffii/tsms/modules/data/web/DashboardController.kt b/src/main/java/com/ffii/tsms/modules/data/web/DashboardController.kt index c53a627..7088825 100644 --- a/src/main/java/com/ffii/tsms/modules/data/web/DashboardController.kt +++ b/src/main/java/com/ffii/tsms/modules/data/web/DashboardController.kt @@ -248,6 +248,18 @@ class DashboardController( } return dashboardService.CashFlowLedger(args) } + + @GetMapping("/getCompanyTeamCashFlow") + fun getCompanyTeamCashFlow(request: HttpServletRequest): List> { + val teamId = request.getParameter("teamId") + val year = request.getParameter("year") + val args = mutableMapOf( + "teamId" to teamId, + "year" to year, + ) + return dashboardService.CompanyTeamCashFlowSQL(args) + } + @GetMapping("/searchTeamCashFlow") fun searchTeamCashFlow(request: HttpServletRequest?): List> { val args = mutableMapOf()