@@ -52,7 +52,6 @@ open class MailReminderService(
val username = settingsService.findByName(SettingNames.MAIL_SMTP_USERNAME).orElseThrow().value
val username = settingsService.findByName(SettingNames.MAIL_SMTP_USERNAME).orElseThrow().value
val pw = settingsService.findByName(SettingNames.MAIL_SMTP_USERNAME).orElseThrow().value
val pw = settingsService.findByName(SettingNames.MAIL_SMTP_USERNAME).orElseThrow().value
return !username.isNullOrEmpty() && !pw.isNullOrEmpty()
return !username.isNullOrEmpty() && !pw.isNullOrEmpty()
} catch (e: Error) {
} catch (e: Error) {
return false
return false
}
}
@@ -106,13 +105,9 @@ open class MailReminderService(
open fun sendTimesheetToTeamLead7TH() {
open fun sendTimesheetToTeamLead7TH() {
if (!isSettingsConfigValid()) return
if (!isSettingsConfigValid()) return
val today = LocalDate.now()
val today = LocalDate.now()
val dayOfWeek = today.dayOfWeek
val holidayList = holidayService.commonHolidayList().map { it.date }
val holidayList = holidayService.commonHolidayList().map { it.date }
val companyHolidayList = companyHolidayService.allCompanyHolidays().map { it.date }
val companyHolidayList = companyHolidayService.allCompanyHolidays().map { it.date }
val allHolidaysList: List<LocalDate> = (holidayList + companyHolidayList).toSet().toList()
val allHolidaysList: List<LocalDate> = (holidayList + companyHolidayList).toSet().toList()
//check if today is holiday
if (allHolidaysList.contains(today) || dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY) return
// get working days from last month
// get working days from last month
val lastMonth = today.minusMonths(1)
val lastMonth = today.minusMonths(1)
val yearMonth = YearMonth.from(lastMonth)
val yearMonth = YearMonth.from(lastMonth)
@@ -133,25 +128,17 @@ open class MailReminderService(
hours = it["hours"].toString().toDouble()
hours = it["hours"].toString().toDouble()
)
)
}
}
// val timesheet = timesheetRepository.findByDeletedFalseAndRecordDateBetweenOrderByRecordDate(filteredLastMonthDays.first(),filteredLastMonthDays.last())
val staffs = staffRepository.findAllByEmployTypeAndDeletedFalseAndDepartDateIsNull(FULLTIME)
val staffs = staffRepository.findAllByEmployTypeAndDeletedFalseAndDepartDateIsNull(FULLTIME)
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 teams = teamRepository.findAll().filter { team -> team.deleted == false }
val teams = teamRepository.findAll().filter { team -> team.deleted == false
// && ( team.code == "WY" || team.code == "TW" || team.code == "CH" || team.code == "MN" || team.code == "MC" )
}
for (team in teams) {
for (team in teams) {
// if (team.id?.toInt() != 5) continue // remove this when finishes
val teamLead = team.staff
val teamLead = team.staff
val teamMembers: List<Staff> = staffs.filter { it.team != null && it.team.id == team.id }
val teamMembers: List<Staff> = staffs.filter { it.team != null && it.team.id == team.id }
if (teamMembers.isEmpty()) continue
if (teamMembers.isEmpty()) continue
val teamMembersIds: List<Long?> = teamMembers.map { it.id }.sorted()
// val filteredTimesheet = timesheet.filter { teamMembersIds.contains(it.staff?.id) }
val teamMembersIds: List<Long> = teamMembers.map { it.id!! }.sorted()
val filteredTimesheet = timesheet.filter { teamMembersIds.contains(it.staffId) }
val filteredTimesheet = timesheet.filter { teamMembersIds.contains(it.staffId) }
// val timesheetByIdAndRecord = filteredTimesheet.groupBy { it.staff?.id to it.recordDate }
// .map { (key, _) ->
// val (staffId, recordDate) = key
// recordDate to mutableListOf<Long>(staffId ?: 0)
// }
println(filteredTimesheet.filter { it.staffId == 4.toLong() })
val timesheetByIdAndRecord = filteredTimesheet.groupBy { it.staffId to it.recordDate }
val timesheetByIdAndRecord = filteredTimesheet.groupBy { it.staffId to it.recordDate }
.mapNotNull { (key, records) ->
.mapNotNull { (key, records) ->
Triple(
Triple(
@@ -160,12 +147,6 @@ open class MailReminderService(
records.sumOf { it.hours }
records.sumOf { it.hours }
)
)
}
}
// val goodStaffsList = filteredLastMonthDays.map { day ->
// timesheetByIdAndRecord.find {
// it.first == day
// } ?: Pair(day, mutableListOf())
// }.sortedBy { it.first }
val goodStaffsList = filteredLastMonthDays.map { date ->
val goodStaffsList = filteredLastMonthDays.map { date ->
val matchedStaffIds = timesheetByIdAndRecord
val matchedStaffIds = timesheetByIdAndRecord
.filter { it.first == date && it.third >= 8 }
.filter { it.first == date && it.third >= 8 }
@@ -203,13 +184,11 @@ open class MailReminderService(
@Scheduled(cron = "0 0 6 15 * ?") // (SS/MM/HH/DD/MM/YY)
@Scheduled(cron = "0 0 6 15 * ?") // (SS/MM/HH/DD/MM/YY)
open fun sendTimesheetToTeamLead15TH() {
open fun sendTimesheetToTeamLead15TH() {
if (!isSettingsConfigValid()) return
if (!isSettingsConfigValid()) return
val today = LocalDate.now() // should always be 15
val today = LocalDate.now() // should always be 15
val firstDay = today.withDayOfMonth(1)
val firstDay = today.withDayOfMonth(1)
val holidayList = holidayService.commonHolidayList().map { it.date }
val holidayList = holidayService.commonHolidayList().map { it.date }
val companyHolidayList = companyHolidayService.allCompanyHolidays().map { it.date }
val companyHolidayList = companyHolidayService.allCompanyHolidays().map { it.date }
val allHolidaysList: List<LocalDate> = (holidayList + companyHolidayList).toSet().toList()
val allHolidaysList: List<LocalDate> = (holidayList + companyHolidayList).toSet().toList()
//get data
//get data
val args = mutableMapOf(
val args = mutableMapOf(
"from" to firstDay,
"from" to firstDay,
@@ -223,18 +202,16 @@ open class MailReminderService(
hours = it["hours"].toString().toDouble()
hours = it["hours"].toString().toDouble()
)
)
}
}
// val timesheet = timesheetRepository.findByDeletedFalseAndRecordDateBetweenOrderByRecordDate(firstDay, today)
val staffs = staffRepository.findAllByEmployTypeAndDeletedFalseAndDepartDateIsNull(FULLTIME)
val staffs = staffRepository.findAllByEmployTypeAndDeletedFalseAndDepartDateIsNull(FULLTIME)
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 teams = teamRepository.findAll().filter { team -> team.deleted == false }
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) }
val dateList = generateSequence(firstDay) { it.plusDays(1) }
.takeWhile { it <= today }
.takeWhile { it <= today }
.toList()
.toList()
val filteredDatesList = dateList.filter {
val filteredDatesList = dateList.filter {
it !in allHolidaysList && it.dayOfWeek != DayOfWeek.SATURDAY && it.dayOfWeek != DayOfWeek.SUNDAY
it !in allHolidaysList && it.dayOfWeek != DayOfWeek.SATURDAY && it.dayOfWeek != DayOfWeek.SUNDAY
}
}
//loop each team
//loop each team
for (team in teams) {
for (team in teams) {
// if (team.id?.toInt() != 5) continue // just for testing with fewer records, remove this when finishes
// if (team.id?.toInt() != 5) continue // just for testing with fewer records, remove this when finishes
@@ -260,7 +237,6 @@ open class MailReminderService(
// Returning a Pair of the date and the list of matched staff IDs
// Returning a Pair of the date and the list of matched staff IDs
Pair(date, matchedStaffIds)
Pair(date, matchedStaffIds)
}.sortedBy { it.first }
}.sortedBy { it.first }
// creating the email content
// creating the email content
val intro = StringBuilder("${teamLead.name}, Staffs Missing Timesheet in the Table Below: ($firstDay-$today) \n")
val intro = StringBuilder("${teamLead.name}, Staffs Missing Timesheet in the Table Below: ($firstDay-$today) \n")
val tableData = mutableListOf<TableRow>()
val tableData = mutableListOf<TableRow>()
@@ -288,10 +264,8 @@ open class MailReminderService(
@Scheduled(cron = "0 0 6 * * ?") // (SS/MM/HH/DD/MM/YY) - Runs at 06:00 AM every day
@Scheduled(cron = "0 0 6 * * ?") // (SS/MM/HH/DD/MM/YY) - Runs at 06:00 AM every day
open fun sendTimesheetReminder() {
open fun sendTimesheetReminder() {
if (!isSettingsConfigValid()) return
if (!isSettingsConfigValid()) return
val today = LocalDate.now()
val today = LocalDate.now()
val dayOfWeek = today.dayOfWeek
val dayOfWeek = today.dayOfWeek
val holidayList = holidayService.commonHolidayList().map { it.date }
val holidayList = holidayService.commonHolidayList().map { it.date }
val companyHolidayList = companyHolidayService.allCompanyHolidays().map { it.date }
val companyHolidayList = companyHolidayService.allCompanyHolidays().map { it.date }
val allHolidaysList: List<LocalDate> = (holidayList + companyHolidayList).toSet().toList()
val allHolidaysList: List<LocalDate> = (holidayList + companyHolidayList).toSet().toList()
@@ -302,9 +276,7 @@ open class MailReminderService(
var fourDaysBefore: LocalDate = today
var fourDaysBefore: LocalDate = today
var sevenDaysBefore: LocalDate = today
var sevenDaysBefore: LocalDate = today
var daysBefore: LocalDate = today.minusDays(1)
var daysBefore: LocalDate = today.minusDays(1)
val workingDaysList = mutableListOf<LocalDate>()
val workingDaysList = mutableListOf<LocalDate>()
while (true) {
while (true) {
if (!allHolidaysList.contains(daysBefore) && daysBefore.dayOfWeek != DayOfWeek.SATURDAY && daysBefore.dayOfWeek != DayOfWeek.SUNDAY) {
if (!allHolidaysList.contains(daysBefore) && daysBefore.dayOfWeek != DayOfWeek.SATURDAY && daysBefore.dayOfWeek != DayOfWeek.SUNDAY) {
daysChecking++
daysChecking++
@@ -335,22 +307,17 @@ open class MailReminderService(
)
)
}
}
// val timesheet = timesheetRepository.findByDeletedFalseAndRecordDateBetweenOrderByRecordDate(sevenDaysBefore, fourDaysBefore)
// val timesheet = timesheetRepository.findByDeletedFalseAndRecordDateBetweenOrderByRecordDate(sevenDaysBefore, fourDaysBefore)
val staffs = staffRepository.findAllByEmployTypeAndDeletedFalseAndDepartDateIsNull(FULLTIME).filter {
staff: Staff? ->
staff?.team?.code == "WY" ||
staff?.team?.code == "TW" ||
staff?.team?.code == "MN" ||
staff?.team?.code == "MC" ||
staff?.team?.code == "CH"
} // FT? FT? etc
val staffs = staffRepository.findAllByEmployTypeAndDeletedFalseAndDepartDateIsNull(FULLTIME)
// .filter {
// staff: Staff? ->
// staff?.team?.code == "WY" ||
// staff?.team?.code == "TW" ||
// staff?.team?.code == "MN" ||
// staff?.team?.code == "MC" ||
// staff?.team?.code == "CH"
// } // FT? FT? etc
// val staffs = staffRepository.findAllByEmployTypeAndDeletedFalseAndDepartDateIsNull(FULLTIME)
// val staffs = staffRepository.findAllByEmployTypeAndDeletedFalseAndDepartDateIsNull(FULLTIME)
val staffIds: List<Long> = staffs.map { it.id as Long }
val staffIds: List<Long> = staffs.map { it.id as Long }
// val timesheetByIdAndRecord = timesheet.groupBy { it.staff?.id to it.recordDate }
// .map { (key, _) ->
// val (staffId, recordDate) = key
// "$recordDate" to mutableListOf<Long>(staffId ?: 0)
// }
val timesheetByIdAndRecord = timesheet.groupBy {
val timesheetByIdAndRecord = timesheet.groupBy {
it.staffId to it.recordDate
it.staffId to it.recordDate
}.mapNotNull { (key, records) ->
}.mapNotNull { (key, records) ->
@@ -444,13 +411,6 @@ open class MailReminderService(
staffIds.forEach { id ->
staffIds.forEach { id ->
var isNaughty: Boolean = false
var isNaughty: Boolean = false
val message = StringBuilder("Enter timesheet for:")
val message = StringBuilder("Enter timesheet for:")
// val tableHtml = StringBuilder(
// "<table width='100%' border='1' align='center'>"
// + "<tr align='center'>"
// + "<td><b>Product Name <b></td>"
// + "<td><b>Count<b></td>"
// + "</tr>"
// )
goodStaffsList.forEach { (key, value) ->
goodStaffsList.forEach { (key, value) ->
if (!value.contains(id)) {
if (!value.contains(id)) {
isNaughty = true
isNaughty = true
@@ -459,7 +419,6 @@ open class MailReminderService(
}
}
println(message)
println(message)
fullLog.append("\n $id, $message")
fullLog.append("\n $id, $message")
println("about to return")
if (!isNaughty) return@forEach
if (!isNaughty) return@forEach
println("no return")
println("no return")
val emailAddress = staffRepository.findById(id).get().email
val emailAddress = staffRepository.findById(id).get().email