Subversion Repositories SmartDukaan

Rev

Rev 8304 | Rev 13276 | 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;
8256 amar.kumar 9
import in.shop2020.model.v1.order.EbayOrder;
5945 mandeep.dh 10
import in.shop2020.model.v1.order.LineItem;
11
import in.shop2020.model.v1.order.Order;
12
import in.shop2020.model.v1.order.OrderStatus;
13
import in.shop2020.model.v1.order.TransactionServiceException;
14
import in.shop2020.thrift.clients.InventoryClient;
15
import in.shop2020.thrift.clients.LogisticsClient;
16
import in.shop2020.thrift.clients.TransactionClient;
17
 
676 chandransh 18
import java.io.ByteArrayOutputStream;
744 chandransh 19
import java.io.File;
20
import java.io.FileOutputStream;
676 chandransh 21
import java.io.IOException;
22
import java.net.MalformedURLException;
23
import java.text.DateFormat;
744 chandransh 24
import java.text.DecimalFormat;
4410 rajveer 25
import java.util.ArrayList;
676 chandransh 26
import java.util.Date;
27
import java.util.List;
28
 
29
import org.apache.thrift.TException;
3062 chandransh 30
import org.slf4j.Logger;
31
import org.slf4j.LoggerFactory;
676 chandransh 32
 
33
import com.itextpdf.text.Document;
34
import com.itextpdf.text.DocumentException;
35
import com.itextpdf.text.Element;
36
import com.itextpdf.text.Font;
5945 mandeep.dh 37
import com.itextpdf.text.Font.FontFamily;
676 chandransh 38
import com.itextpdf.text.FontFactory;
39
import com.itextpdf.text.Image;
40
import com.itextpdf.text.Paragraph;
41
import com.itextpdf.text.Phrase;
42
import com.itextpdf.text.Rectangle;
43
import com.itextpdf.text.pdf.PdfPCell;
44
import com.itextpdf.text.pdf.PdfPTable;
45
import com.itextpdf.text.pdf.PdfWriter;
46
 
