Subversion Repositories SmartDukaan

Rev

Rev 10329 | Rev 10608 | 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
 
8110 manish.sha 414
		addressTable.addCell(new Phrase("If undelivered, return to:", helvetica10));
415
		addressTable.addCell(addressCell);
416
 
7014 rajveer 417
		PdfPTable addressAndNoteTable = new PdfPTable(new float[]{0.3f, 0.7f});
418
		addressAndNoteTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
8110 manish.sha 419
		addressAndNoteTable.addCell(addressTable);
7014 rajveer 420
		addressAndNoteTable.addCell(chargesTable);
2787 chandransh 421
 
8106 manish.sha 422
		titleBarTable.addCell(logoTable);
423
		titleBarTable.addCell(titleCell);
424
		titleBarTable.addCell(" ");
425
 
426
		table.addCell(titleBarTable);
7014 rajveer 427
		table.addCell(dispatchTable);
428
		table.addCell(invoiceTable);
429
		table.addCell(addressAndNoteTable);
430
		return table;
431
	}
2787 chandransh 432
 
8103 manish.sha 433
	private void addLogoTable(PdfPTable logoTable,Order order) {
7318 rajveer 434
		logoTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
435
		logoTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
436
		logoTable.getDefaultCell().setVerticalAlignment(Element.ALIGN_BOTTOM);
8096 manish.sha 437
 
7318 rajveer 438
		PdfPCell logoCell;
439
		String logoPath;
8102 manish.sha 440
 
7556 rajveer 441
		if(order.getSource() == OrderSource.STORE.getValue()){
442
			logoCell = new PdfPCell(new Phrase(""));
443
 
444
		}else{
8102 manish.sha 445
			logoPath = InvoiceGenerationService.class.getResource("/logo.jpg").getPath();
8094 manish.sha 446
 
7318 rajveer 447
			try {
448
				logoCell = new PdfPCell(Image.getInstance(logoPath), false);
449
			} catch (Exception e) {
450
				//Too Many exceptions to catch here: BadElementException, MalformedURLException and IOException
451
				logger.warn("Couldn't load the Saholic logo: ", e);
452
				logoCell = new PdfPCell(new Phrase("Saholic Logo"));
453
			}
454
 
455
		}
8090 manish.sha 456
		logoCell.setBorder(Rectangle.NO_BORDER);
457
		logoCell.setHorizontalAlignment(Element.ALIGN_LEFT);
8102 manish.sha 458
		logoTable.addCell(logoCell);
459
		logoTable.addCell(" ");
8103 manish.sha 460
 
7318 rajveer 461
	}
462
 
7014 rajveer 463
	private Font getBarCodeFont(Provider provider, float barcodeFontSize) {
464
		String fontPath = InvoiceGenerationService.class.getResource("/" + provider.getName().toLowerCase() + "/barcode.TTF").getPath();
465
		FontFactoryImp ttfFontFactory = new FontFactoryImp();
466
		ttfFontFactory.register(fontPath, "barcode");
467
		Font barCodeFont = ttfFontFactory.getFont("barcode", BaseFont.CP1252, true, barcodeFontSize);
468
		return barCodeFont;
469
	}
2787 chandransh 470
 
7014 rajveer 471
	private PdfPCell getTitleCell() {
472
		PdfPCell titleCell = new PdfPCell(new Phrase("Dispatch Advice", helveticaBold12));
473
		titleCell.setHorizontalAlignment(Element.ALIGN_CENTER);
474
		titleCell.setBorder(Rectangle.NO_BORDER);
475
		return titleCell;
476
	}
2787 chandransh 477
 
