Subversion Repositories SmartDukaan

Rev

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