Subversion Repositories SmartDukaan

Rev

Rev 21926 | Rev 22025 | 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;
21915 ashik.ali 5
 
21686 ashik.ali 6
import java.time.LocalDateTime;
21915 ashik.ali 7
import java.util.Locale;
21686 ashik.ali 8
import java.util.Set;
9
 
10
import org.slf4j.Logger;
11
import org.slf4j.LoggerFactory;
12
 
21915 ashik.ali 13
import com.ibm.icu.text.RuleBasedNumberFormat;
21686 ashik.ali 14
import com.itextpdf.text.Chunk;
15
import com.itextpdf.text.Document;
16
import com.itextpdf.text.DocumentException;
17
import com.itextpdf.text.Element;
18
import com.itextpdf.text.Font;
19
import com.itextpdf.text.Paragraph;
20
import com.itextpdf.text.Rectangle;
21
import com.itextpdf.text.pdf.PdfPCell;
22
import com.itextpdf.text.pdf.PdfPTable;
23
import com.itextpdf.text.pdf.PdfWriter;
24
import com.spice.profitmandi.common.model.CustomCustomer;
25
import com.spice.profitmandi.common.model.CustomFofoOrderItem;
26
import com.spice.profitmandi.common.model.CustomRetailer;
27
import com.spice.profitmandi.common.model.PdfModel;
21915 ashik.ali 28
import org.apache.commons.lang3.text.*;
21686 ashik.ali 29
 
30
public class PdfUtils {
31
 
32
	private static final Font FONT_TITLE = new Font(Font.FontFamily.HELVETICA  , 18, Font.BOLD);
21965 ashik.ali 33
	private static Font FONT_NORMAL = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL);
34
	private static Font FONT_BOLD = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD);
21686 ashik.ali 35
	//private static Font fontTableHeader = new Font(Font.FontFamily.TIMES_ROMAN, 14, Font.BOLD);
21926 ashik.ali 36
	private static final String INVOICE_TITLE = "INVOICE";
21686 ashik.ali 37
 
21915 ashik.ali 38
	private static final Locale indianLocale = Locale.getDefault();
39
 
21686 ashik.ali 40
	private static final Logger LOGGER = LoggerFactory.getLogger(PdfUtils.class);
41
 
