Subversion Repositories SmartDukaan

Rev

Rev 23380 | Rev 23532 | 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
import java.io.OutputStream;
23001 amit.gupta 4
import java.util.List;
21915 ashik.ali 5
import java.util.Locale;
21686 ashik.ali 6
import java.util.Set;
7
 
8
import org.slf4j.Logger;
9
import org.slf4j.LoggerFactory;
22068 ashik.ali 10
import org.springframework.util.StringUtils;
21686 ashik.ali 11
 
21915 ashik.ali 12
import com.ibm.icu.text.RuleBasedNumberFormat;
21686 ashik.ali 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;
23001 amit.gupta 24
import com.spice.profitmandi.common.model.CustomInsurancePolicy;
22858 ashik.ali 25
import com.spice.profitmandi.common.model.CustomOrderItem;
21686 ashik.ali 26
import com.spice.profitmandi.common.model.CustomRetailer;
23509 amit.gupta 27
import com.spice.profitmandi.common.model.DebitNotePdfModel;
21686 ashik.ali 28
import com.spice.profitmandi.common.model.PdfModel;
29
 
30
public class PdfUtils {
23509 amit.gupta 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);
23509 amit.gupta 35
	// private static Font fontTableHeader = new
36
	// Font(Font.FontFamily.TIMES_ROMAN, 14, Font.BOLD);
23374 amit.gupta 37
	private static final String INVOICE_TITLE = "TAX INVOICE";
23509 amit.gupta 38
 
39
	private static float[] igstWidthsWithDiscount = new float[] { .3f, 2.6f, 0.7f, .4f, 0.7f, 0.5f, .7f, .6f, 0.6f,
40
			0.9f };
41
	private static float[] stateWidthsWithDiscount = new float[] { .2f, 2.1f, 0.7f, .3f, 0.6f, 0.4f, .7f, .5f, .6f, .5f,
42
			.6f, .8f };
43
 
21915 ashik.ali 44
	private static final Locale indianLocale = Locale.getDefault();
23509 amit.gupta 45
 
21686 ashik.ali 46
	private static final Logger LOGGER = LoggerFactory.getLogger(PdfUtils.class);
23509 amit.gupta 47
 
