Subversion Repositories SmartDukaan

Rev

Rev 5945 | Rev 5980 | 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));
5585 mandeep.dh 365
        providerInfoTable.addCell(new Phrase("AWB Date   : " + DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date(order.getBilling_timestamp())), helvetica8));
2787 chandransh 366
        providerInfoTable.addCell(new Phrase("Weight         : " + order.getTotal_weight() + " Kg", helvetica8));
367
        return providerInfoTable;
368
    }
369
 
2915 chandransh 370
    private PdfPTable getTopInvoiceTable(Order order, String tinNo){
2787 chandransh 371
        PdfPTable invoiceTable = new PdfPTable(new float[]{0.2f, 0.2f, 0.3f, 0.1f, 0.1f, 0.1f});
372
        invoiceTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
373
 
374
        invoiceTable.addCell(getInvoiceTableHeader(6));
375
 
376
        invoiceTable.addCell(new Phrase("Order No", helvetica8));
377
        invoiceTable.addCell(new Phrase("Paymode", helvetica8));
378
        invoiceTable.addCell(new Phrase("Product Name", helvetica8));
379
        invoiceTable.addCell(new Phrase("Quantity", helvetica8));
380
        invoiceTable.addCell(new Phrase("Rate", helvetica8));
381
        invoiceTable.addCell(new Phrase("Amount", helvetica8));
382
        populateTopInvoiceTable(order, invoiceTable);
383
 
384
        invoiceTable.addCell(getTotalCell(4));      
385
        invoiceTable.addCell(getRupeesCell());
386
        invoiceTable.addCell(getTotalAmountCell(order.getTotal_amount()));
387
 
388
        PdfPCell tinCell = new PdfPCell(new Phrase("TIN NO. " + tinNo, helvetica8));
389
        tinCell.setColspan(6);
390
        tinCell.setPadding(2);
391
        invoiceTable.addCell(tinCell);
392
 
393
        return invoiceTable;
394
    }
395
 
396
    private void populateTopInvoiceTable(Order order, PdfPTable invoiceTable) {
397
        List<LineItem> lineitems = order.getLineitems();
398
        for (LineItem lineitem : lineitems) {
399
            invoiceTable.addCell(new Phrase(order.getId() + "", helvetica8));
5908 anupam.sin 400
            if(order.getPickupStoreId() > 0 && order.isCod() == true)
5856 anupam.sin 401
                invoiceTable.addCell(new Phrase("In-Store", helvetica8));
402
            else if (order.isCod())
403
                invoiceTable.addCell(new Phrase("COD", helvetica8));
404
            else
405
                invoiceTable.addCell(new Phrase("Prepaid", helvetica8));
406
            /*
5556 rajveer 407
            if(order.isLogisticsCod())
3065 chandransh 408
                invoiceTable.addCell(new Phrase("COD", helvetica8));
409
            else
410
                invoiceTable.addCell(new Phrase("Prepaid", helvetica8));
5856 anupam.sin 411
            */
2787 chandransh 412
            invoiceTable.addCell(getProductNameCell(lineitem, false));
413
 
414
            invoiceTable.addCell(new Phrase(lineitem.getQuantity() + "", helvetica8));
415
 
416
            invoiceTable.addCell(getPriceCell(lineitem.getUnit_price()));
417
 
418
            invoiceTable.addCell(getPriceCell(lineitem.getTotal_price()));
419
        }
420
    }
421
 
2915 chandransh 422
    private PdfPCell getAddressCell(String address) {
423
        Paragraph addressParagraph = new Paragraph(address, new Font(FontFamily.TIMES_ROMAN, 8f));
2787 chandransh 424
        PdfPCell addressCell = new PdfPCell();
425
        addressCell.addElement(addressParagraph);
426
        addressCell.setHorizontalAlignment(Element.ALIGN_LEFT);
427
        addressCell.setBorder(Rectangle.NO_BORDER);
428
        return addressCell;
429
    }
430
 
