Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.hris.payroll.reports; import com.hris.common.CommonUtil; import com.hris.employee.serviceprovider.EmployeeServiceImpl; import com.hris.model.Advances; import com.hris.model.PayrollRegister; import com.hris.payroll.serviceprovider.AdvancesServiceImpl; import com.hris.payroll.serviceprovider.PayrollServiceImpl; import com.hris.service.AdvancesService; import com.hris.service.CorporationService; import com.hris.service.EmployeeService; import com.hris.service.PayrollService; import com.hris.serviceprovider.CorporationServiceImpl; import com.itextpdf.text.Chunk; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.FontFactory; import com.itextpdf.text.PageSize; import com.itextpdf.text.Paragraph; import com.itextpdf.text.Phrase; import com.itextpdf.text.Rectangle; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import com.vaadin.server.StreamResource.StreamSource; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.Date; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author jetberay */ public class AdvancesReportPdf implements StreamSource { CorporationService cs = new CorporationServiceImpl(); EmployeeService es = new EmployeeServiceImpl(); PayrollService ps = new PayrollServiceImpl(); AdvancesService as = new AdvancesServiceImpl(); private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); private int branchId; private Date payrollDate; private String reportType; Font header = FontFactory.getFont("Times-Roman", 10, Font.BOLD); Font content = FontFactory.getFont("Times-Roman", 8); Font cellContent = FontFactory.getFont("Times-Roman", 8); Font boldFont = FontFactory.getFont("Times-Roman", 8, Font.BOLD); Font smallFont = FontFactory.getFont("Times-Roman", 7); Font smallerFont = FontFactory.getFont("Times-Roman", 6); public AdvancesReportPdf(int branchId, Date payrollDate, String reportType) { this.branchId = branchId; this.payrollDate = payrollDate; this.reportType = reportType; Document document = null; Rectangle pageSize = new Rectangle(318, 825); try { document = new Document(PageSize.A4.rotate(), 37, 37, 37, 37); PdfWriter writer = PdfWriter.getInstance(document, outputStream); document.open(); int tradeId = cs.getTradeIdByBranchId(branchId); int corporateId = cs.getCorporateIdByTradeId(tradeId); Paragraph reportHeader = new Paragraph(); reportHeader.setAlignment(Element.ALIGN_LEFT); reportHeader.add(new Phrase(cs.getCorporateById(corporateId).toUpperCase(), header)); document.add(reportHeader); reportHeader = new Paragraph(); reportHeader.setAlignment(Element.ALIGN_LEFT); reportHeader.add(new Phrase("Trade: " + cs.getTradeById(tradeId).toUpperCase(), header)); document.add(reportHeader); reportHeader = new Paragraph(); reportHeader.setAlignment(Element.ALIGN_LEFT); reportHeader.add(new Phrase("Branch: " + cs.getBranchById(getBranchId()).toUpperCase(), header)); document.add(reportHeader); reportHeader = new Paragraph(); reportHeader.setAlignment(Element.ALIGN_LEFT); reportHeader.add(new Phrase("Advances Type: " + getReportType(), content)); document.add(reportHeader); reportHeader = new Paragraph(); reportHeader.setAlignment(Element.ALIGN_LEFT); reportHeader.add(new Phrase("Payroll Period: " + CommonUtil.changeDateFormat( ps.findPayrollRegisterByBranch(getBranchId(), getPayrollDate()).getPayrollDate().toString()), content)); document.add(reportHeader); reportHeader = new Paragraph(); reportHeader.setAlignment(Element.ALIGN_LEFT); reportHeader.add(new Phrase("Cut-off Date: " + CommonUtil.changeDateFormat(ps.findPayrollRegisterByBranch(getBranchId(), getPayrollDate()) .getAttendancePeriodFrom().toString()) + " - " + CommonUtil.changeDateFormat(ps.findPayrollRegisterByBranch(getBranchId(), getPayrollDate()) .getAttendancePeriodTo().toString()), content)); document.add(reportHeader); document.add(Chunk.NEWLINE); document.add(Chunk.NEWLINE); PdfPTable table = new PdfPTable(3); // table.setWidthPercentage(100); table.setTotalWidth(new float[] { 180, 120, 300 }); table.setHorizontalAlignment(Element.ALIGN_CENTER); table.getDefaultCell().setBorder(Rectangle.NO_BORDER); PdfPCell cellLabel = new PdfPCell(); Paragraph parLabel = new Paragraph(); parLabel.add(new Phrase("EMPLOYEE", boldFont)); parLabel.setAlignment(Element.ALIGN_LEFT); cellLabel.addElement(parLabel); cellLabel.setVerticalAlignment(Element.ALIGN_MIDDLE); cellLabel.setBorder(Rectangle.BOTTOM); table.addCell(cellLabel); cellLabel = new PdfPCell(); parLabel = new Paragraph(); parLabel.add(new Phrase("AMOUNT", boldFont)); parLabel.setAlignment(Element.ALIGN_RIGHT); cellLabel.addElement(parLabel); cellLabel.setVerticalAlignment(Element.ALIGN_MIDDLE); cellLabel.setBorder(Rectangle.BOTTOM); table.addCell(cellLabel); cellLabel = new PdfPCell(); parLabel = new Paragraph(); parLabel.add(new Phrase("PARTICULARS", boldFont)); parLabel.setAlignment(Element.ALIGN_LEFT); cellLabel.addElement(parLabel); cellLabel.setPaddingLeft(20); cellLabel.setVerticalAlignment(Element.ALIGN_MIDDLE); cellLabel.setBorder(Rectangle.BOTTOM); table.addCell(cellLabel); String middlename; double grandTotalAmount = 0; double subTotalAmount; int prCounter = 0; int advancesCounter; for (PayrollRegister pr : ps.findByBranch(getBranchId(), getPayrollDate())) { if (pr.getMiddlename().isEmpty() || pr.getMiddlename() == null) { middlename = ""; } else { middlename = pr.getMiddlename().toUpperCase(); } String employee = pr.getLastname().toUpperCase() + ", " + pr.getFirstname().toUpperCase() + " " + middlename; subTotalAmount = 0; advancesCounter = 0; for (Advances a : as.findByPayroll(pr.getPayrollId(), getReportType())) { cellLabel = new PdfPCell(); parLabel = new Paragraph(); if (advancesCounter == 0) { parLabel.add(new Phrase(employee, content)); } else { parLabel.add(new Phrase("", content)); } parLabel.setAlignment(Element.ALIGN_LEFT); cellLabel.addElement(parLabel); cellLabel.setVerticalAlignment(Element.ALIGN_MIDDLE); cellLabel.setBorder(Rectangle.NO_BORDER); table.addCell(cellLabel); cellLabel = new PdfPCell(); parLabel = new Paragraph(); parLabel.add(new Phrase(CommonUtil.formatNumValue(a.getAmount()), content)); parLabel.setAlignment(Element.ALIGN_RIGHT); cellLabel.addElement(parLabel); cellLabel.setVerticalAlignment(Element.ALIGN_MIDDLE); subTotalAmount = subTotalAmount + a.getAmount(); advancesCounter++; if (advancesCounter == as.findByPayroll(pr.getPayrollId(), getReportType()).size()) { cellLabel.setBorder(Rectangle.BOTTOM); } else { cellLabel.setBorder(Rectangle.NO_BORDER); } table.addCell(cellLabel); cellLabel = new PdfPCell(); parLabel = new Paragraph(); parLabel.add(new Phrase(a.getParticulars(), content)); parLabel.setAlignment(Element.ALIGN_LEFT); cellLabel.addElement(parLabel); cellLabel.setPaddingLeft(20); cellLabel.setVerticalAlignment(Element.ALIGN_MIDDLE); cellLabel.setBorder(Rectangle.NO_BORDER); table.addCell(cellLabel); } if (subTotalAmount != 0) { subTotal(table, subTotalAmount); } grandTotalAmount = grandTotalAmount + subTotalAmount; prCounter++; } grandTotal(table, grandTotalAmount); document.add(table); } catch (DocumentException ex) { Logger.getLogger(AdvancesReportPdf.class.getName()).log(Level.SEVERE, null, ex); } finally { document.close(); } } private void subTotal(PdfPTable table, double subTotalAmount) { PdfPCell cellLabel = new PdfPCell(); Paragraph parLabel = new Paragraph(); parLabel.add(new Phrase("", content)); parLabel.setAlignment(Element.ALIGN_LEFT); cellLabel.addElement(parLabel); cellLabel.setVerticalAlignment(Element.ALIGN_MIDDLE); cellLabel.setBorder(Rectangle.BOTTOM); cellLabel.setPaddingBottom(10); table.addCell(cellLabel); cellLabel = new PdfPCell(); parLabel = new Paragraph(); parLabel.add(new Phrase( "SUB TOTAL: " + CommonUtil.formatNumValue(CommonUtil.roundOffToTwoDecimalPlaces(subTotalAmount)), content)); parLabel.setAlignment(Element.ALIGN_RIGHT); cellLabel.addElement(parLabel); cellLabel.setVerticalAlignment(Element.ALIGN_MIDDLE); cellLabel.setBorder(Rectangle.BOTTOM); cellLabel.setPaddingBottom(10); table.addCell(cellLabel); cellLabel = new PdfPCell(); parLabel = new Paragraph(); parLabel.add(new Phrase("", content)); parLabel.setAlignment(Element.ALIGN_LEFT); cellLabel.addElement(parLabel); cellLabel.setPaddingLeft(20); cellLabel.setVerticalAlignment(Element.ALIGN_MIDDLE); cellLabel.setBorder(Rectangle.BOTTOM); cellLabel.setPaddingBottom(10); table.addCell(cellLabel); } private void grandTotal(PdfPTable table, double grandTotalAmount) { // PdfPCell cellLabel = new PdfPCell(); // Paragraph parLabel = new Paragraph(); // parLabel.add(new Phrase("", content)); // parLabel.setAlignment(Element.ALIGN_LEFT); // cellLabel.addElement(parLabel); // cellLabel.setVerticalAlignment(Element.ALIGN_MIDDLE); // cellLabel.setBorder(Rectangle.BOTTOM); // cellLabel.setPaddingBottom(10); // table.addCell(cellLabel); PdfPCell cellLabel = new PdfPCell(); Paragraph parLabel = new Paragraph(); parLabel.add(new Phrase( "GRAND TOTAL: " + CommonUtil.formatNumValue(CommonUtil.roundOffToTwoDecimalPlaces(grandTotalAmount)), content)); parLabel.setAlignment(Element.ALIGN_RIGHT); cellLabel.addElement(parLabel); cellLabel.setColspan(2); cellLabel.setVerticalAlignment(Element.ALIGN_MIDDLE); cellLabel.setBorder(Rectangle.BOTTOM); cellLabel.setPaddingBottom(10); table.addCell(cellLabel); cellLabel = new PdfPCell(); parLabel = new Paragraph(); parLabel.add(new Phrase("", content)); parLabel.setAlignment(Element.ALIGN_LEFT); cellLabel.addElement(parLabel); cellLabel.setPaddingLeft(20); cellLabel.setVerticalAlignment(Element.ALIGN_MIDDLE); cellLabel.setBorder(Rectangle.BOTTOM); cellLabel.setPaddingBottom(10); table.addCell(cellLabel); } private int getBranchId() { return branchId; } private Date getPayrollDate() { return payrollDate; } private String getReportType() { return reportType; } @Override public InputStream getStream() { return new ByteArrayInputStream(outputStream.toByteArray()); } }