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.service.impl;
017    
018    import java.util.Date;
019    import java.util.Iterator;
020    
021    import org.kuali.kfs.coa.service.AccountingPeriodService;
022    import org.kuali.kfs.gl.businessobject.LedgerEntryForReporting;
023    import org.kuali.kfs.gl.businessobject.LedgerEntryHolder;
024    import org.kuali.kfs.gl.businessobject.Reversal;
025    import org.kuali.kfs.gl.businessobject.Transaction;
026    import org.kuali.kfs.gl.dataaccess.ReversalDao;
027    import org.kuali.kfs.gl.service.ReversalService;
028    import org.kuali.kfs.sys.KFSConstants;
029    import org.kuali.kfs.sys.service.UniversityDateService;
030    import org.springframework.transaction.annotation.Transactional;
031    
032    /**
033     * This transactional class provides the default implementation of the services required in ReversalService
034     * 
035     * @see org.kuali.kfs.gl.service.ReversalService
036     */
037    @Transactional
038    public class ReversalServiceImpl implements ReversalService {
039        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ReversalServiceImpl.class);
040    
041        private ReversalDao reversalDao;
042        private AccountingPeriodService accountingPeriodService;
043        private UniversityDateService universityDateService;
044    
045        /**
046         * Deletes a reversal record
047         * 
048         * @param re the reversal to delete, remove, or otherwise expiate from the database
049         * @see org.kuali.kfs.gl.service.ReversalService#delete(org.kuali.kfs.gl.businessobject.Reversal)
050         */
051        public void delete(Reversal re) {
052            LOG.debug("delete() started");
053    
054            reversalDao.delete(re);
055        }
056    
057        /**
058         * Returns all the reversal records set to reverse on or before the given date
059         * 
060         * @param before the date to find reversals on or before
061         * @see org.kuali.kfs.gl.service.ReversalService#getByDate(java.util.Date)
062         */
063        public Iterator getByDate(Date before) {
064            LOG.debug("getByDate() started");
065    
066            return reversalDao.getByDate(before);
067        }
068    
069        public Reversal getByTransaction(Transaction t) {
070            LOG.debug("getByTransaction() started");
071    
072            return reversalDao.getByTransaction(t);
073        }
074    
075        /**
076         * Summarizes all of the reversal records set to reverse before or on the given date
077         * @param before the date reversals summarized should be on or before
078         * @return a LedgerEntryHolder with a summary of
079         * @see org.kuali.kfs.gl.service.ReversalService#getSummaryByDate(java.util.Date)
080         */
081        public LedgerEntryHolder getSummaryByDate(Date before) {
082            LOG.debug("getSummaryByDate() started");
083    
084            LedgerEntryHolder ledgerEntryHolder = new LedgerEntryHolder();
085    
086            Iterator reversalsIterator = reversalDao.getByDate(before);
087            while (reversalsIterator.hasNext()) {
088                Reversal reversal = (Reversal) reversalsIterator.next();
089                LedgerEntryForReporting ledgerEntry = buildLedgerEntryFromReversal(reversal);
090                ledgerEntryHolder.insertLedgerEntry(ledgerEntry, true);
091            }
092            return ledgerEntryHolder;
093        }
094    
095        /**
096         * Creates a LedgerEntry from a reversal, which is proper for summarization (ie, its fiscal year and period code are based off
097         * the reversal date, not off the transaction date or the reversal's current fiscal year and accounting period)
098         * 
099         * @param reversal reversal to build LedgerEntry with
100         * @return a new LedgerEntry, populated by the reversal
101         */
102        protected LedgerEntryForReporting buildLedgerEntryFromReversal(Reversal reversal) {
103            LedgerEntryForReporting entry = new LedgerEntryForReporting(universityDateService.getFiscalYear(reversal.getFinancialDocumentReversalDate()), accountingPeriodService.getByDate(reversal.getFinancialDocumentReversalDate()).getUniversityFiscalPeriodCode(), reversal.getFinancialBalanceTypeCode(), reversal.getFinancialSystemOriginationCode());
104            if (KFSConstants.GL_CREDIT_CODE.equals(reversal.getTransactionDebitCreditCode())) {
105                entry.setCreditAmount(reversal.getTransactionLedgerEntryAmount());
106                entry.setCreditCount(1);
107            }
108            else if (KFSConstants.GL_DEBIT_CODE.equals(reversal.getTransactionDebitCreditCode())) {
109                entry.setDebitAmount(reversal.getTransactionLedgerEntryAmount());
110                entry.setDebitCount(1);
111            }
112            else {
113                entry.setNoDCAmount(reversal.getTransactionLedgerEntryAmount());
114                entry.setNoDCCount(1);
115            }
116            entry.setRecordCount(1);
117            return entry;
118        }
119    
120        /**
121         * Saves a reversal record
122         * 
123         * @param re the reversal to save
124         * @see org.kuali.kfs.gl.service.ReversalService#save(org.kuali.kfs.gl.businessobject.Reversal)
125         */
126        public void save(Reversal re) {
127            LOG.debug("save() started");
128    
129            reversalDao.save(re);
130        }
131    
132        /**
133         * Sets the reversalDao attribute, allowing injection of an implementation of that data access object
134         * 
135         * @param reversalDao the reversalDao implementation to set
136         * @see org.kuali.kfs.gl.dataaccess.ReversalDao
137         */
138        public void setReversalDao(ReversalDao reversalDao) {
139            this.reversalDao = reversalDao;
140        }
141    
142        /**
143         * Sets the accountingPeriodService attribute, allowing injection of an implementation of that service
144         * 
145         * @param accountingPeriodService the accountingPeriodService implementation to set
146         * @see org.kuali.kfs.coa.service.AccountingPeriodService
147         */
148        public void setAccountingPeriodService(AccountingPeriodService accountingPeriodService) {
149            this.accountingPeriodService = accountingPeriodService;
150        }
151    
152        /**
153         * Sets the unversityDateService attribute, allowing injection of an implementation of that service
154         * 
155         * @param universityDateService the universityDateService implementation to set
156         * @see org.kuali.kfs.sys.service.UniversityDateService
157         */
158        public void setUniversityDateService(UniversityDateService universityDateService) {
159            this.universityDateService = universityDateService;
160        }
161    }