Subversion Repositories SmartDukaan

Rev

Rev 21866 | Rev 21915 | 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();
21901 ashik.ali 43
        	boolean gstWithInState = false;
44
        	String customerAddressStateCode = "", retailerAddressStateCode = "";
45
        	if(customer.getAddress().getState().equals(retailer.getAddress().getState())){
46
        		gstWithInState = true;
47
        		customerAddressStateCode = Utils.getStateCode(customer.getAddress().getState());
48
        	}else{
49
        		customerAddressStateCode = Utils.getStateCode(customer.getAddress().getState());
50
        		retailerAddressStateCode = Utils.getStateCode(retailer.getAddress().getState());
51
        	}
21686 ashik.ali 52
        	Set<CustomFofoOrderItem> orderItems = pdfModel.getOrderItems();
53
 
54
            PdfWriter.getInstance(document,outputStream);
55
 
56
            document.open();
57
            document.addTitle(pdfModel.getTitle());
58
            document.addAuthor(pdfModel.getAuther());
59
 
60
            Paragraph paragraphTitle = new Paragraph(INVOICE_TITLE, FONT_TITLE);
61
            paragraphTitle.setAlignment(Element.ALIGN_CENTER);
62
 
63
            PdfPCell blankCell = new PdfPCell();
64
            blankCell.setBorder(Rectangle.NO_BORDER);
65
            PdfPTable tableCustomerRetailer = new PdfPTable(3);
66
            tableCustomerRetailer.getDefaultCell().setBorder(Rectangle.NO_BORDER);
67
            PdfPCell columnCustomerInfo = new PdfPCell();
68
            columnCustomerInfo.addElement(new Paragraph(customer.getName(), FONT_NORMAL));
69
            columnCustomerInfo.addElement(new Paragraph(customer.getAddress().getLine1() + customer.getAddress().getLine2(), FONT_NORMAL));
21901 ashik.ali 70
            columnCustomerInfo.addElement(new Paragraph(customer.getAddress().getCity() + ", " + customer.getAddress().getState() + "(" + customerAddressStateCode + ")" + "\n" + customer.getAddress().getPinCode(), FONT_NORMAL));
21686 ashik.ali 71
            columnCustomerInfo.addElement(new Paragraph(customer.getMobileNumber(), FONT_NORMAL));
72
            columnCustomerInfo.setBorder(Rectangle.NO_BORDER);
73
            PdfPCell columnRetailerInfo = new PdfPCell();
74
            //columnRetailerInfo.addElement(new Paragraph("Invoice No:"));
75
            columnRetailerInfo.addElement(new Paragraph(retailer.getBusinessName(), FONT_BOLD));
76
           // columnRetailerInfo.addElement(new Paragraph("Plot No. 485, Udyog Vihar Phase V, Gurgoan-122016", FONT_BOLD));
21901 ashik.ali 77
            columnRetailerInfo.addElement(new Paragraph(retailer.getAddress().getLine1() + ", " + retailer.getAddress().getLine2() + ", " + retailer.getAddress().getCity() + "-" + retailer.getAddress().getPinCode() + ", " + retailer.getAddress().getState() + "(" + retailerAddressStateCode + ")", FONT_BOLD));
21686 ashik.ali 78
            columnRetailerInfo.addElement(new Paragraph("Contact No.- "+retailer.getMobileNumber(), FONT_BOLD));
79
            columnRetailerInfo.addElement(new Paragraph("TIN NO. " + retailer.getTinNumber(), FONT_BOLD));
80
            columnRetailerInfo.setBorder(Rectangle.NO_BORDER);
81
            PdfPTable tableInvoiceDateRetailer = new PdfPTable(1);
82
            tableInvoiceDateRetailer.getDefaultCell().setBorder(Rectangle.NO_BORDER);
83
            PdfPTable tableInvoiceDate = new PdfPTable(2);
