Prevent Users to Submit Their Timesheet If Daily Required Hours are not Completed

Following script uses WorklogHelper service to get daily required work hours and daily actual work hours of the user for the timesheet period and checks the differences for totals. If user is submitted less than required work hours, it rejects timesheet submission.

import com.atlassian.jira.component.ComponentAccessor; import com.atlassian.jira.security.JiraAuthenticationContext; import com.atlassian.jira.security.roles.ProjectRoleManager; import com.deniz.jira.worklog.services.*; import com.deniz.jira.worklog.utils.*; import java.time.format.DateTimeFormatter; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.LocalDate; import java.time.ZoneId; import java.time.ZoneRegion; import java.time.ZonedDateTime; import com.deniz.jira.worklog.utils.DateTimeHelper; import static com.deniz.jira.worklog.utils.DateTimeHelper.withEndOfDay; //We need to load WorklogPRO classes differently using getOSGiComponentInstanceOfType def worklogHelper = ComponentAccessor.getOSGiComponentInstanceOfType(WorklogHelper.class); def dateTimeHelper=ComponentAccessor.getOSGiComponentInstanceOfType(DateTimeHelper.class); //please enable logging for package "com.deniz.jira.worklog.scripting" from Administration/System/Logging and Profiling log.debug("userKey, {}", timesheetApprovalRequest.userKey); log.debug("action, {}", timesheetApprovalRequest.action); log.debug("explanation, {}", timesheetApprovalRequest.explanation); log.debug("startDate, {}", timesheetApprovalRequest.period.startDate); log.debug("endDate, {}", timesheetApprovalRequest.period.endDate); ZonedDateTime startDate = dateTimeHelper.parseWithISODateFormat(timesheetApprovalRequest.period.getStartDate()); ZonedDateTime endDate = dateTimeHelper.getEndOfDay(dateTimeHelper.parseWithISODateFormat(timesheetApprovalRequest.period.getEndDate())); log.debug("Period with Hours:{}-{}", startDate, endDate); if (timesheetApprovalRequest.action == "SEND_TO_APPROVAL") { long[] dailyRequiredWorkInSeconds = worklogHelper.getDailyRequiredWorkInSeconds(timesheetApprovalRequest.userKey, startDate, endDate); log.debug("dailyRequiredWorkInSeconds:{}", dailyRequiredWorkInSeconds); long[] dailyActualWorkInSeconds = worklogHelper.getDailyActualWorkInSeconds(timesheetApprovalRequest.userKey, startDate, endDate); log.debug("dailyActualWorkInSeconds:{}", dailyActualWorkInSeconds); long requiredTotal = dailyRequiredWorkInSeconds.sum(); log.debug("requiredTotal(seconds):{}", requiredTotal); long actualTotal = dailyActualWorkInSeconds.sum(); log.debug("actualTotal(seconds):{}", actualTotal); if (actualTotal < requiredTotal) { return String.format("You can't submit timesheet. You have only provided %.2f of %.2f", actualTotal/3600, requiredTotal/3600); } }