Subversion Repositories SmartDukaan

Rev

Rev 8256 | Rev 8304 | 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 {
4410 rajveer 79
			orders = txnClient.getOrdersInBatch(statuses, 0, 0, warehouseId);
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
			}
676 chandransh 125
			ordersTable.addCell(new Phrase("Sl No", helvetica8));
126
			ordersTable.addCell(new Phrase("Order No", helvetica8));
127
			ordersTable.addCell(new Phrase("AWB No", helvetica8));
128
			ordersTable.addCell(new Phrase("Packet Wt.", 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
 
148
			int serialNo = 0;
149
			for(int i=0; i < orders.size();i++){
150
				Order order = orders.get(i);
4788 rajveer 151
				if(!orderIds.contains(order.getId()))
152
					continue;
676 chandransh 153
				if(order.getLogistics_provider_id()!=providerId)
154
					continue;
5554 rajveer 155
				if(order.isLogisticsCod() != isCod)
3062 chandransh 156
	                continue;
676 chandransh 157
				//TODO: These are exactly the orders which will be shipped now. Shouldn't we change their status to be SHIPPED?
158
				serialNo++;
159
				List<LineItem> lineItems = order.getLineitems();
160
				double weight = 0;
161
				for(LineItem lineItem: lineItems)
162
					weight += lineItem.getTotal_weight();
163
 
164
				ordersTable.addCell(new Phrase(serialNo + "", helvetica8));
165
				ordersTable.addCell(new Phrase(order.getId() + "", helvetica8));
166
				ordersTable.addCell(new Phrase(order.getAirwaybill_no(), helvetica8));
919 rajveer 167
				ordersTable.addCell(new Phrase(weightFormat.format(weight), helvetica8));
8256 amar.kumar 168
 
169
				//if Logistics is Ebay Power Ship add extra fields like Sales Record Number and PaisaPayId
170
				if(providerId > 7 && providerId <12) {
171
					try {
172
						EbayOrder ebayOrder = txnClient.getEbayOrderByOrderId(order.getId());
173
						ordersTable.addCell(new Phrase(ebayOrder.getPaisaPayId(), helvetica8));
174
						ordersTable.addCell(new Phrase(new Long(ebayOrder.getSalesRecordNumber()).toString(), helvetica8));
175
					} catch (Exception e) {
176
						logger.error("Error getting Ebay Order for OrderId : " + order.getId(), e);
177
						return baosPDF;
178
					}
179
				}
180
 
5766 rajveer 181
				if(runner == null){
182
					ordersTable.addCell(new Phrase(order.getCustomer_name(), helvetica8));
183
					ordersTable.addCell(new Phrase(order.getCustomer_city(), helvetica8));
184
					ordersTable.addCell(new Phrase(order.getCustomer_pincode(), helvetica8));
185
					ordersTable.addCell(new Phrase(order.getCustomer_state(), helvetica8));
186
				}else{
187
					PickupStore store = getStoreFromId(stores, order.getPickupStoreId());
188
					ordersTable.addCell(new Phrase(store.getHotspotId(), helvetica8));
189
					ordersTable.addCell(new Phrase(store.getLine1() + ((store.getLine2() == null)? "": "\n" + store.getLine2()), helvetica8));
190
					ordersTable.addCell(new Phrase(store.getCity(), helvetica8));
191
					ordersTable.addCell(new Phrase(store.getPhone(), helvetica8));
192
				}
676 chandransh 193
			}
194
 
195
			table.addCell(logoCell);
196
			table.addCell(addressCell);
3062 chandransh 197
			if(isCod)
198
			    table.addCell(new Phrase("PAYMODE: COD", helvetica8));
199
			else
200
			    table.addCell(new Phrase("PAYMODE: Prepaid", helvetica8));
5743 rajveer 201
			if(provider.getPickup() == PickUpType.RUNNER){
202
				table.addCell(new Phrase("Runner Name: " + runner, helvetica8));
203
			}else{
204
				table.addCell(new Phrase("Courier Name: " + provider.getName(), helvetica8));
205
			}
206
 
676 chandransh 207
			table.addCell(new Phrase("Pick up Date: " + DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date()), helvetica8));
208
			table.addCell(ordersTable);
209
			table.setWidthPercentage(90.0f);
210
 
211
			document.add(table);  
212
			document.close();
213
			baosPDF.close();
214
		} catch (DocumentException e) {
3062 chandransh 215
		    logger.error("Error while creating the manifest file", e);
676 chandransh 216
		} catch (MalformedURLException e) {
3062 chandransh 217
		    logger.error("Error while creating the manifest file", e);
676 chandransh 218
		} catch (IOException e) {
3062 chandransh 219
			logger.error("Error while creating the manifest file", e);
676 chandransh 220
		}
221
 
222
		return baosPDF;
223
	}
5678 rajveer 224
 
5766 rajveer 225
	private static PickupStore getStoreFromId(List<PickupStore> stores, long storeId){
226
		for(PickupStore store: stores){
227
			if(store.getId() == storeId)
228
				return store;
229
		}
230
		return null;
231
	}
744 chandransh 232
 
5766 rajveer 233
 