84
            tableInvoiceDate.getDefaultCell().setBorder(Rectangle.NO_BORDER);
85
            PdfPCell invoiceNumberKey = new PdfPCell(new Paragraph("Invoice No:", FONT_NORMAL));
86
            invoiceNumberKey.setBorder(Rectangle.NO_BORDER);
87
            PdfPCell invoiceNumberValue = new PdfPCell(new Paragraph(pdfModel.getInvoiceNumber(), FONT_NORMAL));
88
            invoiceNumberValue.setBorder(Rectangle.NO_BORDER);
89
            PdfPCell dateKey = new PdfPCell(new Paragraph("Date:", FONT_NORMAL));
90
            dateKey.setBorder(Rectangle.NO_BORDER);
91
            LocalDateTime now = LocalDateTime.now();
92
            //PdfPCell dateValue = new PdfPCell(new Paragraph("May 22, 2017", FONT_NORMAL));
93
            PdfPCell dateValue = new PdfPCell(new Paragraph(now.getMonth().name() + now.getDayOfMonth() + ", " + now.getYear(), FONT_NORMAL));
94
            dateValue.setBorder(Rectangle.NO_BORDER);
95
            tableInvoiceDate.addCell(invoiceNumberKey);
96
            //tableInvoiceDate.addCell(blankCell);
97
            tableInvoiceDate.addCell(invoiceNumberValue);
98
            tableInvoiceDate.addCell(dateKey);
99
            //tableInvoiceDate.addCell(blankCell);
100
            tableInvoiceDate.addCell(dateValue);
101
            tableInvoiceDateRetailer.addCell(tableInvoiceDate);
102
            tableInvoiceDateRetailer.addCell(columnRetailerInfo);
103
 
104
            tableCustomerRetailer.addCell(columnCustomerInfo);
105
            tableCustomerRetailer.addCell(blankCell);
106
            tableCustomerRetailer.addCell(tableInvoiceDateRetailer);
107
 
21901 ashik.ali 108
            PdfPTable orders = null;
109
            if(gstWithInState){
110
            	orders = new PdfPTable(9);
111
            }else{
112
            	orders = new PdfPTable(11);
113
            }
21686 ashik.ali 114
            //PdfPCell orderDetail = new PdfPCell(new Paragraph("Order Details:"));
115
            /*PdfPTable ordersTable = new PdfPTable(8);
116
            PdfPCell srNo = new PdfPCell(new Paragraph("Sr No"));
117
            PdfPCell description = new PdfPCell(new Paragraph("Description"));
118
            PdfPCell quantity = new PdfPCell(new Paragraph("Quantity"));
119
            PdfPCell rate = new PdfPCell(new Paragraph("Rate (Rs)"));
120
            PdfPCell amount = new PdfPCell(new Paragraph("Amount (Rs)"));
121
            PdfPCell taxRate = new PdfPCell(new Paragraph("Tax Rate%"));
122
            PdfPCell tax = new PdfPCell(new Paragraph("Tax (Rs)"));
123
            PdfPCell itemTotal = new PdfPCell(new Paragraph("Item Total (Rs)"));
124
            srNo.setBorder(Rectangle.NO_BORDER);
125
            description.setBorder(Rectangle.NO_BORDER);
126
            quantity.setBorder(Rectangle.NO_BORDER);
127
            rate.setBorder(Rectangle.NO_BORDER);
128
            amount.setBorder(Rectangle.NO_BORDER);
129
            taxRate.setBorder(Rectangle.NO_BORDER);
130
            tax.setBorder(Rectangle.NO_BORDER);
131
            itemTotal.setBorder(Rectangle.NO_BORDER);*/
132
            orders.addCell(new Paragraph("Sr No", FONT_BOLD));
133
            orders.addCell(new Paragraph("Description", FONT_BOLD));
21901 ashik.ali 134
            orders.addCell(new Paragraph("HSN Code", FONT_BOLD));
