Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
676 chandransh 1
package in.shop2020.support.services;
2
 
5945 mandeep.dh 3
import in.shop2020.logistics.LogisticsServiceException;
4
import in.shop2020.logistics.PickUpType;
5
import in.shop2020.logistics.PickupStore;
6
import in.shop2020.logistics.Provider;
7
import in.shop2020.model.v1.inventory.InventoryServiceException;
8
import in.shop2020.model.v1.inventory.Warehouse;
8256 amar.kumar 9
import in.shop2020.model.v1.order.EbayOrder;
5945 mandeep.dh 10
import in.shop2020.model.v1.order.LineItem;
11
import in.shop2020.model.v1.order.Order;
12
import in.shop2020.model.v1.order.OrderStatus;
13
import in.shop2020.model.v1.order.TransactionServiceException;
14
import in.shop2020.thrift.clients.InventoryClient;
15
import in.shop2020.thrift.clients.LogisticsClient;
16
import in.shop2020.thrift.clients.TransactionClient;
17
 
676 chandransh 18
import java.io.ByteArrayOutputStream;
744 chandransh 19
import java.io.File;
20
import java.io.FileOutputStream;
676 chandransh 21
import java.io.IOException;
22
import java.net.MalformedURLException;
23
import java.text.DateFormat;
744 chandransh 24
import java.text.DecimalFormat;
4410 rajveer 25
import java.util.ArrayList;
676 chandransh 26
import java.util.Date;
13276 manish.sha 27
import java.util.HashMap;
676 chandransh 28
import java.util.List;
13276 manish.sha 29
import java.util.Map;
676 chandransh 30
 
31
import org.apache.thrift.TException;
3062 chandransh 32
import org.slf4j.Logger;
33
import org.slf4j.LoggerFactory;
676 chandransh 34
 
35
import com.itextpdf.text.Document;
36
import com.itextpdf.text.DocumentException;
37
import com.itextpdf.text.Element;
38
import com.itextpdf.text.Font;
5945 mandeep.dh 39
import com.itextpdf.text.Font.FontFamily;
676 chandransh 40
import com.itextpdf.text.FontFactory;
41
import com.itextpdf.text.Image;
42
import com.itextpdf.text.Paragraph;
43
import com.itextpdf.text.Phrase;
44
import com.itextpdf.text.Rectangle;
45
import com.itextpdf.text.pdf.PdfPCell;
46
import com.itextpdf.text.pdf.PdfPTable;
47
import com.itextpdf.text.pdf.PdfWriter;
48
 
