Subversion Repositories SmartDukaan

Rev

Rev 9511 | Rev 10310 | 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;
7792 anupam.sin 9
import in.shop2020.logistics.ProviderDetails;
7190 amar.kumar 10
import in.shop2020.model.v1.catalog.CatalogService;
11
import in.shop2020.model.v1.catalog.Item;
6746 rajveer 12
import in.shop2020.model.v1.inventory.BillingType;
5948 mandeep.dh 13
import in.shop2020.model.v1.inventory.InventoryServiceException;
5945 mandeep.dh 14
import in.shop2020.model.v1.inventory.Warehouse;
7528 rajveer 15
import in.shop2020.model.v1.order.AmazonOrder;
5527 anupam.sin 16
import in.shop2020.model.v1.order.Attribute;
8182 amar.kumar 17
import in.shop2020.model.v1.order.EbayOrder;
2787 chandransh 18
import in.shop2020.model.v1.order.LineItem;
19
import in.shop2020.model.v1.order.Order;
7318 rajveer 20
import in.shop2020.model.v1.order.OrderSource;
4361 rajveer 21
import in.shop2020.model.v1.order.OrderStatus;
5527 anupam.sin 22
import in.shop2020.model.v1.order.OrderType;
8488 amar.kumar 23
import in.shop2020.model.v1.order.SnapdealOrder;
8996 amar.kumar 24
import in.shop2020.model.v1.order.FlipkartOrder;
9432 amar.kumar 25
import in.shop2020.model.v1.order.TaxType;
7190 amar.kumar 26
import in.shop2020.thrift.clients.CatalogClient;
3132 rajveer 27
import in.shop2020.thrift.clients.LogisticsClient;
28
import in.shop2020.thrift.clients.TransactionClient;
2787 chandransh 29
import in.shop2020.thrift.clients.config.ConfigClient;
5948 mandeep.dh 30
import in.shop2020.thrift.clients.InventoryClient;
2787 chandransh 31
 
8067 manish.sha 32
import java.awt.image.BufferedImage;
2787 chandransh 33
import java.io.ByteArrayOutputStream;
34
import java.io.File;
35
import java.io.FileOutputStream;
36
import java.io.IOException;
8067 manish.sha 37
import java.io.OutputStream;
2787 chandransh 38
import java.text.DateFormat;
39
import java.text.DecimalFormat;
4361 rajveer 40
import java.util.ArrayList;
2787 chandransh 41
import java.util.Date;
42
import java.util.List;
43
import java.util.Locale;
44
 
45
import javax.servlet.ServletException;
46
import javax.servlet.ServletOutputStream;
47
import javax.servlet.http.HttpServlet;
48
import javax.servlet.http.HttpServletRequest;
49
import javax.servlet.http.HttpServletResponse;
50
 
7014 rajveer 51
import org.apache.commons.lang.StringUtils;
2787 chandransh 52
import org.apache.commons.lang.WordUtils;
53
import org.apache.thrift.TException;
8067 manish.sha 54
import org.krysalis.barcode4j.impl.code128.Code128Bean;
55
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
56
import org.krysalis.barcode4j.tools.UnitConv;
2787 chandransh 57
import org.slf4j.Logger;
58
import org.slf4j.LoggerFactory;
59
 
60
import com.ibm.icu.text.RuleBasedNumberFormat;
61
 
62
import com.itextpdf.text.Document;
63
import com.itextpdf.text.Element;
64
import com.itextpdf.text.Font;
65
import com.itextpdf.text.FontFactory;
66
import com.itextpdf.text.FontFactoryImp;
67
import com.itextpdf.text.Image;
68
import com.itextpdf.text.Paragraph;
69
import com.itextpdf.text.Phrase;
70
import com.itextpdf.text.Rectangle;
71
import com.itextpdf.text.Font.FontFamily;
8034 manish.sha 72
import com.itextpdf.text.pdf.Barcode128;
2787 chandransh 73
import com.itextpdf.text.pdf.BaseFont;
8037 manish.sha 74
import com.itextpdf.text.pdf.PdfContentByte;
2787 chandransh 75
import com.itextpdf.text.pdf.PdfPCell;
76
import com.itextpdf.text.pdf.PdfPTable;
77
import com.itextpdf.text.pdf.PdfWriter;
78
import com.itextpdf.text.pdf.draw.DottedLineSeparator;
79
 
80
@SuppressWarnings("serial")
81
public class InvoiceServlet extends HttpServlet {
7014 rajveer 82
 
83
	private static Logger logger = LoggerFactory.getLogger(InvoiceServlet.class);
84
 
85
	@Override
86
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
87
		long orderId = Long.parseLong(request.getParameter("id"));
88
		long warehouseId = Long.parseLong(request.getParameter("warehouse"));
89
		boolean withBill = false;
90
		boolean printAll = false;
91
		try {
92
			withBill = Boolean.parseBoolean(request.getParameter("withBill"));
93
		} catch(Exception e){
94
			logger.warn("Couldn't infer whether bill should be printed. Not printing the bill.", e);
95
		}
96
		try {
97
			printAll = Boolean.parseBoolean(request.getParameter("printAll"));
98
		} catch(Exception e){
99
			logger.warn("Couldn't infer whether bill should be printed. Not printing the bill.", e);
100
		}
101
 
102
		logger.info("Printing invoice for order id: " + orderId);
8182 amar.kumar 103
 
8194 amar.kumar 104
		ByteArrayOutputStream baos = null;
8182 amar.kumar 105
 
106
		InvoiceGenerationService invoiceGenerationService = new InvoiceGenerationService();
107
		baos = invoiceGenerationService.generateInvoice(orderId, withBill, printAll, warehouseId);
7014 rajveer 108
 
109
		response.setContentType("application/pdf");
110
		response.setHeader("Content-disposition", "inline; filename=invoice-"+orderId+".pdf" );
111
 
112
		ServletOutputStream sos;
113
		try {
114
			sos = response.getOutputStream();
115
			baos.writeTo(sos);
116
			sos.flush();
117
		} catch (IOException e) {
118
			logger.error("Encountered error while sending invoice response: ", e);
119
		}
120
	}
2787 chandransh 121
}
122
 
