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(payout.getLeaveCalendarDocumentId());
108
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
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
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 }