From 7bcef47527b80a6f6653cf8c6f5cbde9aae4878a Mon Sep 17 00:00:00 2001 From: "Mac\\David" Date: Mon, 12 Aug 2024 15:01:47 +0800 Subject: [PATCH] update all cumulative expenditure api (dashboard) --- .../modules/data/service/DashboardService.kt | 1688 +++++++++++------ 1 file changed, 1112 insertions(+), 576 deletions(-) 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 88cfbd1..7a6cb09 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 @@ -564,75 +564,161 @@ open class DashboardService( fun searchFinancialSummaryCard(args: Map): List> { val sql = StringBuilder( - "select" - + " t.id as teamId," - + " t.name as teamName," - + " pj.projectNo as projectNo," - + " coalesce(pj.totalFee,0) as totalFee," - + " coalesce(pj.totalBudget,0) as totalBudget," - + " coalesce(sum(i.issueAmount),0) as totalInvoiced," - + " coalesce(pj.totalFee,0) - coalesce(sum(i.issueAmount),0) as unInvoiced," - + " coalesce(sum(i.paidAmount),0) as totalReceived," - + " round(expenditure.cumulativeExpenditure,2) as cumulativeExpenditure," - + " case" - + " when coalesce(round(sum(i.issueAmount) / (expenditure.cumulativeExpenditure),3),0) >= 1 then 'Positive'" - + " when coalesce(round(sum(i.issueAmount) / (expenditure.cumulativeExpenditure),3),0) < 1 then 'Negative'" - + " end as cashFlowStatus," - + " coalesce(round(sum(i.issueAmount) / (expenditure.cumulativeExpenditure),3),0) as cpi," - + " coalesce(round(coalesce(pj.totalFee,0) / (expenditure.cumulativeExpenditure),3),0) as projectedCpi," - + " case" - + " when coalesce(round(coalesce(pj.totalFee,0) / (expenditure.cumulativeExpenditure),3),0) >= 1 then 'Positive'" - + " when coalesce(round(coalesce(pj.totalFee,0) / (expenditure.cumulativeExpenditure),3),0) < 1 then 'Negative'" - + " end as projectedCashFlowStatus" - + " from team t" - + " left join (" - + " select" - + " t2.id as tid," - + " count(p.id) as projectNo," - + " sum(p.expectedTotalFee) as totalFee," - + " sum(round((p.expectedTotalFee - ifnull(p.subContractFee, 0)) * 0.8,2)) as totalBudget" - + " from team t2" - + " left join project p on t2.teamLead = p.teamLead" - + " where p.status = 'On-going'" - + " group by t2.id" - + " ) as pj on pj.tid = t.id" - + " left join (" - + " select" - + " t3.id as tid," - + " sum(i3.issueAmount) as issueAmount," - + " sum(i3.paidAmount) as paidAmount" - + " from team t3" - + " left join project p3 on t3.teamLead = p3.teamLead" - + " left join invoice i3 on p3.code = i3.projectCode" - + " where t3.deleted = 0" - + " and p3.status = 'On-going'" - + " group by t3.id" - + " ) as i on i.tid = t.id" - + " left join (" - + " select" - + " r.teamId as teamId," - + " sum(r.cumulativeExpenditure) as cumulativeExpenditure" - + " from (" - + " select" - + " t1.id as teamId," - + " (coalesce(sum(t2.normalConsumed),0) * s2.hourlyRate) + (coalesce(sum(t2.otConsumed),0) * s2.hourlyRate * 1.0) as cumulativeExpenditure," - + " s2.hourlyRate as hourlyRate," - + " sum(t2.normalConsumed) as normalConsumed," - + " sum(t2.otConsumed) as otConsumed" - + " from team t1" - + " left join project p2 on t1.teamLead = p2.teamLead" - + " 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 s2 on s.salaryId = s2.salaryPoint" - + " where p2.status = 'On-going'" - + " and t2.id is not null" - + " group by s2.hourlyRate,t1.id" - + " ) as r" - + " group by r.teamId" - + " ) as expenditure on expenditure.teamId = t.id" - + " where t.deleted = 0" - + " and pj.projectNo is not null" +// "select" +// + " t.id as teamId," +// + " t.name as teamName," +// + " pj.projectNo as projectNo," +// + " coalesce(pj.totalFee,0) as totalFee," +// + " coalesce(pj.totalBudget,0) as totalBudget," +// + " coalesce(sum(i.issueAmount),0) as totalInvoiced," +// + " coalesce(pj.totalFee,0) - coalesce(sum(i.issueAmount),0) as unInvoiced," +// + " coalesce(sum(i.paidAmount),0) as totalReceived," +// + " round(expenditure.cumulativeExpenditure,2) as cumulativeExpenditure," +// + " case" +// + " when coalesce(round(sum(i.issueAmount) / (expenditure.cumulativeExpenditure),3),0) >= 1 then 'Positive'" +// + " when coalesce(round(sum(i.issueAmount) / (expenditure.cumulativeExpenditure),3),0) < 1 then 'Negative'" +// + " end as cashFlowStatus," +// + " coalesce(round(sum(i.issueAmount) / (expenditure.cumulativeExpenditure),3),0) as cpi," +// + " coalesce(round(coalesce(pj.totalFee,0) / (expenditure.cumulativeExpenditure),3),0) as projectedCpi," +// + " case" +// + " when coalesce(round(coalesce(pj.totalFee,0) / (expenditure.cumulativeExpenditure),3),0) >= 1 then 'Positive'" +// + " when coalesce(round(coalesce(pj.totalFee,0) / (expenditure.cumulativeExpenditure),3),0) < 1 then 'Negative'" +// + " end as projectedCashFlowStatus" +// + " from team t" +// + " left join (" +// + " select" +// + " t2.id as tid," +// + " count(p.id) as projectNo," +// + " sum(p.expectedTotalFee) as totalFee," +// + " sum(round((p.expectedTotalFee - ifnull(p.subContractFee, 0)) * 0.8,2)) as totalBudget" +// + " from team t2" +// + " left join project p on t2.teamLead = p.teamLead" +// + " where p.status = 'On-going'" +// + " group by t2.id" +// + " ) as pj on pj.tid = t.id" +// + " left join (" +// + " select" +// + " t3.id as tid," +// + " sum(i3.issueAmount) as issueAmount," +// + " sum(i3.paidAmount) as paidAmount" +// + " from team t3" +// + " left join project p3 on t3.teamLead = p3.teamLead" +// + " left join invoice i3 on p3.code = i3.projectCode" +// + " where t3.deleted = 0" +// + " and p3.status = 'On-going'" +// + " group by t3.id" +// + " ) as i on i.tid = t.id" +// + " left join (" +// + " select" +// + " r.teamId as teamId," +// + " sum(r.cumulativeExpenditure) as cumulativeExpenditure" +// + " from (" +// + " select" +// + " t1.id as teamId," +// + " (coalesce(sum(t2.normalConsumed),0) * s2.hourlyRate) + (coalesce(sum(t2.otConsumed),0) * s2.hourlyRate * 1.0) as cumulativeExpenditure," +// + " s2.hourlyRate as hourlyRate," +// + " sum(t2.normalConsumed) as normalConsumed," +// + " sum(t2.otConsumed) as otConsumed" +// + " from team t1" +// + " left join project p2 on t1.teamLead = p2.teamLead" +// + " 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 s2 on s.salaryId = s2.salaryPoint" +// + " where p2.status = 'On-going'" +// + " and t2.id is not null" +// + " group by s2.hourlyRate,t1.id" +// + " ) as r" +// + " group by r.teamId" +// + " ) as expenditure on expenditure.teamId = t.id" +// + " where t.deleted = 0" +// + " and pj.projectNo is not null" + " select" + + " t.id as teamId," + + " t.name as teamName," + + " pj.projectNo as projectNo," + + " coalesce(pj.totalFee,0) as totalFee," + + " coalesce(pj.totalBudget,0) as totalBudget," + + " coalesce(sum(i.issueAmount),0) as totalInvoiced," + + " coalesce(pj.totalFee,0) - coalesce(sum(i.issueAmount),0) as unInvoiced," + + " coalesce(sum(i.paidAmount),0) as totalReceived," + + " round(expenditure.cumulativeExpenditure,2) as cumulativeExpenditure," + + " case" + + " when coalesce(round(sum(i.issueAmount) / (expenditure.cumulativeExpenditure),3),0) >= 1 then 'Positive'" + + " when coalesce(round(sum(i.issueAmount) / (expenditure.cumulativeExpenditure),3),0) < 1 then 'Negative'" + + " end as cashFlowStatus," + + " coalesce(round(sum(i.issueAmount) / (expenditure.cumulativeExpenditure),3),0) as cpi," + + " coalesce(round(coalesce(pj.totalFee,0) / (expenditure.cumulativeExpenditure),3),0) as projectedCpi," + + " case" + + " when coalesce(round(coalesce(pj.totalFee,0) / (expenditure.cumulativeExpenditure),3),0) >= 1 then 'Positive'" + + " when coalesce(round(coalesce(pj.totalFee,0) / (expenditure.cumulativeExpenditure),3),0) < 1 then 'Negative'" + + " end as projectedCashFlowStatus" + + " from team t" + + " left join (" + + " select" + + " t2.id as tid," + + " count(p.id) as projectNo," + + " sum(p.expectedTotalFee) as totalFee," + + " sum(round((p.expectedTotalFee - ifnull(p.subContractFee, 0)) * 0.8,2)) as totalBudget" + + " from team t2" + + " left join project p on t2.teamLead = p.teamLead" + + " where p.status = 'On-going'" + + " group by t2.id" + + " ) as pj on pj.tid = t.id" + + " left join (" + + " select" + + " t3.id as tid," + + " sum(i3.issueAmount) as issueAmount," + + " sum(i3.paidAmount) as paidAmount" + + " from team t3" + + " left join project p3 on t3.teamLead = p3.teamLead" + + " left join invoice i3 on p3.code = i3.projectCode" + + " where t3.deleted = 0" + + " and p3.status = 'On-going'" + + " group by t3.id" + + " ) as i on i.tid = t.id" + + " left join (" + + " select" + + " r.teamId as teamId," + + " sum(r.cumulativeExpenditure) as cumulativeExpenditure" + + " from (" + + " select" + + " se.teamId," + + " 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" + + " t1.id as teamId," + + " 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 team t1" + + " left join project p2 on t1.teamLead = p2.teamLead" + + " 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" + + " group by t1.id, 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.teamId, t3.id, se.hourlyRate" + + " ) as r" + + " group by r.teamId" + + " ) as expenditure on expenditure.teamId = t.id" + + " where t.deleted = 0" + + " and pj.projectNo is not null" ) if (viewDashboardAuthority() == "self") { @@ -647,55 +733,124 @@ open class DashboardService( return jdbcDao.queryForList(sql.toString(), args) } fun searchFinancialSummaryAllTeamCard(args: Map): List> { - val sql = StringBuilder( "select DISTINCT" - + " 'All Team' as teamName," - + " count(p.code) as projectNo," - + " sum(p.expectedTotalFee) as totalFee," - + " round((sum(p.expectedTotalFee) - sum(ifnull(p.subContractFee, 0))) * 0.8,2) as totalBudget," - + " round(expenditure.cumulativeExpenditure,2) as cumulativeExpenditure," - + " sum(i.issueAmount) as totalInvoiced," - + " sum(p.expectedTotalFee) - sum(i.issueAmount) as unInvoiced," - + " sum(i.paidAmount) as totalReceived," - + " case" - + " when round(sum(i.issueAmount) / (expenditure.cumulativeExpenditure),3) >= 1 then 'Positive'" - + " when round(sum(i.issueAmount) / (expenditure.cumulativeExpenditure),3) < 1 then 'Negative'" - + " end as cashFlowStatus," - + " round(sum(i.issueAmount) / (expenditure.cumulativeExpenditure),3) as cpi," - + " round(sum(p.expectedTotalFee) / (expenditure.cumulativeExpenditure),3) as projectedCpi," - + " case" - + " when round(sum(p.expectedTotalFee) / (expenditure.cumulativeExpenditure),3) >= 1 then 'Positive'" - + " when round(sum(p.expectedTotalFee) / (expenditure.cumulativeExpenditure),3) < 1 then 'Negative'" - + " end as projectedCashFlowStatus" - + " from project p" - + " left join (" - + " select" - + " p3.id as pid," - + " sum(i3.issueAmount) as issueAmount," - + " sum(i3.paidAmount) as paidAmount" - + " from project p3" - + " left join invoice i3 on p3.code = i3.projectCode" - + " where p3.deleted = 0" - + " and p3.status = 'On-going'" - + " group by p3.id" - + " ) as i on i.pid = p.id" - + " left join (" - + " select" - + " sum(r.cumulativeExpenditure) as cumulativeExpenditure" - + " from(" - + " select" - + " (coalesce(sum(t2.normalConsumed),0) * s2.hourlyRate) + (coalesce(sum(t2.otConsumed),0) * s2.hourlyRate * 1.0) as cumulativeExpenditure" - + " 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 s2 on s.salaryId = s2.salaryPoint" - + " where p2.status = 'On-going'" - + " and t2.id is not null" - + " group by s2.hourlyRate" - + " ) as r" - + " ) as expenditure on 1=1." - + " where p.status = 'On-going'" - + " group by expenditure.cumulativeExpenditure" + val sql = StringBuilder( +// "select DISTINCT" +// + " 'All Team' as teamName," +// + " count(p.code) as projectNo," +// + " sum(p.expectedTotalFee) as totalFee," +// + " round((sum(p.expectedTotalFee) - sum(ifnull(p.subContractFee, 0))) * 0.8,2) as totalBudget," +// + " round(expenditure.cumulativeExpenditure,2) as cumulativeExpenditure," +// + " sum(i.issueAmount) as totalInvoiced," +// + " sum(p.expectedTotalFee) - sum(i.issueAmount) as unInvoiced," +// + " sum(i.paidAmount) as totalReceived," +// + " case" +// + " when round(sum(i.issueAmount) / (expenditure.cumulativeExpenditure),3) >= 1 then 'Positive'" +// + " when round(sum(i.issueAmount) / (expenditure.cumulativeExpenditure),3) < 1 then 'Negative'" +// + " end as cashFlowStatus," +// + " round(sum(i.issueAmount) / (expenditure.cumulativeExpenditure),3) as cpi," +// + " round(sum(p.expectedTotalFee) / (expenditure.cumulativeExpenditure),3) as projectedCpi," +// + " case" +// + " when round(sum(p.expectedTotalFee) / (expenditure.cumulativeExpenditure),3) >= 1 then 'Positive'" +// + " when round(sum(p.expectedTotalFee) / (expenditure.cumulativeExpenditure),3) < 1 then 'Negative'" +// + " end as projectedCashFlowStatus" +// + " from project p" +// + " left join (" +// + " select" +// + " p3.id as pid," +// + " sum(i3.issueAmount) as issueAmount," +// + " sum(i3.paidAmount) as paidAmount" +// + " from project p3" +// + " left join invoice i3 on p3.code = i3.projectCode" +// + " where p3.deleted = 0" +// + " and p3.status = 'On-going'" +// + " group by p3.id" +// + " ) as i on i.pid = p.id" +// + " left join (" +// + " select" +// + " sum(r.cumulativeExpenditure) as cumulativeExpenditure" +// + " from(" +// + " select" +// + " (coalesce(sum(t2.normalConsumed),0) * s2.hourlyRate) + (coalesce(sum(t2.otConsumed),0) * s2.hourlyRate * 1.0) as cumulativeExpenditure" +// + " 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 s2 on s.salaryId = s2.salaryPoint" +// + " where p2.status = 'On-going'" +// + " and t2.id is not null" +// + " group by s2.hourlyRate" +// + " ) as r" +// + " ) as expenditure on 1=1." +// + " where p.status = 'On-going'" +// + " group by expenditure.cumulativeExpenditure" + " select DISTINCT" + + " 'All Team' as teamName," + + " count(p.code) as projectNo," + + " sum(p.expectedTotalFee) as totalFee," + + " round((sum(p.expectedTotalFee) - sum(ifnull(p.subContractFee, 0))) * 0.8,2) as totalBudget," + + " round(expenditure.cumulativeExpenditure,2) as cumulativeExpenditure," + + " sum(i.issueAmount) as totalInvoiced," + + " sum(p.expectedTotalFee) - sum(i.issueAmount) as unInvoiced," + + " sum(i.paidAmount) as totalReceived," + + " case" + + " when round(sum(i.issueAmount) / (expenditure.cumulativeExpenditure),3) >= 1 then 'Positive'" + + " when round(sum(i.issueAmount) / (expenditure.cumulativeExpenditure),3) < 1 then 'Negative'" + + " end as cashFlowStatus," + + " round(sum(i.issueAmount) / (expenditure.cumulativeExpenditure),3) as cpi," + + " round(sum(p.expectedTotalFee) / (expenditure.cumulativeExpenditure),3) as projectedCpi," + + " case" + + " when round(sum(p.expectedTotalFee) / (expenditure.cumulativeExpenditure),3) >= 1 then 'Positive'" + + " when round(sum(p.expectedTotalFee) / (expenditure.cumulativeExpenditure),3) < 1 then 'Negative'" + + " end as projectedCashFlowStatus" + + " from project p" + + " left join (" + + " select" + + " p3.id as pid," + + " sum(i3.issueAmount) as issueAmount," + + " sum(i3.paidAmount) as paidAmount" + + " from project p3" + + " left join invoice i3 on p3.code = i3.projectCode" + + " where p3.deleted = 0" + + " and p3.status = 'On-going'" + + " group by p3.id" + + " ) as i on i.pid = p.id" + + " left join (" + + " select" + + " sum(r.cumulativeExpenditure) as cumulativeExpenditure" + + " from(" + + " select " + + " 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" + + " 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" + + " 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 t3.id, se.hourlyRate" + + " ) as r" + + " ) as expenditure on 1=1." + + " where p.status = 'On-going'" + + " group by expenditure.cumulativeExpenditure" ) return jdbcDao.queryForList(sql.toString(), args) @@ -708,35 +863,41 @@ open class DashboardService( // + " end as totalUninvoiced" fun searchFinancialSummaryByClient(args: Map): List> { -// val sql = StringBuilder( "select" -// + " ROW_NUMBER() OVER (ORDER BY t.id, c.id) AS id," + val sql = StringBuilder( +// "select" +// + " ROW_NUMBER() OVER (ORDER BY t.id, i.cid) AS id," // + " t.id as teamId," -// + " c.id as cid," -// + " c.code as customerCode," -// + " c.name as customerName," -// + " count(p.name) as projectNo," -// + " sum(p.expectedTotalFee) as totalFee," -// + " round(sum(p.expectedTotalFee) * 0.8,2) as totalBudget," +// + " i.cid as cid," +// + " i.customerCode as customerCode," +// + " i.customerName as customerName," +// + " p.projectNo as projectNo," +// + " p.totalFee as totalFee," +// + " p.totalBudget as totalBudget," // + " COALESCE(round(expenditure.cumulativeExpenditure,2),0) as cumulativeExpenditure," // + " coalesce(sum(i.issueAmount),0) as totalInvoiced," // + " coalesce(sum(i.paidAmount),0) as totalReceived," // + " case" -// + " when coalesce(round(sum(i.issueAmount) / (COALESCE(expenditure.cumulativeExpenditure,0)),2),0) >= 1 then 'Positive'" -// + " when coalesce(round(sum(i.issueAmount) / (COALESCE(expenditure.cumulativeExpenditure,0)),2),0) < 1 then 'Negative'" +// + " when coalesce(round(sum(i.issueAmount) / (COALESCE(expenditure.cumulativeExpenditure,0)),3),0) >= 1 then 'Positive'" +// + " when coalesce(round(sum(i.issueAmount) / (COALESCE(expenditure.cumulativeExpenditure,0)),3),0) < 1 then 'Negative'" // + " end as cashFlowStatus," -// + " coalesce(round(sum(i.issueAmount) / (COALESCE(expenditure.cumulativeExpenditure,0)),2),0) as cpi," +// + " coalesce(round(sum(i.issueAmount) / (COALESCE(expenditure.cumulativeExpenditure,0)),3),0) as cpi," +// + " coalesce(round(p.totalFee / (COALESCE(expenditure.cumulativeExpenditure,0)),3),0) as projectedCpi," // + " case" -// + " when coalesce(round(sum(p.expectedTotalFee) * 0.8,2),0) <= COALESCE(expenditure.cumulativeExpenditure,0) then coalesce(round(sum(p.expectedTotalFee) * 0.8,2),0) - coalesce(sum(i.issueAmount),0)" -// + " when coalesce(round(sum(p.expectedTotalFee) * 0.8,2),0) > COALESCE(expenditure.cumulativeExpenditure,0) and COALESCE(expenditure.cumulativeExpenditure,0) < coalesce(sum(i.issueAmount),0) then 0" -// + " when coalesce(round(sum(p.expectedTotalFee) * 0.8,2),0) > COALESCE(expenditure.cumulativeExpenditure,0) and COALESCE(expenditure.cumulativeExpenditure,0) >= coalesce(sum(i.issueAmount),0) then COALESCE(expenditure.cumulativeExpenditure,0) - coalesce(sum(i.issueAmount),0)" +// + " when coalesce(round(p.totalFee / (COALESCE(expenditure.cumulativeExpenditure,0)),3),0) >= 1 then 'Positive'" +// + " when coalesce(round(p.totalFee / (COALESCE(expenditure.cumulativeExpenditure,0)),3),0) < 1 then 'Negative'" +// + " end as projectedCashFlowStatus," +// + " case" +// + " when p.totalFee - sum(i.issueAmount) >= 0 then coalesce(round(p.totalFee - sum(i.issueAmount),2),0)" +// + " when p.totalFee - sum(i.issueAmount) < 0 then 0" +// + " when p.totalFee - sum(i.issueAmount) is null then 0" // + " end as totalUninvoiced" // + " from team t" -// + " left join project p on t.teamLead = p.teamLead" -// + " left join customer c on p.customerId = c.id" // + " left join (" // + " select" // + " t3.id as tid," // + " c3.id as cid," +// + " c3.code as customerCode," +// + " c3.name as customerName," // + " sum(i3.issueAmount) as issueAmount," // + " sum(i3.paidAmount) as paidAmount" // + " from team t3" @@ -745,15 +906,90 @@ open class DashboardService( // + " 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, t3.id" -// + " ) as i on i.cid = c.id and i.tid = t.id" + " select" + + " ROW_NUMBER() OVER (ORDER BY t.id, i.cid) AS id," + + " t.id as teamId," + + " i.cid as cid," + + " i.customerCode as customerCode," + + " i.customerName as customerName," + + " p.projectNo as projectNo," + + " p.totalFee as totalFee," + + " p.totalBudget as totalBudget," + + " COALESCE(round(expenditure.cumulativeExpenditure,2),0) as cumulativeExpenditure," + + " coalesce(sum(i.issueAmount),0) as totalInvoiced," + + " coalesce(sum(i.paidAmount),0) as totalReceived," + + " case" + + " when coalesce(round(sum(i.issueAmount) / (COALESCE(expenditure.cumulativeExpenditure,0)),3),0) >= 1 then 'Positive'" + + " when coalesce(round(sum(i.issueAmount) / (COALESCE(expenditure.cumulativeExpenditure,0)),3),0) < 1 then 'Negative'" + + " end as cashFlowStatus," + + " coalesce(round(sum(i.issueAmount) / (COALESCE(expenditure.cumulativeExpenditure,0)),3),0) as cpi," + + " coalesce(round(p.totalFee / (COALESCE(expenditure.cumulativeExpenditure,0)),3),0) as projectedCpi," + + " case" + + " when coalesce(round(p.totalFee / (COALESCE(expenditure.cumulativeExpenditure,0)),3),0) >= 1 then 'Positive'" + + " when coalesce(round(p.totalFee / (COALESCE(expenditure.cumulativeExpenditure,0)),3),0) < 1 then 'Negative'" + + " end as projectedCashFlowStatus," + + " case" + + " when p.totalFee - sum(i.issueAmount) >= 0 then coalesce(round(p.totalFee - sum(i.issueAmount),2),0)" + + " when p.totalFee - sum(i.issueAmount) < 0 then 0" + + " when p.totalFee - sum(i.issueAmount) is null then 0" + + " end as totalUninvoiced" + + " from team t" + + " left join (" + + " select" + + " t3.id as tid," + + " c3.id as cid," + + " c3.code as customerCode," + + " c3.name as customerName," + + " 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, t3.id, customerCode, customerName" +// + " ) as i on i.tid = t.id" +// + " left join (" +// + " select" +// + " t.id as tid," +// + " c.id as cid," +// + " count(p.id) as projectNo," +// + " sum(p.expectedTotalFee) as totalFee," +// + " round((sum(p.expectedTotalFee) - sum(ifnull(p.subContractFee, 0))) * 0.8,2) as totalBudget" +// + " from team t" +// + " left join project p on t.teamLead = p.teamLead" +// + " left join customer c on p.customerId = c.id" +// + " where t.deleted = 0" +// + " and p.status = 'On-going'" + " group by c3.id, t3.id, customerCode, customerName" + + " ) as i on i.tid = t.id" + + " left join (" + + " select" + + " t.id as tid," + + " c.id as cid," + + " count(p.id) as projectNo," + + " sum(p.expectedTotalFee) as totalFee," + + " round((sum(p.expectedTotalFee) - sum(ifnull(p.subContractFee, 0))) * 0.8,2) as totalBudget" + + " from team t" + + " left join project p on t.teamLead = p.teamLead" + + " left join customer c on p.customerId = c.id" + + " where t.deleted = 0" + + " and p.status = 'On-going'" + ) + if (args != null) { + if (args.containsKey("teamId")) + sql.append(" AND t.id = :teamId"); + } + sql.append( +// " group by t.id, c.id" +// + " ) as p on p.tid = t.id and p.cid = i.cid" // + " left join (" // + " select" // + " r.teamId as teamId," @@ -779,114 +1015,57 @@ open class DashboardService( // + " group by s2.hourlyRate,t1.id,c2.id" // + " ) as r" // + " group by r.teamId, r.customerId" -// + " ) as expenditure on expenditure.teamId = t.id and expenditure.customerId = c.id" +// + " ) as expenditure on expenditure.teamId = t.id and expenditure.customerId = i.cid" // + " where t.deleted = 0" -// + " and p.status = 'On-going'") -// -// if (args != null) { -// if (args.containsKey("teamId")) -// sql.append(" AND t.id = :teamId"); -// } -// sql.append(" group by t.id, c.id, c.code, c.name") - val sql = StringBuilder( - "select" - + " ROW_NUMBER() OVER (ORDER BY t.id, i.cid) AS id," - + " t.id as teamId," - + " i.cid as cid," - + " i.customerCode as customerCode," - + " i.customerName as customerName," - + " p.projectNo as projectNo," - + " p.totalFee as totalFee," - + " p.totalBudget as totalBudget," - + " COALESCE(round(expenditure.cumulativeExpenditure,2),0) as cumulativeExpenditure," - + " coalesce(sum(i.issueAmount),0) as totalInvoiced," - + " coalesce(sum(i.paidAmount),0) as totalReceived," - + " case" - + " when coalesce(round(sum(i.issueAmount) / (COALESCE(expenditure.cumulativeExpenditure,0)),3),0) >= 1 then 'Positive'" - + " when coalesce(round(sum(i.issueAmount) / (COALESCE(expenditure.cumulativeExpenditure,0)),3),0) < 1 then 'Negative'" - + " end as cashFlowStatus," - + " coalesce(round(sum(i.issueAmount) / (COALESCE(expenditure.cumulativeExpenditure,0)),3),0) as cpi," - + " coalesce(round(p.totalFee / (COALESCE(expenditure.cumulativeExpenditure,0)),3),0) as projectedCpi," - + " case" - + " when coalesce(round(p.totalFee / (COALESCE(expenditure.cumulativeExpenditure,0)),3),0) >= 1 then 'Positive'" - + " when coalesce(round(p.totalFee / (COALESCE(expenditure.cumulativeExpenditure,0)),3),0) < 1 then 'Negative'" - + " end as projectedCashFlowStatus," - + " case" - + " when p.totalFee - sum(i.issueAmount) >= 0 then coalesce(round(p.totalFee - sum(i.issueAmount),2),0)" - + " when p.totalFee - sum(i.issueAmount) < 0 then 0" - + " when p.totalFee - sum(i.issueAmount) is null then 0" - + " end as totalUninvoiced" - + " from team t" - + " left join (" - + " select" - + " t3.id as tid," - + " c3.id as cid," - + " c3.code as customerCode," - + " c3.name as customerName," - + " 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, t3.id, customerCode, customerName" - + " ) as i on i.tid = t.id" - + " left join (" - + " select" - + " t.id as tid," - + " c.id as cid," - + " count(p.id) as projectNo," - + " sum(p.expectedTotalFee) as totalFee," - + " round((sum(p.expectedTotalFee) - sum(ifnull(p.subContractFee, 0))) * 0.8,2) as totalBudget" - + " from team t" - + " left join project p on t.teamLead = p.teamLead" - + " left join customer c on p.customerId = c.id" - + " where t.deleted = 0" - + " and p.status = 'On-going'" - ) - if (args != null) { - if (args.containsKey("teamId")) - sql.append(" AND t.id = :teamId"); - } - sql.append( - " group by t.id, c.id" - + " ) as p on p.tid = t.id and p.cid = i.cid" - + " left join (" - + " select" - + " r.teamId as teamId," - + " r.customerId as customerId," - + " sum(r.cumulativeExpenditure) as cumulativeExpenditure" - + " from (" - + " select" - + " t1.id as teamId," - + " c2.id as customerId," - + " (coalesce(sum(t2.normalConsumed),0) * s2.hourlyRate) + (coalesce(sum(t2.otConsumed),0) * s2.hourlyRate * 1.0) as cumulativeExpenditure," - + " s2.hourlyRate as hourlyRate," - + " sum(t2.normalConsumed) as normalConsumed," - + " sum(t2.otConsumed) as otConsumed" - + " from team t1" - + " left join project p2 on t1.teamLead = p2.teamLead" - + " left join customer c2 on p2.customerId = c2 .id" - + " 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 s2 on s.salaryId = s2.salaryPoint" - + " where p2.status = 'On-going'" - + " and t2.id is not null" - + " group by s2.hourlyRate,t1.id,c2.id" - + " ) as r" - + " group by r.teamId, r.customerId" - + " ) as expenditure on expenditure.teamId = t.id and expenditure.customerId = i.cid" - + " where t.deleted = 0" - + " and i.cid is not null" +// + " and i.cid is not null" + " group by t.id, c.id" + + " ) as p on p.tid = t.id and p.cid = i.cid" + + " left join (" + + " select" + + " r.teamId as teamId," + + " r.customerId as customerId," + + " sum(r.cumulativeExpenditure) as cumulativeExpenditure" + + " from (" + + " select" + + " se.teamId," + + " se.customerId," + + " 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" + + " t1.id as teamId," + + " c2.id as customerId," + + " 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 team t1" + + " left join project p2 on t1.teamLead = p2.teamLead" + + " left join customer c2 on p2.customerId = c2 .id" + + " 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" + + " group by t1.id,c2.id,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 t3.id, se.hourlyRate,se.teamId,se.customerId" + + " ) as r" + + " group by r.teamId, r.customerId" + + " ) as expenditure on expenditure.teamId = t.id and expenditure.customerId = i.cid" + + " where t.deleted = 0" + + " and i.cid is not null" ) if (args != null) { if (args.containsKey("teamId")) @@ -897,73 +1076,150 @@ open class DashboardService( return jdbcDao.queryForList(sql.toString(), args) } fun searchFinancialSummaryByProject(args: Map): List> { - val sql = StringBuilder( "select" - + " t.id as teamId," - + " p.id as id," - + " p.code as projectCode," - + " p.name as projectName," - + " c.name as customerName," - + " s3.name as subsidiaryName," - + " p.expectedTotalFee as totalFee," - + " round((p.expectedTotalFee - ifnull(p.subContractFee, 0)) * 0.8,2) as totalBudget," - + " COALESCE(round(expenditure.cumulativeExpenditure,2),0) as cumulativeExpenditure," - + " coalesce(sum(i.issueAmount),0) as totalInvoiced," - + " coalesce(sum(i.paidAmount),0) as totalReceived," - + " case" - + " when coalesce(round(sum(i.issueAmount) / (COALESCE(expenditure.cumulativeExpenditure,0)),3),0) >= 1 then 'Positive'" - + " when coalesce(round(sum(i.issueAmount) / (COALESCE(expenditure.cumulativeExpenditure,0)),3),0) < 1 then 'Negative'" - + " end as cashFlowStatus," - + " coalesce(round(sum(i.issueAmount) / (COALESCE(expenditure.cumulativeExpenditure,0)),3),0) as cpi," - + " coalesce(round(p.expectedTotalFee / (COALESCE(expenditure.cumulativeExpenditure,0)),3),0) as projectedCpi," - + " case" - + " when coalesce(round(p.expectedTotalFee / (COALESCE(expenditure.cumulativeExpenditure,0)),3),0) >= 1 then 'Positive'" - + " when coalesce(round(p.expectedTotalFee / (COALESCE(expenditure.cumulativeExpenditure,0)),3),0) < 1 then 'Negative'" - + " end as projectedCashFlowStatus," + val sql = StringBuilder( +// "select" +// + " t.id as teamId," +// + " p.id as id," +// + " p.code as projectCode," +// + " p.name as projectName," +// + " c.name as customerName," +// + " s3.name as subsidiaryName," +// + " p.expectedTotalFee as totalFee," +// + " round((p.expectedTotalFee - ifnull(p.subContractFee, 0)) * 0.8,2) as totalBudget," +// + " COALESCE(round(expenditure.cumulativeExpenditure,2),0) as cumulativeExpenditure," +// + " coalesce(sum(i.issueAmount),0) as totalInvoiced," +// + " coalesce(sum(i.paidAmount),0) as totalReceived," +// + " case" +// + " when coalesce(round(sum(i.issueAmount) / (COALESCE(expenditure.cumulativeExpenditure,0)),3),0) >= 1 then 'Positive'" +// + " when coalesce(round(sum(i.issueAmount) / (COALESCE(expenditure.cumulativeExpenditure,0)),3),0) < 1 then 'Negative'" +// + " end as cashFlowStatus," +// + " coalesce(round(sum(i.issueAmount) / (COALESCE(expenditure.cumulativeExpenditure,0)),3),0) as cpi," +// + " coalesce(round(p.expectedTotalFee / (COALESCE(expenditure.cumulativeExpenditure,0)),3),0) as projectedCpi," +// + " case" +// + " when coalesce(round(p.expectedTotalFee / (COALESCE(expenditure.cumulativeExpenditure,0)),3),0) >= 1 then 'Positive'" +// + " when coalesce(round(p.expectedTotalFee / (COALESCE(expenditure.cumulativeExpenditure,0)),3),0) < 1 then 'Negative'" +// + " end as projectedCashFlowStatus," // + " case" -// + " when coalesce(round(sum(p.expectedTotalFee) * 0.8,2),0) <= COALESCE(expenditure.cumulativeExpenditure,0) then coalesce(round(sum(p.expectedTotalFee) * 0.8,2),0) - coalesce(sum(i.issueAmount),0)" -// + " when coalesce(round(sum(p.expectedTotalFee) * 0.8,2),0) > COALESCE(expenditure.cumulativeExpenditure,0) and COALESCE(expenditure.cumulativeExpenditure,0) < coalesce(sum(i.issueAmount),0) then 0" -// + " when coalesce(round(sum(p.expectedTotalFee) * 0.8,2),0) > COALESCE(expenditure.cumulativeExpenditure,0) and COALESCE(expenditure.cumulativeExpenditure,0) >= coalesce(sum(i.issueAmount),0) then COALESCE(expenditure.cumulativeExpenditure,0) - coalesce(sum(i.issueAmount),0)" +// + " when p.expectedTotalFee - sum(i.issueAmount) >= 0 then coalesce(round(p.expectedTotalFee - sum(i.issueAmount),2),0)" +// + " when p.expectedTotalFee - sum(i.issueAmount) < 0 then 0" +// + " when p.expectedTotalFee - sum(i.issueAmount) is null then 0" // + " end as totalUninvoiced" - + " case" - + " when p.expectedTotalFee - sum(i.issueAmount) >= 0 then coalesce(round(p.expectedTotalFee - sum(i.issueAmount),2),0)" - + " when p.expectedTotalFee - sum(i.issueAmount) < 0 then 0" - + " when p.expectedTotalFee - sum(i.issueAmount) is null then 0" - + " end as totalUninvoiced" - + " from team t" - + " left join project p on t.teamLead = p.teamLead" - + " left join subsidiary s3 on p.customerSubsidiaryId = s3.id" - + " left join customer c on p.customerId = c.id" - + " left join invoice i on p.code = i.projectCode" - + " left join (" - + " select" - + " r.teamId as teamId," - + " r.customerId as customerId," - + " r.projectId as projectId," - + " sum(r.cumulativeExpenditure) as cumulativeExpenditure" - + " from (" - + " select" - + " t1.id as teamId," - + " c2.id as customerId," - + " p2.id as projectId," - + " (coalesce(sum(t2.normalConsumed),0) * s2.hourlyRate) + (coalesce(sum(t2.otConsumed),0) * s2.hourlyRate * 1.0) as cumulativeExpenditure," - + " s2.hourlyRate as hourlyRate," - + " sum(t2.normalConsumed) as normalConsumed," - + " sum(t2.otConsumed) as otConsumed" - + " from team t1" - + " left join project p2 on t1.teamLead = p2.teamLead" - + " left join customer c2 on p2.customerId = c2 .id" - + " 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 s2 on s.salaryId = s2.salaryPoint" - + " where p2.status = 'On-going'" - + " and t2.id is not null" - + " group by s2.hourlyRate,t1.id,c2.id,p2.id" - + " ) as r" - + " group by r.teamId, r.customerId, r.projectId" - + " ) as expenditure on expenditure.teamId = t.id and expenditure.customerId = c.id and expenditure.projectId = p.id" - + " where t.deleted = 0" - + " and p.status = 'On-going'" +// + " from team t" +// + " left join project p on t.teamLead = p.teamLead" +// + " left join subsidiary s3 on p.customerSubsidiaryId = s3.id" +// + " left join customer c on p.customerId = c.id" +// + " left join invoice i on p.code = i.projectCode" +// + " left join (" +// + " select" +// + " r.teamId as teamId," +// + " r.customerId as customerId," +// + " r.projectId as projectId," +// + " sum(r.cumulativeExpenditure) as cumulativeExpenditure" +// + " from (" +// + " select" +// + " t1.id as teamId," +// + " c2.id as customerId," +// + " p2.id as projectId," +// + " (coalesce(sum(t2.normalConsumed),0) * s2.hourlyRate) + (coalesce(sum(t2.otConsumed),0) * s2.hourlyRate * 1.0) as cumulativeExpenditure," +// + " s2.hourlyRate as hourlyRate," +// + " sum(t2.normalConsumed) as normalConsumed," +// + " sum(t2.otConsumed) as otConsumed" +// + " from team t1" +// + " left join project p2 on t1.teamLead = p2.teamLead" +// + " left join customer c2 on p2.customerId = c2 .id" +// + " 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 s2 on s.salaryId = s2.salaryPoint" +// + " where p2.status = 'On-going'" +// + " and t2.id is not null" +// + " group by s2.hourlyRate,t1.id,c2.id,p2.id" +// + " ) as r" +// + " group by r.teamId, r.customerId, r.projectId" +// + " ) as expenditure on expenditure.teamId = t.id and expenditure.customerId = c.id and expenditure.projectId = p.id" +// + " where t.deleted = 0" +// + " and p.status = 'On-going'" + " select" + + " t.id as teamId," + + " p.id as id," + + " p.code as projectCode," + + " p.name as projectName," + + " c.name as customerName," + + " s3.name as subsidiaryName," + + " p.expectedTotalFee as totalFee," + + " round((p.expectedTotalFee - ifnull(p.subContractFee, 0)) * 0.8,2) as totalBudget," + + " COALESCE(round(expenditure.cumulativeExpenditure,2),0) as cumulativeExpenditure," + + " coalesce(sum(i.issueAmount),0) as totalInvoiced," + + " coalesce(sum(i.paidAmount),0) as totalReceived," + + " case" + + " when coalesce(round(sum(i.issueAmount) / (COALESCE(expenditure.cumulativeExpenditure,0)),3),0) >= 1 then 'Positive'" + + " when coalesce(round(sum(i.issueAmount) / (COALESCE(expenditure.cumulativeExpenditure,0)),3),0) < 1 then 'Negative'" + + " end as cashFlowStatus," + + " coalesce(round(sum(i.issueAmount) / (COALESCE(expenditure.cumulativeExpenditure,0)),3),0) as cpi," + + " coalesce(round(p.expectedTotalFee / (COALESCE(expenditure.cumulativeExpenditure,0)),3),0) as projectedCpi," + + " case" + + " when coalesce(round(p.expectedTotalFee / (COALESCE(expenditure.cumulativeExpenditure,0)),3),0) >= 1 then 'Positive'" + + " when coalesce(round(p.expectedTotalFee / (COALESCE(expenditure.cumulativeExpenditure,0)),3),0) < 1 then 'Negative'" + + " end as projectedCashFlowStatus," + + " case" + + " when p.expectedTotalFee - sum(i.issueAmount) >= 0 then coalesce(round(p.expectedTotalFee - sum(i.issueAmount),2),0)" + + " when p.expectedTotalFee - sum(i.issueAmount) < 0 then 0" + + " when p.expectedTotalFee - sum(i.issueAmount) is null then 0" + + " end as totalUninvoiced" + + " from team t" + + " left join project p on t.teamLead = p.teamLead" + + " left join subsidiary s3 on p.customerSubsidiaryId = s3.id" + + " left join customer c on p.customerId = c.id" + + " left join invoice i on p.code = i.projectCode" + + " left join (" + + " select" + + " r.teamId as teamId," + + " r.customerId as customerId," + + " r.projectId as projectId," + + " sum(r.cumulativeExpenditure) as cumulativeExpenditure" + + " from (" + + " select" + + " se.teamId as teamId," + + " se.customerId as customerId," + + " se.projectId as projectId," + + " 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" + + " t1.id as teamId," + + " c2.id as customerId," + + " p2.id as projectId," + + " 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 team t1" + + " left join project p2 on t1.teamLead = p2.teamLead" + + " left join customer c2 on p2.customerId = c2 .id" + + " 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" + + " group by t1.id,c2.id,p2.id, 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 t3.id, se.hourlyRate, se.teamId, se.customerId, se.projectId" + + " ) as r" + + " group by r.teamId, r.customerId, r.projectId" + + " ) as expenditure on expenditure.teamId = t.id and expenditure.customerId = c.id and expenditure.projectId = p.id" + + " where t.deleted = 0" + + " and p.status = 'On-going'" ) if (args != null) { if (args.containsKey("teamId")) @@ -1053,102 +1309,227 @@ open class DashboardService( } fun CashFlowMonthlyExpenditureByMonth(args: Map): List> { - val sql = StringBuilder("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" + val sql = StringBuilder( +// "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" +// + " month(t.recordDate) as recordMonth," +// + " (coalesce(sum(t.normalConsumed),0) * s2.hourlyRate) + (coalesce(sum(t.otConsumed),0) * s2.hourlyRate * 1.0) as cumulativeExpenditure" +// + " 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)" +// + " and year(t.recordDate) = :year" +// + " group by month(t.recordDate),s2.hourlyRate" +// + " ) as r" +// + " group by r.recordMonth" +// + " ) as expenditure on months.month = expenditure.recordMonth" + " 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 as recordMonth," + + " (coalesce(sum(t3.normalConsumed),0) * se.hourlyRate) + (coalesce(sum(t3.otConsumed),0) * se.hourlyRate * 1.0) as cumulativeExpenditure" + + " from(" + + " select" + + " month(t.recordDate) as recordMonth," + + " t.id as tid," + + " t.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 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_effective se on t.recordDate >= se.`date` and s.id = se.staffId" + + " left join salary s2 on se.salaryId = s2.salaryPoint" + + " where t.id is not null" + + " and p.id in (:projectIds)" + + " and year(t.recordDate) = :year" + + " group by t.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, se.hourlyRate" + + " ) as r" + + " group by r.recordMonth" + + " ) as expenditure on months.month = expenditure.recordMonth" + ) + + return jdbcDao.queryForList(sql.toString(), args) + } + fun CashFlowReceivableAndExpenditure(args: Map): List> { + val sql = StringBuilder( +// "select" +// + " coalesce (round(sum(i.paidAmount)/sum(i.issueAmount)*100,0),0) as receivedPercentage," +// + " coalesce (round(expenditure.expenditure/((sum(p.expectedTotalFee) - sum(ifnull(p.subContractFee, 0)))*0.8)*100,0),0) as expenditurePercentage," +// + " coalesce (sum(i.issueAmount),0) as totalInvoiced," +// + " coalesce (sum(i.paidAmount),0) as totalReceived," +// + " coalesce (sum(i.issueAmount) - sum(i.paidAmount),0) as receivable," +// + " coalesce (round((sum(p.expectedTotalFee) - sum(ifnull(p.subContractFee, 0)))*0.8,2),0) as totalBudget," +// + " coalesce (expenditure.expenditure,0) as totalExpenditure," +// + " coalesce ((sum(p.expectedTotalFee) - sum(ifnull(p.subContractFee, 0)))*0.8 - expenditure.expenditure,0) as expenditureReceivable," +// + " sum(p.expectedTotalFee) as totalProjectFee," +// + " coalesce (round(sum(i.issueAmount)/sum(p.expectedTotalFee)*100,0),0) as invoicedPercentage" +// + " from project p" +// + " left join (" +// + " select" +// + " p3.id as pid," +// + " sum(i3.issueAmount) as issueAmount," +// + " sum(i3.paidAmount) as paidAmount" +// + " from project p3" +// + " left join invoice i3 on p3.code = i3.projectCode" +// + " where p3.deleted = 0" +// + " and p3.status = 'On-going'" +// + " group by p3.id" +// + " ) as i on i.pid = p.id" +// + " left join(" +// + " select" +// + " sum(r.expenditure) as expenditure" +// + " from(" +// + " select" +// + " (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" +// + " ) as r" +// + " ) as expenditure on 1=1" +// + " where p.id in (:projectIds)" +// + " group by expenditure.expenditure" + " select" + + " coalesce (round(sum(i.paidAmount)/sum(i.issueAmount)*100,0),0) as receivedPercentage," + + " coalesce (round(expenditure.expenditure/((sum(p.expectedTotalFee) - sum(ifnull(p.subContractFee, 0)))*0.8)*100,0),0) as expenditurePercentage," + + " coalesce (sum(i.issueAmount),0) as totalInvoiced," + + " coalesce (sum(i.paidAmount),0) as totalReceived," + + " coalesce (sum(i.issueAmount) - sum(i.paidAmount),0) as receivable," + + " coalesce (round((sum(p.expectedTotalFee) - sum(ifnull(p.subContractFee, 0)))*0.8,2),0) as totalBudget," + + " coalesce (expenditure.expenditure,0) as totalExpenditure," + + " coalesce ((sum(p.expectedTotalFee) - sum(ifnull(p.subContractFee, 0)))*0.8 - expenditure.expenditure,0) as expenditureReceivable," + + " sum(p.expectedTotalFee) as totalProjectFee," + + " coalesce (round(sum(i.issueAmount)/sum(p.expectedTotalFee)*100,0),0) as invoicedPercentage" + + " from project p" + + " left join (" + + " select" + + " p3.id as pid," + + " sum(i3.issueAmount) as issueAmount," + + " sum(i3.paidAmount) as paidAmount" + + " from project p3" + + " left join invoice i3 on p3.code = i3.projectCode" + + " where p3.deleted = 0" + + " and p3.status = 'On-going'" + + " group by p3.id" + + " ) as i on i.pid = p.id" + " left join(" - + " SELECT" - + " r.recordMonth as recordMonth," - + " sum(r.cumulativeExpenditure) as expenditure" + + " select" + + " sum(r.expenditure) as expenditure" + " from(" + " select" - + " month(t.recordDate) as recordMonth," - + " (coalesce(sum(t.normalConsumed),0) * s2.hourlyRate) + (coalesce(sum(t.otConsumed),0) * s2.hourlyRate * 1.0) as cumulativeExpenditure" + + " t3.id," + + " (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 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" + + " 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 t2.id is not null" + " and p.id in (:projectIds)" - + " and year(t.recordDate) = :year" - + " group by month(t.recordDate),s2.hourlyRate" + + " 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 t3.id, se.hourlyRate" + " ) as r" - + " group by r.recordMonth" - + " ) as expenditure on months.month = expenditure.recordMonth" - ) - - return jdbcDao.queryForList(sql.toString(), args) - } - fun CashFlowReceivableAndExpenditure(args: Map): List> { - val sql = StringBuilder("select" - + " coalesce (round(sum(i.paidAmount)/sum(i.issueAmount)*100,0),0) as receivedPercentage," - + " coalesce (round(expenditure.expenditure/((sum(p.expectedTotalFee) - sum(ifnull(p.subContractFee, 0)))*0.8)*100,0),0) as expenditurePercentage," - + " coalesce (sum(i.issueAmount),0) as totalInvoiced," - + " coalesce (sum(i.paidAmount),0) as totalReceived," - + " coalesce (sum(i.issueAmount) - sum(i.paidAmount),0) as receivable," - + " coalesce (round((sum(p.expectedTotalFee) - sum(ifnull(p.subContractFee, 0)))*0.8,2),0) as totalBudget," - + " coalesce (expenditure.expenditure,0) as totalExpenditure," - + " coalesce ((sum(p.expectedTotalFee) - sum(ifnull(p.subContractFee, 0)))*0.8 - expenditure.expenditure,0) as expenditureReceivable," - + " sum(p.expectedTotalFee) as totalProjectFee," - + " coalesce (round(sum(i.issueAmount)/sum(p.expectedTotalFee)*100,0),0) as invoicedPercentage" - + " from project p" - + " left join (" - + " select" - + " p3.id as pid," - + " sum(i3.issueAmount) as issueAmount," - + " sum(i3.paidAmount) as paidAmount" - + " from project p3" - + " left join invoice i3 on p3.code = i3.projectCode" - + " where p3.deleted = 0" - + " and p3.status = 'On-going'" - + " group by p3.id" - + " ) as i on i.pid = p.id" - + " left join(" - + " select" - + " sum(r.expenditure) as expenditure" - + " from(" - + " select" - + " (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" - + " ) as r" - + " ) as expenditure on 1=1" - + " where p.id in (:projectIds)" - + " group by expenditure.expenditure" + + " ) as expenditure on 1=1" + + " where p.id in (:projectIds)" + + " group by expenditure.expenditure" ) return jdbcDao.queryForList(sql.toString(), args) @@ -1228,63 +1609,133 @@ open class DashboardService( return jdbcDao.queryForList(sql.toString(), args) } fun CashFlowLedger(args: Map): List> { - val sql = StringBuilder("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" - + " 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" - + " 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;" + val sql = StringBuilder( +// "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" +// + " 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" +// + " 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;" + " 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" + + " 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" ) return jdbcDao.queryForList(sql.toString(), args) @@ -1343,61 +1794,128 @@ open class DashboardService( return jdbcDao.queryForList(sql.toString(), args) } fun TeamCashFlowExpenditure(args: Map): List> { - val sql = StringBuilder("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" - + " month(t.recordDate) as recordMonth," - + " (coalesce(sum(t.normalConsumed),0) * s2.hourlyRate) + (coalesce(sum(t.otConsumed),0) * s2.hourlyRate * 1.0) as cumulativeExpenditure" - + " from project p" - + " left join team t2 on p.teamLead = t2.teamLead" - + " 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" + val sql = StringBuilder( +// "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" +// + " month(t.recordDate) as recordMonth," +// + " (coalesce(sum(t.normalConsumed),0) * s2.hourlyRate) + (coalesce(sum(t.otConsumed),0) * s2.hourlyRate * 1.0) as cumulativeExpenditure" +// + " from project p" +// + " left join team t2 on p.teamLead = t2.teamLead" +// + " 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" + " 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" ) if (args != null) { if (args.containsKey("teamId")) - sql.append(" AND t2.id = :teamId") + sql.append(" AND t.id = :teamId") } - sql.append(" and year(t.recordDate) = :year" - + " group by month(t.recordDate),s2.hourlyRate" + 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") + + " ) as expenditure on months.month = expenditure.recordMonth" + ) return jdbcDao.queryForList(sql.toString(), args) } @@ -1427,72 +1945,90 @@ open class DashboardService( } fun projectResourceSummaryInformation(args: Map): List> { val sql = StringBuilder( - // "select" - // + " concat(p.code,'-',p.name) as projectCodeAndName," - // + " p.expectedTotalFee as totalFee," - // + " expenditure.expenditure as expenditure," - // + " case" - // + " when p.expectedTotalFee - expenditure.expenditure >= 0 then 'Within Budget'" - // + " when p.expectedTotalFee - expenditure.expenditure < 0 then 'Overconsumption'" - // + " end as status," - // + " p.totalManhour as plannedResources," - // + " sum(t.normalConsumed) + sum(t.otConsumed) as resourcesSpent," - // + " p.totalManhour - sum(t.normalConsumed) + sum(t.otConsumed) as remainingResources" - // + " 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(" - // + " select" - // + " sum(r.expenditure) as expenditure" - // + " from(" - // + " select" - // + " (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 = :projectId" - // + " group by s2.hourlyRate" - // + " ) as r" - // + " ) as expenditure on 1 = 1" - // + " where p.id = :projectId" - // + " group by expenditure.expenditure" - "select" - + " concat(p.code,'-',p.name) as projectCodeAndName," - + " p.expectedTotalFee as totalFee," - + " (p.expectedTotalFee - ifnull(p.subContractFee, 0)) * 0.8 as totalBudget," - + " coalesce (expenditure.expenditure,0) as expenditure," - + " ((p.expectedTotalFee - ifnull(p.subContractFee, 0)) * 0.8) - coalesce (expenditure.expenditure,0) as remainingBudget," - + " case" - + " when p.expectedTotalFee - expenditure.expenditure >= 0 then 'Within Budget'" - + " when p.expectedTotalFee - expenditure.expenditure < 0 then 'Overconsumption'" - + " end as status," - + " p.totalManhour as plannedResources," - + " sum(ifnull(t.normalConsumed, 0) + ifnull(t.otConsumed, 0)) as resourcesSpent," - + " p.totalManhour - sum(ifnull(t.normalConsumed, 0) + ifnull(t.otConsumed, 0)) as remainingResources" - + " 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(" - + " select" - + " sum(r.expenditure) as expenditure" - + " from(" - + " select" - + " (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 = :projectId" - + " group by s2.hourlyRate" - + " ) as r" - + " ) as expenditure on 1 = 1" - + " where p.id = :projectId" - + " group by expenditure.expenditure" +// "select" +// + " concat(p.code,'-',p.name) as projectCodeAndName," +// + " p.expectedTotalFee as totalFee," +// + " (p.expectedTotalFee - ifnull(p.subContractFee, 0)) * 0.8 as totalBudget," +// + " coalesce (expenditure.expenditure,0) as expenditure," +// + " ((p.expectedTotalFee - ifnull(p.subContractFee, 0)) * 0.8) - coalesce (expenditure.expenditure,0) as remainingBudget," +// + " case" +// + " when p.expectedTotalFee - expenditure.expenditure >= 0 then 'Within Budget'" +// + " when p.expectedTotalFee - expenditure.expenditure < 0 then 'Overconsumption'" +// + " end as status," +// + " p.totalManhour as plannedResources," +// + " sum(ifnull(t.normalConsumed, 0) + ifnull(t.otConsumed, 0)) as resourcesSpent," +// + " p.totalManhour - sum(ifnull(t.normalConsumed, 0) + ifnull(t.otConsumed, 0)) as remainingResources" +// + " 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(" +// + " select" +// + " sum(r.expenditure) as expenditure" +// + " from(" +// + " select" +// + " (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 = :projectId" +// + " group by s2.hourlyRate" +// + " ) as r" +// + " ) as expenditure on 1 = 1" +// + " where p.id = :projectId" +// + " group by expenditure.expenditure" + " select" + + " concat(p.code,'-',p.name) as projectCodeAndName," + + " p.expectedTotalFee as totalFee," + + " (p.expectedTotalFee - ifnull(p.subContractFee, 0)) * 0.8 as totalBudget," + + " coalesce (expenditure.expenditure,0) as expenditure," + + " ((p.expectedTotalFee - ifnull(p.subContractFee, 0)) * 0.8) - coalesce (expenditure.expenditure,0) as remainingBudget," + + " case" + + " when p.expectedTotalFee - expenditure.expenditure >= 0 then 'Within Budget'" + + " when p.expectedTotalFee - expenditure.expenditure < 0 then 'Overconsumption'" + + " end as status," + + " p.totalManhour as plannedResources," + + " sum(ifnull(t.normalConsumed, 0) + ifnull(t.otConsumed, 0)) as resourcesSpent," + + " p.totalManhour - sum(ifnull(t.normalConsumed, 0) + ifnull(t.otConsumed, 0)) as remainingResources" + + " 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(" + + " select" + + " sum(r.expenditure) as expenditure" + + " from(" + + " select" + + " (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 = :projectId" + + " 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 t3.id, se.hourlyRate" + + " ) as r" + + " ) as expenditure on 1 = 1" + + " where p.id = :projectId" + + " group by expenditure.expenditure" ) return jdbcDao.queryForList(sql.toString(), args)