123
class InvoiceGenerationService {
124
 
7014 rajveer 125
	private static Logger logger = LoggerFactory.getLogger(InvoiceGenerationService.class);
2787 chandransh 126
 
7014 rajveer 127
	private TransactionClient tsc = null;
128
	private InventoryClient csc = null;
129
	private LogisticsClient lsc = null;
7190 amar.kumar 130
	private CatalogClient ctsc = null;
2787 chandransh 131
 
7014 rajveer 132
	private static Locale indianLocale = new Locale("en", "IN");
133
	private DecimalFormat amountFormat = new DecimalFormat("#,##0.00");
2787 chandransh 134
 
7994 manish.sha 135
	//Start:-Added By Manish Sharma for FedEx Integration - Shipment Creation on 21-Aug-2013
136
	private static final Font helvetica6 = FontFactory.getFont(FontFactory.HELVETICA, 6);
7995 manish.sha 137
	//End:-Added By Manish Sharma for FedEx Integration  - Shipment Creation on 21-Aug-2013
7014 rajveer 138
	private static final Font helvetica8 = FontFactory.getFont(FontFactory.HELVETICA, 8);
139
	private static final Font helvetica10 = FontFactory.getFont(FontFactory.HELVETICA, 10);
140
	private static final Font helvetica12 = FontFactory.getFont(FontFactory.HELVETICA, 12);
141
	private static final Font helvetica16 = FontFactory.getFont(FontFactory.HELVETICA, 16);
8551 manish.sha 142
	private static final Font helvetica22 = FontFactory.getFont(FontFactory.HELVETICA, 22);
2787 chandransh 143
 
7014 rajveer 144
	private static final Font helveticaBold8 = FontFactory.getFont(FontFactory.HELVETICA_BOLD, 8);
145
	private static final Font helveticaBold12 = FontFactory.getFont(FontFactory.HELVETICA_BOLD, 12);
146
 
147
	private static final String delhiPincodePrefix = "11";
9319 amar.kumar 148
	private static final String[] maharashtraPincodePrefix = {"40", "41", "42", "43", "44"};
7014 rajveer 149
 
150
	public InvoiceGenerationService() {
151
		try {
152
			tsc = new TransactionClient();
153
			csc = new InventoryClient();
154
			lsc = new LogisticsClient();
7190 amar.kumar 155
			ctsc = new CatalogClient();
7014 rajveer 156
		} catch (Exception e) {
157
			logger.error("Error while instantiating thrift clients.", e);
158
		}
159
	}
160
 
161
	public ByteArrayOutputStream generateInvoice(long orderId, boolean withBill, boolean printAll, long warehouseId) {
162
		ByteArrayOutputStream baosPDF = null;
163
		in.shop2020.model.v1.order.TransactionService.Client tclient = tsc.getClient();
164
		in.shop2020.model.v1.inventory.InventoryService.Client iclient = csc.getClient();
165
		in.shop2020.logistics.LogisticsService.Client logisticsClient = lsc.getClient();
166
 
167
 
168
		try {
169
			baosPDF = new ByteArrayOutputStream();
170
 
171
			Document document = new Document();
8039 manish.sha 172
			PdfWriter.getInstance(document, baosPDF);
7014 rajveer 173
			document.addAuthor("shop2020");
174
			//document.addTitle("Invoice No: " + order.getInvoice_number());
175
			document.open();
176
 
177
			List<Order> orders = new ArrayList<Order>();
178
			if(printAll){
179
				try {
180
					List<OrderStatus> statuses = new ArrayList<OrderStatus>();
181
					statuses.add(OrderStatus.ACCEPTED);
182
					orders = tclient.getAllOrders(statuses, 0, 0, warehouseId);
183
				} catch (Exception e) {
184
					logger.error("Error while getting order information", e);
185
					return baosPDF; 
4361 rajveer 186
				}
7014 rajveer 187
			}else{
188
				orders.add(tclient.getOrder(orderId));	
189
			}
190
			boolean isFirst = true;
5387 rajveer 191
 
7014 rajveer 192
			for(Order order: orders){
193
				Warehouse warehouse = null;
194
				Provider provider = null;
195
				String destCode = null;
8011 rajveer 196
				Warehouse shippingLocation = null;
7014 rajveer 197
				int barcodeFontSize = 0;
198
				try {
199
					warehouse = iclient.getWarehouse(order.getWarehouse_id());
200
					long providerId = order.getLogistics_provider_id();
201
					provider = logisticsClient.getProvider(providerId);
202
					if(provider.getPickup().equals(PickUpType.SELF) || provider.getPickup().equals(PickUpType.RUNNER))
203
						destCode = provider.getPickup().toString();
204
					else
205
						destCode = logisticsClient.getDestinationCode(providerId, order.getCustomer_pincode());
2787 chandransh 206
 
7014 rajveer 207
					barcodeFontSize = Integer.parseInt(ConfigClient.getClient().get(provider.getName().toLowerCase() + "_barcode_fontsize"));
8011 rajveer 208
					shippingLocation = CatalogUtils.getWarehouse(warehouse.getShippingWarehouseId());
7014 rajveer 209
				} catch (InventoryServiceException ise) {
210
					logger.error("Error while getting the warehouse information.", ise);
211
					return baosPDF;
212
				} catch (LogisticsServiceException lse) {
213
					logger.error("Error while getting the provider information.", lse);
214
					return baosPDF;
215
				} catch (ConfigException ce) {
216
					logger.error("Error while getting the fontsize for the given provider", ce);
217
					return baosPDF;
218
				} catch (TException te) {
219
					logger.error("Error while getting some essential information from the services", te);
220
					return baosPDF;
221
				}
4361 rajveer 222
 
7014 rajveer 223
				if(printAll && warehouse.getBillingType() == BillingType.OURS_EXTERNAL){
224
					if(isFirst){
225
						document.add(getFixedTextTable(16, "Spice Online Retail Pvt Ltd"));
226
						isFirst = false;
227
					}
228
					document.add(getExtraInfoTable(order, provider, 16, warehouse.getBillingType()));
229
					continue;
230
				}
8182 amar.kumar 231
 
232
				PdfPTable dispatchAdviceTable = null;
8488 amar.kumar 233
 
234
				if (new Long(order.getSource()).intValue() == OrderSource.SNAPDEAL.getValue()) {
235
					dispatchAdviceTable = new PdfPTable(1);
8998 amar.kumar 236
				}  else if(new Long(order.getSource()).intValue() == OrderSource.FLIPKART.getValue()) {
8996 amar.kumar 237
					dispatchAdviceTable = new PdfPTable(1);
238
				}  else if ((new Long(order.getSource()).intValue() == OrderSource.EBAY.getValue()) && (order.getLogistics_provider_id()>7)) {
8225 amar.kumar 239
					if(order.getWarehouse_id() == 7 || order.getWarehouse_id() == 5 || order.getWarehouse_id() == 9) {
240
						dispatchAdviceTable = new PdfPTable(1);
8303 amar.kumar 241
					} else { 
242
						if (order.getAirwaybill_no()== null || order.getAirwaybill_no().equals("null") || order.getAirwaybill_no().isEmpty()) {
243
							dispatchAdviceTable = new PdfPTable(1);
244
						} else {
245
							EbayInvoiceGenerationService invoiceGenerationService = new EbayInvoiceGenerationService();
246
							dispatchAdviceTable = invoiceGenerationService.getDispatchAdviceTable(orderId, warehouseId);
247
						}
8225 amar.kumar 248
					}
8182 amar.kumar 249
				} else {
250
					dispatchAdviceTable = getDispatchAdviceTable(order, warehouse, provider, barcodeFontSize, destCode, withBill, shippingLocation);
251
				}
7014 rajveer 252
				dispatchAdviceTable.setSpacingAfter(10.0f);
253
				dispatchAdviceTable.setWidthPercentage(90.0f);
254
				document.add(dispatchAdviceTable);
8182 amar.kumar 255
				//TODO fix this logic
256
				if ((new Long(order.getSource()).intValue() == OrderSource.EBAY.getValue()) && (order.getLogistics_provider_id()>7)) {
8303 amar.kumar 257
					if (order.getAirwaybill_no()== null || order.getAirwaybill_no().equals("null") || order.getAirwaybill_no().isEmpty() 
258
							|| order.getWarehouse_id() == 7 || order.getWarehouse_id() == 5 || order.getWarehouse_id() == 9) {
259
						if(withBill){
260
							PdfPTable taxTable = getTaxCumRetailInvoiceTable(order, provider, shippingLocation.getLocation() + "-" + shippingLocation.getPincode() , shippingLocation.getTinNumber());
261
							taxTable.setSpacingBefore(5.0f);
262
							taxTable.setWidthPercentage(90.0f);
263
							document.add(new DottedLineSeparator());
264
							document.add(taxTable);
265
						}else{
266
							PdfPTable extraInfoTable = getExtraInfoTable(order, provider, 16, warehouse.getBillingType());
267
							extraInfoTable.setSpacingBefore(5.0f);
268
							extraInfoTable.setWidthPercentage(90.0f);
269
							document.add(new DottedLineSeparator());
270
							document.add(extraInfoTable);
271
						}
272
					} else {
273
						document.newPage();
274
					}
8488 amar.kumar 275
				} else if (new Long(order.getSource()).intValue() == OrderSource.SNAPDEAL.getValue()) {
276
					if(withBill){
277
						PdfPTable taxTable = getTaxCumRetailInvoiceTable(order, provider, shippingLocation.getLocation() + "-" + shippingLocation.getPincode() , shippingLocation.getTinNumber());
278
						taxTable.setSpacingBefore(5.0f);
279
						taxTable.setWidthPercentage(90.0f);
280
						document.add(new DottedLineSeparator());
281
						document.add(taxTable);
282
					}else{
283
						PdfPTable extraInfoTable = getExtraInfoTable(order, provider, 16, warehouse.getBillingType());
284
						extraInfoTable.setSpacingBefore(5.0f);
285
						extraInfoTable.setWidthPercentage(90.0f);
286
						document.add(new DottedLineSeparator());
287
						document.add(extraInfoTable);
288
					}
8182 amar.kumar 289
				}
7014 rajveer 290
				if(withBill){
8016 rajveer 291
					PdfPTable taxTable = getTaxCumRetailInvoiceTable(order, provider, shippingLocation.getLocation() + "-" + shippingLocation.getPincode() , shippingLocation.getTinNumber());
7014 rajveer 292
					taxTable.setSpacingBefore(5.0f);
293
					taxTable.setWidthPercentage(90.0f);
294
					document.add(new DottedLineSeparator());
295
					document.add(taxTable);
9009 amar.kumar 296
					if(order.getSource() == OrderSource.FLIPKART.getValue()) {
9043 amar.kumar 297
						//document.add(new DottedLineSeparator());
9009 amar.kumar 298
						document.add(getFlipkartBarCodes(order));
299
					}
300
 
7014 rajveer 301
				}else{
302
					PdfPTable extraInfoTable = getExtraInfoTable(order, provider, 16, warehouse.getBillingType());
303
					extraInfoTable.setSpacingBefore(5.0f);
304
					extraInfoTable.setWidthPercentage(90.0f);
305
					document.add(new DottedLineSeparator());
306
					document.add(extraInfoTable);
307
				}
308
				document.newPage();
309
			}
310
			document.close();
311
			baosPDF.close();
312
			// Adding facility to store the bill on the local directory. This will happen for only for Mahipalpur warehouse.
313
			if(withBill && !printAll){
7079 rajveer 314
				String strOrderId = StringUtils.repeat("0", 10-String.valueOf(orderId).length()) + orderId;  
7014 rajveer 315
				String dirPath = "/SaholicInvoices" + File.separator + strOrderId.substring(0, 2) + File.separator + strOrderId.substring(2, 4) + File.separator + strOrderId.substring(4, 6);
316
				String filename = dirPath + File.separator + orderId + ".pdf";
317
				File dirFile = new File(dirPath);
318
				if(!dirFile.exists()){
319
					dirFile.mkdirs();
320
				}
321
				File f = new File(filename);
322
				FileOutputStream fos = new FileOutputStream(f);
323
				baosPDF.writeTo(fos);
324
			}
325
		} catch (Exception e) {
326
			logger.error("Error while generating Invoice: ", e);
327
		}
328
		return baosPDF;
329
	}
3065 chandransh 330
 
8039 manish.sha 331
	private PdfPTable getDispatchAdviceTable(Order order, Warehouse warehouse, Provider provider, float barcodeFontSize, String destCode, boolean withBill, Warehouse shippingLocation){
7014 rajveer 332
		Font barCodeFont = getBarCodeFont(provider, barcodeFontSize);
2787 chandransh 333
 
7014 rajveer 334
		PdfPTable table = new PdfPTable(1);
335
		table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
8106 manish.sha 336
 
337
		PdfPTable titleBarTable = new PdfPTable(new float[]{0.4f, 0.4f, 0.2f});
8107 manish.sha 338
		titleBarTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
8106 manish.sha 339
 
8103 manish.sha 340
		PdfPTable logoTable = new PdfPTable(2);
341
		addLogoTable(logoTable,order); 
7318 rajveer 342
 
7014 rajveer 343
		PdfPCell titleCell = getTitleCell();
344
		PdfPTable customerTable = getCustomerAddressTable(order, destCode, false, helvetica12, false);
8039 manish.sha 345
		PdfPTable providerInfoTable = getProviderTable(order, provider, barCodeFont);
2787 chandransh 346
 
7014 rajveer 347
		PdfPTable dispatchTable = new PdfPTable(new float[]{0.5f, 0.1f, 0.4f});
348
		dispatchTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
349
		dispatchTable.addCell(customerTable);
350
		dispatchTable.addCell(new Phrase(" "));
351
		dispatchTable.addCell(providerInfoTable);
2787 chandransh 352
 
7014 rajveer 353
		PdfPTable invoiceTable = getTopInvoiceTable(order, shippingLocation.getTinNumber());
8110 manish.sha 354
		PdfPTable addressTable = new PdfPTable(1);
355
		addressTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
356
		addressTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
357
 
7014 rajveer 358
		PdfPCell addressCell = getAddressCell(shippingLocation.getLocation() +
8169 manish.sha 359
				" - " + shippingLocation.getPincode() + "\nContact No.- 0120-2479977" + "\n\n");
2787 chandransh 360
 
7014 rajveer 361
		PdfPTable chargesTable = new PdfPTable(1);
362
		chargesTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
363
		chargesTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
364
		if(order.isLogisticsCod()){
7318 rajveer 365
			chargesTable.addCell(new Phrase("AMOUNT TO BE COLLECTED : Rs " + (order.getTotal_amount()-order.getGvAmount()-order.getAdvanceAmount()), helveticaBold12));
7014 rajveer 366
			chargesTable.addCell(new Phrase("RTO ADDRESS:DEL/HPW/111116"));
7994 manish.sha 367
			//Start:-Added By Manish Sharma for FedEx Integration - Shipment Creation on 21-Aug-2013
368
			if(order.getLogistics_provider_id()==7L){
369
				in.shop2020.model.v1.order.TransactionService.Client tclient = tsc.getClient();
370
				String fedexCodReturnBarcode = "";
8080 manish.sha 371
				String fedexCodReturnTrackingId = "";
7994 manish.sha 372
				try {
373
					fedexCodReturnBarcode = tclient.getOrderAttributeValue(order.getId(), "FedEx_COD_Return_BarCode");
8080 manish.sha 374
					fedexCodReturnTrackingId = tclient.getOrderAttributeValue(order.getId(), "FedEx_COD_Return_Tracking_No");
7994 manish.sha 375
				} catch (TException e1) {
376
					logger.error("Error while getting the provider information.", e1);
377
				}
8080 manish.sha 378
				PdfPCell formIdCell= new PdfPCell(new Paragraph("COD Return "+fedexCodReturnTrackingId+" Form id-0325", helvetica6));
8104 manish.sha 379
				formIdCell.setPaddingTop(2.0f);
7994 manish.sha 380
				formIdCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
381
				formIdCell.setBorder(Rectangle.NO_BORDER);
382
				chargesTable.addCell(new Phrase("PRIORITY OVERNIGHT ", helvetica8));
383
				chargesTable.addCell(formIdCell);
8035 manish.sha 384
 
8067 manish.sha 385
				generateBarcode(fedexCodReturnBarcode, "fedex_codr_"+order.getId());
8037 manish.sha 386
 
8067 manish.sha 387
				Image barcodeImage=null;
388
				try {
389
					barcodeImage = Image.getInstance("/tmp/"+"fedex_codr_"+order.getId()+".png");
390
				} catch (Exception e) {
391
					logger.error("Exception during getting Barcode Image for Fedex : ", e);
392
				}
393
 
8173 manish.sha 394
				PdfPTable codReturnTable = new PdfPTable(new float[]{0.6f,0.4f});
395
				codReturnTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
396
				codReturnTable.addCell(barcodeImage);
397
				codReturnTable.addCell(new Phrase(" "));
398
				chargesTable.addCell(codReturnTable);
399
 
7994 manish.sha 400
			}
401
			//End:-Added By Manish Sharma for FedEx Integration - Shipment Creation on 21-Aug-2013
7014 rajveer 402
		} else {
403
			chargesTable.addCell(new Phrase("Do not pay any extra charges to the Courier."));  
404
		}
8080 manish.sha 405
 
406
		if(order.getLogistics_provider_id()==7L){
407
			chargesTable.addCell(new Phrase("Term and Condition:- Subject to the Conditions of Carriage which " +
408
					"limits the liability of FedEx for loss, delay or damage to the consignment." +
409
					" Visit http://www.fedex.com/in/domestic/services/terms to view the conitions of Carriage" ,
8082 manish.sha 410
					new Font(FontFamily.TIMES_ROMAN, 8f)));
8080 manish.sha 411
		}
2787 chandransh 412
 
8110 manish.sha 413
		addressTable.addCell(new Phrase("If undelivered, return to:", helvetica10));
414
		addressTable.addCell(addressCell);
415
 
7014 rajveer 416
		PdfPTable addressAndNoteTable = new PdfPTable(new float[]{0.3f, 0.7f});
417
		addressAndNoteTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
8110 manish.sha 418
		addressAndNoteTable.addCell(addressTable);
7014 rajveer 419
		addressAndNoteTable.addCell(chargesTable);
2787 chandransh 420
 
8106 manish.sha 421
		titleBarTable.addCell(logoTable);
422
		titleBarTable.addCell(titleCell);
423
		titleBarTable.addCell(" ");
424
 
425
		table.addCell(titleBarTable);
7014 rajveer 426
		table.addCell(dispatchTable);
427
		table.addCell(invoiceTable);
428
		table.addCell(addressAndNoteTable);
429
		return table;
430
	}
2787 chandransh 431
 
8103 manish.sha 432
	private void addLogoTable(PdfPTable logoTable,Order order) {
7318 rajveer 433
		logoTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
434
		logoTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
435
		logoTable.getDefaultCell().setVerticalAlignment(Element.ALIGN_BOTTOM);
8096 manish.sha 436
 
7318 rajveer 437
		PdfPCell logoCell;
438
		String logoPath;
8102 manish.sha 439
 
7556 rajveer 440
		if(order.getSource() == OrderSource.STORE.getValue()){
441
			logoCell = new PdfPCell(new Phrase(""));
442
 
443
		}else{
8102 manish.sha 444
			logoPath = InvoiceGenerationService.class.getResource("/logo.jpg").getPath();
8094 manish.sha 445
 
7318 rajveer 446
			try {
447
				logoCell = new PdfPCell(Image.getInstance(logoPath), false);
448
			} catch (Exception e) {
449
				//Too Many exceptions to catch here: BadElementException, MalformedURLException and IOException
450
				logger.warn("Couldn't load the Saholic logo: ", e);
451
				logoCell = new PdfPCell(new Phrase("Saholic Logo"));
452
			}
453
 
454
		}
8090 manish.sha 455
		logoCell.setBorder(Rectangle.NO_BORDER);
456
		logoCell.setHorizontalAlignment(Element.ALIGN_LEFT);
8102 manish.sha 457
		logoTable.addCell(logoCell);
458
		logoTable.addCell(" ");
8103 manish.sha 459
 
7318 rajveer 460
	}
461
 
7014 rajveer 462
	private Font getBarCodeFont(Provider provider, float barcodeFontSize) {
463
		String fontPath = InvoiceGenerationService.class.getResource("/" + provider.getName().toLowerCase() + "/barcode.TTF").getPath();
464
		FontFactoryImp ttfFontFactory = new FontFactoryImp();
465
		ttfFontFactory.register(fontPath, "barcode");
466
		Font barCodeFont = ttfFontFactory.getFont("barcode", BaseFont.CP1252, true, barcodeFontSize);
467
		return barCodeFont;
468
	}
2787 chandransh 469
 
7014 rajveer 470
	private PdfPCell getTitleCell() {
471
		PdfPCell titleCell = new PdfPCell(new Phrase("Dispatch Advice", helveticaBold12));
472
		titleCell.setHorizontalAlignment(Element.ALIGN_CENTER);
473
		titleCell.setBorder(Rectangle.NO_BORDER);
474
		return titleCell;
475
	}
2787 chandransh 476
 
8039 manish.sha 477
	private PdfPTable getProviderTable(Order order, Provider provider, Font barCodeFont) {
7014 rajveer 478
		PdfPTable providerInfoTable = new PdfPTable(1);
479
		providerInfoTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
7318 rajveer 480
		if(order.isLogisticsCod()){
8551 manish.sha 481
			PdfPCell deliveryTypeCell = new PdfPCell(new Phrase("COD   ", helvetica22));
7318 rajveer 482
			deliveryTypeCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
483
			deliveryTypeCell.setBorder(Rectangle.NO_BORDER);
484
			providerInfoTable.addCell(deliveryTypeCell);
485
		}
486
 
8035 manish.sha 487
 
7014 rajveer 488
		PdfPCell providerNameCell = new PdfPCell(new Phrase(provider.getName(), helveticaBold12));
489
		providerNameCell.setHorizontalAlignment(Element.ALIGN_LEFT);
490
		providerNameCell.setBorder(Rectangle.NO_BORDER);
7994 manish.sha 491
		PdfPCell formIdCell= null;
492
		if(order.getLogistics_provider_id()==7L){
493
			if(order.isCod()){
8034 manish.sha 494
				formIdCell = new PdfPCell(new Paragraph(order.getAirwaybill_no()+" Form id-0305", helvetica6));
7994 manish.sha 495
			}
496
			else{
8034 manish.sha 497
				formIdCell = new PdfPCell(new Paragraph(order.getAirwaybill_no()+" Form id-0467", helvetica6));
7994 manish.sha 498
			}
8551 manish.sha 499
			formIdCell.setPaddingTop(1.0f);
8015 rajveer 500
			formIdCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
501
			formIdCell.setBorder(Rectangle.NO_BORDER);
7994 manish.sha 502
		}
8015 rajveer 503
 
7994 manish.sha 504
 
505
		PdfPCell awbNumberCell= null;
8034 manish.sha 506
		String fedexPackageBarcode = "";
7994 manish.sha 507
		if(order.getLogistics_provider_id()!=7L){
508
			awbNumberCell = new PdfPCell(new Paragraph("*" + order.getAirwaybill_no() + "*", barCodeFont));
8017 manish.sha 509
			awbNumberCell.setPaddingTop(20.0f);
7994 manish.sha 510
		}
511
		else{
8013 rajveer 512
			in.shop2020.model.v1.order.TransactionService.Client tclient = tsc.getClient();
513
			try {
514
				fedexPackageBarcode = tclient.getOrderAttributeValue(order.getId(), "FedEx_Package_BarCode");
515
			} catch (TException e1) {
516
				logger.error("Error while getting the provider information.", e1);
517
			}
8174 manish.sha 518
			awbNumberCell = new PdfPCell(new Paragraph(" ", helvetica6));
519
		}
520
		awbNumberCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
521
		awbNumberCell.setBorder(Rectangle.NO_BORDER);
522
 
523
		providerInfoTable.addCell(providerNameCell);
524
		if(formIdCell != null){
525
			providerInfoTable.addCell(formIdCell);
526
		}
527
		//End:-Added By Manish Sharma for FedEx Integration - Shipment Creation on 21-Aug-2013
528
		if(order.getLogistics_provider_id()==7L){
8169 manish.sha 529
			generateBarcode(fedexPackageBarcode, "fedex_"+order.getId());
530
 
531
			Image barcodeImage=null;
532
			try {
533
				barcodeImage = Image.getInstance("/tmp/"+"fedex_"+order.getId()+".png");
534
			} catch (Exception e) {
535
				logger.error("Exception during getting Barcode Image for Fedex : ", e);
536
			}
8174 manish.sha 537
			providerInfoTable.addCell(barcodeImage);
7994 manish.sha 538
		}
8174 manish.sha 539
		providerInfoTable.addCell(awbNumberCell);
7014 rajveer 540
 
7792 anupam.sin 541
		Warehouse warehouse = null;
542
		try{
543
    		InventoryClient isc = new InventoryClient();
7804 amar.kumar 544
    		warehouse = isc.getClient().getWarehouse(order.getWarehouse_id());
7803 amar.kumar 545
		} catch(Exception e) {
7792 anupam.sin 546
		    logger.error("Unable to get warehouse for id : " + order.getWarehouse_id(), e);
7805 amar.kumar 547
		    //TODO throw e;
7792 anupam.sin 548
		}
549
		DeliveryType dt =  DeliveryType.PREPAID;
550
        if (order.isLogisticsCod()) {
551
            dt = DeliveryType.COD;
552
        }
7994 manish.sha 553
        //Start:-Added By Manish Sharma for FedEx Integration - Shipment Creation on 21-Aug-2013
554
        if(order.getLogistics_provider_id()!=7L){
555
	        for (ProviderDetails detail : provider.getDetails()) {
556
	            if(in.shop2020.model.v1.inventory.WarehouseLocation.findByValue((int) detail.getLogisticLocation()) == warehouse.getLogisticsLocation() && detail.getDeliveryType() == dt) {
557
	                providerInfoTable.addCell(new Phrase("Account No : " + detail.getAccountNo(), helvetica8));
558
	            }
559
	        }
7792 anupam.sin 560
        }
7994 manish.sha 561
        else{
562
        	providerInfoTable.addCell(new Phrase("STANDARD OVERNIGHT ", helvetica8));
563
        }
564
        //End:-Added By Manish Sharma for FedEx Integration - Shipment Creation on 21-Aug-2013
7014 rajveer 565
		Date awbDate;
566
		if(order.getBilling_timestamp() == 0){
567
			awbDate = new Date();
568
		}else{
569
			awbDate = new Date(order.getBilling_timestamp());
570
		}
8106 manish.sha 571
		if(order.getLogistics_provider_id()!=7L){
572
			providerInfoTable.addCell(new Phrase("AWB Date   : " + DateFormat.getDateInstance(DateFormat.MEDIUM).format(awbDate), helvetica8));
573
		}
7014 rajveer 574
		providerInfoTable.addCell(new Phrase("Weight         : " + order.getTotal_weight() + " Kg", helvetica8));
8182 amar.kumar 575
		if(order.getSource() == OrderSource.EBAY.getValue()){
576
			EbayOrder ebayOrder = null;
577
			try {
578
				ebayOrder = tsc.getClient().getEbayOrderByOrderId(order.getId());
579
			} catch (TException e) {
580
				logger.error("Error while getting ebay order", e);
581
			}
582
			providerInfoTable.addCell(new Phrase("PaisaPayId            : " + ebayOrder.getPaisaPayId(), helvetica8));
583
			providerInfoTable.addCell(new Phrase("Sales Rec Number: " + ebayOrder.getSalesRecordNumber(), helvetica8));
584
		}
7994 manish.sha 585
		//Start:-Added By Manish Sharma for FedEx Integration - Shipment Creation on 21-Aug-2013
586
		if(order.getLogistics_provider_id()==7L){
587
			providerInfoTable.addCell(new Phrase("Bill T/C Sender      "+ "Bill D/T Sender", helvetica8));
588
		}
589
		//End:-Added By Manish Sharma for FedEx Integration - Shipment Creation on 21-Aug-2013
7014 rajveer 590
		return providerInfoTable;
591
	}
592
 
593
	private PdfPTable getTopInvoiceTable(Order order, String tinNo){
594
		PdfPTable invoiceTable = new PdfPTable(new float[]{0.2f, 0.2f, 0.3f, 0.1f, 0.1f, 0.1f});
595
		invoiceTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
596
 
597
		invoiceTable.addCell(getInvoiceTableHeader(6));
598
 
599
		invoiceTable.addCell(new Phrase("Order No", helvetica8));
600
		invoiceTable.addCell(new Phrase("Paymode", helvetica8));
601
		invoiceTable.addCell(new Phrase("Product Name", helvetica8));
602
		invoiceTable.addCell(new Phrase("Quantity", helvetica8));
603
		invoiceTable.addCell(new Phrase("Rate", helvetica8));
604
		invoiceTable.addCell(new Phrase("Amount", helvetica8));
605
		populateTopInvoiceTable(order, invoiceTable);
606
 
607
 
608
		if(order.getInsurer() > 0) {
609
			invoiceTable.addCell(getInsuranceCell(4));
610
			invoiceTable.addCell(getPriceCell(order.getInsuranceAmount()));
611
			invoiceTable.addCell(getPriceCell(order.getInsuranceAmount()));
612
		}
613
 
7318 rajveer 614
		if(order.getSource() == OrderSource.STORE.getValue()) {
615
			invoiceTable.addCell(getAdvanceAmountCell(4));
616
			invoiceTable.addCell(getPriceCell(order.getAdvanceAmount()));
617
			invoiceTable.addCell(getPriceCell(order.getAdvanceAmount()));
618
		}
619
 
7014 rajveer 620
		invoiceTable.addCell(getTotalCell(4));      
621
		invoiceTable.addCell(getRupeesCell());
7318 rajveer 622
		invoiceTable.addCell(getTotalAmountCell(order.getTotal_amount()-order.getGvAmount()-order.getAdvanceAmount()));
7014 rajveer 623
 
624
		PdfPCell tinCell = new PdfPCell(new Phrase("TIN NO. " + tinNo, helvetica8));
625
		tinCell.setColspan(6);
626
		tinCell.setPadding(2);
627
		invoiceTable.addCell(tinCell);
628
 
629
		return invoiceTable;
630
	}
631
 
632
	private void populateTopInvoiceTable(Order order, PdfPTable invoiceTable) {
633
		List<LineItem> lineitems = order.getLineitems();
634
		for (LineItem lineitem : lineitems) {
635
			invoiceTable.addCell(new Phrase(order.getId() + "", helvetica8));
636
			if(order.getPickupStoreId() > 0 && order.isCod() == true)
637
				invoiceTable.addCell(new Phrase("In-Store", helvetica8));
638
			else if (order.isCod())
639
				invoiceTable.addCell(new Phrase("COD", helvetica8));
640
			else
641
				invoiceTable.addCell(new Phrase("Prepaid", helvetica8));
7318 rajveer 642
 
7190 amar.kumar 643
			invoiceTable.addCell(getProductNameCell(lineitem, false, order.getFreebieItemId()));
2787 chandransh 644
 
7014 rajveer 645
			invoiceTable.addCell(new Phrase(lineitem.getQuantity() + "", helvetica8));
2787 chandransh 646
 
7014 rajveer 647
			invoiceTable.addCell(getPriceCell(lineitem.getUnit_price()-order.getGvAmount()));
648
 
649
			invoiceTable.addCell(getPriceCell(lineitem.getTotal_price()-order.getGvAmount()));
650
		}
651
	}
652
 
653
	private PdfPCell getAddressCell(String address) {
654
		Paragraph addressParagraph = new Paragraph(address, new Font(FontFamily.TIMES_ROMAN, 8f));
655
		PdfPCell addressCell = new PdfPCell();
656
		addressCell.addElement(addressParagraph);
657
		addressCell.setHorizontalAlignment(Element.ALIGN_LEFT);
658
		addressCell.setBorder(Rectangle.NO_BORDER);
659
		return addressCell;
660
	}
661
 
8011 rajveer 662
	private PdfPTable getTaxCumRetailInvoiceTable(Order order, Provider provider, String ourAddress, String tinNo){
7014 rajveer 663
		PdfPTable taxTable = new PdfPTable(1);
664
		Phrase phrase = null;
665
		taxTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
666
		taxTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
8104 manish.sha 667
 
8110 manish.sha 668
		PdfPTable logoTitleAndOurAddressTable = new PdfPTable(new float[]{0.4f, 0.3f, 0.3f});
8107 manish.sha 669
		logoTitleAndOurAddressTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
8112 manish.sha 670
		logoTitleAndOurAddressTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
8107 manish.sha 671
 
8110 manish.sha 672
 
8103 manish.sha 673
		PdfPTable logoTable = new PdfPTable(2);
674
		addLogoTable(logoTable,order); 
7318 rajveer 675
 
7014 rajveer 676
		if (order.getOrderType().equals(OrderType.B2B)) {
677
			phrase = new Phrase("TAX INVOICE", helveticaBold12);
678
		} else {
679
			phrase = new Phrase("RETAIL INVOICE", helveticaBold12);
680
		}
681
		PdfPCell retailInvoiceTitleCell = new PdfPCell(phrase);
682
		retailInvoiceTitleCell.setHorizontalAlignment(Element.ALIGN_CENTER);
683
		retailInvoiceTitleCell.setBorder(Rectangle.NO_BORDER);
684
 
8169 manish.sha 685
		Paragraph sorlAddress = new Paragraph(ourAddress + "\n Contact No.- 0120-2479977" + "\nTIN NO. " + tinNo, new Font(FontFamily.TIMES_ROMAN, 8f, Element.ALIGN_CENTER));
7014 rajveer 686
		PdfPCell sorlAddressCell = new PdfPCell(sorlAddress);
687
		sorlAddressCell.addElement(sorlAddress);
8110 manish.sha 688
		sorlAddressCell.setHorizontalAlignment(Element.ALIGN_LEFT);
7014 rajveer 689
 
690
		PdfPTable customerAddress = getCustomerAddressTable(order, null, true, helvetica8, true);
691
		PdfPTable orderDetails = getOrderDetails(order, provider);
692
 
693
		PdfPTable addrAndOrderDetailsTable = new PdfPTable(new float[]{0.5f, 0.1f, 0.4f});
694
		addrAndOrderDetailsTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
695
		addrAndOrderDetailsTable.addCell(customerAddress);
696
		addrAndOrderDetailsTable.addCell(new Phrase(" "));
697
		addrAndOrderDetailsTable.addCell(orderDetails);
698
 
9319 amar.kumar 699
		boolean isVAT = isVatApplicable(order);
7014 rajveer 700
		PdfPTable invoiceTable = getBottomInvoiceTable(order, isVAT);
701
 
702
		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));