47
public class ManifestGenerator {
3062 chandransh 48
 
49
    private static Logger logger = LoggerFactory.getLogger(ManifestGenerator.class);
50
 
3125 rajveer 51
	private TransactionClient tsc = null;
5945 mandeep.dh 52
	private InventoryClient csc = null;
3125 rajveer 53
	private LogisticsClient lsc = null;
744 chandransh 54
	private DecimalFormat weightFormat = new DecimalFormat("0.000");
676 chandransh 55
	public ManifestGenerator() {
56
		try {
3125 rajveer 57
			tsc = new TransactionClient();
5945 mandeep.dh 58
			csc = new InventoryClient();
3125 rajveer 59
			lsc = new LogisticsClient();
676 chandransh 60
		} catch (Exception e) {
3062 chandransh 61
		    logger.error("Error while initializing one of the thrift clients", e);
676 chandransh 62
		}
63
	}
64
 
5743 rajveer 65
	public ByteArrayOutputStream generateManifestFile(long warehouseId,	long providerId, boolean isCod, List<Long> orderIds, String runner) {
676 chandransh 66
		ByteArrayOutputStream baosPDF = null;
67
		in.shop2020.model.v1.order.TransactionService.Client txnClient = tsc.getClient();
5945 mandeep.dh 68
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = csc.getClient();
676 chandransh 69
		in.shop2020.logistics.LogisticsService.Client logisticsClient = lsc.getClient();
70
 
4410 rajveer 71
		List<OrderStatus> statuses = new ArrayList<OrderStatus>();
72
		statuses.add(OrderStatus.BILLED);
73
 
676 chandransh 74
		List<Order> orders = null;
75
		Warehouse warehouse = null;
76
		Provider provider = null;
5766 rajveer 77
		List<PickupStore> stores = null;
676 chandransh 78
		try {
8304 amar.kumar 79
			orders = txnClient.getOrdersInBatch(statuses, 0, 0, warehouseId, 0);
676 chandransh 80
			warehouse = inventoryClient.getWarehouse(warehouseId);
81
			provider = logisticsClient.getProvider(providerId);
5766 rajveer 82
			stores = logisticsClient.getAllPickupStores();
3062 chandransh 83
		} catch (TException e) {
84
		    logger.error("Error getting information from one of the Thrift Services: ", e);
676 chandransh 85
			return baosPDF;
86
		} catch (InventoryServiceException e) {
3062 chandransh 87
		    logger.error("Error getting warehouse info from the catalog service: ", e);
676 chandransh 88
			return baosPDF;
89
		} catch (LogisticsServiceException e) {
3062 chandransh 90
		    logger.error("Error getting provider info from the logistics service: ", e);
676 chandransh 91
			return baosPDF;
92
		} catch (TransactionServiceException e) {
3062 chandransh 93
		    logger.error("Error getting orders from the transaction service: ", e);
676 chandransh 94
			return baosPDF;
95
		}
96
 
97
		try {
98
			baosPDF = new ByteArrayOutputStream();
99
			Font helvetica8 = FontFactory.getFont(FontFactory.HELVETICA, 8);
100
			Document document = new Document();
101
			PdfWriter.getInstance(document, baosPDF);
102
			document.addAuthor("shop2020");
103
			document.addTitle("Manifest for warehouse " + warehouseId + " provider " + providerId);
104
			document.open();
105
			PdfPTable table = new PdfPTable(1);
106
			table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
107
			table.getDefaultCell().setPaddingBottom(10.0f);
108
 
744 chandransh 109
			String logoPath = ManifestGenerator.class.getResource("/logo.jpg").getPath();
676 chandransh 110
			PdfPCell logoCell = new PdfPCell(Image.getInstance(logoPath), false);
111
			logoCell.setBorder(Rectangle.NO_BORDER);
112
 
113
			String addressString =  warehouse.getLocation() + "\nPIN " + warehouse.getPincode()+ "\n\n";
114
			Paragraph addressParagraph = new Paragraph(addressString, new Font(FontFamily.TIMES_ROMAN,8f));
115
			PdfPCell addressCell = new PdfPCell();
116
			addressCell.addElement(addressParagraph);
117
			addressCell.setHorizontalAlignment(Element.ALIGN_LEFT);
118
			addressCell.setBorder(Rectangle.NO_BORDER);
119
 
8276 amar.kumar 120
			PdfPTable ordersTable = null;
121
			ordersTable = new PdfPTable(8);
122
			if (providerId > 7 && providerId <12) {
123
				ordersTable = new PdfPTable(10);
124
			}
13146 manish.sha 125
			if(provider.isGroupShipmentAllowed()){
126
				ordersTable = new PdfPTable(9);
127
			}
676 chandransh 128
			ordersTable.addCell(new Phrase("Sl No", helvetica8));
13146 manish.sha 129
			if(provider.isGroupShipmentAllowed()){
130
				ordersTable.addCell(new Phrase("Master Order Id", helvetica8));
131
			}
676 chandransh 132
			ordersTable.addCell(new Phrase("Order No", helvetica8));
133
			ordersTable.addCell(new Phrase("AWB No", helvetica8));
134
			ordersTable.addCell(new Phrase("Packet Wt.", helvetica8));
8256 amar.kumar 135
 
136
			//if Logistics is Ebay Power Ship add extra fields like Sales Record Number and PaisaPayId
137
			if(providerId > 7 && providerId <12) {
138
				ordersTable.addCell(new Phrase("PaisaPayId", helvetica8));
139
				ordersTable.addCell(new Phrase("Sales Rec No.", helvetica8));
140
			}
141
 
5766 rajveer 142
			if(runner == null){
143
				ordersTable.addCell(new Phrase("Name", helvetica8));
144
				ordersTable.addCell(new Phrase("Destination City", helvetica8));
145
				ordersTable.addCell(new Phrase("Pincode", helvetica8));
146
				ordersTable.addCell(new Phrase("State", helvetica8));
147
			}else{
148
				ordersTable.addCell(new Phrase("Store Code", helvetica8));
149
				ordersTable.addCell(new Phrase("Store Address", helvetica8));
150
				ordersTable.addCell(new Phrase("Store City", helvetica8));
151
				ordersTable.addCell(new Phrase("Phone", helvetica8));
152
			}
676 chandransh 153
 
154
			int serialNo = 0;
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;
676 chandransh 163
				//TODO: These are exactly the orders which will be shipped now. Shouldn't we change their status to be SHIPPED?
164
				serialNo++;
165
				List<LineItem> lineItems = order.getLineitems();
166
				double weight = 0;
167
				for(LineItem lineItem: lineItems)
168
					weight += lineItem.getTotal_weight();
169
 
170
				ordersTable.addCell(new Phrase(serialNo + "", helvetica8));
13146 manish.sha 171
				if(provider.isGroupShipmentAllowed()){
172
					ordersTable.addCell(new Phrase(order.getLogisticsTransactionId(), helvetica8));
173
				}
676 chandransh 174
				ordersTable.addCell(new Phrase(order.getId() + "", helvetica8));
175
				ordersTable.addCell(new Phrase(order.getAirwaybill_no(), helvetica8));
919 rajveer 176
				ordersTable.addCell(new Phrase(weightFormat.format(weight), helvetica8));
8256 amar.kumar 177
 
178
				//if Logistics is Ebay Power Ship add extra fields like Sales Record Number and PaisaPayId
179
				if(providerId > 7 && providerId <12) {
180
					try {
181
						EbayOrder ebayOrder = txnClient.getEbayOrderByOrderId(order.getId());
182
						ordersTable.addCell(new Phrase(ebayOrder.getPaisaPayId(), helvetica8));
183
						ordersTable.addCell(new Phrase(new Long(ebayOrder.getSalesRecordNumber()).toString(), helvetica8));
184
					} catch (Exception e) {
185
						logger.error("Error getting Ebay Order for OrderId : " + order.getId(), e);
186
						return baosPDF;
187
					}
188
				}
189
 
5766 rajveer 190
				if(runner == null){
191
					ordersTable.addCell(new Phrase(order.getCustomer_name(), helvetica8));
192
					ordersTable.addCell(new Phrase(order.getCustomer_city(), helvetica8));
193
					ordersTable.addCell(new Phrase(order.getCustomer_pincode(), helvetica8));
194
					ordersTable.addCell(new Phrase(order.getCustomer_state(), helvetica8));
195
				}else{
196
					PickupStore store = getStoreFromId(stores, order.getPickupStoreId());
197
					ordersTable.addCell(new Phrase(store.getHotspotId(), helvetica8));
198
					ordersTable.addCell(new Phrase(store.getLine1() + ((store.getLine2() == null)? "": "\n" + store.getLine2()), helvetica8));
199
					ordersTable.addCell(new Phrase(store.getCity(), helvetica8));
200
					ordersTable.addCell(new Phrase(store.getPhone(), helvetica8));
201
				}
676 chandransh 202
			}
203
 
204
			table.addCell(logoCell);
205
			table.addCell(addressCell);
3062 chandransh 206
			if(isCod)
207
			    table.addCell(new Phrase("PAYMODE: COD", helvetica8));
208
			else
209
			    table.addCell(new Phrase("PAYMODE: Prepaid", helvetica8));
5743 rajveer 210
			if(provider.getPickup() == PickUpType.RUNNER){
211
				table.addCell(new Phrase("Runner Name: " + runner, helvetica8));
212
			}else{
213
				table.addCell(new Phrase("Courier Name: " + provider.getName(), helvetica8));
214
			}
215
 
676 chandransh 216
			table.addCell(new Phrase("Pick up Date: " + DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date()), helvetica8));
217
			table.addCell(ordersTable);
218
			table.setWidthPercentage(90.0f);
219
 
220
			document.add(table);  
221
			document.close();
222
			baosPDF.close();
223
		} catch (DocumentException e) {
3062 chandransh 224
		    logger.error("Error while creating the manifest file", e);
676 chandransh 225
		} catch (MalformedURLException e) {
3062 chandransh 226
		    logger.error("Error while creating the manifest file", e);
676 chandransh 227
		} catch (IOException e) {
3062 chandransh 228
			logger.error("Error while creating the manifest file", e);
676 chandransh 229
		}
