1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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
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
81 } else if (DocumentStatus.FINAL.equals(newDocumentStatus)) {
82
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
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 }