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 7e7122e..e8bfd48 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 @@ -3924,13 +3924,25 @@ open class DashboardService( + " ) " // invoice data + " , invoice_data as ( " + + " with uninvoiced as ( " + + " select " + + " p.id as projectId, " + + " p.expectedTotalFee - sum(coalesce(i.issueAmount, 0)) as uninvoicedAmount " + + " from invoice i " + + " inner join project p on p.code = i.projectCode and p.status = 'On-going' " + + " where i.deleted = false " + + (if (args.containsKey("endDate")) " and i.invoiceDate <= :endDate " else "") + + " group by p.id, p.expectedTotalFee " + + " ) " + " select " + " p.id as projectId, " + " sum(coalesce(i.issueAmount, 0)) as invoicedAmount, " + " sum(coalesce(i.paidAmount, 0)) as paidAmount " + + " ,u.uninvoicedAmount " + " from invoice i " + " inner join project p on p.code = i.projectCode and p.status = 'On-going' " + " left join staff s on s.id = p.teamlead " + + " left join uninvoiced u on u.projectId = p.id " + " where i.deleted = false " + (if (args.containsKey("startDate") && args.containsKey("endDate")) " and i.invoiceDate between :startDate AND :endDate " else if (args.containsKey("endDate")) " and i.invoiceDate <= :endDate " @@ -3950,6 +3962,18 @@ open class DashboardService( else if (args.containsKey("endDate")) " and pe.issueDate <= :endDate " else "") + " group by pe.projectId " + + " ) " + // project_fee + + " , projectFee as ( " + + " select " + + " m.projectId, " + + " sum(mp.amount) as projectFee " + + " from milestone_payment mp " + + " left join milestone m on m.id = mp.milestoneId " + + (if (args.containsKey("startDate") && args.containsKey("endDate")) " where mp.`date` >= :startDate and mp.`date` <= :endDate " + else if (args.containsKey("endDate")) " where mp.`date` <= :endDate " + else "") + + " group by m.projectId " + " ) " + " select " + " * " @@ -3964,10 +3988,11 @@ open class DashboardService( + " c.name as customerName, " + " c.code as customerCode, " + " su.name as subsidiary, " - + " p.expectedTotalFee as totalFee, " - + " (p.expectedTotalFee - ifnull(p.subContractFee, 0)) * 0.8 as totalBudget, " + + " coalesce(pf.projectFee, 0) as totalFee, " + + " (coalesce(pf.projectFee, 0) - ifnull(p.subContractFee, 0)) * 0.8 as totalBudget, " + " coalesce(me.manhourExpense, 0) as manhourExpense, " + " coalesce(id.invoicedAmount, 0) as invoicedAmount, " +// + " coalesce(id.uninvoicedAmount, 0) as uninvoicedAmount, " + " coalesce(id.paidAmount, 0) as paidAmount, " + " coalesce(pe.projectExpense, 0) as projectExpense " + " from project p " @@ -3978,10 +4003,14 @@ open class DashboardService( + " left join manhourExpense me on me.projectId = p.id " + " left join invoice_data id on id.projectId = p.id " + " left join project_expense pe on pe.projectId = p.id " + + " left join projectFee pf on pf.projectId = p.id " + " where p.status = 'On-going' " + + " and p.deleted = false " + (if (args.containsKey("teamId")) "and s.teamId = :teamId" else "") + + (if (args.containsKey("endDate")) " and coalesce(p.actualStart, p.planStart) <= :endDate" else "") + " order by p.code " + " ) result ") + println(sql) return jdbcDao.queryForList(sql.toString(), args) }