Subversion Repositories SmartDukaan

Rev

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