Subversion Repositories SmartDukaan

Rev

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