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(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 }