Subversion Repositories SmartDukaan

Rev

Rev 8742 | Rev 8759 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
8616 vikram.rag 1
package in.shop2020;
2
 
3
import in.shop2020.model.v1.catalog.CatalogService;
8739 vikram.rag 4
import in.shop2020.model.v1.catalog.SnapdealItem;
8616 vikram.rag 5
import in.shop2020.model.v1.catalog.CatalogServiceException;
6
import in.shop2020.model.v1.catalog.Item;
7
import in.shop2020.model.v1.inventory.InventoryService;
8
import in.shop2020.model.v1.inventory.InventoryServiceException;
9
import in.shop2020.model.v1.inventory.VendorItemPricing;
10
import in.shop2020.model.v1.inventory.Warehouse;
11
import in.shop2020.model.v1.order.LineItem;
12
import in.shop2020.model.v1.order.OrderStatus;
13
import in.shop2020.model.v1.order.OrderType;
14
import in.shop2020.model.v1.order.SnapdealOrder;
15
import in.shop2020.model.v1.order.SourceDetail;
16
import in.shop2020.model.v1.order.Transaction;
8651 vikram.rag 17
import in.shop2020.model.v1.order.TransactionServiceException;
8616 vikram.rag 18
import in.shop2020.model.v1.order.TransactionStatus;
19
import in.shop2020.model.v1.order.TransactionService.Client;
20
import in.shop2020.model.v1.user.User;
21
import in.shop2020.payments.PaymentException;
22
import in.shop2020.payments.PaymentStatus;
23
import in.shop2020.thrift.clients.CatalogClient;
24
import in.shop2020.thrift.clients.InventoryClient;
25
import in.shop2020.thrift.clients.PaymentClient;
26
import in.shop2020.thrift.clients.TransactionClient;
27
import in.shop2020.thrift.clients.UserClient;
28
 
29
import java.io.File;
8651 vikram.rag 30
import java.text.ParseException;
8616 vikram.rag 31
import java.text.SimpleDateFormat;
32
import java.util.ArrayList;
33
import java.util.Calendar;
34
import java.util.Collections;
35
import java.util.Date;
36
import java.util.List;
37
import javax.xml.bind.JAXBContext;
38
import javax.xml.bind.JAXBException;
39
import javax.xml.bind.Marshaller;
40
import javax.xml.bind.Unmarshaller;
8739 vikram.rag 41
 
8616 vikram.rag 42
import org.apache.thrift.TException;
43
import org.apache.thrift.transport.TTransportException;
8645 vikram.rag 44
import org.slf4j.Logger;
45
import org.slf4j.LoggerFactory;
8616 vikram.rag 46
 
47
import order.SaholicAPI;
48
import order.SaholicAPI.Order;
49
 