703
		disclaimerCell.setHorizontalAlignment(Element.ALIGN_LEFT);
704
		disclaimerCell.setBorder(Rectangle.NO_BORDER);
7318 rajveer 705
 
9014 amar.kumar 706
		PdfPCell powerTextCell = new PdfPCell(new Phrase("Powered By  Flipkart", helvetica8));
707
		powerTextCell.setHorizontalAlignment(Element.ALIGN_LEFT);
708
		powerTextCell.setBorder(Rectangle.NO_BORDER);
709
		powerTextCell.setPaddingBottom(30.0f);
8104 manish.sha 710
 
711
		logoTitleAndOurAddressTable.addCell(logoTable);
8110 manish.sha 712
		logoTitleAndOurAddressTable.addCell(retailInvoiceTitleCell);
713
		logoTitleAndOurAddressTable.addCell(sorlAddress);
8104 manish.sha 714
 
715
		taxTable.addCell(logoTitleAndOurAddressTable);
7014 rajveer 716
		taxTable.addCell(addrAndOrderDetailsTable);
717
		taxTable.addCell(invoiceTable);
718
		taxTable.addCell(disclaimerCell);
9014 amar.kumar 719
		if(order.getSource() == OrderSource.FLIPKART.getValue()) {
720
			taxTable.addCell(powerTextCell);
721
 
722
		}
