Subversion Repositories SmartDukaan

Rev

Rev 5948 | Rev 5994 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2787 chandransh 1
package in.shop2020.hotspot.dashbaord.server;
2
 
3
import in.shop2020.config.ConfigException;
3044 chandransh 4
import in.shop2020.logistics.DeliveryType;
2787 chandransh 5
import in.shop2020.logistics.LogisticsServiceException;
5527 anupam.sin 6
import in.shop2020.logistics.PickUpType;
5556 rajveer 7
import in.shop2020.logistics.PickupStore;
2787 chandransh 8
import in.shop2020.logistics.Provider;
5948 mandeep.dh 9
import in.shop2020.model.v1.inventory.InventoryServiceException;
5945 mandeep.dh 10
import in.shop2020.model.v1.inventory.Warehouse;
11
import in.shop2020.model.v1.catalog.CatalogService.Client;
5527 anupam.sin 12
import in.shop2020.model.v1.order.Attribute;
2787 chandransh 13
import in.shop2020.model.v1.order.LineItem;
14
import in.shop2020.model.v1.order.Order;
4361 rajveer 15
import in.shop2020.model.v1.order.OrderStatus;
5527 anupam.sin 16
import in.shop2020.model.v1.order.OrderType;
3132 rajveer 17
import in.shop2020.thrift.clients.CatalogClient;
18
import in.shop2020.thrift.clients.LogisticsClient;
19
import in.shop2020.thrift.clients.TransactionClient;
2787 chandransh 20
import in.shop2020.thrift.clients.config.ConfigClient;
5948 mandeep.dh 21
import in.shop2020.thrift.clients.InventoryClient;
2787 chandransh 22
 
23
import java.io.ByteArrayOutputStream;
24
import java.io.File;
25
import java.io.FileOutputStream;
26
import java.io.IOException;
27
import java.text.DateFormat;
28
import java.text.DecimalFormat;
4361 rajveer 29
import java.util.ArrayList;
4746 rajveer 30
import java.util.Calendar;
2787 chandransh 31
import java.util.Date;
32
import java.util.Enumeration;
4746 rajveer 33
import java.util.GregorianCalendar;
2787 chandransh 34
import java.util.List;
35
import java.util.Locale;
36
import java.util.Properties;
37
import java.util.ResourceBundle;
38
 
39
import javax.servlet.ServletException;
40
import javax.servlet.ServletOutputStream;
41
import javax.servlet.http.HttpServlet;
42
import javax.servlet.http.HttpServletRequest;
43
import javax.servlet.http.HttpServletResponse;
44
 
45
import org.apache.commons.lang.WordUtils;
46
import org.apache.thrift.TException;
47
import org.slf4j.Logger;
48
import org.slf4j.LoggerFactory;
49
 
50
import com.ibm.icu.text.RuleBasedNumberFormat;
51
 
52
import com.itextpdf.text.Document;
53
import com.itextpdf.text.Element;
54
import com.itextpdf.text.Font;
55
import com.itextpdf.text.FontFactory;
56
import com.itextpdf.text.FontFactoryImp;
57
import com.itextpdf.text.Image;
58
import com.itextpdf.text.Paragraph;
59
import com.itextpdf.text.Phrase;
60
import com.itextpdf.text.Rectangle;
61
import com.itextpdf.text.Font.FontFamily;
62
import com.itextpdf.text.pdf.BaseFont;
63
import com.itextpdf.text.pdf.PdfPCell;
64
import com.itextpdf.text.pdf.PdfPTable;
65
import com.itextpdf.text.pdf.PdfWriter;
66
import com.itextpdf.text.pdf.draw.DottedLineSeparator;
67
 
68
@SuppressWarnings("serial")
69
public class InvoiceServlet extends HttpServlet {
70
 
71
    private static Logger logger = LoggerFactory.getLogger(InvoiceServlet.class);
72
 
73
    @Override
74
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
75
        long orderId = Long.parseLong(request.getParameter("id"));
4361 rajveer 76
        long warehouseId = Long.parseLong(request.getParameter("warehouse"));
2843 chandransh 77
        boolean withBill = false;
4361 rajveer 78
        boolean printAll = false;
2843 chandransh 79
        try {
80
            withBill = Boolean.parseBoolean(request.getParameter("withBill"));
81
        } catch(Exception e){
82
            logger.warn("Couldn't infer whether bill should be printed. Not printing the bill.", e);
83
        }
4361 rajveer 84
        try {
85
        	printAll = Boolean.parseBoolean(request.getParameter("printAll"));
86
        } catch(Exception e){
87
            logger.warn("Couldn't infer whether bill should be printed. Not printing the bill.", e);
88
        }
89
 
2787 chandransh 90
        logger.info("Printing invoice for order id: " + orderId);
91
 
92
        InvoiceGenerationService invoiceGenerationService = new InvoiceGenerationService();
4361 rajveer 93
        ByteArrayOutputStream baos = invoiceGenerationService.generateInvoice(orderId, withBill, printAll, warehouseId);
2787 chandransh 94
        response.setContentType("application/pdf");
95
        response.setHeader("Content-disposition", "inline; filename=invoice-"+orderId+".pdf" );
96
 
97
        ServletOutputStream sos;
98
        try {
99
            sos = response.getOutputStream();
100
            baos.writeTo(sos);
101
            sos.flush();
102
        } catch (IOException e) {
103
            logger.error("Encountered error while sending invoice response: ", e);
104
        }
105
    }
