Subversion Repositories SmartDukaan

Rev

Rev 13446 | Rev 13448 | 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 {
173
					orderDetailsMap.put(order.getLogisticsTransactionId(), order);
174
				}
175
			}
176
 
177
			for(Order order: selectedOrders){
13446 manish.sha 178
				if(order.getLogisticsTransactionId()!=null){
179
					if(totalWeightMap.containsKey(order.getLogisticsTransactionId())){
180
						double totalWeight = totalWeightMap.get(order.getLogisticsTransactionId())+ order.getTotal_weight();
181
						totalWeightMap.put(order.getLogisticsTransactionId(), totalWeight);
182
					}else{
183
						double totalWeight = order.getTotal_weight();
184
						totalWeightMap.put(order.getLogisticsTransactionId(), totalWeight);
185
					}
186
					if(airwayBillNoMap.containsKey(order.getLogisticsTransactionId())){
187
						continue;
188
					}else{
189
						airwayBillNoMap.put(order.getLogisticsTransactionId(), order.getAirwaybill_no());
190
					}
13276 manish.sha 191
				}else{
13446 manish.sha 192
					airwayBillNoMap.put(order.getId()+"", order.getAirwaybill_no());
13447 manish.sha 193
					totalWeightMap.put(order.getId()+"", order.getTotal_weight());
13276 manish.sha 194
				}
195
			}
196
 
197
			int serialNo = 0;
198
 
199
			for(String logisticsTxnId : airwayBillNoMap.keySet()){
200
				Order singleOrder = orderDetailsMap.get(logisticsTxnId);
201
				serialNo++;
202
				ordersTable.addCell(new Phrase(serialNo + "", helvetica8));
203
				ordersTable.addCell(new Phrase(logisticsTxnId, helvetica8));
204
				ordersTable.addCell(new Phrase(airwayBillNoMap.get(logisticsTxnId), helvetica8));
205
				ordersTable.addCell(new Phrase(weightFormat.format(totalWeightMap.get(logisticsTxnId)), helvetica8));
206
 
207
				if(providerId > 7 && providerId <12) {
208
					try {
209
						EbayOrder ebayOrder = txnClient.getEbayOrderByOrderId(singleOrder.getId());
210
						ordersTable.addCell(new Phrase(ebayOrder.getPaisaPayId(), helvetica8));
211
						ordersTable.addCell(new Phrase(new Long(ebayOrder.getSalesRecordNumber()).toString(), helvetica8));
212
					} catch (Exception e) {
213
						logger.error("Error getting Ebay Order for OrderId : " + singleOrder.getId(), e);
214
						return baosPDF;
215
					}
216
				}
217
				if(runner == null){
218
					ordersTable.addCell(new Phrase(singleOrder.getCustomer_name(), helvetica8));
219
					ordersTable.addCell(new Phrase(singleOrder.getCustomer_city(), helvetica8));
220
					ordersTable.addCell(new Phrase(singleOrder.getCustomer_pincode(), helvetica8));
221
					ordersTable.addCell(new Phrase(singleOrder.getCustomer_state(), helvetica8));
222
				}else{
223
					PickupStore store = getStoreFromId(stores, singleOrder.getPickupStoreId());
224
					ordersTable.addCell(new Phrase(store.getHotspotId(), helvetica8));
225
					ordersTable.addCell(new Phrase(store.getLine1() + ((store.getLine2() == null)? "": "\n" + store.getLine2()), helvetica8));
226
					ordersTable.addCell(new Phrase(store.getCity(), helvetica8));
227
					ordersTable.addCell(new Phrase(store.getPhone(), helvetica8));
228
				}
229
 
230
			}