7014 rajveer 723
		return taxTable;
724
	}
9009 amar.kumar 725
 
9319 amar.kumar 726
	private boolean isVatApplicable(Order order) {
727
		if(order.getWarehouse_id() == 7) {
728
			if(order.getCustomer_pincode().startsWith(delhiPincodePrefix)) {
729
				return true;
730
			} else {
731
				return false;
732
			}
733
		} else {
734
			for(int i=0; i< maharashtraPincodePrefix.length; i++) {
735
				if(order.getCustomer_pincode().startsWith(maharashtraPincodePrefix[i])) {
736
					return true;
737
				}
738
			}
739
			return false;
740
		}
741
	}
742
 
9037 amar.kumar 743
	private PdfPTable getFlipkartBarCodes(Order order) {
744
		PdfPTable flipkartTable = new PdfPTable(3);
745
 
746
		PdfPCell spacerCell = new PdfPCell();
9040 amar.kumar 747
		spacerCell.setBorder(Rectangle.NO_BORDER);
9037 amar.kumar 748
		spacerCell.setColspan(3);
9099 amar.kumar 749
		spacerCell.setPaddingTop(330.0f);
9037 amar.kumar 750
 
751
		String flipkartCodeFontPath = InvoiceGenerationService.class.getResource("/saholic-wn.TTF").getPath();
752
		FontFactoryImp ttfFontFactory = new FontFactoryImp();
753
		ttfFontFactory.register(flipkartCodeFontPath, "barcode");
754
		Font flipkartBarCodeFont = ttfFontFactory.getFont("barcode", BaseFont.CP1252, true, 20);
755
 
9040 amar.kumar 756
		String serialNumber = "0000000000";
9511 manish.sha 757
		if(order.getLineitems().get(0).getSerial_number()!=null && !order.getLineitems().get(0).getSerial_number().isEmpty()) {
9040 amar.kumar 758
			serialNumber = order.getLineitems().get(0).getSerial_number();
9511 manish.sha 759
		} else if(order.getLineitems().get(0).getItem_number()!=null && !order.getLineitems().get(0).getItem_number().isEmpty()) {
9040 amar.kumar 760
			serialNumber = order.getLineitems().get(0).getItem_number();
761
		}
762
 
763
		PdfPCell serialNumberBarCodeCell = new PdfPCell(new Paragraph("*" +  serialNumber + "*", flipkartBarCodeFont));
9037 amar.kumar 764
		serialNumberBarCodeCell.setBorder(Rectangle.TOP);
9044 amar.kumar 765
		serialNumberBarCodeCell.setHorizontalAlignment(Element.ALIGN_CENTER);
9037 amar.kumar 766
		serialNumberBarCodeCell.setPaddingTop(11.0f);
767
 
768
 
769
		PdfPCell invoiceNumberBarCodeCell = new PdfPCell(new Paragraph("*" +  order.getInvoice_number() + "*", flipkartBarCodeFont));
770
		invoiceNumberBarCodeCell.setBorder(Rectangle.TOP);
9044 amar.kumar 771
		invoiceNumberBarCodeCell.setHorizontalAlignment(Element.ALIGN_CENTER);
9037 amar.kumar 772
		invoiceNumberBarCodeCell.setPaddingTop(11.0f);
773
 
774
		double rate = order.getLineitems().get(0).getVatRate();
775
		double salesTax = (rate * order.getTotal_amount())/(100 + rate);
9040 amar.kumar 776
		PdfPCell vatAmtBarCodeCell = new PdfPCell(new Paragraph("*" +  amountFormat.format(salesTax) + "*", flipkartBarCodeFont));
9037 amar.kumar 777
		vatAmtBarCodeCell.setBorder(Rectangle.TOP);
9044 amar.kumar 778
		vatAmtBarCodeCell.setHorizontalAlignment(Element.ALIGN_CENTER);
9037 amar.kumar 779
		vatAmtBarCodeCell.setPaddingTop(11.0f);
780
 
781
		flipkartTable.addCell(spacerCell);
782
		flipkartTable.addCell(serialNumberBarCodeCell);
783
		flipkartTable.addCell(invoiceNumberBarCodeCell);
784
		flipkartTable.addCell(vatAmtBarCodeCell);
785
 
786
		return flipkartTable;
787
 
9009 amar.kumar 788
	}
