Subversion Repositories SmartDukaan

Rev

Rev 21686 | Rev 21901 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21686 ashik.ali 1
package com.spice.profitmandi.common.util;
2
 
3
 
4
import java.io.OutputStream;
5
import java.time.LocalDateTime;
6
import java.util.Set;
7
 
8
import org.slf4j.Logger;
9
import org.slf4j.LoggerFactory;
10
 
11
import com.itextpdf.text.Chunk;
12
import com.itextpdf.text.Document;
13
import com.itextpdf.text.DocumentException;
14
import com.itextpdf.text.Element;
15
import com.itextpdf.text.Font;
16
import com.itextpdf.text.Paragraph;
17
import com.itextpdf.text.Rectangle;
18
import com.itextpdf.text.pdf.PdfPCell;
19
import com.itextpdf.text.pdf.PdfPTable;
20
import com.itextpdf.text.pdf.PdfWriter;
21
import com.spice.profitmandi.common.model.CustomCustomer;
22
import com.spice.profitmandi.common.model.CustomFofoOrderItem;
23
import com.spice.profitmandi.common.model.CustomRetailer;
24
import com.spice.profitmandi.common.model.PdfModel;
25
 
26
 
27
public class PdfUtils {
28
 
29
	private static final Font FONT_TITLE = new Font(Font.FontFamily.HELVETICA  , 18, Font.BOLD);
30
	private static Font FONT_NORMAL = new Font(Font.FontFamily.TIMES_ROMAN, 8, Font.NORMAL);
31
	private static Font FONT_BOLD = new Font(Font.FontFamily.TIMES_ROMAN, 8, Font.BOLD);
32
	//private static Font fontTableHeader = new Font(Font.FontFamily.TIMES_ROMAN, 14, Font.BOLD);
33
	private static final String INVOICE_TITLE = "RETAILER_INVOICE";
34
 
35
	private static final Logger LOGGER = LoggerFactory.getLogger(PdfUtils.class);
36
 
37
	public static void generateAndWrite(PdfModel pdfModel, OutputStream outputStream){
38
		Document document = new Document();
39
 
40
        try {
41
        	CustomCustomer customer = pdfModel.getCustomer();
42
        	CustomRetailer retailer = pdfModel.getRetailer();
43
        	Set<CustomFofoOrderItem> orderItems = pdfModel.getOrderItems();
44
 
45
            PdfWriter.getInstance(document,outputStream);
46
 
47
            document.open();
48
            document.addTitle(pdfModel.getTitle());
49
            document.addAuthor(pdfModel.getAuther());
50
 
51
            Paragraph paragraphTitle = new Paragraph(INVOICE_TITLE, FONT_TITLE);
52
            paragraphTitle.setAlignment(Element.ALIGN_CENTER);
53
 
54
            PdfPCell blankCell = new PdfPCell();
55
            blankCell.setBorder(Rectangle.NO_BORDER);
56
            PdfPTable tableCustomerRetailer = new PdfPTable(3);
57
            tableCustomerRetailer.getDefaultCell().setBorder(Rectangle.NO_BORDER);
58
            PdfPCell columnCustomerInfo = new PdfPCell();
59
            columnCustomerInfo.addElement(new Paragraph(customer.getName(), FONT_NORMAL));
60
            columnCustomerInfo.addElement(new Paragraph(customer.getAddress().getLine1() + customer.getAddress().getLine2(), FONT_NORMAL));
61
            columnCustomerInfo.addElement(new Paragraph(customer.getAddress().getCity() + ", " + customer.getAddress().getState() + "\n" + customer.getAddress().getPinCode(), FONT_NORMAL));
62
            columnCustomerInfo.addElement(new Paragraph(customer.getMobileNumber(), FONT_NORMAL));
63
            columnCustomerInfo.setBorder(Rectangle.NO_BORDER);
64
            PdfPCell columnRetailerInfo = new PdfPCell();
65
            //columnRetailerInfo.addElement(new Paragraph("Invoice No:"));
66
            columnRetailerInfo.addElement(new Paragraph(retailer.getBusinessName(), FONT_BOLD));
67
           // columnRetailerInfo.addElement(new Paragraph("Plot No. 485, Udyog Vihar Phase V, Gurgoan-122016", FONT_BOLD));
68
            columnRetailerInfo.addElement(new Paragraph(retailer.getAddress().getLine1() + ", " + retailer.getAddress().getLine2() + ", " + retailer.getAddress().getCity() + "-" + retailer.getAddress().getPinCode() + ", " + retailer.getAddress().getState(), FONT_BOLD));
69
            columnRetailerInfo.addElement(new Paragraph("Contact No.- "+retailer.getMobileNumber(), FONT_BOLD));
70
            columnRetailerInfo.addElement(new Paragraph("TIN NO. " + retailer.getTinNumber(), FONT_BOLD));
71
            columnRetailerInfo.setBorder(Rectangle.NO_BORDER);
72
            PdfPTable tableInvoiceDateRetailer = new PdfPTable(1);
73
            tableInvoiceDateRetailer.getDefaultCell().setBorder(Rectangle.NO_BORDER);
74
            PdfPTable tableInvoiceDate = new PdfPTable(2);
75
            tableInvoiceDate.getDefaultCell().setBorder(Rectangle.NO_BORDER);
76
            PdfPCell invoiceNumberKey = new PdfPCell(new Paragraph("Invoice No:", FONT_NORMAL));
77
            invoiceNumberKey.setBorder(Rectangle.NO_BORDER);
78
            PdfPCell invoiceNumberValue = new PdfPCell(new Paragraph(pdfModel.getInvoiceNumber(), FONT_NORMAL));
79
            invoiceNumberValue.setBorder(Rectangle.NO_BORDER);
80
            PdfPCell dateKey = new PdfPCell(new Paragraph("Date:", FONT_NORMAL));
81
            dateKey.setBorder(Rectangle.NO_BORDER);
82
            LocalDateTime now = LocalDateTime.now();
83
            //PdfPCell dateValue = new PdfPCell(new Paragraph("May 22, 2017", FONT_NORMAL));
84
            PdfPCell dateValue = new PdfPCell(new Paragraph(now.getMonth().name() + now.getDayOfMonth() + ", " + now.getYear(), FONT_NORMAL));
85
            dateValue.setBorder(Rectangle.NO_BORDER);
86
            tableInvoiceDate.addCell(invoiceNumberKey);
87
            //tableInvoiceDate.addCell(blankCell);
88
            tableInvoiceDate.addCell(invoiceNumberValue);
89
            tableInvoiceDate.addCell(dateKey);
90
            //tableInvoiceDate.addCell(blankCell);
91
            tableInvoiceDate.addCell(dateValue);
92
            tableInvoiceDateRetailer.addCell(tableInvoiceDate);
93
            tableInvoiceDateRetailer.addCell(columnRetailerInfo);
94
 
95
            tableCustomerRetailer.addCell(columnCustomerInfo);
96
            tableCustomerRetailer.addCell(blankCell);
97
            tableCustomerRetailer.addCell(tableInvoiceDateRetailer);
98
 
99
 
100
            PdfPTable orders = new PdfPTable(8);
101
            //PdfPCell orderDetail = new PdfPCell(new Paragraph("Order Details:"));
102
            /*PdfPTable ordersTable = new PdfPTable(8);
103
            PdfPCell srNo = new PdfPCell(new Paragraph("Sr No"));
104
            PdfPCell description = new PdfPCell(new Paragraph("Description"));
105
            PdfPCell quantity = new PdfPCell(new Paragraph("Quantity"));
106
            PdfPCell rate = new PdfPCell(new Paragraph("Rate (Rs)"));
107
            PdfPCell amount = new PdfPCell(new Paragraph("Amount (Rs)"));
108
            PdfPCell taxRate = new PdfPCell(new Paragraph("Tax Rate%"));
109
            PdfPCell tax = new PdfPCell(new Paragraph("Tax (Rs)"));
110
            PdfPCell itemTotal = new PdfPCell(new Paragraph("Item Total (Rs)"));
111
            srNo.setBorder(Rectangle.NO_BORDER);
112
            description.setBorder(Rectangle.NO_BORDER);
113
            quantity.setBorder(Rectangle.NO_BORDER);
114
            rate.setBorder(Rectangle.NO_BORDER);
115
            amount.setBorder(Rectangle.NO_BORDER);
116
            taxRate.setBorder(Rectangle.NO_BORDER);
117
            tax.setBorder(Rectangle.NO_BORDER);
118
            itemTotal.setBorder(Rectangle.NO_BORDER);*/
119
            orders.addCell(new Paragraph("Sr No", FONT_BOLD));
120
            orders.addCell(new Paragraph("Description", FONT_BOLD));
121
            orders.addCell(new Paragraph("Quantity", FONT_BOLD));
122
            orders.addCell(new Paragraph("Rate (Rs)", FONT_BOLD));
123
            orders.addCell(new Paragraph("Amount (Rs)", FONT_BOLD));
124
            orders.addCell(new Paragraph("Tax Rate%", FONT_BOLD));
125
            orders.addCell(new Paragraph("Tax (Rs)", FONT_BOLD));
126
            orders.addCell(new Paragraph("Item Total (Rs)", FONT_BOLD));
127
            orders.setWidths(new int[]{1, 3, 1, 1, 1, 1, 1, 1});
128
            orders.setHeaderRows(1);
129
            //orders.setSkipFirstHeader(true);
130
            int index = 1;
131
        	for(CustomFofoOrderItem orderItem : orderItems){
132
            	orders.addCell(new Paragraph(String.valueOf(index++), FONT_NORMAL));
133
            	orders.addCell(new Paragraph(orderItem.getDescription(), FONT_NORMAL));
134
            	orders.addCell(new Paragraph(String.valueOf(orderItem.getQuantity()), FONT_NORMAL));
135
            	orders.addCell(new Paragraph(String.valueOf(orderItem.getRate()), FONT_NORMAL));
136
            	orders.addCell(new Paragraph(String.valueOf(orderItem.getAmount()), FONT_NORMAL));
137
            	orders.addCell(new Paragraph(String.valueOf(orderItem.getTaxRate()), FONT_NORMAL));
138
            	orders.addCell(new Paragraph(String.valueOf(orderItem.getTax()), FONT_NORMAL));
139
            	orders.addCell(new Paragraph(String.valueOf(orderItem.getItemTotal()), FONT_NORMAL));
140
            }
141
            //orders.addCell("1");
142
            //orders.addCell("Sansui X71Activ Ta2s");
143
            //orders.setHeaderRows(1);
144
            //orders.getDefaultCell().setBorder(Rectangle.NO_BORDER);
145
            //orders.addCell(orderDetail);
146
            //orders.addCell(ordersTable);
147
            document.add(paragraphTitle);
148
            document.add(Chunk.NEWLINE);
149
            document.add(Chunk.NEWLINE);
150
            //document.add(paragraphRetailerName);
151
            document.add(tableCustomerRetailer);
152
 
153
            document.add(Chunk.NEWLINE);
154
            document.add(Chunk.NEWLINE);
155
            document.add(Chunk.NEWLINE);
156
            document.add(orders);
157
            PdfPTable grandTotalTable = new PdfPTable(3);
158
            grandTotalTable.setWidths(new int[]{8, 1, 1});
159
            /*for(int i = 0; i < 6; i++){
160
            	grandTotalTable.addCell(new Paragraph());
161
            }*/
162
            //PdfPCell grandTotalCell = new PdfPCell(new Paragraph("Grand total", fontBold));
163
            //grandTotalCell.setVerticalAlignment(Element.ALIGN_RIGHT);
164
            Paragraph grandTotalParagraph = new Paragraph("Grand total", FONT_BOLD);
165
            grandTotalParagraph.setIndentationRight(20);
166
            grandTotalTable.addCell(grandTotalParagraph);
167
            Paragraph rsParagraph = new Paragraph("Rs.", FONT_BOLD);
168
            grandTotalTable.addCell(rsParagraph);
169
            Paragraph amountParagraph = new Paragraph(String.valueOf(pdfModel.getTotalAmount()), FONT_BOLD);
170
            grandTotalTable.addCell(amountParagraph);
171
            //grandTotalCell.setColspan(6);
172
 
173
            document.add(grandTotalTable);
174
 
175
            PdfPTable amountInWordsTable = new PdfPTable(3);
176
            amountInWordsTable.setWidths(new int[]{1, 6, 3});
177
            amountInWordsTable.addCell(new Paragraph("Amount in Words:", FONT_BOLD));
178
            amountInWordsTable.addCell(new Paragraph("Rs. Four Thousand Nine Hundred Sixty-five and Zero paise", FONT_BOLD));
179
            amountInWordsTable.addCell(new Paragraph("E & O.E", FONT_NORMAL));
180
            document.add(amountInWordsTable);
181
 
182
            Paragraph warningParagraph = new Paragraph("Goods once sold will not be taken back.\nAll Disputes subject to "+pdfModel.getRetailer().getAddress().getState()+" Jurisdiction.\nThis is a Computer Generated Invoice.", FONT_NORMAL);
183
            warningParagraph.setIndentationLeft(50);
184
            document.add(Chunk.NEWLINE);
185
            document.add(warningParagraph);
186
            document.add(Chunk.NEWLINE);
187
            document.add(Chunk.NEWLINE);
188
 
189
            PdfPTable itemSerialNumbers = new PdfPTable(2);
190
            itemSerialNumbers.addCell(new Paragraph("Item Name", FONT_BOLD));
191
            itemSerialNumbers.addCell(new Paragraph("Serial Number", FONT_BOLD));
192
            itemSerialNumbers.setHeaderRows(1);
193
 
194
            for(CustomFofoOrderItem orderItem : orderItems){
195
            	itemSerialNumbers.addCell(new Paragraph(orderItem.getDescription(), FONT_NORMAL));
196
            	itemSerialNumbers.addCell(new Paragraph(orderItem.getSerialNumbers().toString(), FONT_NORMAL));
197
            }
198
 
199
            document.add(itemSerialNumbers);
200
            document.close(); // no need to close PDFwriter?
201
 
202
        } catch (DocumentException e) {
203
            LOGGER.error("Unable to write data to pdf file : ", e);
204
        }
205
	}
206
}