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.
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.