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.report;
017    
018    import java.util.Iterator;
019    
020    import org.kuali.kfs.gl.businessobject.Transaction;
021    import org.kuali.kfs.sys.KFSConstants;
022    import org.kuali.kfs.sys.service.ReportWriterService;
023    import org.kuali.rice.kns.util.KualiDecimal;
024    
025    /**
026     * This class prints out a transaction listing report. This is different from a transaction report in that this lists all the
027     * transactions and a total amount. The transaction report shows the primary key from transactions and a list of messages for each
028     * one.
029     */
030    public class TransactionListingReport {
031        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(TransactionListingReport.class);
032    
033        protected int transactionCount;
034        protected KualiDecimal debitTotal;
035        protected KualiDecimal creditTotal;
036        protected KualiDecimal budgetTotal;
037        
038        public TransactionListingReport() {
039            super();
040            
041            transactionCount = 0;
042            debitTotal = KualiDecimal.ZERO;
043            creditTotal = KualiDecimal.ZERO;
044            budgetTotal = KualiDecimal.ZERO;
045        }
046        
047        /**
048         * This will write a transaction to the report. It collects data in order for this to be a listing, hence it is the developers responsibility
049         * to call generateStatistics after printing the listing.
050         * 
051         * @param reportWriterService destination report
052         * @param transaction Transaction to be printed
053         */
054        public void generateReport(ReportWriterService reportWriterService, Transaction transaction) {
055            LOG.debug("generateReport() started");
056    
057            if (transaction != null) {
058                if (transactionCount == 0) {
059                    reportWriterService.writeTableHeader(transaction);
060                }
061                
062                reportWriterService.writeTableRow(transaction);
063                
064                if (KFSConstants.GL_DEBIT_CODE.equals(transaction.getTransactionDebitCreditCode())) {
065                    debitTotal = debitTotal.add(transaction.getTransactionLedgerEntryAmount());
066                }
067                if (KFSConstants.GL_CREDIT_CODE.equals(transaction.getTransactionDebitCreditCode())) {
068                    creditTotal = creditTotal.add(transaction.getTransactionLedgerEntryAmount());
069                }
070                if (!KFSConstants.GL_CREDIT_CODE.equals(transaction.getTransactionDebitCreditCode()) && !KFSConstants.GL_DEBIT_CODE.equals(transaction.getTransactionDebitCreditCode())) {
071                    budgetTotal = budgetTotal.add(transaction.getTransactionLedgerEntryAmount());
072                }
073                transactionCount++;
074            }
075        }
076        
077        /**
078         * Writes the statistics to the report that were collected by this class
079         * 
080         * @param reportWriterService destination report
081         */
082        public void generateStatistics(ReportWriterService reportWriterService) {
083            LOG.debug("generateStatistics() started");
084            
085            reportWriterService.writeStatisticLine("Total Transactions                %,9d", transactionCount);
086            reportWriterService.writeStatisticLine("Total Debit Amount                %,9.2f", debitTotal.doubleValue());
087            reportWriterService.writeStatisticLine("Total Credit Amount               %,9.2f", creditTotal.doubleValue());
088            reportWriterService.writeStatisticLine("Total Budget Amount               %,9.2f", budgetTotal.doubleValue());
089        }
090        
091        /**
092         * This will generate a report on the transactions passed to it
093         * 
094         * @param reportWriterService destination report
095         * @param transactions Transactions sorted properly
096         */
097        public void generateReport(ReportWriterService reportWriterService, Iterator<? extends Transaction> transactions) {
098            LOG.debug("generateReport() started");
099    
100            if (transactions != null) {
101                while (transactions.hasNext()) {
102                    Transaction tran = (Transaction) transactions.next();
103    
104                    this.generateReport(reportWriterService, tran);
105                }
106            }
107    
108            this.generateStatistics(reportWriterService);
109        }
110    }