789
 
7014 rajveer 790
	private PdfPTable getCustomerAddressTable(Order order, String destCode, boolean showPaymentMode, Font font, boolean forInvoce){
791
		PdfPTable customerTable = new PdfPTable(1);
792
		customerTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
793
		if(forInvoce || order.getPickupStoreId() == 0){
794
			customerTable.addCell(new Phrase(order.getCustomer_name(), font));
795
			customerTable.addCell(new Phrase(order.getCustomer_address1(), font));
796
			customerTable.addCell(new Phrase(order.getCustomer_address2(), font));
797
			customerTable.addCell(new Phrase(order.getCustomer_city() + "," + order.getCustomer_state(), font));
7994 manish.sha 798
			//Start:-Added By Manish Sharma for FedEx Integration - Shipment Creation on 21-Aug-2013
799
			if(order.getLogistics_provider_id()!=7L){
800
				if(destCode != null)
801
					customerTable.addCell(new Phrase(order.getCustomer_pincode() + " - " + destCode, helvetica16));
802
				else
803
					customerTable.addCell(new Phrase(order.getCustomer_pincode(), font));
804
				}
805
			else{
806
				in.shop2020.model.v1.order.TransactionService.Client tclient = tsc.getClient();
807
				String fedexLocationcode = "";
808
				try {
809
					fedexLocationcode = tclient.getOrderAttributeValue(order.getId(), "FedEx_Location_Code");
810
				} catch (TException e1) {
811
					logger.error("Error while getting the provider information.", e1);
812
				}
813
				customerTable.addCell(new Phrase(order.getCustomer_pincode() + " - " + fedexLocationcode, helvetica16));
814
			}
815
			//Start:-Added By Manish Sharma for FedEx Integration - Shipment Creation on 21-Aug-2013
9100 amar.kumar 816
			if(order.getCustomer_mobilenumber()!=null && !order.getCustomer_mobilenumber().isEmpty()) {
817
				customerTable.addCell(new Phrase("Phone : " + (order.getCustomer_mobilenumber()== null ? "" : order.getCustomer_mobilenumber()), font));
818
			}
7014 rajveer 819
		}else{
820
			try {
5556 rajveer 821
				in.shop2020.logistics.LogisticsService.Client lclient = (new LogisticsClient()).getClient();
7014 rajveer 822
				PickupStore store = lclient.getPickupStore(order.getPickupStoreId());
823
				customerTable.addCell(new Phrase(order.getCustomer_name() + " \nc/o " + store.getName(), font));
824
				customerTable.addCell(new Phrase(store.getLine1(), font));
825
				customerTable.addCell(new Phrase(store.getLine2(), font));
826
				customerTable.addCell(new Phrase(store.getCity() + "," + store.getState(), font));
827
				if(destCode != null)
828
					customerTable.addCell(new Phrase(store.getPin() + " - " + destCode, helvetica16));
829
				else
830
					customerTable.addCell(new Phrase(store.getPin(), font));
831
				customerTable.addCell(new Phrase("Phone :" + store.getPhone(), font));
5556 rajveer 832
			} catch (TException e) {
833
				// TODO Auto-generated catch block
834
				e.printStackTrace();
835
			}
5527 anupam.sin 836
 
7014 rajveer 837
		}
838
 
839
		if(order.getOrderType().equals(OrderType.B2B)) {
840
			String tin = null;
841
			in.shop2020.model.v1.order.TransactionService.Client tclient = tsc.getClient();
842
			List<Attribute> attributes;
843
			try {
844
				attributes = tclient.getAllAttributesForOrderId(order.getId());
845
 
846
				for(Attribute attribute : attributes) {
847
					if(attribute.getName().equals("tinNumber")) {
848
						tin = attribute.getValue();
849
					}
850
				}
851
				if (tin != null) {
852
					customerTable.addCell(new Phrase("TIN :" + tin, font));
853
				}
854
 
855
			} catch (Exception e) {
856
				logger.error("Error while getting order attributes", e);
857
			}
858
		}
859
		/*
2787 chandransh 860
        if(showPaymentMode){
861
            customerTable.addCell(new Phrase(" ", font));
862
            customerTable.addCell(new Phrase("Payment Mode: Prepaid", font));
5856 anupam.sin 863
        }*/
7014 rajveer 864
		return customerTable;
865
	}