48
	public static void generateAndWrite(PdfModel pdfModel, OutputStream outputStream) {
21686 ashik.ali 49
		Document document = new Document();
21926 ashik.ali 50
		document.setMargins(0, 0, 25, 0);
23509 amit.gupta 51
		try {
52
			CustomCustomer customer = pdfModel.getCustomer();
53
			CustomRetailer retailer = pdfModel.getRetailer();
54
			boolean stateGst = false;
55
			String customerAddressStateCode = "", retailerAddressStateCode = "";
56
			if (customer.getAddress().getState().equals(retailer.getAddress().getState())) {
57
				stateGst = true;
58
				customerAddressStateCode = Utils.getStateCode(customer.getAddress().getState());
59
			} else {
60
				customerAddressStateCode = Utils.getStateCode(customer.getAddress().getState());
61
				retailerAddressStateCode = Utils.getStateCode(retailer.getAddress().getState());
62
			}
63
			Set<CustomOrderItem> orderItems = pdfModel.getOrderItems();
21686 ashik.ali 64
 
23509 amit.gupta 65
			PdfWriter.getInstance(document, outputStream);
22687 amit.gupta 66
 
23509 amit.gupta 67
			document.open();
68
			document.addTitle(pdfModel.getTitle());
69
			document.addAuthor(pdfModel.getAuther());
22889 amit.gupta 70
 
23509 amit.gupta 71
			Paragraph paragraphTitle = new Paragraph(INVOICE_TITLE, FONT_TITLE);
72
			paragraphTitle.setAlignment(Element.ALIGN_CENTER);
21686 ashik.ali 73
 
23509 amit.gupta 74
			PdfPCell blankCell = new PdfPCell();
75
			blankCell.setBorder(Rectangle.NO_BORDER);
76
			PdfPTable tableCustomerRetailer = new PdfPTable(3);
77
			tableCustomerRetailer.setWidthPercentage(90);
78
			tableCustomerRetailer.getDefaultCell().setBorder(Rectangle.NO_BORDER);
79
			PdfPCell columnCustomerInfo = new PdfPCell();
80
			columnCustomerInfo.addElement(new Paragraph("Customer Details", FONT_BOLD));
81
			columnCustomerInfo.addElement(new Paragraph(
82
					StringUtils.capitalize(customer.getFirstName() + " " + customer.getLastName()), FONT_NORMAL));
83
			columnCustomerInfo.addElement(new Paragraph(StringUtils.capitalize(customer.getAddress().getLine1()) + ", "
84
					+ StringUtils.capitalize(customer.getAddress().getLine2()), FONT_NORMAL));
85
			columnCustomerInfo.addElement(new Paragraph(StringUtils.capitalize(customer.getAddress().getCity()) + ", "
86
					+ StringUtils.capitalize(customer.getAddress().getState()) + "(" + customerAddressStateCode + ")"
87
					+ "\n" + customer.getAddress().getPinCode(), FONT_NORMAL));
88
			columnCustomerInfo.addElement(new Paragraph("Mobile - " + customer.getMobileNumber(), FONT_NORMAL));
89
			if (customer.getGstNumber() != null && !customer.getGstNumber().isEmpty()) {
90
				columnCustomerInfo.addElement(new Paragraph("GST Number - " + customer.getGstNumber(), FONT_NORMAL));
91
			}
92
			columnCustomerInfo.setBorder(Rectangle.NO_BORDER);
93
			PdfPCell columnRetailerInfo = new PdfPCell();
94
			columnRetailerInfo
95
					.addElement(new Paragraph(StringUtils.capitalize(retailer.getAddress().getName()), FONT_BOLD));
96
			columnRetailerInfo.addElement(new Paragraph(StringUtils.capitalize(retailer.getAddress().getLine1()) + ", "
97
					+ StringUtils.capitalize(retailer.getAddress().getLine2()) + ", "
98
					+ StringUtils.capitalize(retailer.getAddress().getCity()) + "-" + retailer.getAddress().getPinCode()
99
					+ ", " + retailer.getAddress().getState() + "("
100
					+ (stateGst ? customerAddressStateCode : retailerAddressStateCode) + ")", FONT_BOLD));
101
			columnRetailerInfo
102
					.addElement(new Paragraph("Contact No.- " + retailer.getAddress().getPhoneNumber(), FONT_BOLD));
103
			columnRetailerInfo.addElement(new Paragraph("GST NO. " + retailer.getGstNumber(), FONT_BOLD));
104
			columnRetailerInfo.setBorder(Rectangle.NO_BORDER);
105
 
106
			PdfPTable tableInvoiceDateRetailer = new PdfPTable(1);
107
			tableInvoiceDateRetailer.getDefaultCell().setBorder(Rectangle.NO_BORDER);
108
 
109
			PdfPTable tableInvoiceDate = new PdfPTable(2);
110
			tableInvoiceDate.getDefaultCell().setBorder(Rectangle.NO_BORDER);
111
 
112
			PdfPCell invoiceNumberKey = new PdfPCell(new Paragraph("Invoice No:", FONT_NORMAL));
113
			invoiceNumberKey.setBorder(Rectangle.NO_BORDER);
114
 
115
			PdfPCell invoiceNumberValue = new PdfPCell(new Paragraph(pdfModel.getInvoiceNumber(), FONT_NORMAL));
116
			invoiceNumberValue.setBorder(Rectangle.NO_BORDER);
117
 
118
			PdfPCell dateKey = new PdfPCell(new Paragraph("Date:", FONT_NORMAL));
119
			dateKey.setBorder(Rectangle.NO_BORDER);
120
 
121
			PdfPCell dateValue = new PdfPCell(new Paragraph(pdfModel.getInvoiceDate(), FONT_NORMAL));
122
			dateValue.setBorder(Rectangle.NO_BORDER);
123
			tableInvoiceDate.addCell(invoiceNumberKey);
124
			// tableInvoiceDate.addCell(blankCell);
125
			tableInvoiceDate.addCell(invoiceNumberValue);
126
			tableInvoiceDate.addCell(dateKey);
127
			// tableInvoiceDate.addCell(blankCell);
128
			tableInvoiceDate.addCell(dateValue);
129
			tableInvoiceDateRetailer.addCell(tableInvoiceDate);
130
			tableInvoiceDateRetailer.addCell(columnRetailerInfo);
131
 
132
			tableCustomerRetailer.addCell(columnCustomerInfo);
133
			tableCustomerRetailer.addCell(blankCell);
134
			tableCustomerRetailer.addCell(tableInvoiceDateRetailer);
135
 
136
			PdfPTable orders = null;
137
			if (stateGst) {
138
				orders = new PdfPTable(stateWidthsWithDiscount.length);
139
				orders.setWidths(stateWidthsWithDiscount);
140
			} else {
141
				orders = new PdfPTable(igstWidthsWithDiscount.length);
142
				orders.setWidths(igstWidthsWithDiscount);
143
			}
144
			orders.setWidthPercentage(90);
145
			orders.addCell(new Paragraph("Sl", FONT_BOLD));
146
			orders.addCell(new Paragraph("Description", FONT_BOLD));
147
			orders.addCell(new Paragraph("HSN", FONT_BOLD));
148
			orders.addCell(new Paragraph("Qty", FONT_BOLD));
149
			orders.addCell(new Paragraph("Rate\n(Per pc)", FONT_BOLD));
150
			orders.addCell(new Paragraph("Disc.", FONT_BOLD));
151
			orders.addCell(new Paragraph("Total\nTaxable", FONT_BOLD));
152
			if (!stateGst) {
153
				orders.addCell(new Paragraph("IGST\n%", FONT_BOLD));
154
				orders.addCell(new Paragraph("IGST", FONT_BOLD));
155
				// orders.setWidths(new float[]{1, 3, 1, 1, 1, 1, 1, 1});
156
				// total 8f
157
			} else {
158
				orders.addCell(new Paragraph("CGST %", FONT_BOLD));
159
				orders.addCell(new Paragraph("CGST", FONT_BOLD));
160
				orders.addCell(new Paragraph("SGST %", FONT_BOLD));
161
				orders.addCell(new Paragraph("SGST", FONT_BOLD));
162
				// orders.setWidths(new float[]{1, 3, 1, 1, 1, 1, 1, 1, 1, 1});
163
				// total 8f
164
			}
165
			orders.addCell(new Paragraph("Total", FONT_BOLD));
166
 
167
			// orders.addCell(new Paragraph("Item Total (Rs)", FONT_BOLD));
168
 
169
			orders.setHeaderRows(1);
170
			// orders.setSkipFirstHeader(true);
171
 
172
			float igstTotalAmount = 0, cgstTotalAmount = 0, sgstTotalAmount = 0;
173
			int index = 1;
174
			for (CustomOrderItem orderItem : orderItems) {
175
				orders.addCell(new Paragraph(String.valueOf(index++), FONT_NORMAL));
176
				orders.addCell(new Paragraph(orderItem.getDescription(), FONT_NORMAL));
177
				orders.addCell(new Paragraph(orderItem.getHsnCode(), FONT_NORMAL));
178
				orders.addCell(new Paragraph(String.valueOf(orderItem.getQuantity()), FONT_NORMAL));
179
				orders.addCell(new Paragraph(String.format("%.2f", orderItem.getRate()), FONT_NORMAL));
180
				orders.addCell(new Paragraph(String.format("%.2f", orderItem.getDiscount()), FONT_NORMAL));
181
				orders.addCell(new Paragraph(String.format("%.2f", orderItem.getAmount()), FONT_NORMAL));
182
				if (!stateGst) {
183
					orders.addCell(new Paragraph(String.format("%.2f", orderItem.getIgstRate()), FONT_NORMAL));
184
					orders.addCell(new Paragraph(String.format("%.2f", orderItem.getIgstAmount()), FONT_NORMAL));
185
					igstTotalAmount = igstTotalAmount + orderItem.getIgstAmount();
186
				} else {
187
					orders.addCell(new Paragraph(String.format("%.2f", orderItem.getCgstRate()), FONT_NORMAL));
188
					orders.addCell(new Paragraph(String.format("%.2f", orderItem.getCgstAmount()), FONT_NORMAL));
189
					orders.addCell(new Paragraph(String.format("%.2f", orderItem.getSgstRate()), FONT_NORMAL));
190
					orders.addCell(new Paragraph(String.format("%.2f", orderItem.getSgstAmount()), FONT_NORMAL));
191
					cgstTotalAmount = cgstTotalAmount + orderItem.getCgstAmount();
192
					sgstTotalAmount = sgstTotalAmount + orderItem.getSgstAmount();
193
				}
194
				orders.addCell(new Paragraph(String.format("%.0f", orderItem.getNetAmount()), FONT_NORMAL));
195
				// orders.addCell(new Paragraph(String.format("%.2f",
196
				// orderItem.getItemTotal()), FONT_NORMAL));
197
			}
198
 
199
			for (CustomInsurancePolicy insurancePolicy : pdfModel.getInsurancePolicies()) {
200
				orders.addCell(new Paragraph(String.valueOf(index++), FONT_NORMAL));
201
				orders.addCell(new Paragraph(insurancePolicy.getDescription(), FONT_NORMAL));
202
				orders.addCell(new Paragraph(insurancePolicy.getHsnCode(), FONT_NORMAL));
203
				orders.addCell(new Paragraph("1", FONT_NORMAL));
204
				orders.addCell(new Paragraph(String.format("%.2f", insurancePolicy.getRate()), FONT_NORMAL));
205
				orders.addCell(new Paragraph("-", FONT_NORMAL));
206
				orders.addCell(new Paragraph(String.format("%.2f", insurancePolicy.getRate()), FONT_NORMAL));
207
				if (!stateGst) {
208
					orders.addCell(new Paragraph(String.format("%.2f", insurancePolicy.getIgstRate()), FONT_NORMAL));
209
					orders.addCell(new Paragraph(String.format("%.2f", insurancePolicy.getIgstAmount()), FONT_NORMAL));
210
					igstTotalAmount = igstTotalAmount + insurancePolicy.getIgstAmount();
211
				} else {
212
					orders.addCell(new Paragraph(String.format("%.2f", insurancePolicy.getCgstRate()), FONT_NORMAL));
213
					orders.addCell(new Paragraph(String.format("%.2f", insurancePolicy.getCgstAmount()), FONT_NORMAL));
214
					orders.addCell(new Paragraph(String.format("%.2f", insurancePolicy.getSgstRate()), FONT_NORMAL));
215
					orders.addCell(new Paragraph(String.format("%.2f", insurancePolicy.getSgstAmount()), FONT_NORMAL));
216
					cgstTotalAmount = cgstTotalAmount + insurancePolicy.getCgstAmount();
217
					sgstTotalAmount = sgstTotalAmount + insurancePolicy.getSgstAmount();
218
				}
219
				orders.addCell(new Paragraph(String.format("%.0f", insurancePolicy.getNetAmount()), FONT_NORMAL));
220
			}
221
 
222
			document.add(paragraphTitle);
223
			document.add(Chunk.NEWLINE);
224
			document.add(Chunk.NEWLINE);
225
			document.add(tableCustomerRetailer);
226
 
227
			document.add(Chunk.NEWLINE);
228
			document.add(orders);
229
 
230
			PdfPTable grandTotalTable = new PdfPTable(3);
231
			if (stateGst) {
232
				grandTotalTable.setWidths(new float[] { 6.6f, .6f, .8f });
233
			} else {
234
				grandTotalTable.setWidths(new float[] { 6.5f, .6f, .9f });
235
			}
236
			grandTotalTable.setWidthPercentage(90);
237
 
238
			Paragraph grandTotalParagraph = new Paragraph("Grand total", FONT_BOLD);
239
			grandTotalParagraph.setIndentationRight(20);
240
			grandTotalTable.addCell(grandTotalParagraph);
241
			Paragraph rsParagraph = new Paragraph("Rs.", FONT_BOLD);
242
			grandTotalTable.addCell(rsParagraph);
243
			Paragraph amountParagraph = new Paragraph(String.format("%.2f", pdfModel.getTotalAmount()), FONT_BOLD);
244
			grandTotalTable.addCell(amountParagraph);
245
 
246
			document.add(grandTotalTable);
247
 
248
			PdfPTable amountInWordsTable = new PdfPTable(3);
249
			if (!stateGst) {
250
				amountInWordsTable.setWidths(new float[] { 2, 5.1f, 0.9f });
251
			} else {
252
				amountInWordsTable.setWidths(new float[] { 2, 5.2f, 0.8f });
253
			}
254
			amountInWordsTable.setWidthPercentage(90);
255
			amountInWordsTable.addCell(new Paragraph("Amount in Words:", FONT_BOLD));
256
 
257
			String amountInWords = toAmountInWords(pdfModel.getTotalAmount());
258
			amountInWordsTable.addCell(new Paragraph(amountInWords.toString(), FONT_BOLD));
259
			amountInWordsTable.addCell(new Paragraph("E & O.E", FONT_NORMAL));
260
			document.add(amountInWordsTable);
261
 
262
			StringBuffer sb = new StringBuffer();
263
			List<String> tncs = pdfModel.getTncs();
264
			for (String tnc : tncs) {
265
				sb.append(tnc).append("\n");
266
			}
267
 
268
			Paragraph warningParagraph = new Paragraph(sb.toString(), FONT_NORMAL);
269
			warningParagraph.setIndentationLeft(40);
270
			document.add(Chunk.NEWLINE);
271
			document.add(warningParagraph);
272
 
273
			document.close(); // no need to close PDFwriter?
274
 
275
		} catch (DocumentException e) {
276
			LOGGER.error("Unable to write data to pdf file : ", e);
277
		} catch (Exception e) {
21901 ashik.ali 278
			// TODO Auto-generated catch block
279
			e.printStackTrace();
280
		}
21686 ashik.ali 281
	}
