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 2efb46a..e9029d1 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 @@ -10,6 +10,7 @@ import com.ffii.tsms.modules.data.web.models.SaveCustomerResponse import com.ffii.tsms.modules.project.web.models.SaveCustomerRequest import org.springframework.beans.BeanUtils import org.springframework.stereotype.Service +import java.math.BigDecimal import java.util.Optional @Service @@ -140,6 +141,87 @@ open class DashboardService( return jdbcDao.queryForList(sql.toString(), args) } + + open fun getFinancialStatus(): List> { + val sql = StringBuilder( + " with cte_invoice as (select p.code, sum(i.issueAmount) as sumIssuedAmount , sum(i.paidAmount) as sumPaidAmount" + + " from invoice i" + + " left join project p on p.code = i.projectCode" + + " group by p.code" + + " )," + + " cte_teamLead as (" + + " select p.teamLead, p.code, t.name as teamName , t.code as teamCode" + + " from project p" + + " left join team t on t.teamLead = p.teamLead " + + " )" + + " Select p.code, p.description, c.name as client, concat(cte_t.teamCode, \' - \', cte_t.teamName) as teamLead, p.planStart , p.planEnd , p.expectedTotalFee ," + + " s.name as staff , IFNULL(t.normalConsumed, 0) as normalConsumed, IFNULL(t.otConsumed , 0) as otConsumed, s2.hourlyRate," + + " IFNULL(cte_i.sumIssuedAmount, 0) as sumIssuedAmount , IFNULL(cte_i.sumPaidAmount, 0) as sumPaidAmount " + + " from timesheet t" + + " left join project_task pt on pt.id = t.projectTaskId" + + " left join project p ON p.id = pt.project_id" + + " left join staff s on s.id = t.staffId" + + " left join salary s2 on s.salaryId = s2.salaryPoint" + + " left join customer c on c.id = p.customerId" + + " left join team t2 on t2.id = s.teamId" + + " left join cte_invoice cte_i on cte_i.code = p.code" + + " left join cte_teamLead cte_t on cte_t.code = p.code" + + " where p.status = \'On-going\' " + ) + + sql.append(" order by p.code") + + return jdbcDao.queryForList(sql.toString()) + } + + fun searchFinancialSummary(): List> { + val financialStatus: List> = getFinancialStatus() + + val otFactor = BigDecimal(1) + + val tempList = mutableListOf>() + + for (item in financialStatus) { + val normalConsumed = item.getValue("normalConsumed") as Double + val hourlyRate = item.getValue("hourlyRate") as BigDecimal +// println("normalConsumed------------- $normalConsumed") +// println("hourlyRate------------- $hourlyRate") + val manHourRate = normalConsumed.toBigDecimal().multiply(hourlyRate) +// println("manHourRate------------ $manHourRate") + + val otConsumed = item.getValue("otConsumed") as Double + val manOtHourRate = otConsumed.toBigDecimal().multiply(hourlyRate).multiply(otFactor) + + if (!tempList.any { it.containsValue(item.getValue("code")) }) { + + tempList.add( + mapOf( + "code" to item.getValue("code"), + "description" to item.getValue("description"), + "client" to item.getValue("client"), + "teamLead" to item.getValue("teamLead"), + "planStart" to item.getValue("planStart"), + "planEnd" to item.getValue("planEnd"), + "expectedTotalFee" to item.getValue("expectedTotalFee"), + "normalConsumed" to manHourRate, + "otConsumed" to manOtHourRate, + "issuedAmount" to item.getValue("sumIssuedAmount"), + "paidAmount" to item.getValue("sumPaidAmount"), + ) + ) + } else { + // Find the existing Map in the tempList that has the same "code" value + val existingMap = tempList.find { it.containsValue(item.getValue("code")) }!! + + // Update the existing Map with the new manHourRate and manOtHourRate values + tempList[tempList.indexOf(existingMap)] = existingMap.toMutableMap().apply { + put("normalConsumed", (get("normalConsumed") as BigDecimal).add(manHourRate)) + put("otConsumed", (get("otConsumed") as BigDecimal).add(manOtHourRate)) + } + } + } + return tempList + } } 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 dea71c7..06d9f5f 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 @@ -69,4 +69,9 @@ class DashboardController( } return result } + + @GetMapping("/searchFinancialSummary") + fun searchFinancialSummary(): List>{ + return dashboardService.searchFinancialSummary() + } } \ No newline at end of file