431
    private PdfPTable getTaxCumRetailInvoiceTable(Order order, Provider provider){
432
        PdfPTable taxTable = new PdfPTable(1);
5527 anupam.sin 433
        Phrase phrase = null;
2787 chandransh 434
        taxTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
435
        taxTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
436
 
5527 anupam.sin 437
        if (order.getOrderType().equals(OrderType.B2B)) {
438
            phrase = new Phrase("TAX INVOICE", helveticaBold12);
439
        } else {
440
            phrase = new Phrase("RETAIL INVOICE", helveticaBold12);
441
        }
442
        PdfPCell retailInvoiceTitleCell = new PdfPCell(phrase);
2787 chandransh 443
        retailInvoiceTitleCell.setHorizontalAlignment(Element.ALIGN_CENTER);
444
        retailInvoiceTitleCell.setBorder(Rectangle.NO_BORDER);
445
 
446
        Paragraph sorlAddress = new Paragraph(ourAddress + "TIN NO. " + tinNo, new Font(FontFamily.TIMES_ROMAN, 8f, Element.ALIGN_CENTER));
447
        PdfPCell sorlAddressCell = new PdfPCell(sorlAddress);
448
        sorlAddressCell.addElement(sorlAddress);
449
        sorlAddressCell.setHorizontalAlignment(Element.ALIGN_CENTER);
450
 
5556 rajveer 451
        PdfPTable customerAddress = getCustomerAddressTable(order, null, true, helvetica8, true);
2787 chandransh 452
        PdfPTable orderDetails = getOrderDetails(order, provider);
453
 
454
        PdfPTable addrAndOrderDetailsTable = new PdfPTable(new float[]{0.5f, 0.1f, 0.4f});
455
        addrAndOrderDetailsTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
456
        addrAndOrderDetailsTable.addCell(customerAddress);
457
        addrAndOrderDetailsTable.addCell(new Phrase(" "));
458
        addrAndOrderDetailsTable.addCell(orderDetails);
459
 
460
        boolean isVAT = order.getCustomer_pincode().startsWith(delhiPincodePrefix);
461
        PdfPTable invoiceTable = getBottomInvoiceTable(order, isVAT);
462
 
5527 anupam.sin 463
        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 464
        disclaimerCell.setHorizontalAlignment(Element.ALIGN_LEFT);
465
        disclaimerCell.setBorder(Rectangle.NO_BORDER);
466
 
467
        taxTable.addCell(retailInvoiceTitleCell);
468
        taxTable.addCell(sorlAddress);
469
        taxTable.addCell(addrAndOrderDetailsTable);
470
        taxTable.addCell(invoiceTable);
471
        taxTable.addCell(disclaimerCell);
472
 
473
        return taxTable;
474
    }
475
 
5556 rajveer 476
    private PdfPTable getCustomerAddressTable(Order order, String destCode, boolean showPaymentMode, Font font, boolean forInvoce){
2787 chandransh 477
        PdfPTable customerTable = new PdfPTable(1);
478
        customerTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
5556 rajveer 479
        if(forInvoce || order.getPickupStoreId() == 0){
480
	        customerTable.addCell(new Phrase(order.getCustomer_name(), font));
481
	        customerTable.addCell(new Phrase(order.getCustomer_address1(), font));
482
	        customerTable.addCell(new Phrase(order.getCustomer_address2(), font));
483
	        customerTable.addCell(new Phrase(order.getCustomer_city() + "," + order.getCustomer_state(), font));
484
	        if(destCode != null)
485
	            customerTable.addCell(new Phrase(order.getCustomer_pincode() + " - " + destCode, helvetica16));
486
	        else
487
	            customerTable.addCell(new Phrase(order.getCustomer_pincode(), font));
488
	        customerTable.addCell(new Phrase("Phone :" + order.getCustomer_mobilenumber(), font));
489
        }else{
490
        	try {
491
				in.shop2020.logistics.LogisticsService.Client lclient = (new LogisticsClient()).getClient();
492
	            PickupStore store = lclient.getPickupStore(order.getPickupStoreId());
5757 rajveer 493
			    customerTable.addCell(new Phrase(order.getCustomer_name() + " \nc/o " + store.getName(), font));
5556 rajveer 494
		        customerTable.addCell(new Phrase(store.getLine1(), font));
495
		        customerTable.addCell(new Phrase(store.getLine2(), font));
496
		        customerTable.addCell(new Phrase(store.getCity() + "," + store.getState(), font));
497
		        if(destCode != null)
498
		            customerTable.addCell(new Phrase(store.getPin() + " - " + destCode, helvetica16));
499
		        else
500
		            customerTable.addCell(new Phrase(store.getPin(), font));
501
		        customerTable.addCell(new Phrase("Phone :" + store.getPhone(), font));
502
			} catch (TException e) {
503
				// TODO Auto-generated catch block
504
				e.printStackTrace();
505
			}
506
 
507
        }
508
 
5527 anupam.sin 509
        if(order.getOrderType().equals(OrderType.B2B)) {
510
            String tin = null;
511
            in.shop2020.model.v1.order.TransactionService.Client tclient = tsc.getClient();
512
            List<Attribute> attributes;
513
            try {
514
                attributes = tclient.getAllAttributesForOrderId(order.getId());
515
 
516
                for(Attribute attribute : attributes) {
517
                    if(attribute.getName().equals("tinNumber")) {
518
                        tin = attribute.getValue();
519
                    }
520
                }
521
                if (tin != null) {
522
                    customerTable.addCell(new Phrase("TIN :" + tin, font));
523
                }
524
 
525
            } catch (Exception e) {
526
                logger.error("Error while getting order attributes", e);
527
            }
528
        }
5856 anupam.sin 529
        /*
2787 chandransh 530
        if(showPaymentMode){
531
            customerTable.addCell(new Phrase(" ", font));
532
            customerTable.addCell(new Phrase("Payment Mode: Prepaid", font));
5856 anupam.sin 533
        }*/
2787 chandransh 534
        return customerTable;
535
    }
