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 f668a71..d659eb1 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 @@ -480,7 +480,7 @@ open class ProjectsService( for (i in 2.. 0, + isSubsidiaryContact = subsidiary?.id != null && subsidiary.id!! > 0, manhourPercentageByGrade = taskTemplate.gradeAllocations.associateBy( keySelector = { it.grade!!.id!! }, valueTransform = { it.percentage!! } 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 a387127..3f87ff9 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 @@ -1,17 +1,22 @@ package com.ffii.tsms.modules.timesheet.service import com.ffii.core.exception.BadRequestException +import com.ffii.core.utils.ExcelUtils +import com.ffii.tsms.modules.data.entity.BuildingType import com.ffii.tsms.modules.data.entity.Staff import com.ffii.tsms.modules.data.entity.StaffRepository +import com.ffii.tsms.modules.data.entity.WorkNature 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.ProjectTaskRepository -import com.ffii.tsms.modules.project.entity.TaskRepository +import com.ffii.tsms.modules.project.entity.* +import com.ffii.tsms.modules.project.web.models.* import com.ffii.tsms.modules.timesheet.entity.Timesheet 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 org.apache.commons.logging.LogFactory +import org.apache.poi.ss.usermodel.Sheet +import org.apache.poi.ss.usermodel.Workbook import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional import java.time.LocalDate @@ -159,4 +164,77 @@ open class TimesheetsService( ) } } } + + @Transactional(rollbackFor = [Exception::class]) + open fun importFile(workbook: Workbook?): String { + val logger = LogFactory.getLog(javaClass) + + if (workbook == null) { + return "No Excel import" // if workbook is null + } + + val sheet: Sheet = workbook.getSheetAt(0) + + val timesheetList = mutableListOf().toMutableList(); + for (i in 1.. task?.let { t -> projectTaskRepository.findByProjectAndTask(p, t) } } + + // process record date + logger.info("---------record date-------") + val formatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy") + val recordDate = LocalDate.parse(row.getCell(6).toString(), formatter); + + // normal hour + ot hour + logger.info("---------normal hour + ot hour-------") + val hours: Double = row.getCell(7).numericCellValue + val normalHours = if (hours > 8.0) 8.0 else hours + val otHours = if (hours > 8.0) hours - 8.0 else 0.0 + + timesheetList += Timesheet().apply { + this.staff = staff + this.recordDate = recordDate + this.normalConsumed = normalHours + this.otConsumed = otHours + this.projectTask = projectTask + this.project = project + } + } + } + + timesheetRepository.saveAll(timesheetList) + logger.info("---------end-------") + + return if (sheet.lastRowNum > 0) "Import Excel success" else "Import Excel failure" + } } \ No newline at end of file diff --git a/src/main/java/com/ffii/tsms/modules/timesheet/web/TimesheetsController.kt b/src/main/java/com/ffii/tsms/modules/timesheet/web/TimesheetsController.kt index 6f2d4ce..29d1c28 100644 --- a/src/main/java/com/ffii/tsms/modules/timesheet/web/TimesheetsController.kt +++ b/src/main/java/com/ffii/tsms/modules/timesheet/web/TimesheetsController.kt @@ -5,12 +5,18 @@ import com.ffii.tsms.modules.timesheet.entity.LeaveType import com.ffii.tsms.modules.timesheet.service.LeaveService import com.ffii.tsms.modules.timesheet.service.TimesheetsService import com.ffii.tsms.modules.timesheet.web.models.* +import jakarta.servlet.http.HttpServletRequest import jakarta.validation.Valid +import org.apache.poi.ss.usermodel.Workbook +import org.apache.poi.xssf.usermodel.XSSFWorkbook +import org.springframework.http.ResponseEntity +import org.springframework.web.bind.ServletRequestBindingException import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestController +import org.springframework.web.multipart.MultipartHttpServletRequest import java.time.LocalDate import java.time.format.DateTimeFormatter @@ -85,4 +91,20 @@ class TimesheetsController(private val timesheetsService: TimesheetsService, pri fun leaveTypes(): List { return leaveService.getLeaveTypes() } + + @PostMapping("/import") + @Throws(ServletRequestBindingException::class) + fun importFile(request: HttpServletRequest): ResponseEntity<*> { + var workbook: Workbook? = null + + try { + val multipartFile = (request as MultipartHttpServletRequest).getFile("multipartFileList") + workbook = XSSFWorkbook(multipartFile?.inputStream) + } catch (e: Exception) { + println("Excel Wrong") + println(e) + } + + return ResponseEntity.ok(timesheetsService.importFile(workbook)) + } } \ No newline at end of file