23509 amit.gupta 282
 
283
	public static void generateAndWriteDebitNote(List<DebitNotePdfModel> debitNotePdfModels,
284
			OutputStream outputStream) {
285
		Document document = new Document();
286
		document.setMargins(0, 0, 25, 0);
287
		try {
288
			for (DebitNotePdfModel debitNotePdfModel : debitNotePdfModels) {
289
 
290
				PdfModel pdfModel = debitNotePdfModel.getPdfModel();
291
				CustomCustomer customer = pdfModel.getCustomer();
292
				CustomRetailer retailer = pdfModel.getRetailer();
293
				boolean stateGst = false;
294
				String customerAddressStateCode = "", retailerAddressStateCode = "";
295
				if (customer.getAddress().getState().equals(retailer.getAddress().getState())) {
296
					stateGst = true;
297
					customerAddressStateCode = Utils.getStateCode(customer.getAddress().getState());
298
				} else {
299
					customerAddressStateCode = Utils.getStateCode(customer.getAddress().getState());
300
					retailerAddressStateCode = Utils.getStateCode(retailer.getAddress().getState());
301
				}
302
				Set<CustomOrderItem> orderItems = pdfModel.getOrderItems();
303
 
304
				PdfWriter.getInstance(document, outputStream);
305
 
306
				document.open();
307
				document.addTitle(pdfModel.getTitle());
308
				document.addAuthor(pdfModel.getAuther());
309
 
310
				Paragraph paragraphTitle = new Paragraph(INVOICE_TITLE, FONT_TITLE);
311
				paragraphTitle.setAlignment(Element.ALIGN_CENTER);
312
 
313
				PdfPCell blankCell = new PdfPCell();
314
				blankCell.setBorder(Rectangle.NO_BORDER);
315
				PdfPTable tableCustomerRetailer = new PdfPTable(3);
316
				tableCustomerRetailer.setWidthPercentage(90);
317
				tableCustomerRetailer.getDefaultCell().setBorder(Rectangle.NO_BORDER);
318
				PdfPCell columnCustomerInfo = new PdfPCell();
319
				columnCustomerInfo.addElement(new Paragraph("Customer Details", FONT_BOLD));
320
				columnCustomerInfo.addElement(new Paragraph(
321
						StringUtils.capitalize(customer.getFirstName() + " " + customer.getLastName()), FONT_NORMAL));
322
				columnCustomerInfo.addElement(new Paragraph(StringUtils.capitalize(customer.getAddress().getLine1())
323
						+ ", " + StringUtils.capitalize(customer.getAddress().getLine2()), FONT_NORMAL));
324
				columnCustomerInfo.addElement(new Paragraph(
325
						StringUtils.capitalize(customer.getAddress().getCity()) + ", "
326
								+ StringUtils.capitalize(customer.getAddress().getState()) + "("
327
								+ customerAddressStateCode + ")" + "\n" + customer.getAddress().getPinCode(),
328
						FONT_NORMAL));
329
				columnCustomerInfo.addElement(new Paragraph("Mobile - " + customer.getMobileNumber(), FONT_NORMAL));
330
				if (customer.getGstNumber() != null && !customer.getGstNumber().isEmpty()) {
331
					columnCustomerInfo
332
							.addElement(new Paragraph("GST Number - " + customer.getGstNumber(), FONT_NORMAL));
333
				}
334
				columnCustomerInfo.setBorder(Rectangle.NO_BORDER);
335
				PdfPCell columnRetailerInfo = new PdfPCell();
336
				columnRetailerInfo
337
						.addElement(new Paragraph(StringUtils.capitalize(retailer.getAddress().getName()), FONT_BOLD));
338
				columnRetailerInfo.addElement(new Paragraph(StringUtils.capitalize(retailer.getAddress().getLine1())
339
						+ ", " + StringUtils.capitalize(retailer.getAddress().getLine2()) + ", "
340
						+ StringUtils.capitalize(retailer.getAddress().getCity()) + "-"
341
						+ retailer.getAddress().getPinCode() + ", " + retailer.getAddress().getState() + "("
342
						+ (stateGst ? customerAddressStateCode : retailerAddressStateCode) + ")", FONT_BOLD));
343
				columnRetailerInfo
344
						.addElement(new Paragraph("Contact No.- " + retailer.getAddress().getPhoneNumber(), FONT_BOLD));
345
				columnRetailerInfo.addElement(new Paragraph("GST NO. " + retailer.getGstNumber(), FONT_BOLD));
346
				columnRetailerInfo.setBorder(Rectangle.NO_BORDER);
347
 
348
				PdfPTable tableInvoiceDateRetailer = new PdfPTable(1);
349
				tableInvoiceDateRetailer.getDefaultCell().setBorder(Rectangle.NO_BORDER);
350
 
351
				PdfPTable tableInvoiceDate = new PdfPTable(2);
352
				tableInvoiceDate.getDefaultCell().setBorder(Rectangle.NO_BORDER);
353
 
354
				PdfPCell invoiceNumberKey = new PdfPCell(new Paragraph("Invoice No:", FONT_NORMAL));
355
				invoiceNumberKey.setBorder(Rectangle.NO_BORDER);
356
 
357
				PdfPCell invoiceNumberValue = new PdfPCell(new Paragraph(pdfModel.getInvoiceNumber(), FONT_NORMAL));
358
				invoiceNumberValue.setBorder(Rectangle.NO_BORDER);
359
 
360
				PdfPCell dateKey = new PdfPCell(new Paragraph("Date:", FONT_NORMAL));
361
				dateKey.setBorder(Rectangle.NO_BORDER);
362
 
363
				PdfPCell dateValue = new PdfPCell(new Paragraph(pdfModel.getInvoiceDate(), FONT_NORMAL));
364
				dateValue.setBorder(Rectangle.NO_BORDER);
365
				tableInvoiceDate.addCell(invoiceNumberKey);
366
				// tableInvoiceDate.addCell(blankCell);
367
				tableInvoiceDate.addCell(invoiceNumberValue);
368
				tableInvoiceDate.addCell(dateKey);
369
				// tableInvoiceDate.addCell(blankCell);
370
				tableInvoiceDate.addCell(dateValue);
371
				tableInvoiceDateRetailer.addCell(tableInvoiceDate);
372
				tableInvoiceDateRetailer.addCell(columnRetailerInfo);
373
 
374
				tableCustomerRetailer.addCell(columnCustomerInfo);
375
				tableCustomerRetailer.addCell(blankCell);
376
				tableCustomerRetailer.addCell(tableInvoiceDateRetailer);
377
 
378
				PdfPTable orders = null;
379
				if (stateGst) {
380
					orders = new PdfPTable(stateWidthsWithDiscount.length);
381
					orders.setWidths(stateWidthsWithDiscount);
382
				} else {
383
					orders = new PdfPTable(igstWidthsWithDiscount.length);
384
					orders.setWidths(igstWidthsWithDiscount);
385
				}
386
				orders.setWidthPercentage(90);
387
				orders.addCell(new Paragraph("Sl", FONT_BOLD));
388
				orders.addCell(new Paragraph("Description", FONT_BOLD));
389
				orders.addCell(new Paragraph("HSN", FONT_BOLD));
390
				orders.addCell(new Paragraph("Qty", FONT_BOLD));
391
				orders.addCell(new Paragraph("Rate\n(Per pc)", FONT_BOLD));
392
				orders.addCell(new Paragraph("Disc.", FONT_BOLD));
393
				orders.addCell(new Paragraph("Total\nTaxable", FONT_BOLD));
394
				if (!stateGst) {
395
					orders.addCell(new Paragraph("IGST\n%", FONT_BOLD));
396
					orders.addCell(new Paragraph("IGST", FONT_BOLD));
397
					// orders.setWidths(new float[]{1, 3, 1, 1, 1, 1, 1, 1});
398
					// total 8f
399
				} else {
400
					orders.addCell(new Paragraph("CGST %", FONT_BOLD));
401
					orders.addCell(new Paragraph("CGST", FONT_BOLD));
402
					orders.addCell(new Paragraph("SGST %", FONT_BOLD));
403
					orders.addCell(new Paragraph("SGST", FONT_BOLD));
404
					// orders.setWidths(new float[]{1, 3, 1, 1, 1, 1, 1, 1, 1,
405
					// 1});
406
					// total 8f
407
				}
408
				orders.addCell(new Paragraph("Total", FONT_BOLD));
409
 
410
				// orders.addCell(new Paragraph("Item Total (Rs)", FONT_BOLD));
411
 
412
				orders.setHeaderRows(1);
413
				// orders.setSkipFirstHeader(true);
414
 
415
				float igstTotalAmount = 0, cgstTotalAmount = 0, sgstTotalAmount = 0;
416
				int index = 1;
417
				for (CustomOrderItem orderItem : orderItems) {
418
					orders.addCell(new Paragraph(String.valueOf(index++), FONT_NORMAL));
419
					orders.addCell(new Paragraph(orderItem.getDescription(), FONT_NORMAL));
420
					orders.addCell(new Paragraph(orderItem.getHsnCode(), FONT_NORMAL));
421
					orders.addCell(new Paragraph(String.valueOf(orderItem.getQuantity()), FONT_NORMAL));
422
					orders.addCell(new Paragraph(String.format("%.2f", orderItem.getRate()), FONT_NORMAL));
423
					orders.addCell(new Paragraph(String.format("%.2f", orderItem.getDiscount()), FONT_NORMAL));
424
					orders.addCell(new Paragraph(String.format("%.2f", orderItem.getAmount()), FONT_NORMAL));
425
					if (!stateGst) {
426
						orders.addCell(new Paragraph(String.format("%.2f", orderItem.getIgstRate()), FONT_NORMAL));
427
						orders.addCell(new Paragraph(String.format("%.2f", orderItem.getIgstAmount()), FONT_NORMAL));
428
						igstTotalAmount = igstTotalAmount + orderItem.getIgstAmount();
429
					} else {
430
						orders.addCell(new Paragraph(String.format("%.2f", orderItem.getCgstRate()), FONT_NORMAL));
431
						orders.addCell(new Paragraph(String.format("%.2f", orderItem.getCgstAmount()), FONT_NORMAL));
432
						orders.addCell(new Paragraph(String.format("%.2f", orderItem.getSgstRate()), FONT_NORMAL));
433
						orders.addCell(new Paragraph(String.format("%.2f", orderItem.getSgstAmount()), FONT_NORMAL));
434
						cgstTotalAmount = cgstTotalAmount + orderItem.getCgstAmount();
435
						sgstTotalAmount = sgstTotalAmount + orderItem.getSgstAmount();
436
					}
437
					orders.addCell(new Paragraph(String.format("%.0f", orderItem.getNetAmount()), FONT_NORMAL));
438
					// orders.addCell(new Paragraph(String.format("%.2f",
439
					// orderItem.getItemTotal()), FONT_NORMAL));
440
				}
441
 
442
				for (CustomInsurancePolicy insurancePolicy : pdfModel.getInsurancePolicies()) {
443
					orders.addCell(new Paragraph(String.valueOf(index++), FONT_NORMAL));
444
					orders.addCell(new Paragraph(insurancePolicy.getDescription(), FONT_NORMAL));
445
					orders.addCell(new Paragraph(insurancePolicy.getHsnCode(), FONT_NORMAL));
446
					orders.addCell(new Paragraph("1", FONT_NORMAL));
447
					orders.addCell(new Paragraph(String.format("%.2f", insurancePolicy.getRate()), FONT_NORMAL));
448
					orders.addCell(new Paragraph("-", FONT_NORMAL));
449
					orders.addCell(new Paragraph(String.format("%.2f", insurancePolicy.getRate()), FONT_NORMAL));
450
					if (!stateGst) {
451
						orders.addCell(
452
								new Paragraph(String.format("%.2f", insurancePolicy.getIgstRate()), FONT_NORMAL));
453
						orders.addCell(
454
								new Paragraph(String.format("%.2f", insurancePolicy.getIgstAmount()), FONT_NORMAL));
455
						igstTotalAmount = igstTotalAmount + insurancePolicy.getIgstAmount();
456
					} else {
457
						orders.addCell(
458
								new Paragraph(String.format("%.2f", insurancePolicy.getCgstRate()), FONT_NORMAL));
459
						orders.addCell(
460
								new Paragraph(String.format("%.2f", insurancePolicy.getCgstAmount()), FONT_NORMAL));
461
						orders.addCell(
462
								new Paragraph(String.format("%.2f", insurancePolicy.getSgstRate()), FONT_NORMAL));
463
						orders.addCell(
464
								new Paragraph(String.format("%.2f", insurancePolicy.getSgstAmount()), FONT_NORMAL));
465
						cgstTotalAmount = cgstTotalAmount + insurancePolicy.getCgstAmount();
466
						sgstTotalAmount = sgstTotalAmount + insurancePolicy.getSgstAmount();
467
					}
468
					orders.addCell(new Paragraph(String.format("%.0f", insurancePolicy.getNetAmount()), FONT_NORMAL));
469
				}
470
 
471
				document.add(paragraphTitle);
472
				document.add(Chunk.NEWLINE);
473
				document.add(Chunk.NEWLINE);
474
				document.add(tableCustomerRetailer);
475
 
476
				document.add(Chunk.NEWLINE);
477
				document.add(orders);
478
 
479
				PdfPTable grandTotalTable = new PdfPTable(3);
480
				if (stateGst) {
481
					grandTotalTable.setWidths(new float[] { 6.6f, .6f, .8f });
482
				} else {
483
					grandTotalTable.setWidths(new float[] { 6.5f, .6f, .9f });
484
				}
485
				grandTotalTable.setWidthPercentage(90);
486
 
487
				Paragraph grandTotalParagraph = new Paragraph("Grand total", FONT_BOLD);
488
				grandTotalParagraph.setIndentationRight(20);
489
				grandTotalTable.addCell(grandTotalParagraph);
490
				Paragraph rsParagraph = new Paragraph("Rs.", FONT_BOLD);
491
				grandTotalTable.addCell(rsParagraph);
492
				Paragraph amountParagraph = new Paragraph(String.format("%.2f", pdfModel.getTotalAmount()), FONT_BOLD);
493
				grandTotalTable.addCell(amountParagraph);
494
 
495
				document.add(grandTotalTable);
496
 
497
				PdfPTable amountInWordsTable = new PdfPTable(3);
498
				if (!stateGst) {
499
					amountInWordsTable.setWidths(new float[] { 2, 5.1f, 0.9f });
500
				} else {
501
					amountInWordsTable.setWidths(new float[] { 2, 5.2f, 0.8f });
502
				}
503
				amountInWordsTable.setWidthPercentage(90);
504
				amountInWordsTable.addCell(new Paragraph("Amount in Words:", FONT_BOLD));
505
 
506
				String amountInWords = toAmountInWords(pdfModel.getTotalAmount());
507
				amountInWordsTable.addCell(new Paragraph(amountInWords.toString(), FONT_BOLD));
508
				amountInWordsTable.addCell(new Paragraph("E & O.E", FONT_NORMAL));
509
				document.add(amountInWordsTable);
510
 
511
				StringBuffer sb = new StringBuffer();
512
				List<String> tncs = pdfModel.getTncs();
513
				for (String tnc : tncs) {
514
					sb.append(tnc).append("\n");
515
				}
516
 
517
				Paragraph warningParagraph = new Paragraph(sb.toString(), FONT_NORMAL);
518
				warningParagraph.setIndentationLeft(40);
519
				document.add(Chunk.NEWLINE);
520
				document.add(warningParagraph);
521
				document.newPage();
522
			}
523
			document.close(); // no need to close PDFwriter?
524
		} catch (DocumentException e) {
525
			LOGGER.error("Unable to write data to pdf file : ", e);
526
		} catch (Exception e) {
527
			// TODO Auto-generated catch block
528
			e.printStackTrace();
529
		}
21915 ashik.ali 530
	}
23509 amit.gupta 531
 
532
	private static String toAmountInWords(float amount) {
533
		RuleBasedNumberFormat amountInWordsFormat = new RuleBasedNumberFormat(indianLocale,
534
				RuleBasedNumberFormat.SPELLOUT);
535
		StringBuilder amountInWords = new StringBuilder("Rs. ");
536
		amountInWords.append(StringUtils.capitalize(amountInWordsFormat.format((int) amount)));
537
		amountInWords.append(" and ");
538
		amountInWords.append(StringUtils.capitalize(amountInWordsFormat.format((int) (amount * 100) % 100)));
539
		amountInWords.append(" paise");
540
		return amountInWords.toString();
541
	}
21686 ashik.ali 542
}