View Javadoc

1   /**
2    * Copyright 2004-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.kpme.tklm.leave.payout;
17  
18  import java.math.BigDecimal;
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.kuali.kpme.core.accrualcategory.AccrualCategory;
24  import org.kuali.kpme.core.api.assignment.Assignable;
25  import org.kuali.kpme.core.assignment.Assignment;
26  import org.kuali.kpme.core.bo.HrBusinessObject;
27  import org.kuali.kpme.core.earncode.EarnCode;
28  import org.kuali.kpme.core.principal.PrincipalHRAttributes;
29  import org.kuali.kpme.core.service.HrServiceLocator;
30  import org.kuali.kpme.tklm.api.leave.payout.LeavePayoutContract;
31  import org.kuali.kpme.tklm.leave.block.LeaveBlock;
32  import org.kuali.kpme.tklm.leave.service.LmServiceLocator;
33  import org.kuali.rice.kim.api.identity.Person;
34  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
35  
36  public class LeavePayout extends HrBusinessObject implements Assignable, LeavePayoutContract {
37  
38  	private static final long serialVersionUID = 1L;
39  
40  	private String lmLeavePayoutId;
41  	private String principalId;
42  	private String documentHeaderId;
43  	private String fromAccrualCategory;
44  	private String earnCode;
45  	private String description;
46  	private BigDecimal payoutAmount = new BigDecimal("0.0");
47      private BigDecimal forfeitedAmount = new BigDecimal("0.0");
48  	private transient Person principal;
49  	private transient AccrualCategory fromAccrualCategoryObj;
50  	private transient EarnCode earnCodeObj;
51  	private transient PrincipalHRAttributes principalHRAttrObj;
52      private String leaveCalendarDocumentId;
53      private String accrualCategoryRule;
54      private String forfeitedLeaveBlockId;
55      private String payoutFromLeaveBlockId;
56      private String payoutLeaveBlockId;
57  
58  	private String status;
59  
60  	public String getEarnCode() {
61  		return earnCode;
62  	}
63  	public void setEarnCode(String earnCode) {
64  		this.earnCode = earnCode;
65  	}
66  	public EarnCode getEarnCodeObj() {
67  		if (earnCodeObj == null) {
68              earnCodeObj = HrServiceLocator.getEarnCodeService().getEarnCode(this.earnCode, getEffectiveLocalDate());
69          }
70          return earnCodeObj;
71  	}
72  	public void setEarnCodeObj(EarnCode earnCodeObj) {
73  		this.earnCodeObj = earnCodeObj;
74  	}
75  	public String getPrincipalId() {
76  		return principalId;
77  	}
78  	public void setPrincipalId(String principalId) {
79  		this.principalId = principalId;
80  	}
81      public Person getPrincipal() {
82          return principal;
83      }
84      public void setPrincipal(Person principal) {
85          this.principal = principal;
86      }
87  	public String getName() {
88  		if (principal == null) {
89          principal = KimApiServiceLocator.getPersonService().getPerson(this.principalId);
90  		}
91  		return (principal != null) ? principal.getName() : "";
92  	}
93  	
94  	public String getLeavePlan() {
95  		if (!StringUtils.isEmpty(this.principalId)) {
96  			principalHRAttrObj = HrServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(principalId, this.getEffectiveLocalDate());
97  		}
98  		return (principalHRAttrObj != null) ? principalHRAttrObj.getLeavePlan() : "";
99  	}
100 
101 	public void setLeavePlan(String leavePlan) {
102 	}
103 	public String getFromAccrualCategory() {
104 		return fromAccrualCategory;
105 	}
106 	public void setFromAccrualCategory(String accrualCategory) {
107 		this.fromAccrualCategory = accrualCategory;
108 	}
109 	
110 	public String getDescription() {
111 		return description;
112 	}
113 	public void setDescription(String description) {
114 		this.description = description;
115 	}
116 	public BigDecimal getPayoutAmount() {
117 		return payoutAmount;
118 	}
119 	public void setPayoutAmount(BigDecimal amount) {
120 		this.payoutAmount = amount;
121 	}
122     public BigDecimal getForfeitedAmount() {
123         return forfeitedAmount;
124     }
125     public void setForfeitedAmount(BigDecimal amount) {
126         this.forfeitedAmount = amount;
127     }
128 	public AccrualCategory getFromAccrualCategoryObj() {
129         if (fromAccrualCategoryObj == null) {
130             fromAccrualCategoryObj =  HrServiceLocator.getAccrualCategoryService().getAccrualCategory(fromAccrualCategory, getEffectiveLocalDate());
131         }
132         return fromAccrualCategoryObj;
133 	}
134 	public void setFromAccrualCategoryObj(AccrualCategory accrualCategoryObj) {
135 		this.fromAccrualCategoryObj = accrualCategoryObj;
136 	}
137 	public static long getSerialversionuid() {
138 		return serialVersionUID;
139 	}
140 	public String getLmLeavePayoutId() {
141 		return lmLeavePayoutId;
142 	}
143 	public void setLmLeavePayoutId(String id) {
144 		this.lmLeavePayoutId = id;
145 	}
146 	
147 	@Override
148 	protected String getUniqueKey() {
149 		return getLmLeavePayoutId();
150 	}
151 	
152 	@Override
153 	public String getId() {
154 		return getLmLeavePayoutId();
155 	}
156 
157 	@Override
158 	public void setId(String id) {
159 		setLmLeavePayoutId(id);
160 	}
161 
162 	public PrincipalHRAttributes getPrincipalHRAttrObj() {
163 		return principalHRAttrObj;
164 	}
165 	public void setPrincipalHRAttrObj(PrincipalHRAttributes hrObj) {
166 		this.principalHRAttrObj = hrObj;
167 	}
168 	public String getAccrualCategoryRule() {
169 		return accrualCategoryRule;
170 	}
171 	public void setAccrualCategoryRule(String accrualCategoryRule) {
172 		this.accrualCategoryRule = accrualCategoryRule;
173 	}
174 	public String getForfeitedLeaveBlockId() {
175 		return forfeitedLeaveBlockId;
176 	}
177 	public void setForfeitedLeaveBlockId(String forfeitedLeaveBlockId) {
178 		this.forfeitedLeaveBlockId = forfeitedLeaveBlockId;
179 	}
180 	public String getPayoutLeaveBlockId() {
181 		return payoutLeaveBlockId;
182 	}
183 	public void setPayoutLeaveBlockId(String payoutLeaveBlockId) {
184 		this.payoutLeaveBlockId = payoutLeaveBlockId;
185 	}
186 	public String getPayoutFromLeaveBlockId() {
187 		return payoutFromLeaveBlockId;
188 	}
189 	public void setPayoutFromLeaveBlockId(String payoutFromLeaveBlockId) {
190 		this.payoutFromLeaveBlockId = payoutFromLeaveBlockId;
191 	}
192 
193 	public void setLeaveCalendarDocumentId(String leaveCalendarDocumentId) {
194 		this.leaveCalendarDocumentId = leaveCalendarDocumentId;
195 	}
196 
197     public List<LeaveBlock> getLeaveBlocks() {
198         List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
199 
200 		if (getForfeitedLeaveBlockId() != null) {
201 		    leaveBlocks.add(LmServiceLocator.getLeaveBlockService().getLeaveBlock(forfeitedLeaveBlockId));
202         }
203         if (getPayoutFromLeaveBlockId() != null) {
204 		    leaveBlocks.add(LmServiceLocator.getLeaveBlockService().getLeaveBlock(payoutFromLeaveBlockId));
205         }
206         if (getPayoutLeaveBlockId() != null) {
207 		    leaveBlocks.add(LmServiceLocator.getLeaveBlockService().getLeaveBlock(payoutLeaveBlockId));
208         }
209         return leaveBlocks;
210     }
211 
212     public String getLeaveCalendarDocumentId() {
213         return leaveCalendarDocumentId;
214     }
215 	public LeavePayout adjust(BigDecimal payoutAmount) {
216 		BigDecimal difference = this.payoutAmount.subtract(payoutAmount);
217 		//technically if there is forfeiture, then the transfer amount has already been maximized
218 		//via BalanceTransferService::initializeTransfer(...)
219 		//i.o.w. transfer amount cannot be increased.
220 		//this method is written with the intention of eventually allowing end user to adjust the transfer
221 		//amount as many times as they wish before submitting. Currently they cannot.
222 		if(difference.signum() < 0) {
223 			//transfer amount is being increased.
224 			if(forfeitedAmount.compareTo(BigDecimal.ZERO) > 0) {
225 				//transfer amount has already been maximized.
226 				if(forfeitedAmount.compareTo(difference.abs()) >= 0)
227 					// there is enough leave in the forfeited amount to take out the difference.
228 					forfeitedAmount = forfeitedAmount.subtract(difference.abs());
229 				else
230 					// the difference zero's the forfeited amount.
231 					forfeitedAmount = BigDecimal.ZERO;
232 			}
233 			// a forfeited amount equal to zero with an increase in the transfer amount
234 			// does not produce forfeiture.
235 			// forfeiture cannot be negative.
236 		}
237 		else if (difference.signum() > 0) {
238 			//transfer amount is being decreased
239 			forfeitedAmount = forfeitedAmount.add(difference);
240 		}
241 
242 		this.payoutAmount = payoutAmount;
243 		
244 		return this;
245 	}
246 	
247 	public String getStatus() {
248 		return status;
249 	}
250 	
251 	public void setStatus(String code) {
252 		status = code;
253 	}
254 	public String getDocumentHeaderId() {
255 		return documentHeaderId;
256 	}
257 	public void setDocumentHeaderId(String documentHeaderId) {
258 		this.documentHeaderId = documentHeaderId;
259 	}
260 
261     @Override
262     public List<Assignment> getAssignments() {
263         return HrServiceLocator.getAssignmentService().getAssignments(getPrincipalId(), getEffectiveLocalDate());
264     }
265 }