View Javadoc

1   /**
2    * Copyright 2004-2014 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.kpme.tklm.leave.payout.web;
17  
18  import java.math.BigDecimal;
19  import java.util.List;
20  
21  import org.apache.commons.collections.CollectionUtils;
22  import org.apache.commons.lang.StringUtils;
23  import org.kuali.kpme.core.bo.HrBusinessObject;
24  import org.kuali.kpme.core.bo.HrBusinessObjectMaintainableImpl;
25  import org.kuali.kpme.core.util.HrConstants;
26  import org.kuali.kpme.core.util.TKUtils;
27  import org.kuali.kpme.tklm.common.LMConstants;
28  import org.kuali.kpme.tklm.leave.block.LeaveBlock;
29  import org.kuali.kpme.tklm.leave.payout.LeavePayout;
30  import org.kuali.kpme.tklm.leave.service.LmServiceLocator;
31  import org.kuali.rice.kew.api.document.DocumentStatus;
32  import org.kuali.rice.kew.api.exception.WorkflowException;
33  import org.kuali.rice.kns.document.MaintenanceDocument;
34  import org.kuali.rice.krad.bo.DocumentHeader;
35  import org.kuali.rice.krad.service.DocumentService;
36  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
37  import org.kuali.rice.krad.util.ObjectUtils;
38  
39  public class LeavePayoutMaintainableImpl extends
40          HrBusinessObjectMaintainableImpl {
41  
42      private static final long serialVersionUID = 1L;
43  
44      @Override
45      public HrBusinessObject getObjectById(String id) {
46          return LmServiceLocator.getLeavePayoutService().getLeavePayoutById(id);
47      }
48  
49      @Override
50      public void doRouteStatusChange(DocumentHeader documentHeader) {
51          //ProcessDocReport pdr = new ProcessDocReport(true, "");
52          String documentId = documentHeader.getDocumentNumber();
53          LeavePayout payout = (LeavePayout)this.getDataObject();
54          DocumentService documentService = KRADServiceLocatorWeb.getDocumentService();
55          payout.setDocumentHeaderId(documentId);
56          DocumentStatus newDocumentStatus = documentHeader.getWorkflowDocument().getStatus();
57          String routedByPrincipalId = documentHeader.getWorkflowDocument().getRoutedByPrincipalId();
58          if (DocumentStatus.ENROUTE.equals(newDocumentStatus)
59                  && CollectionUtils.isEmpty(payout.getLeaveBlocks())) {
60              //when payout document is routed, initiate the leave payout - creating the leave blocks
61              try {
62                  MaintenanceDocument md = (MaintenanceDocument)KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderId(documentId);
63                  payout = LmServiceLocator.getLeavePayoutService().payout(payout);
64                  md.getDocumentHeader().setDocumentDescription(TKUtils.getDocumentDescription(payout.getPrincipalId(), payout.getEffectiveLocalDate()));
65                  md.getNewMaintainableObject().setDataObject(payout);
66                  documentService.saveDocument(md);
67              }
68              catch (WorkflowException e) {
69                  LOG.error("caught exception while handling doRouteStatusChange -> documentService.getByDocumentHeaderId(" + documentHeader.getDocumentNumber() + "). ", e);
70                  throw new RuntimeException("caught exception while handling doRouteStatusChange -> documentService.getByDocumentHeaderId(" + documentHeader.getDocumentNumber() + "). ", e);
71              }
72          } else if (DocumentStatus.DISAPPROVED.equals(newDocumentStatus)) {
73              //When payout document is disapproved, set all leave block's request statuses to disapproved.
74              for(LeaveBlock lb : payout.getLeaveBlocks()) {
75                  if(ObjectUtils.isNotNull(lb)) {
76                      lb.setRequestStatus(HrConstants.REQUEST_STATUS.DISAPPROVED);
77                      LmServiceLocator.getLeaveBlockService().deleteLeaveBlock(lb.getLmLeaveBlockId(), routedByPrincipalId);
78                  }
79              }
80              //update status of document and associated leave blocks.
81          } else if (DocumentStatus.FINAL.equals(newDocumentStatus)) {
82              //When payout document moves to final, set all leave block's request statuses to approved.
83              for(LeaveBlock lb : payout.getLeaveBlocks()) {
84                  if(ObjectUtils.isNotNull(lb)) {
85                      lb.setRequestStatus(HrConstants.REQUEST_STATUS.APPROVED);
86                      LmServiceLocator.getLeaveBlockService().updateLeaveBlock(lb, routedByPrincipalId);
87                  }
88              }
89              
90              List<LeaveBlock> leaveBlocks = LmServiceLocator.getLeaveBlockService().getLeaveBlocksForDocumentId(payout.getLeaveCalendarDocumentId());
91              for(LeaveBlock lb : leaveBlocks) {
92              	if(StringUtils.equals(lb.getAccrualCategory(),payout.getFromAccrualCategory())
93              			&& StringUtils.equals(lb.getLeaveBlockType(), LMConstants.LEAVE_BLOCK_TYPE.CARRY_OVER_ADJUSTMENT)) {
94              		BigDecimal adjustment = new BigDecimal(0);
95              		if(payout.getPayoutAmount() != null)
96              			adjustment = adjustment.add(payout.getPayoutAmount().abs());
97              		if(payout.getForfeitedAmount() != null)
98              			adjustment = adjustment.add(payout.getForfeitedAmount().abs());
99              		BigDecimal adjustedLeaveAmount = lb.getLeaveAmount().abs().subtract(adjustment);
100             		lb.setLeaveAmount(adjustedLeaveAmount.negate());
101             		LmServiceLocator.getLeaveBlockService().updateLeaveBlock(lb, routedByPrincipalId);
102             	}
103             }
104         } else if (DocumentStatus.CANCELED.equals(newDocumentStatus)) {
105             //When payout document is canceled, set all leave block's request statuses to deferred
106             for(LeaveBlock lb : payout.getLeaveBlocks()) {
107                 if(ObjectUtils.isNotNull(lb)) {
108                     lb.setRequestStatus(HrConstants.REQUEST_STATUS.DEFERRED);
109                     LmServiceLocator.getLeaveBlockService().updateLeaveBlock(lb, routedByPrincipalId);
110                 }
111             }
112         }
113     }
114 
115 }