From b0eea4b574c33daf1884857ecd47bbd83f0b252c Mon Sep 17 00:00:00 2001 From: "cyril.tsui" Date: Wed, 26 Jun 2024 14:58:19 +0800 Subject: [PATCH] update --- .../modules/project/service/InvoiceService.kt | 8 +- .../project/service/ProjectsService.kt | 7 +- .../modules/report/service/ReportService.kt | 4 +- .../timesheet/service/TimesheetsService.kt | 93 ++++++++++++------- 4 files changed, 67 insertions(+), 45 deletions(-) diff --git a/src/main/java/com/ffii/tsms/modules/project/service/InvoiceService.kt b/src/main/java/com/ffii/tsms/modules/project/service/InvoiceService.kt index bb087eb..950ab7e 100644 --- a/src/main/java/com/ffii/tsms/modules/project/service/InvoiceService.kt +++ b/src/main/java/com/ffii/tsms/modules/project/service/InvoiceService.kt @@ -362,7 +362,7 @@ open class InvoiceService( // Check the import invoice with the data in DB for (i in 2..sheet.lastRowNum){ - val sheetInvoice = ExcelUtils.getCell(sheet, i, 0).stringCellValue + val sheetInvoice = ExcelUtils.getCell(sheet, i, 0).toString() val sheetProjectCode = ExcelUtils.getCell(sheet, i, 1).stringCellValue checkInvoiceNo(sheetInvoice, invoices, invoicesResult, true) checkProjectCode(sheetProjectCode, projects, newProjectCodes) @@ -398,7 +398,7 @@ open class InvoiceService( // val paymentMilestoneId = getMilestonePaymentId(ExcelUtils.getCell(sheet, i, 1).stringCellValue, ExcelUtils.getCell(sheet, i, 5).stringCellValue) // val milestonePayment = milestonePaymentRepository.findById(paymentMilestoneId).orElseThrow() val invoice = Invoice().apply { - invoiceNo = ExcelUtils.getCell(sheet, i, 0).stringCellValue + invoiceNo = ExcelUtils.getCell(sheet, i, 0).toString() projectCode = ExcelUtils.getCell(sheet, i, 1).stringCellValue projectName = ExcelUtils.getCell(sheet, i, 2).stringCellValue team = ExcelUtils.getCell(sheet, i, 3).stringCellValue @@ -447,7 +447,7 @@ open class InvoiceService( val duplicateItemsInInvoice = checkDuplicateItemInImportedInvoice(sheet,2,0) for (i in 2..sheet.lastRowNum){ - val sheetInvoice = ExcelUtils.getCell(sheet, i, 0).stringCellValue + val sheetInvoice = ExcelUtils.getCell(sheet, i, 0).toString() val sheetProjectCode = ExcelUtils.getCell(sheet, i, 1).stringCellValue checkInvoiceNo(sheetInvoice, invoices, invoicesResult, false) checkProjectCode(sheetProjectCode, projects, newProjectCodes) @@ -489,7 +489,7 @@ open class InvoiceService( } for (i in 2..sheet.lastRowNum){ - val invoice = getInvoiceByInvoiceNo(ExcelUtils.getCell(sheet, i, 0).stringCellValue) + val invoice = getInvoiceByInvoiceNo(ExcelUtils.getCell(sheet, i, 0).toString()) invoice.paidAmount = ExcelUtils.getCell(sheet, i, 5).numericCellValue.toBigDecimal() invoice.receiptDate = ExcelUtils.getCell(sheet, i, 4).dateCellValue.toInstant().atZone(ZoneId.systemDefault()).toLocalDate() saveAndFlush(invoice) 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 d659eb1..2748323 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 @@ -200,11 +200,10 @@ open class ProjectsService( totalManhour = request.totalManhour actualStart = request.projectActualStart actualEnd = request.projectActualEnd - status = request.projectStatus - ?: if (this.status == "Deleted" || this.deleted == true) "Deleted" + status = if (this.status == "Deleted" || this.deleted == true) "Deleted" else if (this.actualStart != null && this.actualEnd != null) "Completed" else if (this.actualStart != null) "On-going" - else "Pending To Start" + else request.projectStatus ?: "Pending To Start" isClpProject = request.isClpProject this.mainProject = mainProject @@ -526,7 +525,7 @@ open class ProjectsService( Project().apply { name = row.getCell(1).stringCellValue description = row.getCell(1).stringCellValue - code = splitProjectCode[0] + code = splitMainProjectCode[0] + '-' + String.format("%04d", splitMainProjectCode[1].toInt()) status = "Completed" projectCategory = projectCategoryRepository.findById(1).orElseThrow() customer = currentClient diff --git a/src/main/java/com/ffii/tsms/modules/report/service/ReportService.kt b/src/main/java/com/ffii/tsms/modules/report/service/ReportService.kt index d148010..f7796cb 100644 --- a/src/main/java/com/ffii/tsms/modules/report/service/ReportService.kt +++ b/src/main/java/com/ffii/tsms/modules/report/service/ReportService.kt @@ -691,12 +691,12 @@ open class ReportService( rowIndex = 10 sheet.getRow(rowIndex).apply { createCell(1).apply { - setCellValue(project.expectedTotalFee!! * 0.8) + setCellValue(if (project.expectedTotalFee != null) project.expectedTotalFee!! * 0.8 else 0.0) cellStyle.dataFormat = accountingStyle } createCell(2).apply { - setCellValue(project.expectedTotalFee!!) + setCellValue(project.expectedTotalFee ?: 0.0) cellStyle.dataFormat = accountingStyle } } 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 3f87ff9..0b4bf12 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 @@ -45,14 +45,15 @@ open class TimesheetsService( mergeTimeEntriesByProjectAndTask(timeEntries).map { timeEntry -> val task = timeEntry.taskId?.let { taskRepository.findById(it).getOrNull() } val project = timeEntry.projectId?.let { projectRepository.findById(it).getOrNull() } - val projectTask = project?.let { p -> task?.let { t -> projectTaskRepository.findByProjectAndTask(p, t) } } + val projectTask = + project?.let { p -> task?.let { t -> projectTaskRepository.findByProjectAndTask(p, t) } } Timesheet().apply { this.staff = currentStaff this.recordDate = entryDate this.normalConsumed = timeEntry.inputHours this.otConsumed = timeEntry.otHours - this.projectTask = projectTask + this.projectTask = projectTask this.project = project this.remark = timeEntry.remark } @@ -65,7 +66,11 @@ open class TimesheetsService( } @Transactional - open fun saveMemberTimeEntry(staffId: Long, entry: TimeEntry, recordDate: LocalDate?): Map> { + open fun saveMemberTimeEntry( + staffId: Long, + entry: TimeEntry, + recordDate: LocalDate? + ): Map> { val authorities = staffsService.currentAuthorities() ?: throw BadRequestException() if (!authorities.stream().anyMatch { it.authority.equals("MAINTAIN_TIMESHEET") }) { @@ -80,11 +85,11 @@ open class TimesheetsService( 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) } } + val projectTask = project?.let { p -> task?.let { t -> projectTaskRepository.findByProjectAndTask(p, t) } } this.normalConsumed = entry.inputHours this.otConsumed = entry.otHours - this.projectTask = projectTask + this.projectTask = projectTask this.project = project this.remark = entry.remark this.recordDate = this.recordDate ?: recordDate @@ -114,7 +119,7 @@ open class TimesheetsService( open fun getTeamMemberTimesheet(): Map { val authorities = staffsService.currentAuthorities() ?: return emptyMap() - if (authorities.stream().anyMatch { it.authority.equals("MAINTAIN_TIMESHEET")}) { + if (authorities.stream().anyMatch { it.authority.equals("MAINTAIN_TIMESHEET") }) { val currentStaff = staffsService.currentStaff() // Get team where current staff is team lead @@ -141,27 +146,31 @@ open class TimesheetsService( private fun transformToTimeEntryMap(timesheets: List): Map> { return timesheets .groupBy { timesheet -> timesheet.recordDate!!.format(DateTimeFormatter.ISO_LOCAL_DATE) } - .mapValues { (_, timesheets) -> timesheets.map { timesheet -> - TimeEntry( - id = timesheet.id!!, - projectId = timesheet.projectTask?.project?.id ?: timesheet.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 - ) - } } + .mapValues { (_, timesheets) -> + timesheets.map { timesheet -> + TimeEntry( + id = timesheet.id!!, + projectId = timesheet.projectTask?.project?.id ?: timesheet.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 + ) + } + } } private fun mergeTimeEntriesByProjectAndTask(entries: List): List { return entries .groupBy { timeEntry -> Pair(timeEntry.projectId, timeEntry.taskId) } .values.map { timeEntries -> - timeEntries.reduce { acc, timeEntry -> acc.copy( - inputHours = (acc.inputHours ?: 0.0) + (timeEntry.inputHours ?: 0.0), - otHours = (acc.otHours ?: 0.0) + (timeEntry.otHours ?: 0.0) - ) } + timeEntries.reduce { acc, timeEntry -> + acc.copy( + inputHours = (acc.inputHours ?: 0.0) + (timeEntry.inputHours ?: 0.0), + otHours = (acc.otHours ?: 0.0) + (timeEntry.otHours ?: 0.0) + ) + } } } @@ -173,13 +182,18 @@ open class TimesheetsService( return "No Excel import" // if workbook is null } + val notExistProjectList = mutableListOf() val sheet: Sheet = workbook.getSheetAt(0) + logger.info("---------Start Import Timesheets-------") val timesheetList = mutableListOf().toMutableList(); for (i in 1.. task?.let { t -> projectTaskRepository.findByProjectAndTask(p, t) } } + val projectTask = + project?.let { p -> task?.let { t -> projectTaskRepository.findByProjectAndTask(p, t) } } // process record date logger.info("---------record date-------") @@ -221,20 +239,25 @@ open class TimesheetsService( 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 + if (project != null) { + timesheetList += Timesheet().apply { + this.staff = staff + this.recordDate = recordDate + this.normalConsumed = normalHours + this.otConsumed = otHours + this.projectTask = projectTask + this.project = project + } + } else { + notExistProjectList += projectCode } } } timesheetRepository.saveAll(timesheetList) logger.info("---------end-------") + logger.info("Not Exist Project List: "+ notExistProjectList.joinToString(", ")) - return if (sheet.lastRowNum > 0) "Import Excel success" else "Import Excel failure" + return if (sheet.lastRowNum > 0) "Import Excel success btw " + notExistProjectList.joinToString(", ") else "Import Excel failure" } } \ No newline at end of file