Subversion Repositories SmartDukaan

Rev

Rev 20840 | Rev 20842 | 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));
20841 amit.gupta 251
			table.addCell(new Phrase("", helvetica8));
20836 amit.gupta 252
			document.add(new Paragraph(buyerInfo.getOrganisationName() + " Manifest Report", new Font(FontFamily.TIMES_ROMAN,12f)));
253
			document.add(new Paragraph(addressString, new Font(FontFamily.TIMES_ROMAN,8f)));
676 chandransh 254
			document.add(table);  
20834 amit.gupta 255
			document.add(ordersTable);  
676 chandransh 256
			document.close();
257
			baosPDF.close();
258
		} catch (DocumentException e) {
3062 chandransh 259
		    logger.error("Error while creating the manifest file", e);
676 chandransh 260
		} catch (MalformedURLException e) {
3062 chandransh 261
		    logger.error("Error while creating the manifest file", e);
676 chandransh 262
		} catch (IOException e) {
3062 chandransh 263
			logger.error("Error while creating the manifest file", e);
676 chandransh 264
		}
265
 
266
		return baosPDF;
267
	}
5678 rajveer 268
 
5766 rajveer 269
	private static PickupStore getStoreFromId(List<PickupStore> stores, long storeId){
270
		for(PickupStore store: stores){
271
			if(store.getId() == storeId)
272
				return store;
273
		}
274
		return null;
275
	}
744 chandransh 276
 
5766 rajveer 277
 
5714 rajveer 278
	public ByteArrayOutputStream generateManifestFile(long providerId, long storeId, List<Long> orderIds, List<String> awbs) {
5678 rajveer 279
		ByteArrayOutputStream baosPDF = null;
280
		in.shop2020.model.v1.order.TransactionService.Client txnClient = tsc.getClient();
281
		in.shop2020.logistics.LogisticsService.Client logisticsClient = lsc.getClient();
282
 
283
		List<OrderStatus> statuses = new ArrayList<OrderStatus>();
284
		statuses.add(OrderStatus.RET_PICKUP_REQUEST_RAISED);
285
 
286
		List<Order> orders = null;
287
		PickupStore store = null;
288
		Provider provider = null;
289
		try {
8304 amar.kumar 290
			orders = txnClient.getOrdersInBatch(statuses, 0, 0, 0, 0);
5678 rajveer 291
			store = logisticsClient.getPickupStore(storeId);
292
			provider = logisticsClient.getProvider(providerId);
293
		} catch (TException e) {
294
		    logger.error("Error getting information from one of the Thrift Services: ", e);
295
			return baosPDF;
296
		} catch (LogisticsServiceException e) {
297
		    logger.error("Error getting provider info from the logistics service: ", e);
298
			return baosPDF;
299
		} catch (TransactionServiceException e) {
300
		    logger.error("Error getting orders from the transaction service: ", e);
301
			return baosPDF;
302
		}
303
 
304
		try {
305
			baosPDF = new ByteArrayOutputStream();
306
			Font helvetica8 = FontFactory.getFont(FontFactory.HELVETICA, 8);
307
			Document document = new Document();
308
			PdfWriter.getInstance(document, baosPDF);
309
			document.addAuthor("shop2020");
310
			document.addTitle("Manifest for store " + storeId + " provider " + providerId);
311
			document.open();
312
			PdfPTable table = new PdfPTable(1);
313
			table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
314
			table.getDefaultCell().setPaddingBottom(10.0f);
315
 
5766 rajveer 316
			String addressString =  store.getName() +  "\n" + store.getLine1() + ((store.getLine2() == null)? "": "\n" + store.getLine2()) 
317
									+ "\n" + store.getCity()  + "\n" + store.getState() + "\nPIN " + store.getPin()+ "\n\n";
5678 rajveer 318
			Paragraph addressParagraph = new Paragraph(addressString, new Font(FontFamily.TIMES_ROMAN,8f));
319
			PdfPCell addressCell = new PdfPCell();
320
			addressCell.addElement(addressParagraph);
321
			addressCell.setHorizontalAlignment(Element.ALIGN_LEFT);
322
			addressCell.setBorder(Rectangle.NO_BORDER);
323
 
324
			PdfPTable ordersTable = new PdfPTable(4);
325
			ordersTable.addCell(new Phrase("Sl No", helvetica8));
326
			ordersTable.addCell(new Phrase("Order No", helvetica8));
327
			ordersTable.addCell(new Phrase("AWB No", helvetica8));
328
			ordersTable.addCell(new Phrase("Packet Wt.", helvetica8));
329
 
330
			int serialNo = 0;
331
			for(int i=0; i < orders.size();i++){
332
				Order order = orders.get(i);
333
				if(!orderIds.contains(order.getId()))
334
					continue;
335
				if(order.getLogistics_provider_id()!=providerId)
336
					continue;
337
				if(order.getPickupStoreId()!=storeId)
338
					continue;
339
				//TODO: These are exactly the orders which will be shipped now. Shouldn't we change their status to be SHIPPED?
340
				serialNo++;
341
				List<LineItem> lineItems = order.getLineitems();
342
				double weight = 0;
343
				for(LineItem lineItem: lineItems)
344
					weight += lineItem.getTotal_weight();
345
 
346
				ordersTable.addCell(new Phrase(serialNo + "", helvetica8));
347
				ordersTable.addCell(new Phrase(order.getId() + "", helvetica8));
5714 rajveer 348
				ordersTable.addCell(new Phrase(awbs.get(orderIds.indexOf(order.getId())), helvetica8));
5678 rajveer 349
				ordersTable.addCell(new Phrase(weightFormat.format(weight), helvetica8));
350
			}
351
 
352
			table.addCell(addressCell);
353
			table.addCell(new Phrase("Courier Name: " + provider.getName(), helvetica8));
354
			table.addCell(new Phrase("Pick up Date: " + DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date()), helvetica8));
355
			table.addCell(ordersTable);
356
			table.setWidthPercentage(90.0f);
357
 
358
			document.add(table);  
359
			document.close();
360
			baosPDF.close();
361
		} catch (DocumentException e) {
362
		    logger.error("Error while creating the manifest file", e);
363
		} catch (MalformedURLException e) {
364
		    logger.error("Error while creating the manifest file", e);
365
		} catch (IOException e) {
366
			logger.error("Error while creating the manifest file", e);
367
		}
368
 
369
		return baosPDF;
370
	}
371
 
744 chandransh 372
	public static void main(String[] args) throws IOException {
373
		ManifestGenerator manifestGenerator = new ManifestGenerator();
5743 rajveer 374
		ByteArrayOutputStream baos = manifestGenerator.generateManifestFile(1, 1, true, null, null);
744 chandransh 375
		File f = new File("/home/ashish/Downloads/manifest-1-2.pdf");
376
		FileOutputStream fos = new FileOutputStream(f);
377
		baos.writeTo(fos);
378
	}
676 chandransh 379
}