8039 manish.sha 478
	private PdfPTable getProviderTable(Order order, Provider provider, Font barCodeFont) {
7014 rajveer 479
		PdfPTable providerInfoTable = new PdfPTable(1);
480
		providerInfoTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
7318 rajveer 481
		if(order.isLogisticsCod()){
8551 manish.sha 482
			PdfPCell deliveryTypeCell = new PdfPCell(new Phrase("COD   ", helvetica22));
7318 rajveer 483
			deliveryTypeCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
484
			deliveryTypeCell.setBorder(Rectangle.NO_BORDER);
485
			providerInfoTable.addCell(deliveryTypeCell);
486
		}
487
 
8035 manish.sha 488
 
7014 rajveer 489
		PdfPCell providerNameCell = new PdfPCell(new Phrase(provider.getName(), helveticaBold12));
490
		providerNameCell.setHorizontalAlignment(Element.ALIGN_LEFT);
491
		providerNameCell.setBorder(Rectangle.NO_BORDER);
7994 manish.sha 492
		PdfPCell formIdCell= null;
493
		if(order.getLogistics_provider_id()==7L){
494
			if(order.isCod()){
8034 manish.sha 495
				formIdCell = new PdfPCell(new Paragraph(order.getAirwaybill_no()+" Form id-0305", helvetica6));
7994 manish.sha 496
			}
497
			else{
8034 manish.sha 498
				formIdCell = new PdfPCell(new Paragraph(order.getAirwaybill_no()+" Form id-0467", helvetica6));
7994 manish.sha 499
			}
8551 manish.sha 500
			formIdCell.setPaddingTop(1.0f);
8015 rajveer 501
			formIdCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
502
			formIdCell.setBorder(Rectangle.NO_BORDER);
7994 manish.sha 503
		}
8015 rajveer 504
 
7994 manish.sha 505
 
506
		PdfPCell awbNumberCell= null;
8034 manish.sha 507
		String fedexPackageBarcode = "";
7994 manish.sha 508
		if(order.getLogistics_provider_id()!=7L){
509
			awbNumberCell = new PdfPCell(new Paragraph("*" + order.getAirwaybill_no() + "*", barCodeFont));
8017 manish.sha 510
			awbNumberCell.setPaddingTop(20.0f);
7994 manish.sha 511
		}
512
		else{
8013 rajveer 513
			in.shop2020.model.v1.order.TransactionService.Client tclient = tsc.getClient();
514
			try {
515
				fedexPackageBarcode = tclient.getOrderAttributeValue(order.getId(), "FedEx_Package_BarCode");
516
			} catch (TException e1) {
517
				logger.error("Error while getting the provider information.", e1);
518
			}
8174 manish.sha 519
			awbNumberCell = new PdfPCell(new Paragraph(" ", helvetica6));
520
		}
521
		awbNumberCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
522
		awbNumberCell.setBorder(Rectangle.NO_BORDER);
523
 
524
		providerInfoTable.addCell(providerNameCell);
525
		if(formIdCell != null){
526
			providerInfoTable.addCell(formIdCell);
527
		}
528
		//End:-Added By Manish Sharma for FedEx Integration - Shipment Creation on 21-Aug-2013
529
		if(order.getLogistics_provider_id()==7L){
8169 manish.sha 530
			generateBarcode(fedexPackageBarcode, "fedex_"+order.getId());
531
 
532
			Image barcodeImage=null;
533
			try {
534
				barcodeImage = Image.getInstance("/tmp/"+"fedex_"+order.getId()+".png");
535
			} catch (Exception e) {
536
				logger.error("Exception during getting Barcode Image for Fedex : ", e);
537
			}
8174 manish.sha 538
			providerInfoTable.addCell(barcodeImage);
7994 manish.sha 539
		}
8174 manish.sha 540
		providerInfoTable.addCell(awbNumberCell);
7014 rajveer 541
 
7792 anupam.sin 542
		Warehouse warehouse = null;
543
		try{
544
    		InventoryClient isc = new InventoryClient();
7804 amar.kumar 545
    		warehouse = isc.getClient().getWarehouse(order.getWarehouse_id());
7803 amar.kumar 546
		} catch(Exception e) {
7792 anupam.sin 547
		    logger.error("Unable to get warehouse for id : " + order.getWarehouse_id(), e);
7805 amar.kumar 548
		    //TODO throw e;
7792 anupam.sin 549
		}
550
		DeliveryType dt =  DeliveryType.PREPAID;
551
        if (order.isLogisticsCod()) {
552
            dt = DeliveryType.COD;
553
        }
7994 manish.sha 554
        //Start:-Added By Manish Sharma for FedEx Integration - Shipment Creation on 21-Aug-2013
555
        if(order.getLogistics_provider_id()!=7L){
556
	        for (ProviderDetails detail : provider.getDetails()) {
557
	            if(in.shop2020.model.v1.inventory.WarehouseLocation.findByValue((int) detail.getLogisticLocation()) == warehouse.getLogisticsLocation() && detail.getDeliveryType() == dt) {
558
	                providerInfoTable.addCell(new Phrase("Account No : " + detail.getAccountNo(), helvetica8));
559
	            }
560
	        }
7792 anupam.sin 561
        }
7994 manish.sha 562
        else{
563
        	providerInfoTable.addCell(new Phrase("STANDARD OVERNIGHT ", helvetica8));
564
        }
565
        //End:-Added By Manish Sharma for FedEx Integration - Shipment Creation on 21-Aug-2013
7014 rajveer 566
		Date awbDate;
567
		if(order.getBilling_timestamp() == 0){
568
			awbDate = new Date();
569
		}else{
570
			awbDate = new Date(order.getBilling_timestamp());
571
		}
8106 manish.sha 572
		if(order.getLogistics_provider_id()!=7L){
573
			providerInfoTable.addCell(new Phrase("AWB Date   : " + DateFormat.getDateInstance(DateFormat.MEDIUM).format(awbDate), helvetica8));
574
		}
7014 rajveer 575
		providerInfoTable.addCell(new Phrase("Weight         : " + order.getTotal_weight() + " Kg", helvetica8));
8182 amar.kumar 576
		if(order.getSource() == OrderSource.EBAY.getValue()){
577
			EbayOrder ebayOrder = null;
578
			try {
579
				ebayOrder = tsc.getClient().getEbayOrderByOrderId(order.getId());
580
			} catch (TException e) {
581
				logger.error("Error while getting ebay order", e);
582
			}
583
			providerInfoTable.addCell(new Phrase("PaisaPayId            : " + ebayOrder.getPaisaPayId(), helvetica8));
584
			providerInfoTable.addCell(new Phrase("Sales Rec Number: " + ebayOrder.getSalesRecordNumber(), helvetica8));
585
		}
7994 manish.sha 586
		//Start:-Added By Manish Sharma for FedEx Integration - Shipment Creation on 21-Aug-2013
587
		if(order.getLogistics_provider_id()==7L){
588
			providerInfoTable.addCell(new Phrase("Bill T/C Sender      "+ "Bill D/T Sender", helvetica8));
589
		}
590
		//End:-Added By Manish Sharma for FedEx Integration - Shipment Creation on 21-Aug-2013
7014 rajveer 591
		return providerInfoTable;
592
	}
593
 
594
	private PdfPTable getTopInvoiceTable(Order order, String tinNo){
595
		PdfPTable invoiceTable = new PdfPTable(new float[]{0.2f, 0.2f, 0.3f, 0.1f, 0.1f, 0.1f});
596
		invoiceTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
597
 
598
		invoiceTable.addCell(getInvoiceTableHeader(6));
599
 
600
		invoiceTable.addCell(new Phrase("Order No", helvetica8));
601
		invoiceTable.addCell(new Phrase("Paymode", helvetica8));
602
		invoiceTable.addCell(new Phrase("Product Name", helvetica8));
603
		invoiceTable.addCell(new Phrase("Quantity", helvetica8));
604
		invoiceTable.addCell(new Phrase("Rate", helvetica8));
605
		invoiceTable.addCell(new Phrase("Amount", helvetica8));
606
		populateTopInvoiceTable(order, invoiceTable);
607
 
608
 
609
		if(order.getInsurer() > 0) {
610
			invoiceTable.addCell(getInsuranceCell(4));
611
			invoiceTable.addCell(getPriceCell(order.getInsuranceAmount()));
612
			invoiceTable.addCell(getPriceCell(order.getInsuranceAmount()));
613
		}
614
 
7318 rajveer 615
		if(order.getSource() == OrderSource.STORE.getValue()) {
616
			invoiceTable.addCell(getAdvanceAmountCell(4));
617
			invoiceTable.addCell(getPriceCell(order.getAdvanceAmount()));
618
			invoiceTable.addCell(getPriceCell(order.getAdvanceAmount()));
619
		}
620
 
7014 rajveer 621
		invoiceTable.addCell(getTotalCell(4));      
622
		invoiceTable.addCell(getRupeesCell());
7318 rajveer 623
		invoiceTable.addCell(getTotalAmountCell(order.getTotal_amount()-order.getGvAmount()-order.getAdvanceAmount()));
7014 rajveer 624
 
625
		PdfPCell tinCell = new PdfPCell(new Phrase("TIN NO. " + tinNo, helvetica8));
626
		tinCell.setColspan(6);
627
		tinCell.setPadding(2);
628
		invoiceTable.addCell(tinCell);
629
 
630
		return invoiceTable;
631
	}
632
 
