View Javadoc

1   /**
2    * Copyright 2004-2013 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.hr.lm.leavepayout;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.hr.lm.accrual.AccrualCategory;
20  import org.kuali.hr.lm.accrual.AccrualCategoryRule;
21  import org.kuali.hr.lm.leaveblock.LeaveBlock;
22  import org.kuali.hr.time.HrBusinessObject;
23  import org.kuali.hr.time.earncode.EarnCode;
24  import org.kuali.hr.time.principal.PrincipalHRAttributes;
25  import org.kuali.hr.time.service.base.TkServiceLocator;
26  import org.kuali.rice.kim.api.identity.Person;
27  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
28  import org.kuali.rice.krad.util.ObjectUtils;
29  
30  import java.math.BigDecimal;
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  public class LeavePayout extends HrBusinessObject {
35  
36  	private static final long serialVersionUID = 1L;
37  
38  	private String lmLeavePayoutId;
39  	private String principalId;
40  	private String documentHeaderId;
41  	private String fromAccrualCategory;
42  	private String earnCode;
43  	private String description;
44  	private BigDecimal payoutAmount = new BigDecimal("0.0");
45      private BigDecimal forfeitedAmount = new BigDecimal("0.0");
46  	private Person principal;
47  	private AccrualCategory fromAccrualCategoryObj;
48  	private EarnCode earnCodeObj;
49  	private PrincipalHRAttributes principalHRAttrObj;
50      private String leaveCalendarDocumentId;
51      private String accrualCategoryRule;
52      private String forfeitedLeaveBlockId;
53      private String payoutFromLeaveBlockId;
54      private String payoutLeaveBlockId;
55  
56  	private String status;
57  
58  	public String getEarnCode() {
59  		return earnCode;
60  	}
61  	public void setEarnCode(String earnCode) {
62  		this.earnCode = earnCode;
63  	}
64  	public EarnCode getEarnCodeObj() {
65  		return TkServiceLocator.getEarnCodeService().getEarnCode(earnCode, getEffectiveDate());
66  	}
67  	public void setEarnCodeObj(EarnCode earnCodeObj) {
68  		this.earnCodeObj = earnCodeObj;
69  	}
70  	public String getPrincipalId() {
71  		return principalId;
72  	}
73  	public void setPrincipalId(String principalId) {
74  		this.principalId = principalId;
75  	}
76      public Person getPrincipal() {
77          return principal;
78      }
79      public void setPrincipal(Person principal) {
80          this.principal = principal;
81      }
82  	public String getName() {
83  		if (principal == null) {
84          principal = KimApiServiceLocator.getPersonService().getPerson(this.principalId);
85  		}
86  		return (principal != null) ? principal.getName() : "";
87  	}
88  	
89  	public String getLeavePlan() {
90  		if (!StringUtils.isEmpty(this.principalId)) {
91  			principalHRAttrObj = TkServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(principalId, this.getEffectiveDate());
92  		}
93  		return (principalHRAttrObj != null) ? principalHRAttrObj.getLeavePlan() : "";
94  	}
95  
96  	public void setLeavePlan(String leavePlan) {
97  	}
98  	public String getFromAccrualCategory() {
99  		return fromAccrualCategory;
100 	}
101 	public void setFromAccrualCategory(String accrualCategory) {
102 		this.fromAccrualCategory = accrualCategory;
103 	}
104 	
105 	public String getDescription() {
106 		return description;
107 	}
108 	public void setDescription(String description) {
109 		this.description = description;
110 	}
111 	public BigDecimal getPayoutAmount() {
112 		return payoutAmount;
113 	}
114 	public void setPayoutAmount(BigDecimal amount) {
115 		this.payoutAmount = amount;
116 	}
117     public BigDecimal getForfeitedAmount() {
118         return forfeitedAmount;
119     }
120     public void setForfeitedAmount(BigDecimal amount) {
121         this.forfeitedAmount = amount;
122     }
123 	public AccrualCategory getFromAccrualCategoryObj() {
124 		return TkServiceLocator.getAccrualCategoryService().getAccrualCategory(fromAccrualCategory, getEffectiveDate());
125 	}
126 	public void setFromAccrualCategoryObj(AccrualCategory accrualCategoryObj) {
127 		this.fromAccrualCategoryObj = accrualCategoryObj;
128 	}
129 	public static long getSerialversionuid() {
130 		return serialVersionUID;
131 	}
132 	public String getLmLeavePayoutId() {
133 		return lmLeavePayoutId;
134 	}
135 	public void setLmLeavePayoutId(String id) {
136 		this.lmLeavePayoutId = id;
137 	}
138 	
139 	@Override
140 	protected String getUniqueKey() {
141 		return getLmLeavePayoutId();
142 	}
143 	
144 	@Override
145 	public String getId() {
146 		return getLmLeavePayoutId();
147 	}
148 
149 	@Override
150 	public void setId(String id) {
151 		setLmLeavePayoutId(id);
152 	}
153 
154 	public PrincipalHRAttributes getPrincipalHRAttrObj() {
155 		return principalHRAttrObj;
156 	}
157 	public void setPrincipalHRAttrObj(PrincipalHRAttributes hrObj) {
158 		this.principalHRAttrObj = hrObj;
159 	}
160 	public String getAccrualCategoryRule() {
161 		return accrualCategoryRule;
162 	}
163 	public void setAccrualCategoryRule(String accrualCategoryRule) {
164 		this.accrualCategoryRule = accrualCategoryRule;
165 	}
166 	public String getForfeitedLeaveBlockId() {
167 		return forfeitedLeaveBlockId;
168 	}
169 	public void setForfeitedLeaveBlockId(String forfeitedLeaveBlockId) {
170 		this.forfeitedLeaveBlockId = forfeitedLeaveBlockId;
171 	}
172 	public String getPayoutLeaveBlockId() {
173 		return payoutLeaveBlockId;
174 	}
175 	public void setPayoutLeaveBlockId(String payoutLeaveBlockId) {
176 		this.payoutLeaveBlockId = payoutLeaveBlockId;
177 	}
178 	public String getPayoutFromLeaveBlockId() {
179 		return payoutFromLeaveBlockId;
180 	}
181 	public void setPayoutFromLeaveBlockId(String payoutFromLeaveBlockId) {
182 		this.payoutFromLeaveBlockId = payoutFromLeaveBlockId;
183 	}
184 
185 	public void setLeaveCalendarDocumentId(String leaveCalendarDocumentId) {
186 		this.leaveCalendarDocumentId = leaveCalendarDocumentId;
187 	}
188 
189     public List<LeaveBlock> getLeaveBlocks() {
190         List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
191 
192 		if (getForfeitedLeaveBlockId() != null) {
193 		    leaveBlocks.add(TkServiceLocator.getLeaveBlockService().getLeaveBlock(forfeitedLeaveBlockId));
194         }
195         if (getPayoutFromLeaveBlockId() != null) {
196 		    leaveBlocks.add(TkServiceLocator.getLeaveBlockService().getLeaveBlock(payoutFromLeaveBlockId));
197         }
198         if (getPayoutLeaveBlockId() != null) {
199 		    leaveBlocks.add(TkServiceLocator.getLeaveBlockService().getLeaveBlock(payoutLeaveBlockId));
200         }
201         return leaveBlocks;
202     }
203 
204     public String getLeaveCalendarDocumentId() {
205         return leaveCalendarDocumentId;
206     }
207 	public LeavePayout adjust(BigDecimal payoutAmount) {
208 		BigDecimal difference = this.payoutAmount.subtract(payoutAmount);
209 		//technically if there is forfeiture, then the transfer amount has already been maximized
210 		//via BalanceTransferService::initializeTransfer(...)
211 		//i.o.w. transfer amount cannot be increased.
212 		//this method is written with the intention of eventually allowing end user to adjust the transfer
213 		//amount as many times as they wish before submitting. Currently they cannot.
214 		if(difference.signum() < 0) {
215 			//transfer amount is being increased.
216 			if(forfeitedAmount.compareTo(BigDecimal.ZERO) > 0) {
217 				//transfer amount has already been maximized.
218 				if(forfeitedAmount.compareTo(difference.abs()) >= 0)
219 					// there is enough leave in the forfeited amount to take out the difference.
220 					forfeitedAmount = forfeitedAmount.subtract(difference.abs());
221 				else
222 					// the difference zero's the forfeited amount.
223 					forfeitedAmount = BigDecimal.ZERO;
224 			}
225 			// a forfeited amount equal to zero with an increase in the transfer amount
226 			// does not produce forfeiture.
227 			// forfeiture cannot be negative.
228 		}
229 		else if (difference.signum() > 0) {
230 			//transfer amount is being decreased
231 			forfeitedAmount = forfeitedAmount.add(difference);
232 		}
233 
234 		this.payoutAmount = payoutAmount;
235 		
236 		return this;
237 	}
238 	
239 	public String getStatus() {
240 		return status;
241 	}
242 	
243 	public void setStatus(String code) {
244 		status = code;
245 	}
246 	public String getDocumentHeaderId() {
247 		return documentHeaderId;
248 	}
249 	public void setDocumentHeaderId(String documentHeaderId) {
250 		this.documentHeaderId = documentHeaderId;
251 	}
252 }