106
}
107
 
108
class InvoiceGenerationService {
109
 
110
    private static Logger logger = LoggerFactory.getLogger(InvoiceGenerationService.class);
111
 
3132 rajveer 112
    private TransactionClient tsc = null;
5948 mandeep.dh 113
    private InventoryClient csc = null;
3132 rajveer 114
    private LogisticsClient lsc = null;
2787 chandransh 115
 
116
    private static Locale indianLocale = new Locale("en", "IN");
117
    private DecimalFormat amountFormat = new DecimalFormat("#,##0.00");
118
 
119
    private static final Font helvetica8 = FontFactory.getFont(FontFactory.HELVETICA, 8);
120
    private static final Font helvetica10 = FontFactory.getFont(FontFactory.HELVETICA, 10);
121
    private static final Font helvetica12 = FontFactory.getFont(FontFactory.HELVETICA, 12);
122
    private static final Font helvetica16 = FontFactory.getFont(FontFactory.HELVETICA, 16);
3065 chandransh 123
    private static final Font helvetica28 = FontFactory.getFont(FontFactory.HELVETICA, 28);
2787 chandransh 124
 
125
    private static final Font helveticaBold8 = FontFactory.getFont(FontFactory.HELVETICA_BOLD, 8);
126
    private static final Font helveticaBold12 = FontFactory.getFont(FontFactory.HELVETICA_BOLD, 12);
127
 
128
    private static final Properties properties = readProperties();
129
    private static final String ourAddress = properties.getProperty("sales_tax_address",
130
                    "Spice Online Retail Pvt. Ltd.\nKhasra No. 819, Block-K\nMahipalpur, New Delhi-110037\n");
131
    private static final String tinNo = properties.getProperty("sales_tax_tin", "07250399732");
132
 
133
    private static final String delhiPincodePrefix = "11";
134
 
135
    private static final double salesTaxLowRate = Double.parseDouble(properties.getProperty("sales_tax_low_rate", "5.0"));
136
    private static final double salesTaxHighRate = Double.parseDouble(properties.getProperty("sales_tax_high_rate", "12.5"));
5872 mandeep.dh 137
    private static final double salesTaxCutOff = Double.parseDouble(properties.getProperty("sales_tax_cutoff", "10000"));
2787 chandransh 138
 
139
    private static Properties readProperties(){
140
        ResourceBundle resource = ResourceBundle.getBundle(InvoiceGenerationService.class.getName());
141
        Properties props = new Properties();
142
 
143
        Enumeration<String> keys = resource.getKeys();
144
        while (keys.hasMoreElements()) {
145
            String key = keys.nextElement();
146
            props.put(key, resource.getString(key));
147
        }
148
        return props;
149
    }
150
 
151
    public InvoiceGenerationService() {
152
        try {
3132 rajveer 153
            tsc = new TransactionClient();
5948 mandeep.dh 154
            csc = new InventoryClient();
3132 rajveer 155
            lsc = new LogisticsClient();
2787 chandransh 156
        } catch (Exception e) {
157
            logger.error("Error while instantiating thrift clients.", e);
158
        }
159
    }
160
 
4361 rajveer 161
    public ByteArrayOutputStream generateInvoice(long orderId, boolean withBill, boolean printAll, long warehouseId) {
2787 chandransh 162
        ByteArrayOutputStream baosPDF = null;
163
        in.shop2020.model.v1.order.TransactionService.Client tclient = tsc.getClient();
5948 mandeep.dh 164
        in.shop2020.model.v1.inventory.InventoryService.Client iclient = csc.getClient();
2787 chandransh 165
        in.shop2020.logistics.LogisticsService.Client logisticsClient = lsc.getClient();
166
 
4361 rajveer 167
 
2787 chandransh 168
        try {
169
            baosPDF = new ByteArrayOutputStream();
170
 
171
            Document document = new Document();
172
            PdfWriter.getInstance(document, baosPDF);
173
            document.addAuthor("shop2020");
4361 rajveer 174
            //document.addTitle("Invoice No: " + order.getInvoice_number());
2787 chandransh 175
            document.open();
4361 rajveer 176
 
177
            List<Order> orders = new ArrayList<Order>();
178
            if(printAll){
179
	            try {
4801 anupam.sin 180
	                List<OrderStatus> statuses = new ArrayList<OrderStatus>();
181
	                statuses.add(OrderStatus.ACCEPTED);
182
	                orders = tclient.getAllOrders(statuses, 0, 0, warehouseId);
4361 rajveer 183
	            } catch (Exception e) {
184
	            	logger.error("Error while getting order information", e);
185
	                return baosPDF; 
186
				}
187
            }else{
188
            	orders.add(tclient.getOrder(orderId));	
189
            }
2787 chandransh 190
 
4361 rajveer 191
            for(Order order: orders){
192
            	Warehouse warehouse = null;
193
            	Provider provider = null;
194
            	String destCode = null;
195
            	int barcodeFontSize = 0;
196
            	try {
197
            		warehouse = iclient.getWarehouse(order.getWarehouse_id());
198
            		long providerId = order.getLogistics_provider_id();
199
            		provider = logisticsClient.getProvider(providerId);
5527 anupam.sin 200
            		if(provider.getPickup().equals(PickUpType.SELF) || provider.getPickup().equals(PickUpType.RUNNER))
201
            			destCode = provider.getPickup().toString();
5387 rajveer 202
            		else
203
            			destCode = logisticsClient.getDestinationCode(providerId, order.getCustomer_pincode());
204
 
4361 rajveer 205
            		barcodeFontSize = Integer.parseInt(ConfigClient.getClient().get(provider.getName().toLowerCase() + "_barcode_fontsize"));
5948 mandeep.dh 206
            	} catch (InventoryServiceException ise) {
4361 rajveer 207
            		logger.error("Error while getting the warehouse information.", ise);
208
            		return baosPDF;
209
            	} catch (LogisticsServiceException lse) {
210
            		logger.error("Error while getting the provider information.", lse);
211
            		return baosPDF;
212
            	} catch (ConfigException ce) {
213
            		logger.error("Error while getting the fontsize for the given provider", ce);
214
            		return baosPDF;
215
            	} catch (TException te) {
216
            		logger.error("Error while getting some essential information from the services", te);
217
            		return baosPDF;
218
            	}
2787 chandransh 219
 
4361 rajveer 220
	            PdfPTable dispatchAdviceTable = getDispatchAdviceTable(order, warehouse, provider, barcodeFontSize, destCode, withBill);
221
	            dispatchAdviceTable.setSpacingAfter(10.0f);
222
	            dispatchAdviceTable.setWidthPercentage(90.0f);
223
 
224
	            document.add(dispatchAdviceTable);
225
	            if(withBill){
226
	                PdfPTable taxTable = getTaxCumRetailInvoiceTable(order, provider);
227
	                taxTable.setSpacingBefore(5.0f);
228
	                taxTable.setWidthPercentage(90.0f);
229
	                document.add(new DottedLineSeparator());
230
	                document.add(taxTable);
231
	            }else{
232
	            	PdfPTable extraInfoTable = getExtraInfoTable(order, provider, 16);
233
	            	extraInfoTable.setSpacingBefore(5.0f);
234
	            	extraInfoTable.setWidthPercentage(90.0f);
235
	                document.add(new DottedLineSeparator());
236
	                document.add(extraInfoTable);
237
	            }
238
	            document.newPage();
2843 chandransh 239
            }
2787 chandransh 240
            document.close();
241
            baosPDF.close();
4747 rajveer 242
         // Adding facility to store the bill on the local directory. This will happen for only for Mahipalpur warehouse.
243
            if(withBill && !printAll){
244
            	Calendar cal = new GregorianCalendar();
245
            	cal.setTimeInMillis(orders.get(0).getBilling_timestamp());
246
            	int year = cal.get(Calendar.YEAR);
247
      		  	int month = cal.get(Calendar.MONTH);
248
      		  	int day = cal.get(Calendar.DAY_OF_MONTH);
249
      		  	String dirPath = "/SaholicInvoices" + File.separator + year + File.separator  + month + File.separator + day;
250
      		  	File dirFile = new File(dirPath);
251
      		  	if(!dirFile.exists()){
252
      		  		dirFile.mkdirs();	
253
      		  	}
254
      		  	File f = new File( dirPath + File.separator + orderId + ".pdf");
255
                FileOutputStream fos = new FileOutputStream(f);
256
                baosPDF.writeTo(fos);
257
            }
2787 chandransh 258
        } catch (Exception e) {
259
            logger.error("Error while generating Invoice: ", e);
260
        }
261
        return baosPDF;
262
    }
263
 
2915 chandransh 264
    private PdfPTable getDispatchAdviceTable(Order order, Warehouse warehouse, Provider provider, float barcodeFontSize, String destCode, boolean withBill){
2787 chandransh 265
        Font barCodeFont = getBarCodeFont(provider, barcodeFontSize);
266
 
267
        PdfPTable table = new PdfPTable(1);
268
        table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
269
 
3065 chandransh 270
        PdfPTable logoTable = new PdfPTable(2);
271
        logoTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
272
        logoTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
273
        logoTable.getDefaultCell().setVerticalAlignment(Element.ALIGN_BOTTOM);
274
        logoTable.addCell(getLogoCell());
5556 rajveer 275
        if(order.isLogisticsCod())
3065 chandransh 276
            logoTable.addCell(new Phrase("COD   ", helvetica28));
4550 rajveer 277
        else
278
        	logoTable.addCell(new Phrase("   ", helvetica28));
2787 chandransh 279
        PdfPCell titleCell = getTitleCell();
5556 rajveer 280
        PdfPTable customerTable = getCustomerAddressTable(order, destCode, false, helvetica12, false);
2787 chandransh 281
        PdfPTable providerInfoTable = getProviderTable(order, provider, barCodeFont);
282
 
283
        PdfPTable dispatchTable = new PdfPTable(new float[]{0.5f, 0.1f, 0.4f});
284
        dispatchTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
285
        dispatchTable.addCell(customerTable);
286
        dispatchTable.addCell(new Phrase(" "));
287
        dispatchTable.addCell(providerInfoTable);
288
 
5684 mandeep.dh 289
        Warehouse shippingLocation = CatalogUtils.getWarehouse(warehouse.getShippingWarehouseId());
290
        PdfPTable invoiceTable = getTopInvoiceTable(order, shippingLocation.getTinNumber());
291
        PdfPCell addressCell = getAddressCell(shippingLocation.getLocation() +
292
                                    "\nPIN " + warehouse.getPincode() + "\n\n");
293
 
3065 chandransh 294
        PdfPTable chargesTable = new PdfPTable(1);
295
        chargesTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
296
        chargesTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
5556 rajveer 297
        if(order.isLogisticsCod()){
3065 chandransh 298
            chargesTable.addCell(new Phrase("AMOUNT TO BE COLLECTED : Rs " + order.getTotal_amount(), helveticaBold12));
5703 rajveer 299
            chargesTable.addCell(new Phrase("RTO ADDRESS:DEL/HPW/111116"));
3065 chandransh 300
        } else {
301
            chargesTable.addCell(new Phrase("Do not pay any extra charges to the Courier."));  
302
        }
303
 
304
        PdfPTable addressAndNoteTable = new PdfPTable(new float[]{0.3f, 0.7f});
305
        addressAndNoteTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
306
        addressAndNoteTable.addCell(addressCell);
307
        addressAndNoteTable.addCell(chargesTable);
308
 
309
        table.addCell(logoTable);
2787 chandransh 310
        table.addCell(titleCell);
311
        table.addCell(dispatchTable);
312
        table.addCell(invoiceTable);
313
        table.addCell(new Phrase("If undelivered, return to:", helvetica10));
3065 chandransh 314
        table.addCell(addressAndNoteTable);
2787 chandransh 315
        return table;
316
    }
317
 
318
    private Font getBarCodeFont(Provider provider, float barcodeFontSize) {
319
        String fontPath = InvoiceGenerationService.class.getResource("/" + provider.getName().toLowerCase() + "/barcode.TTF").getPath();
320
        FontFactoryImp ttfFontFactory = new FontFactoryImp();
321
        ttfFontFactory.register(fontPath, "barcode");
322
        Font barCodeFont = ttfFontFactory.getFont("barcode", BaseFont.CP1252, true, barcodeFontSize);
323
        return barCodeFont;
324
    }
325
 
326
    private PdfPCell getLogoCell() {
327
        String logoPath = InvoiceGenerationService.class.getResource("/logo.jpg").getPath();
328
        PdfPCell logoCell;
329
        try {
330
            logoCell = new PdfPCell(Image.getInstance(logoPath), false);
331
        } catch (Exception e) {
332
            //Too Many exceptions to catch here: BadElementException, MalformedURLException and IOException
333
            logger.warn("Couldn't load the Saholic logo: ", e);
334
            logoCell = new PdfPCell(new Phrase("Saholic Logo"));
335
        }
336
        logoCell.setBorder(Rectangle.NO_BORDER);
337
        return logoCell;
338
    }
339
 
340
    private PdfPCell getTitleCell() {
341
        PdfPCell titleCell = new PdfPCell(new Phrase("Dispatch Advice", helveticaBold12));
342
        titleCell.setHorizontalAlignment(Element.ALIGN_CENTER);
343
        titleCell.setBorder(Rectangle.NO_BORDER);
344
        return titleCell;
345
    }
346
 
347
    private PdfPTable getProviderTable(Order order, Provider provider, Font barCodeFont) {
348
        PdfPTable providerInfoTable = new PdfPTable(1);
349
        providerInfoTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
350
        PdfPCell providerNameCell = new PdfPCell(new Phrase(provider.getName(), helveticaBold12));
351
        providerNameCell.setHorizontalAlignment(Element.ALIGN_LEFT);
352
        providerNameCell.setBorder(Rectangle.NO_BORDER);
353
 
354
        PdfPCell awbNumberCell = new PdfPCell(new Paragraph("*" + order.getAirwaybill_no() + "*", barCodeFont));
355
        awbNumberCell.setPaddingTop(20.0f);
356
        awbNumberCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
357
        awbNumberCell.setBorder(Rectangle.NO_BORDER);
358
 
359
        providerInfoTable.addCell(providerNameCell);
360
        providerInfoTable.addCell(awbNumberCell);
5556 rajveer 361
        if(order.isLogisticsCod())
3065 chandransh 362
            providerInfoTable.addCell(new Phrase("Account No : " + provider.getDetails().get(DeliveryType.COD).getAccountNo(), helvetica8));
363
        else
364
            providerInfoTable.addCell(new Phrase("Account No : " + provider.getDetails().get(DeliveryType.PREPAID).getAccountNo(), helvetica8));
5980 rajveer 365
        Date awbDate;
366
        if(order.getBilling_timestamp() == 0){
367
        	awbDate = new Date();
368
        }else{
369
        	awbDate = new Date(order.getBilling_timestamp());
370
        }
371
        providerInfoTable.addCell(new Phrase("AWB Date   : " + DateFormat.getDateInstance(DateFormat.MEDIUM).format(awbDate), helvetica8));
2787 chandransh 372
        providerInfoTable.addCell(new Phrase("Weight         : " + order.getTotal_weight() + " Kg", helvetica8));
373
        return providerInfoTable;
374
    }
375
 
2915 chandransh 376
    private PdfPTable getTopInvoiceTable(Order order, String tinNo){
2787 chandransh 377
        PdfPTable invoiceTable = new PdfPTable(new float[]{0.2f, 0.2f, 0.3f, 0.1f, 0.1f, 0.1f});
378
        invoiceTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
379
 
380
        invoiceTable.addCell(getInvoiceTableHeader(6));
381
 
382
        invoiceTable.addCell(new Phrase("Order No", helvetica8));
383
        invoiceTable.addCell(new Phrase("Paymode", helvetica8));
384
        invoiceTable.addCell(new Phrase("Product Name", helvetica8));
385
        invoiceTable.addCell(new Phrase("Quantity", helvetica8));
386
        invoiceTable.addCell(new Phrase("Rate", helvetica8));
387
        invoiceTable.addCell(new Phrase("Amount", helvetica8));
388
        populateTopInvoiceTable(order, invoiceTable);
389
 
390
        invoiceTable.addCell(getTotalCell(4));      
391
        invoiceTable.addCell(getRupeesCell());
392
        invoiceTable.addCell(getTotalAmountCell(order.getTotal_amount()));
393
 
394
        PdfPCell tinCell = new PdfPCell(new Phrase("TIN NO. " + tinNo, helvetica8));
395
        tinCell.setColspan(6);
396
        tinCell.setPadding(2);
397
        invoiceTable.addCell(tinCell);
398
 
399
        return invoiceTable;
400
    }
401
 
402
    private void populateTopInvoiceTable(Order order, PdfPTable invoiceTable) {
403
        List<LineItem> lineitems = order.getLineitems();
404
        for (LineItem lineitem : lineitems) {
405
            invoiceTable.addCell(new Phrase(order.getId() + "", helvetica8));
5908 anupam.sin 406
            if(order.getPickupStoreId() > 0 && order.isCod() == true)
5856 anupam.sin 407
                invoiceTable.addCell(new Phrase("In-Store", helvetica8));
408
            else if (order.isCod())
409
                invoiceTable.addCell(new Phrase("COD", helvetica8));
410
            else
411
                invoiceTable.addCell(new Phrase("Prepaid", helvetica8));
412
            /*
5556 rajveer 413
            if(order.isLogisticsCod())
3065 chandransh 414
                invoiceTable.addCell(new Phrase("COD", helvetica8));
415
            else
416
                invoiceTable.addCell(new Phrase("Prepaid", helvetica8));
5856 anupam.sin 417
            */
2787 chandransh 418
            invoiceTable.addCell(getProductNameCell(lineitem, false));
419
 
420
            invoiceTable.addCell(new Phrase(lineitem.getQuantity() + "", helvetica8));
421
 
422
            invoiceTable.addCell(getPriceCell(lineitem.getUnit_price()));
423
 
424
            invoiceTable.addCell(getPriceCell(lineitem.getTotal_price()));
425
        }
426
    }
427
 
2915 chandransh 428
    private PdfPCell getAddressCell(String address) {
429
        Paragraph addressParagraph = new Paragraph(address, new Font(FontFamily.TIMES_ROMAN, 8f));
2787 chandransh 430
        PdfPCell addressCell = new PdfPCell();
431
        addressCell.addElement(addressParagraph);
432
        addressCell.setHorizontalAlignment(Element.ALIGN_LEFT);
433
        addressCell.setBorder(Rectangle.NO_BORDER);
434
        return addressCell;
435
    }
436
 
437
    private PdfPTable getTaxCumRetailInvoiceTable(Order order, Provider provider){
438
        PdfPTable taxTable = new PdfPTable(1);
5527 anupam.sin 439
        Phrase phrase = null;
2787 chandransh 440
        taxTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
441
        taxTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
442
 
5527 anupam.sin 443
        if (order.getOrderType().equals(OrderType.B2B)) {
444
            phrase = new Phrase("TAX INVOICE", helveticaBold12);
445
        } else {
446
            phrase = new Phrase("RETAIL INVOICE", helveticaBold12);
447
        }
448
        PdfPCell retailInvoiceTitleCell = new PdfPCell(phrase);
2787 chandransh 449
        retailInvoiceTitleCell.setHorizontalAlignment(Element.ALIGN_CENTER);
450
        retailInvoiceTitleCell.setBorder(Rectangle.NO_BORDER);
451
 
452
        Paragraph sorlAddress = new Paragraph(ourAddress + "TIN NO. " + tinNo, new Font(FontFamily.TIMES_ROMAN, 8f, Element.ALIGN_CENTER));
453
        PdfPCell sorlAddressCell = new PdfPCell(sorlAddress);
454
        sorlAddressCell.addElement(sorlAddress);
455
        sorlAddressCell.setHorizontalAlignment(Element.ALIGN_CENTER);
456
 
5556 rajveer 457
        PdfPTable customerAddress = getCustomerAddressTable(order, null, true, helvetica8, true);
2787 chandransh 458
        PdfPTable orderDetails = getOrderDetails(order, provider);
459
 
460
        PdfPTable addrAndOrderDetailsTable = new PdfPTable(new float[]{0.5f, 0.1f, 0.4f});
461
        addrAndOrderDetailsTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
462
        addrAndOrderDetailsTable.addCell(customerAddress);
463
        addrAndOrderDetailsTable.addCell(new Phrase(" "));
464
        addrAndOrderDetailsTable.addCell(orderDetails);
465
 
466
        boolean isVAT = order.getCustomer_pincode().startsWith(delhiPincodePrefix);
467
        PdfPTable invoiceTable = getBottomInvoiceTable(order, isVAT);
468
 
5527 anupam.sin 469
        PdfPCell disclaimerCell = new PdfPCell(new Phrase("Goods once sold will not be taken back.\nAll disputes subject to Delhi Jurisdiction.\nThis is a Computer generated Invoice.", helvetica8));
2787 chandransh 470
        disclaimerCell.setHorizontalAlignment(Element.ALIGN_LEFT);
471
        disclaimerCell.setBorder(Rectangle.NO_BORDER);
472
 
473
        taxTable.addCell(retailInvoiceTitleCell);
474
        taxTable.addCell(sorlAddress);
475
        taxTable.addCell(addrAndOrderDetailsTable);
476
        taxTable.addCell(invoiceTable);
477
        taxTable.addCell(disclaimerCell);
478
 
479
        return taxTable;
480
    }
481
 
5556 rajveer 482
    private PdfPTable getCustomerAddressTable(Order order, String destCode, boolean showPaymentMode, Font font, boolean forInvoce){
2787 chandransh 483
        PdfPTable customerTable = new PdfPTable(1);
484
        customerTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
5556 rajveer 485
        if(forInvoce || order.getPickupStoreId() == 0){
486
	        customerTable.addCell(new Phrase(order.getCustomer_name(), font));
487
	        customerTable.addCell(new Phrase(order.getCustomer_address1(), font));
488
	        customerTable.addCell(new Phrase(order.getCustomer_address2(), font));
489
	        customerTable.addCell(new Phrase(order.getCustomer_city() + "," + order.getCustomer_state(), font));
490
	        if(destCode != null)
491
	            customerTable.addCell(new Phrase(order.getCustomer_pincode() + " - " + destCode, helvetica16));
492
	        else
493
	            customerTable.addCell(new Phrase(order.getCustomer_pincode(), font));
494
	        customerTable.addCell(new Phrase("Phone :" + order.getCustomer_mobilenumber(), font));
495
        }else{
496
        	try {
497
				in.shop2020.logistics.LogisticsService.Client lclient = (new LogisticsClient()).getClient();
498
	            PickupStore store = lclient.getPickupStore(order.getPickupStoreId());
5757 rajveer 499
			    customerTable.addCell(new Phrase(order.getCustomer_name() + " \nc/o " + store.getName(), font));
5556 rajveer 500
		        customerTable.addCell(new Phrase(store.getLine1(), font));
501
		        customerTable.addCell(new Phrase(store.getLine2(), font));
502
		        customerTable.addCell(new Phrase(store.getCity() + "," + store.getState(), font));
503
		        if(destCode != null)
504
		            customerTable.addCell(new Phrase(store.getPin() + " - " + destCode, helvetica16));
505
		        else
506
		            customerTable.addCell(new Phrase(store.getPin(), font));
507
		        customerTable.addCell(new Phrase("Phone :" + store.getPhone(), font));
508
			} catch (TException e) {
509
				// TODO Auto-generated catch block
510
				e.printStackTrace();
511
			}
512
 
513
        }
514
 
5527 anupam.sin 515
        if(order.getOrderType().equals(OrderType.B2B)) {
516
            String tin = null;
517
            in.shop2020.model.v1.order.TransactionService.Client tclient = tsc.getClient();
518
            List<Attribute> attributes;
519
            try {
520
                attributes = tclient.getAllAttributesForOrderId(order.getId());
521
 
522
                for(Attribute attribute : attributes) {
523
                    if(attribute.getName().equals("tinNumber")) {
524
                        tin = attribute.getValue();
525
                    }
526
                }
527
                if (tin != null) {
528
                    customerTable.addCell(new Phrase("TIN :" + tin, font));
529
                }
530
 
531
            } catch (Exception e) {
532
                logger.error("Error while getting order attributes", e);
533
            }
534
        }
5856 anupam.sin 535
        /*
2787 chandransh 536
        if(showPaymentMode){
537
            customerTable.addCell(new Phrase(" ", font));
538
            customerTable.addCell(new Phrase("Payment Mode: Prepaid", font));
5856 anupam.sin 539
        }*/
2787 chandransh 540
        return customerTable;
541
    }
542
 
543
    private PdfPTable getOrderDetails(Order order, Provider provider){
544
        PdfPTable orderTable = new PdfPTable(new float[]{0.4f, 0.6f});
545
        orderTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
546
 
547
        orderTable.addCell(new Phrase("Invoice No:", helvetica8));
548
        orderTable.addCell(new Phrase(order.getInvoice_number(), helvetica8));
549
 
550
        orderTable.addCell(new Phrase("Date:", helvetica8));
551
        orderTable.addCell(new Phrase(DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date(order.getBilling_timestamp())), helvetica8));
552
 
553
        orderTable.addCell(new Phrase(" "));
554
        orderTable.addCell(new Phrase(" "));
555
 
556
        orderTable.addCell(new Phrase("Order ID:", helvetica8));
557
        orderTable.addCell(new Phrase("" + order.getId(), helvetica8));
558
 
559
        orderTable.addCell(new Phrase("Order Date:", helvetica8));
560
        orderTable.addCell(new Phrase(DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date(order.getCreated_timestamp())), helvetica8));
