diff --git a/src/main/java/com/ffii/tsms/modules/common/mail/service/MailReminderService.kt b/src/main/java/com/ffii/tsms/modules/common/mail/service/MailReminderService.kt index 5b1bcf2..e7fccf1 100644 --- a/src/main/java/com/ffii/tsms/modules/common/mail/service/MailReminderService.kt +++ b/src/main/java/com/ffii/tsms/modules/common/mail/service/MailReminderService.kt @@ -128,7 +128,7 @@ open class MailReminderService( staffId = it["staffId"].toString().toLong(), recordDate = LocalDate.parse(it["recordDate"].toString()), hours = it["hours"].toString().toDouble(), - joinDate = LocalDate.parse(it["joinDate"].toString()) + joinDate = it["joinDate"]?.toString()?.let { date -> LocalDate.parse(date) } ?: LocalDate.of(1970, 1, 1) ) } val staffs = staffRepository.findAllByEmployTypeAndDeletedFalseAndDepartDateIsNull(FULLTIME).filter { it.staffId != "A003" && it.staffId != "A004" && it.staffId != "B011" }.filter{ it.team?.code != "HO"} @@ -217,7 +217,7 @@ open class MailReminderService( staffId = it["staffId"].toString().toLong(), recordDate = LocalDate.parse(it["recordDate"].toString()), hours = it["hours"].toString().toDouble(), - joinDate = LocalDate.parse(it["joinDate"].toString()) + joinDate = it["joinDate"]?.toString()?.let { date -> LocalDate.parse(date) } ?: LocalDate.of(1970, 1, 1) ) } val staffs = staffRepository.findAllByEmployTypeAndDeletedFalseAndDepartDateIsNull(FULLTIME).filter { it.staffId != "A003" && it.staffId != "A004" && it.staffId != "B011" }.filter{ it.team?.code != "HO"} @@ -256,6 +256,7 @@ open class MailReminderService( Pair(date, matchedStaffIds) }.sortedBy { it.first } + println(goodStaffsList) } } @@ -279,7 +280,7 @@ open class MailReminderService( staffId = it["staffId"].toString().toLong(), recordDate = LocalDate.parse(it["recordDate"].toString()), hours = it["hours"].toString().toDouble(), - joinDate = LocalDate.parse(it["joinDate"].toString()) + joinDate = it["joinDate"]?.toString()?.let { date -> LocalDate.parse(date) } ?: LocalDate.of(1970, 1, 1) ) } val staffs = staffRepository.findAllByEmployTypeAndDeletedFalseAndDepartDateIsNull(FULLTIME).filter { it.staffId != "A003" && it.staffId != "A004" && it.staffId != "B011" }.filter{ it.team?.code != "HO"} @@ -352,6 +353,75 @@ open class MailReminderService( createEmailRequest(emailContent.toString(), receiver) } } + + open fun test15thStaffList() { + if (!isSettingsConfigValid()) return + val today = LocalDate.now().minusDays(1) // should always be 14, exclude 15th because the email is sent at 0600, suppose no one input timesheet in advance + val firstDay = today.withDayOfMonth(1) + val holidayList = holidayService.commonHolidayList().map { it.date } + val companyHolidayList = companyHolidayService.allCompanyHolidays().map { it.date } + val allHolidaysList: List = (holidayList + companyHolidayList).toSet().toList() + //get data + val args = mutableMapOf( + "from" to firstDay, + "to" to today, + ) + val ts = timesheetsService.workHourRecordsWithinRange(args) + val timesheet: List = ts.map { + WorkHourRecordsWithJoinDate( + staffId = it["staffId"].toString().toLong(), + recordDate = LocalDate.parse(it["recordDate"].toString()), + hours = it["hours"].toString().toDouble(), + joinDate = it["joinDate"]?.toString()?.let { date -> LocalDate.parse(date) } ?: LocalDate.of(1970, 1, 1) + ) + } + val staffs = staffRepository.findAllByEmployTypeAndDeletedFalseAndDepartDateIsNull(FULLTIME).filter { it.staffId != "A003" && it.staffId != "A004" && it.staffId != "B011" }.filter{ it.team?.code != "HO"} + val teams = teamRepository.findAll().filter { team -> team.deleted == false +// && ( team.code == "WY" || team.code == "TW" || team.code == "CH" || team.code == "MN" || team.code == "MC" ) + } + val dateList = generateSequence(firstDay) { it.plusDays(1) } + .takeWhile { it <= today } + .toList() + val filteredDatesList = dateList.filter { + it !in allHolidaysList && it.dayOfWeek != DayOfWeek.SATURDAY && it.dayOfWeek != DayOfWeek.SUNDAY + } + //loop each team + for (team in teams) { +// if (team.id?.toInt() != 5) continue // just for testing with fewer records, remove this when finishes + val teamLead = team.staff + val teamMembers: List = staffs.filter { it.team != null &&it.team.id == team.id } + if (teamMembers.isEmpty()) continue + val teamMembersIds: List = teamMembers.map { it.id }.sorted() + // getting the naughty list + val filteredTimesheet = timesheet.filter { teamMembersIds.contains(it.staffId) } // filter team members' timesheet + val timesheetByIdAndRecord = filteredTimesheet.groupBy { Triple(it.staffId, it.recordDate, it.joinDate) } + .mapNotNull { (key, records) -> + StaffRecords( + key.second, + key.first, + records.sumOf { it.hours }, + key.third + ) + } + // change the date list with desired time range + val goodStaffsList = filteredDatesList.map { date -> + val matchedStaffIds = timesheetByIdAndRecord + .filter { + if(it.joinDate!! <= date){ + it.recordDate == date && it.sumOfHours >= 8 + }else{ + true + } + } + .map { it.staffId } // Extracting the second element (staffId) + .distinct() + // Returning a Pair of the date and the list of matched staff IDs + Pair(date, matchedStaffIds) + }.sortedBy { it.first } + + println(goodStaffsList) + } + } @Scheduled(cron = "0 0 6 * * ?") // (SS/MM/HH/DD/MM/YY) - Runs at 06:00 AM every day open fun sendTimesheetReminder() { if (!isSettingsConfigValid()) return