Subversion Repositories SmartDukaan

Rev

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