001: /*
002: * Copyright 2006-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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.module.gl.util;
017:
018: import java.io.FileOutputStream;
019: import java.text.NumberFormat;
020: import java.text.SimpleDateFormat;
021: import java.util.Date;
022: import java.util.Iterator;
023:
024: import org.kuali.core.util.KualiDecimal;
025: import org.kuali.kfs.KFSConstants;
026: import org.kuali.module.gl.bo.OriginEntryFull;
027:
028: import com.lowagie.text.Document;
029: import com.lowagie.text.Font;
030: import com.lowagie.text.FontFactory;
031: import com.lowagie.text.PageSize;
032: import com.lowagie.text.Phrase;
033: import com.lowagie.text.pdf.PdfPCell;
034: import com.lowagie.text.pdf.PdfPTable;
035: import com.lowagie.text.pdf.PdfWriter;
036:
037: /**
038: * This class represents functionality needed to generate a general ledger pending entry report
039: */
040: public class GeneralLedgerPendingEntryReport {
041: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
042: .getLogger(GeneralLedgerPendingEntryReport.class);
043:
044: private PdfPTable dataTable;
045: private Font headerFont;
046: private Font textFont;
047: private NumberFormat amountFormat;
048: private NumberFormat countFormat;
049:
050: /**
051: * This method generates the actual report body for the general ledger pending entry report
052: *
053: * @param runDate date report is run
054: * @param batchReportsDirectory directory where batch reports reside
055: * @param sdf simple date format
056: * @param entries iterator containing origin entries
057: */
058: public void generateReport(Date runDate,
059: String batchReportsDirectory, SimpleDateFormat sdf,
060: Iterator entries) {
061: LOG.debug("generateReport() started");
062:
063: String title = "PENDING LEDGER ENTRY TABLE";
064: String filePrefix = "glpe_list";
065:
066: headerFont = FontFactory.getFont(FontFactory.COURIER, 8,
067: Font.BOLD);
068: textFont = FontFactory.getFont(FontFactory.COURIER, 8,
069: Font.NORMAL);
070:
071: amountFormat = NumberFormat.getInstance();
072: amountFormat.setGroupingUsed(true);
073: amountFormat.setMaximumFractionDigits(2);
074: amountFormat.setMinimumFractionDigits(2);
075: countFormat = NumberFormat.getInstance();
076: countFormat.setGroupingUsed(true);
077: countFormat.setMaximumFractionDigits(0);
078: countFormat.setMinimumFractionDigits(0);
079:
080: Document document = new Document(PageSize.A4.rotate());
081:
082: TransactionReport.PageHelper helper = new TransactionReport.PageHelper();
083:
084: helper.runDate = runDate;
085: helper.headerFont = headerFont;
086: helper.title = title;
087:
088: try {
089: String filename = batchReportsDirectory + "/" + filePrefix
090: + "_";
091:
092: filename = filename + sdf.format(runDate);
093: filename = filename + ".pdf";
094: PdfWriter writer = PdfWriter.getInstance(document,
095: new FileOutputStream(filename));
096: writer.setPageEvent(helper);
097:
098: document.open();
099:
100: float[] columnWidths = new float[] { 6, 15, 5, 5, 15, 6,
101: 16, 16, 16 };
102:
103: dataTable = new PdfPTable(columnWidths);
104: dataTable.setHeaderRows(1);
105: dataTable.setWidthPercentage(100);
106:
107: String[] columnHeaders = new String[] { "Doc Type",
108: "Document Number", "Bal Type", "COA Code",
109: "Account Number", "Object Code", "Credit", "Debit",
110: "Blank" };
111:
112: for (int x = 0; x < columnHeaders.length; x++) {
113: PdfPCell cell = new PdfPCell(new Phrase(
114: columnHeaders[x], headerFont));
115: dataTable.addCell(cell);
116: }
117:
118: String previousDocumentType = "-1";
119: String previousDocumentNumber = "-1";
120:
121: KualiDecimal totalCredit = KualiDecimal.ZERO;
122: KualiDecimal totalDebit = KualiDecimal.ZERO;
123: KualiDecimal totalBlank = KualiDecimal.ZERO;
124: int totalCount = 0;
125:
126: KualiDecimal totalDocumentTypeCredit = KualiDecimal.ZERO;
127: KualiDecimal totalDocumentTypeDebit = KualiDecimal.ZERO;
128: KualiDecimal totalDocumentTypeBlank = KualiDecimal.ZERO;
129: int totalDocumentTypeCount = 0;
130:
131: KualiDecimal totalDocumentCredit = KualiDecimal.ZERO;
132: KualiDecimal totalDocumentDebit = KualiDecimal.ZERO;
133: KualiDecimal totalDocumentBlank = KualiDecimal.ZERO;
134:
135: OriginEntryFull lastEntry = null;
136:
137: boolean firstAccount = true;
138:
139: while (entries.hasNext()) {
140: OriginEntryFull entry = (OriginEntryFull) entries
141: .next();
142:
143: String docNumber = entry
144: .getFinancialSystemOriginationCode()
145: + "-" + entry.getDocumentNumber();
146:
147: if (!docNumber.equals(previousDocumentNumber)
148: && !"-1".equals(previousDocumentNumber)) {
149: printTotal("Totals:", totalDocumentCredit,
150: totalDocumentDebit, totalDocumentBlank);
151: totalDocumentCredit = KualiDecimal.ZERO;
152: totalDocumentDebit = KualiDecimal.ZERO;
153: totalDocumentBlank = KualiDecimal.ZERO;
154: firstAccount = true;
155: }
156:
157: // Show doc type totals.
158: if (!entry.getFinancialDocumentTypeCode().equals(
159: previousDocumentType)
160: && !"-1".equals(previousDocumentType)) {
161: printTotal("Totals for Document Type "
162: + previousDocumentType
163: + " Cnt: "
164: + countFormat
165: .format(totalDocumentTypeCount),
166: totalDocumentTypeCredit,
167: totalDocumentTypeDebit,
168: totalDocumentTypeBlank);
169: totalDocumentTypeCredit = KualiDecimal.ZERO;
170: totalDocumentTypeDebit = KualiDecimal.ZERO;
171: totalDocumentTypeBlank = KualiDecimal.ZERO;
172: totalDocumentTypeCount = 0;
173: }
174:
175: previousDocumentNumber = docNumber;
176: previousDocumentType = entry
177: .getFinancialDocumentTypeCode();
178:
179: totalDocumentTypeCount++;
180: totalCount++;
181:
182: if (firstAccount) {
183: firstAccount = false;
184:
185: PdfPCell column = new PdfPCell(new Phrase(entry
186: .getFinancialDocumentTypeCode(), textFont));
187: column.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
188: dataTable.addCell(column);
189:
190: column = new PdfPCell(new Phrase(docNumber,
191: textFont));
192: column.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
193: dataTable.addCell(column);
194:
195: column = new PdfPCell(new Phrase(entry
196: .getFinancialBalanceTypeCode(), textFont));
197: column.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
198: dataTable.addCell(column);
199: } else {
200: PdfPCell column = new PdfPCell(new Phrase(" ",
201: textFont));
202: column.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
203: dataTable.addCell(column);
204:
205: column = new PdfPCell(new Phrase(" ", textFont));
206: column.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
207: dataTable.addCell(column);
208:
209: column = new PdfPCell(new Phrase(" ", textFont));
210: column.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
211: dataTable.addCell(column);
212: }
213:
214: PdfPCell column = new PdfPCell(new Phrase(entry
215: .getChartOfAccountsCode(), textFont));
216: column.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
217: dataTable.addCell(column);
218:
219: column = new PdfPCell(new Phrase(entry
220: .getAccountNumber(), textFont));
221: column.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
222: dataTable.addCell(column);
223:
224: column = new PdfPCell(new Phrase(entry
225: .getFinancialObjectCode(), textFont));
226: column.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
227: dataTable.addCell(column);
228:
229: KualiDecimal amount = null;
230: if (KFSConstants.GL_DEBIT_CODE.equals(entry
231: .getTransactionDebitCreditCode())) {
232: amount = entry.getTransactionLedgerEntryAmount();
233: totalDocumentDebit = totalDocumentDebit.add(amount);
234: totalDocumentTypeDebit = totalDocumentTypeDebit
235: .add(amount);
236: totalDebit = totalDebit.add(amount);
237: }
238: column = new PdfPCell(new Phrase(null == amount ? " "
239: : amountFormat.format(amount.doubleValue()),
240: textFont));
241: column.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
242: dataTable.addCell(column);
243:
244: amount = null;
245: if (KFSConstants.GL_CREDIT_CODE.equals(entry
246: .getTransactionDebitCreditCode())) {
247: amount = entry.getTransactionLedgerEntryAmount();
248: totalDocumentCredit = totalDocumentCredit
249: .add(amount);
250: totalDocumentTypeCredit = totalDocumentTypeCredit
251: .add(amount);
252: totalCredit = totalCredit.add(amount);
253: }
254: column = new PdfPCell(new Phrase(null == amount ? " "
255: : amountFormat.format(amount.doubleValue()),
256: textFont));
257: column.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
258: dataTable.addCell(column);
259:
260: amount = null;
261: if (KFSConstants.GL_BUDGET_CODE.equals(entry
262: .getTransactionDebitCreditCode())) {
263: amount = entry.getTransactionLedgerEntryAmount();
264: totalDocumentBlank = totalDocumentBlank.add(amount);
265: totalDocumentTypeBlank = totalDocumentTypeBlank
266: .add(amount);
267: totalBlank = totalBlank.add(amount);
268: }
269: column = new PdfPCell(new Phrase(null == amount ? " "
270: : amountFormat.format(amount.doubleValue()),
271: textFont));
272: column.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
273: dataTable.addCell(column);
274:
275: lastEntry = entry;
276: }
277:
278: if (totalCount > 0) {
279: printTotal("Totals:", totalDocumentCredit,
280: totalDocumentDebit, totalDocumentBlank);
281: }
282:
283: if (totalCount > 0) {
284: printTotal("Totals for Document Type "
285: + lastEntry.getFinancialDocumentTypeCode()
286: + " Cnt: "
287: + countFormat.format(totalDocumentTypeCount),
288: totalDocumentTypeCredit,
289: totalDocumentTypeDebit, totalDocumentTypeBlank);
290: }
291:
292: printTotal("Grand Totals Cnt: "
293: + countFormat.format(totalCount), totalCredit,
294: totalDebit, totalBlank);
295:
296: document.add(dataTable);
297: } catch (Exception de) {
298: LOG
299: .error(
300: "generatePendingEntryReport() Error creating PDF report",
301: de);
302: throw new RuntimeException("Report Generation Failed");
303: }
304: document.close();
305: }
306:
307: /**
308: * Methods prints totals in report
309: *
310: * @param title title for total
311: * @param credit credit amount
312: * @param debit debit amount
313: * @param blank blank amount
314: */
315: private void printTotal(String title, KualiDecimal credit,
316: KualiDecimal debit, KualiDecimal blank) {
317: PdfPCell column = new PdfPCell(new Phrase(title, headerFont));
318: column.setColspan(6);
319: column.setPaddingTop(10.0F);
320: column.setPaddingBottom(10.0F);
321: column.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
322: dataTable.addCell(column);
323:
324: column = new PdfPCell(new Phrase(amountFormat.format(debit
325: .doubleValue()), headerFont));
326: column.setPaddingTop(10.0F);
327: column.setPaddingBottom(10.0F);
328: column.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
329: dataTable.addCell(column);
330:
331: column = new PdfPCell(new Phrase(amountFormat.format(credit
332: .doubleValue()), headerFont));
333: column.setPaddingTop(10.0F);
334: column.setPaddingBottom(10.0F);
335: column.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
336: dataTable.addCell(column);
337:
338: column = new PdfPCell(new Phrase(amountFormat.format(blank
339: .doubleValue()), headerFont));
340: column.setPaddingTop(10.0F);
341: column.setPaddingBottom(10.0F);
342: column.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
343: dataTable.addCell(column);
344: }
345: }
|