633
	private void populateTopInvoiceTable(Order order, PdfPTable invoiceTable) {
634
		List<LineItem> lineitems = order.getLineitems();
635
		for (LineItem lineitem : lineitems) {
636
			invoiceTable.addCell(new Phrase(order.getId() + "", helvetica8));
637
			if(order.getPickupStoreId() > 0 && order.isCod() == true)
638
				invoiceTable.addCell(new Phrase("In-Store", helvetica8));
639
			else if (order.isCod())
640
				invoiceTable.addCell(new Phrase("COD", helvetica8));
641
			else
642
				invoiceTable.addCell(new Phrase("Prepaid", helvetica8));
7318 rajveer 643
 
7190 amar.kumar 644
			invoiceTable.addCell(getProductNameCell(lineitem, false, order.getFreebieItemId()));
2787 chandransh 645
 
7014 rajveer 646
			invoiceTable.addCell(new Phrase(lineitem.getQuantity() + "", helvetica8));
2787 chandransh 647
 
7014 rajveer 648
			invoiceTable.addCell(getPriceCell(lineitem.getUnit_price()-order.getGvAmount()));
649
 
650
			invoiceTable.addCell(getPriceCell(lineitem.getTotal_price()-order.getGvAmount()));
651
		}
652
	}
653
 
654
	private PdfPCell getAddressCell(String address) {
655
		Paragraph addressParagraph = new Paragraph(address, new Font(FontFamily.TIMES_ROMAN, 8f));
656
		PdfPCell addressCell = new PdfPCell();
657
		addressCell.addElement(addressParagraph);
658
		addressCell.setHorizontalAlignment(Element.ALIGN_LEFT);
659
		addressCell.setBorder(Rectangle.NO_BORDER);
660
		return addressCell;
661
	}
662
 
8011 rajveer 663
	private PdfPTable getTaxCumRetailInvoiceTable(Order order, Provider provider, String ourAddress, String tinNo){
7014 rajveer 664
		PdfPTable taxTable = new PdfPTable(1);
665
		Phrase phrase = null;
666
		taxTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
667
		taxTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
8104 manish.sha 668
 
8110 manish.sha 669
		PdfPTable logoTitleAndOurAddressTable = new PdfPTable(new float[]{0.4f, 0.3f, 0.3f});
8107 manish.sha 670
		logoTitleAndOurAddressTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
8112 manish.sha 671
		logoTitleAndOurAddressTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
8107 manish.sha 672
 
8110 manish.sha 673
 
8103 manish.sha 674
		PdfPTable logoTable = new PdfPTable(2);
675
		addLogoTable(logoTable,order); 
7318 rajveer 676
 
7014 rajveer 677
		if (order.getOrderType().equals(OrderType.B2B)) {
678
			phrase = new Phrase("TAX INVOICE", helveticaBold12);
679
		} else {
680
			phrase = new Phrase("RETAIL INVOICE", helveticaBold12);
681
		}
682
		PdfPCell retailInvoiceTitleCell = new PdfPCell(phrase);
683
		retailInvoiceTitleCell.setHorizontalAlignment(Element.ALIGN_CENTER);
684
		retailInvoiceTitleCell.setBorder(Rectangle.NO_BORDER);
685
 
8169 manish.sha 686
		Paragraph sorlAddress = new Paragraph(ourAddress + "\n Contact No.- 0120-2479977" + "\nTIN NO. " + tinNo, new Font(FontFamily.TIMES_ROMAN, 8f, Element.ALIGN_CENTER));
7014 rajveer 687
		PdfPCell sorlAddressCell = new PdfPCell(sorlAddress);
688
		sorlAddressCell.addElement(sorlAddress);
8110 manish.sha 689
		sorlAddressCell.setHorizontalAlignment(Element.ALIGN_LEFT);
7014 rajveer 690
 
691
		PdfPTable customerAddress = getCustomerAddressTable(order, null, true, helvetica8, true);
692
		PdfPTable orderDetails = getOrderDetails(order, provider);
693
 
694
		PdfPTable addrAndOrderDetailsTable = new PdfPTable(new float[]{0.5f, 0.1f, 0.4f});
695
		addrAndOrderDetailsTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
696
		addrAndOrderDetailsTable.addCell(customerAddress);
697
		addrAndOrderDetailsTable.addCell(new Phrase(" "));
698
		addrAndOrderDetailsTable.addCell(orderDetails);
699
 
9319 amar.kumar 700
		boolean isVAT = isVatApplicable(order);
7014 rajveer 701
		PdfPTable invoiceTable = getBottomInvoiceTable(order, isVAT);
702
 
10607 manish.sha 703
		PdfPTable regAddAndDisCellTable = new PdfPTable(2);
704
 
7014 rajveer 705
		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));
706
		disclaimerCell.setHorizontalAlignment(Element.ALIGN_LEFT);
707
		disclaimerCell.setBorder(Rectangle.NO_BORDER);
7318 rajveer 708
 
10607 manish.sha 709
		PdfPCell regAddressCell = new PdfPCell(new Phrase(" SPICE ONLINE RETAIL PRIVATE LIMITED\n Regd. Add. 60-D, STREET NO. C-5, SAINIK FARMS,NEW DELHI-110062\n CIN: U74140DL2008PTC183856\n Tel. No. 0120-2479977\n E-mail. help@saholic.com\n Website. www.saholic.com", helvetica8));
710
		regAddressCell.setHorizontalAlignment(Element.ALIGN_LEFT);
711
		regAddressCell.setBorder(Rectangle.NO_BORDER);
712
		/*SPICE ONLINE RETAIL PRIVATE LIMITED
713
		Regd. Add. 60-D, STREET NO. C-5, SAINIK FARMS,NEW DELHI-110062
714
		CIN: U74140DL2008PTC183856
715
		Tel. No. 0120-2479977
716
		E-mail. help@saholic.com
717
		Website. www.saholic.com*/
9014 amar.kumar 718
		PdfPCell powerTextCell = new PdfPCell(new Phrase("Powered By  Flipkart", helvetica8));
719
		powerTextCell.setHorizontalAlignment(Element.ALIGN_LEFT);
720
		powerTextCell.setBorder(Rectangle.NO_BORDER);
721
		powerTextCell.setPaddingBottom(30.0f);
8104 manish.sha 722
 
723
		logoTitleAndOurAddressTable.addCell(logoTable);
8110 manish.sha 724
		logoTitleAndOurAddressTable.addCell(retailInvoiceTitleCell);
725
		logoTitleAndOurAddressTable.addCell(sorlAddress);
8104 manish.sha 726
 
10607 manish.sha 727
		regAddAndDisCellTable.addCell(disclaimerCell);
728
		regAddAndDisCellTable.addCell(regAddressCell);
729
 
8104 manish.sha 730
		taxTable.addCell(logoTitleAndOurAddressTable);
7014 rajveer 731
		taxTable.addCell(addrAndOrderDetailsTable);
732
		taxTable.addCell(invoiceTable);
10320 amar.kumar 733
		taxTable.addCell(disclaimerCell);
