Subversion Repositories SmartDukaan

Rev

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