50
public class ProcessSnapdealOrder {
51
	private static final int SNAPDEAL_SOURCE_ID = 7;
52
	private static final String FIRSTFLIGHT = "First Flight";
8627 vikram.rag 53
	private static final String DELHIVERY = "DELHIVERY";
54
	private static final String BLUEDART = "Bluedart";
8715 vikram.rag 55
	private static final String CHHOTU = "CHHOTU";	
56
	private static final String LAST_MILE = "LAST_MILE";
57
	private static final String  ECOMM_EXP = "ECOMM_EXP";
58
	private static final String  EWINGS = "EWINGS";
59
	private static final String DTDC = "DTDC";
60
	private static final String WAREOLOG = "DTDC";
61
	private static final String  MIRAKLE = "MIRAKLE";
62
	private static final String RAJIV_TRANSPORT = "RAJIV_TRANSPORT";
63
	private static final String BLUEDART_HIGH_VALUE = "BLUEDART_HIGH_VALUE";
64
	private static final String BLUEDART_SURFACE = "BLUEDART_SURFACE";
65
	private static final String BLUEDART_APEX = "BLUEDART_APEX";
66
	private static final String ARAMEX = "ARAMEX";
67
	private static final String GATI = "GATI";
68
	private static final String INDIA_POST = "INDIA_POST";
69
	private static final String FEDEX = "FEDEX";
70
	private static final String IOT = "IOT";
71
	private static final String RED_EXP = "RED_EXP";
72
	private static final String GHARPAY = "GHARPAY";
73
	private static final String QUANTIUM = "QUANTIUM";
74
	private static final String GATI_AIR = "GATI_AIR`";
75
	private static final String VDELIVER = "VDELIVER";
8627 vikram.rag 76
	private static String transactionId;
77
	private static final int SNAPDEAL_GATEWAY_ID = 18;
8646 vikram.rag 78
	private static Logger logger;
8627 vikram.rag 79
 
8616 vikram.rag 80
	public static void processSnapdealOrders(File file) throws JAXBException{
8646 vikram.rag 81
		logger = LoggerFactory.getLogger(ProcessSnapdealOrder.class);
8616 vikram.rag 82
		JAXBContext jc = JAXBContext.newInstance(SaholicAPI.class);
83
		Unmarshaller unmarshaller = jc.createUnmarshaller();
84
		SaholicAPI orders = (SaholicAPI) unmarshaller.unmarshal(file);
85
		Marshaller marshaller = jc.createMarshaller();
86
		marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
87
		marshaller.marshal(orders, System.out);
88
		SourceDetail sourceDetail = null;
89
		User user = null;
90
		TransactionClient tsc = null;
91
		try {
92
			tsc = new TransactionClient();
93
			sourceDetail = tsc.getClient().getSourceDetail(SNAPDEAL_SOURCE_ID);
94
		} catch (Exception e) {
8645 vikram.rag 95
			logger.error("Unable to establish connection to the transaction service while getting Snapdeal Source Detail", e);
8616 vikram.rag 96
		}
97
		try {   
98
			in.shop2020.model.v1.user.UserContextService.Client userClient = new UserClient().getClient();
99
			user = userClient.getUserByEmail(sourceDetail.getEmail());
100
		} catch (Exception e) {
8649 vikram.rag 101
			logger.error("Unable to establish connection to the User service", e);
8616 vikram.rag 102
		}
103
		for(Order order:orders.getOrder()){
8627 vikram.rag 104
			long subOrderId = (long) order.getSuborderId();
105
			Client transaction_client = null;
106
			try {
107
				transaction_client = new TransactionClient().getClient();
108
				System.out.println("Suborder id " + subOrderId);
109
				if(transaction_client.snapdealOrderExists(subOrderId)) {
8739 vikram.rag 110
					logger.error("Snapdeal suborder id exists " + subOrderId);
8627 vikram.rag 111
					System.out.println("Suborder id exists");
112
					continue;
113
				}
114
 
115
			} catch (TTransportException e1) {
116
				// TODO Auto-generated catch block
117
				e1.printStackTrace();
118
			} catch (TException e) {
119
				// TODO Auto-generated catch block
120
				e.printStackTrace();
121
			}
8739 vikram.rag 122
			//System.out.println("Inside Order Parsing");
8616 vikram.rag 123
			Transaction txn = new Transaction();
8739 vikram.rag 124
			//System.out.println("User details are " + user.getActiveCartId());
8616 vikram.rag 125
			txn.setShoppingCartid(user.getActiveCartId());
8739 vikram.rag 126
			//System.out.println("creating transaction");
8616 vikram.rag 127
			txn.setCustomer_id(user.getUserId());
8627 vikram.rag 128
			System.out.println("User Id is " + user.getUserId());
8616 vikram.rag 129
			txn.setCreatedOn(new Date().getTime());
130
			txn.setTransactionStatus(TransactionStatus.INIT);
131
			txn.setStatusDescription("Order for Snapdeal ");
132
			List<in.shop2020.model.v1.order.Order> orderlist = new ArrayList<in.shop2020.model.v1.order.Order>();
133
			LineItem lineItem = null;
8739 vikram.rag 134
			CatalogService.Client catalogClient = null;
135
			SnapdealItem snapdealItem = null;
136
			double SellingPrice = 0;
8616 vikram.rag 137
			try {
8739 vikram.rag 138
				catalogClient =  new CatalogClient().getClient();
139
				snapdealItem = catalogClient.getSnapdealItem(order.getSKUCode());
8742 vikram.rag 140
				if(snapdealItem.getItem_id()!=0 && order.getSellingPricePerItem() <  snapdealItem.getExceptionPrice()){
8739 vikram.rag 141
					SellingPrice = snapdealItem.getExceptionPrice();
142
				}
143
				else{
144
					SellingPrice = order.getSellingPricePerItem();
145
				}
146
				lineItem = createLineItem(order.getSKUCode(),SellingPrice);
8627 vikram.rag 147
				System.out.println("Line item created");
8616 vikram.rag 148
			} catch (CatalogServiceException e) {
8645 vikram.rag 149
				logger.error("Unable to create order for suborderid  " + order.getSuborderId());
8616 vikram.rag 150
				e.printStackTrace();
8744 vikram.rag 151
				continue;
8616 vikram.rag 152
			} catch (TException e) {
8645 vikram.rag 153
				logger.error("Unable to create order for suborderid  " + order.getSuborderId());
8616 vikram.rag 154
				e.printStackTrace();
8744 vikram.rag 155
				continue;
8616 vikram.rag 156
			}
157
			in.shop2020.model.v1.order.Order t_order = new in.shop2020.model.v1.order.Order();
8627 vikram.rag 158
			t_order.setCustomer_id(user.getUserId());
159
			t_order.setCustomer_email(sourceDetail.getEmail());
160
			t_order.setCustomer_name(order.getCustomerName());
161
			t_order.setCustomer_address1("");
162
			t_order.setCustomer_address2("");
163
			t_order.setCustomer_city(order.getCity());
164
			t_order.setCustomer_state(order.getState());
165
			t_order.setCustomer_pincode(String.valueOf(order.getPINCode()));
8742 vikram.rag 166
			t_order.setTotal_amount(SellingPrice);            
8627 vikram.rag 167
			t_order.setTotal_weight(lineItem.getTotal_weight());
168
			t_order.setLineitems(Collections.singletonList(lineItem));            
169
			t_order.setStatus(OrderStatus.SUBMITTED_FOR_PROCESSING);
170
			t_order.setStatusDescription("In Process");
171
			t_order.setCreated_timestamp(new Date().getTime());
172
			t_order.setOrderType(OrderType.B2C);
173
			t_order.setCod(false);
174
			System.out.println("t order created");
175
			try {
8661 vikram.rag 176
				SimpleDateFormat istFormatter = new SimpleDateFormat("yyyy-MM-dd");
177
				Date shipDate = null;
178
				try {
179
					shipDate= istFormatter.parse(order.getShipByDate());
180
				} catch (ParseException e) {
8744 vikram.rag 181
					logger.error("Could not parse order ship date from file ");
8661 vikram.rag 182
					e.printStackTrace();
8744 vikram.rag 183
					continue;
8661 vikram.rag 184
				}
8627 vikram.rag 185
				Calendar time = Calendar.getInstance();
8661 vikram.rag 186
				t_order.setPromised_shipping_time(shipDate.getTime());
187
				t_order.setExpected_shipping_time(shipDate.getTime());
8744 vikram.rag 188
				time.add(Calendar.DAY_OF_MONTH, 4);
8627 vikram.rag 189
				t_order.setPromised_delivery_time(time.getTimeInMillis());
190
				t_order.setExpected_delivery_time(time.getTimeInMillis());
191
				System.out.println("Dates set in transaction");
8697 vikram.rag 192
			} catch(Exception e) {	
8645 vikram.rag 193
				logger.error("Error in updating Shipping or Delivery Time for suborderid  " + order.getSuborderId());
8627 vikram.rag 194
				continue;
195
			}
8657 vikram.rag 196
			InventoryService.Client inventoryClient = null;
8739 vikram.rag 197
			Warehouse fulfillmentWarehouse= null;
198
			try {
199
				inventoryClient = new InventoryClient().getClient();
200
				if(snapdealItem.getItem_id()!=0 && snapdealItem.getWarehouseId()!=0) {
201
					fulfillmentWarehouse = inventoryClient.getWarehouse(snapdealItem.getWarehouseId());
202
				} else {
203
					List<Long> itemAvailability = inventoryClient.getItemAvailabilityAtLocation(order.getSKUCode(), 1);
204
					fulfillmentWarehouse = inventoryClient.getWarehouse(itemAvailability.get(0));
205
				}
206
				logger.info("Fulfillment warehouse id for suborderid  " + order.getSuborderId() + " is " + fulfillmentWarehouse.getId());
207
				t_order.setFulfilmentWarehouseId(fulfillmentWarehouse.getId());
208
				logger.info("Billing warehouse id for suborderid  " + order.getSuborderId() + " is " + fulfillmentWarehouse.getBillingWarehouseId());
209
				t_order.setWarehouse_id(fulfillmentWarehouse.getBillingWarehouseId());
210
				VendorItemPricing vendorItemPricing = inventoryClient.getItemPricing(lineItem.getItem_id(), fulfillmentWarehouse.getVendor().getId());
211
				t_order.getLineitems().get(0).setTransfer_price(vendorItemPricing.getTransferPrice());
212
				t_order.getLineitems().get(0).setNlc(vendorItemPricing.getNlc());
8616 vikram.rag 213
			} catch (InventoryServiceException e) {
8662 vikram.rag 214
				logger.error("Error connecting inventory service for suborderid  " + order.getSuborderId());
8739 vikram.rag 215
				continue;
8616 vikram.rag 216
			} catch (TTransportException e) {
217
				// TODO Auto-generated catch block
218
				e.printStackTrace();
8744 vikram.rag 219
				continue;
8616 vikram.rag 220
			} catch (TException e) {
221
				// TODO Auto-generated catch block
222
				e.printStackTrace();
8744 vikram.rag 223
				continue;
8657 vikram.rag 224
			}
8616 vikram.rag 225
			if(order.getCourier().equalsIgnoreCase(FIRSTFLIGHT)) {
226
				t_order.setLogistics_provider_id(12);
227
			} else if(order.getCourier().equalsIgnoreCase(DELHIVERY)) {
228
				t_order.setLogistics_provider_id(13);
229
			} else if(order.getCourier().equalsIgnoreCase(BLUEDART)) {
230
				t_order.setLogistics_provider_id(14);
8642 vikram.rag 231
			}else if(order.getCourier().equalsIgnoreCase(CHHOTU)) {
232
				t_order.setLogistics_provider_id(16);
8715 vikram.rag 233
			}
234
			else if(order.getCourier().equalsIgnoreCase(LAST_MILE)){
235
				t_order.setLogistics_provider_id(17);
236
			}
237
			else if(order.getCourier().equalsIgnoreCase(ECOMM_EXP)){
238
				t_order.setLogistics_provider_id(18);
239
			}
240
			else if(order.getCourier().equalsIgnoreCase(EWINGS)){
241
				t_order.setLogistics_provider_id(19);
242
			}
243
			else if(order.getCourier().equalsIgnoreCase(DTDC)){
244
				t_order.setLogistics_provider_id(20);
245
			}
246
			else if(order.getCourier().equalsIgnoreCase(WAREOLOG)){
247
				t_order.setLogistics_provider_id(21);
248
			}
249
			else if(order.getCourier().equalsIgnoreCase(MIRAKLE)){
250
				t_order.setLogistics_provider_id(22);
251
			}
252
			else if(order.getCourier().equalsIgnoreCase(RAJIV_TRANSPORT)){
253
				t_order.setLogistics_provider_id(23);
254
			}
255
			else if(order.getCourier().equalsIgnoreCase(BLUEDART_HIGH_VALUE)){
256
				t_order.setLogistics_provider_id(24);
257
			}
258
			else if(order.getCourier().equalsIgnoreCase(BLUEDART_SURFACE)){
259
				t_order.setLogistics_provider_id(25);
260
			}
261
			else if(order.getCourier().equalsIgnoreCase(BLUEDART_APEX)){
262
				t_order.setLogistics_provider_id(26);
263
			}
264
			else if(order.getCourier().equalsIgnoreCase(ARAMEX)){
265
				t_order.setLogistics_provider_id(27);
266
			}
267
			else if(order.getCourier().equalsIgnoreCase(GATI)){
268
				t_order.setLogistics_provider_id(28);
269
			}
270
			else if(order.getCourier().equalsIgnoreCase(INDIA_POST)){
271
				t_order.setLogistics_provider_id(29);
272
			}
273
			else if(order.getCourier().equalsIgnoreCase(FEDEX)){
274
				t_order.setLogistics_provider_id(30);
275
			}
276
			else if(order.getCourier().equalsIgnoreCase(IOT)){
277
				t_order.setLogistics_provider_id(31);
278
			}
279
			else if(order.getCourier().equalsIgnoreCase(RED_EXP)){
280
				t_order.setLogistics_provider_id(32);
281
			}
282
			else if(order.getCourier().equalsIgnoreCase(VDELIVER)){
283
				t_order.setLogistics_provider_id(33);
284
			}
285
			else if(order.getCourier().equalsIgnoreCase(GHARPAY)){
286
				t_order.setLogistics_provider_id(34);
287
			}
288
			else if(order.getCourier().equalsIgnoreCase(QUANTIUM)){
289
				t_order.setLogistics_provider_id(35);
290
			}
291
			else if(order.getCourier().equalsIgnoreCase(GATI_AIR)){
292
				t_order.setLogistics_provider_id(36);
293
			}
294
			else {
8652 vikram.rag 295
				logger.error("Error No Courier Match please add courier to our system for suborderid  " + order.getSuborderId() + "Courier Name " + order.getCourier());
8627 vikram.rag 296
				continue;
8616 vikram.rag 297
			}
8627 vikram.rag 298
 
8616 vikram.rag 299
			t_order.setAirwaybill_no(String.valueOf(order.getAWBNumber()));
300
			t_order.setTracking_id(String.valueOf(order.getAWBNumber()));
8739 vikram.rag 301
			t_order.setTotal_amount(SellingPrice);
8627 vikram.rag 302
			t_order.setOrderType(OrderType.B2C);
303
			t_order.setSource(SNAPDEAL_SOURCE_ID);
304
			t_order.setOrderType(OrderType.B2C);
305
			orderlist.add(t_order);
306
			txn.setOrders(orderlist);
8739 vikram.rag 307
			String referenceCode = order.getReferenceCode();
308
			String productName = order.getProduct();
309
			SimpleDateFormat istFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
310
			Date snapdealTxnDate = null;
311
			try {
312
				snapdealTxnDate = istFormatter.parse(order.getOrderPlacedDate());
313
			} catch (ParseException e) {
8744 vikram.rag 314
				logger.error("Could not parse snapdeal order date from file ");
8739 vikram.rag 315
				e.printStackTrace();
8744 vikram.rag 316
				continue;
8739 vikram.rag 317
			}
318
			try {
319
				transactionId =  String.valueOf(transaction_client.createTransaction(txn));
320
			} catch (TransactionServiceException e) {
321
				logger.error("Could not create transaction ");
322
				e.printStackTrace();
8744 vikram.rag 323
				continue;
8739 vikram.rag 324
			} catch (TException e) {
325
				logger.error("Problem with transaction service while creating transaction");
326
				e.printStackTrace();
8744 vikram.rag 327
				continue;
8739 vikram.rag 328
			}
329
			createPayment(user, (new Long(subOrderId)).toString(),lineItem.getTotal_price());
8627 vikram.rag 330
 
8739 vikram.rag 331
			Transaction transaction = null;
332
			try {
333
				transaction = transaction_client.getTransaction(Long.parseLong(transactionId));
334
			} catch (NumberFormatException e) {
335
				logger.error("Problem parsing transaction id " + transactionId);
336
				e.printStackTrace();
8744 vikram.rag 337
				continue;
8739 vikram.rag 338
			} catch (TransactionServiceException e) {
339
				logger.error("Problem getting transaction from service transaction id " + transactionId);
340
				e.printStackTrace();
8744 vikram.rag 341
				continue;
8739 vikram.rag 342
			} catch (TException e) {
343
				logger.error("Problem with transaction service while getting transaction id " + transactionId);
344
				e.printStackTrace();
8744 vikram.rag 345
				continue;
8739 vikram.rag 346
			}
347
			in.shop2020.model.v1.order.Order snapdealorder = transaction.getOrders().get(0);
8744 vikram.rag 348
			try {
349
				inventoryClient.reserveItemInWarehouse(lineItem.getItem_id(), fulfillmentWarehouse.getId(), 1, 
350
				snapdealorder.getId(), snapdealorder.getCreated_timestamp(), snapdealorder.getPromised_shipping_time(), snapdealorder.getLineitems().get(0).getQuantity());
351
			} catch (InventoryServiceException e1) {
352
				logger.error("Problem while reserving item in inventory service" + snapdealorder.getId());
353
				e1.printStackTrace();
354
				continue;
355
			} catch (TException e1) {
356
				logger.error("Problem with inventory service" + snapdealorder.getId());
357
				e1.printStackTrace();
358
				continue;
359
			}
8739 vikram.rag 360
			SnapdealOrder snapdealOrder = new SnapdealOrder();
361
			snapdealOrder.setOrderId(snapdealorder.getId());
362
			snapdealOrder.setSubOrderId(subOrderId);
363
			snapdealOrder.setReferenceCode(referenceCode);
364
			snapdealOrder.setProductName(productName);
365
			snapdealOrder.setSnapdealTxnDate(snapdealTxnDate.getTime());
366
			snapdealOrder.setListingPrice(order.getSellingPricePerItem());
367
			try {
368
				transaction_client.createSnapdealOrder(snapdealOrder);
369
			} catch (TException e) {
370
				logger.error("Could not create Snapdeal Order");
371
				e.printStackTrace();
8744 vikram.rag 372
				continue;
8739 vikram.rag 373
			}
8627 vikram.rag 374
 
8739 vikram.rag 375
		} 
8616 vikram.rag 376
 
377
	}
378
 
8649 vikram.rag 379
	public static void createPayment(User user, String subOrderId, double amount) {
380
		in.shop2020.payments.PaymentService.Client client = null;
381
		try {
382
			client = new PaymentClient().getClient();
383
		} catch (TTransportException e) {
384
			logger.error("Could not connect to payment service ");
385
			e.printStackTrace();
386
		}
387
		long paymentId = 0;
388
		try {
389
			paymentId = client.createPayment(user.getUserId(), amount, SNAPDEAL_GATEWAY_ID, Long.valueOf(transactionId), false);
390
		} catch (NumberFormatException e) {
391
			logger.error("Could not create payment");
392
			e.printStackTrace();
393
		} catch (PaymentException e) {
394
			logger.error("Could not create payment payment exception");
395
			e.printStackTrace();
396
		} catch (TException e) {
397
			logger.error("Could not create payment thrift exception");
398
			e.printStackTrace();
399
		}
400
		try {
401
			client.updatePaymentDetails(paymentId, null, null, null, null, null, null, subOrderId, null, PaymentStatus.AUTHORIZED, null, null);
402
		} catch (PaymentException e) {
403
			logger.error("Could not update payment to order");
404
			e.printStackTrace();
405
		} catch (TException e) {
406
			logger.error("Could not update payment thrift exception");
407
			e.printStackTrace();
408
		}
8627 vikram.rag 409
	}   
410
 
8616 vikram.rag 411
	public static LineItem createLineItem(long itemId, double amount) throws CatalogServiceException, TException {
8627 vikram.rag 412
		LineItem lineItem = new LineItem();
413
		CatalogService.Client catalogClient = new CatalogClient().getClient();
414
		Item item = catalogClient.getItem(itemId);
8616 vikram.rag 415
 
8627 vikram.rag 416
		lineItem.setProductGroup(item.getProductGroup());
417
		lineItem.setBrand(item.getBrand());
418
		lineItem.setModel_number(item.getModelNumber());
419
		lineItem.setModel_name(item.getModelName());
420
		lineItem.setExtra_info(item.getFeatureDescription());
421
		lineItem.setQuantity(1);
422
		lineItem.setItem_id(item.getId());
423
		lineItem.setUnit_weight(item.getWeight());
424
		lineItem.setTotal_weight(item.getWeight());
425
		lineItem.setUnit_price(amount);
426
		lineItem.setTotal_price(amount);
427
 
8739 vikram.rag 428
		if (item.getColor() == null || "NA".equals(item.getColor())) {
8627 vikram.rag 429
			lineItem.setColor("");
430
		} else {
431
			lineItem.setColor(item.getColor());
432
		}
433
		return lineItem;
8616 vikram.rag 434
	}
435
 
436
}