2787 chandransh 866
 
7014 rajveer 867
	private PdfPTable getOrderDetails(Order order, Provider provider){
868
		PdfPTable orderTable = new PdfPTable(new float[]{0.4f, 0.6f});
869
		orderTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
2787 chandransh 870
 
7014 rajveer 871
		orderTable.addCell(new Phrase("Invoice No:", helvetica8));
872
		orderTable.addCell(new Phrase(order.getInvoice_number(), helvetica8));
2787 chandransh 873
 
7014 rajveer 874
		orderTable.addCell(new Phrase("Date:", helvetica8));
875
		orderTable.addCell(new Phrase(DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date(order.getBilling_timestamp())), helvetica8));
2787 chandransh 876
 
7014 rajveer 877
		orderTable.addCell(new Phrase("Order ID:", helvetica8));
878
		orderTable.addCell(new Phrase("" + order.getId(), helvetica8));
2787 chandransh 879
 
7528 rajveer 880
		if(order.getSource() == OrderSource.AMAZON.getValue()){
881
			AmazonOrder aorder = null;
882
			try {
883
				aorder = tsc.getClient().getAmazonOrder(order.getId());
884
			} catch (TException e) {
885
				logger.error("Error while getting amazon order", e);
886
			}
887
			orderTable.addCell(new Phrase("Amazon Order ID:", helvetica8));
888
			orderTable.addCell(new Phrase(aorder.getAmazonOrderCode(), helvetica8));
8182 amar.kumar 889
		} else if(order.getSource() == OrderSource.EBAY.getValue()){
890
			EbayOrder ebayOrder = null;
891
			try {
892
				ebayOrder = tsc.getClient().getEbayOrderByOrderId(order.getId());
893
			} catch (TException e) {
894
				logger.error("Error while getting ebay order", e);
895
			}
896
			orderTable.addCell(new Phrase("PaisaPayId:", helvetica8));
897
			orderTable.addCell(new Phrase(ebayOrder.getPaisaPayId(), helvetica8));
898
			orderTable.addCell(new Phrase("Sales Rec Number:", helvetica8));
899
			orderTable.addCell(new Phrase(new Long(ebayOrder.getSalesRecordNumber()).toString(), helvetica8));
8488 amar.kumar 900
		} else if(order.getSource() == OrderSource.SNAPDEAL.getValue()){
901
			SnapdealOrder snapdealOrder = null;
902
			try {
903
				snapdealOrder = tsc.getClient().getSnapdealOrder(order.getId(), null, 0);
904
			} catch (TException e) {
905
				logger.error("Error while getting snapdeal order", e);
906
			}
907
			orderTable.addCell(new Phrase("Snapdeal OrderId:", helvetica8));
908
			orderTable.addCell(new Phrase(new Long(snapdealOrder.getSubOrderId()).toString(), helvetica8));
8828 amar.kumar 909
 
910
			String refernceCodeFontPath = InvoiceGenerationService.class.getResource("/saholic-wn.TTF").getPath();
911
			FontFactoryImp ttfFontFactory = new FontFactoryImp();
912
			ttfFontFactory.register(refernceCodeFontPath, "barcode");
8876 amar.kumar 913
			Font referenceCodeBarCodeFont = ttfFontFactory.getFont("barcode", BaseFont.CP1252, true, 20);
8828 amar.kumar 914
 
8874 amar.kumar 915
			PdfPCell snapdealReferenceBarCodeCell = new PdfPCell(new Paragraph("*" +  snapdealOrder.getReferenceCode() + "*", referenceCodeBarCodeFont));
8828 amar.kumar 916
			snapdealReferenceBarCodeCell.setBorder(Rectangle.NO_BORDER);
8876 amar.kumar 917
			snapdealReferenceBarCodeCell.setPaddingTop(9.0f);
918
			snapdealReferenceBarCodeCell.setColspan(2);
9042 amar.kumar 919
			snapdealReferenceBarCodeCell.setHorizontalAlignment(Element.ALIGN_CENTER);
8876 amar.kumar 920
			//orderTable.addCell(new Phrase("Snapdeal ReferenceCode:", helvetica8));
8828 amar.kumar 921
			orderTable.addCell(snapdealReferenceBarCodeCell);
922
			//orderTable.addCell(new Phrase(snapdealOrder.getReferenceCode(), helvetica8));
7528 rajveer 923
		}