21686 ashik.ali 135
            orders.addCell(new Paragraph("Quantity", FONT_BOLD));
136
            orders.addCell(new Paragraph("Rate (Rs)", FONT_BOLD));
137
            orders.addCell(new Paragraph("Amount (Rs)", FONT_BOLD));
21901 ashik.ali 138
            if(gstWithInState){
139
            	orders.addCell(new Paragraph("IGST Rate%", FONT_BOLD));
140
                orders.addCell(new Paragraph("IGST Amount", FONT_BOLD));
141
                orders.setWidths(new int[]{1, 3, 1, 1, 1, 1, 1, 1, 1});
142
            }else{
143
            	orders.addCell(new Paragraph("CGST Rate%", FONT_BOLD));
144
                orders.addCell(new Paragraph("CGST Amount", FONT_BOLD));
145
                orders.addCell(new Paragraph("SGST Rate%", FONT_BOLD));
146
                orders.addCell(new Paragraph("SGST Amount", FONT_BOLD));
147
                orders.setWidths(new int[]{1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1});
148
            }
149
 
150
            //orders.addCell(new Paragraph("Item Total (Rs)", FONT_BOLD));
151
 
21686 ashik.ali 152
            orders.setHeaderRows(1);
153
            //orders.setSkipFirstHeader(true);
21901 ashik.ali 154
 
21686 ashik.ali 155
            int index = 1;
156
        	for(CustomFofoOrderItem orderItem : orderItems){
157
            	orders.addCell(new Paragraph(String.valueOf(index++), FONT_NORMAL));
158
            	orders.addCell(new Paragraph(orderItem.getDescription(), FONT_NORMAL));
21901 ashik.ali 159
            	orders.addCell(new Paragraph(orderItem.getHsnCode(), FONT_NORMAL));
21686 ashik.ali 160
            	orders.addCell(new Paragraph(String.valueOf(orderItem.getQuantity()), FONT_NORMAL));
161
            	orders.addCell(new Paragraph(String.valueOf(orderItem.getRate()), FONT_NORMAL));
162
            	orders.addCell(new Paragraph(String.valueOf(orderItem.getAmount()), FONT_NORMAL));
21901 ashik.ali 163
            	float totalTaxRate = orderItem.getIgstRate() + orderItem.getSgstRate() + orderItem.getCgstRate();
164
    			float taxableAmount = pdfModel.getTotalAmount() / (1 + (totalTaxRate / 100));
165
            	if(gstWithInState){
166
            		float igstAmount = (taxableAmount * orderItem.getIgstRate()) / 100;
167
            		orders.addCell(new Paragraph(String.valueOf(orderItem.getIgstRate()), FONT_NORMAL));
168
            		orders.addCell(new Paragraph(String.valueOf(igstAmount), FONT_NORMAL));
169
            	}else{
170
            		float cgstAmount = (taxableAmount * orderItem.getCgstRate()) / 100;
171
            		float sgstAmount = (taxableAmount * orderItem.getSgstRate()) / 100;
172
            		orders.addCell(new Paragraph(String.valueOf(orderItem.getCgstRate()), FONT_NORMAL));
173
            		orders.addCell(new Paragraph(String.valueOf(cgstAmount), FONT_NORMAL));
174
            		orders.addCell(new Paragraph(String.valueOf(orderItem.getSgstRate()), FONT_NORMAL));
175
            		orders.addCell(new Paragraph(String.valueOf(sgstAmount), FONT_NORMAL));
176
            	}
21686 ashik.ali 177
            	orders.addCell(new Paragraph(String.valueOf(orderItem.getItemTotal()), FONT_NORMAL));
178
            }
179
            //orders.addCell("1");
180
            //orders.addCell("Sansui X71Activ Ta2s");
181
            //orders.setHeaderRows(1);
182
            //orders.getDefaultCell().setBorder(Rectangle.NO_BORDER);
183
            //orders.addCell(orderDetail);