49
public class ManifestGenerator {
3062 chandransh 50
 
51
    private static Logger logger = LoggerFactory.getLogger(ManifestGenerator.class);
52
 
3125 rajveer 53
	private TransactionClient tsc = null;
5945 mandeep.dh 54
	private InventoryClient csc = null;
3125 rajveer 55
	private LogisticsClient lsc = null;
744 chandransh 56
	private DecimalFormat weightFormat = new DecimalFormat("0.000");
676 chandransh 57
	public ManifestGenerator() {
58
		try {
3125 rajveer 59
			tsc = new TransactionClient();
5945 mandeep.dh 60
			csc = new InventoryClient();
3125 rajveer 61
			lsc = new LogisticsClient();
676 chandransh 62
		} catch (Exception e) {
3062 chandransh 63
		    logger.error("Error while initializing one of the thrift clients", e);
676 chandransh 64
		}
65
	}
66
 
5743 rajveer 67
	public ByteArrayOutputStream generateManifestFile(long warehouseId,	long providerId, boolean isCod, List<Long> orderIds, String runner) {
676 chandransh 68
		ByteArrayOutputStream baosPDF = null;
69
		in.shop2020.model.v1.order.TransactionService.Client txnClient = tsc.getClient();
5945 mandeep.dh 70
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = csc.getClient();
676 chandransh 71
		in.shop2020.logistics.LogisticsService.Client logisticsClient = lsc.getClient();
72
 
4410 rajveer 73
		List<OrderStatus> statuses = new ArrayList<OrderStatus>();
74
		statuses.add(OrderStatus.BILLED);
75
 
676 chandransh 76
		List<Order> orders = null;
77
		Warehouse warehouse = null;
78
		Provider provider = null;
5766 rajveer 79
		List<PickupStore> stores = null;
676 chandransh 80
		try {
8304 amar.kumar 81
			orders = txnClient.getOrdersInBatch(statuses, 0, 0, warehouseId, 0);
676 chandransh 82
			warehouse = inventoryClient.getWarehouse(warehouseId);
83
			provider = logisticsClient.getProvider(providerId);
5766 rajveer 84
			stores = logisticsClient.getAllPickupStores();
3062 chandransh 85
		} catch (TException e) {
86
		    logger.error("Error getting information from one of the Thrift Services: ", e);
676 chandransh 87
			return baosPDF;
88
		} catch (InventoryServiceException e) {
3062 chandransh 89
		    logger.error("Error getting warehouse info from the catalog service: ", e);
676 chandransh 90
			return baosPDF;
91
		} catch (LogisticsServiceException e) {
3062 chandransh 92
		    logger.error("Error getting provider info from the logistics service: ", e);
676 chandransh 93
			return baosPDF;
94
		} catch (TransactionServiceException e) {
3062 chandransh 95
		    logger.error("Error getting orders from the transaction service: ", e);
676 chandransh 96
			return baosPDF;
97
		}
98
 
99
		try {
100
			baosPDF = new ByteArrayOutputStream();
101
			Font helvetica8 = FontFactory.getFont(FontFactory.HELVETICA, 8);
102
			Document document = new Document();
103
			PdfWriter.getInstance(document, baosPDF);
104
			document.addAuthor("shop2020");
105
			document.addTitle("Manifest for warehouse " + warehouseId + " provider " + providerId);
106
			document.open();
107
			PdfPTable table = new PdfPTable(1);
108
			table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
109
			table.getDefaultCell().setPaddingBottom(10.0f);
110
 
744 chandransh 111
			String logoPath = ManifestGenerator.class.getResource("/logo.jpg").getPath();
676 chandransh 112
			PdfPCell logoCell = new PdfPCell(Image.getInstance(logoPath), false);
113
			logoCell.setBorder(Rectangle.NO_BORDER);
114
 
115
			String addressString =  warehouse.getLocation() + "\nPIN " + warehouse.getPincode()+ "\n\n";
116
			Paragraph addressParagraph = new Paragraph(addressString, new Font(FontFamily.TIMES_ROMAN,8f));
117
			PdfPCell addressCell = new PdfPCell();
118
			addressCell.addElement(addressParagraph);
119
			addressCell.setHorizontalAlignment(Element.ALIGN_LEFT);
120
			addressCell.setBorder(Rectangle.NO_BORDER);
121
 
8276 amar.kumar 122
			PdfPTable ordersTable = null;
123
			ordersTable = new PdfPTable(8);
124
			if (providerId > 7 && providerId <12) {
125
				ordersTable = new PdfPTable(10);
126
			}
13276 manish.sha 127
 
676 chandransh 128
			ordersTable.addCell(new Phrase("Sl No", helvetica8));
13276 manish.sha 129
			ordersTable.addCell(new Phrase("Master Order Id", helvetica8));
130
 
676 chandransh 131
			ordersTable.addCell(new Phrase("AWB No", helvetica8));
132
			ordersTable.addCell(new Phrase("Packet Wt.", helvetica8));
8256 amar.kumar 133
 
134
			//if Logistics is Ebay Power Ship add extra fields like Sales Record Number and PaisaPayId
135
			if(providerId > 7 && providerId <12) {
136
				ordersTable.addCell(new Phrase("PaisaPayId", helvetica8));
137
				ordersTable.addCell(new Phrase("Sales Rec No.", helvetica8));
138
			}
139
 
5766 rajveer 140
			if(runner == null){
141
				ordersTable.addCell(new Phrase("Name", helvetica8));
142
				ordersTable.addCell(new Phrase("Destination City", helvetica8));
143
				ordersTable.addCell(new Phrase("Pincode", helvetica8));
144
				ordersTable.addCell(new Phrase("State", helvetica8));
145
			}else{
146
				ordersTable.addCell(new Phrase("Store Code", helvetica8));
147
				ordersTable.addCell(new Phrase("Store Address", helvetica8));
148
				ordersTable.addCell(new Phrase("Store City", helvetica8));
149
				ordersTable.addCell(new Phrase("Phone", helvetica8));
150
			}
676 chandransh 151
 
13276 manish.sha 152
			Map<String, Double> totalWeightMap = new HashMap<String, Double>();
153
			Map<String, String> airwayBillNoMap = new HashMap<String, String>();
154
			Map<String, Order> orderDetailsMap = new HashMap<String, Order>();
155
 
156
			List<Order> selectedOrders = new ArrayList<Order>();
157
 
676 chandransh 158
			for(int i=0; i < orders.size();i++){
159
				Order order = orders.get(i);
4788 rajveer 160
				if(!orderIds.contains(order.getId()))
161
					continue;
676 chandransh 162
				if(order.getLogistics_provider_id()!=providerId)
163
					continue;
5554 rajveer 164
				if(order.isLogisticsCod() != isCod)
3062 chandransh 165
	                continue;
13276 manish.sha 166
				selectedOrders.add(order);
167
			}
168
 
169
			for(Order order: selectedOrders){
170
				if(orderDetailsMap.containsKey(order.getLogisticsTransactionId())){
171
					continue;
172
				} else {
13448 manish.sha 173
					if(order.getLogisticsTransactionId()!=null){
174
						orderDetailsMap.put(order.getLogisticsTransactionId(), order);
175
					}else {
176
						orderDetailsMap.put(order.getId()+"", order);
177
					}
13276 manish.sha 178
				}
179
			}
180
 
181
			for(Order order: selectedOrders){
13446 manish.sha 182
				if(order.getLogisticsTransactionId()!=null){
183
					if(totalWeightMap.containsKey(order.getLogisticsTransactionId())){
184
						double totalWeight = totalWeightMap.get(order.getLogisticsTransactionId())+ order.getTotal_weight();
185
						totalWeightMap.put(order.getLogisticsTransactionId(), totalWeight);
186
					}else{
187
						double totalWeight = order.getTotal_weight();
188
						totalWeightMap.put(order.getLogisticsTransactionId(), totalWeight);
189
					}
190
					if(airwayBillNoMap.containsKey(order.getLogisticsTransactionId())){
191
						continue;
192
					}else{
193
						airwayBillNoMap.put(order.getLogisticsTransactionId(), order.getAirwaybill_no());
194
					}
13276 manish.sha 195
				}else{
13446 manish.sha 196
					airwayBillNoMap.put(order.getId()+"", order.getAirwaybill_no());
13447 manish.sha 197
					totalWeightMap.put(order.getId()+"", order.getTotal_weight());
13276 manish.sha 198
				}
199
			}
200
 
201
			int serialNo = 0;
202
 
203
			for(String logisticsTxnId : airwayBillNoMap.keySet()){
204
				Order singleOrder = orderDetailsMap.get(logisticsTxnId);
205
				serialNo++;
206
				ordersTable.addCell(new Phrase(serialNo + "", helvetica8));
207
				ordersTable.addCell(new Phrase(logisticsTxnId, helvetica8));
208
				ordersTable.addCell(new Phrase(airwayBillNoMap.get(logisticsTxnId), helvetica8));
209
				ordersTable.addCell(new Phrase(weightFormat.format(totalWeightMap.get(logisticsTxnId)), helvetica8));
210
 
211
				if(providerId > 7 && providerId <12) {
212
					try {
213
						EbayOrder ebayOrder = txnClient.getEbayOrderByOrderId(singleOrder.getId());
214
						ordersTable.addCell(new Phrase(ebayOrder.getPaisaPayId(), helvetica8));
215
						ordersTable.addCell(new Phrase(new Long(ebayOrder.getSalesRecordNumber()).toString(), helvetica8));
216
					} catch (Exception e) {
217
						logger.error("Error getting Ebay Order for OrderId : " + singleOrder.getId(), e);
218
						return baosPDF;
219
					}
220
				}
221
				if(runner == null){
222
					ordersTable.addCell(new Phrase(singleOrder.getCustomer_name(), helvetica8));
223
					ordersTable.addCell(new Phrase(singleOrder.getCustomer_city(), helvetica8));
224
					ordersTable.addCell(new Phrase(singleOrder.getCustomer_pincode(), helvetica8));
225
					ordersTable.addCell(new Phrase(singleOrder.getCustomer_state(), helvetica8));
226
				}else{
227
					PickupStore store = getStoreFromId(stores, singleOrder.getPickupStoreId());
228
					ordersTable.addCell(new Phrase(store.getHotspotId(), helvetica8));
229
					ordersTable.addCell(new Phrase(store.getLine1() + ((store.getLine2() == null)? "": "\n" + store.getLine2()), helvetica8));
230
					ordersTable.addCell(new Phrase(store.getCity(), helvetica8));
231
					ordersTable.addCell(new Phrase(store.getPhone(), helvetica8));
232
				}
233
 
234
			}
235
			/*for(int i=0; i < orders.size();i++){
236
				Order order = orders.get(i);
237
				if(!orderIds.contains(order.getId()))
238
					continue;
239
				if(order.getLogistics_provider_id()!=providerId)
240
					continue;
241
				if(order.isLogisticsCod() != isCod)
242
	                continue;
676 chandransh 243
				//TODO: These are exactly the orders which will be shipped now. Shouldn't we change their status to be SHIPPED?
244
				serialNo++;
245
				List<LineItem> lineItems = order.getLineitems();
246
				double weight = 0;
247
				for(LineItem lineItem: lineItems)
248
					weight += lineItem.getTotal_weight();
249
 
250
				ordersTable.addCell(new Phrase(serialNo + "", helvetica8));
13146 manish.sha 251
				if(provider.isGroupShipmentAllowed()){
252
					ordersTable.addCell(new Phrase(order.getLogisticsTransactionId(), helvetica8));
253
				}
676 chandransh 254
				ordersTable.addCell(new Phrase(order.getId() + "", helvetica8));
255
				ordersTable.addCell(new Phrase(order.getAirwaybill_no(), helvetica8));
919 rajveer 256
				ordersTable.addCell(new Phrase(weightFormat.format(weight), helvetica8));
8256 amar.kumar 257
 
258
				//if Logistics is Ebay Power Ship add extra fields like Sales Record Number and PaisaPayId
259
				if(providerId > 7 && providerId <12) {
260
					try {
261
						EbayOrder ebayOrder = txnClient.getEbayOrderByOrderId(order.getId());
262
						ordersTable.addCell(new Phrase(ebayOrder.getPaisaPayId(), helvetica8));
263
						ordersTable.addCell(new Phrase(new Long(ebayOrder.getSalesRecordNumber()).toString(), helvetica8));
264
					} catch (Exception e) {
265
						logger.error("Error getting Ebay Order for OrderId : " + order.getId(), e);
266
						return baosPDF;
267
					}
268
				}
269
 
5766 rajveer 270
				if(runner == null){
271
					ordersTable.addCell(new Phrase(order.getCustomer_name(), helvetica8));
272
					ordersTable.addCell(new Phrase(order.getCustomer_city(), helvetica8));
273
					ordersTable.addCell(new Phrase(order.getCustomer_pincode(), helvetica8));
274
					ordersTable.addCell(new Phrase(order.getCustomer_state(), helvetica8));
275
				}else{
276
					PickupStore store = getStoreFromId(stores, order.getPickupStoreId());
277
					ordersTable.addCell(new Phrase(store.getHotspotId(), helvetica8));
278
					ordersTable.addCell(new Phrase(store.getLine1() + ((store.getLine2() == null)? "": "\n" + store.getLine2()), helvetica8));
279
					ordersTable.addCell(new Phrase(store.getCity(), helvetica8));
280
					ordersTable.addCell(new Phrase(store.getPhone(), helvetica8));
281
				}
13276 manish.sha 282
			}*/
676 chandransh 283
 
284
			table.addCell(logoCell);
285
			table.addCell(addressCell);
3062 chandransh 286
			if(isCod)
287
			    table.addCell(new Phrase("PAYMODE: COD", helvetica8));
288
			else
289
			    table.addCell(new Phrase("PAYMODE: Prepaid", helvetica8));
5743 rajveer 290
			if(provider.getPickup() == PickUpType.RUNNER){
291
				table.addCell(new Phrase("Runner Name: " + runner, helvetica8));
292
			}else{
293
				table.addCell(new Phrase("Courier Name: " + provider.getName(), helvetica8));
294
			}
295
 
676 chandransh 296
			table.addCell(new Phrase("Pick up Date: " + DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date()), helvetica8));
297
			table.addCell(ordersTable);
298
			table.setWidthPercentage(90.0f);
299
 
300
			document.add(table);  
301
			document.close();
302
			baosPDF.close();
303
		} catch (DocumentException e) {
3062 chandransh 304
		    logger.error("Error while creating the manifest file", e);
676 chandransh 305
		} catch (MalformedURLException e) {
3062 chandransh 306
		    logger.error("Error while creating the manifest file", e);
676 chandransh 307
		} catch (IOException e) {
3062 chandransh 308
			logger.error("Error while creating the manifest file", e);
676 chandransh 309
		}
310
 
311
		return baosPDF;
312
	}
