Checking Account Budget Overflow

Checking budget overflow for every worklog entry is costly operation. But if your accounts are small and does not contain a lot of worklogs you can check account budget before worklog entry automatically.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 import com.atlassian.jira.component.ComponentAccessor; import com.deniz.jira.worklog.accounting.*; import com.deniz.jira.worklog.services.*; import java.util.*; import java.time.*; if (account == null) { return; } def accountService = ComponentAccessor.getOSGiComponentInstanceOfType(AccountService.class); def timesheetService = ComponentAccessor.getOSGiComponentInstanceOfType(TimesheetService.class); def accounts = [account.id]; def start = ZonedDateTime.now().minusYears(30); def end = ZonedDateTime.now().plusYears(30); def worklogs = timesheetService.getAccountWorklogs(start, end, false, null, null, null, (Integer[]) accounts) ; def totalTimeSpent = 0; worklogs.projects.each { p -> p.issues.each { i -> i.workLogs.each { w -> totalTimeSpent+=w.timeSpent; } } } if (totalTimeSpent > account.budget) { return "You are over budget for this account"; };

Above code does not check budget hierarchy. It just checks budget itself.