1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.lm.leavepayout.validation;
17
18 import java.math.BigDecimal;
19 import java.sql.Date;
20 import java.util.List;
21
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.commons.lang.time.DateUtils;
24 import org.kuali.hr.lm.accrual.AccrualCategory;
25 import org.kuali.hr.lm.accrual.AccrualCategoryRule;
26 import org.kuali.hr.lm.leavepayout.LeavePayout;
27 import org.kuali.hr.time.earncode.EarnCode;
28 import org.kuali.hr.time.principal.PrincipalHRAttributes;
29 import org.kuali.hr.time.service.base.TkServiceLocator;
30 import org.kuali.hr.time.util.TKContext;
31 import org.kuali.hr.time.util.TKUtils;
32 import org.kuali.rice.kns.document.MaintenanceDocument;
33 import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
34 import org.kuali.rice.krad.bo.PersistableBusinessObject;
35 import org.kuali.rice.krad.util.GlobalVariables;
36 import org.kuali.rice.krad.util.ObjectUtils;
37
38 public class LeavePayoutValidation extends MaintenanceDocumentRuleBase {
39
40 private boolean validateAgainstLeavePlan(PrincipalHRAttributes pha, AccrualCategory fromAccrualCategory, Date effectiveDate) {
41 boolean isValid = true;
42
43 List<AccrualCategory> accrualCategories = TkServiceLocator.getAccrualCategoryService().getActiveAccrualCategoriesForLeavePlan(pha.getLeavePlan(), effectiveDate);
44 if(accrualCategories.size() > 0) {
45 boolean isFromInLeavePlan = false;
46 for(AccrualCategory activeAccrualCategory : accrualCategories) {
47 if(StringUtils.equals(activeAccrualCategory.getLmAccrualCategoryId(),fromAccrualCategory.getLmAccrualCategoryId())) {
48 isFromInLeavePlan = true;
49 }
50 }
51 if(!isFromInLeavePlan) {
52 GlobalVariables.getMessageMap().putError("document.newMaintainableObject.fromAccrualCategory", "leavePayout.accrualCategory.notInLeavePlan", fromAccrualCategory.getAccrualCategory());
53 isValid &= false;
54 }
55 }
56 else {
57 GlobalVariables.getMessageMap().putError("document.newMaintainableObject.principalId", "leavePayout.principal.noACinLeavePlan");
58 isValid &=false;
59 }
60
61 return isValid;
62 }
63
64
65
66
67
68
69
70
71
72
73
74
75
76 private boolean validatePayoutAmount(BigDecimal transferAmount,
77 AccrualCategory debitedAccrualCategory,
78 EarnCode payoutEarnCode, String principalId, Date effectiveDate) {
79
80 if(transferAmount.compareTo(BigDecimal.ZERO) < 0 ) {
81 GlobalVariables.getMessageMap().putError("document.newMaintainableObject.transferAmount", "leavePayout.amount.negative");
82 return false;
83 }
84
85
86 return true;
87 }
88
89
90
91
92
93
94 private boolean validateEffectiveDate(Date date) {
95
96 if(date.getTime() > DateUtils.addYears(TKUtils.getCurrentDate(), 1).getTime()) {
97 GlobalVariables.getMessageMap().putError("document.newMaintainableObject.effectiveDate", "leavePayout.effectiveDate.overOneYear");
98 return false;
99 }
100 return true;
101 }
102
103
104
105
106
107
108
109
110
111
112 private boolean validateTransferFromAccrualCategory(AccrualCategory accrualCategory, String principalId,
113 Date effectiveDate, AccrualCategoryRule acr) {
114
115 PrincipalHRAttributes pha = TkServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(principalId, effectiveDate);
116
117 return true;
118 }
119
120 private boolean validatePrincipal(PrincipalHRAttributes pha, String principalId) {
121
122
123
124
125 return true;
126 }
127
128
129
130
131
132
133
134
135
136
137
138
139 @Override
140 public boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
141 boolean isValid = true;
142 LOG.debug("entering custom validation for Balance Transfer");
143
144 PersistableBusinessObject pbo = (PersistableBusinessObject) this.getNewBo();
145
146 if(pbo instanceof LeavePayout) {
147
148 LeavePayout leavePayout = (LeavePayout) pbo;
149
150
151
152
153
154
155
156
157
158
159
160 String principalId = leavePayout.getPrincipalId();
161 Date effectiveDate = leavePayout.getEffectiveDate();
162 String fromAccrualCategory = leavePayout.getFromAccrualCategory();
163 EarnCode payoutEarnCode = leavePayout.getEarnCodeObj();
164 AccrualCategory fromCat = TkServiceLocator.getAccrualCategoryService().getAccrualCategory(fromAccrualCategory, effectiveDate);
165 PrincipalHRAttributes pha = TkServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(principalId,effectiveDate);
166
167 boolean isDeptAdmin = TKContext.getUser().isDepartmentAdmin();
168 boolean isSysAdmin = TKContext.getUser().isSystemAdmin();
169 if(isDeptAdmin || isSysAdmin) {
170 isValid &= validatePayoutAmount(leavePayout.getPayoutAmount(),fromCat,payoutEarnCode, principalId, effectiveDate);
171 }
172 else {
173 if(ObjectUtils.isNotNull(pha)) {
174 if(ObjectUtils.isNotNull(pha.getLeavePlan())) {
175 AccrualCategoryRule acr = TkServiceLocator.getAccrualCategoryRuleService().getAccrualCategoryRuleForDate(fromCat, effectiveDate, pha.getServiceDate());
176 if(ObjectUtils.isNotNull(acr)) {
177 if(ObjectUtils.isNotNull(acr.getMaxBalFlag())
178 && StringUtils.isNotBlank(acr.getMaxBalFlag())
179 && StringUtils.isNotEmpty(acr.getMaxBalFlag())
180 && StringUtils.equals(acr.getMaxBalFlag(), "Y")) {
181 if(ObjectUtils.isNotNull(payoutEarnCode)) {
182 isValid &= validatePrincipal(pha,principalId);
183 isValid &= validateEffectiveDate(effectiveDate);
184 isValid &= validateAgainstLeavePlan(pha,fromCat,effectiveDate);
185 isValid &= validateTransferFromAccrualCategory(fromCat,principalId,effectiveDate,acr);
186 isValid &= validatePayoutAmount(leavePayout.getPayoutAmount(),fromCat,payoutEarnCode, null, null);
187 }
188 else {
189
190 GlobalVariables.getMessageMap().putError("document.newMaintainableObject.fromAccrualCategory",
191 "leavePayout.fromAccrualCategory.rules.payoutToEarnCode",
192 fromAccrualCategory);
193 isValid &= false;
194 }
195 }
196 else {
197
198 GlobalVariables.getMessageMap().putError("document.newMaintinableObject.fromAccrualCategory",
199 "leavePayout.fromAccrualCategory.rules.maxBalFlag", fromAccrualCategory);
200 isValid &= false;
201 }
202 }
203 else {
204
205 GlobalVariables.getMessageMap().putError("document.newMaintainableObject.fromAccrualCategory",
206 "leavePayout.fromAccrualCategory.rules.exist",fromCat.getAccrualCategory());
207 isValid &= false;
208 }
209 }
210 else {
211
212 GlobalVariables.getMessageMap().putError("document.newMaintainableObject.principalId","leavePayout.principal.noLeavePlan");
213 isValid &=false;
214 }
215 }
216 else {
217
218 GlobalVariables.getMessageMap().putError("document.newMaintainableObject.principalId","leavePayout.principal.noAttributes");
219 isValid &= false;
220 }
221 }
222 }
223 return isValid;
224 }
225
226 @Override
227 protected boolean processCustomApproveDocumentBusinessRules(
228 MaintenanceDocument document) {
229
230 return super.processCustomApproveDocumentBusinessRules(document);
231 }
232
233 }