Subversion Repositories SmartDukaan

Rev

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