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.balancetransfer;
17  
18  import java.math.BigDecimal;
19  import java.sql.Date;
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import org.kuali.hr.lm.accrual.AccrualCategory;
24  import org.kuali.hr.lm.accrual.AccrualCategoryRule;
25  import org.kuali.hr.lm.leaveblock.LeaveBlock;
26  import org.kuali.hr.time.HrBusinessObject;
27  import org.kuali.hr.time.service.base.TkServiceLocator;
28  import org.kuali.hr.time.util.TkConstants;
29  import org.kuali.rice.kim.api.identity.Person;
30  import org.kuali.rice.krad.util.ObjectUtils;
31  
32  public class BalanceTransfer extends HrBusinessObject {
33  
34  	private static final long serialVersionUID = 6948695780968441016L;
35  	
36  	private String balanceTransferId;
37  	private String documentHeaderId;
38  	private String accrualCategoryRule;
39  	private String principalId;
40  	private String toAccrualCategory;
41  	private String fromAccrualCategory;
42  	private BigDecimal transferAmount;
43  	private BigDecimal amountTransferred;
44  	private BigDecimal forfeitedAmount;
45  	private String leaveCalendarDocumentId;
46  
47  	private String status;
48  	private String forfeitedLeaveBlockId;
49  	private String accruedLeaveBlockId;
50  	private String debitedLeaveBlockId;
51  	private String sstoId;
52  	
53  	private Person principal;
54  	
55  	public String getPrincipalId() {
56  		return principalId;
57  	}
58  
59  	public void setPrincipalId(String principalId) {
60  		this.principalId = principalId;
61  	}
62  
63  	public String getToAccrualCategory() {
64  		return toAccrualCategory;
65  	}
66  
67  	public void setToAccrualCategory(String toAccrualCategory) {
68  		this.toAccrualCategory = toAccrualCategory;
69  	}
70  
71  	public String getFromAccrualCategory() {
72  		return fromAccrualCategory;
73  	}
74  
75  	public void setFromAccrualCategory(String fromAccrualCategory) {
76  		this.fromAccrualCategory = fromAccrualCategory;
77  	}
78  
79  	public BigDecimal getTransferAmount() {
80  		return transferAmount;
81  	}
82  
83  	public void setTransferAmount(BigDecimal transferAmount) {
84  		this.transferAmount = transferAmount;
85  	}
86  
87  	public BigDecimal getForfeitedAmount() {
88  		return forfeitedAmount;
89  	}
90  
91  	public void setForfeitedAmount(BigDecimal forfeitedAmount) {
92  		this.forfeitedAmount = forfeitedAmount;
93  	}
94  	
95  	public String getBalanceTransferId() {
96  		return balanceTransferId;
97  	}
98  
99  	public void setBalanceTransferId(String balanceTransferId) {
100 		this.balanceTransferId = balanceTransferId;
101 	}
102 	
103 	public String getAccrualCategoryRule() {
104 		return accrualCategoryRule;
105 	}
106 
107 	public void setAccrualCategoryRule(String accrualCategoryRule) {
108 		this.accrualCategoryRule = accrualCategoryRule;
109 	}
110 
111 	@Override
112 	protected String getUniqueKey() {
113 		return balanceTransferId;
114 	}
115 
116 	@Override
117 	public String getId() {
118 		return getBalanceTransferId();
119 	}
120 
121 	@Override
122 	public void setId(String id) {
123 		setBalanceTransferId(id);
124 	}
125 
126 	public Person getPrincipal() {
127 		return principal;
128 	}
129 
130 	public void setPrincipal(Person principal) {
131 		this.principal = principal;
132 	}
133 
134 	public AccrualCategory getCreditedAccrualCategory() {
135 		return TkServiceLocator.getAccrualCategoryService().getAccrualCategory(toAccrualCategory, super.getEffectiveDate());
136 	}
137 
138 	public AccrualCategory getDebitedAccrualCategory() {
139 		return TkServiceLocator.getAccrualCategoryService().getAccrualCategory(fromAccrualCategory, super.getEffectiveDate());
140 	}
141 
142 	public String getLeaveCalendarDocumentId() {
143 		return leaveCalendarDocumentId;
144 	}
145 
146 	public void setLeaveCalendarDocumentId(String leaveCalendarDocumentId) {
147 		this.leaveCalendarDocumentId = leaveCalendarDocumentId;
148 	}
149 
150 	/**
151 	 * Returns a balance transfer object adjusted for the new transfer amount.
152 	 * 
153 	 * "this" must be a default initialized balance transfer. i.e. transfer amount plus forfeited amount
154 	 * equal to the amount of leave in excess of the from accrual category's max balance for the given principal.
155 	 * 
156 	 * calling this method without first validating the supplied transfer amount via BalanceTransferValidationUtils may produce undesired results.
157 	 *
158 	 * @param transferAmount The desired transfer amount
159 	 * @return A balance transfer object with forfeited and amount transfer amounts adjusted to transferAmount
160 	 */
161 	public BalanceTransfer adjust(BigDecimal transferAmount) {
162 		BigDecimal difference = this.transferAmount.subtract(transferAmount);
163 		AccrualCategoryRule aRule = TkServiceLocator.getAccrualCategoryRuleService().getAccrualCategoryRule(accrualCategoryRule);
164 		//technically if there is forfeiture, then the transfer amount has already been maximized
165 		//via BalanceTransferService::initializeTransfer(...)
166 		//i.o.w. transfer amount cannot be increased.
167 		//this method is written with the intention of eventually allowing end user to adjust the transfer
168 		//amount as many times as they wish before submitting. Currently they cannot.
169 		if(difference.signum() < 0) {
170 			//transfer amount is being increased.
171 			if(forfeitedAmount.compareTo(BigDecimal.ZERO) > 0) {
172 				//transfer amount has already been maximized.
173 				if(forfeitedAmount.compareTo(difference.abs()) >= 0)
174 					// there is enough leave in the forfeited amount to take out the difference.
175 					forfeitedAmount = forfeitedAmount.subtract(difference.abs());
176 				else
177 					// the difference zero's the forfeited amount.
178 					forfeitedAmount = BigDecimal.ZERO;
179 			}
180 			// a forfeited amount equal to zero with an increase in the transfer amount
181 			// does not produce forfeiture.
182 			// forfeiture cannot be negative.
183 		}
184 		else if (difference.signum() > 0) {
185 			//transfer amount is being decreased
186 			forfeitedAmount = forfeitedAmount.add(difference);
187 		}
188 
189 		this.transferAmount = transferAmount;
190 
191 		if(ObjectUtils.isNotNull(aRule.getMaxBalanceTransferConversionFactor()))
192 			this.amountTransferred = transferAmount.multiply(aRule.getMaxBalanceTransferConversionFactor()).setScale(2);
193 		else
194 			this.amountTransferred = transferAmount;
195 		
196 		return this;
197 	}
198 
199 	public List<LeaveBlock> getLeaveBlocks() {
200 		List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
201 		if (getForfeitedLeaveBlockId() != null) {
202 		    leaveBlocks.add(TkServiceLocator.getLeaveBlockService().getLeaveBlock(forfeitedLeaveBlockId));
203         }
204         if (getAccruedLeaveBlockId() != null) {
205 		    leaveBlocks.add(TkServiceLocator.getLeaveBlockService().getLeaveBlock(accruedLeaveBlockId));
206         }
207         if (getDebitedLeaveBlockId() != null) {
208 		    leaveBlocks.add(TkServiceLocator.getLeaveBlockService().getLeaveBlock(debitedLeaveBlockId));
209         }
210 
211 		return leaveBlocks;
212 	}
213 
214 	public String getStatus() {
215 		return status;
216 	}
217 
218 	public void setStatus(String status) {
219 		this.status = status;
220 	}
221 
222 	public void disapprove() {
223 		TkServiceLocator.getLeaveBlockService().updateLeaveBlock(null, principalId);
224 		setStatus(TkConstants.ROUTE_STATUS.DISAPPROVED);
225 	}
226 
227 	public void approve() {
228 
229 		setStatus(TkConstants.ROUTE_STATUS.FINAL);
230 	}
231 
232 	public void cancel() {
233 
234 		setStatus(TkConstants.ROUTE_STATUS.CANCEL);
235 	}
236 
237 	public String getAccruedLeaveBlockId() {
238 		return accruedLeaveBlockId;
239 	}
240 
241 	public void setAccruedLeaveBlockId(String accruedLeaveBlockId) {
242 		this.accruedLeaveBlockId = accruedLeaveBlockId;
243 	}
244 
245 	public String getForfeitedLeaveBlockId() {
246 		return forfeitedLeaveBlockId;
247 	}
248 
249 	public void setForfeitedLeaveBlockId(String forfeitedLeaveBlockId) {
250 		this.forfeitedLeaveBlockId = forfeitedLeaveBlockId;
251 	}
252 
253 	public String getDebitedLeaveBlockId() {
254 		return debitedLeaveBlockId;
255 	}
256 
257 	public void setDebitedLeaveBlockId(String debitedLeaveBlockId) {
258 		this.debitedLeaveBlockId = debitedLeaveBlockId;
259 	}
260 
261 	public BigDecimal getAmountTransferred() {
262 		return amountTransferred;
263 	}
264 
265 	public void setAmountTransferred(BigDecimal amountTransferrerd) {
266 		this.amountTransferred = amountTransferrerd;
267 	}
268 	
269 	public String getSstoId() {
270 		return sstoId;
271 	}
272 
273 	public void setSstoId(String sstoId) {
274 		this.sstoId = sstoId;
275 	}
276 
277 	public String getDocumentHeaderId() {
278 		return documentHeaderId;
279 	}
280 
281 	public void setDocumentHeaderId(String documentHeaderId) {
282 		this.documentHeaderId = documentHeaderId;
283 	}
284 	
285 	//Comparable for order handling of more than one transfer occurring during the same
286 	//action frequency interval?
287 	
288 }