230
 
231
		return baosPDF;
232
	}
5678 rajveer 233
 
5766 rajveer 234
	private static PickupStore getStoreFromId(List<PickupStore> stores, long storeId){
235
		for(PickupStore store: stores){
236
			if(store.getId() == storeId)
237
				return store;
238
		}
239
		return null;
240
	}
744 chandransh 241
 
5766 rajveer 242
 
5714 rajveer 243
	public ByteArrayOutputStream generateManifestFile(long providerId, long storeId, List<Long> orderIds, List<String> awbs) {
5678 rajveer 244
		ByteArrayOutputStream baosPDF = null;
245
		in.shop2020.model.v1.order.TransactionService.Client txnClient = tsc.getClient();
246
		in.shop2020.logistics.LogisticsService.Client logisticsClient = lsc.getClient();
247
 
248
		List<OrderStatus> statuses = new ArrayList<OrderStatus>();
249
		statuses.add(OrderStatus.RET_PICKUP_REQUEST_RAISED);
250
 
251
		List<Order> orders = null;
252
		PickupStore store = null;
253
		Provider provider = null;
254
		try {
8304 amar.kumar 255
			orders = txnClient.getOrdersInBatch(statuses, 0, 0, 0, 0);
5678 rajveer 256
			store = logisticsClient.getPickupStore(storeId);
257
			provider = logisticsClient.getProvider(providerId);
258
		} catch (TException e) {
259
		    logger.error("Error getting information from one of the Thrift Services: ", e);
260
			return baosPDF;
261
		} catch (LogisticsServiceException e) {
262
		    logger.error("Error getting provider info from the logistics service: ", e);
263
			return baosPDF;
264
		} catch (TransactionServiceException e) {
265
		    logger.error("Error getting orders from the transaction service: ", e);
266
			return baosPDF;
267
		}
268
 
269
		try {
270
			baosPDF = new ByteArrayOutputStream();
271
			Font helvetica8 = FontFactory.getFont(FontFactory.HELVETICA, 8);
272
			Document document = new Document();
273
			PdfWriter.getInstance(document, baosPDF);
274
			document.addAuthor("shop2020");
275
			document.addTitle("Manifest for store " + storeId + " provider " + providerId);
276
			document.open();
277
			PdfPTable table = new PdfPTable(1);
278
			table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
279
			table.getDefaultCell().setPaddingBottom(10.0f);
280
 
5766 rajveer 281
			String addressString =  store.getName() +  "\n" + store.getLine1() + ((store.getLine2() == null)? "": "\n" + store.getLine2()) 
282
									+ "\n" + store.getCity()  + "\n" + store.getState() + "\nPIN " + store.getPin()+ "\n\n";
5678 rajveer 283
			Paragraph addressParagraph = new Paragraph(addressString, new Font(FontFamily.TIMES_ROMAN,8f));
284
			PdfPCell addressCell = new PdfPCell();
285
			addressCell.addElement(addressParagraph);
286
			addressCell.setHorizontalAlignment(Element.ALIGN_LEFT);
287
			addressCell.setBorder(Rectangle.NO_BORDER);
288
 
289
			PdfPTable ordersTable = new PdfPTable(4);
290
			ordersTable.addCell(new Phrase("Sl No", helvetica8));
291
			ordersTable.addCell(new Phrase("Order No", helvetica8));
292
			ordersTable.addCell(new Phrase("AWB No", helvetica8));
293
			ordersTable.addCell(new Phrase("Packet Wt.", helvetica8));
294
 
295
			int serialNo = 0;
296
			for(int i=0; i < orders.size();i++){
297
				Order order = orders.get(i);
298
				if(!orderIds.contains(order.getId()))
299
					continue;
300
				if(order.getLogistics_provider_id()!=providerId)
301
					continue;
302
				if(order.getPickupStoreId()!=storeId)
303
					continue;
304
				//TODO: These are exactly the orders which will be shipped now. Shouldn't we change their status to be SHIPPED?
305
				serialNo++;
306
				List<LineItem> lineItems = order.getLineitems();
307
				double weight = 0;
308
				for(LineItem lineItem: lineItems)
309
					weight += lineItem.getTotal_weight();
310
 
311
				ordersTable.addCell(new Phrase(serialNo + "", helvetica8));
312
				ordersTable.addCell(new Phrase(order.getId() + "", helvetica8));
5714 rajveer 313
				ordersTable.addCell(new Phrase(awbs.get(orderIds.indexOf(order.getId())), helvetica8));
5678 rajveer 314
				ordersTable.addCell(new Phrase(weightFormat.format(weight), helvetica8));
315
			}
316
 
317
			table.addCell(addressCell);
318
			table.addCell(new Phrase("Courier Name: " + provider.getName(), helvetica8));
319
			table.addCell(new Phrase("Pick up Date: " + DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date()), helvetica8));
320
			table.addCell(ordersTable);
321
			table.setWidthPercentage(90.0f);
322
 
323
			document.add(table);  
324
			document.close();
325
			baosPDF.close();
326
		} catch (DocumentException e) {
327
		    logger.error("Error while creating the manifest file", e);
328
		} catch (MalformedURLException e) {
329
		    logger.error("Error while creating the manifest file", e);
330
		} catch (IOException e) {
331
			logger.error("Error while creating the manifest file", e);
332
		}
333
 
334
		return baosPDF;
335
	}
336
 
744 chandransh 337
	public static void main(String[] args) throws IOException {
338
		ManifestGenerator manifestGenerator = new ManifestGenerator();
5743 rajveer 339
		ByteArrayOutputStream baos = manifestGenerator.generateManifestFile(1, 1, true, null, null);
744 chandransh 340
		File f = new File("/home/ashish/Downloads/manifest-1-2.pdf");
341
		FileOutputStream fos = new FileOutputStream(f);
342
		baos.writeTo(fos);
343
	}
676 chandransh 344
}