231
			/*for(int i=0; i < orders.size();i++){
232
				Order order = orders.get(i);
233
				if(!orderIds.contains(order.getId()))
234
					continue;
235
				if(order.getLogistics_provider_id()!=providerId)
236
					continue;
237
				if(order.isLogisticsCod() != isCod)
238
	                continue;
676 chandransh 239
				//TODO: These are exactly the orders which will be shipped now. Shouldn't we change their status to be SHIPPED?
240
				serialNo++;
241
				List<LineItem> lineItems = order.getLineitems();
242
				double weight = 0;
243
				for(LineItem lineItem: lineItems)
244
					weight += lineItem.getTotal_weight();
245
 
246
				ordersTable.addCell(new Phrase(serialNo + "", helvetica8));
13146 manish.sha 247
				if(provider.isGroupShipmentAllowed()){
248
					ordersTable.addCell(new Phrase(order.getLogisticsTransactionId(), helvetica8));
249
				}
676 chandransh 250
				ordersTable.addCell(new Phrase(order.getId() + "", helvetica8));
251
				ordersTable.addCell(new Phrase(order.getAirwaybill_no(), helvetica8));
919 rajveer 252
				ordersTable.addCell(new Phrase(weightFormat.format(weight), helvetica8));
8256 amar.kumar 253
 
254
				//if Logistics is Ebay Power Ship add extra fields like Sales Record Number and PaisaPayId
255
				if(providerId > 7 && providerId <12) {
256
					try {
257
						EbayOrder ebayOrder = txnClient.getEbayOrderByOrderId(order.getId());
258
						ordersTable.addCell(new Phrase(ebayOrder.getPaisaPayId(), helvetica8));
259
						ordersTable.addCell(new Phrase(new Long(ebayOrder.getSalesRecordNumber()).toString(), helvetica8));
260
					} catch (Exception e) {
261
						logger.error("Error getting Ebay Order for OrderId : " + order.getId(), e);
262
						return baosPDF;
263
					}
264
				}
265
 
5766 rajveer 266
				if(runner == null){
267
					ordersTable.addCell(new Phrase(order.getCustomer_name(), helvetica8));
268
					ordersTable.addCell(new Phrase(order.getCustomer_city(), helvetica8));
269
					ordersTable.addCell(new Phrase(order.getCustomer_pincode(), helvetica8));
270
					ordersTable.addCell(new Phrase(order.getCustomer_state(), helvetica8));
271
				}else{
272
					PickupStore store = getStoreFromId(stores, order.getPickupStoreId());
273
					ordersTable.addCell(new Phrase(store.getHotspotId(), helvetica8));
274
					ordersTable.addCell(new Phrase(store.getLine1() + ((store.getLine2() == null)? "": "\n" + store.getLine2()), helvetica8));
275
					ordersTable.addCell(new Phrase(store.getCity(), helvetica8));
276
					ordersTable.addCell(new Phrase(store.getPhone(), helvetica8));
277
				}
13276 manish.sha 278
			}*/
676 chandransh 279
 
280
			table.addCell(logoCell);
281
			table.addCell(addressCell);
3062 chandransh 282
			if(isCod)
283
			    table.addCell(new Phrase("PAYMODE: COD", helvetica8));
284
			else
285
			    table.addCell(new Phrase("PAYMODE: Prepaid", helvetica8));
5743 rajveer 286
			if(provider.getPickup() == PickUpType.RUNNER){
287
				table.addCell(new Phrase("Runner Name: " + runner, helvetica8));
288
			}else{
289
				table.addCell(new Phrase("Courier Name: " + provider.getName(), helvetica8));
290
			}
291
 
676 chandransh 292
			table.addCell(new Phrase("Pick up Date: " + DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date()), helvetica8));
293
			table.addCell(ordersTable);
294
			table.setWidthPercentage(90.0f);
295
 
296
			document.add(table);  
297
			document.close();
298
			baosPDF.close();
299
		} catch (DocumentException e) {
3062 chandransh 300
		    logger.error("Error while creating the manifest file", e);
676 chandransh 301
		} catch (MalformedURLException e) {
3062 chandransh 302
		    logger.error("Error while creating the manifest file", e);
676 chandransh 303
		} catch (IOException e) {
3062 chandransh 304
			logger.error("Error while creating the manifest file", e);
676 chandransh 305
		}
306
 
307
		return baosPDF;
308
	}
5678 rajveer 309
 
5766 rajveer 310
	private static PickupStore getStoreFromId(List<PickupStore> stores, long storeId){
311
		for(PickupStore store: stores){
312
			if(store.getId() == storeId)
313
				return store;
314
		}
315
		return null;
316
	}
744 chandransh 317
 
5766 rajveer 318
 