42
	public static void generateAndWrite(PdfModel pdfModel, OutputStream outputStream){
43
		Document document = new Document();
21926 ashik.ali 44
		document.setMargins(0, 0, 25, 0);
21686 ashik.ali 45
        try {
46
        	CustomCustomer customer = pdfModel.getCustomer();
47
        	CustomRetailer retailer = pdfModel.getRetailer();
21901 ashik.ali 48
        	boolean gstWithInState = false;
49
        	String customerAddressStateCode = "", retailerAddressStateCode = "";
50
        	if(customer.getAddress().getState().equals(retailer.getAddress().getState())){
51
        		gstWithInState = true;
52
        		customerAddressStateCode = Utils.getStateCode(customer.getAddress().getState());
53
        	}else{
54
        		customerAddressStateCode = Utils.getStateCode(customer.getAddress().getState());
55
        		retailerAddressStateCode = Utils.getStateCode(retailer.getAddress().getState());
56
        	}
21686 ashik.ali 57
        	Set<CustomFofoOrderItem> orderItems = pdfModel.getOrderItems();
58
 
59
            PdfWriter.getInstance(document,outputStream);
60
 
61
            document.open();
62
            document.addTitle(pdfModel.getTitle());
63
            document.addAuthor(pdfModel.getAuther());
64
 
65
            Paragraph paragraphTitle = new Paragraph(INVOICE_TITLE, FONT_TITLE);
66
            paragraphTitle.setAlignment(Element.ALIGN_CENTER);
67
 
68
            PdfPCell blankCell = new PdfPCell();
69
            blankCell.setBorder(Rectangle.NO_BORDER);
70
            PdfPTable tableCustomerRetailer = new PdfPTable(3);
71
            tableCustomerRetailer.getDefaultCell().setBorder(Rectangle.NO_BORDER);
72
            PdfPCell columnCustomerInfo = new PdfPCell();
73
            columnCustomerInfo.addElement(new Paragraph(customer.getName(), FONT_NORMAL));
21965 ashik.ali 74
            columnCustomerInfo.addElement(new Paragraph(customer.getAddress().getLine1() + ", " + customer.getAddress().getLine2(), FONT_NORMAL));
21901 ashik.ali 75
            columnCustomerInfo.addElement(new Paragraph(customer.getAddress().getCity() + ", " + customer.getAddress().getState() + "(" + customerAddressStateCode + ")" + "\n" + customer.getAddress().getPinCode(), FONT_NORMAL));
21686 ashik.ali 76
            columnCustomerInfo.addElement(new Paragraph(customer.getMobileNumber(), FONT_NORMAL));
77
            columnCustomerInfo.setBorder(Rectangle.NO_BORDER);
78
            PdfPCell columnRetailerInfo = new PdfPCell();
79
            //columnRetailerInfo.addElement(new Paragraph("Invoice No:"));
80
            columnRetailerInfo.addElement(new Paragraph(retailer.getBusinessName(), FONT_BOLD));
81
           // columnRetailerInfo.addElement(new Paragraph("Plot No. 485, Udyog Vihar Phase V, Gurgoan-122016", FONT_BOLD));
21915 ashik.ali 82
            columnRetailerInfo.addElement(new Paragraph(retailer.getAddress().getLine1() + ", " + retailer.getAddress().getLine2() + ", " + retailer.getAddress().getCity() + "-" + retailer.getAddress().getPinCode() + ", " + retailer.getAddress().getState() + "(" + (gstWithInState? customerAddressStateCode : retailerAddressStateCode) + ")", FONT_BOLD));
21686 ashik.ali 83
            columnRetailerInfo.addElement(new Paragraph("Contact No.- "+retailer.getMobileNumber(), FONT_BOLD));
84
            columnRetailerInfo.addElement(new Paragraph("TIN NO. " + retailer.getTinNumber(), FONT_BOLD));
85
            columnRetailerInfo.setBorder(Rectangle.NO_BORDER);
86
            PdfPTable tableInvoiceDateRetailer = new PdfPTable(1);
87
            tableInvoiceDateRetailer.getDefaultCell().setBorder(Rectangle.NO_BORDER);
88
            PdfPTable tableInvoiceDate = new PdfPTable(2);
89
            tableInvoiceDate.getDefaultCell().setBorder(Rectangle.NO_BORDER);
90
            PdfPCell invoiceNumberKey = new PdfPCell(new Paragraph("Invoice No:", FONT_NORMAL));
91
            invoiceNumberKey.setBorder(Rectangle.NO_BORDER);
92
            PdfPCell invoiceNumberValue = new PdfPCell(new Paragraph(pdfModel.getInvoiceNumber(), FONT_NORMAL));
93
            invoiceNumberValue.setBorder(Rectangle.NO_BORDER);
94
            PdfPCell dateKey = new PdfPCell(new Paragraph("Date:", FONT_NORMAL));
95
            dateKey.setBorder(Rectangle.NO_BORDER);
96
            LocalDateTime now = LocalDateTime.now();
97
            //PdfPCell dateValue = new PdfPCell(new Paragraph("May 22, 2017", FONT_NORMAL));
98
            PdfPCell dateValue = new PdfPCell(new Paragraph(now.getMonth().name() + now.getDayOfMonth() + ", " + now.getYear(), FONT_NORMAL));
99
            dateValue.setBorder(Rectangle.NO_BORDER);
100
            tableInvoiceDate.addCell(invoiceNumberKey);
101
            //tableInvoiceDate.addCell(blankCell);
102
            tableInvoiceDate.addCell(invoiceNumberValue);
103
            tableInvoiceDate.addCell(dateKey);
104
            //tableInvoiceDate.addCell(blankCell);
105
            tableInvoiceDate.addCell(dateValue);
106
            tableInvoiceDateRetailer.addCell(tableInvoiceDate);
107
            tableInvoiceDateRetailer.addCell(columnRetailerInfo);
108
 
109
            tableCustomerRetailer.addCell(columnCustomerInfo);
110
            tableCustomerRetailer.addCell(blankCell);
111
            tableCustomerRetailer.addCell(tableInvoiceDateRetailer);
112
 
21901 ashik.ali 113
            PdfPTable orders = null;
114
            if(gstWithInState){
21915 ashik.ali 115
            	orders = new PdfPTable(8);
21901 ashik.ali 116
            }else{
21915 ashik.ali 117
            	orders = new PdfPTable(10);
21901 ashik.ali 118
            }
21686 ashik.ali 119
            //PdfPCell orderDetail = new PdfPCell(new Paragraph("Order Details:"));
120
            /*PdfPTable ordersTable = new PdfPTable(8);
121
            PdfPCell srNo = new PdfPCell(new Paragraph("Sr No"));
122
            PdfPCell description = new PdfPCell(new Paragraph("Description"));
123
            PdfPCell quantity = new PdfPCell(new Paragraph("Quantity"));
124
            PdfPCell rate = new PdfPCell(new Paragraph("Rate (Rs)"));
125
            PdfPCell amount = new PdfPCell(new Paragraph("Amount (Rs)"));
126
            PdfPCell taxRate = new PdfPCell(new Paragraph("Tax Rate%"));
127
            PdfPCell tax = new PdfPCell(new Paragraph("Tax (Rs)"));
128
            PdfPCell itemTotal = new PdfPCell(new Paragraph("Item Total (Rs)"));
129
            srNo.setBorder(Rectangle.NO_BORDER);
130
            description.setBorder(Rectangle.NO_BORDER);
131
            quantity.setBorder(Rectangle.NO_BORDER);
132
            rate.setBorder(Rectangle.NO_BORDER);
133
            amount.setBorder(Rectangle.NO_BORDER);
134
            taxRate.setBorder(Rectangle.NO_BORDER);
135
            tax.setBorder(Rectangle.NO_BORDER);
136
            itemTotal.setBorder(Rectangle.NO_BORDER);*/
137
            orders.addCell(new Paragraph("Sr No", FONT_BOLD));
138
            orders.addCell(new Paragraph("Description", FONT_BOLD));
21901 ashik.ali 139
            orders.addCell(new Paragraph("HSN Code", FONT_BOLD));
21686 ashik.ali 140
            orders.addCell(new Paragraph("Quantity", FONT_BOLD));
141
            orders.addCell(new Paragraph("Rate (Rs)", FONT_BOLD));
142
            orders.addCell(new Paragraph("Amount (Rs)", FONT_BOLD));
21901 ashik.ali 143
            if(gstWithInState){
144
            	orders.addCell(new Paragraph("IGST Rate%", FONT_BOLD));
145
                orders.addCell(new Paragraph("IGST Amount", FONT_BOLD));
21915 ashik.ali 146
                orders.setWidths(new int[]{1, 3, 1, 1, 1, 1, 1, 1});
21901 ashik.ali 147
            }else{
148
            	orders.addCell(new Paragraph("CGST Rate%", FONT_BOLD));
149
                orders.addCell(new Paragraph("CGST Amount", FONT_BOLD));
150
                orders.addCell(new Paragraph("SGST Rate%", FONT_BOLD));
151
                orders.addCell(new Paragraph("SGST Amount", FONT_BOLD));
21915 ashik.ali 152
                orders.setWidths(new int[]{1, 3, 1, 1, 1, 1, 1, 1, 1, 1});
21901 ashik.ali 153
            }
154
 
155
            //orders.addCell(new Paragraph("Item Total (Rs)", FONT_BOLD));
156
 
21686 ashik.ali 157
            orders.setHeaderRows(1);
158
            //orders.setSkipFirstHeader(true);
21901 ashik.ali 159
 
21926 ashik.ali 160
            float igstTotalAmount = 0, cgstTotalAmount = 0, sgstTotalAmount = 0;
21686 ashik.ali 161
            int index = 1;
162
        	for(CustomFofoOrderItem orderItem : orderItems){
163
            	orders.addCell(new Paragraph(String.valueOf(index++), FONT_NORMAL));
164
            	orders.addCell(new Paragraph(orderItem.getDescription(), FONT_NORMAL));
21901 ashik.ali 165
            	orders.addCell(new Paragraph(orderItem.getHsnCode(), FONT_NORMAL));
21686 ashik.ali 166
            	orders.addCell(new Paragraph(String.valueOf(orderItem.getQuantity()), FONT_NORMAL));
167
            	orders.addCell(new Paragraph(String.valueOf(orderItem.getRate()), FONT_NORMAL));
21926 ashik.ali 168
            	orders.addCell(new Paragraph(String.format("%.2f", orderItem.getAmount()), FONT_NORMAL));
21901 ashik.ali 169
            	if(gstWithInState){
21926 ashik.ali 170
            		orders.addCell(new Paragraph(String.format("%.2f", orderItem.getIgstRate()), FONT_NORMAL));
171
            		orders.addCell(new Paragraph(String.format("%.2f", orderItem.getIgstAmount()), FONT_NORMAL));
172
            		igstTotalAmount = igstTotalAmount + orderItem.getIgstAmount();
21901 ashik.ali 173
            	}else{
21926 ashik.ali 174
            		orders.addCell(new Paragraph(String.format("%.2f", orderItem.getCgstRate()), FONT_NORMAL));
175
            		orders.addCell(new Paragraph(String.format("%.2f", orderItem.getCgstAmount()), FONT_NORMAL));
176
            		orders.addCell(new Paragraph(String.format("%.2f", orderItem.getSgstRate()), FONT_NORMAL));
177
            		orders.addCell(new Paragraph(String.format("%.2f", orderItem.getSgstAmount()), FONT_NORMAL));
178
            		cgstTotalAmount = cgstTotalAmount + orderItem.getCgstAmount();
179
            		sgstTotalAmount = sgstTotalAmount + orderItem.getSgstAmount();
21901 ashik.ali 180
            	}
21926 ashik.ali 181
            	orders.addCell(new Paragraph(String.format("%.2f", orderItem.getItemTotal()), FONT_NORMAL));
21686 ashik.ali 182
            }
183
            //orders.addCell("1");
184
            //orders.addCell("Sansui X71Activ Ta2s");
185
            //orders.setHeaderRows(1);
186
            //orders.getDefaultCell().setBorder(Rectangle.NO_BORDER);
187
            //orders.addCell(orderDetail);
188
            //orders.addCell(ordersTable);
189
            document.add(paragraphTitle);
190
            document.add(Chunk.NEWLINE);
191
            document.add(Chunk.NEWLINE);
192
            //document.add(paragraphRetailerName);
193
            document.add(tableCustomerRetailer);
194
 
21965 ashik.ali 195
            //document.add(Chunk.NEWLINE);
196
            //document.add(Chunk.NEWLINE);
21686 ashik.ali 197
            document.add(Chunk.NEWLINE);
198
            document.add(orders);
21926 ashik.ali 199
 
200
 
201
 
202
 
203
            PdfPTable gstTotalTable = null;
204
            if(gstWithInState){
205
            	gstTotalTable = new PdfPTable(3);
206
            	gstTotalTable.setWidths(new int[]{8, 1, 1});
207
            }else{
208
            	gstTotalTable = new PdfPTable(5);
209
            	gstTotalTable.setWidths(new int[]{8, 1, 1, 1, 1});
210
            }
211
 
212
            /*for(int i = 0; i < 6; i++){
213
            	grandTotalTable.addCell(new Paragraph());
214
            }*/
215
            //PdfPCell grandTotalCell = new PdfPCell(new Paragraph("Grand total", fontBold));
216
            //grandTotalCell.setVerticalAlignment(Element.ALIGN_RIGHT);
217
            Paragraph gstTotalParagraph = new Paragraph("Gst Total", FONT_BOLD);
218
            gstTotalParagraph.setIndentationRight(20);
219
            gstTotalTable.addCell(gstTotalParagraph);
220
            if(gstWithInState){
221
            	gstTotalTable.addCell(new Paragraph("IGST", FONT_BOLD));
222
            	gstTotalTable.addCell(new Paragraph(String.format("%.2f", igstTotalAmount), FONT_BOLD));
223
            }else{
224
            	gstTotalTable.addCell(new Paragraph("CGST", FONT_BOLD));
225
            	gstTotalTable.addCell(new Paragraph(String.format("%.2f", cgstTotalAmount), FONT_BOLD));
226
            	gstTotalTable.addCell(new Paragraph("SGST", FONT_BOLD));
227
            	gstTotalTable.addCell(new Paragraph(String.format("%.2f", sgstTotalAmount), FONT_BOLD));
228
            }
229
 
230
            //grandTotalCell.setColspan(6);
231
            document.add(gstTotalTable);
232
 
21965 ashik.ali 233
            PdfPTable grandTotalTable = new PdfPTable(3);
21901 ashik.ali 234
            if(gstWithInState){
235
            	grandTotalTable.setWidths(new int[]{8, 1, 1});
236
            }else{
21965 ashik.ali 237
            	grandTotalTable.setWidths(new int[]{10, 1, 1});
21901 ashik.ali 238
            }
239
 
21965 ashik.ali 240
 
21686 ashik.ali 241
            Paragraph grandTotalParagraph = new Paragraph("Grand total", FONT_BOLD);
242
            grandTotalParagraph.setIndentationRight(20);
243
            grandTotalTable.addCell(grandTotalParagraph);
244
            Paragraph rsParagraph = new Paragraph("Rs.", FONT_BOLD);
245
            grandTotalTable.addCell(rsParagraph);
21926 ashik.ali 246
            Paragraph amountParagraph = new Paragraph(String.format("%.2f", pdfModel.getTotalAmount()), FONT_BOLD);
21686 ashik.ali 247
            grandTotalTable.addCell(amountParagraph);
248
 
21926 ashik.ali 249
 
21686 ashik.ali 250
            document.add(grandTotalTable);
251
 
252
            PdfPTable amountInWordsTable = new PdfPTable(3);
21965 ashik.ali 253
            if(gstWithInState){
254
            	amountInWordsTable.setWidths(new int[]{1, 6, 3});
255
            }else{
256
            	amountInWordsTable.setWidths(new int[]{1, 8, 3});
257
            }
21686 ashik.ali 258
            amountInWordsTable.addCell(new Paragraph("Amount in Words:", FONT_BOLD));
21915 ashik.ali 259
 
260
            String amountInWords = toAmountInWords(pdfModel.getTotalAmount());
261
           	amountInWordsTable.addCell(new Paragraph(amountInWords.toString(), FONT_BOLD));
21686 ashik.ali 262
            amountInWordsTable.addCell(new Paragraph("E & O.E", FONT_NORMAL));
21915 ashik.ali 263
            document.add(amountInWordsTable);            
21686 ashik.ali 264
 
21965 ashik.ali 265
            Paragraph warningParagraph = new Paragraph("Goods once sold will not be taken back.\nThis is a Computer Generated Invoice.", FONT_NORMAL);
21686 ashik.ali 266
            warningParagraph.setIndentationLeft(50);
267
            document.add(Chunk.NEWLINE);
268
            document.add(warningParagraph);
269
            document.add(Chunk.NEWLINE);
270
            document.add(Chunk.NEWLINE);
271
 
272
            PdfPTable itemSerialNumbers = new PdfPTable(2);
273
            itemSerialNumbers.addCell(new Paragraph("Item Name", FONT_BOLD));
274
            itemSerialNumbers.addCell(new Paragraph("Serial Number", FONT_BOLD));
275
            itemSerialNumbers.setHeaderRows(1);
276
 
277
            for(CustomFofoOrderItem orderItem : orderItems){
278
            	itemSerialNumbers.addCell(new Paragraph(orderItem.getDescription(), FONT_NORMAL));
279
            	itemSerialNumbers.addCell(new Paragraph(orderItem.getSerialNumbers().toString(), FONT_NORMAL));
280
            }
281
 
282
            document.add(itemSerialNumbers);
283
            document.close(); // no need to close PDFwriter?
284
 
285
        } catch (DocumentException e) {
286
            LOGGER.error("Unable to write data to pdf file : ", e);
21901 ashik.ali 287
        } catch (Exception e) {
288
			// TODO Auto-generated catch block
289
			e.printStackTrace();
290
		}
21686 ashik.ali 291
	}
21915 ashik.ali 292
 
293
	@SuppressWarnings("deprecation")
294
	private static String toAmountInWords(float amount){
295
		RuleBasedNumberFormat amountInWordsFormat = new RuleBasedNumberFormat(indianLocale, RuleBasedNumberFormat.SPELLOUT);
296
       	StringBuilder amountInWords = new StringBuilder("Rs. ");
297
        amountInWords.append(WordUtils.capitalize(amountInWordsFormat.format((int)amount)));
298
       	amountInWords.append(" and ");
299
        amountInWords.append(WordUtils.capitalize(amountInWordsFormat.format((int)(amount*100)%100)));
300
       	amountInWords.append(" paise");
301
       	return amountInWords.toString();
302
	}
21686 ashik.ali 303
}