1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
52 }
53 if(existingBt.getForfeitedAmount().compareTo(bt.getForfeitedAmount()) != 0) {
54
55 }
56
57 }
58 }
59
60 @Override
61 public HrBusinessObject getObjectById(String id) {
62
63 return TkServiceLocator.getLeavePayoutService().getLeavePayoutById(id);
64 }
65
66 @Override
67 public void doRouteStatusChange(DocumentHeader documentHeader) {
68
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
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
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
98 } else if (DocumentStatus.FINAL.equals(newDocumentStatus)) {
99
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
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 }