View Javadoc

1   /**
2    * Copyright 2004-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.hr.lm.leavepayout.service;
17  
18  import java.math.BigDecimal;
19  import java.util.List;
20  
21  import org.apache.commons.collections.CollectionUtils;
22  import org.apache.commons.lang3.StringUtils;
23  import org.kuali.hr.lm.LMConstants;
24  import org.kuali.hr.lm.balancetransfer.BalanceTransfer;
25  import org.kuali.hr.lm.leavepayout.LeavePayout;
26  import org.kuali.hr.lm.leaveblock.LeaveBlock;
27  import org.kuali.hr.time.HrBusinessObject;
28  import org.kuali.hr.time.service.base.TkServiceLocator;
29  import org.kuali.hr.time.util.HrBusinessObjectMaintainableImpl;
30  import org.kuali.rice.kew.api.document.DocumentStatus;
31  import org.kuali.rice.kew.api.exception.WorkflowException;
32  import org.kuali.rice.kns.document.MaintenanceDocument;
33  import org.kuali.rice.krad.bo.DocumentHeader;
34  import org.kuali.rice.krad.service.DocumentService;
35  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
36  import org.kuali.rice.krad.util.ObjectUtils;
37  
38  public class LeavePayoutMaintainableImpl extends
39          HrBusinessObjectMaintainableImpl {
40  
41      private static final long serialVersionUID = 1L;
42  
43      @Override
44      public void saveBusinessObject() {
45  		LeavePayout bt = (LeavePayout) this.getBusinessObject();
46  		
47  		LeavePayout existingBt = TkServiceLocator.getLeavePayoutService().getLeavePayoutById(bt.getId());
48  		
49  		if(ObjectUtils.isNotNull(existingBt)) {
50  			if(existingBt.getPayoutAmount().compareTo(bt.getPayoutAmount()) != 0) {
51  				//TODO: Create leave block reference within bt, and update leave amount.
52  			}
53  			if(existingBt.getForfeitedAmount().compareTo(bt.getForfeitedAmount()) != 0) {
54  				//TODO: Create reference within bt for forfeited leave block, update leave amount.
55  			}
56  			//Will approvers / department admins be changing accrual category? effective date?
57  		}
58      }
59      
60      @Override
61      public HrBusinessObject getObjectById(String id) {
62          // TODO Auto-generated method stub
63          return TkServiceLocator.getLeavePayoutService().getLeavePayoutById(id);
64      }
65  
66      @Override
67      public void doRouteStatusChange(DocumentHeader documentHeader) {
68          //ProcessDocReport pdr = new ProcessDocReport(true, "");
69          String documentId = documentHeader.getDocumentNumber();
70          LeavePayout payout = (LeavePayout)this.getDataObject();
71          DocumentService documentService = KRADServiceLocatorWeb.getDocumentService();
72  
73          DocumentStatus newDocumentStatus = documentHeader.getWorkflowDocument().getStatus();
74          String routedByPrincipalId = documentHeader.getWorkflowDocument().getRoutedByPrincipalId();
75          if (DocumentStatus.ENROUTE.equals(newDocumentStatus)
76                  && CollectionUtils.isEmpty(payout.getLeaveBlocks())) {
77              //when payout document is routed, initiate the leave payout - creating the leave blocks
78              try {
79                  MaintenanceDocument md = (MaintenanceDocument)KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderId(documentId);
80  
81                  payout = TkServiceLocator.getLeavePayoutService().payout(payout);
82                  md.getNewMaintainableObject().setDataObject(payout);
83                  documentService.saveDocument(md);
84              }
85              catch (WorkflowException e) {
86                  LOG.error("caught exception while handling doRouteStatusChange -> documentService.getByDocumentHeaderId(" + documentHeader.getDocumentNumber() + "). ", e);
87                  throw new RuntimeException("caught exception while handling doRouteStatusChange -> documentService.getByDocumentHeaderId(" + documentHeader.getDocumentNumber() + "). ", e);
88              }
89          } else if (DocumentStatus.DISAPPROVED.equals(newDocumentStatus)) {
90              //When payout document is disapproved, set all leave block's request statuses to disapproved.
91              for(LeaveBlock lb : payout.getLeaveBlocks()) {
92                  if(ObjectUtils.isNotNull(lb)) {
93                      lb.setRequestStatus(LMConstants.REQUEST_STATUS.DISAPPROVED);
94                      TkServiceLocator.getLeaveBlockService().deleteLeaveBlock(lb.getLmLeaveBlockId(), routedByPrincipalId);
95                  }
96              }
97              //update status of document and associated leave blocks.
98          } else if (DocumentStatus.FINAL.equals(newDocumentStatus)) {
99              //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(payout.getLeaveCalendarDocumentId());
108             //List<LeaveBlock> leaveBlocks = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(payout.getPrincipalId(), payout.getEffectiveDate());
109             for(LeaveBlock lb : leaveBlocks) {
110             	if(StringUtils.equals(lb.getAccrualCategory(),payout.getFromAccrualCategory())
111             			&& StringUtils.equals(lb.getLeaveBlockType(), LMConstants.LEAVE_BLOCK_TYPE.CARRY_OVER_ADJUSTMENT)) {
112             		//KPME-2253
113                 	BigDecimal adjustment = new BigDecimal(0);
114                 	if(payout.getPayoutAmount() != null)
115                 		adjustment = adjustment.add(payout.getPayoutAmount().abs());
116                 	if(payout.getForfeitedAmount() != null)
117                 		adjustment = adjustment.add(payout.getForfeitedAmount().abs());
118                 	BigDecimal adjustedLeaveAmount = lb.getLeaveAmount().abs().subtract(adjustment);
119                 	lb.setLeaveAmount(adjustedLeaveAmount.negate());
120             		TkServiceLocator.getLeaveBlockService().updateLeaveBlock(lb, routedByPrincipalId);
121             	}
122             }
123         } else if (DocumentStatus.CANCELED.equals(newDocumentStatus)) {
124             //When payout document is canceled, set all leave block's request statuses to deferred
125             for(LeaveBlock lb : payout.getLeaveBlocks()) {
126                 if(ObjectUtils.isNotNull(lb)) {
127                     lb.setRequestStatus(LMConstants.REQUEST_STATUS.DEFERRED);
128                     TkServiceLocator.getLeaveBlockService().updateLeaveBlock(lb, routedByPrincipalId);
129                 }
130             }
131         }
132     }
133 
134 }