Subversion Repositories SmartDukaan

Rev

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