Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
9073 vikram.rag 1
package in.shop2020.serving.services;
2
 
3
import in.shop2020.model.v1.catalog.CatalogService;
4
import in.shop2020.model.v1.catalog.CatalogServiceException;
5
import in.shop2020.model.v1.catalog.Item;
6
import in.shop2020.model.v1.catalog.SnapdealItem;
7
import in.shop2020.model.v1.inventory.InventoryService;
8
import in.shop2020.model.v1.inventory.InventoryServiceException;
9
import in.shop2020.model.v1.inventory.InventoryType;
10
import in.shop2020.model.v1.inventory.VendorItemPricing;
11
import in.shop2020.model.v1.inventory.Warehouse;
12
import in.shop2020.model.v1.inventory.WarehouseType;
13
import in.shop2020.model.v1.order.FlipkartOrder;
14
import in.shop2020.model.v1.order.LineItem;
15
import in.shop2020.model.v1.order.OrderStatus;
16
import in.shop2020.model.v1.order.OrderType;
17
import in.shop2020.model.v1.order.SourceDetail;
18
import in.shop2020.model.v1.order.Transaction;
19
import in.shop2020.model.v1.order.TransactionServiceException;
20
import in.shop2020.model.v1.order.TransactionStatus;
21
import in.shop2020.model.v1.order.TransactionService.Client;
22
import in.shop2020.model.v1.user.User;
23
import in.shop2020.payments.PaymentException;
24
import in.shop2020.payments.PaymentStatus;
25
import in.shop2020.serving.model.Order;
26
import in.shop2020.serving.model.OrderItems;
27
import in.shop2020.thrift.clients.CatalogClient;
28
import in.shop2020.thrift.clients.InventoryClient;
29
import in.shop2020.thrift.clients.PaymentClient;
30
import in.shop2020.thrift.clients.TransactionClient;
31
import in.shop2020.thrift.clients.UserClient;
32
import in.shop2020.utils.GmailUtils;
33
import java.io.BufferedReader;
34
import java.io.File;
35
import java.io.FileInputStream;
36
import java.io.IOException;
37
import java.io.InputStreamReader;
38
import java.text.ParseException;
39
import java.text.SimpleDateFormat;
40
import java.util.ArrayList;
41
import java.util.Calendar;
42
import java.util.Collections;
43
import java.util.Date;
44
import java.util.List;
45
import org.apache.http.client.HttpClient;
46
import org.apache.http.client.entity.UrlEncodedFormEntity;
47
import org.apache.http.client.methods.HttpGet;
48
import org.apache.http.client.methods.HttpPost;
49
import org.apache.http.impl.client.DefaultHttpClient;
50
import org.apache.http.message.BasicNameValuePair;
51
import org.apache.http.HttpResponse;
52
import org.apache.http.NameValuePair;
53
import org.apache.thrift.TException;
54
import org.apache.thrift.transport.TTransportException;
55
import org.slf4j.Logger;
56
import org.slf4j.LoggerFactory;
57
import com.google.gson.Gson;
58
 
59
 
60
 