9014 amar.kumar 734
		if(order.getSource() == OrderSource.FLIPKART.getValue()) {
735
			taxTable.addCell(powerTextCell);
736
 
737
		}
10320 amar.kumar 738
		if(order.getProductCondition().equals(ProductCondition.BAD)){
10328 amar.kumar 739
			PdfPCell badSaleDisclaimerCell = new PdfPCell(new Phrase(" Item(s) above are sold on as is where is basis. They " +
10320 amar.kumar 740
					"may be in dead/defective/damaged/refurbished/incomplete/open condition. These " +
741
					"are not returnable, exchangeable or refundable under any circumstances. No " +
742
					"warranty is assured on these items." ,
10329 amar.kumar 743
					new Font(FontFamily.TIMES_ROMAN, 8f)));
10328 amar.kumar 744
			badSaleDisclaimerCell.setHorizontalAlignment(Element.ALIGN_LEFT);
745
			badSaleDisclaimerCell.setBorder(Rectangle.NO_BORDER);
746
			taxTable.addCell(badSaleDisclaimerCell);
10320 amar.kumar 747
		}
7014 rajveer 748
		return taxTable;
749
	}
9009 amar.kumar 750
 
9319 amar.kumar 751
	private boolean isVatApplicable(Order order) {
752
		if(order.getWarehouse_id() == 7) {
753
			if(order.getCustomer_pincode().startsWith(delhiPincodePrefix)) {
754
				return true;
755
			} else {
756
				return false;
757
			}
758
		} else {
759
			for(int i=0; i< maharashtraPincodePrefix.length; i++) {
760
				if(order.getCustomer_pincode().startsWith(maharashtraPincodePrefix[i])) {
761
					return true;
762
				}
763
			}
764
			return false;
765
		}
766
	}
767
 
9037 amar.kumar 768
	private PdfPTable getFlipkartBarCodes(Order order) {
769
		PdfPTable flipkartTable = new PdfPTable(3);
770
 
771
		PdfPCell spacerCell = new PdfPCell();
9040 amar.kumar 772
		spacerCell.setBorder(Rectangle.NO_BORDER);
9037 amar.kumar 773
		spacerCell.setColspan(3);
9099 amar.kumar 774
		spacerCell.setPaddingTop(330.0f);
9037 amar.kumar 775
 
776
		String flipkartCodeFontPath = InvoiceGenerationService.class.getResource("/saholic-wn.TTF").getPath();
777
		FontFactoryImp ttfFontFactory = new FontFactoryImp();
778
		ttfFontFactory.register(flipkartCodeFontPath, "barcode");
779
		Font flipkartBarCodeFont = ttfFontFactory.getFont("barcode", BaseFont.CP1252, true, 20);
780
 
9040 amar.kumar 781
		String serialNumber = "0000000000";
9511 manish.sha 782
		if(order.getLineitems().get(0).getSerial_number()!=null && !order.getLineitems().get(0).getSerial_number().isEmpty()) {
9040 amar.kumar 783
			serialNumber = order.getLineitems().get(0).getSerial_number();
9511 manish.sha 784
		} else if(order.getLineitems().get(0).getItem_number()!=null && !order.getLineitems().get(0).getItem_number().isEmpty()) {
9040 amar.kumar 785
			serialNumber = order.getLineitems().get(0).getItem_number();
786
		}
787
 
788
		PdfPCell serialNumberBarCodeCell = new PdfPCell(new Paragraph("*" +  serialNumber + "*", flipkartBarCodeFont));
9037 amar.kumar 789
		serialNumberBarCodeCell.setBorder(Rectangle.TOP);
9044 amar.kumar 790
		serialNumberBarCodeCell.setHorizontalAlignment(Element.ALIGN_CENTER);
9037 amar.kumar 791
		serialNumberBarCodeCell.setPaddingTop(11.0f);
792
 
793
 
794
		PdfPCell invoiceNumberBarCodeCell = new PdfPCell(new Paragraph("*" +  order.getInvoice_number() + "*", flipkartBarCodeFont));
795
		invoiceNumberBarCodeCell.setBorder(Rectangle.TOP);
9044 amar.kumar 796
		invoiceNumberBarCodeCell.setHorizontalAlignment(Element.ALIGN_CENTER);
9037 amar.kumar 797
		invoiceNumberBarCodeCell.setPaddingTop(11.0f);
798
 
799
		double rate = order.getLineitems().get(0).getVatRate();
800
		double salesTax = (rate * order.getTotal_amount())/(100 + rate);
9040 amar.kumar 801
		PdfPCell vatAmtBarCodeCell = new PdfPCell(new Paragraph("*" +  amountFormat.format(salesTax) + "*", flipkartBarCodeFont));
9037 amar.kumar 802
		vatAmtBarCodeCell.setBorder(Rectangle.TOP);
9044 amar.kumar 803
		vatAmtBarCodeCell.setHorizontalAlignment(Element.ALIGN_CENTER);
9037 amar.kumar 804
		vatAmtBarCodeCell.setPaddingTop(11.0f);
805
 
806
		flipkartTable.addCell(spacerCell);
807
		flipkartTable.addCell(serialNumberBarCodeCell);
808
		flipkartTable.addCell(invoiceNumberBarCodeCell);
809
		flipkartTable.addCell(vatAmtBarCodeCell);
810
 
811
		return flipkartTable;
812
 
9009 amar.kumar 813
	}
814
 
