Subversion Repositories SmartDukaan

Rev

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