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.kuali.hr.lm.LMConstants;
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.lm.employeeoverride.EmployeeOverride;
28  import org.kuali.hr.time.principal.PrincipalHRAttributes;
29  import org.kuali.hr.time.service.base.TkServiceLocator;
30  import org.kuali.hr.time.util.TKUtils;
31  import org.kuali.hr.time.util.TkConstants;
32  import org.kuali.rice.krad.util.GlobalVariables;
33  import org.kuali.rice.krad.util.ObjectUtils;
34  
35  public class LeavePayoutValidationUtils {
36  
37  	public static boolean validateTransfer(LeavePayout leavePayout) {
38  		boolean isValid = true;
39  		String principalId = leavePayout.getPrincipalId();
40  		Date effectiveDate = leavePayout.getEffectiveDate();
41  		String fromAccrualCategory = leavePayout.getFromAccrualCategory();
42  		String payoutEarnCode = leavePayout.getEarnCode();
43  		AccrualCategory fromCat = TkServiceLocator.getAccrualCategoryService().getAccrualCategory(fromAccrualCategory, effectiveDate);
44  		AccrualCategory toCat = TkServiceLocator.getAccrualCategoryService().getAccrualCategory(payoutEarnCode, effectiveDate);
45  		PrincipalHRAttributes pha = TkServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(principalId,effectiveDate);
46  		
47  		if(ObjectUtils.isNotNull(pha)) {
48  			if(ObjectUtils.isNotNull(pha.getLeavePlan())) {
49  				AccrualCategoryRule acr = TkServiceLocator.getAccrualCategoryRuleService().getAccrualCategoryRuleForDate(fromCat, effectiveDate, pha.getServiceDate());
50  				if(ObjectUtils.isNotNull(acr)) {
51  					if(ObjectUtils.isNotNull(acr.getMaxBalFlag())
52  							&& StringUtils.isNotBlank(acr.getMaxBalFlag())
53  							&& StringUtils.isNotEmpty(acr.getMaxBalFlag())
54  							&& StringUtils.equals(acr.getMaxBalFlag(), "Y")) {
55  						if(ObjectUtils.isNotNull(acr.getMaxPayoutEarnCode()) || StringUtils.equals(LMConstants.ACTION_AT_MAX_BAL.LOSE, acr.getActionAtMaxBalance())) {
56  
57  
58  
59  
60  
61  							isValid &= validatePayoutAmount(leavePayout.getPayoutAmount(),fromCat,toCat, principalId, effectiveDate, acr);
62  						}
63  						else {
64  							
65  							GlobalVariables.getMessageMap().putError("document.newMaintainableObject.fromAccrualCategory",
66  									"leavePayout.fromAccrualCategory.rules.payoutToEarnCode",
67  									fromAccrualCategory);
68  							isValid &= false;
69  						}
70  					}
71  					else {
72  						
73  						GlobalVariables.getMessageMap().putError("document.newMaintinableObject.fromAccrualCategory",
74  								"leavePayout.fromAccrualCategory.rules.maxBalFlag", fromAccrualCategory);
75  						isValid &= false;
76  					}
77  				}
78  				else {
79  					
80  					GlobalVariables.getMessageMap().putError("document.newMaintainableObject.fromAccrualCategory",
81  							"leavePayout.fromAccrualCategory.rules.exist",fromCat.getAccrualCategory());
82  					isValid &= false;
83  				}
84  			}
85  			else {
86  				
87  				GlobalVariables.getMessageMap().putError("document.newMaintainableObject.principalId","leavePayout.principal.noLeavePlan");
88  				isValid &=false;
89  			}
90  		}
91  		else  {
92  			
93  			GlobalVariables.getMessageMap().putError("document.newMaintainableObject.principalId","leavePayout.principal.noAttributes");
94  			isValid &= false;
95  		}
96  
97  		return isValid;
98  
99  	}
100 
101 	private static boolean validatePayoutAmount(BigDecimal payoutAmount,
102 			AccrualCategory fromCat, AccrualCategory toCat, String principalId,
103 			Date effectiveDate, AccrualCategoryRule accrualRule) {
104 
105 		
106 		
107 		boolean isValid = true;
108 		BigDecimal maxPayoutAmount = null;
109 		BigDecimal adjustedMaxPayoutAmount = null;
110 		if(ObjectUtils.isNotNull(accrualRule.getMaxPayoutAmount())) {
111 			maxPayoutAmount = new BigDecimal(accrualRule.getMaxPayoutAmount());
112 			BigDecimal fullTimeEngagement = TkServiceLocator.getJobService().getFteSumForAllActiveLeaveEligibleJobs(principalId, effectiveDate);
113 			adjustedMaxPayoutAmount = maxPayoutAmount.multiply(fullTimeEngagement);
114 		}
115 		
116 		
117 		List<EmployeeOverride> overrides = TkServiceLocator.getEmployeeOverrideService().getEmployeeOverrides(principalId, effectiveDate);
118 		for(EmployeeOverride override : overrides) {
119 			if(override.getOverrideType().equals(TkConstants.EMPLOYEE_OVERRIDE_TYPE.get("MPA")))
120 				adjustedMaxPayoutAmount = new BigDecimal(override.getOverrideValue());
121 		}
122 				
123 		if(ObjectUtils.isNotNull(adjustedMaxPayoutAmount)) {
124 			if(payoutAmount.compareTo(adjustedMaxPayoutAmount) > 0) {
125 				isValid &= false;
126 				String fromUnitOfTime = TkConstants.UNIT_OF_TIME.get(fromCat.getUnitOfTime());
127 				GlobalVariables.getMessageMap().putError("leavePayout.payoutAmount","leavePayout.payoutAmount.maxPayoutAmount",adjustedMaxPayoutAmount.toString(),fromUnitOfTime);
128 			}
129 		}
130 		
131 		if(payoutAmount.compareTo(BigDecimal.ZERO) < 0 ) {
132 			isValid &= false;
133 			GlobalVariables.getMessageMap().putError("leavePayout.payoutAmount","leavePayout.payoutAmount.negative");
134 		}
135 		return isValid;
136 	}
137 
138 	
139 }