5714 rajveer 319
	public ByteArrayOutputStream generateManifestFile(long providerId, long storeId, List<Long> orderIds, List<String> awbs) {
5678 rajveer 320
		ByteArrayOutputStream baosPDF = null;
321
		in.shop2020.model.v1.order.TransactionService.Client txnClient = tsc.getClient();
322
		in.shop2020.logistics.LogisticsService.Client logisticsClient = lsc.getClient();
323
 
324
		List<OrderStatus> statuses = new ArrayList<OrderStatus>();
325
		statuses.add(OrderStatus.RET_PICKUP_REQUEST_RAISED);
326
 
327
		List<Order> orders = null;
328
		PickupStore store = null;
329
		Provider provider = null;
330
		try {
8304 amar.kumar 331
			orders = txnClient.getOrdersInBatch(statuses, 0, 0, 0, 0);
5678 rajveer 332
			store = logisticsClient.getPickupStore(storeId);
333
			provider = logisticsClient.getProvider(providerId);
334
		} catch (TException e) {
335
		    logger.error("Error getting information from one of the Thrift Services: ", e);
336
			return baosPDF;
337
		} catch (LogisticsServiceException e) {
338
		    logger.error("Error getting provider info from the logistics service: ", e);
339
			return baosPDF;
340
		} catch (TransactionServiceException e) {
341
		    logger.error("Error getting orders from the transaction service: ", e);
342
			return baosPDF;
343
		}
344
 
345
		try {
346
			baosPDF = new ByteArrayOutputStream();
347
			Font helvetica8 = FontFactory.getFont(FontFactory.HELVETICA, 8);
348
			Document document = new Document();
349
			PdfWriter.getInstance(document, baosPDF);
350
			document.addAuthor("shop2020");
351
			document.addTitle("Manifest for store " + storeId + " provider " + providerId);
352
			document.open();
353
			PdfPTable table = new PdfPTable(1);
354
			table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
355
			table.getDefaultCell().setPaddingBottom(10.0f);
356
 
5766 rajveer 357
			String addressString =  store.getName() +  "\n" + store.getLine1() + ((store.getLine2() == null)? "": "\n" + store.getLine2()) 
358
									+ "\n" + store.getCity()  + "\n" + store.getState() + "\nPIN " + store.getPin()+ "\n\n";
5678 rajveer 359
			Paragraph addressParagraph = new Paragraph(addressString, new Font(FontFamily.TIMES_ROMAN,8f));
360
			PdfPCell addressCell = new PdfPCell();
361
			addressCell.addElement(addressParagraph);
362
			addressCell.setHorizontalAlignment(Element.ALIGN_LEFT);
363
			addressCell.setBorder(Rectangle.NO_BORDER);
364
 
365
			PdfPTable ordersTable = new PdfPTable(4);
366
			ordersTable.addCell(new Phrase("Sl No", helvetica8));
367
			ordersTable.addCell(new Phrase("Order No", helvetica8));
368
			ordersTable.addCell(new Phrase("AWB No", helvetica8));
369
			ordersTable.addCell(new Phrase("Packet Wt.", helvetica8));
370
 
371
			int serialNo = 0;
372
			for(int i=0; i < orders.size();i++){
373
				Order order = orders.get(i);
374
				if(!orderIds.contains(order.getId()))
375
					continue;
376
				if(order.getLogistics_provider_id()!=providerId)
377
					continue;
378
				if(order.getPickupStoreId()!=storeId)
379
					continue;
380
				//TODO: These are exactly the orders which will be shipped now. Shouldn't we change their status to be SHIPPED?
381
				serialNo++;
382
				List<LineItem> lineItems = order.getLineitems();
383
				double weight = 0;
384
				for(LineItem lineItem: lineItems)
385
					weight += lineItem.getTotal_weight();
386
 
387
				ordersTable.addCell(new Phrase(serialNo + "", helvetica8));
388
				ordersTable.addCell(new Phrase(order.getId() + "", helvetica8));
5714 rajveer 389
				ordersTable.addCell(new Phrase(awbs.get(orderIds.indexOf(order.getId())), helvetica8));
5678 rajveer 390
				ordersTable.addCell(new Phrase(weightFormat.format(weight), helvetica8));
391
			}
392
 
393
			table.addCell(addressCell);
394
			table.addCell(new Phrase("Courier Name: " + provider.getName(), helvetica8));
395
			table.addCell(new Phrase("Pick up Date: " + DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date()), helvetica8));
396
			table.addCell(ordersTable);
397
			table.setWidthPercentage(90.0f);
398
 
399
			document.add(table);  
400
			document.close();
401
			baosPDF.close();
402
		} catch (DocumentException e) {
403
		    logger.error("Error while creating the manifest file", e);
404
		} catch (MalformedURLException e) {
405
		    logger.error("Error while creating the manifest file", e);
406
		} catch (IOException e) {
407
			logger.error("Error while creating the manifest file", e);
408
		}
409
 
410
		return baosPDF;
411
	}
412
 
744 chandransh 413
	public static void main(String[] args) throws IOException {
414
		ManifestGenerator manifestGenerator = new ManifestGenerator();
5743 rajveer 415
		ByteArrayOutputStream baos = manifestGenerator.generateManifestFile(1, 1, true, null, null);
744 chandransh 416
		File f = new File("/home/ashish/Downloads/manifest-1-2.pdf");
417
		FileOutputStream fos = new FileOutputStream(f);
418
		baos.writeTo(fos);
419
	}
676 chandransh 420
}