561
 
562
        orderTable.addCell(new Phrase("Courier:", helvetica8));
563
        orderTable.addCell(new Phrase(provider.getName(), helvetica8));
564
 
565
        orderTable.addCell(new Phrase("AWB No:", helvetica8));
566
        orderTable.addCell(new Phrase(order.getAirwaybill_no(), helvetica8));
567
 
568
        orderTable.addCell(new Phrase("AWB Date:", helvetica8));
5585 mandeep.dh 569
        orderTable.addCell(new Phrase(DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date(order.getBilling_timestamp())), helvetica8));
2787 chandransh 570
 
571
        return orderTable;
572
    }
573
 
574
    private PdfPTable getBottomInvoiceTable(Order order, boolean isVAT){
575
        PdfPTable invoiceTable = new PdfPTable(new float[]{0.2f, 0.5f, 0.1f, 0.1f, 0.1f});
576
        invoiceTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
577
 
578
        invoiceTable.addCell(getInvoiceTableHeader(5));
579
 
580
        invoiceTable.addCell(new Phrase("Sl. No.", helveticaBold8));
581
        invoiceTable.addCell(new Phrase("Description", helveticaBold8));
582
        invoiceTable.addCell(new Phrase("Quantity", helveticaBold8));
583
        invoiceTable.addCell(new Phrase("Rate (Rs)", helveticaBold8));
584
        invoiceTable.addCell(new Phrase("Amount (Rs)", helveticaBold8));
585
 
586
        double orderAmount = order.getTotal_amount();
4951 mandeep.dh 587
        double rate = getTaxRate(order.getLineitems().get(0).getUnit_price());
2787 chandransh 588
        double salesTax = (rate * orderAmount)/(100 + rate);
589
 
590
        populateBottomInvoiceTable(order, invoiceTable, rate);
591
 
592
        PdfPCell salesTaxCell = getPriceCell(salesTax);
593
 
594
        invoiceTable.addCell(getVATLabelCell(isVAT));
595
        invoiceTable.addCell(new Phrase(rate + "%", helvetica8));
596
        invoiceTable.addCell(salesTaxCell);
597
 
598
        invoiceTable.addCell(getEmptyCell(5));
599
 
600
        invoiceTable.addCell(getTotalCell(3));
601
        invoiceTable.addCell(getRupeesCell());
602
        invoiceTable.addCell(getTotalAmountCell(orderAmount));
603
 
604
        invoiceTable.addCell(new Phrase("Amount in Words:", helvetica8));
605
        invoiceTable.addCell(getAmountInWordsCell(orderAmount));
606
 
607
        invoiceTable.addCell(getEOECell(5));
608
 
609
        return invoiceTable;
610
    }