7014 rajveer 815
	private PdfPTable getCustomerAddressTable(Order order, String destCode, boolean showPaymentMode, Font font, boolean forInvoce){
816
		PdfPTable customerTable = new PdfPTable(1);
817
		customerTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
818
		if(forInvoce || order.getPickupStoreId() == 0){
819
			customerTable.addCell(new Phrase(order.getCustomer_name(), font));
820
			customerTable.addCell(new Phrase(order.getCustomer_address1(), font));
821
			customerTable.addCell(new Phrase(order.getCustomer_address2(), font));
822
			customerTable.addCell(new Phrase(order.getCustomer_city() + "," + order.getCustomer_state(), font));
7994 manish.sha 823
			//Start:-Added By Manish Sharma for FedEx Integration - Shipment Creation on 21-Aug-2013
824
			if(order.getLogistics_provider_id()!=7L){
825
				if(destCode != null)
826
					customerTable.addCell(new Phrase(order.getCustomer_pincode() + " - " + destCode, helvetica16));
827
				else
828
					customerTable.addCell(new Phrase(order.getCustomer_pincode(), font));
829
				}
830
			else{
831
				in.shop2020.model.v1.order.TransactionService.Client tclient = tsc.getClient();
832
				String fedexLocationcode = "";
833
				try {
834
					fedexLocationcode = tclient.getOrderAttributeValue(order.getId(), "FedEx_Location_Code");
835
				} catch (TException e1) {
836
					logger.error("Error while getting the provider information.", e1);
837
				}
838
				customerTable.addCell(new Phrase(order.getCustomer_pincode() + " - " + fedexLocationcode, helvetica16));
839
			}
840
			//Start:-Added By Manish Sharma for FedEx Integration - Shipment Creation on 21-Aug-2013
9100 amar.kumar 841
			if(order.getCustomer_mobilenumber()!=null && !order.getCustomer_mobilenumber().isEmpty()) {
842
				customerTable.addCell(new Phrase("Phone : " + (order.getCustomer_mobilenumber()== null ? "" : order.getCustomer_mobilenumber()), font));
843
			}
7014 rajveer 844
		}else{
845
			try {
5556 rajveer 846
				in.shop2020.logistics.LogisticsService.Client lclient = (new LogisticsClient()).getClient();
7014 rajveer 847
				PickupStore store = lclient.getPickupStore(order.getPickupStoreId());
848
				customerTable.addCell(new Phrase(order.getCustomer_name() + " \nc/o " + store.getName(), font));
849
				customerTable.addCell(new Phrase(store.getLine1(), font));
850
				customerTable.addCell(new Phrase(store.getLine2(), font));
851
				customerTable.addCell(new Phrase(store.getCity() + "," + store.getState(), font));
852
				if(destCode != null)
853
					customerTable.addCell(new Phrase(store.getPin() + " - " + destCode, helvetica16));
854
				else
855
					customerTable.addCell(new Phrase(store.getPin(), font));
856
				customerTable.addCell(new Phrase("Phone :" + store.getPhone(), font));
5556 rajveer 857
			} catch (TException e) {
858
				// TODO Auto-generated catch block
859
				e.printStackTrace();
860
			}
5527 anupam.sin 861
 
7014 rajveer 862
		}
863
 
864
		if(order.getOrderType().equals(OrderType.B2B)) {
865
			String tin = null;
866
			in.shop2020.model.v1.order.TransactionService.Client tclient = tsc.getClient();
867
			List<Attribute> attributes;
868
			try {
869
				attributes = tclient.getAllAttributesForOrderId(order.getId());
870
 
871
				for(Attribute attribute : attributes) {
872
					if(attribute.getName().equals("tinNumber")) {
873
						tin = attribute.getValue();
874
					}
875
				}
876
				if (tin != null) {
877
					customerTable.addCell(new Phrase("TIN :" + tin, font));
878
				}
879
 
880
			} catch (Exception e) {
881
				logger.error("Error while getting order attributes", e);
882
			}
883
		}
884
		/*
2787 chandransh 885
        if(showPaymentMode){
886
            customerTable.addCell(new Phrase(" ", font));
887
            customerTable.addCell(new Phrase("Payment Mode: Prepaid", font));
5856 anupam.sin 888
        }*/
7014 rajveer 889
		return customerTable;
890
	}
2787 chandransh 891
 
7014 rajveer 892
	private PdfPTable getOrderDetails(Order order, Provider provider){
893
		PdfPTable orderTable = new PdfPTable(new float[]{0.4f, 0.6f});
894
		orderTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
2787 chandransh 895
 
7014 rajveer 896
		orderTable.addCell(new Phrase("Invoice No:", helvetica8));
897
		orderTable.addCell(new Phrase(order.getInvoice_number(), helvetica8));
2787 chandransh 898
 
7014 rajveer 899
		orderTable.addCell(new Phrase("Date:", helvetica8));
900
		orderTable.addCell(new Phrase(DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date(order.getBilling_timestamp())), helvetica8));
2787 chandransh 901
 
7014 rajveer 902
		orderTable.addCell(new Phrase("Order ID:", helvetica8));
903
		orderTable.addCell(new Phrase("" + order.getId(), helvetica8));
2787 chandransh 904
 
7528 rajveer 905
		if(order.getSource() == OrderSource.AMAZON.getValue()){
906
			AmazonOrder aorder = null;
907
			try {
908
				aorder = tsc.getClient().getAmazonOrder(order.getId());
909
			} catch (TException e) {
910
				logger.error("Error while getting amazon order", e);
911
			}
912
			orderTable.addCell(new Phrase("Amazon Order ID:", helvetica8));
913
			orderTable.addCell(new Phrase(aorder.getAmazonOrderCode(), helvetica8));
8182 amar.kumar 914
		} else if(order.getSource() == OrderSource.EBAY.getValue()){
915
			EbayOrder ebayOrder = null;
916
			try {
917
				ebayOrder = tsc.getClient().getEbayOrderByOrderId(order.getId());
918
			} catch (TException e) {
919
				logger.error("Error while getting ebay order", e);
920
			}
921
			orderTable.addCell(new Phrase("PaisaPayId:", helvetica8));
922
			orderTable.addCell(new Phrase(ebayOrder.getPaisaPayId(), helvetica8));
923
			orderTable.addCell(new Phrase("Sales Rec Number:", helvetica8));
924
			orderTable.addCell(new Phrase(new Long(ebayOrder.getSalesRecordNumber()).toString(), helvetica8));
8488 amar.kumar 925
		} else if(order.getSource() == OrderSource.SNAPDEAL.getValue()){
926
			SnapdealOrder snapdealOrder = null;
927
			try {
928
				snapdealOrder = tsc.getClient().getSnapdealOrder(order.getId(), null, 0);
929
			} catch (TException e) {
930
				logger.error("Error while getting snapdeal order", e);
931
			}
932
			orderTable.addCell(new Phrase("Snapdeal OrderId:", helvetica8));
933
			orderTable.addCell(new Phrase(new Long(snapdealOrder.getSubOrderId()).toString(), helvetica8));
8828 amar.kumar 934
 
935
			String refernceCodeFontPath = InvoiceGenerationService.class.getResource("/saholic-wn.TTF").getPath();
936
			FontFactoryImp ttfFontFactory = new FontFactoryImp();
937
			ttfFontFactory.register(refernceCodeFontPath, "barcode");
8876 amar.kumar 938
			Font referenceCodeBarCodeFont = ttfFontFactory.getFont("barcode", BaseFont.CP1252, true, 20);
8828 amar.kumar 939
 
8874 amar.kumar 940
			PdfPCell snapdealReferenceBarCodeCell = new PdfPCell(new Paragraph("*" +  snapdealOrder.getReferenceCode() + "*", referenceCodeBarCodeFont));
8828 amar.kumar 941
			snapdealReferenceBarCodeCell.setBorder(Rectangle.NO_BORDER);
8876 amar.kumar 942
			snapdealReferenceBarCodeCell.setPaddingTop(9.0f);
943
			snapdealReferenceBarCodeCell.setColspan(2);
9042 amar.kumar 944
			snapdealReferenceBarCodeCell.setHorizontalAlignment(Element.ALIGN_CENTER);
8876 amar.kumar 945
			//orderTable.addCell(new Phrase("Snapdeal ReferenceCode:", helvetica8));
8828 amar.kumar 946
			orderTable.addCell(snapdealReferenceBarCodeCell);
947
			//orderTable.addCell(new Phrase(snapdealOrder.getReferenceCode(), helvetica8));
7528 rajveer 948
		}
