Selaa lähdekoodia

Add import OS Timesheets function

tags/Baseline_30082024_BACKEND_UAT
cyril.tsui 1 vuosi sitten
vanhempi
commit
d786d75257
2 muutettua tiedostoa jossa 100 lisäystä ja 6 poistoa
  1. +69
    -0
      src/main/java/com/ffii/tsms/modules/timesheet/service/TimesheetsService.kt
  2. +31
    -6
      src/main/java/com/ffii/tsms/modules/timesheet/web/TimesheetsController.kt

+ 69
- 0
src/main/java/com/ffii/tsms/modules/timesheet/service/TimesheetsService.kt Näytä tiedosto

@@ -249,6 +249,75 @@ open class TimesheetsService(
return if (sheet.lastRowNum > 0) "Import Excel success btw " + notExistProjectList.distinct().joinToString(", ") else "Import Excel failure"
}

@Transactional(rollbackFor = [Exception::class])
open fun importOSFile(workbook: Workbook?): String {
val logger = LogFactory.getLog(javaClass)

if (workbook == null) {
return "No Excel import" // if workbook is null
}

val notExistStaffIdList = mutableListOf<String>()
val sheet: Sheet = workbook.getSheetAt(0)

logger.info("---------Start Import OS Timesheets-------")
val timesheetList = mutableListOf<Timesheet>().toMutableList();
for (i in 1..<sheet.lastRowNum) {
val row = sheet.getRow(i)

if (row.getCell(1) != null && !row.getCell(
1
).stringCellValue.isNullOrBlank()
) {
logger.info("row :$i | lastCellNum" + row.lastCellNum)

// process staff
logger.info("---------staff-------")
var staff = staffRepository.findByStaffId(row.getCell(1).stringCellValue).getOrNull()
if (staff == null) {
notExistStaffIdList += row.getCell(1).stringCellValue
staff = staffRepository.findByStaffId("B000").orElseThrow()
}

// process project
logger.info("---------project-------")

val project = null

// process project task
logger.info("---------project task-------")
val projectTask = null

val nonBillableTask = taskRepository.findById(44).orElseThrow()
// process record date
logger.info("---------record date-------")
val formatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy")
val recordDate = LocalDate.parse(row.getCell(7).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
this.nonBillableTask = nonBillableTask
}
}
}

timesheetRepository.saveAll(timesheetList)
logger.info("---------end-------")

return if (sheet.lastRowNum > 0) "Import Excel success btw staffId:" + notExistStaffIdList.distinct().joinToString(", ") else "Import Excel failure"
}

@Transactional(rollbackFor = [Exception::class])
open fun rearrangeTimesheets(): String {
val logger = LogFactory.getLog(javaClass)


+ 31
- 6
src/main/java/com/ffii/tsms/modules/timesheet/web/TimesheetsController.kt Näytä tiedosto

@@ -3,6 +3,7 @@ package com.ffii.tsms.modules.timesheet.web
import com.ffii.core.exception.BadRequestException
import com.ffii.tsms.modules.timesheet.entity.LeaveType
import com.ffii.tsms.modules.timesheet.service.LeaveService
import com.ffii.tsms.modules.timesheet.service.MailReminderService
import com.ffii.tsms.modules.timesheet.service.TimesheetsService
import com.ffii.tsms.modules.timesheet.web.models.*
import jakarta.servlet.http.HttpServletRequest
@@ -11,18 +12,16 @@ 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.bind.annotation.*
import org.springframework.web.multipart.MultipartHttpServletRequest
import java.io.UnsupportedEncodingException
import java.text.ParseException
import java.time.LocalDate
import java.time.format.DateTimeFormatter

@RestController
@RequestMapping("/timesheets")
class TimesheetsController(private val timesheetsService: TimesheetsService, private val leaveService: LeaveService) {
class TimesheetsController(private val timesheetsService: TimesheetsService, private val leaveService: LeaveService, private val mailReminderService: MailReminderService) {
@PostMapping("/save")
fun newTimesheetEntry(@Valid @RequestBody recordTimesheet: Map<String, List<TimeEntry>>): Map<String, List<TimeEntry>> {
val parsedEntries = kotlin.runCatching {
@@ -137,6 +136,22 @@ class TimesheetsController(private val timesheetsService: TimesheetsService, pri
return ResponseEntity.ok(timesheetsService.importFile(workbook))
}

@PostMapping("/import-OS")
@Throws(ServletRequestBindingException::class)
fun importOSFile(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.importOSFile(workbook))
}

@PostMapping("/import-leave")
@Throws(ServletRequestBindingException::class)
fun importLeaveFile(request: HttpServletRequest): ResponseEntity<*> {
@@ -159,4 +174,14 @@ class TimesheetsController(private val timesheetsService: TimesheetsService, pri

return ResponseEntity.ok(timesheetsService.rearrangeTimesheets())
}

// @GetMapping("/testReminder")
// @Throws(
// UnsupportedEncodingException::class,
// ParseException::class,
// ServletRequestBindingException::class
// )
// fun testReminder(request: HttpServletRequest) {
// mailReminderService.sendTimesheetReminder()
// }
}

Ladataan…
Peruuta
Tallenna