536
 
537
    private PdfPTable getOrderDetails(Order order, Provider provider){
538
        PdfPTable orderTable = new PdfPTable(new float[]{0.4f, 0.6f});
539
        orderTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
540
 
541
        orderTable.addCell(new Phrase("Invoice No:", helvetica8));
542
        orderTable.addCell(new Phrase(order.getInvoice_number(), helvetica8));
543
 
544
        orderTable.addCell(new Phrase("Date:", helvetica8));
545
        orderTable.addCell(new Phrase(DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date(order.getBilling_timestamp())), helvetica8));
546
 
547
        orderTable.addCell(new Phrase(" "));
548
        orderTable.addCell(new Phrase(" "));
549
 
550
        orderTable.addCell(new Phrase("Order ID:", helvetica8));
551
        orderTable.addCell(new Phrase("" + order.getId(), helvetica8));
552
 
553
        orderTable.addCell(new Phrase("Order Date:", helvetica8));
554
        orderTable.addCell(new Phrase(DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date(order.getCreated_timestamp())), helvetica8));
555
 
556
        orderTable.addCell(new Phrase("Courier:", helvetica8));
557
        orderTable.addCell(new Phrase(provider.getName(), helvetica8));
558
 
559
        orderTable.addCell(new Phrase("AWB No:", helvetica8));
560
        orderTable.addCell(new Phrase(order.getAirwaybill_no(), helvetica8));
561
 
562
        orderTable.addCell(new Phrase("AWB Date:", helvetica8));
5585 mandeep.dh 563
        orderTable.addCell(new Phrase(DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date(order.getBilling_timestamp())), helvetica8));
2787 chandransh 564
 
565
        return orderTable;
566
    }
567
 
568
    private PdfPTable getBottomInvoiceTable(Order order, boolean isVAT){
569
        PdfPTable invoiceTable = new PdfPTable(new float[]{0.2f, 0.5f, 0.1f, 0.1f, 0.1f});
570
        invoiceTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
571
 
572
        invoiceTable.addCell(getInvoiceTableHeader(5));
573
 
574
        invoiceTable.addCell(new Phrase("Sl. No.", helveticaBold8));
575
        invoiceTable.addCell(new Phrase("Description", helveticaBold8));
576
        invoiceTable.addCell(new Phrase("Quantity", helveticaBold8));
577
        invoiceTable.addCell(new Phrase("Rate (Rs)", helveticaBold8));
578
        invoiceTable.addCell(new Phrase("Amount (Rs)", helveticaBold8));
579
 
580
        double orderAmount = order.getTotal_amount();
4951 mandeep.dh 581
        double rate = getTaxRate(order.getLineitems().get(0).getUnit_price());
2787 chandransh 582
        double salesTax = (rate * orderAmount)/(100 + rate);
583
 
584
        populateBottomInvoiceTable(order, invoiceTable, rate);
585
 
586
        PdfPCell salesTaxCell = getPriceCell(salesTax);
587
 
588
        invoiceTable.addCell(getVATLabelCell(isVAT));
589
        invoiceTable.addCell(new Phrase(rate + "%", helvetica8));
590
        invoiceTable.addCell(salesTaxCell);
591
 
592
        invoiceTable.addCell(getEmptyCell(5));
593
 
594
        invoiceTable.addCell(getTotalCell(3));
595
        invoiceTable.addCell(getRupeesCell());
596
        invoiceTable.addCell(getTotalAmountCell(orderAmount));
597
 
598
        invoiceTable.addCell(new Phrase("Amount in Words:", helvetica8));
599
        invoiceTable.addCell(getAmountInWordsCell(orderAmount));
600
 
601
        invoiceTable.addCell(getEOECell(5));
602
 
603
        return invoiceTable;
604
    }