8989 vikram.rag 949
		else if(order.getSource() == OrderSource.FLIPKART.getValue()){
950
			FlipkartOrder flipkartOrder = null;
951
			try {
952
				flipkartOrder = tsc.getClient().getFlipkartOrder(order.getId());
953
			} catch (TException e) {
8996 amar.kumar 954
				logger.error("Error while getting flipkart order", e);
8989 vikram.rag 955
			}
956
			orderTable.addCell(new Phrase("Flipkart OrderId:", helvetica8));
957
			orderTable.addCell(new Phrase(flipkartOrder.getFlipkartOrderId(), helvetica8));
9037 amar.kumar 958
 
959
			//orderTable.addCell(new Phrase("Flipkart OrderItemId:", helvetica8));
960
			String flipkartBarCodeFontPath = InvoiceGenerationService.class.getResource("/saholic-wn.TTF").getPath();
961
			FontFactoryImp ttfFontFactory = new FontFactoryImp();
962
			ttfFontFactory.register(flipkartBarCodeFontPath, "barcode");
9043 amar.kumar 963
			Font flipkartBarCodeFont = ttfFontFactory.getFont("barcode", BaseFont.CP1252, true, 18);
9037 amar.kumar 964
 
9043 amar.kumar 965
			orderTable.addCell(new Phrase("Flipkart OrderItemId:", helvetica8));
9037 amar.kumar 966
			PdfPCell flipkartOrderItemIdBarCodeCell = new PdfPCell(new Paragraph("*" +  new Long(flipkartOrder.getFlipkartSubOrderId()).toString() + "*", flipkartBarCodeFont));
967
			flipkartOrderItemIdBarCodeCell.setBorder(Rectangle.NO_BORDER);
968
			flipkartOrderItemIdBarCodeCell.setPaddingTop(9.0f);
9043 amar.kumar 969
			//flipkartOrderItemIdBarCodeCell.setColspan(2);
970
			//flipkartOrderItemIdBarCodeCell.setHorizontalAlignment(Element.ALIGN_CENTER);
9037 amar.kumar 971
			orderTable.addCell(flipkartOrderItemIdBarCodeCell);
972
			//orderTable.addCell(new Phrase("Flipkart OrderItemId:", helvetica8));
973
			//orderTable.addCell(new Phrase(new Long(flipkartOrder.getFlipkartSubOrderId()).toString(), helvetica8));
8989 vikram.rag 974
		}
975
 
976
 
7014 rajveer 977
		orderTable.addCell(new Phrase("Order Date:", helvetica8));
978
		orderTable.addCell(new Phrase(DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date(order.getCreated_timestamp())), helvetica8));
2787 chandransh 979
 
7014 rajveer 980
		orderTable.addCell(new Phrase("Courier:", helvetica8));
981
		orderTable.addCell(new Phrase(provider.getName(), helvetica8));
2787 chandransh 982
 
9038 amar.kumar 983
		if(order.getAirwaybill_no()!=null && !order.getAirwaybill_no().isEmpty()) {
984
			orderTable.addCell(new Phrase("AWB No:", helvetica8));
985
			orderTable.addCell(new Phrase(order.getAirwaybill_no(), helvetica8));
986
		}
2787 chandransh 987
 
7014 rajveer 988
		orderTable.addCell(new Phrase("AWB Date:", helvetica8));
989
		orderTable.addCell(new Phrase(DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date(order.getBilling_timestamp())), helvetica8));
2787 chandransh 990
 
7014 rajveer 991
		return orderTable;
992
	}
2787 chandransh 993
 
7014 rajveer 994
	private PdfPTable getBottomInvoiceTable(Order order, boolean isVAT){
995
		PdfPTable invoiceTable = new PdfPTable(new float[]{0.2f, 0.5f, 0.1f, 0.1f, 0.1f});
996
		invoiceTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
2787 chandransh 997
 
7014 rajveer 998
		invoiceTable.addCell(getInvoiceTableHeader(5));
4262 rajveer 999
 
7014 rajveer 1000
		invoiceTable.addCell(new Phrase("Sl. No.", helveticaBold8));
1001
		invoiceTable.addCell(new Phrase("Description", helveticaBold8));
1002
		invoiceTable.addCell(new Phrase("Quantity", helveticaBold8));
1003
		invoiceTable.addCell(new Phrase("Rate (Rs)", helveticaBold8));
1004
		invoiceTable.addCell(new Phrase("Amount (Rs)", helveticaBold8));
1005
		LineItem lineItem = order.getLineitems().get(0);
1006
		double orderAmount = order.getTotal_amount();
1007
		double rate = lineItem.getVatRate();
7057 amar.kumar 1008
		double salesTax = (rate * (orderAmount - order.getInsuranceAmount()))/(100 + rate);
6750 rajveer 1009
 
7014 rajveer 1010
		populateBottomInvoiceTable(order, invoiceTable, rate);
6750 rajveer 1011
 
7014 rajveer 1012
		PdfPCell salesTaxCell = getPriceCell(salesTax);
9432 amar.kumar 1013
 
9588 amar.kumar 1014
		if(order.getTaxType().equals(TaxType.CFORM)) {
9432 amar.kumar 1015
			invoiceTable.addCell(getCFORMLabelCell());
1016
		} else {
1017
			invoiceTable.addCell(getVATLabelCell(isVAT));
1018
		}
7014 rajveer 1019
		invoiceTable.addCell(new Phrase(rate + "%", helvetica8));
1020
		invoiceTable.addCell(salesTaxCell);
1021
 
1022
		if(order.getInsurer() > 0) {
1023
			invoiceTable.addCell(getInsuranceCell(3));
1024
			invoiceTable.addCell(getPriceCell(order.getInsuranceAmount()));
1025
			invoiceTable.addCell(getPriceCell(order.getInsuranceAmount()));
1026
		}
1027
 
1028
		invoiceTable.addCell(getEmptyCell(5));
1029
 
1030
		invoiceTable.addCell(getTotalCell(3));
1031
		invoiceTable.addCell(getRupeesCell());
1032
		invoiceTable.addCell(getTotalAmountCell(orderAmount));
1033
 
1034
		invoiceTable.addCell(new Phrase("Amount in Words:", helvetica8));
1035
		invoiceTable.addCell(getAmountInWordsCell(orderAmount));
1036
 
1037
		invoiceTable.addCell(getEOECell(5));
1038
 
1039
		return invoiceTable;
1040
	}