184
            //orders.addCell(ordersTable);
185
            document.add(paragraphTitle);
186
            document.add(Chunk.NEWLINE);
187
            document.add(Chunk.NEWLINE);
188
            //document.add(paragraphRetailerName);
189
            document.add(tableCustomerRetailer);
190
 
191
            document.add(Chunk.NEWLINE);
192
            document.add(Chunk.NEWLINE);
193
            document.add(Chunk.NEWLINE);
194
            document.add(orders);
21901 ashik.ali 195
            PdfPTable grandTotalTable = null;
196
            if(gstWithInState){
197
            	grandTotalTable = new PdfPTable(3);
198
            	grandTotalTable.setWidths(new int[]{8, 1, 1});
199
            }else{
200
            	grandTotalTable = new PdfPTable(5);
201
            	grandTotalTable.setWidths(new int[]{8, 1, 1, 1, 1});
202
            }
203
 
21686 ashik.ali 204
            /*for(int i = 0; i < 6; i++){
205
            	grandTotalTable.addCell(new Paragraph());
206
            }*/
207
            //PdfPCell grandTotalCell = new PdfPCell(new Paragraph("Grand total", fontBold));
208
            //grandTotalCell.setVerticalAlignment(Element.ALIGN_RIGHT);
209
            Paragraph grandTotalParagraph = new Paragraph("Grand total", FONT_BOLD);
210
            grandTotalParagraph.setIndentationRight(20);
211
            grandTotalTable.addCell(grandTotalParagraph);
212
            Paragraph rsParagraph = new Paragraph("Rs.", FONT_BOLD);
213
            grandTotalTable.addCell(rsParagraph);
214
            Paragraph amountParagraph = new Paragraph(String.valueOf(pdfModel.getTotalAmount()), FONT_BOLD);
215
            grandTotalTable.addCell(amountParagraph);
216
            //grandTotalCell.setColspan(6);
217
 
218
            document.add(grandTotalTable);
219
 
220
            PdfPTable amountInWordsTable = new PdfPTable(3);
221
            amountInWordsTable.setWidths(new int[]{1, 6, 3});
222
            amountInWordsTable.addCell(new Paragraph("Amount in Words:", FONT_BOLD));
223
            amountInWordsTable.addCell(new Paragraph("Rs. Four Thousand Nine Hundred Sixty-five and Zero paise", FONT_BOLD));
224
            amountInWordsTable.addCell(new Paragraph("E & O.E", FONT_NORMAL));
225
            document.add(amountInWordsTable);
226
 
227
            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);
228
            warningParagraph.setIndentationLeft(50);
229
            document.add(Chunk.NEWLINE);
230
            document.add(warningParagraph);
231
            document.add(Chunk.NEWLINE);
232
            document.add(Chunk.NEWLINE);
233
 
234
            PdfPTable itemSerialNumbers = new PdfPTable(2);
235
            itemSerialNumbers.addCell(new Paragraph("Item Name", FONT_BOLD));
236
            itemSerialNumbers.addCell(new Paragraph("Serial Number", FONT_BOLD));
237
            itemSerialNumbers.setHeaderRows(1);
238
 
239
            for(CustomFofoOrderItem orderItem : orderItems){
240
            	itemSerialNumbers.addCell(new Paragraph(orderItem.getDescription(), FONT_NORMAL));
241
            	itemSerialNumbers.addCell(new Paragraph(orderItem.getSerialNumbers().toString(), FONT_NORMAL));
242
            }
243
 
244
            document.add(itemSerialNumbers);
245
            document.close(); // no need to close PDFwriter?
246
 
247
        } catch (DocumentException e) {
248
            LOGGER.error("Unable to write data to pdf file : ", e);
21901 ashik.ali 249
        } catch (Exception e) {
250
			// TODO Auto-generated catch block
251
			e.printStackTrace();
252
		}
21686 ashik.ali 253
	}
254
}