Subversion Repositories SmartDukaan

Rev

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

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