611
 
612
    private PdfPCell getInvoiceTableHeader(int colspan) {
613
        PdfPCell invoiceTableHeader = new PdfPCell(new Phrase("Order Details:", helveticaBold12));
614
        invoiceTableHeader.setBorder(Rectangle.NO_BORDER);
615
        invoiceTableHeader.setColspan(colspan);
616
        invoiceTableHeader.setPaddingTop(10);
617
        return invoiceTableHeader;
618
    }
619
 
620
    private double getTaxRate(double orderAmount) {
621
        double rate;
5872 mandeep.dh 622
        if(orderAmount < salesTaxCutOff){
2787 chandransh 623
            rate = salesTaxLowRate;
624
        } else {
625
            rate = salesTaxHighRate;
626
        }
627
        return rate;
628
    }
629
 
630
    private void populateBottomInvoiceTable(Order order, PdfPTable invoiceTable, double rate) {
631
        for (LineItem lineitem : order.getLineitems()) {
632
            invoiceTable.addCell(new Phrase("" + order.getId() , helvetica8));
633
 
634
            invoiceTable.addCell(getProductNameCell(lineitem, true));
635
 
636
            invoiceTable.addCell(new Phrase("" + lineitem.getQuantity(), helvetica8));
637
 
638
            double itemPrice = lineitem.getUnit_price();
639
            double showPrice = (100 * itemPrice)/(100 + rate);
640
            invoiceTable.addCell(getPriceCell(showPrice)); //Unit Price Cell
641
 
642
            double totalPrice = lineitem.getTotal_price();
643
            showPrice = (100 * totalPrice)/(100 + rate);
644
            invoiceTable.addCell(getPriceCell(showPrice));  //Total Price Cell
645
        }
646
    }