1041
 
1042
	private PdfPCell getInvoiceTableHeader(int colspan) {
1043
		PdfPCell invoiceTableHeader = new PdfPCell(new Phrase("Order Details:", helveticaBold12));
1044
		invoiceTableHeader.setBorder(Rectangle.NO_BORDER);
1045
		invoiceTableHeader.setColspan(colspan);
8551 manish.sha 1046
		invoiceTableHeader.setPaddingTop(1);
7014 rajveer 1047
		return invoiceTableHeader;
1048
	}
1049
 
1050
	private void populateBottomInvoiceTable(Order order, PdfPTable invoiceTable, double rate) {
1051
		for (LineItem lineitem : order.getLineitems()) {
1052
			invoiceTable.addCell(new Phrase("" + order.getId() , helvetica8));
1053
 
7190 amar.kumar 1054
			invoiceTable.addCell(getProductNameCell(lineitem, true, order.getFreebieItemId()));
7014 rajveer 1055
 
1056
			invoiceTable.addCell(new Phrase("" + lineitem.getQuantity(), helvetica8));
1057
 
1058
			double itemPrice = lineitem.getUnit_price();
1059
			double showPrice = (100 * itemPrice)/(100 + rate);
1060
			invoiceTable.addCell(getPriceCell(showPrice)); //Unit Price Cell
1061
 
1062
			double totalPrice = lineitem.getTotal_price();
1063
			showPrice = (100 * totalPrice)/(100 + rate);
1064
			invoiceTable.addCell(getPriceCell(showPrice));  //Total Price Cell
1065
		}
1066
	}
1067
 
7190 amar.kumar 1068
	private PdfPCell getProductNameCell(LineItem lineitem, boolean appendIMEI, Long freebieItemId) {
7014 rajveer 1069
		String itemName = getItemDisplayName(lineitem, appendIMEI);
7190 amar.kumar 1070
		if(freebieItemId!=null && freebieItemId!=0){
1071
			try {
1072
				CatalogService.Client catalogClient = ctsc.getClient();
1073
				Item item = catalogClient.getItem(freebieItemId);
1074
				itemName = itemName + "\n(Free Item: " + item.getBrand() + " " + item.getModelName() + " " + item.getModelNumber() + ")";
1075
			} catch(Exception tex) {
1076
				logger.error("Not able to get Freebie Item Details for ItemId:" + freebieItemId, tex);
1077
			}
1078
		}
7014 rajveer 1079
		PdfPCell productNameCell = new PdfPCell(new Phrase(itemName, helvetica8));
1080
		productNameCell.setHorizontalAlignment(Element.ALIGN_LEFT);
1081
		return productNameCell;
1082
	}
1083
 
1084
	private PdfPCell getPriceCell(double price) {
1085
		PdfPCell totalPriceCell = new PdfPCell(new Phrase(amountFormat.format(price), helvetica8));
1086
		totalPriceCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
1087
		return totalPriceCell;
1088
	}
1089
 
1090
	private PdfPCell getVATLabelCell(boolean isVAT) {
1091
		PdfPCell vatCell = null;
1092
		if(isVAT){
1093
			vatCell = new PdfPCell(new Phrase("VAT", helveticaBold8));
1094
		} else {
1095
			vatCell = new PdfPCell(new Phrase("CST", helveticaBold8));
1096
		}
1097
		vatCell.setColspan(3);
1098
		vatCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
1099
		return vatCell;
1100
	}
1101
 
9432 amar.kumar 1102
	private PdfPCell getCFORMLabelCell() {
1103
		PdfPCell cFormCell = null;
1104
		cFormCell = new PdfPCell(new Phrase("CST Against CForm", helveticaBold8));
1105
		cFormCell.setColspan(3);
1106
		cFormCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
1107
		return cFormCell;
1108
	}
1109
 
7318 rajveer 1110
	private PdfPCell getAdvanceAmountCell(int colspan) {
1111
		PdfPCell insuranceCell = null;
1112
		insuranceCell = new PdfPCell(new Phrase("Advance Amount Received", helvetica8));
1113
		insuranceCell.setColspan(colspan);
1114
		insuranceCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
1115
		return insuranceCell;
1116
	}
1117
 
7014 rajveer 1118
	private PdfPCell getInsuranceCell(int colspan) {
1119
		PdfPCell insuranceCell = null;
1120
		insuranceCell = new PdfPCell(new Phrase("1 Year WorldWide Theft Insurance", helvetica8));
1121
		insuranceCell.setColspan(colspan);
1122
		insuranceCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
1123
		return insuranceCell;
1124
	}
1125
 
1126
	private PdfPCell getEmptyCell(int colspan) {
1127
		PdfPCell emptyCell = new PdfPCell(new Phrase(" ", helvetica8));
1128
		emptyCell.setColspan(colspan);
1129
		return emptyCell;
1130
	}
1131
 
1132
	private PdfPCell getTotalCell(int colspan) {
1133
		PdfPCell totalCell = new PdfPCell(new Phrase("Total", helveticaBold8));
1134
		totalCell.setColspan(colspan);
1135
		totalCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
1136
		return totalCell;
1137
	}
1138
 
1139
	private PdfPCell getRupeesCell() {
1140
		PdfPCell rupeesCell = new PdfPCell(new Phrase("Rs.", helveticaBold8));
1141
		rupeesCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
1142
		return rupeesCell;
1143
	}
1144
 
1145
	private PdfPCell getTotalAmountCell(double orderAmount) {
1146
		PdfPCell totalAmountCell = new PdfPCell(new Phrase(amountFormat.format(orderAmount), helveticaBold8));
1147
		totalAmountCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
1148
		return totalAmountCell;
1149
	}
1150
 
1151
	/**
1152
	 * This method uses ICU4J libraries to convert the given amount into words
1153
	 * of Indian locale.
1154
	 * 
1155
	 * @param orderAmount
1156
	 *            The amount to convert.
1157
	 * @return the string representation of the given amount.
1158
	 */
1159
	private PdfPCell getAmountInWordsCell(double orderAmount) {
1160
		RuleBasedNumberFormat amountInWordsFormat = new RuleBasedNumberFormat(indianLocale, RuleBasedNumberFormat.SPELLOUT);
1161
		StringBuilder amountInWords = new StringBuilder("Rs. ");
1162
		amountInWords.append(WordUtils.capitalize(amountInWordsFormat.format((int)orderAmount)));
1163
		amountInWords.append(" and ");
1164
		amountInWords.append(WordUtils.capitalize(amountInWordsFormat.format((int)(orderAmount*100)%100)));
1165
		amountInWords.append(" paise");
1166
 
1167
		PdfPCell amountInWordsCell= new PdfPCell(new Phrase(amountInWords.toString(), helveticaBold8));
1168
		amountInWordsCell.setColspan(4);
1169
		return amountInWordsCell;
1170
	}
