Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

You can execute execute a Groovy script just before usual handling of timesheet submission and stop the submission process by returning an error message. Within this script you can access Jira services, WorklogPRO services and services from other plugins. You can use ApprovalRequest TimesheetApprovalRequest parameter to access values submitted by the user. 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.

Code Block
languagegroovy
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.*;
 
//We need to load WorklogPRO classes differently using getOSGiComponentInstanceOfType
def worklogHelper = ComponentAccessor.getOSGiComponentInstanceOfType(WorklogHelper.class);

//please enable logging for package "com.deniz.jira.worklog.scripting" from Administration/System/Logging and Profiling
log.debug("userKey, {}", approvalRequesttimesheetApprovalRequest.userKey);
log.debug("action, {}", approvalRequesttimesheetApprovalRequest.action);
log.debug("explanation, {}", approvalRequesttimesheetApprovalRequest.explanation);
log.debug("startDate, {}", approvalRequesttimesheetApprovalRequest.period.startDate);
log.debug("endDate, {}", approvalRequesttimesheetApprovalRequest.period.endDate);

if (timesheetApprovalRequest.action == "SEND_TO_APPROVAL") {
  long[] dailyRequiredWorkInSeconds = worklogHelper.getDailyRequiredWorkInSeconds(approvalRequesttimesheetApprovalRequest.userKey, approvalRequesttimesheetApprovalRequest.period);
  log.debug("dailyRequiredWorkInSeconds:{}", dailyRequiredWorkInSeconds);
  
  long[] dailyActualWorkInSeconds = worklogHelper.getDailyActualWorkInSeconds(approvalRequesttimesheetApprovalRequest.userKey, approvalRequesttimesheetApprovalRequest.period);
  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 not submit timesheet. You have only provided %.2f of %.2f", actualTotal/3600, requiredTotal/3600);
  }
}