Subversion Repositories SmartDukaan

Rev

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