1171
 
1172
	/**
1173
	 * Returns the item name to be displayed in the invoice table.
1174
	 * 
1175
	 * @param lineitem
1176
	 *            The line item whose name has to be displayed
1177
	 * @param appendIMEI
1178
	 *            Whether to attach the IMEI No. to the item name
1179
	 * @return The name to be displayed for the given line item.
1180
	 */
1181
	private String getItemDisplayName(LineItem lineitem, boolean appendIMEI){
1182
		StringBuffer itemName = new StringBuffer();
1183
		if(lineitem.getBrand()!= null)
1184
			itemName.append(lineitem.getBrand() + " ");
1185
		if(lineitem.getModel_name() != null)
1186
			itemName.append(lineitem.getModel_name() + " ");
1187
		if(lineitem.getModel_number() != null )
1188
			itemName.append(lineitem.getModel_number() + " ");
1189
		if(lineitem.getColor() != null && !lineitem.getColor().trim().equals("NA"))
1190
			itemName.append("("+lineitem.getColor()+")");
1191
		if(appendIMEI && lineitem.isSetSerial_number()){
1192
			itemName.append("\nIMEI No. " + lineitem.getSerial_number());
1193
		}
1194
 
1195
		return itemName.toString();
1196
	}
1197
 
1198
	/**
1199
	 * 
1200
	 * @param colspan
1201
	 * @return a PdfPCell containing the E&amp;OE text and spanning the given
1202
	 *         no. of columns
1203
	 */
1204
	private PdfPCell getEOECell(int colspan) {
1205
		PdfPCell eoeCell = new PdfPCell(new Phrase("E & O.E", helvetica8));
1206
		eoeCell.setColspan(colspan);
1207
		eoeCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
1208
		return eoeCell;
1209
	}
1210
 
1211
	private PdfPTable getExtraInfoTable(Order order, Provider provider, float barcodeFontSize, BillingType billingType){
1212
		PdfPTable extraInfoTable = new PdfPTable(1);
1213
		extraInfoTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
1214
		extraInfoTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
1215
 
1216
		String fontPath = InvoiceGenerationService.class.getResource("/saholic-wn.TTF").getPath();
1217
		FontFactoryImp ttfFontFactory = new FontFactoryImp();
1218
		ttfFontFactory.register(fontPath, "barcode");
1219
		Font barCodeFont = ttfFontFactory.getFont("barcode", BaseFont.CP1252, true, barcodeFontSize);
1220
 
1221
		PdfPCell extraInfoCell;
1222
		if(billingType == BillingType.EXTERNAL){
1223
			extraInfoCell = new PdfPCell(new Paragraph( "*" + order.getId() + "*        *" + order.getCustomer_name() + "*        *"  + order.getTotal_amount() + "*", barCodeFont));
1224
		}else{
1225
			extraInfoCell = new PdfPCell(new Paragraph( "*" + order.getId() + "*        *" + order.getLineitems().get(0).getTransfer_price() + "*", barCodeFont));	
1226
		}
1227
 
1228
		extraInfoCell.setPaddingTop(20.0f);
1229
		extraInfoCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
1230
		extraInfoCell.setBorder(Rectangle.NO_BORDER);
1231
 
1232
		extraInfoTable.addCell(extraInfoCell);
1233
 
1234
 
1235
		return extraInfoTable;
1236
	}
1237
 
1238
	private PdfPTable getFixedTextTable(float barcodeFontSize, String printText){
1239
		PdfPTable extraInfoTable = new PdfPTable(1);
1240
		extraInfoTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
1241
		extraInfoTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
1242
 
1243
		String fontPath = InvoiceGenerationService.class.getResource("/saholic-wn.TTF").getPath();
1244
		FontFactoryImp ttfFontFactory = new FontFactoryImp();
1245
		ttfFontFactory.register(fontPath, "barcode");
1246
		Font barCodeFont = ttfFontFactory.getFont("barcode", BaseFont.CP1252, true, barcodeFontSize);
1247
 
1248
		PdfPCell extraInfoCell = new PdfPCell(new Paragraph( "*" + printText + "*", barCodeFont));
1249
 
1250
		extraInfoCell.setPaddingTop(20.0f);
1251
		extraInfoCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
1252
		extraInfoCell.setBorder(Rectangle.NO_BORDER);
1253
 
1254
		extraInfoTable.addCell(extraInfoCell);
1255
 
1256
		return extraInfoTable;
1257
	}
8067 manish.sha 1258
 
1259
	private void generateBarcode(String barcodeString, String fileName){
1260
		Code128Bean bean = new Code128Bean();
7014 rajveer 1261
 
8067 manish.sha 1262
		final int dpi = 60;
1263
 
1264
		//Configure the barcode generator
1265
		bean.setModuleWidth(UnitConv.in2mm(1.0f / dpi)); //makes the narrow bar 
1266
		                                                 //width exactly one pixel
1267
		bean.setFontSize(bean.getFontSize()+1.0f);
1268
		bean.doQuietZone(false);
1269
 
1270
		try {
1271
			File outputFile = new File("/tmp/"+fileName+".png");
1272
			OutputStream out = new FileOutputStream(outputFile);
1273
 
1274
		    //Set up the canvas provider for monochrome PNG output 
1275
		    BitmapCanvasProvider canvas = new BitmapCanvasProvider(
1276
		            out, "image/x-png", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);
1277
 
1278
		    //Generate the barcode
1279
		    bean.generateBarcode(canvas, barcodeString);
1280
 
1281
		    //Signal end of generation
1282
		    canvas.finish();
1283
		    out.close();
1284
 
1285
		} 
1286
		catch(Exception e){
1287
			logger.error("Exception during generating Barcode : ", e);
1288
		}
1289
	}
1290
 
7014 rajveer 1291
	public static void main(String[] args) throws IOException {
1292
		InvoiceGenerationService invoiceGenerationService = new InvoiceGenerationService();
7318 rajveer 1293
		long orderId = 356324;
1294
		ByteArrayOutputStream baos = invoiceGenerationService.generateInvoice(orderId, true, false, 1);
7014 rajveer 1295
		String userHome = System.getProperty("user.home");
1296
		File f = new File(userHome + "/invoice-" + orderId + ".pdf");
1297
		FileOutputStream fos = new FileOutputStream(f);
1298
		baos.writeTo(fos);
1299
		System.out.println("Invoice generated.");
1300
	}
2787 chandransh 1301
}