605
 
606
    private PdfPCell getInvoiceTableHeader(int colspan) {
607
        PdfPCell invoiceTableHeader = new PdfPCell(new Phrase("Order Details:", helveticaBold12));
608
        invoiceTableHeader.setBorder(Rectangle.NO_BORDER);
609
        invoiceTableHeader.setColspan(colspan);
610
        invoiceTableHeader.setPaddingTop(10);
611
        return invoiceTableHeader;
612
    }
613
 
614
    private double getTaxRate(double orderAmount) {
615
        double rate;
5872 mandeep.dh 616
        if(orderAmount < salesTaxCutOff){
2787 chandransh 617
            rate = salesTaxLowRate;
618
        } else {
619
            rate = salesTaxHighRate;
620
        }
621
        return rate;
622
    }
623
 
624
    private void populateBottomInvoiceTable(Order order, PdfPTable invoiceTable, double rate) {
625
        for (LineItem lineitem : order.getLineitems()) {
626
            invoiceTable.addCell(new Phrase("" + order.getId() , helvetica8));
627
 
628
            invoiceTable.addCell(getProductNameCell(lineitem, true));
629
 
630
            invoiceTable.addCell(new Phrase("" + lineitem.getQuantity(), helvetica8));
631
 
632
            double itemPrice = lineitem.getUnit_price();
633
            double showPrice = (100 * itemPrice)/(100 + rate);
634
            invoiceTable.addCell(getPriceCell(showPrice)); //Unit Price Cell
635
 
636
            double totalPrice = lineitem.getTotal_price();
637
            showPrice = (100 * totalPrice)/(100 + rate);
638
            invoiceTable.addCell(getPriceCell(showPrice));  //Total Price Cell
639
        }
640
    }
641
 
642
    private PdfPCell getProductNameCell(LineItem lineitem, boolean appendIMEI) {
643
        String itemName = getItemDisplayName(lineitem, appendIMEI);
644
        PdfPCell productNameCell = new PdfPCell(new Phrase(itemName, helvetica8));
645
        productNameCell.setHorizontalAlignment(Element.ALIGN_LEFT);
646
        return productNameCell;
647
    }
648
 
649
    private PdfPCell getPriceCell(double price) {
650
        PdfPCell totalPriceCell = new PdfPCell(new Phrase(amountFormat.format(price), helvetica8));
651
        totalPriceCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
652
        return totalPriceCell;
653
    }
654
 
655
    private PdfPCell getVATLabelCell(boolean isVAT) {
656
        PdfPCell vatCell = null;
657
        if(isVAT){
658
            vatCell = new PdfPCell(new Phrase("VAT", helveticaBold8));
659
        } else {
660
            vatCell = new PdfPCell(new Phrase("CST", helveticaBold8));
661
        }
662
        vatCell.setColspan(3);
663
        vatCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
664
        return vatCell;
665
    }
666
 
667
    private PdfPCell getEmptyCell(int colspan) {
668
        PdfPCell emptyCell = new PdfPCell(new Phrase(" ", helvetica8));
669
        emptyCell.setColspan(colspan);
670
        return emptyCell;
671
    }
672
 
673
    private PdfPCell getTotalCell(int colspan) {
674
        PdfPCell totalCell = new PdfPCell(new Phrase("Total", helveticaBold8));
675
        totalCell.setColspan(colspan);
676
        totalCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
677
        return totalCell;
678
    }
679
 
680
    private PdfPCell getRupeesCell() {
681
        PdfPCell rupeesCell = new PdfPCell(new Phrase("Rs.", helveticaBold8));
682
        rupeesCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
683
        return rupeesCell;
684
    }
685
 
686
    private PdfPCell getTotalAmountCell(double orderAmount) {
687
        PdfPCell totalAmountCell = new PdfPCell(new Phrase(amountFormat.format(orderAmount), helveticaBold8));
688
        totalAmountCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
689
        return totalAmountCell;
690
    }
691
 
692
    /**
693
     * This method uses ICU4J libraries to convert the given amount into words
694
     * of Indian locale.
695
     * 
696
     * @param orderAmount
697
     *            The amount to convert.
698
     * @return the string representation of the given amount.
699
     */
