Subversion Repositories SmartDukaan

Rev

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