8989 vikram.rag 924
		else if(order.getSource() == OrderSource.FLIPKART.getValue()){
925
			FlipkartOrder flipkartOrder = null;
926
			try {
927
				flipkartOrder = tsc.getClient().getFlipkartOrder(order.getId());
928
			} catch (TException e) {
8996 amar.kumar 929
				logger.error("Error while getting flipkart order", e);
8989 vikram.rag 930
			}
931
			orderTable.addCell(new Phrase("Flipkart OrderId:", helvetica8));
932
			orderTable.addCell(new Phrase(flipkartOrder.getFlipkartOrderId(), helvetica8));
9037 amar.kumar 933
 
934
			//orderTable.addCell(new Phrase("Flipkart OrderItemId:", helvetica8));
935
			String flipkartBarCodeFontPath = InvoiceGenerationService.class.getResource("/saholic-wn.TTF").getPath();
936
			FontFactoryImp ttfFontFactory = new FontFactoryImp();
937
			ttfFontFactory.register(flipkartBarCodeFontPath, "barcode");
9043 amar.kumar 938
			Font flipkartBarCodeFont = ttfFontFactory.getFont("barcode", BaseFont.CP1252, true, 18);
9037 amar.kumar 939
 
9043 amar.kumar 940
			orderTable.addCell(new Phrase("Flipkart OrderItemId:", helvetica8));
9037 amar.kumar 941
			PdfPCell flipkartOrderItemIdBarCodeCell = new PdfPCell(new Paragraph("*" +  new Long(flipkartOrder.getFlipkartSubOrderId()).toString() + "*", flipkartBarCodeFont));
942
			flipkartOrderItemIdBarCodeCell.setBorder(Rectangle.NO_BORDER);
943
			flipkartOrderItemIdBarCodeCell.setPaddingTop(9.0f);
9043 amar.kumar 944
			//flipkartOrderItemIdBarCodeCell.setColspan(2);
945
			//flipkartOrderItemIdBarCodeCell.setHorizontalAlignment(Element.ALIGN_CENTER);
9037 amar.kumar 946
			orderTable.addCell(flipkartOrderItemIdBarCodeCell);
947
			//orderTable.addCell(new Phrase("Flipkart OrderItemId:", helvetica8));
948
			//orderTable.addCell(new Phrase(new Long(flipkartOrder.getFlipkartSubOrderId()).toString(), helvetica8));
8989 vikram.rag 949
		}
950
 
951
 
7014 rajveer 952
		orderTable.addCell(new Phrase("Order Date:", helvetica8));
953
		orderTable.addCell(new Phrase(DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date(order.getCreated_timestamp())), helvetica8));
2787 chandransh 954
 
7014 rajveer 955
		orderTable.addCell(new Phrase("Courier:", helvetica8));
956
		orderTable.addCell(new Phrase(provider.getName(), helvetica8));
2787 chandransh 957
 
9038 amar.kumar 958
		if(order.getAirwaybill_no()!=null && !order.getAirwaybill_no().isEmpty()) {
959
			orderTable.addCell(new Phrase("AWB No:", helvetica8));
960
			orderTable.addCell(new Phrase(order.getAirwaybill_no(), helvetica8));
961
		}
2787 chandransh 962
 
7014 rajveer 963
		orderTable.addCell(new Phrase("AWB Date:", helvetica8));
964
		orderTable.addCell(new Phrase(DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date(order.getBilling_timestamp())), helvetica8));
2787 chandransh 965
 
7014 rajveer 966
		return orderTable;
967
	}
2787 chandransh 968
 
7014 rajveer 969
	private PdfPTable getBottomInvoiceTable(Order order, boolean isVAT){
970
		PdfPTable invoiceTable = new PdfPTable(new float[]{0.2f, 0.5f, 0.1f, 0.1f, 0.1f});
971
		invoiceTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
2787 chandransh 972
 
7014 rajveer 973
		invoiceTable.addCell(getInvoiceTableHeader(5));
4262 rajveer 974
 
7014 rajveer 975
		invoiceTable.addCell(new Phrase("Sl. No.", helveticaBold8));
976
		invoiceTable.addCell(new Phrase("Description", helveticaBold8));
977
		invoiceTable.addCell(new Phrase("Quantity", helveticaBold8));
978
		invoiceTable.addCell(new Phrase("Rate (Rs)", helveticaBold8));
979
		invoiceTable.addCell(new Phrase("Amount (Rs)", helveticaBold8));
980
		LineItem lineItem = order.getLineitems().get(0);
981
		double orderAmount = order.getTotal_amount();
982
		double rate = lineItem.getVatRate();
7057 amar.kumar 983
		double salesTax = (rate * (orderAmount - order.getInsuranceAmount()))/(100 + rate);
6750 rajveer 984
 
7014 rajveer 985
		populateBottomInvoiceTable(order, invoiceTable, rate);
6750 rajveer 986
 
7014 rajveer 987
		PdfPCell salesTaxCell = getPriceCell(salesTax);
9432 amar.kumar 988
 
9588 amar.kumar 989
		if(order.getTaxType().equals(TaxType.CFORM)) {
9432 amar.kumar 990
			invoiceTable.addCell(getCFORMLabelCell());
991
		} else {
992
			invoiceTable.addCell(getVATLabelCell(isVAT));
993
		}
7014 rajveer 994
		invoiceTable.addCell(new Phrase(rate + "%", helvetica8));
995
		invoiceTable.addCell(salesTaxCell);
996
 
997
		if(order.getInsurer() > 0) {
998
			invoiceTable.addCell(getInsuranceCell(3));
999
			invoiceTable.addCell(getPriceCell(order.getInsuranceAmount()));
1000
			invoiceTable.addCell(getPriceCell(order.getInsuranceAmount()));
1001
		}
1002
 
1003
		invoiceTable.addCell(getEmptyCell(5));
1004
 
1005
		invoiceTable.addCell(getTotalCell(3));
1006
		invoiceTable.addCell(getRupeesCell());
1007
		invoiceTable.addCell(getTotalAmountCell(orderAmount));
1008
 
1009
		invoiceTable.addCell(new Phrase("Amount in Words:", helvetica8));
1010
		invoiceTable.addCell(getAmountInWordsCell(orderAmount));
1011
 
1012
		invoiceTable.addCell(getEOECell(5));
1013
 
1014
		return invoiceTable;
1015
	}
1016
 
1017
	private PdfPCell getInvoiceTableHeader(int colspan) {
1018
		PdfPCell invoiceTableHeader = new PdfPCell(new Phrase("Order Details:", helveticaBold12));
1019
		invoiceTableHeader.setBorder(Rectangle.NO_BORDER);
1020
		invoiceTableHeader.setColspan(colspan);
8551 manish.sha 1021
		invoiceTableHeader.setPaddingTop(1);
7014 rajveer 1022
		return invoiceTableHeader;
1023
	}
1024
 
1025
	private void populateBottomInvoiceTable(Order order, PdfPTable invoiceTable, double rate) {
1026
		for (LineItem lineitem : order.getLineitems()) {
1027
			invoiceTable.addCell(new Phrase("" + order.getId() , helvetica8));
1028
 
7190 amar.kumar 1029
			invoiceTable.addCell(getProductNameCell(lineitem, true, order.getFreebieItemId()));
7014 rajveer 1030
 
1031
			invoiceTable.addCell(new Phrase("" + lineitem.getQuantity(), helvetica8));
1032
 
1033
			double itemPrice = lineitem.getUnit_price();
1034
			double showPrice = (100 * itemPrice)/(100 + rate);
1035
			invoiceTable.addCell(getPriceCell(showPrice)); //Unit Price Cell
1036
 
1037
			double totalPrice = lineitem.getTotal_price();
1038
			showPrice = (100 * totalPrice)/(100 + rate);
1039
			invoiceTable.addCell(getPriceCell(showPrice));  //Total Price Cell
1040
		}
1041
	}
1042
 
7190 amar.kumar 1043
	private PdfPCell getProductNameCell(LineItem lineitem, boolean appendIMEI, Long freebieItemId) {
7014 rajveer 1044
		String itemName = getItemDisplayName(lineitem, appendIMEI);
7190 amar.kumar 1045
		if(freebieItemId!=null && freebieItemId!=0){
1046
			try {
1047
				CatalogService.Client catalogClient = ctsc.getClient();
1048
				Item item = catalogClient.getItem(freebieItemId);
1049
				itemName = itemName + "\n(Free Item: " + item.getBrand() + " " + item.getModelName() + " " + item.getModelNumber() + ")";
1050
			} catch(Exception tex) {
1051
				logger.error("Not able to get Freebie Item Details for ItemId:" + freebieItemId, tex);
1052
			}
1053
		}
7014 rajveer 1054
		PdfPCell productNameCell = new PdfPCell(new Phrase(itemName, helvetica8));
1055
		productNameCell.setHorizontalAlignment(Element.ALIGN_LEFT);
1056
		return productNameCell;
1057
	}
1058
 
1059
	private PdfPCell getPriceCell(double price) {
1060
		PdfPCell totalPriceCell = new PdfPCell(new Phrase(amountFormat.format(price), helvetica8));
1061
		totalPriceCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
1062
		return totalPriceCell;
1063
	}
1064
 
1065
	private PdfPCell getVATLabelCell(boolean isVAT) {
1066
		PdfPCell vatCell = null;
1067
		if(isVAT){
1068
			vatCell = new PdfPCell(new Phrase("VAT", helveticaBold8));
1069
		} else {
1070
			vatCell = new PdfPCell(new Phrase("CST", helveticaBold8));
1071
		}
1072
		vatCell.setColspan(3);
1073
		vatCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
1074
		return vatCell;
1075
	}
1076
 
9432 amar.kumar 1077
	private PdfPCell getCFORMLabelCell() {
1078
		PdfPCell cFormCell = null;
1079
		cFormCell = new PdfPCell(new Phrase("CST Against CForm", helveticaBold8));
1080
		cFormCell.setColspan(3);
1081
		cFormCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
1082
		return cFormCell;
1083
	}
1084
 