700
    private PdfPCell getAmountInWordsCell(double orderAmount) {
701
        RuleBasedNumberFormat amountInWordsFormat = new RuleBasedNumberFormat(indianLocale, RuleBasedNumberFormat.SPELLOUT);
702
        StringBuilder amountInWords = new StringBuilder("Rs. ");
703
        amountInWords.append(WordUtils.capitalize(amountInWordsFormat.format((int)orderAmount)));
704
        amountInWords.append(" and ");
705
        amountInWords.append(WordUtils.capitalize(amountInWordsFormat.format((int)(orderAmount*100)%100)));
706
        amountInWords.append(" paise");
707
 
708
        PdfPCell amountInWordsCell= new PdfPCell(new Phrase(amountInWords.toString(), helveticaBold8));
709
        amountInWordsCell.setColspan(4);
710
        return amountInWordsCell;
711
    }
712
 
713
    /**
714
     * Returns the item name to be displayed in the invoice table.
715
     * 
716
     * @param lineitem
717
     *            The line item whose name has to be displayed
718
     * @param appendIMEI
719
     *            Whether to attach the IMEI No. to the item name
720
     * @return The name to be displayed for the given line item.
721
     */
722
    private String getItemDisplayName(LineItem lineitem, boolean appendIMEI){
723
        StringBuffer itemName = new StringBuffer();
724
        if(lineitem.getBrand()!= null)
725
            itemName.append(lineitem.getBrand() + " ");
726
        if(lineitem.getModel_name() != null)
727
            itemName.append(lineitem.getModel_name() + " ");
728
        if(lineitem.getModel_number() != null )
729
            itemName.append(lineitem.getModel_number() + " ");
730
        if(lineitem.getColor() != null && !lineitem.getColor().trim().equals("NA"))
731
            itemName.append("("+lineitem.getColor()+")");
4659 mandeep.dh 732
        if(appendIMEI && lineitem.isSetSerial_number()){
733
            itemName.append("\nIMEI No. " + lineitem.getSerial_number());
2787 chandransh 734
        }
735
 
736
        return itemName.toString();
737
    }
738
 
739
    /**
740
     * 
741
     * @param colspan
742
     * @return a PdfPCell containing the E&amp;OE text and spanning the given
743
     *         no. of columns
744
     */
745
    private PdfPCell getEOECell(int colspan) {
746
        PdfPCell eoeCell = new PdfPCell(new Phrase("E & O.E", helvetica8));
747
        eoeCell.setColspan(colspan);
748
        eoeCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
749
        return eoeCell;
750
    }
751
 
4262 rajveer 752
    private PdfPTable getExtraInfoTable(Order order, Provider provider, float barcodeFontSize){
753
        PdfPTable extraInfoTable = new PdfPTable(1);
754
        extraInfoTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
755
        extraInfoTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
756
 
757
        String fontPath = InvoiceGenerationService.class.getResource("/saholic.TTF").getPath();
758
        FontFactoryImp ttfFontFactory = new FontFactoryImp();
759
        ttfFontFactory.register(fontPath, "barcode");
760
        Font barCodeFont = ttfFontFactory.getFont("barcode", BaseFont.CP1252, true, barcodeFontSize);
761
 
762
        PdfPCell extraInfoCell = new PdfPCell(new Paragraph( "*" + order.getId() + "*        *" + order.getCustomer_name() + "*        *"  + order.getTotal_amount() + "*", barCodeFont));
763
        extraInfoCell.setPaddingTop(20.0f);
764
        extraInfoCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
765
        extraInfoCell.setBorder(Rectangle.NO_BORDER);
766
 
767
        extraInfoTable.addCell(extraInfoCell);
768
 
769
 
770
        return extraInfoTable;
771
    }
5527 anupam.sin 772
 
2787 chandransh 773
    public static void main(String[] args) throws IOException {
774
        InvoiceGenerationService invoiceGenerationService = new InvoiceGenerationService();
5527 anupam.sin 775
        long orderId = 148574;
4361 rajveer 776
        ByteArrayOutputStream baos = invoiceGenerationService.generateInvoice(orderId, false, false, 1);
2787 chandransh 777
        String userHome = System.getProperty("user.home");
778
        File f = new File(userHome + "/invoice-" + orderId + ".pdf");
779
        FileOutputStream fos = new FileOutputStream(f);
780
        baos.writeTo(fos);
781
        System.out.println("Invoice generated.");
782
    }
783
}