61
public class FetchNewFlipCartOrders {
62
	private static final long FLIPKART_SOURCE_ID = 8;
63
	private static final int FLIPKART_GATEWAY_ID = 17;
64
	private static final int FLIPKART_LOGISTICS_ID = 19;
65
	private static String transactionId;
66
 
67
	private static Logger logger = LoggerFactory.getLogger(FetchNewFlipCartOrders .class);	
68
	public static void main(String[] args) throws  CatalogServiceException, TException, IOException {
69
		HttpClient client = new DefaultHttpClient();
70
		HttpPost post = new HttpPost("https://seller.flipkart.com/login");
71
		//HttpGet get = new HttpGet("https://seller.flipkart.com/dashboard/som/shipped_order_items?status=ready_to_ship%2Cdispatched%2Cshipped%2Cdispute_created%2Cdispute_closed%2Creturn_cancelled%2Cdelivered&page=1&page_size=25&_=1386575219023");
72
		HttpGet get = new HttpGet("https://seller.flipkart.com/dashboard/som/new_order_items?status=on_hold%2Capproved.payment_approved&sort=confirm_by_date&page=1&page_size=25&_=1386574724458");
73
		BufferedReader rd= null;
74
		try {
75
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
76
			nameValuePairs.add(new BasicNameValuePair("authName",
77
			"flipkart"));
78
			nameValuePairs.add(new BasicNameValuePair("username",
79
			"flipkart-support@saholic.com"));
80
			nameValuePairs.add(new BasicNameValuePair("password",
81
			"076c27ee24d7596b06608a8ed2559f87"));
9075 vikram.rag 82
			post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
9073 vikram.rag 83
			HttpResponse response = client.execute(post);
84
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
85
			String line = "";
86
			while ((line = rd.readLine()) != null) {
87
				System.out.println(line);
88
			}
89
			response = client.execute(get);
90
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
91
			line = "";
92
			/*while ((line = rd.readLine()) != null) {
93
        System.out.println(line);
94
      }*/
95
		} catch (IOException e) {
96
			e.printStackTrace();
97
		}
98
		Gson gson = new Gson();
99
		OrderItems orders = (OrderItems) gson.fromJson(rd, OrderItems.class);
100
		processOrders(orders.getOrder_items());
101
		/*for(Order order:orders.getOrder_items()){
102
			System.out.println("Order  " + order.getSku());
103
			System.out.println("ID  " + order.getOrderItemId());
104
			String create_date = order.getCreatedDate()+" "+order.getCreatedTime();
105
			System.out.println("Order Created date "+ create_date);
106
			System.out.println("Customer Name "+ order.getCustomerName());
107
			System.out.println("Total Price "+ order.getTotalPrice());
108
			System.out.println("Unit Price "+ order.getListPrice());
109
			System.out.println("Shipping Address Name "+ order.getShippingAddressName());
110
			System.out.println("Shipping Address "+ order.getShippingAddressLine1() +" "+order.getShippingAddressLine2()+
111
					" "+order.getShippingAddressCity()+" "+order.getShippingAddressState()+" "+order.getShippingAddressPincode());
112
 
113
		}*/
114
 
115
	}
116
 
117
	public static void processOrders(List<Order> orders) throws IOException, CatalogServiceException, TException{
118
		logger.info("Before Processing orders ");
119
		StringBuffer sb = new StringBuffer();
120
		int orders_processed = 0;
121
		String order_string = "";
122
		User user = null;
123
		TransactionClient tsc = null;
124
		SourceDetail sourceDetail = null;
125
		logger.info("Before Fetching sourcedetail");
126
		try {
127
			tsc = new TransactionClient();
128
			sourceDetail = tsc.getClient().getSourceDetail(FLIPKART_SOURCE_ID);
129
			logger.info("Flipkart sourcedetail " + sourceDetail.getEmail() + " " + sourceDetail.getName());
130
		} catch (Exception e) {
131
			logger.error("Unable to establish connection to the transaction service while getting Flipkart Source Detail ", e);
132
		}
133
		try {   
134
			in.shop2020.model.v1.user.UserContextService.Client userClient = new UserClient().getClient();
135
			logger.info("Before Fetching User by Email");
136
			user = userClient.getUserByEmail(sourceDetail.getEmail());
137
			logger.info("User is " + user.getEmail());
138
		} catch (Exception e) {
139
			logger.error("Unable to establish connection to the User service ", e);
140
		}
141
		logger.info("Before iterating orders in file");
142
		for(Order order : orders){
143
 
144
			String orderId,subOrderId,create_date;
145
			long sku;
146
 
147
			if(order.getExternalId().length()==0 || order.getOrderItemId().length()==0 ){
148
				sb.append(" Could not parse order id " + order.getExternalId()+ " " + order.getExternalId() + "\n");
149
				continue;
150
			}
151
			else{
152
				logger.info("Processing Order  " +  order.getExternalId() + " " + order.getOrderItemId());
153
				orderId = order.getExternalId();
154
				subOrderId = order.getOrderItemId();
155
			}
156
 
157
			if(order.getCreatedDate()!=null){
158
				create_date = order.getCreatedDate()+" "+order.getCreatedTime();
159
			}
160
			else{
161
				sb.append(orderId+" "+subOrderId + " Could not parse order date" +"\n");
162
				logger.info(orderId+" "+subOrderId + " Could not parse order date");
163
				continue;
164
			}
165
			if(order.getSku()==0L){
166
				sb.append(orderId+" "+subOrderId + " Could not parse sku" +"\n");
167
				logger.info(orderId+" "+subOrderId + " Could not parse sku");
168
				continue;
169
			}
170
			else{
171
				sku =  order.getSku();
172
				logger.info(orderId+" "+subOrderId + " Processing  sku " + sku);
173
			}
174
			if(order.getStatus().length()!=0 && order.getStatus().equalsIgnoreCase("Approved")){
175
				String status = order.getStatus();	
176
			}
177
			else{
178
				if(order.getStatus().length()==0){
179
					sb.append(orderId+" "+subOrderId + " Could not parse status " + "\n");
180
					logger.info(orderId+" "+subOrderId + " Could not parse status " +"\n");
181
				}
182
				else{
183
					sb.append(orderId+" "+subOrderId + " Order not Approved" +"\n");
184
					logger.info(orderId+" "+subOrderId + " Order not Approved" +"\n");
185
				}
186
				continue;
187
			}
188
			double unitSellingPrice,shippingPrice,octroiFee,emiFee;
189
			if(order.getListPrice()!=0 ){
190
				if(order.getListPrice() > 0){
191
					unitSellingPrice =  order.getListPrice();
192
				}
193
				else{
194
					sb.append(orderId+" "+subOrderId + " Unit Price set to 0 " +"\n");
195
					logger.info(orderId+" "+subOrderId + " Unit Price set to 0 " +"\n");
196
					continue;
197
				}
198
			}
199
			else{
200
				sb.append(orderId+" "+subOrderId + " Unit Price not set " +"\n");
201
				logger.info(orderId+" "+subOrderId + " Unit Price not set " +"\n");
202
				continue;
203
			}
204
			if(order.getShippingFees() > 0){
205
				shippingPrice  =  order.getShippingFees();
206
				sb.append(orderId+" "+subOrderId + " Shipping Fee :"+ shippingPrice +"\n");
207
				logger.info(orderId+" "+subOrderId + " Shipping Fee :"+ shippingPrice +"\n");
208
 
209
			}
210
			else{
211
				shippingPrice=0;
212
			}
213
			if(order.getOctroi()!=0){
214
				octroiFee =  order.getOctroi();
215
				if(octroiFee >0){
216
					sb.append(orderId+" "+subOrderId + " OctroiFee :"+ octroiFee +"\n");
217
					logger.info(orderId+" "+subOrderId + " OctroiFee :"+ octroiFee +"\n");
218
				}
219
			}
220
			else{
221
				octroiFee=0;
222
			}
223
			if(order.getEmi()!=0){
224
				emiFee =  order.getEmi();
225
				if(emiFee >0){
226
					sb.append(orderId+" "+subOrderId + " EMI :"+ emiFee +"\n");
227
					logger.info(orderId+" "+subOrderId + " EMI :"+ emiFee +"\n");
228
				}
229
			}
230
			else{
231
				emiFee = 0;
232
			}
233
			int quantity;
234
			if(order.getQuantity()!=0){
235
				quantity = order.getQuantity();
236
				if(quantity > 1){
237
					sb.append(orderId+" "+subOrderId + " Quantity > 1 " +quantity+"\n");
238
					logger.info(orderId+" "+subOrderId + " Quantity > 1 " +quantity+"\n");
239
				}
240
				else{
241
					if(quantity==0){
242
						sb.append(orderId+" "+subOrderId + " Quantity not set " +quantity+"\n");
243
						logger.info(orderId+" "+subOrderId + " Quantity not set " +quantity+"\n");
244
						continue;
245
					}
246
				}
247
			}
248
			else{
249
				sb.append(orderId+" "+subOrderId + " Quantity not set " + "0" +"\n");
250
				logger.info(orderId+" "+subOrderId + " Quantity not set " +"0"+"\n");
251
				continue;
252
			}
253
			double totalsellingPrice; 
254
			if(order.getTotalPrice()!=0){
255
				totalsellingPrice= order.getTotalPrice();
256
				if(totalsellingPrice==0){
257
					sb.append(orderId+" "+subOrderId + " Total Selling Price set to 0 " +"\n");
258
					logger.info(orderId+" "+subOrderId + " Total Selling Price set to 0 " +"\n");
259
					continue;
260
				}
261
			}
262
			else{
263
				sb.append(orderId+" "+subOrderId + " Total Selling Price not set " +"\n");
264
				logger.info(orderId+" "+subOrderId + " Total Selling Price not set " +"\n");
265
				continue;
266
			}
267
 
268
			String shipToName,addressLine1,addressLine2,city,state,pincode,buyerName="unknown";
269
			if(order.getCustomerName().length() > 0){
270
				buyerName = order.getCustomerName();
271
			}
272
			else{
273
				sb.append(orderId+" "+subOrderId + " Buyer Name not set " +"\n");
274
				logger.info(orderId+" "+subOrderId + " Buyer Name not set " +"\n");
275
			}
276
			if(order.getShippingAddressName().length() > 0){
277
				shipToName = order.getShippingAddressName();
278
			}
279
			else{
280
				sb.append(orderId+" "+subOrderId + " Ship to Name not set " +"\n");
281
				logger.info(orderId+" "+subOrderId + " Ship to Name not set " +"\n");
282
				continue;
283
			}
284
			if(buyerName.contains("unknown")){
285
				buyerName = shipToName;
286
			}
287
			if(order.getShippingAddressLine1().length() > 0){
288
				addressLine1 = order.getShippingAddressLine1();
289
			}
290
			else{
291
				addressLine1 ="";
292
			}
293
			if(order.getShippingAddressLine2().length() > 0){
294
				addressLine2 = order.getShippingAddressLine2();
295
			}
296
			else{
297
				addressLine2 ="";
298
			}
299
			if(order.getShippingAddressCity().length()>0){
300
				city = order.getShippingAddressCity();
301
			}
302
			else{
303
				sb.append(orderId+" "+subOrderId + " City not set " +"\n");
304
				logger.info(orderId+" "+subOrderId + " City not set " +"\n");
305
				continue;
306
			}
307
			if(order.getShippingAddressState().length()>0){
308
				state = order.getShippingAddressState();
309
			}
310
			else{
311
				sb.append(orderId+" "+subOrderId + " State not set " +"\n");
312
				logger.info(orderId+" "+subOrderId + " State not set " +"\n");
313
				continue;
314
			}
315
			if(order.getShippingAddressPincode().length()>0){
316
				pincode = order.getShippingAddressPincode();
317
			}
318
			else{
319
				sb.append(orderId+" "+subOrderId + " Pincode not set " +"\n");
320
				logger.info(orderId+" "+subOrderId + " Pincode not set " +"\n");
321
				continue;
322
			}
323
			int sla; 
324
			if(order.getSla()>0){
325
				sla = order.getSla();
326
			}
327
			else{
328
				sb.append(orderId+" "+subOrderId + " Ship to date not available " +"\n");
329
				logger.info(orderId+" "+subOrderId + " Ship to date not available " +"\n");
330
				continue;
331
			}
332
			//String shipByDate = nextLine[26];
333
			Client transaction_client = null;
334
			try {
335
				transaction_client = new TransactionClient().getClient();
336
				if(transaction_client.flipkartOrderExists(orderId,subOrderId)) {
337
					logger.error("Flipkart order exists " + "id : " + orderId + " suborder id : " + subOrderId);
338
					sb.append("Flipkart order exists " + orderId+" "+subOrderId+"\n");
339
					continue;
340
				}
341
 
342
			} catch (TTransportException e1) {
343
				logger.error("Problem with Transaction service " , e1);
344
				e1.printStackTrace();
345
			} catch (TException e) {
346
				logger.error("Problem.. thrift exception with Transaction service " , e);
347
				e.printStackTrace();
348
			}
349
			SimpleDateFormat istFormatter = new SimpleDateFormat("MMM dd, yyyy hh:mm aaa");
350
			Date flipkartTxnDate = null;
351
			try {
352
				create_date = create_date.replaceAll("\"","");
353
				flipkartTxnDate = istFormatter.parse(create_date);
354
			} catch (ParseException e) {
355
				logger.error(orderId+" "+subOrderId + " Could not parse flipkart order date from file " , e);
356
				sb.append(orderId+" "+subOrderId + " Could not parse order date" +"\n");
357
				continue;
358
			}
359
			Transaction txn = new Transaction();
360
			txn.setShoppingCartid(user.getActiveCartId());
361
			txn.setCustomer_id(user.getUserId());
362
			System.out.println("User Id is " + user.getUserId());
363
			txn.setCreatedOn(new Date().getTime());
364
			txn.setTransactionStatus(TransactionStatus.INIT);
365
			txn.setStatusDescription("Order for flipkart ");
366
			List<in.shop2020.model.v1.order.Order> orderlist = new ArrayList<in.shop2020.model.v1.order.Order>();
367
			double total_price=0;
368
			InventoryService.Client inventoryClient = null;
369
			Warehouse fulfillmentWarehouse= null;
370
			boolean exit = false;
371
			for(int i=0;i<quantity;i++){
372
				LineItem lineItem = null;
373
				lineItem = createLineItem(sku,unitSellingPrice);
374
				logger.info(orderId+" "+subOrderId + "sku and Price " + sku + " " + unitSellingPrice);
375
				lineItem.setExtra_info("flipkartOrderId = " + orderId + " flipkartsubOrderId = " + subOrderId);
376
				in.shop2020.model.v1.order.Order t_order = new in.shop2020.model.v1.order.Order();
377
				t_order.setCustomer_id(user.getUserId());
378
				t_order.setCustomer_email(sourceDetail.getEmail());
379
				t_order.setCustomer_name(shipToName);
380
				addressLine1 = addressLine1.replaceAll("\"",""); 
381
				t_order.setCustomer_address1(addressLine1);
382
				addressLine2 = addressLine2.replaceAll("\"","");
383
				t_order.setCustomer_address2(addressLine2);
384
				t_order.setCustomer_city(city);
385
				t_order.setCustomer_state(state);
386
				t_order.setCustomer_pincode(pincode);
387
				t_order.setTotal_amount(unitSellingPrice);            
388
				t_order.setTotal_weight(lineItem.getTotal_weight());
389
				t_order.setLineitems(Collections.singletonList(lineItem));            
390
				t_order.setStatus(OrderStatus.PAYMENT_PENDING);
391
				t_order.setStatusDescription("In Process");
392
				t_order.setCreated_timestamp(new Date().getTime());
393
				t_order.setOrderType(OrderType.B2C);
394
				t_order.setCod(false);
395
				try {
396
					Date shipDate = new Date();
397
					shipDate.setTime( flipkartTxnDate.getTime() + sla*24*60*60*1000);
398
					Calendar time = Calendar.getInstance();
399
					t_order.setPromised_shipping_time(shipDate.getTime());
400
					t_order.setExpected_shipping_time(shipDate.getTime());
401
					time.add(Calendar.DAY_OF_MONTH, 4);
402
					t_order.setPromised_delivery_time(time.getTimeInMillis());
403
					t_order.setExpected_delivery_time(time.getTimeInMillis());
404
				} catch(Exception e) {	
405
					logger.error("Error in updating Shipping or Delivery Time for suborderid  " + subOrderId);
406
					sb.append(orderId + " "+ subOrderId  + " Could not update delivery time" + " " + "\n");
407
					exit = true;
408
					continue;
409
				}
410
				SnapdealItem snapdealItem = new CatalogClient().getClient().getSnapdealItem(sku);
411
				inventoryClient = new InventoryClient().getClient();
412
				try {
413
					logger.info("Snapdeal Item id is " + snapdealItem.getItem_id());
414
					if(snapdealItem.getItem_id()!=0 && snapdealItem.getWarehouseId()!=0) {
415
						logger.info("SnapdealItem Warehouse Id " + snapdealItem.getWarehouseId());
416
						fulfillmentWarehouse = inventoryClient.getWarehouse(snapdealItem.getWarehouseId());
417
						logger.info("fulfillmentWarehouse is " + fulfillmentWarehouse.getId() + " " + fulfillmentWarehouse.getDisplayName() );
418
 
419
					} else {
420
						List<Long> itemAvailability = inventoryClient.getItemAvailabilityAtLocation(sku, 1);
421
						fulfillmentWarehouse = inventoryClient.getWarehouse(itemAvailability.get(0));
422
						if(fulfillmentWarehouse.getStateId()!=0){
423
							fulfillmentWarehouse = inventoryClient.getWarehouse(7);
424
						}
425
					}
426
					t_order.setFulfilmentWarehouseId(fulfillmentWarehouse.getId());
427
					long billingWarehouseId = 0;
428
					if(fulfillmentWarehouse.getBillingWarehouseId()== 0) {
429
						inventoryClient = new InventoryClient().getClient();
430
						List<Warehouse> warehouses = inventoryClient.getWarehouses(WarehouseType.OURS, InventoryType.GOOD, fulfillmentWarehouse.getVendor().getId(), 0, 0);
431
						for(Warehouse warehouse : warehouses) {
432
							if(warehouse.getBillingWarehouseId()!=0) {
433
								billingWarehouseId = warehouse.getBillingWarehouseId();
434
								break;
435
							}
436
						}
437
					}else {
438
						billingWarehouseId = fulfillmentWarehouse.getBillingWarehouseId();
439
					}
440
 
441
					//logger.info("Billing warehouse id for suborderid  " + order.getSuborderId() + " is " + fulfillmentWarehouse.getBillingWarehouseId());
442
					t_order.setWarehouse_id(billingWarehouseId);
443
					VendorItemPricing vendorItemPricing = new VendorItemPricing();
444
					if(fulfillmentWarehouse.getId()==7) {
445
						Item item = new CatalogClient().getClient().getItem(lineItem.getItem_id());
446
						vendorItemPricing = inventoryClient.getItemPricing(lineItem.getItem_id(), item.getPreferredVendor());
447
					} else {
448
						vendorItemPricing = inventoryClient.getItemPricing(lineItem.getItem_id(), fulfillmentWarehouse.getVendor().getId());
449
					}
450
 
451
					t_order.getLineitems().get(0).setTransfer_price(vendorItemPricing.getTransferPrice());
452
					t_order.getLineitems().get(0).setNlc(vendorItemPricing.getNlc());
453
				} catch (InventoryServiceException e) {
454
					logger.error("Error connecting inventory service for suborderid  " + orderId + " " + subOrderId , e);
455
					sb.append(orderId + " " + subOrderId+ " Inventory Service Exception" + " " + "\n");
456
					exit = true;
457
					continue;
458
				} catch (TTransportException e) {
459
					logger.error("Transport Exception with Inventory Service for suborderid  " + orderId + " " + subOrderId , e);
460
					sb.append(orderId + " " + subOrderId + " Transport Exception with Inventory Service" + " " + "\n");
461
					exit = true;
462
					continue;
463
				} catch (TException e) {
464
					logger.error("Exception with Inventory Service for suborderid  " + orderId + " " + subOrderId , e);
465
					sb.append(orderId + " " + subOrderId + " Exception in Inventory Service" + " " + "\n");
466
					exit = true;
467
					continue;
468
				} catch (CatalogServiceException e) {
469
					logger.error("Exception with Catalog Service for   " + orderId + " " + subOrderId + " while getting item " + lineItem.getItem_id(), e);
470
					sb.append(orderId + " " + subOrderId + " Exception in Catalog Service" + " " + "\n");
471
					exit = true;
472
					continue;
473
				}
474
				t_order.setLogistics_provider_id(FLIPKART_LOGISTICS_ID);
475
				t_order.setAirwaybill_no("");
476
				t_order.setTracking_id("");
477
				t_order.setTotal_amount(unitSellingPrice);
478
				t_order.setOrderType(OrderType.B2C);
479
				t_order.setSource(FLIPKART_SOURCE_ID);
480
				t_order.setOrderType(OrderType.B2C);
481
				total_price = total_price + unitSellingPrice;
482
				orderlist.add(t_order);
483
			}
484
			if(exit){
485
				continue;
486
			}
487
			txn.setOrders(orderlist);
488
			try {
489
				transactionId =  String.valueOf(transaction_client.createTransaction(txn));
490
				logger.info("Transaction id is : " + transactionId);
491
			} catch (TransactionServiceException e) {
492
				logger.error(orderId+" "+subOrderId + " Could not create transaction " , e);
493
				sb.append(orderId+" "+subOrderId + " Could not create transaction" +"\n");
494
				continue;
495
			} catch (TException e) {
496
				sb.append(orderId+" "+subOrderId + " Transaction Service Exception could not create transaction" +"\n");
497
				logger.info(orderId+" "+subOrderId + " Transaction Service Exception could not create transaction" +"\n");
498
				continue;
499
			}
500
			try{
501
				logger.info("Creating payment for suborder id " + subOrderId);
502
				createPayment(user,subOrderId,unitSellingPrice);
503
			}
504
			catch (NumberFormatException e) {
505
				logger.error("Could not create payment",e);
506
				sb.append(orderId+" "+subOrderId + " Could not create payment");
507
				e.printStackTrace();
508
				continue;
509
			} catch (PaymentException e) {
510
				logger.error("Could not create payment payment exception",e);
511
				sb.append(orderId+" "+subOrderId + " Could not create payment Payment exception");
512
				e.printStackTrace();
513
				continue;
514
			} catch (TException e) {
515
				logger.error("Could not create payment thrift exception",e);
516
				sb.append(orderId+" "+subOrderId + " Could not create payment Thrift exception");
517
				e.printStackTrace();
518
				continue;
519
			}
520
			Transaction transaction = null;
521
			try {
522
				transaction = transaction_client.getTransaction(Long.parseLong(transactionId));
523
			} catch (NumberFormatException e) {
524
				logger.error("Problem parsing transaction id " + transactionId);
525
				sb.append(orderId+" "+subOrderId  + " Problem parsing transaction id "+ transactionId +"\n");
526
				e.printStackTrace();
527
				continue;
528
			} catch (TransactionServiceException e) {
529
				logger.error("Problem getting transaction from service transaction id " + transactionId);
530
				sb.append(orderId+" "+subOrderId  + " Problem getting transaction id "+ transactionId +"\n");
531
				e.printStackTrace();
532
				continue;
533
			} catch (TException e) {
534
				logger.error("Problem with transaction service while getting transaction id " + transactionId);
535
				sb.append(orderId+" "+subOrderId + " Problem with transaction service while getting transaction id "+ transactionId +"\n");
536
				e.printStackTrace();
537
				continue;
538
			}
539
			List<in.shop2020.model.v1.order.Order> flipkartorders = transaction.getOrders();
540
			for(in.shop2020.model.v1.order.Order flipkartorder:flipkartorders){
541
				try {
542
					List<in.shop2020.model.v1.order.Attribute> attributeList = new ArrayList<in.shop2020.model.v1.order.Attribute>();
543
					inventoryClient.reserveItemInWarehouse(flipkartorder.getLineitems().get(0).getItem_id(), fulfillmentWarehouse.getId(), 1, 
544
							flipkartorder.getId(), flipkartorder.getCreated_timestamp(), flipkartorder.getPromised_shipping_time(), flipkartorder.getLineitems().get(0).getQuantity());
545
					FlipkartOrder flipkartOrder = new FlipkartOrder();
546
					flipkartOrder.setOrderId(flipkartorder.getId());
547
					flipkartOrder.setFlipkartOrderId(orderId);
548
					flipkartOrder.setFlipkartSubOrderId(subOrderId);
549
					flipkartOrder.setFlipkartTxnDate(flipkartTxnDate.getTime());
550
					flipkartOrder.setEmiFee(emiFee);
551
					flipkartOrder.setOctroiFee(octroiFee);
552
					flipkartOrder.setShippingPrice(shippingPrice);
553
					in.shop2020.model.v1.order.Attribute attribute = new in.shop2020.model.v1.order.Attribute();
554
					attribute.setName("Buyer Name");
555
					attribute.setValue(buyerName);
556
					attributeList.add(attribute);
557
					try {
558
						transaction_client.createFlipkartOrder(flipkartOrder);
559
						transaction_client.setOrderAttributes(flipkartOrder.getOrderId(),attributeList);
560
						orders_processed++;
561
					} catch (TException e) {
562
						logger.error("Could not create flipkart order");
563
						sb.append(orderId+" "+subOrderId + " Could not create flipkart order"+"\n");
564
						continue;
565
					}
566
 
567
				} catch (InventoryServiceException e1) {
568
					logger.error("Problem while reserving item in inventory service" + flipkartorder.getId());
569
					sb.append(orderId+" "+subOrderId + " Could not reserve inventory for sku "+ sku +"\n");
570
					continue;
571
				} catch (TException e1) {
572
					logger.error("Problem with inventory service" + flipkartorder.getId());
573
					sb.append(orderId+" "+subOrderId + " Problem with inventory service while reserving inventory for sku "+ sku +"\n");
574
					continue;
575
				}
576
			}
577
		}
578
 
579
		if(orders_processed==1){
580
			order_string = "Order";
581
		}
582
		else{
583
			order_string = "Orders";
584
		}
585
 
586
	}
587
 
588
	public static LineItem createLineItem(long itemId, double amount) throws CatalogServiceException, TException {
589
		LineItem lineItem = new LineItem();
590
		CatalogService.Client catalogClient = new CatalogClient().getClient();
591
		Item item = catalogClient.getItem(itemId);
592
		if(item.getId()==0){
593
			//in case item id is incorrect..
594
			return null;
595
		}
596
 
597
		lineItem.setProductGroup(item.getProductGroup());
598
		lineItem.setBrand(item.getBrand());
599
		lineItem.setModel_number(item.getModelNumber());
600
		lineItem.setModel_name(item.getModelName());
601
		lineItem.setExtra_info(item.getFeatureDescription());
602
		lineItem.setQuantity(1);
603
		lineItem.setItem_id(item.getId());
604
		lineItem.setUnit_weight(item.getWeight());
605
		lineItem.setTotal_weight(item.getWeight());
606
		lineItem.setUnit_price(amount);
607
		lineItem.setTotal_price(amount);
608
 
609
		if (item.getColor() == null || "NA".equals(item.getColor())) {
610
			lineItem.setColor("");
611
		} else {
612
			lineItem.setColor(item.getColor());
613
		}
614
		return lineItem;
615
	}
616
	public static void createPayment(User user, String subOrderId, double amount) throws PaymentException, TException {
617
		in.shop2020.payments.PaymentService.Client client = new PaymentClient().getClient();
618
		logger.info("Creating payment for user id " + user.getUserId() + " Gateway id " + FLIPKART_GATEWAY_ID);
619
		logger.info("Long value of transaction id : " + Long.valueOf(transactionId));
620
		long paymentId = client.createPayment(user.getUserId(), amount, FLIPKART_GATEWAY_ID, Long.valueOf(transactionId), false);
621
		logger.info("transaction id : " + Long.valueOf(transactionId) + " Payment id : " + paymentId);
622
		client.updatePaymentDetails(paymentId, null, null, null, null, null, null, subOrderId, null, PaymentStatus.AUTHORIZED, null, null);
623
	}   
624
 
625
 
626
 
627
}