Subversion Repositories SmartDukaan

Rev

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