Subversion Repositories SmartDukaan

Rev

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