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.io.ByteArrayOutputStream;
019    import java.math.BigDecimal;
020    import java.util.List;
021    
022    import org.kuali.kfs.module.endow.report.util.TransactionStatementReportDataHolder.TransactionArchiveInfo;
023    
024    import com.lowagie.text.Document;
025    import com.lowagie.text.Element;
026    import com.lowagie.text.Font;
027    import com.lowagie.text.Paragraph;
028    import com.lowagie.text.Phrase;
029    import com.lowagie.text.pdf.PdfPTable;
030    import com.lowagie.text.pdf.PdfWriter;
031    
032    public class TransactionStatementReportPrint extends EndowmentReportPrintBase {
033    
034        private final String ZERO_FOR_REPORT = "0.00";
035        
036        /**
037         * Generates the report in PDF using iText
038         * 
039         * @param reportRequestHeaderDataHolder
040         * @param transactionStatementDataReportHolders
041         * @return
042         */
043        public ByteArrayOutputStream printTransactionStatementReport(EndowmentReportHeaderDataHolder reportRequestHeaderDataHolder, List<TransactionStatementReportDataHolder> transactionStatementDataReportHolders, String listKemidsInHeader) {
044            
045            final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(TransactionStatementReportPrint.class);
046            
047            Document document = new Document();
048            document.setPageSize(LETTER_PORTRAIT);
049            document.addTitle("Endowment Transaction Statement");
050            
051            ByteArrayOutputStream pdfStream = new ByteArrayOutputStream();
052    
053            try {            
054                PdfWriter.getInstance(document, pdfStream);            
055                document.open();
056    
057                // print the report header
058                if (printReportHeaderPage(reportRequestHeaderDataHolder, document, listKemidsInHeader)) {
059                    // and then the body
060                    if (transactionStatementDataReportHolders != null && transactionStatementDataReportHolders.size() > 0) {            
061                        printTransactionStatementReportBody(transactionStatementDataReportHolders, document);
062                    } 
063                } else {
064                    LOG.error("Transaction Statement Report Header Error");
065                }
066    
067                document.close();
068    
069            } catch (Exception e) {
070                LOG.error("PDF Error: " + e.getMessage());
071                return null;
072            }
073            
074            return pdfStream;
075        }
076        
077        /**
078         * Generates the Transaction Statement report    
079         * 
080         * @param transactionStatementReports
081         * @param document
082         * @return
083         */
084        public boolean printTransactionStatementReportBody(List<TransactionStatementReportDataHolder> transactionStatementReportDataHolders, Document document) {
085                        
086            // for each kemid
087            try {                               
088                Font cellFont = regularFont;
089                for (TransactionStatementReportDataHolder transactionStatementReport : transactionStatementReportDataHolders) {
090                    
091                    // new page
092                    document.newPage();
093                    
094                    // header
095                    StringBuffer title = new StringBuffer();
096                    title.append(transactionStatementReport.getInstitution()).append("\n");
097                    title.append("STATEMENT OF TRANSACTIONS FROM").append("\n");
098                    title.append(transactionStatementReport.getBeginningDate()).append(" to ").append(transactionStatementReport.getEndingDate()).append("\n");
099                    title.append(transactionStatementReport.getKemid()).append("     ").append(transactionStatementReport.getKemidLongTitle()).append("\n\n");
100                    Paragraph header = new Paragraph(title.toString());
101                    header.setAlignment(Element.ALIGN_CENTER);                
102                    document.add(header);
103    
104                    // report table
105                    PdfPTable table = new PdfPTable(4);
106                    table.setWidthPercentage(FULL_TABLE_WIDTH);
107                    int[] relativeWidths = {10, 40, 25, 25};
108                    table.setWidths(relativeWidths);
109                    table.getDefaultCell().setPadding(5);
110                    
111                    // table titles
112                    table.addCell(new Phrase("DATE", titleFont));
113                    table.addCell(new Phrase("DESCRIPTION", titleFont));
114                    table.addCell(createCell("INCOME AMOUNT", titleFont, Element.ALIGN_RIGHT, Element.ALIGN_MIDDLE, true));
115                    table.addCell(createCell("PRINCIPAL AMOUNT", titleFont, Element.ALIGN_RIGHT, Element.ALIGN_MIDDLE, true));
116                    
117                    // beginning cash balance
118                    table.addCell(new Phrase(transactionStatementReport.getBeginningDate(), cellFont));
119                    table.addCell(new Phrase("Beginning Cash Balance", cellFont));                
120                    String amount = "";
121                    amount = formatAmount(transactionStatementReport.getBeginningIncomeCash());
122                    table.addCell(createCell(amount, cellFont, Element.ALIGN_RIGHT, true));                
123                    amount = formatAmount(transactionStatementReport.getBeginningPrincipalCash());
124                    table.addCell(createCell(amount, cellFont, Element.ALIGN_RIGHT, true));
125                    
126                    // transactions
127                    List<TransactionArchiveInfo> TransactionArchiveInfoList = transactionStatementReport.getTransactionArchiveInfoList(); 
128                    for (TransactionArchiveInfo transactionArchiveInfo : TransactionArchiveInfoList) {
129                        table.addCell(new Phrase(transactionArchiveInfo.getPostedDate(), cellFont));
130                        table.addCell(new Phrase(getDescription(transactionArchiveInfo), cellFont));                
131                        amount = formatAmount(transactionArchiveInfo.getTransactionIncomeCash());
132                        table.addCell(createCell(amount, cellFont, Element.ALIGN_RIGHT, true));
133                        amount = formatAmount(transactionArchiveInfo.getTransactionPrincipalCash());
134                        table.addCell(createCell(amount, cellFont, Element.ALIGN_RIGHT, true));
135                    }
136                    
137                    // ending cash balance
138                    table.addCell(new Phrase(transactionStatementReport.getEndingDate(), cellFont));
139                    table.addCell(new Phrase("Ending Cash Balance", cellFont));                
140                    amount = formatAmount(transactionStatementReport.getEndingIncomeCash());
141                    table.addCell(createCell(amount, cellFont, Element.ALIGN_RIGHT, true));
142                    amount = formatAmount(transactionStatementReport.getEndingPrincipalCash());
143                    table.addCell(createCell(amount, cellFont, Element.ALIGN_RIGHT, true));
144                    
145                    document.add(table);
146    
147                    // footer
148                    printFooter(transactionStatementReport.getFooter(), document);
149                }
150                
151            } catch (Exception e) {
152                return false;
153            }
154            
155            return true;
156        }
157        
158        /**
159         * Gets the transaction info
160         * 
161         * @param transactionArchiveInfo
162         * @return
163         */
164        protected String getDescription(TransactionArchiveInfo transactionArchiveInfo) {
165            
166            StringBuffer description = new StringBuffer();
167            
168            description.append(transactionArchiveInfo.getDocumentName()).append("\n");
169            if (transactionArchiveInfo.getEtranCode() != null) {
170                description.append(transactionArchiveInfo.getEtranCode())
171                           .append(" - ")
172                           .append(transactionArchiveInfo.getEtranCodeDesc()).append("\n");
173            }
174    
175            description.append(transactionArchiveInfo.getTransactionDesc()).append("\n");
176            
177            if (transactionArchiveInfo.getTransactionSecurity() != null && !transactionArchiveInfo.getTransactionSecurity().isEmpty()) {
178                description.append(transactionArchiveInfo.getTransactionSecurity()).append("\n");
179            }
180            if (transactionArchiveInfo.getTransactionSecurityUnits() != null && transactionArchiveInfo.getTransactionSecurityUnits() != BigDecimal.ZERO) {
181                description.append(formatAmount(transactionArchiveInfo.getTransactionSecurityUnits(), FORMAT164))
182                           .append(" at ")
183                           .append(formatAmount(transactionArchiveInfo.getTransactionSecurityUnitValue(), FORMAT195))
184                           .append(" per unit");
185            }
186         
187            return description.toString();
188        }
189    
190    }
191