001    /**
002     * Copyright 2004-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.hr.lm.leavepayout;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.hr.lm.accrual.AccrualCategory;
020    import org.kuali.hr.lm.accrual.AccrualCategoryRule;
021    import org.kuali.hr.lm.leaveblock.LeaveBlock;
022    import org.kuali.hr.time.HrBusinessObject;
023    import org.kuali.hr.time.earncode.EarnCode;
024    import org.kuali.hr.time.principal.PrincipalHRAttributes;
025    import org.kuali.hr.time.service.base.TkServiceLocator;
026    import org.kuali.rice.kim.api.identity.Person;
027    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
028    import org.kuali.rice.krad.util.ObjectUtils;
029    
030    import java.math.BigDecimal;
031    import java.util.ArrayList;
032    import java.util.List;
033    
034    public class LeavePayout extends HrBusinessObject {
035    
036            private static final long serialVersionUID = 1L;
037    
038            private String lmLeavePayoutId;
039            private String principalId;
040            private String documentHeaderId;
041            private String fromAccrualCategory;
042            private String earnCode;
043            private String description;
044            private BigDecimal payoutAmount = new BigDecimal("0.0");
045        private BigDecimal forfeitedAmount = new BigDecimal("0.0");
046            private transient Person principal;
047            private transient AccrualCategory fromAccrualCategoryObj;
048            private transient EarnCode earnCodeObj;
049            private transient PrincipalHRAttributes principalHRAttrObj;
050        private String leaveCalendarDocumentId;
051        private String accrualCategoryRule;
052        private String forfeitedLeaveBlockId;
053        private String payoutFromLeaveBlockId;
054        private String payoutLeaveBlockId;
055    
056            private String status;
057    
058            public String getEarnCode() {
059                    return earnCode;
060            }
061            public void setEarnCode(String earnCode) {
062                    this.earnCode = earnCode;
063            }
064            public EarnCode getEarnCodeObj() {
065                    if (earnCodeObj == null) {
066                earnCodeObj = TkServiceLocator.getEarnCodeService().getEarnCode(this.earnCode, getEffectiveDate());
067            }
068            return earnCodeObj;
069            }
070            public void setEarnCodeObj(EarnCode earnCodeObj) {
071                    this.earnCodeObj = earnCodeObj;
072            }
073            public String getPrincipalId() {
074                    return principalId;
075            }
076            public void setPrincipalId(String principalId) {
077                    this.principalId = principalId;
078            }
079        public Person getPrincipal() {
080            return principal;
081        }
082        public void setPrincipal(Person principal) {
083            this.principal = principal;
084        }
085            public String getName() {
086                    if (principal == null) {
087            principal = KimApiServiceLocator.getPersonService().getPerson(this.principalId);
088                    }
089                    return (principal != null) ? principal.getName() : "";
090            }
091            
092            public String getLeavePlan() {
093                    if (!StringUtils.isEmpty(this.principalId)) {
094                            principalHRAttrObj = TkServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(principalId, this.getEffectiveDate());
095                    }
096                    return (principalHRAttrObj != null) ? principalHRAttrObj.getLeavePlan() : "";
097            }
098    
099            public void setLeavePlan(String leavePlan) {
100            }
101            public String getFromAccrualCategory() {
102                    return fromAccrualCategory;
103            }
104            public void setFromAccrualCategory(String accrualCategory) {
105                    this.fromAccrualCategory = accrualCategory;
106            }
107            
108            public String getDescription() {
109                    return description;
110            }
111            public void setDescription(String description) {
112                    this.description = description;
113            }
114            public BigDecimal getPayoutAmount() {
115                    return payoutAmount;
116            }
117            public void setPayoutAmount(BigDecimal amount) {
118                    this.payoutAmount = amount;
119            }
120        public BigDecimal getForfeitedAmount() {
121            return forfeitedAmount;
122        }
123        public void setForfeitedAmount(BigDecimal amount) {
124            this.forfeitedAmount = amount;
125        }
126            public AccrualCategory getFromAccrualCategoryObj() {
127            if (fromAccrualCategoryObj == null) {
128                fromAccrualCategoryObj =  TkServiceLocator.getAccrualCategoryService().getAccrualCategory(fromAccrualCategory, getEffectiveDate());
129            }
130            return fromAccrualCategoryObj;
131            }
132            public void setFromAccrualCategoryObj(AccrualCategory accrualCategoryObj) {
133                    this.fromAccrualCategoryObj = accrualCategoryObj;
134            }
135            public static long getSerialversionuid() {
136                    return serialVersionUID;
137            }
138            public String getLmLeavePayoutId() {
139                    return lmLeavePayoutId;
140            }
141            public void setLmLeavePayoutId(String id) {
142                    this.lmLeavePayoutId = id;
143            }
144            
145            @Override
146            protected String getUniqueKey() {
147                    return getLmLeavePayoutId();
148            }
149            
150            @Override
151            public String getId() {
152                    return getLmLeavePayoutId();
153            }
154    
155            @Override
156            public void setId(String id) {
157                    setLmLeavePayoutId(id);
158            }
159    
160            public PrincipalHRAttributes getPrincipalHRAttrObj() {
161                    return principalHRAttrObj;
162            }
163            public void setPrincipalHRAttrObj(PrincipalHRAttributes hrObj) {
164                    this.principalHRAttrObj = hrObj;
165            }
166            public String getAccrualCategoryRule() {
167                    return accrualCategoryRule;
168            }
169            public void setAccrualCategoryRule(String accrualCategoryRule) {
170                    this.accrualCategoryRule = accrualCategoryRule;
171            }
172            public String getForfeitedLeaveBlockId() {
173                    return forfeitedLeaveBlockId;
174            }
175            public void setForfeitedLeaveBlockId(String forfeitedLeaveBlockId) {
176                    this.forfeitedLeaveBlockId = forfeitedLeaveBlockId;
177            }
178            public String getPayoutLeaveBlockId() {
179                    return payoutLeaveBlockId;
180            }
181            public void setPayoutLeaveBlockId(String payoutLeaveBlockId) {
182                    this.payoutLeaveBlockId = payoutLeaveBlockId;
183            }
184            public String getPayoutFromLeaveBlockId() {
185                    return payoutFromLeaveBlockId;
186            }
187            public void setPayoutFromLeaveBlockId(String payoutFromLeaveBlockId) {
188                    this.payoutFromLeaveBlockId = payoutFromLeaveBlockId;
189            }
190    
191            public void setLeaveCalendarDocumentId(String leaveCalendarDocumentId) {
192                    this.leaveCalendarDocumentId = leaveCalendarDocumentId;
193            }
194    
195        public List<LeaveBlock> getLeaveBlocks() {
196            List<LeaveBlock> leaveBlocks = new ArrayList<LeaveBlock>();
197    
198                    if (getForfeitedLeaveBlockId() != null) {
199                        leaveBlocks.add(TkServiceLocator.getLeaveBlockService().getLeaveBlock(forfeitedLeaveBlockId));
200            }
201            if (getPayoutFromLeaveBlockId() != null) {
202                        leaveBlocks.add(TkServiceLocator.getLeaveBlockService().getLeaveBlock(payoutFromLeaveBlockId));
203            }
204            if (getPayoutLeaveBlockId() != null) {
205                        leaveBlocks.add(TkServiceLocator.getLeaveBlockService().getLeaveBlock(payoutLeaveBlockId));
206            }
207            return leaveBlocks;
208        }
209    
210        public String getLeaveCalendarDocumentId() {
211            return leaveCalendarDocumentId;
212        }
213            public LeavePayout adjust(BigDecimal payoutAmount) {
214                    BigDecimal difference = this.payoutAmount.subtract(payoutAmount);
215                    //technically if there is forfeiture, then the transfer amount has already been maximized
216                    //via BalanceTransferService::initializeTransfer(...)
217                    //i.o.w. transfer amount cannot be increased.
218                    //this method is written with the intention of eventually allowing end user to adjust the transfer
219                    //amount as many times as they wish before submitting. Currently they cannot.
220                    if(difference.signum() < 0) {
221                            //transfer amount is being increased.
222                            if(forfeitedAmount.compareTo(BigDecimal.ZERO) > 0) {
223                                    //transfer amount has already been maximized.
224                                    if(forfeitedAmount.compareTo(difference.abs()) >= 0)
225                                            // there is enough leave in the forfeited amount to take out the difference.
226                                            forfeitedAmount = forfeitedAmount.subtract(difference.abs());
227                                    else
228                                            // the difference zero's the forfeited amount.
229                                            forfeitedAmount = BigDecimal.ZERO;
230                            }
231                            // a forfeited amount equal to zero with an increase in the transfer amount
232                            // does not produce forfeiture.
233                            // forfeiture cannot be negative.
234                    }
235                    else if (difference.signum() > 0) {
236                            //transfer amount is being decreased
237                            forfeitedAmount = forfeitedAmount.add(difference);
238                    }
239    
240                    this.payoutAmount = payoutAmount;
241                    
242                    return this;
243            }
244            
245            public String getStatus() {
246                    return status;
247            }
248            
249            public void setStatus(String code) {
250                    status = code;
251            }
252            public String getDocumentHeaderId() {
253                    return documentHeaderId;
254            }
255            public void setDocumentHeaderId(String documentHeaderId) {
256                    this.documentHeaderId = documentHeaderId;
257            }
258    }