647
 
648
    private PdfPCell getProductNameCell(LineItem lineitem, boolean appendIMEI) {
649
        String itemName = getItemDisplayName(lineitem, appendIMEI);
650
        PdfPCell productNameCell = new PdfPCell(new Phrase(itemName, helvetica8));
651
        productNameCell.setHorizontalAlignment(Element.ALIGN_LEFT);
652
        return productNameCell;
653
    }
654
 
655
    private PdfPCell getPriceCell(double price) {
656
        PdfPCell totalPriceCell = new PdfPCell(new Phrase(amountFormat.format(price), helvetica8));
657
        totalPriceCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
658
        return totalPriceCell;
659
    }
660
 
661
    private PdfPCell getVATLabelCell(boolean isVAT) {
662
        PdfPCell vatCell = null;
663
        if(isVAT){
664
            vatCell = new PdfPCell(new Phrase("VAT", helveticaBold8));
665
        } else {
666
            vatCell = new PdfPCell(new Phrase("CST", helveticaBold8));
667
        }
668
        vatCell.setColspan(3);
669
        vatCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
670
        return vatCell;
671
    }
672
 
673
    private PdfPCell getEmptyCell(int colspan) {
674
        PdfPCell emptyCell = new PdfPCell(new Phrase(" ", helvetica8));
675
        emptyCell.setColspan(colspan);
676
        return emptyCell;
677
    }
