|
@@ -2,16 +2,19 @@ package com.ffii.tsms.modules.timesheet.service |
|
|
|
|
|
|
|
|
import com.ffii.core.exception.BadRequestException |
|
|
import com.ffii.core.exception.BadRequestException |
|
|
import com.ffii.tsms.modules.data.service.StaffsService |
|
|
import com.ffii.tsms.modules.data.service.StaffsService |
|
|
|
|
|
import com.ffii.tsms.modules.data.service.TeamService |
|
|
import com.ffii.tsms.modules.project.entity.ProjectRepository |
|
|
import com.ffii.tsms.modules.project.entity.ProjectRepository |
|
|
import com.ffii.tsms.modules.project.entity.ProjectTaskRepository |
|
|
import com.ffii.tsms.modules.project.entity.ProjectTaskRepository |
|
|
import com.ffii.tsms.modules.project.entity.TaskRepository |
|
|
import com.ffii.tsms.modules.project.entity.TaskRepository |
|
|
import com.ffii.tsms.modules.timesheet.entity.Timesheet |
|
|
import com.ffii.tsms.modules.timesheet.entity.Timesheet |
|
|
import com.ffii.tsms.modules.timesheet.entity.TimesheetRepository |
|
|
import com.ffii.tsms.modules.timesheet.entity.TimesheetRepository |
|
|
|
|
|
import com.ffii.tsms.modules.timesheet.web.models.TeamMemberTimeEntries |
|
|
import com.ffii.tsms.modules.timesheet.web.models.TimeEntry |
|
|
import com.ffii.tsms.modules.timesheet.web.models.TimeEntry |
|
|
import org.springframework.stereotype.Service |
|
|
import org.springframework.stereotype.Service |
|
|
import org.springframework.transaction.annotation.Transactional |
|
|
import org.springframework.transaction.annotation.Transactional |
|
|
import java.time.LocalDate |
|
|
import java.time.LocalDate |
|
|
import java.time.format.DateTimeFormatter |
|
|
import java.time.format.DateTimeFormatter |
|
|
|
|
|
import kotlin.jvm.optionals.getOrDefault |
|
|
import kotlin.jvm.optionals.getOrNull |
|
|
import kotlin.jvm.optionals.getOrNull |
|
|
|
|
|
|
|
|
@Service |
|
|
@Service |
|
@@ -20,7 +23,8 @@ open class TimesheetsService( |
|
|
private val projectTaskRepository: ProjectTaskRepository, |
|
|
private val projectTaskRepository: ProjectTaskRepository, |
|
|
private val projectRepository: ProjectRepository, |
|
|
private val projectRepository: ProjectRepository, |
|
|
private val taskRepository: TaskRepository, |
|
|
private val taskRepository: TaskRepository, |
|
|
private val staffsService: StaffsService |
|
|
|
|
|
|
|
|
private val staffsService: StaffsService, |
|
|
|
|
|
private val teamService: TeamService |
|
|
) { |
|
|
) { |
|
|
@Transactional |
|
|
@Transactional |
|
|
open fun saveTimesheet(recordTimeEntry: Map<LocalDate, List<TimeEntry>>): Map<String, List<TimeEntry>> { |
|
|
open fun saveTimesheet(recordTimeEntry: Map<LocalDate, List<TimeEntry>>): Map<String, List<TimeEntry>> { |
|
@@ -52,12 +56,61 @@ open class TimesheetsService( |
|
|
return transformToTimeEntryMap(savedTimesheets) |
|
|
return transformToTimeEntryMap(savedTimesheets) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Transactional |
|
|
|
|
|
open fun saveMemberTimeEntry(staffId: Long, entry: TimeEntry): TimeEntry { |
|
|
|
|
|
val currentStaff = staffsService.currentStaff() ?: throw BadRequestException() |
|
|
|
|
|
// Make sure current staff is a team lead |
|
|
|
|
|
teamService.getMyTeamForStaff(currentStaff) ?: throw BadRequestException() |
|
|
|
|
|
|
|
|
|
|
|
val timesheet = timesheetRepository.findById(entry.id).getOrDefault(Timesheet()).apply { |
|
|
|
|
|
val task = entry.taskId?.let { taskRepository.findById(it).getOrNull() } |
|
|
|
|
|
val project = entry.projectId?.let { projectRepository.findById(it).getOrNull() } |
|
|
|
|
|
val projectTask = project?.let { p -> task?.let { t -> projectTaskRepository.findByProjectAndTask(p, t) } } |
|
|
|
|
|
|
|
|
|
|
|
this.normalConsumed = entry.inputHours |
|
|
|
|
|
this.otConsumed = entry.otHours |
|
|
|
|
|
this.projectTask = projectTask |
|
|
|
|
|
this.remark = entry.remark |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
timesheetRepository.save(timesheet) |
|
|
|
|
|
|
|
|
|
|
|
return TimeEntry( |
|
|
|
|
|
id = timesheet.id!!, |
|
|
|
|
|
projectId = timesheet.projectTask?.project?.id, |
|
|
|
|
|
taskId = timesheet.projectTask?.task?.id, |
|
|
|
|
|
taskGroupId = timesheet.projectTask?.task?.taskGroup?.id, |
|
|
|
|
|
inputHours = timesheet.normalConsumed ?: 0.0, |
|
|
|
|
|
otHours = timesheet.otConsumed ?: 0.0, |
|
|
|
|
|
remark = timesheet.remark |
|
|
|
|
|
) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
open fun getTimesheet(): Map<String, List<TimeEntry>> { |
|
|
open fun getTimesheet(): Map<String, List<TimeEntry>> { |
|
|
// Need to be associated with a staff |
|
|
// Need to be associated with a staff |
|
|
val currentStaff = staffsService.currentStaff() ?: return emptyMap() |
|
|
val currentStaff = staffsService.currentStaff() ?: return emptyMap() |
|
|
return transformToTimeEntryMap(timesheetRepository.findAllByStaff(currentStaff)) |
|
|
return transformToTimeEntryMap(timesheetRepository.findAllByStaff(currentStaff)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
open fun getTimeMemberTimesheet(): Map<Long, TeamMemberTimeEntries> { |
|
|
|
|
|
val currentStaff = staffsService.currentStaff() ?: return emptyMap() |
|
|
|
|
|
// Get team where current staff is team lead |
|
|
|
|
|
val myTeam = teamService.getMyTeamForStaff(currentStaff) ?: return emptyMap() |
|
|
|
|
|
|
|
|
|
|
|
val teamMembers = staffsService.findAllByTeamId(myTeam.id!!).getOrDefault(emptyList()) |
|
|
|
|
|
|
|
|
|
|
|
return teamMembers.associate { member -> |
|
|
|
|
|
Pair( |
|
|
|
|
|
member.id!!, |
|
|
|
|
|
TeamMemberTimeEntries( |
|
|
|
|
|
staffId = member.staffId, |
|
|
|
|
|
name = member.name, |
|
|
|
|
|
timeEntries = transformToTimeEntryMap(timesheetRepository.findAllByStaff(member)) |
|
|
|
|
|
) |
|
|
|
|
|
) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
private fun transformToTimeEntryMap(timesheets: List<Timesheet>): Map<String, List<TimeEntry>> { |
|
|
private fun transformToTimeEntryMap(timesheets: List<Timesheet>): Map<String, List<TimeEntry>> { |
|
|
return timesheets |
|
|
return timesheets |
|
|
.groupBy { timesheet -> timesheet.recordDate!!.format(DateTimeFormatter.ISO_LOCAL_DATE) } |
|
|
.groupBy { timesheet -> timesheet.recordDate!!.format(DateTimeFormatter.ISO_LOCAL_DATE) } |
|
|