001    /*
002     * Copyright 2011 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.kfs.gl.businessobject;
017    
018    import java.util.HashMap;
019    import java.util.Iterator;
020    import java.util.LinkedHashMap;
021    import java.util.List;
022    import java.util.Map;
023    
024    import org.apache.commons.lang.StringUtils;
025    import org.kuali.kfs.coa.businessobject.OrganizationReversionCategory;
026    import org.kuali.rice.kns.bo.PersistableBusinessObjectBase;
027    import org.kuali.rice.kns.util.KualiDecimal;
028    
029    /**
030     * This class represents a unit of work for the organization reversion
031     */
032    public class OrgReversionUnitOfWork extends PersistableBusinessObjectBase {
033        public String chartOfAccountsCode = "";
034        public String accountNumber = "";
035        public String subAccountNumber = "";
036        public Map<String, OrgReversionUnitOfWorkCategoryAmount> amounts;
037        private KualiDecimal totalReversion;
038        private KualiDecimal totalCarryForward;
039        private KualiDecimal totalAvailable;
040        private KualiDecimal totalCash;
041    
042        public OrgReversionUnitOfWork() {
043            amounts = new HashMap<String, OrgReversionUnitOfWorkCategoryAmount>();
044        }
045    
046        public OrgReversionUnitOfWork(String chart, String acct, String subAcct) {
047            this();
048            chartOfAccountsCode = chart;
049            accountNumber = acct;
050            subAccountNumber = subAcct;
051        }
052    
053        /**
054         * Returns true if COA code and account number are not blank.
055         * 
056         * @return true if COA code and account number are not blank.
057         */
058        public boolean isInitialized() {
059            return !StringUtils.isBlank(chartOfAccountsCode) && !StringUtils.isBlank(accountNumber);
060        }
061    
062        public void setFields(String chart, String acct, String subAcct) {
063            chartOfAccountsCode = chart;
064            accountNumber = acct;
065            subAccountNumber = subAcct;
066            cascadeCategoryAmountKeys();
067            clearAmounts();
068        }
069    
070        /**
071         * Set category amounts
072         * @param cats list of organization reversion categories
073         */
074        public void setCategories(List<OrganizationReversionCategory> cats) {
075            for (OrganizationReversionCategory element : cats) {
076                OrgReversionUnitOfWorkCategoryAmount ca = new OrgReversionUnitOfWorkCategoryAmount(element.getOrganizationReversionCategoryCode());
077                amounts.put(element.getOrganizationReversionCategoryCode(), ca);
078            }
079        }
080    
081        /**
082         * This method adds to the actual amount for a specific category code
083         * @param categoryCode category code
084         * @param amount amount
085         */
086        public void addActualAmount(String categoryCode, KualiDecimal amount) {
087            OrgReversionUnitOfWorkCategoryAmount ca = amounts.get(categoryCode);
088            ca.setActual(ca.getActual().add(amount));
089        }
090    
091        /**
092         * This method adds to the budget amount for a specific category code
093         * @param categoryCode category code
094         * @param amount amount
095         */
096        public void addBudgetAmount(String categoryCode, KualiDecimal amount) {
097            OrgReversionUnitOfWorkCategoryAmount ca = amounts.get(categoryCode);
098            ca.setBudget(ca.getBudget().add(amount));
099        }
100    
101        /**
102         * This method adds to the encumbrance amount for a specific category code
103         * @param categoryCode category code
104         * @param amount amount
105         */
106        public void addEncumbranceAmount(String categoryCode, KualiDecimal amount) {
107            OrgReversionUnitOfWorkCategoryAmount ca = amounts.get(categoryCode);
108            ca.setEncumbrance(ca.getEncumbrance().add(amount));
109        }
110    
111        /**
112         * This method adds to the carry forward amount for a specific category code
113         * @param categoryCode category code
114         * @param amount amount
115         */
116        public void addCarryForwardAmount(String categoryCode, KualiDecimal amount) {
117            OrgReversionUnitOfWorkCategoryAmount ca = amounts.get(categoryCode);
118            ca.setCarryForward(ca.getCarryForward().add(amount));
119        }
120    
121        /**
122         * This method clears all amounts for this unit of work
123         */
124        public void clearAmounts() {
125            totalAvailable = KualiDecimal.ZERO;
126            totalCarryForward = KualiDecimal.ZERO;
127            totalCash = KualiDecimal.ZERO;
128            totalReversion = KualiDecimal.ZERO;
129    
130            for (Iterator<OrgReversionUnitOfWorkCategoryAmount> iter = amounts.values().iterator(); iter.hasNext();) {
131                OrgReversionUnitOfWorkCategoryAmount element = iter.next();
132                element.setActual(KualiDecimal.ZERO);
133                element.setBudget(KualiDecimal.ZERO);
134                element.setEncumbrance(KualiDecimal.ZERO);
135            }
136        }
137    
138        /**
139         * This method updates the category amount keys for the current unit of work
140         */
141        public void cascadeCategoryAmountKeys() {
142            for (String category : amounts.keySet()) {
143                OrgReversionUnitOfWorkCategoryAmount catAmt = amounts.get(category);
144                catAmt.setChartOfAccountsCode(this.chartOfAccountsCode);
145                catAmt.setAccountNumber(this.accountNumber);
146                catAmt.setSubAccountNumber(this.subAccountNumber);
147            }
148        }
149    
150        /**
151         * This method returns true if this unit of work's chart of accounts code, account number, and sub account number match the passed in parameter values
152         * @param chart
153         * @param acct
154         * @param subAcct
155         * @return true if this unit of work's chart of accounts code, account number, and sub account number match the passed in parameter values
156         */
157        public boolean isSame(String chart, String acct, String subAcct) {
158            return (chartOfAccountsCode.equals(chart) && accountNumber.equals(acct) && subAccountNumber.equals(subAcct));
159        }
160    
161        /**
162         * Return true of this unit of work has the same chart of accounts code, account number, and sub account number as the passed in balance
163         * @param balance
164         * @return
165         */
166        public boolean wouldHold(Balance balance) {
167            return StringUtils.equals(chartOfAccountsCode, balance.getChartOfAccountsCode()) && StringUtils.equals(accountNumber, balance.getAccountNumber()) && StringUtils.equals(subAccountNumber, balance.getSubAccountNumber());
168        }
169    
170        public KualiDecimal getTotalAccountAvailable() {
171            KualiDecimal amount = KualiDecimal.ZERO;
172            for (Iterator<OrgReversionUnitOfWorkCategoryAmount> iter = amounts.values().iterator(); iter.hasNext();) {
173                OrgReversionUnitOfWorkCategoryAmount element = iter.next();
174                amount = amount.add(element.getAvailable());
175            }
176            return amount;
177        }
178    
179        public KualiDecimal getTotalCarryForward() {
180            return totalCarryForward;
181        }
182    
183        public void setTotalCarryForward(KualiDecimal totalCarryForward) {
184            this.totalCarryForward = totalCarryForward;
185        }
186    
187        public KualiDecimal getTotalReversion() {
188            return totalReversion;
189        }
190    
191        public void addTotalCarryForward(KualiDecimal amount) {
192            totalCarryForward = totalCarryForward.add(amount);
193        }
194    
195        public void setTotalReversion(KualiDecimal totalReversion) {
196            this.totalReversion = totalReversion;
197        }
198    
199        public void addTotalReversion(KualiDecimal amount) {
200            totalReversion = totalReversion.add(amount);
201        }
202    
203        public KualiDecimal getTotalAvailable() {
204            return totalAvailable;
205        }
206    
207        public void addTotalAvailable(KualiDecimal amount) {
208            totalAvailable = totalAvailable.add(amount);
209        }
210    
211        public void setTotalAvailable(KualiDecimal totalAvailable) {
212            this.totalAvailable = totalAvailable;
213        }
214    
215        public void addTotalCash(KualiDecimal amount) {
216            totalCash = totalCash.add(amount);
217        }
218    
219        public KualiDecimal getTotalCash() {
220            return totalCash;
221        }
222    
223        public void setTotalCash(KualiDecimal totalCash) {
224            this.totalCash = totalCash;
225        }
226    
227        public Map<String, OrgReversionUnitOfWorkCategoryAmount> getCategoryAmounts() {
228            return amounts;
229        }
230    
231        /**
232         * @see org.kuali.rice.kns.bo.BusinessObjectBase#toStringMapper()
233         */
234        @Override
235        public LinkedHashMap toStringMapper() {
236            LinkedHashMap pkMap = new LinkedHashMap();
237            pkMap.put("chartOfAccountsCode", this.chartOfAccountsCode);
238            pkMap.put("accountNbr", this.accountNumber);
239            pkMap.put("subAccountNbr", this.subAccountNumber);
240            return pkMap;
241        }
242    
243        /**
244         * Gets the accountNumber attribute.
245         * 
246         * @return Returns the accountNumber.
247         */
248        public String getAccountNumber() {
249            return accountNumber;
250        }
251    
252        /**
253         * Sets the accountNumber attribute value.
254         * 
255         * @param accountNumber The accountNumber to set.
256         */
257        public void setAccountNumber(String accountNumber) {
258            this.accountNumber = accountNumber;
259        }
260    
261        /**
262         * Gets the chartOfAccountsCode attribute.
263         * 
264         * @return Returns the chartOfAccountsCode.
265         */
266        public String getChartOfAccountsCode() {
267            return chartOfAccountsCode;
268        }
269    
270        /**
271         * Sets the chartOfAccountsCode attribute value.
272         * 
273         * @param chartOfAccountsCode The chartOfAccountsCode to set.
274         */
275        public void setChartOfAccountsCode(String chartOfAccountsCode) {
276            this.chartOfAccountsCode = chartOfAccountsCode;
277        }
278    
279        /**
280         * Gets the subAccountNumber attribute.
281         * 
282         * @return Returns the subAccountNumber.
283         */
284        public String getSubAccountNumber() {
285            return subAccountNumber;
286        }
287    
288        /**
289         * Sets the subAccountNumber attribute value.
290         * 
291         * @param subAccountNumber The subAccountNumber to set.
292         */
293        public void setSubAccountNumber(String subAccountNumber) {
294            this.subAccountNumber = subAccountNumber;
295        }
296    
297    }