678
 
679
    private PdfPCell getTotalCell(int colspan) {
680
        PdfPCell totalCell = new PdfPCell(new Phrase("Total", helveticaBold8));
681
        totalCell.setColspan(colspan);
682
        totalCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
683
        return totalCell;
684
    }
685
 
686
    private PdfPCell getRupeesCell() {
687
        PdfPCell rupeesCell = new PdfPCell(new Phrase("Rs.", helveticaBold8));
688
        rupeesCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
689
        return rupeesCell;
690
    }
691
 
692
    private PdfPCell getTotalAmountCell(double orderAmount) {
693
        PdfPCell totalAmountCell = new PdfPCell(new Phrase(amountFormat.format(orderAmount), helveticaBold8));
694
        totalAmountCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
695
        return totalAmountCell;
696
    }
697
 
698
    /**
699
     * This method uses ICU4J libraries to convert the given amount into words
700
     * of Indian locale.
701
     * 
702
     * @param orderAmount
703
     *            The amount to convert.
704
     * @return the string representation of the given amount.
705
     */
706
    private PdfPCell getAmountInWordsCell(double orderAmount) {
707
        RuleBasedNumberFormat amountInWordsFormat = new RuleBasedNumberFormat(indianLocale, RuleBasedNumberFormat.SPELLOUT);
708
        StringBuilder amountInWords = new StringBuilder("Rs. ");
709
        amountInWords.append(WordUtils.capitalize(amountInWordsFormat.format((int)orderAmount)));
710
        amountInWords.append(" and ");
711
        amountInWords.append(WordUtils.capitalize(amountInWordsFormat.format((int)(orderAmount*100)%100)));
712
        amountInWords.append(" paise");
713
 
714
        PdfPCell amountInWordsCell= new PdfPCell(new Phrase(amountInWords.toString(), helveticaBold8));
715
        amountInWordsCell.setColspan(4);
716
        return amountInWordsCell;
717
    }
