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.endow.report.util;
017    
018    import java.awt.Color;
019    import java.io.ByteArrayOutputStream;
020    import java.math.BigDecimal;
021    import java.util.Date;
022    import java.util.List;
023    
024    import org.kuali.kfs.module.endow.document.service.KEMService;
025    import org.kuali.kfs.sys.context.SpringContext;
026    import org.kuali.rice.kns.service.DateTimeService;
027    
028    import com.lowagie.text.Document;
029    import com.lowagie.text.Element;
030    import com.lowagie.text.Font;
031    import com.lowagie.text.HeaderFooter;
032    import com.lowagie.text.Paragraph;
033    import com.lowagie.text.Phrase;
034    import com.lowagie.text.Rectangle;
035    import com.lowagie.text.pdf.PdfPCell;
036    import com.lowagie.text.pdf.PdfPTable;
037    import com.lowagie.text.pdf.PdfWriter;
038    
039    /**
040     * Helper to print the report in PDF
041     */
042    public class TrialBalanceReportPrint extends EndowmentReportPrintBase {
043    
044        private final String ZERO_FOR_REPORT = "0.00";
045            
046        /**
047         * Generates the report in PDF using iText
048         * 
049         * @param reportRequestHeaderDataHolder
050         * @param trialBalanceDataReportHolders
051         * @return ByteArrayOutputStream
052         */
053        public ByteArrayOutputStream printTrialBalanceReport(EndowmentReportHeaderDataHolder reportRequestHeaderDataHolder, List<TrialBalanceReportDataHolder> trialBalanceDataReportHolders, String listKemidsInHeader) {
054            
055            final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(TrialBalanceReportPrint.class);
056                    
057            Document document = new Document();
058            document.setPageSize(LETTER_PORTRAIT);
059            document.addTitle("Endowment Trial Balance");
060            
061            // get the stream for PDF
062            ByteArrayOutputStream pdfStream = new ByteArrayOutputStream();
063    
064            try {            
065                PdfWriter.getInstance(document, pdfStream);
066                document.open();
067                
068                // page
069                HeaderFooter header = new HeaderFooter(new Phrase(new Date().toString() + "     Page: ", headerFont), true);
070                header.setBorder(Rectangle.NO_BORDER);
071                header.setAlignment(Element.ALIGN_RIGHT);
072                header.setPageNumber(0);
073                document.setHeader(header);
074    
075                // print the report header
076                if (printReportHeaderPage(reportRequestHeaderDataHolder, document, listKemidsInHeader)) {
077                        
078                    if (trialBalanceDataReportHolders != null && trialBalanceDataReportHolders.size() > 0) {   
079                        document.setPageSize(LETTER_LANDSCAPE);                    
080                        document.resetPageCount();
081                        header.setPageNumber(1);
082                        document.newPage();                    
083                        printTrialBalanceReportBody(trialBalanceDataReportHolders, document);
084                    } else {
085                        LOG.error("Trial Balance Report Header Error");
086                    }
087                } 
088                
089                document.close();
090    
091            } catch (Exception e) {
092                LOG.error("PDF Error: " + e.getMessage());
093                return null;
094            }
095            
096            return pdfStream;
097        }
098        
099        /**
100         * Generates the Trial Balance report
101         * 
102         * @param trialBalanceReports
103         * @param document
104         * @return
105         */
106        public boolean printTrialBalanceReportBody(List<TrialBalanceReportDataHolder> trialBalanceReports, Document document) {
107                    
108            try {
109                // title
110                Paragraph title = new Paragraph("KEMID Trial Balance");
111                title.setAlignment(Element.ALIGN_CENTER);
112                Date currentDate = SpringContext.getBean(KEMService.class).getCurrentDate();
113                DateTimeService dateTimeService = SpringContext.getBean(DateTimeService.class);
114                String asOfDate = dateTimeService.toDateString(currentDate);            
115                title.add("\nAs of " + asOfDate + "\n\n");
116                document.add(title);
117                   
118                // report table
119                PdfPTable table = new PdfPTable(7);
120                table.setWidthPercentage(FULL_TABLE_WIDTH);
121                int[] relativeWidths = {10, 15, 15, 15, 15, 15, 15};
122                table.setWidths(relativeWidths);
123                table.getDefaultCell().setPadding(2);
124                
125                // table titles
126                table.addCell(new Phrase("KEMID", titleFont));
127                table.addCell(new Phrase("KEMID Name", titleFont));
128                table.addCell(new Phrase("Income Cash Balance", titleFont));
129                table.addCell(new Phrase("Principal Cash Balance", titleFont));
130                table.addCell(new Phrase("KEMID Total Market Value", titleFont));
131                table.addCell(new Phrase("Available Expendable Funds", titleFont));
132                table.addCell(new Phrase("FY Remainder Estimated Income", titleFont));
133                
134                // table body
135                Font cellFont = regularFont;
136                BigDecimal totalIncomeCashBalance = BigDecimal.ZERO;
137                BigDecimal totalPrincipalCashBalance = BigDecimal.ZERO;
138                BigDecimal totalMarketValueBalance = BigDecimal.ZERO;
139                BigDecimal totalAvailableExpendableFundsBalance = BigDecimal.ZERO;
140                BigDecimal totalFyRemainderEstimatedIncome = BigDecimal.ZERO;
141                
142                for (TrialBalanceReportDataHolder trialBalanceReport : trialBalanceReports) {
143                    
144                    // totals, which is the last row
145                    if (trialBalanceReport.getKemid().equalsIgnoreCase("TOTALS")) {
146                        cellFont = titleFont;
147                    }
148                    
149                    table.addCell(new Phrase(trialBalanceReport.getKemid(), cellFont));
150                    table.addCell(new Phrase(trialBalanceReport.getKemidName(), cellFont));
151                    if (trialBalanceReport.getInocmeCashBalance() != null) { 
152                        String amount = formatAmount(trialBalanceReport.getInocmeCashBalance().bigDecimalValue());
153                        table.addCell(createCell(amount, cellFont, Element.ALIGN_RIGHT, true));
154                        totalIncomeCashBalance = totalIncomeCashBalance.add(trialBalanceReport.getInocmeCashBalance().bigDecimalValue());
155                    } else { 
156                        table.addCell(createCell(ZERO_FOR_REPORT, cellFont, Element.ALIGN_RIGHT, true));
157                    }
158                    if (trialBalanceReport.getPrincipalcashBalance() != null) {
159                        String amount = formatAmount(trialBalanceReport.getPrincipalcashBalance().bigDecimalValue());
160                        totalPrincipalCashBalance = totalPrincipalCashBalance.add(trialBalanceReport.getPrincipalcashBalance().bigDecimalValue());
161                        table.addCell(createCell(amount, cellFont, Element.ALIGN_RIGHT, true));
162                    } else {
163                        table.addCell(createCell(ZERO_FOR_REPORT, cellFont, Element.ALIGN_RIGHT, true));
164                    }
165                    if (trialBalanceReport.getKemidTotalMarketValue() != null) {
166                        String amount = formatAmount(trialBalanceReport.getKemidTotalMarketValue());
167                        totalMarketValueBalance = totalMarketValueBalance.add(trialBalanceReport.getKemidTotalMarketValue());
168                        table.addCell(createCell(amount, cellFont, Element.ALIGN_RIGHT, true));
169                    } else {
170                        table.addCell(createCell(ZERO_FOR_REPORT, cellFont, Element.ALIGN_RIGHT, true));
171                    }
172                    if (trialBalanceReport.getAvailableExpendableFunds() != null) {
173                        String amount = formatAmount(trialBalanceReport.getAvailableExpendableFunds());
174                        totalAvailableExpendableFundsBalance = totalAvailableExpendableFundsBalance.add(trialBalanceReport.getAvailableExpendableFunds());
175                        table.addCell(createCell(amount, cellFont, Element.ALIGN_RIGHT, true));
176                    } else {
177                        table.addCell(createCell(ZERO_FOR_REPORT, cellFont, Element.ALIGN_RIGHT, true));
178                    }
179                    if (trialBalanceReport.getFyRemainderEstimatedIncome() != null) {
180                        String amount = formatAmount(trialBalanceReport.getFyRemainderEstimatedIncome());
181                        totalFyRemainderEstimatedIncome = totalFyRemainderEstimatedIncome.add(trialBalanceReport.getFyRemainderEstimatedIncome());
182                        table.addCell(createCell(amount, cellFont, Element.ALIGN_RIGHT, true));
183                    } else {
184                        table.addCell(createCell(ZERO_FOR_REPORT, cellFont, Element.ALIGN_RIGHT, true));
185                    }
186                }
187                
188                // totals
189                PdfPCell blank = new PdfPCell(new Paragraph("", cellFont));
190                blank.setColspan(7);
191                blank.setBackgroundColor(Color.LIGHT_GRAY);
192                table.addCell(blank);
193                
194                table.addCell(createCell("TOTALS", titleFont, Element.ALIGN_LEFT, true));
195                table.addCell(createCell("", cellFont, Element.ALIGN_RIGHT, true));
196                table.addCell(createCell(formatAmount(totalIncomeCashBalance), cellFont, Element.ALIGN_RIGHT, true));
197                table.addCell(createCell(formatAmount(totalPrincipalCashBalance), cellFont, Element.ALIGN_RIGHT, true));
198                table.addCell(createCell(formatAmount(totalMarketValueBalance), cellFont, Element.ALIGN_RIGHT, true));
199                table.addCell(createCell(formatAmount(totalAvailableExpendableFundsBalance), cellFont, Element.ALIGN_RIGHT, true));
200                table.addCell(createCell(formatAmount(totalFyRemainderEstimatedIncome), cellFont, Element.ALIGN_RIGHT, true));
201                
202                document.add(table);
203                
204            } catch (Exception e) {
205                return false;
206            }
207            
208            return true;
209        }   
210    
211    }