From 12758f591832b3a38864d5c2c391de6468d298d3 Mon Sep 17 00:00:00 2001 From: "MSI\\derek" Date: Thu, 19 Dec 2024 15:33:57 +0800 Subject: [PATCH] update unsubmitted timesheet by staff --- .../modules/data/service/DashboardService.kt | 164 ++++++++++++++++++ 1 file changed, 164 insertions(+) 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 548fe7c..ba2c720 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 @@ -4,6 +4,7 @@ import com.ffii.core.support.JdbcDao import com.ffii.core.utils.CheckingUtils import com.ffii.tsms.modules.data.entity.CustomerRepository import com.ffii.tsms.modules.data.entity.CustomerTypeRepository +import com.ffii.tsms.modules.data.entity.StaffRepository import com.ffii.tsms.modules.data.entity.TeamRepository import com.ffii.tsms.modules.data.web.models.FinancialSummaryByClient import com.ffii.tsms.modules.data.web.models.FinancialSummaryByProject @@ -21,7 +22,9 @@ import org.springframework.stereotype.Service import java.io.ByteArrayOutputStream import java.io.IOException import java.math.BigDecimal +import java.time.DayOfWeek import java.time.LocalDate +import java.time.YearMonth import java.time.format.DateTimeFormatter @Service @@ -41,6 +44,7 @@ open class DashboardService( private val salaryEffectiveService: SalaryEffectiveService, private val projectExpenseRepository: ProjectExpenseRepository, private val teamRepository: TeamRepository, + private val companyHolidayService: CompanyHolidayService, private val jdbcDao: JdbcDao ) { private val DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy/MM/dd") @@ -2648,6 +2652,166 @@ open class DashboardService( return jdbcDao.queryForList(sql.toString(), args) } + + fun monthlySubmittedTimesheet(args: Map): List> { + val unsubmittedSQL = StringBuilder( + " with team_cte as ( " + + " select " + + " * " + + " from team_log tl " + + " where :startdate >= tl.`from` and (:startdate < tl.`to` or tl.`to` is null) " + + " ) " + + " select " + + " result.recordDate, result.staffId, sum(result.marked) as marked " + + " from ( " + + " select " + + " t.recordDate, " + + " t.staffId, " + + " (coalesce(t.normalConsumed, 0) + coalesce(t.otConsumed, 0)) as marked " + + " from timesheet t " + + " left join team_cte tc on tc.staffId = t.staffId " + + " left join staff s on s.id = t.staffId " + + " where t.recordDate >= :startdate and t.recordDate < DATE_ADD(:startdate, INTERVAL 1 MONTH) " + + " union " + + " select " + + " recordDate, " + + " staffId, " + + " (leaveHours) as marked " + + " from `leave` l " + + " where l.deleted = false " + + " and l.recordDate >= :startdate and l.recorddate < DATE_ADD(:startdate, INTERVAL 1 MONTH) " + + " ) result " + + " group by recordDate, staffId " + + " having marked >= 8 " + + " and staffId = :staffId; " + + ) + return jdbcDao.queryForList(unsubmittedSQL.toString(), args) + } + fun weeklySubmittedTimesheet(args: Map): List> { + // departDate + val submittedSQL = StringBuilder( + " with team_cte as ( " + + " select " + + " * " + + " from team_log tl " + + " where :startdate >= tl.`from` and (:startdate < tl.`to` or tl.`to` is null) " + + " ) " + + " select " + + " result.recordDate, result.staffId, sum(result.marked) as marked " + + " from ( " + + " select " + + " t.recordDate, " + + " t.staffId, " + + " (coalesce(t.normalConsumed, 0) + coalesce(t.otConsumed, 0)) as marked " + + " from timesheet t " + + " left join team_cte tc on tc.staffId = t.staffId " + + " left join staff s on s.id = t.staffId " + + " where t.recordDate >= :startdate and t.recordDate < DATE_ADD(:startdate, INTERVAL 7 DAY) " + + " union " + + " select " + + " recordDate, " + + " staffId, " + + " (leaveHours) as marked " + + " from `leave` l " + + " where l.deleted = false " + + " and l.recordDate >= :startdate and l.recorddate < DATE_ADD(:startdate, INTERVAL 7 DAY) " + + " ) result " + + " group by recordDate, staffId " + + " having marked >= 8 " + + " and staffId = :staffId; " + ) + return jdbcDao.queryForList(submittedSQL.toString(), args) + } + + fun getWeeklyUnsubmittedTimesheet(args: Map): List> { + val result = mutableListOf>() + val teamId = args["teamId"] as Long + val staffs = staffsService.findAll().orElseThrow() + val startDate = args["startdate"] as LocalDate + val year = startDate.year + val month = startDate.month + val day = startDate.dayOfMonth + + val yearMonth = YearMonth.of(year, month) + val daysInMonth = yearMonth.lengthOfMonth() + val dateList = (0..6).mapNotNull { + val date = LocalDate.of(year, month, day + it) + if (date.dayOfWeek != DayOfWeek.SATURDAY && date.dayOfWeek != DayOfWeek.SUNDAY) { + date // Include the date if it's not Saturday or Sunday + } else { + null + } + }.toMutableList() + val publicHolidayList = (args["publicHolidayList"] as List).filter { it.year == startDate.year && it.month == startDate.month } + val companyHoliday = companyHolidayService.allCompanyHolidays().filter { it.date.month == month } + val thisArgs = args.toMutableMap() + + for (curr in staffs) { + val thisTeam = teamLogService.getStaffTeamLog(curr.id!!, startDate) + println(thisTeam) + if (thisTeam == null || thisTeam.team.id != teamId) { + continue + } + if (curr.departDate != null) { + dateList.filter { it.isBefore(curr.departDate) } + } + thisArgs["staffId"] = curr.id!! + val submittedMonth = monthlySubmittedTimesheet(thisArgs) + val UnsubmittedCount = dateList.size - publicHolidayList.size - companyHoliday.size - submittedMonth.size + result.add( + mapOf( + "id" to curr.id!!, + "name" to curr.name, + "teamId" to thisTeam.team.id, + "UnsubmittedCount" to UnsubmittedCount, + )) + } + return result + } + fun getMonthlyUnsubmittedTimesheet(args: Map): List> { + val result = mutableListOf>() + val teamId = args["teamId"] as Long + val staffs = staffsService.findAll().orElseThrow() + val startDate = args["startdate"] as LocalDate + val year = startDate.year + val month = startDate.month + val yearMonth = YearMonth.of(year, month) + val daysInMonth = yearMonth.lengthOfMonth() + val dateList = (1..daysInMonth).mapNotNull { day -> + val date = LocalDate.of(year, month, day) + if (date.dayOfWeek != DayOfWeek.SATURDAY && date.dayOfWeek != DayOfWeek.SUNDAY) { + date // Include the date if it's not Saturday or Sunday + } else { + null + } + }.toMutableList() + val publicHolidayList = (args["publicHolidayList"] as List).filter { it.year == startDate.year && it.month == startDate.month } + val companyHoliday = companyHolidayService.allCompanyHolidays().filter { it.date.month == month } + val thisArgs = args.toMutableMap() + + for (curr in staffs) { + val thisTeam = teamLogService.getStaffTeamLog(curr.id!!, startDate) + println(thisTeam) + if (thisTeam == null || thisTeam.team.id != teamId) { + continue + } + if (curr.departDate != null) { + dateList.filter { it.isBefore(curr.departDate) } + } + thisArgs["staffId"] = curr.id!! + val submittedMonth = monthlySubmittedTimesheet(thisArgs) + val UnsubmittedCount = dateList.size - publicHolidayList.size - companyHoliday.size - submittedMonth.size + result.add( + mapOf( + "id" to curr.id!!, + "name" to curr.name, + "teamId" to thisTeam.team.id, + "UnsubmittedCount" to UnsubmittedCount, + )) + } + return result + } fun weeklyUnsubmittedTimeSheet(args: Map): List> { val sql = StringBuilder( "SELECT"