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.module.ld.service.impl;
017    
018    import java.util.Collection;
019    import java.util.Iterator;
020    import java.util.List;
021    import java.util.Map;
022    import java.util.Set;
023    
024    import org.apache.commons.collections.IteratorUtils;
025    import org.kuali.kfs.gl.OJBUtility;
026    import org.kuali.kfs.module.ld.businessobject.EmployeeFunding;
027    import org.kuali.kfs.module.ld.businessobject.LaborBalanceSummary;
028    import org.kuali.kfs.module.ld.businessobject.LaborTransaction;
029    import org.kuali.kfs.module.ld.businessobject.LedgerBalance;
030    import org.kuali.kfs.module.ld.businessobject.LedgerBalanceForYearEndBalanceForward;
031    import org.kuali.kfs.module.ld.dataaccess.LaborLedgerBalanceDao;
032    import org.kuali.kfs.module.ld.service.LaborCalculatedSalaryFoundationTrackerService;
033    import org.kuali.kfs.module.ld.service.LaborLedgerBalanceService;
034    import org.kuali.kfs.module.ld.util.DebitCreditUtil;
035    import org.kuali.kfs.sys.ObjectUtil;
036    import org.kuali.kfs.sys.service.NonTransactional;
037    import org.kuali.rice.kns.util.KualiDecimal;
038    import org.springframework.transaction.annotation.Transactional;
039    
040    public class LaborLedgerBalanceServiceImpl implements LaborLedgerBalanceService {
041        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LaborLedgerBalanceServiceImpl.class);
042    
043        private LaborLedgerBalanceDao laborLedgerBalanceDao;
044        private LaborCalculatedSalaryFoundationTrackerService laborCalculatedSalaryFoundationTrackerService;
045    
046        /**
047         * @see org.kuali.kfs.module.ld.service.LaborLedgerBalanceService#findBalancesForFiscalYear(java.lang.Integer)
048         */
049        @NonTransactional
050        public Iterator<LedgerBalance> findBalancesForFiscalYear(Integer fiscalYear) {
051            return laborLedgerBalanceDao.findBalancesForFiscalYear(fiscalYear);
052        }
053    
054        /**
055         * @see org.kuali.kfs.module.ld.service.LaborLedgerBalanceService#findBalancesForFiscalYear(java.lang.Integer, java.util.Map)
056         */
057        @NonTransactional
058        public Iterator<LedgerBalance> findBalancesForFiscalYear(Integer fiscalYear, Map<String, String> fieldValues) {
059            return laborLedgerBalanceDao.findBalancesForFiscalYear(fiscalYear, fieldValues);
060        }
061    
062        /**
063         * @see org.kuali.kfs.module.ld.service.LaborLedgerBalanceService#findBalance(java.util.Map, boolean)
064         */
065        @NonTransactional
066        public Iterator findBalance(Map fieldValues, boolean isConsolidated) {
067            LOG.debug("findBalance() started");
068            return laborLedgerBalanceDao.findBalance(fieldValues, isConsolidated);
069        }
070    
071        /**
072         * @see org.kuali.kfs.module.ld.service.LaborLedgerBalanceService#getBalanceRecordCount(java.util.Map, boolean)
073         */
074        @NonTransactional
075        public Integer getBalanceRecordCount(Map fieldValues, boolean isConsolidated) {
076            LOG.debug("getBalanceRecordCount() started");
077    
078            Integer recordCount = null;
079            if (!isConsolidated) {
080                recordCount = OJBUtility.getResultSizeFromMap(fieldValues, new LedgerBalance()).intValue();
081            }
082            else {
083                Iterator recordCountIterator = laborLedgerBalanceDao.getConsolidatedBalanceRecordCount(fieldValues);
084                List recordCountList = IteratorUtils.toList(recordCountIterator);
085                recordCount = recordCountList.size();
086            }
087            return recordCount;
088        }
089    
090        /**
091         * @see org.kuali.kfs.module.ld.service.LaborLedgerBalanceService#findLedgerBalance(java.util.Collection,
092         *      org.kuali.kfs.module.ld.businessobject.LaborTransaction)
093         */
094        @NonTransactional
095        public <T extends LedgerBalance> T findLedgerBalance(Collection<T> ledgerBalanceCollection, LaborTransaction transaction, List<String> keyList) {
096            for (T ledgerBalance : ledgerBalanceCollection) {
097                boolean found = ObjectUtil.equals(ledgerBalance, transaction, keyList);
098                if (found) {
099                    return ledgerBalance;
100                }
101            }
102            return null;
103        }
104    
105        /**
106         * @see org.kuali.kfs.module.ld.service.LaborLedgerBalanceService#findLedgerBalance(java.util.Collection,
107         *      org.kuali.kfs.module.ld.businessobject.LaborTransaction)
108         */
109        @NonTransactional
110        public <T extends LedgerBalance> T findLedgerBalance(Collection<T> ledgerBalanceCollection, LaborTransaction transaction) {
111            for (T ledgerBalance : ledgerBalanceCollection) {
112                boolean found = ObjectUtil.equals(ledgerBalance, transaction, ledgerBalance.getPrimaryKeyList());
113                if (found) {
114                    return ledgerBalance;
115                }
116            }
117            return null;
118        }
119    
120        /**
121         * @see org.kuali.kfs.module.ld.service.LaborLedgerBalanceService#updateLedgerBalance(org.kuali.kfs.module.ld.businessobject.LedgerBalance,
122         *      org.kuali.kfs.module.ld.businessobject.LaborTransaction)
123         */
124        @Transactional
125        public <T extends LedgerBalance> void updateLedgerBalance(T ledgerBalance, LaborTransaction transaction) {
126            String debitCreditCode = transaction.getTransactionDebitCreditCode();
127            KualiDecimal amount = transaction.getTransactionLedgerEntryAmount();
128            amount = DebitCreditUtil.getNumericAmount(amount, debitCreditCode);
129            ledgerBalance.addAmount(transaction.getUniversityFiscalPeriodCode(), amount);
130        }
131    
132        /**
133         * @see org.kuali.kfs.module.ld.service.LaborLedgerBalanceService#addLedgerBalance(java.util.Collection,
134         *      org.kuali.kfs.module.ld.businessobject.LaborTransaction)
135         */
136        @Transactional
137        public LedgerBalance addLedgerBalance(Collection<LedgerBalance> ledgerBalanceCollection, LaborTransaction transaction) {
138            LedgerBalance ledgerBalance = this.findLedgerBalance(ledgerBalanceCollection, transaction);
139    
140            if (ledgerBalance == null) {
141                LedgerBalance newLedgerBalance = new LedgerBalance();
142                ObjectUtil.buildObject(newLedgerBalance, transaction);
143                updateLedgerBalance(newLedgerBalance, transaction);
144    
145                ledgerBalanceCollection.add(newLedgerBalance);
146                return newLedgerBalance;
147            }
148            return null;
149        }
150    
151        /**
152         * @see org.kuali.kfs.module.ld.service.LaborLedgerBalanceService#findEmployeeFunding(java.util.Map)
153         */
154        @NonTransactional
155        public List<EmployeeFunding> findEmployeeFunding(Map fieldValues, boolean isConsolidated) {
156            List<EmployeeFunding> currentFundsCollection = laborLedgerBalanceDao.findCurrentEmployeeFunds(fieldValues);
157            List<EmployeeFunding> encumbranceFundsCollection = laborLedgerBalanceDao.findEncumbranceEmployeeFunds(fieldValues);
158    
159            // merge encumberance with the current funds
160            for (EmployeeFunding encumbranceFunding : encumbranceFundsCollection) {
161                KualiDecimal encumbrance = encumbranceFunding.getAccountLineAnnualBalanceAmount().add(encumbranceFunding.getContractsGrantsBeginningBalanceAmount());
162                encumbranceFunding.setOutstandingEncumbrance(encumbrance);
163    
164                if (currentFundsCollection.contains(encumbranceFunding)) {
165                    int index = currentFundsCollection.indexOf(encumbranceFunding);
166                    currentFundsCollection.get(index).setOutstandingEncumbrance(encumbrance);
167                }
168                else if(encumbrance != null && encumbrance.isNonZero()){
169                    currentFundsCollection.add(encumbranceFunding);
170                }
171            }
172    
173            // update the employee fundings
174            for (EmployeeFunding employeeFunding : currentFundsCollection) {
175                employeeFunding.setCurrentAmount(employeeFunding.getAccountLineAnnualBalanceAmount());
176            }
177            return currentFundsCollection;
178        }
179    
180        /**
181         * @see org.kuali.kfs.module.ld.service.LaborLedgerBalanceService#findEmployeeFundingWithCSFTracker(java.util.Map)
182         */
183        @NonTransactional
184        public List<EmployeeFunding> findEmployeeFundingWithCSFTracker(Map fieldValues, boolean isConsolidated) {
185            List<EmployeeFunding> currentFundsCollection = this.findEmployeeFunding(fieldValues, isConsolidated);
186            List<EmployeeFunding> CSFTrackersCollection = laborCalculatedSalaryFoundationTrackerService.findCSFTrackersAsEmployeeFunding(fieldValues, isConsolidated);
187    
188            for (EmployeeFunding CSFTrackerAsEmployeeFunding : CSFTrackersCollection) {
189                if (currentFundsCollection.contains(CSFTrackerAsEmployeeFunding)) {
190                    int index = currentFundsCollection.indexOf(CSFTrackerAsEmployeeFunding);
191                    EmployeeFunding currentFunds = currentFundsCollection.get(index);
192    
193                    currentFunds.setCsfDeleteCode(CSFTrackerAsEmployeeFunding.getCsfDeleteCode());
194                    currentFunds.setCsfTimePercent(CSFTrackerAsEmployeeFunding.getCsfTimePercent());
195                    currentFunds.setCsfFundingStatusCode(CSFTrackerAsEmployeeFunding.getCsfFundingStatusCode());
196                    currentFunds.setCsfAmount(CSFTrackerAsEmployeeFunding.getCsfAmount());
197                    currentFunds.setCsfFullTimeEmploymentQuantity(CSFTrackerAsEmployeeFunding.getCsfFullTimeEmploymentQuantity());
198                }
199            }
200    
201            return currentFundsCollection;
202        }
203    
204        /**
205         * @see org.kuali.kfs.module.ld.service.LaborLedgerBalanceService#findBalanceSummary(java.lang.Integer, java.util.Collection)
206         */
207        @NonTransactional
208        public List<LaborBalanceSummary> findBalanceSummary(Integer fiscalYear, Collection<String> balanceTypes) {
209            return laborLedgerBalanceDao.findBalanceSummary(fiscalYear, balanceTypes);
210        }
211    
212        /**
213         * @see org.kuali.kfs.module.ld.service.LaborLedgerBalanceService#save(org.kuali.kfs.module.ld.businessobject.LedgerBalance)
214         */
215        @Transactional
216        public void save(LedgerBalance ledgerBalance) {
217            laborLedgerBalanceDao.save(ledgerBalance);
218        }
219    
220        /**
221         * @see org.kuali.kfs.module.ld.service.LaborLedgerBalanceService#findBalancesForFiscalYear(java.lang.Integer, java.util.Map,
222         *      java.util.List, java.util.List)
223         */
224        @NonTransactional
225        public Iterator<LedgerBalanceForYearEndBalanceForward> findBalancesForFiscalYear(Integer fiscalYear, Map<String, String> fieldValues, List<String> subFundGroupCodes, List<String> fundGroupCodes) {
226            return laborLedgerBalanceDao.findBalancesForFiscalYear(fiscalYear, fieldValues, subFundGroupCodes, fundGroupCodes);
227        }
228    
229        /**
230         * @see org.kuali.kfs.module.ld.service.LaborLedgerBalanceService#findAccountsInFundGroups(java.lang.Integer, java.util.Map,
231         *      java.util.List, java.util.List)
232         */
233        @NonTransactional
234        public List<List<String>> findAccountsInFundGroups(Integer fiscalYear, Map<String, String> fieldValues, List<String> subFundGroupCodes, List<String> fundGroupCodes) {
235            return laborLedgerBalanceDao.findAccountsInFundGroups(fiscalYear, fieldValues, subFundGroupCodes, fundGroupCodes);
236        }
237    
238        /**
239         * @see org.kuali.kfs.module.ld.service.LaborLedgerBalanceService#findLedgerBalances(java.util.Map, java.util.Map, java.util.Set,
240         *      java.util.List, java.util.List)
241         */
242        @NonTransactional
243        public Collection<LedgerBalance> findLedgerBalances(Map<String, List<String>> fieldValues, Map<String, List<String>> excludedFieldValues, Set<Integer> fiscalYears, List<String> balanceTypeList, List<String> positionObjectGroupCodes) {
244            return laborLedgerBalanceDao.findLedgerBalances(fieldValues, excludedFieldValues, fiscalYears, balanceTypeList, positionObjectGroupCodes);
245        }
246        
247        /**
248         * @see org.kuali.kfs.module.ld.service.LaborLedgerBalanceService#deleteLedgerBalancesPriorToYear(java.lang.Integer, java.lang.String)
249         */
250        @Transactional
251        public void deleteLedgerBalancesPriorToYear(Integer fiscalYear, String chartOfAccountsCode) {
252            laborLedgerBalanceDao.deleteLedgerBalancesPriorToYear(fiscalYear, chartOfAccountsCode);
253        }
254    
255        /**
256         * Sets the laborLedgerBalanceDao attribute value.
257         * 
258         * @param laborLedgerBalanceDao The laborLedgerBalanceDao to set.
259         */
260        @NonTransactional
261        public void setLaborLedgerBalanceDao(LaborLedgerBalanceDao laborLedgerBalanceDao) {
262            this.laborLedgerBalanceDao = laborLedgerBalanceDao;
263        }
264    
265        /**
266         * Sets the laborCalculatedSalaryFoundationTrackerService attribute value.
267         * 
268         * @param laborCalculatedSalaryFoundationTrackerService The laborCalculatedSalaryFoundationTrackerService to set.
269         */
270        @NonTransactional
271        public void setLaborCalculatedSalaryFoundationTrackerService(LaborCalculatedSalaryFoundationTrackerService laborCalculatedSalaryFoundationTrackerService) {
272            this.laborCalculatedSalaryFoundationTrackerService = laborCalculatedSalaryFoundationTrackerService;
273        }
274    }
275