Subversion Repositories SmartDukaan

Rev

Rev 21901 | Rev 21926 | 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);
33
	private static Font FONT_NORMAL = new Font(Font.FontFamily.TIMES_ROMAN, 8, Font.NORMAL);
34
	private static Font FONT_BOLD = new Font(Font.FontFamily.TIMES_ROMAN, 8, Font.BOLD);
35
	//private static Font fontTableHeader = new Font(Font.FontFamily.TIMES_ROMAN, 14, Font.BOLD);
36
	private static final String INVOICE_TITLE = "RETAILER_INVOICE";
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();
44
 
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));
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
 
21686 ashik.ali 160
            int index = 1;
161
        	for(CustomFofoOrderItem orderItem : orderItems){
162
            	orders.addCell(new Paragraph(String.valueOf(index++), FONT_NORMAL));
163
            	orders.addCell(new Paragraph(orderItem.getDescription(), FONT_NORMAL));
21901 ashik.ali 164
            	orders.addCell(new Paragraph(orderItem.getHsnCode(), FONT_NORMAL));
21686 ashik.ali 165
            	orders.addCell(new Paragraph(String.valueOf(orderItem.getQuantity()), FONT_NORMAL));
166
            	orders.addCell(new Paragraph(String.valueOf(orderItem.getRate()), FONT_NORMAL));
167
            	orders.addCell(new Paragraph(String.valueOf(orderItem.getAmount()), FONT_NORMAL));
21901 ashik.ali 168
            	if(gstWithInState){
169
            		orders.addCell(new Paragraph(String.valueOf(orderItem.getIgstRate()), FONT_NORMAL));
21915 ashik.ali 170
            		orders.addCell(new Paragraph(String.valueOf(orderItem.getIgstAmount()), FONT_NORMAL));
21901 ashik.ali 171
            	}else{
172
            		orders.addCell(new Paragraph(String.valueOf(orderItem.getCgstRate()), FONT_NORMAL));
21915 ashik.ali 173
            		orders.addCell(new Paragraph(String.valueOf(orderItem.getCgstAmount()), FONT_NORMAL));
21901 ashik.ali 174
            		orders.addCell(new Paragraph(String.valueOf(orderItem.getSgstRate()), FONT_NORMAL));
21915 ashik.ali 175
            		orders.addCell(new Paragraph(String.valueOf(orderItem.getSgstAmount()), FONT_NORMAL));
21901 ashik.ali 176
            	}
21686 ashik.ali 177
            	orders.addCell(new Paragraph(String.valueOf(orderItem.getItemTotal()), FONT_NORMAL));
178
            }
179
            //orders.addCell("1");
180
            //orders.addCell("Sansui X71Activ Ta2s");
181
            //orders.setHeaderRows(1);
182
            //orders.getDefaultCell().setBorder(Rectangle.NO_BORDER);
183
            //orders.addCell(orderDetail);
184
            //orders.addCell(ordersTable);
185
            document.add(paragraphTitle);
186
            document.add(Chunk.NEWLINE);
187
            document.add(Chunk.NEWLINE);
188
            //document.add(paragraphRetailerName);
189
            document.add(tableCustomerRetailer);
190
 
191
            document.add(Chunk.NEWLINE);
192
            document.add(Chunk.NEWLINE);
193
            document.add(Chunk.NEWLINE);
194
            document.add(orders);
21901 ashik.ali 195
            PdfPTable grandTotalTable = null;
196
            if(gstWithInState){
197
            	grandTotalTable = new PdfPTable(3);
198
            	grandTotalTable.setWidths(new int[]{8, 1, 1});
199
            }else{
200
            	grandTotalTable = new PdfPTable(5);
201
            	grandTotalTable.setWidths(new int[]{8, 1, 1, 1, 1});
202
            }
203
 
21686 ashik.ali 204
            /*for(int i = 0; i < 6; i++){
205
            	grandTotalTable.addCell(new Paragraph());
206
            }*/