718
 
719
    /**
720
     * Returns the item name to be displayed in the invoice table.
721
     * 
722
     * @param lineitem
723
     *            The line item whose name has to be displayed
724
     * @param appendIMEI
725
     *            Whether to attach the IMEI No. to the item name
726
     * @return The name to be displayed for the given line item.
727
     */
728
    private String getItemDisplayName(LineItem lineitem, boolean appendIMEI){
729
        StringBuffer itemName = new StringBuffer();
730
        if(lineitem.getBrand()!= null)
731
            itemName.append(lineitem.getBrand() + " ");
732
        if(lineitem.getModel_name() != null)
733
            itemName.append(lineitem.getModel_name() + " ");
734
        if(lineitem.getModel_number() != null )
735
            itemName.append(lineitem.getModel_number() + " ");
736
        if(lineitem.getColor() != null && !lineitem.getColor().trim().equals("NA"))
737
            itemName.append("("+lineitem.getColor()+")");
4659 mandeep.dh 738
        if(appendIMEI && lineitem.isSetSerial_number()){
739
            itemName.append("\nIMEI No. " + lineitem.getSerial_number());
2787 chandransh 740
        }
741
 
742
        return itemName.toString();
743
    }
744
 
745
    /**
746
     * 
747
     * @param colspan
748
     * @return a PdfPCell containing the E&amp;OE text and spanning the given
749
     *         no. of columns
750
     */