7318 rajveer 1085
	private PdfPCell getAdvanceAmountCell(int colspan) {
1086
		PdfPCell insuranceCell = null;
1087
		insuranceCell = new PdfPCell(new Phrase("Advance Amount Received", helvetica8));
1088
		insuranceCell.setColspan(colspan);
1089
		insuranceCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
1090
		return insuranceCell;
1091
	}
1092
 
7014 rajveer 1093
	private PdfPCell getInsuranceCell(int colspan) {
1094
		PdfPCell insuranceCell = null;
1095
		insuranceCell = new PdfPCell(new Phrase("1 Year WorldWide Theft Insurance", helvetica8));
1096
		insuranceCell.setColspan(colspan);
1097
		insuranceCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
1098
		return insuranceCell;
1099
	}
1100
 
1101
	private PdfPCell getEmptyCell(int colspan) {
1102
		PdfPCell emptyCell = new PdfPCell(new Phrase(" ", helvetica8));
1103
		emptyCell.setColspan(colspan);
1104
		return emptyCell;
1105
	}
1106
 
1107
	private PdfPCell getTotalCell(int colspan) {
1108
		PdfPCell totalCell = new PdfPCell(new Phrase("Total", helveticaBold8));
1109
		totalCell.setColspan(colspan);
1110
		totalCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
1111
		return totalCell;
1112
	}
1113
 
1114
	private PdfPCell getRupeesCell() {
1115
		PdfPCell rupeesCell = new PdfPCell(new Phrase("Rs.", helveticaBold8));
1116
		rupeesCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
1117
		return rupeesCell;
1118
	}
1119
 
1120
	private PdfPCell getTotalAmountCell(double orderAmount) {
1121
		PdfPCell totalAmountCell = new PdfPCell(new Phrase(amountFormat.format(orderAmount), helveticaBold8));
1122
		totalAmountCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
1123
		return totalAmountCell;
1124
	}
1125
 
1126
	/**
1127
	 * This method uses ICU4J libraries to convert the given amount into words
1128
	 * of Indian locale.
1129
	 * 
1130
	 * @param orderAmount
1131
	 *            The amount to convert.
1132
	 * @return the string representation of the given amount.
1133
	 */
1134
	private PdfPCell getAmountInWordsCell(double orderAmount) {
1135
		RuleBasedNumberFormat amountInWordsFormat = new RuleBasedNumberFormat(indianLocale, RuleBasedNumberFormat.SPELLOUT);
1136
		StringBuilder amountInWords = new StringBuilder("Rs. ");
1137
		amountInWords.append(WordUtils.capitalize(amountInWordsFormat.format((int)orderAmount)));
1138
		amountInWords.append(" and ");
1139
		amountInWords.append(WordUtils.capitalize(amountInWordsFormat.format((int)(orderAmount*100)%100)));
1140
		amountInWords.append(" paise");
1141
 
1142
		PdfPCell amountInWordsCell= new PdfPCell(new Phrase(amountInWords.toString(), helveticaBold8));
1143
		amountInWordsCell.setColspan(4);
1144
		return amountInWordsCell;
1145
	}
1146
 
1147
	/**
1148
	 * Returns the item name to be displayed in the invoice table.
1149
	 * 
1150
	 * @param lineitem
1151
	 *            The line item whose name has to be displayed
1152
	 * @param appendIMEI
1153
	 *            Whether to attach the IMEI No. to the item name
1154
	 * @return The name to be displayed for the given line item.
1155
	 */
1156
	private String getItemDisplayName(LineItem lineitem, boolean appendIMEI){
1157
		StringBuffer itemName = new StringBuffer();
1158
		if(lineitem.getBrand()!= null)
1159
			itemName.append(lineitem.getBrand() + " ");
1160
		if(lineitem.getModel_name() != null)
1161
			itemName.append(lineitem.getModel_name() + " ");
1162
		if(lineitem.getModel_number() != null )
1163
			itemName.append(lineitem.getModel_number() + " ");
1164
		if(lineitem.getColor() != null && !lineitem.getColor().trim().equals("NA"))
1165
			itemName.append("("+lineitem.getColor()+")");
1166
		if(appendIMEI && lineitem.isSetSerial_number()){
1167
			itemName.append("\nIMEI No. " + lineitem.getSerial_number());
1168
		}
1169
 
1170
		return itemName.toString();
1171
	}
1172
 
1173
	/**
1174
	 * 
1175
	 * @param colspan
1176
	 * @return a PdfPCell containing the E&amp;OE text and spanning the given
1177
	 *         no. of columns
1178
	 */
1179
	private PdfPCell getEOECell(int colspan) {
1180
		PdfPCell eoeCell = new PdfPCell(new Phrase("E & O.E", helvetica8));
1181
		eoeCell.setColspan(colspan);
1182
		eoeCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
1183
		return eoeCell;
1184
	}
1185
 
1186
	private PdfPTable getExtraInfoTable(Order order, Provider provider, float barcodeFontSize, BillingType billingType){
1187
		PdfPTable extraInfoTable = new PdfPTable(1);
1188
		extraInfoTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
1189
		extraInfoTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
1190
 
1191
		String fontPath = InvoiceGenerationService.class.getResource("/saholic-wn.TTF").getPath();
1192
		FontFactoryImp ttfFontFactory = new FontFactoryImp();
1193
		ttfFontFactory.register(fontPath, "barcode");
1194
		Font barCodeFont = ttfFontFactory.getFont("barcode", BaseFont.CP1252, true, barcodeFontSize);
1195
 
1196
		PdfPCell extraInfoCell;
1197
		if(billingType == BillingType.EXTERNAL){
1198
			extraInfoCell = new PdfPCell(new Paragraph( "*" + order.getId() + "*        *" + order.getCustomer_name() + "*        *"  + order.getTotal_amount() + "*", barCodeFont));
1199
		}else{
1200
			extraInfoCell = new PdfPCell(new Paragraph( "*" + order.getId() + "*        *" + order.getLineitems().get(0).getTransfer_price() + "*", barCodeFont));	
1201
		}
1202
 
1203
		extraInfoCell.setPaddingTop(20.0f);
1204
		extraInfoCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
1205
		extraInfoCell.setBorder(Rectangle.NO_BORDER);
1206
 
1207
		extraInfoTable.addCell(extraInfoCell);
1208
 
1209
 
1210
		return extraInfoTable;
1211
	}
1212
 
1213
	private PdfPTable getFixedTextTable(float barcodeFontSize, String printText){
1214
		PdfPTable extraInfoTable = new PdfPTable(1);
1215
		extraInfoTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
1216
		extraInfoTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
1217
 
1218
		String fontPath = InvoiceGenerationService.class.getResource("/saholic-wn.TTF").getPath();
1219
		FontFactoryImp ttfFontFactory = new FontFactoryImp();
1220
		ttfFontFactory.register(fontPath, "barcode");
1221
		Font barCodeFont = ttfFontFactory.getFont("barcode", BaseFont.CP1252, true, barcodeFontSize);
1222
 
1223
		PdfPCell extraInfoCell = new PdfPCell(new Paragraph( "*" + printText + "*", barCodeFont));
1224
 
1225
		extraInfoCell.setPaddingTop(20.0f);
1226
		extraInfoCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
1227
		extraInfoCell.setBorder(Rectangle.NO_BORDER);
1228
 
1229
		extraInfoTable.addCell(extraInfoCell);
1230
 
1231
		return extraInfoTable;
1232
	}
8067 manish.sha 1233
 
1234
	private void generateBarcode(String barcodeString, String fileName){
1235
		Code128Bean bean = new Code128Bean();
7014 rajveer 1236
 
8067 manish.sha 1237
		final int dpi = 60;
1238
 
1239
		//Configure the barcode generator
1240
		bean.setModuleWidth(UnitConv.in2mm(1.0f / dpi)); //makes the narrow bar 
1241
		                                                 //width exactly one pixel
1242
		bean.setFontSize(bean.getFontSize()+1.0f);
1243
		bean.doQuietZone(false);
1244
 
1245
		try {
1246
			File outputFile = new File("/tmp/"+fileName+".png");
1247
			OutputStream out = new FileOutputStream(outputFile);
1248
 
1249
		    //Set up the canvas provider for monochrome PNG output 
1250
		    BitmapCanvasProvider canvas = new BitmapCanvasProvider(
1251
		            out, "image/x-png", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);
1252
 
1253
		    //Generate the barcode
1254
		    bean.generateBarcode(canvas, barcodeString);
1255
 
1256
		    //Signal end of generation
1257
		    canvas.finish();
1258
		    out.close();
1259
 
1260
		} 
1261
		catch(Exception e){
1262
			logger.error("Exception during generating Barcode : ", e);
1263
		}
1264
	}
1265
 
7014 rajveer 1266
	public static void main(String[] args) throws IOException {
1267
		InvoiceGenerationService invoiceGenerationService = new InvoiceGenerationService();
7318 rajveer 1268
		long orderId = 356324;
1269
		ByteArrayOutputStream baos = invoiceGenerationService.generateInvoice(orderId, true, false, 1);
7014 rajveer 1270
		String userHome = System.getProperty("user.home");
1271
		File f = new File(userHome + "/invoice-" + orderId + ".pdf");
1272
		FileOutputStream fos = new FileOutputStream(f);
1273
		baos.writeTo(fos);
1274
		System.out.println("Invoice generated.");
1275
	}
2787 chandransh 1276
}