207
            //PdfPCell grandTotalCell = new PdfPCell(new Paragraph("Grand total", fontBold));
208
            //grandTotalCell.setVerticalAlignment(Element.ALIGN_RIGHT);
209
            Paragraph grandTotalParagraph = new Paragraph("Grand total", FONT_BOLD);
210
            grandTotalParagraph.setIndentationRight(20);
211
            grandTotalTable.addCell(grandTotalParagraph);
212
            Paragraph rsParagraph = new Paragraph("Rs.", FONT_BOLD);
213
            grandTotalTable.addCell(rsParagraph);
214
            Paragraph amountParagraph = new Paragraph(String.valueOf(pdfModel.getTotalAmount()), FONT_BOLD);
215
            grandTotalTable.addCell(amountParagraph);
216
            //grandTotalCell.setColspan(6);
217
 
218
            document.add(grandTotalTable);
219
 
220
            PdfPTable amountInWordsTable = new PdfPTable(3);
221
            amountInWordsTable.setWidths(new int[]{1, 6, 3});
222
            amountInWordsTable.addCell(new Paragraph("Amount in Words:", FONT_BOLD));
21915 ashik.ali 223
 
224
            String amountInWords = toAmountInWords(pdfModel.getTotalAmount());
225
           	amountInWordsTable.addCell(new Paragraph(amountInWords.toString(), FONT_BOLD));
21686 ashik.ali 226
            amountInWordsTable.addCell(new Paragraph("E & O.E", FONT_NORMAL));
21915 ashik.ali 227
            document.add(amountInWordsTable);            
21686 ashik.ali 228
 
229
            Paragraph warningParagraph = new Paragraph("Goods once sold will not be taken back.\nAll Disputes subject to "+pdfModel.getRetailer().getAddress().getState()+" Jurisdiction.\nThis is a Computer Generated Invoice.", FONT_NORMAL);
230
            warningParagraph.setIndentationLeft(50);
231
            document.add(Chunk.NEWLINE);
232
            document.add(warningParagraph);
233
            document.add(Chunk.NEWLINE);
234
            document.add(Chunk.NEWLINE);
235
 
236
            PdfPTable itemSerialNumbers = new PdfPTable(2);
237
            itemSerialNumbers.addCell(new Paragraph("Item Name", FONT_BOLD));
238
            itemSerialNumbers.addCell(new Paragraph("Serial Number", FONT_BOLD));
239
            itemSerialNumbers.setHeaderRows(1);
240
 
241
            for(CustomFofoOrderItem orderItem : orderItems){
242
            	itemSerialNumbers.addCell(new Paragraph(orderItem.getDescription(), FONT_NORMAL));
243
            	itemSerialNumbers.addCell(new Paragraph(orderItem.getSerialNumbers().toString(), FONT_NORMAL));
244
            }
245
 
246
            document.add(itemSerialNumbers);
247
            document.close(); // no need to close PDFwriter?
248
 
249
        } catch (DocumentException e) {
250
            LOGGER.error("Unable to write data to pdf file : ", e);
21901 ashik.ali 251
        } catch (Exception e) {
252
			// TODO Auto-generated catch block
253
			e.printStackTrace();
254
		}
21686 ashik.ali 255
	}
21915 ashik.ali 256
 
257
	@SuppressWarnings("deprecation")
258
	private static String toAmountInWords(float amount){
259
		RuleBasedNumberFormat amountInWordsFormat = new RuleBasedNumberFormat(indianLocale, RuleBasedNumberFormat.SPELLOUT);
260
       	StringBuilder amountInWords = new StringBuilder("Rs. ");
261
        amountInWords.append(WordUtils.capitalize(amountInWordsFormat.format((int)amount)));
262
       	amountInWords.append(" and ");
263
        amountInWords.append(WordUtils.capitalize(amountInWordsFormat.format((int)(amount*100)%100)));
264
       	amountInWords.append(" paise");
265
       	return amountInWords.toString();
266
	}
21686 ashik.ali 267
}