5714 rajveer 234
	public ByteArrayOutputStream generateManifestFile(long providerId, long storeId, List<Long> orderIds, List<String> awbs) {
5678 rajveer 235
		ByteArrayOutputStream baosPDF = null;
236
		in.shop2020.model.v1.order.TransactionService.Client txnClient = tsc.getClient();
237
		in.shop2020.logistics.LogisticsService.Client logisticsClient = lsc.getClient();
238
 
239
		List<OrderStatus> statuses = new ArrayList<OrderStatus>();
240
		statuses.add(OrderStatus.RET_PICKUP_REQUEST_RAISED);
241
 
242
		List<Order> orders = null;
243
		PickupStore store = null;
244
		Provider provider = null;
245
		try {
246
			orders = txnClient.getOrdersInBatch(statuses, 0, 0, 0);
247
			store = logisticsClient.getPickupStore(storeId);
248
			provider = logisticsClient.getProvider(providerId);
249
		} catch (TException e) {
250
		    logger.error("Error getting information from one of the Thrift Services: ", e);
251
			return baosPDF;
252
		} catch (LogisticsServiceException e) {
253
		    logger.error("Error getting provider info from the logistics service: ", e);
254
			return baosPDF;
255
		} catch (TransactionServiceException e) {
256
		    logger.error("Error getting orders from the transaction service: ", e);
257
			return baosPDF;
258
		}
259
 
260
		try {
261
			baosPDF = new ByteArrayOutputStream();
262
			Font helvetica8 = FontFactory.getFont(FontFactory.HELVETICA, 8);
263
			Document document = new Document();
264
			PdfWriter.getInstance(document, baosPDF);
265
			document.addAuthor("shop2020");
266
			document.addTitle("Manifest for store " + storeId + " provider " + providerId);
267
			document.open();
268
			PdfPTable table = new PdfPTable(1);
269
			table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
270
			table.getDefaultCell().setPaddingBottom(10.0f);
271
 
5766 rajveer 272
			String addressString =  store.getName() +  "\n" + store.getLine1() + ((store.getLine2() == null)? "": "\n" + store.getLine2()) 
273
									+ "\n" + store.getCity()  + "\n" + store.getState() + "\nPIN " + store.getPin()+ "\n\n";
5678 rajveer 274
			Paragraph addressParagraph = new Paragraph(addressString, new Font(FontFamily.TIMES_ROMAN,8f));
275
			PdfPCell addressCell = new PdfPCell();
276
			addressCell.addElement(addressParagraph);
277
			addressCell.setHorizontalAlignment(Element.ALIGN_LEFT);
278
			addressCell.setBorder(Rectangle.NO_BORDER);
279
 
280
			PdfPTable ordersTable = new PdfPTable(4);
281
			ordersTable.addCell(new Phrase("Sl No", helvetica8));
282
			ordersTable.addCell(new Phrase("Order No", helvetica8));
283
			ordersTable.addCell(new Phrase("AWB No", helvetica8));
284
			ordersTable.addCell(new Phrase("Packet Wt.", helvetica8));
285
 
286
			int serialNo = 0;
287
			for(int i=0; i < orders.size();i++){
288
				Order order = orders.get(i);
289
				if(!orderIds.contains(order.getId()))
290
					continue;
291
				if(order.getLogistics_provider_id()!=providerId)
292
					continue;
293
				if(order.getPickupStoreId()!=storeId)
294
					continue;
295
				//TODO: These are exactly the orders which will be shipped now. Shouldn't we change their status to be SHIPPED?
296
				serialNo++;
297
				List<LineItem> lineItems = order.getLineitems();
298
				double weight = 0;
299
				for(LineItem lineItem: lineItems)
300
					weight += lineItem.getTotal_weight();
301
 
302
				ordersTable.addCell(new Phrase(serialNo + "", helvetica8));
303
				ordersTable.addCell(new Phrase(order.getId() + "", helvetica8));
5714 rajveer 304
				ordersTable.addCell(new Phrase(awbs.get(orderIds.indexOf(order.getId())), helvetica8));
5678 rajveer 305
				ordersTable.addCell(new Phrase(weightFormat.format(weight), helvetica8));
306
			}
307
 
308
			table.addCell(addressCell);
309
			table.addCell(new Phrase("Courier Name: " + provider.getName(), helvetica8));
310
			table.addCell(new Phrase("Pick up Date: " + DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date()), helvetica8));
311
			table.addCell(ordersTable);
312
			table.setWidthPercentage(90.0f);
313
 
314
			document.add(table);  
315
			document.close();
316
			baosPDF.close();
317
		} catch (DocumentException e) {
318
		    logger.error("Error while creating the manifest file", e);
319
		} catch (MalformedURLException e) {
320
		    logger.error("Error while creating the manifest file", e);
321
		} catch (IOException e) {
322
			logger.error("Error while creating the manifest file", e);
323
		}
324
 
325
		return baosPDF;
326
	}
327
 
744 chandransh 328
	public static void main(String[] args) throws IOException {
329
		ManifestGenerator manifestGenerator = new ManifestGenerator();
5743 rajveer 330
		ByteArrayOutputStream baos = manifestGenerator.generateManifestFile(1, 1, true, null, null);
744 chandransh 331
		File f = new File("/home/ashish/Downloads/manifest-1-2.pdf");
332
		FileOutputStream fos = new FileOutputStream(f);
333
		baos.writeTo(fos);
334
	}
676 chandransh 335
}