List of usage examples for org.joda.time DateTime toLocalDate
public LocalDate toLocalDate()
LocalDate
with the same date and chronology. From source file:org.kuali.kpme.core.role.proxy.service.KpmeRoleProxyDerivedRoleTypeServiceImpl.java
License:Educational Community License
@Override public boolean isMemberOfGroup(String principalId, Group group, DateTime asOfDate, boolean activeOnly) { // the return value boolean isMemberOfGroup = false; if ((asOfDate.toLocalDate().toDateTimeAtStartOfDay().equals(LocalDate.now().toDateTimeAtStartOfDay())) && activeOnly) {//from ww w . ja v a2 s . c om // use the group service to get all the group ids in which the principal is a member. List<String> groupIds = getGroupService().getGroupIdsByPrincipalId(principalId); isMemberOfGroup = groupIds.contains(group.getId()); } else { List<GroupMember> primaryGroupMembers = new ArrayList<GroupMember>(); if (group != null) { //get the primary group members via a predicate-based query List<Predicate> predicates = new ArrayList<Predicate>(); predicates.add(equal(KIMPropertyConstants.GroupMember.GROUP_ID, group.getId())); if (activeOnly) { predicates.add( or(isNull("activeFromDateValue"), lessThanOrEqual("activeFromDateValue", asOfDate))); predicates.add(or(isNull("activeToDateValue"), greaterThan("activeToDateValue", asOfDate))); } primaryGroupMembers = getGroupService() .findGroupMembers( QueryByCriteria.Builder.fromPredicates(predicates.toArray(new Predicate[] {}))) .getResults(); } // iterate through the primary group members, first checking all principal type members before // descending recursively into any nested group members. List<GroupMember> nestedGroups = new ArrayList<GroupMember>(); for (GroupMember groupMember : primaryGroupMembers) { if (KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE.equals(groupMember.getType())) { if (StringUtils.equals(groupMember.getMemberId(), principalId)) { isMemberOfGroup = true; break; } } else if (KimConstants.KimGroupMemberTypes.GROUP_MEMBER_TYPE.equals(groupMember.getType())) { // put the nested group members into a different list for faster processing below nestedGroups.add(groupMember); } } // check nested groups recursively if not found amongst primary principal type members if (!isMemberOfGroup) { for (GroupMember nestedGroupMember : nestedGroups) { Group nestedGroup = getGroupService().getGroup(nestedGroupMember.getMemberId()); // recursive call to check nested group if (isMemberOfGroup(principalId, nestedGroup, asOfDate, activeOnly)) { isMemberOfGroup = true; break; } } } } return isMemberOfGroup; }
From source file:org.kuali.kpme.core.service.role.KPMERoleServiceImpl.java
License:Educational Community License
public List<Long> getWorkAreasForPrincipalInRole(String principalId, String namespaceCode, String roleName, DateTime asOfDate, boolean isActiveOnly) { Set<Long> workAreas = new HashSet<Long>(); Role role = getRoleService().getRoleByNamespaceCodeAndName(namespaceCode, roleName); List<Map<String, String>> roleQualifiers = getRoleQualifiers(principalId, role, asOfDate, isActiveOnly); for (Map<String, String> roleQualifier : roleQualifiers) { Long workArea = MapUtils.getLong(roleQualifier, KPMERoleMemberAttribute.WORK_AREA.getRoleMemberAttributeName()); if (workArea != null) { workAreas.add(workArea);//from w w w. j a va 2 s . c o m } } List<String> departments = getDepartmentsForPrincipalInRole(principalId, namespaceCode, roleName, asOfDate, isActiveOnly); for (String department : departments) { List<WorkArea> workAreaObjs = getWorkAreaService().getWorkAreas(department, asOfDate.toLocalDate()); for (WorkArea workAreaObj : workAreaObjs) { workAreas.add(workAreaObj.getWorkArea()); } } return new ArrayList<Long>(workAreas); }
From source file:org.kuali.kpme.core.service.role.KPMERoleServiceImpl.java
License:Educational Community License
public List<String> getDepartmentsForPrincipalInRole(String principalId, String namespaceCode, String roleName, DateTime asOfDate, boolean isActiveOnly) { Set<String> departments = new HashSet<String>(); Role role = getRoleService().getRoleByNamespaceCodeAndName(namespaceCode, roleName); List<Map<String, String>> roleQualifiers = getRoleQualifiers(principalId, role, asOfDate, isActiveOnly); for (Map<String, String> roleQualifier : roleQualifiers) { String department = MapUtils.getString(roleQualifier, KPMERoleMemberAttribute.DEPARTMENT.getRoleMemberAttributeName()); if (department != null) { departments.add(department); }//from ww w . jav a 2 s . co m } List<String> locations = getLocationsForPrincipalInRole(principalId, namespaceCode, roleName, asOfDate, isActiveOnly); for (String location : locations) { List<Department> departmentObjs = getDepartmentService().getDepartments(location, asOfDate.toLocalDate()); for (Department departmentObj : departmentObjs) { departments.add(departmentObj.getDept()); } } return new ArrayList<String>(departments); }
From source file:org.kuali.kpme.tklm.common.BatchJobServiceImpl.java
License:Educational Community License
@Override public void scheduleInitiateJobs(CalendarEntry calendarEntry, DateTime scheduleDate) throws SchedulerException { Calendar calendar = getCalendarService().getCalendar(calendarEntry.getHrCalendarId()); String calendarTypes = calendar.getCalendarTypes(); String calendarName = calendar.getCalendarName(); DateTime beginDate = calendarEntry.getBeginPeriodFullDateTime(); DateTime endDate = calendarEntry.getEndPeriodFullDateTime(); if (StringUtils.equals(calendarTypes, "Pay")) { List<PrincipalHRAttributes> principalHRAttributes = getPrincipalHRAttributesService() .getActiveEmployeesForPayCalendar(calendarName, scheduleDate.toLocalDate()); for (PrincipalHRAttributes principalHRAttribute : principalHRAttributes) { String principalId = principalHRAttribute.getPrincipalId(); List<Assignment> assignments = getAssignmentService() .getAssignmentsByCalEntryForTimeCalendar(principalId, calendarEntry); for (Assignment assignment : assignments) { Job job = assignment.getJob(); if (StringUtils.equalsIgnoreCase(job.getFlsaStatus(), HrConstants.FLSA_STATUS_EXEMPT)) { TimesheetDocumentHeader timesheetDocumentHeader = getTimesheetDocumentHeaderService() .getDocumentHeader(principalId, beginDate, endDate); if (timesheetDocumentHeader == null || StringUtils.equals( timesheetDocumentHeader.getDocumentStatus(), HrConstants.ROUTE_STATUS.CANCEL)) { scheduleInitiateJob(calendarEntry, scheduleDate, assignment.getPrincipalId()); }//from w ww . j a v a 2s.c o m } } } } else if (StringUtils.equals(calendarTypes, "Leave")) { List<PrincipalHRAttributes> principalHRAttributes = getPrincipalHRAttributesService() .getActiveEmployeesForLeaveCalendar(calendarName, scheduleDate.toLocalDate()); for (PrincipalHRAttributes principalHRAttribute : principalHRAttributes) { String principalId = principalHRAttribute.getPrincipalId(); List<Assignment> assignments = getAssignmentService() .getAssignmentsByCalEntryForLeaveCalendar(principalId, calendarEntry); for (Assignment assignment : assignments) { Job job = assignment.getJob(); if (job.isEligibleForLeave() && StringUtils.equalsIgnoreCase(job.getFlsaStatus(), HrConstants.FLSA_STATUS_NON_EXEMPT)) { LeaveCalendarDocumentHeader leaveCalendarDocumentHeader = getLeaveCalendarDocumentHeaderService() .getDocumentHeader(principalId, beginDate, endDate); if (leaveCalendarDocumentHeader == null || StringUtils.equals( leaveCalendarDocumentHeader.getDocumentStatus(), HrConstants.ROUTE_STATUS.CANCEL)) { scheduleInitiateJob(calendarEntry, scheduleDate, assignment.getPrincipalId()); } } } } } }
From source file:org.kuali.kpme.tklm.common.BatchJobServiceImpl.java
License:Educational Community License
@Override public void scheduleEndReportingPeriodJobs(CalendarEntry calendarEntry, DateTime scheduleDate) throws SchedulerException { Calendar calendar = getCalendarService().getCalendar(calendarEntry.getHrCalendarId()); String calendarTypes = calendar.getCalendarTypes(); String calendarName = calendar.getCalendarName(); DateTime beginDate = calendarEntry.getBeginPeriodFullDateTime(); DateTime endDate = calendarEntry.getEndPeriodFullDateTime(); if (StringUtils.equals(calendarTypes, "Pay")) { List<PrincipalHRAttributes> principalHRAttributes = getPrincipalHRAttributesService() .getActiveEmployeesForPayCalendar(calendarName, scheduleDate.toLocalDate()); for (PrincipalHRAttributes principalHRAttribute : principalHRAttributes) { String principalId = principalHRAttribute.getPrincipalId(); TimesheetDocumentHeader timesheetDocumentHeader = getTimesheetDocumentHeaderService() .getDocumentHeader(principalId, beginDate, endDate); if (timesheetDocumentHeader != null) { scheduleEndReportingPeriodJob(calendarEntry, scheduleDate, principalId); }//w w w. j a v a2s .c om } } else if (StringUtils.equals(calendarTypes, "Leave")) { List<PrincipalHRAttributes> principalHRAttributes = getPrincipalHRAttributesService() .getActiveEmployeesForLeaveCalendar(calendarName, scheduleDate.toLocalDate()); for (PrincipalHRAttributes principalHRAttribute : principalHRAttributes) { String principalId = principalHRAttribute.getPrincipalId(); LeaveCalendarDocumentHeader leaveCalendarDocumentHeader = getLeaveCalendarDocumentHeaderService() .getDocumentHeader(principalId, beginDate, endDate); if (leaveCalendarDocumentHeader != null) { scheduleEndReportingPeriodJob(calendarEntry, scheduleDate, principalId); } } } }
From source file:org.kuali.kpme.tklm.common.BatchJobServiceImpl.java
License:Educational Community License
@Override public void scheduleEndPayPeriodJobs(CalendarEntry calendarEntry, DateTime scheduleDate) throws SchedulerException { String calendarName = calendarEntry.getCalendarName(); List<PrincipalHRAttributes> principalHRAttributes = getPrincipalHRAttributesService() .getActiveEmployeesForPayCalendar(calendarName, scheduleDate.toLocalDate()); for (PrincipalHRAttributes principalHRAttribute : principalHRAttributes) { String principalId = principalHRAttribute.getPrincipalId(); List<Assignment> assignments = getAssignmentService() .getAssignmentsByCalEntryForTimeCalendar(principalId, calendarEntry); for (Assignment assignment : assignments) { String jobNumber = String.valueOf(assignment.getJobNumber()); String workArea = String.valueOf(assignment.getWorkArea()); String task = String.valueOf(assignment.getTask()); ClockLog lastClockLog = getClockLogService().getLastClockLog(principalId, jobNumber, workArea, task, calendarEntry);/*from w ww . jav a 2s. c o m*/ if (lastClockLog != null && TkConstants.ON_THE_CLOCK_CODES.contains(lastClockLog.getClockAction())) { scheduleEndPayPeriodJob(calendarEntry, scheduleDate, lastClockLog); } } } }
From source file:org.kuali.kpme.tklm.common.PayrollApprovalJob.java
License:Educational Community License
@Override public void execute(JobExecutionContext context) throws JobExecutionException { String batchUserPrincipalId = getBatchUserPrincipalId(); if (batchUserPrincipalId != null) { JobDataMap jobDataMap = context.getJobDetail().getJobDataMap(); String hrCalendarEntryId = jobDataMap.getString("hrCalendarEntryId"); CalendarEntry calendarEntry = HrServiceLocator.getCalendarEntryService() .getCalendarEntry(hrCalendarEntryId); Calendar calendar = HrServiceLocator.getCalendarService().getCalendar(calendarEntry.getHrCalendarId()); DateTime beginDate = calendarEntry.getBeginPeriodFullDateTime(); DateTime endDate = calendarEntry.getEndPeriodFullDateTime(); List<RoleMember> roleMembers = new ArrayList<RoleMember>(); String subject = ""; List<Long> workAreas = new ArrayList<Long>(); // used to flag if any document has be routed by this batch job. If true, // we need to reschedule this job since the newly routed documents are not approved by this job, we need a new job to approve them boolean needToReschedule = false; if (StringUtils.equals(calendar.getCalendarTypes(), "Pay")) { List<TimesheetDocumentHeader> timesheetDocumentHeaders = TkServiceLocator .getTimesheetDocumentHeaderService().getDocumentHeaders(beginDate, endDate); for (TimesheetDocumentHeader timesheetDocumentHeader : timesheetDocumentHeaders) { if (timesheetDocumentHeader != null) { String docId = timesheetDocumentHeader.getDocumentId(); TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService() .getTimesheetDocument(docId); String documentStatus = KEWServiceLocator.getRouteHeaderService().getDocumentStatus(docId); if (documentStatus.equals(DocumentStatus.ENROUTE.getCode())) { PrincipalHRAttributes phraRecord = HrServiceLocator.getPrincipalHRAttributeService() .getPrincipalCalendar(timesheetDocument.getPrincipalId(), endDate.toLocalDate()); if (phraRecord != null && phraRecord.getPayCalendar().equals(calendar.getCalendarName())) { // find all request actions and approve the document as the users with the request action List<ActionRequest> requestList = KewApiServiceLocator.getWorkflowDocumentService() .getPendingActionRequests(docId); for (ActionRequest aRequest : requestList) { if (aRequest.getActionRequested() != null && aRequest.getActionRequested().getCode() .equals(KewApiConstants.ACTION_REQUEST_APPROVE_REQ) && StringUtils.isNotBlank(aRequest.getPrincipalId()) && StringUtils.equals(aRequest.getQualifiedRoleNameLabel(), KPMERole.PAYROLL_PROCESSOR.getRoleName())) { TkServiceLocator.getTimesheetService().approveTimesheet( aRequest.getPrincipalId(), timesheetDocument, HrConstants.BATCH_JOB_ACTIONS.PAYROLL_JOB_APPROVE); roleMembers = getRoleMembersInDepartment( timesheetDocument.getAllAssignments(), KPMENamespace.KPME_TK); subject = "Payroll Batch Approved Timesheet Document " + docId; }// ww w . j av a 2 s. c o m } } } else if (documentStatus.equals(DocumentStatus.INITIATED.getCode()) || documentStatus.equals(DocumentStatus.SAVED.getCode())) { // if there are documents still not submitted by the time payroll processor approval batch job runs, we need to route the document, then reschedule the payroll processor job String principalId = timesheetDocument.getPrincipalId(); PrincipalHRAttributes phraRecord = HrServiceLocator.getPrincipalHRAttributeService() .getPrincipalCalendar(principalId, endDate.toLocalDate()); if (phraRecord != null && phraRecord.getPayCalendar().equals(calendar.getCalendarName())) { TkServiceLocator.getTimesheetService().routeTimesheet(batchUserPrincipalId, timesheetDocument, HrConstants.BATCH_JOB_ACTIONS.BATCH_JOB_ROUTE); needToReschedule = true; } } } } } else if (StringUtils.equals(calendar.getCalendarTypes(), "Leave")) { List<LeaveCalendarDocumentHeader> leaveCalendarDocumentHeaders = LmServiceLocator .getLeaveCalendarDocumentHeaderService().getDocumentHeaders(beginDate, endDate); for (LeaveCalendarDocumentHeader leaveCalendarDocumentHeader : leaveCalendarDocumentHeaders) { if (leaveCalendarDocumentHeader != null) { String docId = leaveCalendarDocumentHeader.getDocumentId(); LeaveCalendarDocument leaveCalendarDocument = LmServiceLocator.getLeaveCalendarService() .getLeaveCalendarDocument(docId); String documentStatus = KEWServiceLocator.getRouteHeaderService().getDocumentStatus(docId); // only approve documents in enroute status if (documentStatus.equals(DocumentStatus.ENROUTE.getCode())) { PrincipalHRAttributes phraRecord = HrServiceLocator.getPrincipalHRAttributeService() .getPrincipalCalendar(leaveCalendarDocument.getPrincipalId(), endDate.toLocalDate()); if (phraRecord != null && phraRecord.getLeaveCalendar().equals(calendar.getCalendarName())) { // find all request actions and approve the document as the users with the request action List<ActionRequest> requestList = KewApiServiceLocator.getWorkflowDocumentService() .getPendingActionRequests(docId); for (ActionRequest aRequest : requestList) { if (aRequest.getActionRequested() != null && aRequest.getActionRequested().getCode() .equals(KewApiConstants.ACTION_REQUEST_APPROVE_REQ) && StringUtils.isNotBlank(aRequest.getPrincipalId()) && StringUtils.equals(aRequest.getQualifiedRoleNameLabel(), KPMERole.PAYROLL_PROCESSOR.getRoleName())) { LmServiceLocator.getLeaveCalendarService().approveLeaveCalendar( aRequest.getPrincipalId(), leaveCalendarDocument, HrConstants.BATCH_JOB_ACTIONS.PAYROLL_JOB_APPROVE); roleMembers = getRoleMembersInDepartment( leaveCalendarDocument.getAssignments(), KPMENamespace.KPME_LM); subject = "Payroll Batch Approved Leave Calendar Document " + docId; } } } } else if (documentStatus.equals(DocumentStatus.INITIATED.getCode()) || documentStatus.equals(DocumentStatus.SAVED.getCode())) { String principalId = leaveCalendarDocument.getPrincipalId(); PrincipalHRAttributes phraRecord = HrServiceLocator.getPrincipalHRAttributeService() .getPrincipalCalendar(principalId, endDate.toLocalDate()); if (phraRecord != null && phraRecord.getLeaveCalendar().equals(calendar.getCalendarName())) { LmServiceLocator.getLeaveCalendarService().routeLeaveCalendar(batchUserPrincipalId, leaveCalendarDocument, HrConstants.BATCH_JOB_ACTIONS.BATCH_JOB_ROUTE); needToReschedule = true; } } } } } sendNotifications(subject, roleMembers, workAreas); if (needToReschedule) { rescheduleJob(context); } } else { String principalName = getBatchUserPrincipalName(); LOG.error("Could not run batch jobs due to missing batch user " + principalName); } }
From source file:org.kuali.kpme.tklm.leave.accrual.service.AccrualServiceImpl.java
License:Educational Community License
@SuppressWarnings("unchecked") @Override/*from w ww . jav a2s. c o m*/ public void runAccrual(String principalId, DateTime startDate, DateTime endDate, boolean recordRanData, String runAsPrincipalId) { List<LeaveBlock> accrualLeaveBlocks = new ArrayList<LeaveBlock>(); Map<String, BigDecimal> accumulatedAccrualCatToAccrualAmounts = new HashMap<String, BigDecimal>(); Map<String, BigDecimal> accumulatedAccrualCatToNegativeAccrualAmounts = new HashMap<String, BigDecimal>(); if (startDate != null && endDate != null) { LOG.info("AccrualServiceImpl.runAccrual() STARTED with Principal: " + principalId + " Start: " + startDate.toString() + " End: " + endDate.toString()); } if (startDate.isAfter(endDate)) { LOG.error("Start Date " + startDate.toString() + " should not be later than End Date " + endDate.toString()); return; // throw new RuntimeException("Start Date " + startDate.toString() + " should not be later than End Date " + endDate.toString()); } //Inactivate all previous accrual-generated entries for this span of time deactivateOldAccruals(principalId, startDate, endDate, runAsPrincipalId); //Build a rate range aggregate with appropriate information for this period of time detailing Rate Ranges for job //entries for this range of time RateRangeAggregate rrAggregate = this.buildRateRangeAggregate(principalId, startDate, endDate); PrincipalHRAttributes phra = null; PrincipalHRAttributes endPhra = null; LeavePlan lp = null; List<AccrualCategory> accrCatList = null; //Iterate over every day in span DateTime currentDate = startDate; while (!currentDate.isAfter(endDate)) { RateRange currentRange = rrAggregate.getRateOnDate(currentDate); if (currentRange == null) { currentDate = currentDate.plusDays(1); continue; } phra = currentRange.getPrincipalHRAttributes(); if (phra == null || currentDate.toLocalDate().isBefore(phra.getServiceLocalDate())) { currentDate = currentDate.plusDays(1); continue; } // use the effectiveDate of this principalHRAttribute to search for inactive entries for this principalId // If there's an inactive entry, it means the job is going to end on the effectiveDate of the inactive entry // used for minimumPercentage and proration endPhra = currentRange.getEndPrincipalHRAttributes(); if (endPhra != null && currentDate.toLocalDate().isAfter(endPhra.getEffectiveLocalDate())) { currentDate = currentDate.plusDays(1); continue; } // if the date range is before the service date of this employee, do not calculate accrual if (endDate.toLocalDate().isBefore(phra.getServiceLocalDate())) { return; } lp = currentRange.getLeavePlan(); accrCatList = currentRange.getAcList(); // if the employee status is changed, create an empty leave block on the currentDate if (currentRange.isStatusChanged()) { this.createEmptyLeaveBlockForStatusChange(principalId, accrualLeaveBlocks, currentDate.toLocalDate()); } // if no job found for the employee on the currentDate, do nothing if (CollectionUtils.isEmpty(currentRange.getJobs())) { currentDate = currentDate.plusDays(1); continue; } BigDecimal ftePercentage = currentRange.getAccrualRatePercentageModifier(); BigDecimal totalOfStandardHours = currentRange.getStandardHours(); boolean fullFteGranted = false; for (AccrualCategory anAC : accrCatList) { fullFteGranted = false; if (!currentDate.toLocalDate().isBefore(phra.getEffectiveLocalDate()) && !anAC.getAccrualEarnInterval().equals("N")) { // "N" means no accrual boolean prorationFlag = this.isProrationFlag(anAC.getProration()); // get the accrual rule AccrualCategoryRule currentAcRule = this.getRuleForAccrualCategory(currentRange.getAcRuleList(), anAC); // check if accrual category rule changed if (currentAcRule != null) { DateTime ruleStartDate = getRuleStartDate(currentAcRule.getServiceUnitOfTime(), phra.getServiceLocalDate(), currentAcRule.getStart()); DateTime previousIntervalDay = this.getPrevIntervalDate(ruleStartDate, anAC.getAccrualEarnInterval(), phra.getPayCalendar(), rrAggregate.getCalEntryMap()); DateTime nextIntervalDay = this.getNextIntervalDate(ruleStartDate, anAC.getAccrualEarnInterval(), phra.getPayCalendar(), rrAggregate.getCalEntryMap()); RateRange previousRange = rrAggregate.getRateOnDate(previousIntervalDay); AccrualCategoryRule previousAcRule = null; if (previousRange != null) { previousAcRule = this.getRuleForAccrualCategory(previousRange.getAcRuleList(), anAC); } // rule changed if (previousAcRule != null && !previousAcRule.getLmAccrualCategoryRuleId() .equals(currentAcRule.getLmAccrualCategoryRuleId())) { if (currentDate.toLocalDate().compareTo(previousIntervalDay.toLocalDate()) >= 0 && currentDate.toLocalDate().compareTo(nextIntervalDay.toLocalDate()) <= 0) { int workDaysInBetween = TKUtils.getWorkDays(ruleStartDate, nextIntervalDay); boolean minReachedFlag = minimumPercentageReachedForPayPeriod( anAC.getMinPercentWorked(), anAC.getAccrualEarnInterval(), workDaysInBetween, nextIntervalDay, phra.getPayCalendar(), rrAggregate.getCalEntryMap()); if (prorationFlag) { if (minReachedFlag) { // min reached, proration=true, rule changed, then use actual work days of currentRule for calculation // so nothing special needs to be done here } else { //minimum percentage NOT reached, proration = true, rule changed, then use previousRule for the whole pay period currentAcRule = previousAcRule; } } else { if (minReachedFlag) { // min reached, proration = false, rule changed, then accrual the whole fte of the new rule for this pay interval accumulatedAccrualCatToAccrualAmounts.put(anAC.getLmAccrualCategoryId(), currentAcRule.getAccrualRate()); fullFteGranted = true; } else { //min NOT reached, proration = false, rule changed, then accrual the whole fte of the previous rule for this pay interval accumulatedAccrualCatToAccrualAmounts.put(anAC.getLmAccrualCategoryId(), previousAcRule.getAccrualRate()); fullFteGranted = true; } } } } } // check for first pay period of principal attributes considering minimum percentage and proration DateTime firstIntervalDate = this.getNextIntervalDate( phra.getEffectiveLocalDate().toDateTimeAtStartOfDay(), anAC.getAccrualEarnInterval(), phra.getPayCalendar(), rrAggregate.getCalEntryMap()); if (!currentDate.toLocalDate().isBefore(phra.getEffectiveLocalDate()) && !currentDate.toLocalDate().isAfter(firstIntervalDate.toLocalDate())) { int workDaysInBetween = TKUtils.getWorkDays( phra.getEffectiveLocalDate().toDateTimeAtStartOfDay(), firstIntervalDate); boolean minReachedFlag = minimumPercentageReachedForPayPeriod(anAC.getMinPercentWorked(), anAC.getAccrualEarnInterval(), workDaysInBetween, firstIntervalDate, phra.getPayCalendar(), rrAggregate.getCalEntryMap()); if (prorationFlag) { if (minReachedFlag) { // minimum reached, proration = true, first pay period, then use actual work days of currentRule for calculation // so nothing special needs to be done here } else { // min NOT reached, proration = true, first pay period, then no accrual for this pay period accumulatedAccrualCatToAccrualAmounts.remove(anAC.getLmAccrualCategoryId()); accumulatedAccrualCatToNegativeAccrualAmounts.remove(anAC.getLmAccrualCategoryId()); continue; } } else { if (minReachedFlag) { // minimum reached, proration = false, first pay period, then accrual the whole fte of current AC rule for this pay interval accumulatedAccrualCatToAccrualAmounts.put(anAC.getLmAccrualCategoryId(), currentAcRule.getAccrualRate()); fullFteGranted = true; } else { // min NOT reached, proration = false, first pay period, then no accrual for this pay period accumulatedAccrualCatToAccrualAmounts.remove(anAC.getLmAccrualCategoryId()); accumulatedAccrualCatToNegativeAccrualAmounts.remove(anAC.getLmAccrualCategoryId()); continue; } } } // last accrual interval if (endPhra != null) { // the employment is going to end on the effectiveDate of enPhra DateTime previousIntervalDate = this.getPrevIntervalDate( endPhra.getEffectiveLocalDate().toDateTimeAtStartOfDay(), anAC.getAccrualEarnInterval(), phra.getPayCalendar(), rrAggregate.getCalEntryMap()); // currentDate is between the end date and the last interval date, so we are in the last interval if (!currentDate.toLocalDate().isAfter(endPhra.getEffectiveLocalDate()) && currentDate.toLocalDate().isAfter(previousIntervalDate.toLocalDate())) { DateTime lastIntervalDate = this.getNextIntervalDate( endPhra.getEffectiveLocalDate().toDateTimeAtStartOfDay(), anAC.getAccrualEarnInterval(), phra.getPayCalendar(), rrAggregate.getCalEntryMap()); int workDaysInBetween = TKUtils.getWorkDays(previousIntervalDate, endPhra.getEffectiveLocalDate().toDateTimeAtStartOfDay()); boolean minReachedFlag = minimumPercentageReachedForPayPeriod( anAC.getMinPercentWorked(), anAC.getAccrualEarnInterval(), workDaysInBetween, lastIntervalDate, phra.getPayCalendar(), rrAggregate.getCalEntryMap()); if (prorationFlag) { if (minReachedFlag) { // minimum reached, proration = true, first pay period, then use actual work days of currentRule for calculation // so nothing special needs to be done here } else { // min NOT reached, proration = true, first pay period, then no accrual for this pay period accumulatedAccrualCatToAccrualAmounts.remove(anAC.getLmAccrualCategoryId()); accumulatedAccrualCatToNegativeAccrualAmounts .remove(anAC.getLmAccrualCategoryId()); continue; } } else { if (minReachedFlag) { // minimum reached, proration = false, first pay period, then accrual the whole fte of current AC rule for this pay interval accumulatedAccrualCatToAccrualAmounts.put(anAC.getLmAccrualCategoryId(), currentAcRule.getAccrualRate()); fullFteGranted = true; } else { // min NOT reached, proration = false, first pay period, then no accrual for this pay period accumulatedAccrualCatToAccrualAmounts.remove(anAC.getLmAccrualCategoryId()); accumulatedAccrualCatToNegativeAccrualAmounts .remove(anAC.getLmAccrualCategoryId()); continue; } } } } if (currentAcRule == null) { accumulatedAccrualCatToAccrualAmounts.remove(anAC.getLmAccrualCategoryId()); accumulatedAccrualCatToNegativeAccrualAmounts.remove(anAC.getLmAccrualCategoryId()); continue; } // only accrual on work days if (!TKUtils.isWeekend(currentDate) && !fullFteGranted) { BigDecimal accrualRate = currentAcRule.getAccrualRate(); int numberOfWorkDays = this.getWorkDaysInInterval(currentDate, anAC.getAccrualEarnInterval(), phra.getPayCalendar(), rrAggregate.getCalEntryMap()); BigDecimal dayRate = numberOfWorkDays > 0 ? accrualRate.divide(new BigDecimal(numberOfWorkDays), 6, BigDecimal.ROUND_HALF_UP) : new BigDecimal(0); //Fetch the accural rate based on rate range for today(Rate range is the accumulated list of jobs and accrual rate for today) //Add to total accumulatedAccrualCatToAccrualAmounts //use rule and ftePercentage to calculate the hours this.calculateHours(anAC.getLmAccrualCategoryId(), ftePercentage, dayRate, accumulatedAccrualCatToAccrualAmounts); //get not eligible for accrual hours based on leave block on this day BigDecimal noAccrualHours = getNotEligibleForAccrualHours(principalId, currentDate.toLocalDate()); if (noAccrualHours != null && noAccrualHours.compareTo(BigDecimal.ZERO) != 0 && totalOfStandardHours.compareTo(BigDecimal.ZERO) != 0) { BigDecimal dayHours = totalOfStandardHours.divide(new BigDecimal(5), 6, BigDecimal.ROUND_HALF_UP); BigDecimal noAccrualRate = dayRate.multiply(noAccrualHours.divide(dayHours)); this.calculateHours(anAC.getLmAccrualCategoryId(), ftePercentage, noAccrualRate, accumulatedAccrualCatToNegativeAccrualAmounts); } } //Determine if we are at the accrual earn interval in the span, if so add leave block for accumulated accrual amount to list //and reset accumulatedAccrualCatToAccrualAmounts and accumulatedAccrualCatToNegativeAccrualAmounts for this accrual category if (this.isDateAnIntervalDate(currentDate.toLocalDate(), anAC.getAccrualEarnInterval(), phra.getPayCalendar(), rrAggregate.getCalEntryMap())) { BigDecimal acHours = accumulatedAccrualCatToAccrualAmounts .get(anAC.getLmAccrualCategoryId()); if (acHours != null) { createLeaveBlock(principalId, accrualLeaveBlocks, currentDate.toLocalDate(), acHours, anAC, null, true, currentRange.getLeaveCalendarDocumentId()); accumulatedAccrualCatToAccrualAmounts.remove(anAC.getLmAccrualCategoryId()); // reset accumulatedAccrualCatToAccrualAmounts fullFteGranted = false; } BigDecimal adjustmentHours = accumulatedAccrualCatToNegativeAccrualAmounts .get(anAC.getLmAccrualCategoryId()); if (adjustmentHours != null && adjustmentHours.compareTo(BigDecimal.ZERO) != 0) { // do not create leave block if the ajustment amount is 0 createLeaveBlock(principalId, accrualLeaveBlocks, currentDate.toLocalDate(), adjustmentHours, anAC, null, false, currentRange.getLeaveCalendarDocumentId()); accumulatedAccrualCatToNegativeAccrualAmounts.remove(anAC.getLmAccrualCategoryId()); // reset accumulatedAccrualCatToNegativeAccrualAmounts } } } } //Determine if today is a system scheduled time off and accrue holiday if so. SystemScheduledTimeOff ssto = currentRange.getSysScheTimeOff(); if (ssto != null) { AccrualCategory anAC = HrServiceLocator.getAccrualCategoryService() .getAccrualCategory(ssto.getAccrualCategory(), ssto.getEffectiveLocalDate()); if (anAC == null) { LOG.error("Cannot find Accrual Category for system scheduled time off " + ssto.getLmSystemScheduledTimeOffId()); return; // throw new RuntimeException("Cannot find Accrual Category for system scheduled time off " + ssto.getLmSystemScheduledTimeOffId()); } BigDecimal hrs = ssto.getAmountofTime().multiply(ftePercentage); // system scheduled time off leave block createLeaveBlock(principalId, accrualLeaveBlocks, currentDate.toLocalDate(), hrs, anAC, ssto.getLmSystemScheduledTimeOffId(), true, currentRange.getLeaveCalendarDocumentId()); // usage leave block with negative amount createLeaveBlock(principalId, accrualLeaveBlocks, currentDate.toLocalDate(), hrs.negate(), anAC, ssto.getLmSystemScheduledTimeOffId(), true, currentRange.getLeaveCalendarDocumentId()); } // if today is the last day of the employment, create leave blocks if there's any hours available if (endPhra != null && currentDate.toLocalDate().equals(endPhra.getEffectiveLocalDate())) { // accumulated accrual amount if (!accumulatedAccrualCatToAccrualAmounts.isEmpty()) { for (Map.Entry<String, BigDecimal> entry : accumulatedAccrualCatToAccrualAmounts.entrySet()) { if (entry.getValue() != null && entry.getValue().compareTo(BigDecimal.ZERO) != 0) { AccrualCategory anAC = HrServiceLocator.getAccrualCategoryService() .getAccrualCategory(entry.getKey()); createLeaveBlock(principalId, accrualLeaveBlocks, currentDate.toLocalDate(), entry.getValue(), anAC, null, true, currentRange.getLeaveCalendarDocumentId()); } } accumulatedAccrualCatToAccrualAmounts = new HashMap<String, BigDecimal>(); // reset accumulatedAccrualCatToAccrualAmounts } // negative/adjustment accrual amount if (!accumulatedAccrualCatToNegativeAccrualAmounts.isEmpty()) { for (Map.Entry<String, BigDecimal> entry : accumulatedAccrualCatToNegativeAccrualAmounts .entrySet()) { if (entry.getValue() != null && entry.getValue().compareTo(BigDecimal.ZERO) != 0) { AccrualCategory anAC = HrServiceLocator.getAccrualCategoryService() .getAccrualCategory(entry.getKey()); createLeaveBlock(principalId, accrualLeaveBlocks, currentDate.toLocalDate(), entry.getValue(), anAC, null, true, currentRange.getLeaveCalendarDocumentId()); } } accumulatedAccrualCatToNegativeAccrualAmounts = new HashMap<String, BigDecimal>(); // reset accumulatedAccrualCatToNegativeAccrualAmounts } phra = null; // reset principal attribute so new value will be retrieved endPhra = null; // reset end principal attribute so new value will be retrieved } currentDate = currentDate.plusDays(1); } //Save accrual leave blocks at the very end LmServiceLocator.getLeaveBlockService().saveLeaveBlocks(accrualLeaveBlocks); // record timestamp of this accrual run in database if (recordRanData) { LmServiceLocator.getPrincipalAccrualRanService().updatePrincipalAccrualRanInfo(principalId); } }
From source file:org.kuali.kpme.tklm.leave.accrual.service.AccrualServiceImpl.java
License:Educational Community License
private void deactivateOldAccruals(String principalId, DateTime startDate, DateTime endDate, String runAsPrincipalId) { List<LeaveBlock> previousLB = LmServiceLocator.getLeaveBlockService() .getAccrualGeneratedLeaveBlocks(principalId, startDate.toLocalDate(), endDate.toLocalDate()); List<LeaveBlock> sstoAccrualList = new ArrayList<LeaveBlock>(); List<LeaveBlock> sstoUsageList = new ArrayList<LeaveBlock>(); for (LeaveBlock lb : previousLB) { if (StringUtils.isNotEmpty(lb.getScheduleTimeOffId())) { if (lb.getLeaveAmount().compareTo(BigDecimal.ZERO) > 0) { sstoAccrualList.add(lb); } else if (lb.getLeaveAmount().compareTo(BigDecimal.ZERO) < 0) { sstoUsageList.add(lb);// www . j a va 2 s . c om } } else { //if(!(StringUtils.equals(lb.getLeaveBlockType(),LMConstants.LEAVE_BLOCK_TYPE.BALANCE_TRANSFER) || // StringUtils.equals(lb.getLeaveBlockType(),LMConstants.LEAVE_BLOCK_TYPE.LEAVE_PAYOUT))) { LmServiceLocator.getLeaveBlockService().deleteLeaveBlock(lb.getLmLeaveBlockId(), runAsPrincipalId); //} } } for (LeaveBlock accrualLb : sstoAccrualList) { for (LeaveBlock usageLb : sstoUsageList) { // both usage and accrual ssto leave blocks are there, so the ssto accural is not banked, removed both leave blocks // if this is no ssto usage leave block, it means the user has banked this ssto hours. Don't delete this ssto accrual leave block if (accrualLb.getScheduleTimeOffId().equals(usageLb.getScheduleTimeOffId())) { LmServiceLocator.getLeaveBlockService().deleteLeaveBlock(accrualLb.getLmLeaveBlockId(), runAsPrincipalId); LmServiceLocator.getLeaveBlockService().deleteLeaveBlock(usageLb.getLmLeaveBlockId(), runAsPrincipalId); } } } }
From source file:org.kuali.kpme.tklm.leave.accrual.service.AccrualServiceImpl.java
License:Educational Community License
@Override public RateRangeAggregate buildRateRangeAggregate(String principalId, DateTime startDate, DateTime endDate) { RateRangeAggregate rrAggregate = new RateRangeAggregate(); List<RateRange> rateRangeList = new ArrayList<RateRange>(); // get all active jobs that are effective before the endDate List<Job> activeJobs = HrServiceLocator.getJobService().getAllActiveLeaveJobs(principalId, endDate.toLocalDate()); List<Job> inactiveJobs = HrServiceLocator.getJobService().getAllInActiveLeaveJobsInRange(principalId, endDate.toLocalDate());//from w w w . j a v a2 s. c om List<PrincipalHRAttributes> phaList = HrServiceLocator.getPrincipalHRAttributeService() .getAllActivePrincipalHrAttributesForPrincipalId(principalId, endDate.toLocalDate()); List<PrincipalHRAttributes> inactivePhaList = HrServiceLocator.getPrincipalHRAttributeService() .getAllInActivePrincipalHrAttributesForPrincipalId(principalId, endDate.toLocalDate()); if (activeJobs.isEmpty() || phaList.isEmpty()) { return rrAggregate; } Set<String> phaLpSet = new HashSet<String>(); Set<String> calNameSet = new HashSet<String>(); if (CollectionUtils.isNotEmpty(phaList)) { for (PrincipalHRAttributes pha : phaList) { phaLpSet.add(pha.getLeavePlan()); calNameSet.add(pha.getPayCalendar()); } } List<LeavePlan> activeLpList = new ArrayList<LeavePlan>(); List<LeavePlan> inactiveLpList = new ArrayList<LeavePlan>(); for (String lpString : phaLpSet) { List<LeavePlan> aList = HrServiceLocator.getLeavePlanService().getAllActiveLeavePlan(lpString, endDate.toLocalDate()); activeLpList.addAll(aList); aList = HrServiceLocator.getLeavePlanService().getAllInActiveLeavePlan(lpString, endDate.toLocalDate()); inactiveLpList.addAll(aList); } // get all pay calendar entries for this employee. used to determine interval dates Map<String, List<CalendarEntry>> calEntryMap = new HashMap<String, List<CalendarEntry>>(); for (String calName : calNameSet) { org.kuali.kpme.core.calendar.Calendar aCal = HrServiceLocator.getCalendarService() .getCalendarByGroup(calName); if (aCal != null) { List<CalendarEntry> aList = HrServiceLocator.getCalendarEntryService() .getAllCalendarEntriesForCalendarId(aCal.getHrCalendarId()); Collections.sort(aList); calEntryMap.put(calName, aList); } } rrAggregate.setCalEntryMap(calEntryMap); Set<String> lpStringSet = new HashSet<String>(); if (CollectionUtils.isNotEmpty(activeLpList)) { for (LeavePlan lp : activeLpList) { lpStringSet.add(lp.getLeavePlan()); } } List<SystemScheduledTimeOff> sstoList = new ArrayList<SystemScheduledTimeOff>(); for (String lpString : lpStringSet) { List<SystemScheduledTimeOff> aList = LmServiceLocator.getSysSchTimeOffService() .getSystemScheduledTimeOffsForLeavePlan(startDate.toLocalDate(), endDate.toLocalDate(), lpString); if (CollectionUtils.isNotEmpty(aList)) { sstoList.addAll(aList); } } List<AccrualCategory> activeAccrCatList = new ArrayList<AccrualCategory>(); List<AccrualCategory> inactiveAccrCatList = new ArrayList<AccrualCategory>(); for (String lpString : lpStringSet) { List<AccrualCategory> aList = HrServiceLocator.getAccrualCategoryService() .getActiveLeaveAccrualCategoriesForLeavePlan(lpString, endDate.toLocalDate()); if (CollectionUtils.isNotEmpty(aList)) { activeAccrCatList.addAll(aList); } aList = HrServiceLocator.getAccrualCategoryService() .getInActiveLeaveAccrualCategoriesForLeavePlan(lpString, endDate.toLocalDate()); if (CollectionUtils.isNotEmpty(aList)) { inactiveAccrCatList.addAll(aList); } } List<AccrualCategoryRule> activeRuleList = new ArrayList<AccrualCategoryRule>(); List<AccrualCategoryRule> inactiveRuleList = new ArrayList<AccrualCategoryRule>(); for (AccrualCategory ac : activeAccrCatList) { List<AccrualCategoryRule> aRuleList = HrServiceLocator.getAccrualCategoryRuleService() .getActiveRulesForAccrualCategoryId(ac.getLmAccrualCategoryId()); activeRuleList.addAll(aRuleList); aRuleList = HrServiceLocator.getAccrualCategoryRuleService() .getInActiveRulesForAccrualCategoryId(ac.getLmAccrualCategoryId()); inactiveRuleList.addAll(aRuleList); } List<LeaveCalendarDocumentHeader> lcDocList = LmServiceLocator.getLeaveCalendarDocumentHeaderService() .getAllDocumentHeadersInRangeForPricipalId(principalId, startDate, endDate); BigDecimal previousFte = null; List<Job> jobs = new ArrayList<Job>(); DateTime currentDate = startDate; while (!currentDate.isAfter(endDate)) { RateRange rateRange = new RateRange(); jobs = this.getJobsForDate(activeJobs, inactiveJobs, currentDate.toLocalDate()); if (jobs.isEmpty()) { // no jobs found for this day currentDate = currentDate.plusDays(1); continue; } rateRange.setJobs(jobs); // detect if there's a status change BigDecimal fteSum = HrServiceLocator.getJobService().getFteSumForJobs(jobs); rateRange.setAccrualRatePercentageModifier(fteSum); BigDecimal standardHours = HrServiceLocator.getJobService().getStandardHoursSumForJobs(jobs); rateRange.setStandardHours(standardHours); if (previousFte != null && !previousFte.equals(fteSum)) { rateRange.setStatusChanged(true); rrAggregate.setRateRangeChanged(true); } previousFte = fteSum; // figure out the PrincipalHRAttributes for this day PrincipalHRAttributes phra = this.getPrincipalHrAttributesForDate(phaList, currentDate.toLocalDate()); rateRange.setPrincipalHRAttributes(phra); if (rateRange.getPrincipalHRAttributes() != null) { // figure out if there's an end principalHrAttributes for the initial principalHRAttributes PrincipalHRAttributes endPhra = this.getInactivePrincipalHrAttributesForDate(inactivePhaList, rateRange.getPrincipalHRAttributes().getEffectiveLocalDate(), currentDate.toLocalDate()); rateRange.setEndPrincipalHRAttributes(endPhra); } // get leave plan for this day if (rateRange.getPrincipalHRAttributes() != null) { rateRange.setLeavePlan(this.getLeavePlanForDate(activeLpList, inactiveLpList, rateRange.getPrincipalHRAttributes().getLeavePlan(), currentDate.toLocalDate())); } if (rateRange.getLeavePlan() != null) { // get accrual category list for this day List<AccrualCategory> acsForDay = this.getAccrualCategoriesForDate(activeAccrCatList, inactiveAccrCatList, rateRange.getLeavePlan().getLeavePlan(), currentDate.toLocalDate()); rateRange.setAcList(acsForDay); // get System scheduled time off for this day for (SystemScheduledTimeOff ssto : sstoList) { if (ssto.getAccruedLocalDate().equals(currentDate.toLocalDate()) && ssto.getLeavePlan().equals(rateRange.getLeavePlan().getLeavePlan())) { // if there exists a ssto accrualed leave block with this ssto id, it means the ssto hours has been banked or transferred by the employee // this logic depends on the deactivateOldAccruals() runs before buildRateRangeAggregate() // because deactivateOldAccruals() removes accrued ssto leave blocks unless they are banked/transferred List<LeaveBlock> sstoLbList = LmServiceLocator.getLeaveBlockService().getSSTOLeaveBlocks( principalId, ssto.getLmSystemScheduledTimeOffId(), ssto.getAccruedLocalDate()); if (CollectionUtils.isEmpty(sstoLbList)) { rateRange.setSysScheTimeOff(ssto); } } } } // set accrual category rules for the day if (CollectionUtils.isNotEmpty(rateRange.getAcList())) { List<AccrualCategoryRule> rulesForDay = new ArrayList<AccrualCategoryRule>(); for (AccrualCategory ac : rateRange.getAcList()) { rulesForDay.addAll(this.getAccrualCategoryRulesForDate(activeRuleList, ac.getLmAccrualCategoryId(), currentDate.toLocalDate(), rateRange.getPrincipalHRAttributes().getServiceLocalDate())); } rateRange.setAcRuleList(rulesForDay); } Interval range = new Interval(currentDate, currentDate.plusDays(1)); rateRange.setRange(range); // assign leave document id to range if there is an existing leave doc for currentDate. // The doc Id will be assigned to leave blocks created at this rate range rateRange.setLeaveCalendarDocumentId(this.getLeaveDocumentForDate(lcDocList, currentDate)); rateRangeList.add(rateRange); currentDate = currentDate.plusDays(1); } rrAggregate.setRateRanges(rateRangeList); rrAggregate.setCurrentRate(null); return rrAggregate; }