5678 rajveer 313
 
5766 rajveer 314
	private static PickupStore getStoreFromId(List<PickupStore> stores, long storeId){
315
		for(PickupStore store: stores){
316
			if(store.getId() == storeId)
317
				return store;
318
		}
319
		return null;
320
	}
744 chandransh 321
 
5766 rajveer 322
 
5714 rajveer 323
	public ByteArrayOutputStream generateManifestFile(long providerId, long storeId, List<Long> orderIds, List<String> awbs) {
5678 rajveer 324
		ByteArrayOutputStream baosPDF = null;
325
		in.shop2020.model.v1.order.TransactionService.Client txnClient = tsc.getClient();
326
		in.shop2020.logistics.LogisticsService.Client logisticsClient = lsc.getClient();
327
 
328
		List<OrderStatus> statuses = new ArrayList<OrderStatus>();
329
		statuses.add(OrderStatus.RET_PICKUP_REQUEST_RAISED);
330
 
331
		List<Order> orders = null;
332
		PickupStore store = null;
333
		Provider provider = null;
334
		try {
8304 amar.kumar 335
			orders = txnClient.getOrdersInBatch(statuses, 0, 0, 0, 0);
5678 rajveer 336
			store = logisticsClient.getPickupStore(storeId);
337
			provider = logisticsClient.getProvider(providerId);
338
		} catch (TException e) {
339
		    logger.error("Error getting information from one of the Thrift Services: ", e);
340
			return baosPDF;
341
		} catch (LogisticsServiceException e) {
342
		    logger.error("Error getting provider info from the logistics service: ", e);
343
			return baosPDF;
344
		} catch (TransactionServiceException e) {
345
		    logger.error("Error getting orders from the transaction service: ", e);
346
			return baosPDF;
347
		}
348
 
349
		try {
350
			baosPDF = new ByteArrayOutputStream();
351
			Font helvetica8 = FontFactory.getFont(FontFactory.HELVETICA, 8);
352
			Document document = new Document();
353
			PdfWriter.getInstance(document, baosPDF);
354
			document.addAuthor("shop2020");
355
			document.addTitle("Manifest for store " + storeId + " provider " + providerId);
356
			document.open();
357
			PdfPTable table = new PdfPTable(1);
358
			table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
359
			table.getDefaultCell().setPaddingBottom(10.0f);
360
 
5766 rajveer 361
			String addressString =  store.getName() +  "\n" + store.getLine1() + ((store.getLine2() == null)? "": "\n" + store.getLine2()) 
362
									+ "\n" + store.getCity()  + "\n" + store.getState() + "\nPIN " + store.getPin()+ "\n\n";
5678 rajveer 363
			Paragraph addressParagraph = new Paragraph(addressString, new Font(FontFamily.TIMES_ROMAN,8f));
364
			PdfPCell addressCell = new PdfPCell();
365
			addressCell.addElement(addressParagraph);
366
			addressCell.setHorizontalAlignment(Element.ALIGN_LEFT);
367
			addressCell.setBorder(Rectangle.NO_BORDER);
368
 
369
			PdfPTable ordersTable = new PdfPTable(4);
370
			ordersTable.addCell(new Phrase("Sl No", helvetica8));
371
			ordersTable.addCell(new Phrase("Order No", helvetica8));
372
			ordersTable.addCell(new Phrase("AWB No", helvetica8));
373
			ordersTable.addCell(new Phrase("Packet Wt.", helvetica8));
374
 
375
			int serialNo = 0;
376
			for(int i=0; i < orders.size();i++){
377
				Order order = orders.get(i);
378
				if(!orderIds.contains(order.getId()))
379
					continue;
380
				if(order.getLogistics_provider_id()!=providerId)
381
					continue;
382
				if(order.getPickupStoreId()!=storeId)
383
					continue;
384
				//TODO: These are exactly the orders which will be shipped now. Shouldn't we change their status to be SHIPPED?
385
				serialNo++;
386
				List<LineItem> lineItems = order.getLineitems();
387
				double weight = 0;
388
				for(LineItem lineItem: lineItems)
389
					weight += lineItem.getTotal_weight();
390
 
391
				ordersTable.addCell(new Phrase(serialNo + "", helvetica8));
392
				ordersTable.addCell(new Phrase(order.getId() + "", helvetica8));
5714 rajveer 393
				ordersTable.addCell(new Phrase(awbs.get(orderIds.indexOf(order.getId())), helvetica8));
5678 rajveer 394
				ordersTable.addCell(new Phrase(weightFormat.format(weight), helvetica8));
395
			}
396
 
397
			table.addCell(addressCell);
398
			table.addCell(new Phrase("Courier Name: " + provider.getName(), helvetica8));
399
			table.addCell(new Phrase("Pick up Date: " + DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date()), helvetica8));
400
			table.addCell(ordersTable);
401
			table.setWidthPercentage(90.0f);
402
 
403
			document.add(table);  
404
			document.close();
405
			baosPDF.close();
406
		} catch (DocumentException e) {
407
		    logger.error("Error while creating the manifest file", e);
408
		} catch (MalformedURLException e) {
409
		    logger.error("Error while creating the manifest file", e);
410
		} catch (IOException e) {
411
			logger.error("Error while creating the manifest file", e);
412
		}
413
 
414
		return baosPDF;
415
	}
416
 
744 chandransh 417
	public static void main(String[] args) throws IOException {
418
		ManifestGenerator manifestGenerator = new ManifestGenerator();
5743 rajveer 419
		ByteArrayOutputStream baos = manifestGenerator.generateManifestFile(1, 1, true, null, null);
744 chandransh 420
		File f = new File("/home/ashish/Downloads/manifest-1-2.pdf");
421
		FileOutputStream fos = new FileOutputStream(f);
422
		baos.writeTo(fos);
423
	}
676 chandransh 424
}