1 package reports; 2 3 import com.lowagie.text.*; 4 import com.lowagie.text.pdf.*; 5 import java.io.ByteArrayOutputStream ; 6 import dinamica.*; 7 8 16 public class PDFCustomerOrdersReport extends AbstractPDFOutput 17 { 18 19 22 protected void createPDF( 23 GenericTransaction data, 24 ByteArrayOutputStream buf) 25 throws Throwable 26 { 27 28 Document doc = new Document(); 30 PdfWriter docWriter = PdfWriter.getInstance(doc, buf); 31 doc.setPageSize(PageSize.LETTER); 32 33 HeaderFooter header = new HeaderFooter(new Phrase(getHeader()), false); 35 header.setBorder(Rectangle.BOTTOM); 36 header.setAlignment(Rectangle.ALIGN_CENTER); 37 doc.setHeader(header); 38 39 HeaderFooter footer = new HeaderFooter(new Phrase(getFooter()), true); 41 footer.setBorder(Rectangle.TOP); 42 footer.setAlignment(Rectangle.ALIGN_RIGHT); 43 doc.setFooter(footer); 44 45 doc.open(); 46 47 Paragraph t = new Paragraph(getReportTitle(),new Font(Font.HELVETICA, 18f)); 49 t.setAlignment(Rectangle.ALIGN_CENTER); 50 doc.add(t); 51 52 PdfPTable custinfo = getCustomerSection(data); 54 doc.add(custinfo); 55 56 MasterDetailReader dataobj = (MasterDetailReader)data; 58 Recordset master = dataobj.getRecordset("master"); 59 master.top(); 60 while (master.next()) 61 { 62 doc.add(new Paragraph(" ")); 64 65 doc.add( getGroupMaster(master) ); 67 68 doc.add( getGroupDetail(master, dataobj.getDetail(master)) ); 70 71 } 72 73 doc.add(new Paragraph(" ")); 75 doc.add( getTotal(dataobj) ); 76 77 doc.close(); 78 docWriter.close(); 79 80 } 81 82 87 PdfPTable getCustomerSection(GenericTransaction data) throws Throwable 88 { 89 90 93 94 Recordset custinfo = data.getRecordset("query.sql"); 96 custinfo.first(); 97 98 PdfPTable datatable = new PdfPTable(2); 100 101 datatable.getDefaultCell().setPadding(3); 103 int headerwidths[] = {30, 70}; datatable.setWidths(headerwidths); 105 datatable.setWidthPercentage(50); datatable.setHorizontalAlignment(Element.ALIGN_CENTER); 107 datatable.getDefaultCell().setBorderWidth(1); 108 datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT); 109 110 PdfPCell c1 = new PdfPCell(new Phrase("Contacto")); 111 c1.setGrayFill(0.9f); 112 datatable.addCell(c1); 113 datatable.addCell(custinfo.getString("contactname")); 114 115 PdfPCell c2 = new PdfPCell(new Phrase("Empresa")); 116 c2.setGrayFill(0.9f); 117 datatable.addCell(c2); 118 datatable.addCell(custinfo.getString("companyname")); 119 120 PdfPCell c3 = new PdfPCell(new Phrase("Teléfono")); 121 c3.setGrayFill(0.9f); 122 datatable.addCell(c3); 123 datatable.addCell(custinfo.getString("phone")); 124 125 PdfPCell c4 = new PdfPCell(new Phrase("País")); 126 c4.setGrayFill(0.9f); 127 datatable.addCell(c4); 128 datatable.addCell(custinfo.getString("country")); 129 130 return datatable; 131 132 } 133 134 140 Paragraph getGroupMaster(Recordset master) throws Throwable 141 { 142 143 String text = "Order #" + master.getString("orderid"); 144 text = text + " Date: " + StringUtil.formatDate(master.getDate("orderdate"), "dd-MMM-yyyy"); 145 146 Paragraph t = new Paragraph(text,new Font(Font.HELVETICA, 12f, Font.BOLD)); 147 t.setAlignment(Rectangle.ALIGN_LEFT); 148 return t; 149 150 } 151 152 157 PdfPTable getGroupDetail(Recordset master, Recordset detail) throws Throwable 158 { 159 160 PdfPTable datatable = new PdfPTable(6); 162 163 datatable.getDefaultCell().setPadding(1); 165 int headerwidths[] = {35, 20, 10, 10, 10, 15}; datatable.setWidths(headerwidths); 167 datatable.setWidthPercentage(100); datatable.setHorizontalAlignment(Element.ALIGN_CENTER); 169 datatable.getDefaultCell().setBorderWidth(1); 170 datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); 171 datatable.getDefaultCell().setGrayFill(0.9f); 172 datatable.addCell("Product"); 173 datatable.addCell("Category"); 174 datatable.addCell("Quantity"); 175 datatable.addCell("Unit Price"); 176 datatable.addCell("Discount"); 177 datatable.addCell("Sub-Total"); 178 179 datatable.setHeaderRows(1); 180 datatable.getDefaultCell().setGrayFill(0.0f); 181 datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT); 182 183 while (detail.next()) 185 { 186 String data = null; 187 188 datatable.addCell(detail.getString("productname")); 189 datatable.addCell(detail.getString("categoryname")); 190 191 PdfPCell c1 = new PdfPCell(new Phrase(detail.getString("quantity"))); 192 c1.setHorizontalAlignment(Element.ALIGN_RIGHT); 193 datatable.addCell(c1); 194 195 data = StringUtil.formatNumber(detail.getValue("unitprice"),"#,##0.00"); 196 PdfPCell c2 = new PdfPCell(new Phrase(data)); 197 c2.setHorizontalAlignment(Element.ALIGN_RIGHT); 198 datatable.addCell(c2); 199 200 data = StringUtil.formatNumber(detail.getValue("discount"),"#,##0.00"); 201 PdfPCell c3 = new PdfPCell(new Phrase(data)); 202 c3.setHorizontalAlignment(Element.ALIGN_RIGHT); 203 datatable.addCell(c3); 204 205 data = StringUtil.formatNumber(detail.getValue("subtotal"),"#,##0.00"); 206 PdfPCell c4 = new PdfPCell(new Phrase(data)); 207 c4.setHorizontalAlignment(Element.ALIGN_RIGHT); 208 datatable.addCell(c4); 209 210 } 211 212 PdfPCell c1 = new PdfPCell(new Phrase("TOTAL", new Font(Font.HELVETICA, 10f, Font.BOLD))); 214 c1.setColspan(5); 215 datatable.addCell(c1); 216 217 PdfPCell c2 = new PdfPCell(new Phrase(StringUtil.formatNumber(master.getValue("total"), "#,##0.00"))); 218 c2.setHorizontalAlignment(Element.ALIGN_RIGHT); 219 datatable.addCell(c2); 220 221 return datatable; 222 223 } 224 225 230 PdfPTable getTotal(MasterDetailReader dataobj) throws Throwable 231 { 232 233 PdfPTable datatable = new PdfPTable(2); 235 236 datatable.getDefaultCell().setPadding(1); 238 int headerwidths[] = {85, 15}; datatable.setWidths(headerwidths); 240 datatable.setWidthPercentage(100); datatable.setHorizontalAlignment(Element.ALIGN_CENTER); 242 datatable.getDefaultCell().setBorderWidth(1); 243 datatable.getDefaultCell().setGrayFill(0.0f); 244 245 Recordset custinfo = dataobj.getRecordset("query.sql"); 247 custinfo.first(); 248 249 PdfPCell c1 = new PdfPCell(new Phrase("TOTAL", new Font(Font.HELVETICA, 10f, Font.BOLD))); 250 datatable.addCell(c1); 251 252 PdfPCell c2 = new PdfPCell(new Phrase(StringUtil.formatNumber(custinfo.getValue("total"), "#,##0.00"))); 253 c2.setHorizontalAlignment(Element.ALIGN_RIGHT); 254 datatable.addCell(c2); 255 256 return datatable; 257 258 } 259 260 } 261 | Popular Tags |