751
    private PdfPCell getEOECell(int colspan) {
752
        PdfPCell eoeCell = new PdfPCell(new Phrase("E & O.E", helvetica8));
753
        eoeCell.setColspan(colspan);
754
        eoeCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
755
        return eoeCell;
756
    }
757
 
4262 rajveer 758
    private PdfPTable getExtraInfoTable(Order order, Provider provider, float barcodeFontSize){
759
        PdfPTable extraInfoTable = new PdfPTable(1);
760
        extraInfoTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
761
        extraInfoTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
762
 
763
        String fontPath = InvoiceGenerationService.class.getResource("/saholic.TTF").getPath();
764
        FontFactoryImp ttfFontFactory = new FontFactoryImp();
765
        ttfFontFactory.register(fontPath, "barcode");
766
        Font barCodeFont = ttfFontFactory.getFont("barcode", BaseFont.CP1252, true, barcodeFontSize);
767
 
768
        PdfPCell extraInfoCell = new PdfPCell(new Paragraph( "*" + order.getId() + "*        *" + order.getCustomer_name() + "*        *"  + order.getTotal_amount() + "*", barCodeFont));
769
        extraInfoCell.setPaddingTop(20.0f);
770
        extraInfoCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
771
        extraInfoCell.setBorder(Rectangle.NO_BORDER);
772
 
773
        extraInfoTable.addCell(extraInfoCell);
774
 
775
 
776
        return extraInfoTable;
777
    }
5527 anupam.sin 778
 
2787 chandransh 779
    public static void main(String[] args) throws IOException {
780
        InvoiceGenerationService invoiceGenerationService = new InvoiceGenerationService();
5527 anupam.sin 781
        long orderId = 148574;
4361 rajveer 782
        ByteArrayOutputStream baos = invoiceGenerationService.generateInvoice(orderId, false, false, 1);
2787 chandransh 783
        String userHome = System.getProperty("user.home");
784
        File f = new File(userHome + "/invoice-" + orderId + ".pdf");
785
        FileOutputStream fos = new FileOutputStream(f);
786
        baos.writeTo(fos);
787
        System.out.println("Invoice generated.");
788
    }
789
}