Subversion Repositories SmartDukaan

Rev

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