001 /**
002 * Copyright 2004-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.hr.lm.leavepayout.service;
017
018 import java.math.BigDecimal;
019 import java.util.List;
020
021 import org.apache.commons.collections.CollectionUtils;
022 import org.apache.commons.lang3.StringUtils;
023 import org.kuali.hr.lm.LMConstants;
024 import org.kuali.hr.lm.balancetransfer.BalanceTransfer;
025 import org.kuali.hr.lm.leavepayout.LeavePayout;
026 import org.kuali.hr.lm.leaveblock.LeaveBlock;
027 import org.kuali.hr.time.HrBusinessObject;
028 import org.kuali.hr.time.service.base.TkServiceLocator;
029 import org.kuali.hr.time.util.HrBusinessObjectMaintainableImpl;
030 import org.kuali.rice.kew.api.document.DocumentStatus;
031 import org.kuali.rice.kew.api.exception.WorkflowException;
032 import org.kuali.rice.kns.document.MaintenanceDocument;
033 import org.kuali.rice.krad.bo.DocumentHeader;
034 import org.kuali.rice.krad.service.DocumentService;
035 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
036 import org.kuali.rice.krad.util.ObjectUtils;
037
038 public class LeavePayoutMaintainableImpl extends
039 HrBusinessObjectMaintainableImpl {
040
041 private static final long serialVersionUID = 1L;
042
043 @Override
044 public void saveBusinessObject() {
045 LeavePayout bt = (LeavePayout) this.getBusinessObject();
046
047 LeavePayout existingBt = TkServiceLocator.getLeavePayoutService().getLeavePayoutById(bt.getId());
048
049 if(ObjectUtils.isNotNull(existingBt)) {
050 if(existingBt.getPayoutAmount().compareTo(bt.getPayoutAmount()) != 0) {
051 //TODO: Create leave block reference within bt, and update leave amount.
052 }
053 if(existingBt.getForfeitedAmount().compareTo(bt.getForfeitedAmount()) != 0) {
054 //TODO: Create reference within bt for forfeited leave block, update leave amount.
055 }
056 //Will approvers / department admins be changing accrual category? effective date?
057 }
058 }
059
060 @Override
061 public HrBusinessObject getObjectById(String id) {
062 // TODO Auto-generated method stub
063 return TkServiceLocator.getLeavePayoutService().getLeavePayoutById(id);
064 }
065
066 @Override
067 public void doRouteStatusChange(DocumentHeader documentHeader) {
068 //ProcessDocReport pdr = new ProcessDocReport(true, "");
069 String documentId = documentHeader.getDocumentNumber();
070 LeavePayout payout = (LeavePayout)this.getDataObject();
071 DocumentService documentService = KRADServiceLocatorWeb.getDocumentService();
072
073 DocumentStatus newDocumentStatus = documentHeader.getWorkflowDocument().getStatus();
074 String routedByPrincipalId = documentHeader.getWorkflowDocument().getRoutedByPrincipalId();
075 if (DocumentStatus.ENROUTE.equals(newDocumentStatus)
076 && CollectionUtils.isEmpty(payout.getLeaveBlocks())) {
077 //when payout document is routed, initiate the leave payout - creating the leave blocks
078 try {
079 MaintenanceDocument md = (MaintenanceDocument)KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderId(documentId);
080
081 payout = TkServiceLocator.getLeavePayoutService().payout(payout);
082 md.getNewMaintainableObject().setDataObject(payout);
083 documentService.saveDocument(md);
084 }
085 catch (WorkflowException e) {
086 LOG.error("caught exception while handling doRouteStatusChange -> documentService.getByDocumentHeaderId(" + documentHeader.getDocumentNumber() + "). ", e);
087 throw new RuntimeException("caught exception while handling doRouteStatusChange -> documentService.getByDocumentHeaderId(" + documentHeader.getDocumentNumber() + "). ", e);
088 }
089 } else if (DocumentStatus.DISAPPROVED.equals(newDocumentStatus)) {
090 //When payout document is disapproved, set all leave block's request statuses to disapproved.
091 for(LeaveBlock lb : payout.getLeaveBlocks()) {
092 if(ObjectUtils.isNotNull(lb)) {
093 lb.setRequestStatus(LMConstants.REQUEST_STATUS.DISAPPROVED);
094 TkServiceLocator.getLeaveBlockService().deleteLeaveBlock(lb.getLmLeaveBlockId(), routedByPrincipalId);
095 }
096 }
097 //update status of document and associated leave blocks.
098 } else if (DocumentStatus.FINAL.equals(newDocumentStatus)) {
099 //When payout document moves to final, set all leave block's request statuses to approved.
100 for(LeaveBlock lb : payout.getLeaveBlocks()) {
101 if(ObjectUtils.isNotNull(lb)) {
102 lb.setRequestStatus(LMConstants.REQUEST_STATUS.APPROVED);
103 TkServiceLocator.getLeaveBlockService().updateLeaveBlock(lb, routedByPrincipalId);
104 }
105 }
106
107 List<LeaveBlock> leaveBlocks = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDocumentId(documentId);
108 LeaveBlock carryOverBlock = null;
109 for(LeaveBlock lb : leaveBlocks) {
110 if(StringUtils.equals(lb.getAccrualCategory(),payout.getFromAccrualCategory())
111 && StringUtils.equals(lb.getDescription(),"Max carry over adjustment")
112 && lb.getAccrualGenerated()) {
113 carryOverBlock = lb;
114 }
115 }
116 if(carryOverBlock != null) {
117 BigDecimal adjustment = new BigDecimal(0);
118 if(payout.getPayoutAmount() != null)
119 adjustment = adjustment.add(payout.getPayoutAmount().abs());
120 if(payout.getForfeitedAmount() != null)
121 adjustment = adjustment.add(payout.getForfeitedAmount().abs());
122 BigDecimal adjustedLeaveAmount = carryOverBlock.getLeaveAmount().abs().subtract(adjustment);
123 carryOverBlock.setLeaveAmount(adjustedLeaveAmount.negate());
124 TkServiceLocator.getLeaveBlockService().updateLeaveBlock(carryOverBlock, routedByPrincipalId);
125 }
126
127 } else if (DocumentStatus.CANCELED.equals(newDocumentStatus)) {
128 //When payout document is canceled, set all leave block's request statuses to deferred
129 for(LeaveBlock lb : payout.getLeaveBlocks()) {
130 if(ObjectUtils.isNotNull(lb)) {
131 lb.setRequestStatus(LMConstants.REQUEST_STATUS.DEFERRED);
132 TkServiceLocator.getLeaveBlockService().updateLeaveBlock(lb, routedByPrincipalId);
133 }
134 }
135 }
136 }
137
138 }