From 1306b7169d5feaab2534bba9b11adb9621a68562 Mon Sep 17 00:00:00 2001 From: Wayne Date: Wed, 8 May 2024 23:21:27 +0900 Subject: [PATCH] Add ot hours for timesheet --- .../ffii/tsms/modules/project/service/ProjectsService.kt | 6 ++++-- .../tsms/modules/timesheet/entity/TimesheetRepository.kt | 5 +++-- .../timesheet/entity/projections/TimesheetHours.kt | 6 ++++++ .../tsms/modules/timesheet/service/TimesheetsService.kt | 8 ++++++-- .../ffii/tsms/modules/timesheet/web/models/TimeEntry.kt | 3 ++- 5 files changed, 21 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/ffii/tsms/modules/timesheet/entity/projections/TimesheetHours.kt diff --git a/src/main/java/com/ffii/tsms/modules/project/service/ProjectsService.kt b/src/main/java/com/ffii/tsms/modules/project/service/ProjectsService.kt index a30e8eb..2a6a509 100644 --- a/src/main/java/com/ffii/tsms/modules/project/service/ProjectsService.kt +++ b/src/main/java/com/ffii/tsms/modules/project/service/ProjectsService.kt @@ -68,6 +68,8 @@ open class ProjectsService( staffRepository.findByUserId(user.id).getOrNull()?.let { staff -> staffAllocationRepository.findAssignedProjectsByStaff(staff) .mapNotNull { it.project?.let { project -> + val timesheetHours = timesheetRepository.totalHoursConsumedByProject(project) + AssignedProject( id = project.id!!, code = project.code!!, @@ -82,8 +84,8 @@ open class ProjectsService( ) }, hoursAllocated = project.totalManhour ?: 0.0, hoursAllocatedOther = 0.0, - hoursSpent = timesheetRepository.totalNormalHoursConsumedByProject(project), - hoursSpentOther = 0.0 + hoursSpent = timesheetHours.normalConsumed, + hoursSpentOther = timesheetHours.otConsumed ) } } } diff --git a/src/main/java/com/ffii/tsms/modules/timesheet/entity/TimesheetRepository.kt b/src/main/java/com/ffii/tsms/modules/timesheet/entity/TimesheetRepository.kt index 59b09f8..6f20be5 100644 --- a/src/main/java/com/ffii/tsms/modules/timesheet/entity/TimesheetRepository.kt +++ b/src/main/java/com/ffii/tsms/modules/timesheet/entity/TimesheetRepository.kt @@ -3,6 +3,7 @@ package com.ffii.tsms.modules.timesheet.entity; import com.ffii.core.support.AbstractRepository import com.ffii.tsms.modules.data.entity.Staff import com.ffii.tsms.modules.project.entity.Project +import com.ffii.tsms.modules.timesheet.entity.projections.TimesheetHours import org.springframework.data.jpa.repository.Query import java.time.LocalDate @@ -12,6 +13,6 @@ interface TimesheetRepository : AbstractRepository { fun deleteAllByStaffAndRecordDate(staff: Staff, recordDate: LocalDate) - @Query("SELECT IFNULL(SUM(normalConsumed), 0) FROM Timesheet t JOIN ProjectTask pt on t.projectTask = pt WHERE pt.project = ?1") - fun totalNormalHoursConsumedByProject(project: Project): Double + @Query("SELECT new com.ffii.tsms.modules.timesheet.entity.projections.TimesheetHours(IFNULL(SUM(normalConsumed), 0), IFNULL(SUM(otConsumed), 0)) FROM Timesheet t JOIN ProjectTask pt on t.projectTask = pt WHERE pt.project = ?1") + fun totalHoursConsumedByProject(project: Project): TimesheetHours } \ No newline at end of file diff --git a/src/main/java/com/ffii/tsms/modules/timesheet/entity/projections/TimesheetHours.kt b/src/main/java/com/ffii/tsms/modules/timesheet/entity/projections/TimesheetHours.kt new file mode 100644 index 0000000..7a6d9c3 --- /dev/null +++ b/src/main/java/com/ffii/tsms/modules/timesheet/entity/projections/TimesheetHours.kt @@ -0,0 +1,6 @@ +package com.ffii.tsms.modules.timesheet.entity.projections + +data class TimesheetHours( + val normalConsumed: Double, + val otConsumed: Double +) diff --git a/src/main/java/com/ffii/tsms/modules/timesheet/service/TimesheetsService.kt b/src/main/java/com/ffii/tsms/modules/timesheet/service/TimesheetsService.kt index bf6960c..04903af 100644 --- a/src/main/java/com/ffii/tsms/modules/timesheet/service/TimesheetsService.kt +++ b/src/main/java/com/ffii/tsms/modules/timesheet/service/TimesheetsService.kt @@ -12,7 +12,6 @@ import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional import java.time.LocalDate import java.time.format.DateTimeFormatter -import kotlin.jvm.optionals.getOrDefault import kotlin.jvm.optionals.getOrNull @Service @@ -41,6 +40,7 @@ open class TimesheetsService( this.staff = currentStaff this.recordDate = entryDate this.normalConsumed = timeEntry.inputHours + this.otConsumed = timeEntry.otHours this.projectTask = projectTask this.remark = timeEntry.remark } @@ -68,6 +68,7 @@ open class TimesheetsService( taskId = timesheet.projectTask?.task?.id, taskGroupId = timesheet.projectTask?.task?.taskGroup?.id, inputHours = timesheet.normalConsumed ?: 0.0, + otHours = timesheet.otConsumed ?: 0.0, remark = timesheet.remark ) } } @@ -77,7 +78,10 @@ open class TimesheetsService( return entries .groupBy { timeEntry -> Pair(timeEntry.projectId, timeEntry.taskId) } .values.map { timeEntries -> - timeEntries.reduce { acc, timeEntry -> acc.copy(inputHours = acc.inputHours + timeEntry.inputHours) } + timeEntries.reduce { acc, timeEntry -> acc.copy( + inputHours = (acc.inputHours ?: 0.0) + (timeEntry.inputHours ?: 0.0), + otHours = (acc.otHours ?: 0.0) + (timeEntry.otHours ?: 0.0) + ) } } } } \ No newline at end of file diff --git a/src/main/java/com/ffii/tsms/modules/timesheet/web/models/TimeEntry.kt b/src/main/java/com/ffii/tsms/modules/timesheet/web/models/TimeEntry.kt index e206308..6af3c26 100644 --- a/src/main/java/com/ffii/tsms/modules/timesheet/web/models/TimeEntry.kt +++ b/src/main/java/com/ffii/tsms/modules/timesheet/web/models/TimeEntry.kt @@ -6,6 +6,7 @@ data class TimeEntry( val projectId: Long?, val taskGroupId: Long?, val taskId: Long?, - val inputHours: Double, + val inputHours: Double?, + val otHours: Double?, val remark: String? ) \ No newline at end of file