Subversion Repositories SmartDukaan

Rev

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