Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
8961 vikram.rag 1
package in.shop2020.support.controllers;
2
 
3
import in.shop2020.model.v1.catalog.Amazonlisted;
4
import in.shop2020.model.v1.catalog.CatalogService;
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.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.AmazonFbaSalesSnapshot;
14
import in.shop2020.model.v1.order.FlipkartOrder;
15
import in.shop2020.model.v1.order.LineItem;
16
import in.shop2020.model.v1.order.OrderStatus;
17
import in.shop2020.model.v1.order.OrderType;
18
import in.shop2020.model.v1.order.SourceDetail;
19
import in.shop2020.model.v1.order.Transaction;
20
import in.shop2020.model.v1.order.TransactionServiceException;
21
import in.shop2020.model.v1.order.TransactionStatus;
22
import in.shop2020.model.v1.order.TransactionService.Client;
23
import in.shop2020.model.v1.user.User;
24
import in.shop2020.payments.PaymentException;
25
import in.shop2020.payments.PaymentStatus;
26
import in.shop2020.support.utils.ReportsUtils;
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
 
33
import java.io.BufferedInputStream;
34
import java.io.BufferedReader;
35
import java.io.File;
36
import java.io.FileInputStream;
37
import java.io.FileNotFoundException;
38
import java.io.FileOutputStream;
39
import java.io.FileReader;
40
import java.io.FileWriter;
41
import java.io.IOException;
42
import java.io.InputStream;
43
import java.io.Writer;
44
import java.text.ParseException;
45
import java.text.SimpleDateFormat;
46
import java.util.ArrayList;
47
import java.util.Arrays;
48
import java.util.Calendar;
49
import java.util.Collections;
50
import java.util.Date;
51
import java.util.HashMap;
52
import java.util.List;
53
import java.util.Map;
54
 
55
import javax.servlet.ServletContext;
56
import javax.servlet.ServletOutputStream;
57
import javax.servlet.http.HttpServletRequest;
58
import javax.servlet.http.HttpServletResponse;
59
import javax.servlet.http.HttpSession;
60
 
61
import org.apache.commons.io.FileUtils;
62
import org.apache.commons.lang.xwork.StringUtils;
63
import org.apache.poi.hssf.usermodel.HSSFRow;
64
import org.apache.poi.hssf.usermodel.HSSFSheet;
65
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
66
import org.apache.poi.ss.usermodel.Cell;
67
import org.apache.struts2.convention.annotation.InterceptorRef;
68
import org.apache.struts2.convention.annotation.InterceptorRefs;
69
import org.apache.struts2.interceptor.ServletRequestAware;
70
import org.apache.struts2.interceptor.ServletResponseAware;
71
import org.apache.struts2.util.ServletContextAware;
72
import org.apache.thrift.TException;
73
import org.apache.thrift.transport.TTransportException;
74
import org.slf4j.Logger;
75
import org.slf4j.LoggerFactory;
76
 
77
import au.com.bytecode.opencsv.CSVReader;
78
 
79
import com.opensymphony.xwork2.ValidationAwareSupport;
80
 
81
public class FlipkartDashboardController extends ValidationAwareSupport implements ServletRequestAware ,ServletResponseAware, ServletContextAware{
82
	private static Logger logger = LoggerFactory.getLogger(FlipkartDashboardController.class);
83
	private static final int FLIPKART_SOURCE_ID = 8;
8963 vikram.rag 84
	private static final int FLIPKART_GATEWAY_ID = 17;
8962 vikram.rag 85
	private static final int FLIPKART_LOGISTICS_ID = 19;
8961 vikram.rag 86
	private HttpServletRequest request;
87
	private HttpServletResponse response;
88
	private HttpSession session;
89
	private ServletContext context;
90
	private String url;
91
	private File file;
92
	private String errMsg;
93
	private String id;
94
	private static String transactionId;
95
	public String uploadorders(){
96
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
97
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
98
			return "authfail";
99
		}
100
		return "flipkart-upload-orders";
101
	}
102
	public String show() {
103
		logger.info("Before fetching role");
104
		logger.info(request.getSession().toString());
105
		logger.info(ReportsUtils.ROLE);
106
		logger.info(session.getAttribute(ReportsUtils.ROLE).toString());
107
		logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1]);
108
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1])) {
109
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1]);
110
			return "authfail";
111
		}
112
 
113
		if (StringUtils.equals(id, "flipkart-options")){
114
			return "flipkart-options";
115
		}
116
 
117
		return "id";
118
	}
8968 vikram.rag 119
	@SuppressWarnings("unused")
8961 vikram.rag 120
	public void uploadOrdersFile() throws IOException, CatalogServiceException, TException{
121
		File fileToCreate = new File("/tmp/", "Flipkart-Orders.csv");
122
		FileUtils.copyFile(this.file, fileToCreate);
123
		FileInputStream iFile = new FileInputStream(new File(file.getAbsolutePath()));
124
		CSVReader orderfilereader = null;
125
		replacecommas(fileToCreate);
126
		try {
127
			orderfilereader = new CSVReader(new FileReader("/tmp/formatted.csv"),'$');
128
		} catch (FileNotFoundException e) {
129
			// TODO Auto-generated catch block
130
			e.printStackTrace();
131
		}
132
		logger.info("Before Processing orders ");
133
		String [] nextLine;
134
		Writer sb = null;
135
		try {
136
			User user = null;
137
			TransactionClient tsc = null;
138
			SourceDetail sourceDetail = null;
139
			logger.info("Before Fetching sourcedetail");
140
			try {
141
				tsc = new TransactionClient();
142
				sourceDetail = tsc.getClient().getSourceDetail(FLIPKART_SOURCE_ID);
143
				logger.info("Flipkart sourcedetail " + sourceDetail.getEmail() + " " + sourceDetail.getName());
144
			} catch (Exception e) {
145
				logger.error("Unable to establish connection to the transaction service while getting Flipkart Source Detail ", e);
146
			}
147
			try {   
148
				in.shop2020.model.v1.user.UserContextService.Client userClient = new UserClient().getClient();
149
				logger.info("Before Fetching User by Email");
150
				user = userClient.getUserByEmail(sourceDetail.getEmail());
151
				logger.info("User is " + user.getEmail());
152
			} catch (Exception e) {
153
				logger.error("Unable to establish connection to the User service ", e);
154
			}
155
			logger.info("Before iterating orders in file");
156
			while ((nextLine = orderfilereader.readNext()) != null) {
157
				logger.info("Order file entry  ");
158
				String orderId,subOrderId,create_date;
159
				long sku;
160
				logger.info("Processing order " + nextLine[3]+ " " + nextLine[4]);
161
				if(nextLine[3].length()==0 || nextLine[4].length()==0 ){
162
					sb.append(" Could not parse order id " + nextLine[3]+ " " + nextLine[4] + "\n");
163
					logger.info("Processing order " + nextLine[3]+ " " + nextLine[4]);
164
					continue;
165
				}
166
				else{
167
					orderId = nextLine[3];
168
					subOrderId = nextLine[4];
169
				}
170
 
171
				if(nextLine[0].length()!=0){
172
					create_date = nextLine[0];
173
				}
174
				else{
175
					sb.append(orderId+" "+subOrderId + " Could not parse order date" +"\n");
176
					logger.info(orderId+" "+subOrderId + " Could not parse order date");
177
					continue;
178
				}
179
				if(nextLine[5].length()==0){
180
					sb.append(orderId+" "+subOrderId + " Could not parse sku" +"\n");
181
					logger.info(orderId+" "+subOrderId + " Could not parse sku");
182
					continue;
183
				}
184
				else{
185
					sku =  Long.parseLong(nextLine[5]);
186
					logger.info(orderId+" "+subOrderId + " Processing  sku " + sku);
187
				}
188
				if(nextLine[6].length()!=0 && nextLine[6].equalsIgnoreCase("Approved")){
189
					String status = nextLine[6];	
190
				}
191
				else{
192
					if(nextLine[6].length()==0){
193
						sb.append(orderId+" "+subOrderId + " Could not parse status" +"\n");
194
						logger.info(orderId+" "+subOrderId + " Could not parse status" +"\n");
195
					}
196
					continue;
197
				}
198
				double unitSellingPrice,shippingPrice,octroiFee,emiFee;
199
				if(nextLine[7].length()!=0 ){
200
					if(Double.parseDouble(nextLine[7]) > 0){
201
						unitSellingPrice =  Double.parseDouble(nextLine[7]);
202
					}
203
					else{
204
						sb.append(orderId+" "+subOrderId + " Unit Price set to 0 " +"\n");
205
						logger.info(orderId+" "+subOrderId + " Unit Price set to 0 " +"\n");
206
						continue;
207
					}
208
				}
209
				else{
210
					sb.append(orderId+" "+subOrderId + " Unit Price not set " +"\n");
211
					logger.info(orderId+" "+subOrderId + " Unit Price not set " +"\n");
212
					continue;
213
				}
214
				if(nextLine[8].length()!=0){
215
					shippingPrice  =  Double.parseDouble(nextLine[8]);
216
				}
217
				else{
218
					shippingPrice=0;
219
				}
220
				if(nextLine[9].length()!=0){
221
					octroiFee =  Double.parseDouble(nextLine[9]);
222
					if(octroiFee >0){
223
						sb.append(orderId+" "+subOrderId + " OctroiFee :"+ octroiFee +"\n");
224
						logger.info(orderId+" "+subOrderId + " OctroiFee :"+ octroiFee +"\n");
225
					}
226
				}
227
				else{
228
					octroiFee=0;
229
				}
230
				if(nextLine[10].length()!=0){
231
					emiFee =  Double.parseDouble(nextLine[10]);
232
					if(emiFee >0){
233
						sb.append(orderId+" "+subOrderId + " EMI :"+ emiFee +"\n");
234
						logger.info(orderId+" "+subOrderId + " EMI :"+ emiFee +"\n");
235
					}
236
				}
237
				else{
238
					emiFee = 0;
239
				}
240
				int quantity;
241
				if(nextLine[11].length()!=0){
242
					quantity = Integer.parseInt(nextLine[11]);
243
					if(quantity > 1){
244
						sb.append(orderId+" "+subOrderId + " Quantity > 1 " +"\n");
245
						logger.info(orderId+" "+subOrderId + " Quantity > 1 " +"\n");
246
					}
247
					else{
248
						if(quantity==0){
249
							sb.append(orderId+" "+subOrderId + " Quantity not set " +"\n");
250
							logger.info(orderId+" "+subOrderId + " Quantity not set " +"\n");
251
							continue;
252
						}
253
					}
254
				}
255
				else{
256
					sb.append(orderId+" "+subOrderId + " Quantity not set " +"\n");
257
					logger.info(orderId+" "+subOrderId + " Quantity not set " +"\n");
258
					continue;
259
				}
260
				double totalsellingPrice; 
261
				if(nextLine[12].length()!=0){
262
					totalsellingPrice= Double.parseDouble(nextLine[12]);
263
					if(totalsellingPrice==0){
264
						sb.append(orderId+" "+subOrderId + " Total Selling Price set to 0 " +"\n");
265
						logger.info(orderId+" "+subOrderId + " Total Selling Price set to 0 " +"\n");
266
						continue;
267
					}
268
				}
269
				else{
270
					sb.append(orderId+" "+subOrderId + " Total Selling Price not set " +"\n");
271
					logger.info(orderId+" "+subOrderId + " Total Selling Price not set " +"\n");
272
					continue;
273
				}
274
				//String buyerName = nextLine[17];
275
				String shipToName,addressLine1,addressLine2,city,state,pincode;
276
				if(nextLine[18].length()>0){
277
					shipToName = nextLine[18];
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(nextLine[19].length()>0){
285
					addressLine1 = nextLine[19];
286
				}
287
				else{
288
					addressLine1 ="";
289
				}
290
				if(nextLine[20].length()>0){
291
					addressLine2 = nextLine[20];
292
				}
293
				else{
294
					addressLine2 ="";
295
				}
296
				if(nextLine[21].length()>0){
297
					city = nextLine[21];
298
				}
299
				else{
300
					sb.append(orderId+" "+subOrderId + " City not set " +"\n");
301
					logger.info(orderId+" "+subOrderId + " City not set " +"\n");
302
					continue;
303
				}
304
				if(nextLine[22].length()>0){
305
					state = nextLine[22];
306
				}
307
				else{
308
					sb.append(orderId+" "+subOrderId + " State not set " +"\n");
309
					logger.info(orderId+" "+subOrderId + " State not set " +"\n");
310
					continue;
311
				}
312
				if(nextLine[23].length()>0){
313
					pincode = nextLine[23];
314
				}
315
				else{
316
					sb.append(orderId+" "+subOrderId + " Pincode not set " +"\n");
317
					logger.info(orderId+" "+subOrderId + " Pincode not set " +"\n");
318
					continue;
319
				}
320
				int sla; 
321
				if(nextLine[23].length()>0){
322
					sla = Integer.parseInt(nextLine[24]);
323
				}
324
				else{
325
					sb.append(orderId+" "+subOrderId + " Ship to date not available " +"\n");
326
					logger.info(orderId+" "+subOrderId + " Ship to date not available " +"\n");
327
					continue;
328
				}
329
				//String shipByDate = nextLine[26];
330
				Client transaction_client = null;
331
				try {
332
					transaction_client = new TransactionClient().getClient();
333
					if(transaction_client.flipkartOrderExists(orderId,subOrderId)) {
334
						logger.error("Flipkart order exists " + "id : " + orderId + " suborder id : ");
335
						continue;
336
					}
337
 
338
				} catch (TTransportException e1) {
339
					logger.error("Problem with Transaction service " , e1);
340
					e1.printStackTrace();
341
				} catch (TException e) {
342
					logger.error("Problem.. thrift exception with Transaction service " , e);
343
					e.printStackTrace();
344
				}
345
				SimpleDateFormat istFormatter = new SimpleDateFormat("MMM dd, yyyy");
346
				Date flipkartTxnDate = null;
347
				try {
348
					create_date = create_date.replaceAll("\"","");
349
					flipkartTxnDate = istFormatter.parse(create_date);
350
				} catch (ParseException e) {
351
					logger.error(orderId+" "+subOrderId + " Could not parse flipkart order date from file " , e);
352
					sb.append(orderId+" "+subOrderId + " Could not parse order date" +"\n");
353
					continue;
354
				}
355
				Transaction txn = new Transaction();
356
				txn.setShoppingCartid(user.getActiveCartId());
357
				txn.setCustomer_id(user.getUserId());
358
				System.out.println("User Id is " + user.getUserId());
359
				txn.setCreatedOn(new Date().getTime());
360
				txn.setTransactionStatus(TransactionStatus.INIT);
361
				txn.setStatusDescription("Order for flipkart ");
362
				List<in.shop2020.model.v1.order.Order> orderlist = new ArrayList<in.shop2020.model.v1.order.Order>();
363
				double total_price=0;
364
				InventoryService.Client inventoryClient = null;
365
				Warehouse fulfillmentWarehouse= null;
366
				for(int i=0;i<quantity;i++){
367
					LineItem lineItem = null;
368
					lineItem = createLineItem(sku,unitSellingPrice);
8965 vikram.rag 369
					logger.info(orderId+" "+subOrderId + "sku and Price " + sku + " " + unitSellingPrice);
8961 vikram.rag 370
					lineItem.setExtra_info("flipkartOrderId = " + orderId + " flipkartsubOrderId = " + subOrderId);
371
					in.shop2020.model.v1.order.Order t_order = new in.shop2020.model.v1.order.Order();
372
					t_order.setCustomer_id(user.getUserId());
373
					t_order.setCustomer_email(sourceDetail.getEmail());
374
					t_order.setCustomer_name(shipToName);
375
					addressLine1 = addressLine1.replaceAll("\"",""); 
376
					t_order.setCustomer_address1(addressLine1);
377
					addressLine2 = addressLine2.replaceAll("\"","");
378
					t_order.setCustomer_address2(addressLine2);
379
					t_order.setCustomer_city(city);
380
					t_order.setCustomer_state(state);
381
					t_order.setCustomer_pincode(pincode);
382
					t_order.setTotal_amount(unitSellingPrice);            
383
					t_order.setTotal_weight(lineItem.getTotal_weight());
384
					t_order.setLineitems(Collections.singletonList(lineItem));            
385
					t_order.setStatus(OrderStatus.PAYMENT_PENDING);
386
					t_order.setStatusDescription("Payment Pending");
387
					t_order.setCreated_timestamp(new Date().getTime());
388
					t_order.setOrderType(OrderType.B2C);
389
					t_order.setCod(false);
390
					try {
391
						Date shipDate = new Date();
392
						shipDate.setTime( flipkartTxnDate.getTime() + sla*24*60*60*1000);
393
						Calendar time = Calendar.getInstance();
394
						t_order.setPromised_shipping_time(shipDate.getTime());
395
						t_order.setExpected_shipping_time(shipDate.getTime());
396
						time.add(Calendar.DAY_OF_MONTH, 4);
397
						t_order.setPromised_delivery_time(time.getTimeInMillis());
398
						t_order.setExpected_delivery_time(time.getTimeInMillis());
399
					} catch(Exception e) {	
400
						logger.error("Error in updating Shipping or Delivery Time for suborderid  " + subOrderId);
401
						sb.append(orderId + " "+ subOrderId  + " Could not update delivery time" + " " + "\n");
402
						continue;
403
					}
404
					try {
405
						inventoryClient = new InventoryClient().getClient();
406
						List<Long> itemAvailability = inventoryClient.getItemAvailabilityAtLocation(sku, 1);
407
						fulfillmentWarehouse = inventoryClient.getWarehouse(itemAvailability.get(0));
408
						if(fulfillmentWarehouse.getStateId()!=0){
409
							fulfillmentWarehouse = inventoryClient.getWarehouse(7);
410
						}
411
						t_order.setFulfilmentWarehouseId(fulfillmentWarehouse.getId());
412
						long billingWarehouseId = 0;
413
						if(fulfillmentWarehouse.getBillingWarehouseId()== 0) {
414
							inventoryClient = new InventoryClient().getClient();
415
							List<Warehouse> warehouses = inventoryClient.getWarehouses(WarehouseType.OURS, InventoryType.GOOD, fulfillmentWarehouse.getVendor().getId(), 0, 0);
416
							for(Warehouse warehouse : warehouses) {
417
								if(warehouse.getBillingWarehouseId()!=0) {
418
									billingWarehouseId = warehouse.getBillingWarehouseId();
419
									break;
420
								}
421
							}
422
						}else {
423
							billingWarehouseId = fulfillmentWarehouse.getBillingWarehouseId();
424
						}
425
 
426
						t_order.setWarehouse_id(billingWarehouseId);
427
						VendorItemPricing vendorItemPricing = new VendorItemPricing();
428
						if(fulfillmentWarehouse.getId()==7) {
429
							Item item = new CatalogClient().getClient().getItem(lineItem.getItem_id());
430
							vendorItemPricing = inventoryClient.getItemPricing(lineItem.getItem_id(), item.getPreferredVendor());
431
						} else {
432
							vendorItemPricing = inventoryClient.getItemPricing(lineItem.getItem_id(), fulfillmentWarehouse.getVendor().getId());
433
						}
434
 
435
						t_order.getLineitems().get(0).setTransfer_price(vendorItemPricing.getTransferPrice());
436
						t_order.getLineitems().get(0).setNlc(vendorItemPricing.getNlc());
437
					} catch (InventoryServiceException e) {
438
						logger.error("Error connecting inventory service for suborderid  " + orderId + " " + subOrderId , e);
439
						sb.append(orderId + " " + subOrderId+ " Inventory Service Exception" + " " + "\n");
440
						continue;
441
					} catch (TTransportException e) {
442
						logger.error("Transport Exception with Inventory Service for suborderid  " + orderId + " " + subOrderId , e);
443
						sb.append(orderId + " " + subOrderId + " Transport Exception with Inventory Service" + " " + "\n");
444
						continue;
445
					} catch (TException e) {
446
						logger.error("Exception with Inventory Service for suborderid  " + orderId + " " + subOrderId , e);
447
						sb.append(orderId + " " + subOrderId + " Exception in Inventory Service" + " " + "\n");
448
						continue;
449
					} catch (CatalogServiceException e) {
450
						logger.error("Exception with Catalog Service for   " + orderId + " " + subOrderId + " while getting item " + lineItem.getItem_id(), e);
451
						sb.append(orderId + " " + subOrderId + " Exception in Catalog Service" + " " + "\n");
452
						continue;
453
					}
8962 vikram.rag 454
					t_order.setLogistics_provider_id(FLIPKART_LOGISTICS_ID);
8961 vikram.rag 455
					t_order.setAirwaybill_no("");
456
					t_order.setTracking_id("");
457
					t_order.setTotal_amount(unitSellingPrice);
458
					t_order.setOrderType(OrderType.B2C);
459
					t_order.setSource(FLIPKART_SOURCE_ID);
460
					t_order.setOrderType(OrderType.B2C);
461
					total_price = total_price + unitSellingPrice;
462
					orderlist.add(t_order);
463
				}
464
				txn.setOrders(orderlist);
465
				try {
466
					transactionId =  String.valueOf(transaction_client.createTransaction(txn));
8965 vikram.rag 467
					logger.info("Transaction id is : " + transactionId);
8961 vikram.rag 468
				} catch (TransactionServiceException e) {
469
					logger.error(orderId+" "+subOrderId + " Could not create transaction " , e);
470
					sb.append(orderId+" "+subOrderId + " Could not create transaction" +"\n");
471
					logger.info(orderId+" "+subOrderId + " Could not create transaction" +"\n");
472
					continue;
473
				} catch (TException e) {
474
					logger.error("Problem with transaction service while creating transaction", e);
475
					sb.append(orderId+" "+subOrderId + " Transaction Service Exception could not create transaction" +"\n");
476
					logger.info(orderId+" "+subOrderId + " Transaction Service Exception could not create transaction" +"\n");
477
					continue;
478
				}
479
				try{
8965 vikram.rag 480
					logger.info("Creating payment for suborder id " + subOrderId);
8964 vikram.rag 481
					createPayment(user,subOrderId,unitSellingPrice);
8961 vikram.rag 482
				}
483
				catch (NumberFormatException e) {
484
					logger.error("Could not create payment",e);
485
					sb.append(orderId+" "+subOrderId + " Could not create payment");
486
					e.printStackTrace();
487
					continue;
488
				} catch (PaymentException e) {
489
					logger.error("Could not create payment payment exception",e);
490
					sb.append(orderId+" "+subOrderId + " Could not create payment Payment exception");
491
					e.printStackTrace();
492
					continue;
493
				} catch (TException e) {
494
					logger.error("Could not create payment thrift exception",e);
495
					sb.append(orderId+" "+subOrderId + " Could not create payment Thrift exception");
496
					e.printStackTrace();
497
					continue;
498
				}
499
				Transaction transaction = null;
500
				try {
501
					transaction = transaction_client.getTransaction(Long.parseLong(transactionId));
502
				} catch (NumberFormatException e) {
503
					logger.error("Problem parsing transaction id " + transactionId);
504
					sb.append(orderId+" "+subOrderId  + " Problem parsing transaction id "+ transactionId +"\n");
505
					e.printStackTrace();
506
					continue;
507
				} catch (TransactionServiceException e) {
508
					logger.error("Problem getting transaction from service transaction id " + transactionId);
509
					sb.append(orderId+" "+subOrderId  + " Problem getting transaction id "+ transactionId +"\n");
510
					e.printStackTrace();
511
					continue;
512
				} catch (TException e) {
513
					logger.error("Problem with transaction service while getting transaction id " + transactionId);
514
					sb.append(orderId+" "+subOrderId + " Problem with transaction service while getting transaction id "+ transactionId +"\n");
515
					e.printStackTrace();
516
					continue;
517
				}
518
				List<in.shop2020.model.v1.order.Order> flipkartorders = transaction.getOrders();
519
				for(in.shop2020.model.v1.order.Order flipkartorder:flipkartorders){
520
					try {
521
						inventoryClient.reserveItemInWarehouse(flipkartorder.getLineitems().get(0).getItem_id(), fulfillmentWarehouse.getId(), 1, 
522
								flipkartorder.getId(), flipkartorder.getCreated_timestamp(), flipkartorder.getPromised_shipping_time(), flipkartorder.getLineitems().get(0).getQuantity());
523
						FlipkartOrder flipkartOrder = new FlipkartOrder();
524
						flipkartOrder.setOrderId(flipkartorder.getId());
525
						flipkartOrder.setFlipkartOrderId(orderId);
526
						flipkartOrder.setFlipkartSubOrderId(subOrderId);
527
						flipkartOrder.setFlipkartTxnDate(flipkartTxnDate.getTime());
528
						flipkartOrder.setEmiFee(emiFee);
529
						flipkartOrder.setOctroiFee(octroiFee);
530
						flipkartOrder.setShippingPrice(shippingPrice);
531
						try {
532
							transaction_client.createFlipkartOrder(flipkartOrder);
533
						} catch (TException e) {
534
							logger.error("Could not create flipkart order");
535
							sb.append(orderId+" "+subOrderId + " Could not create flipkart order"+"\n");
536
							continue;
537
						}
538
 
539
					} catch (InventoryServiceException e1) {
540
						logger.error("Problem while reserving item in inventory service" + flipkartorder.getId());
541
						sb.append(orderId+" "+subOrderId + " Could not reserve inventory for sku "+ sku +"\n");
542
						continue;
543
					} catch (TException e1) {
544
						logger.error("Problem with inventory service" + flipkartorder.getId());
545
						sb.append(orderId+" "+subOrderId + " Problem with inventory service while reserving inventory for sku "+ sku +"\n");
546
						continue;
547
					}
548
				}
549
			}
550
			response.setHeader("Content-Type", "text/javascript");
551
 
552
			ServletOutputStream sos;
553
			try {
554
				sos = response.getOutputStream();
8968 vikram.rag 555
				if(sb!=null){
556
					sos.write(sb.toString().getBytes());
557
				}
558
				else{
559
					sos.write("Orders uploaded successfully".toString().getBytes());
560
				}
8961 vikram.rag 561
				sos.flush();
562
			} catch (IOException e) {
563
				System.out.println("Unable to stream the manifest file");
564
			}   
565
 
566
		}
567
		catch (FileNotFoundException e) {
568
			// TODO Auto-generated catch block
569
			e.printStackTrace();
570
		}
571
 
572
 
573
	}
574
 
575
	public static Logger getLogger() {
576
		return logger;
577
	}
578
	public static void setLogger(Logger logger) {
579
		FlipkartDashboardController.logger = logger;
580
	}
581
	public HttpServletRequest getRequest() {
582
		return request;
583
	}
584
	public void setRequest(HttpServletRequest request) {
585
		this.request = request;
586
	}
587
	public HttpServletResponse getResponse() {
588
		return response;
589
	}
590
	public void setResponse(HttpServletResponse response) {
591
		this.response = response;
592
	}
593
	public HttpSession getSession() {
594
		return session;
595
	}
596
	public void setSession(HttpSession session) {
597
		this.session = session;
598
	}
599
	public ServletContext getContext() {
600
		return context;
601
	}
602
	public void setContext(ServletContext context) {
603
		this.context = context;
604
	}
605
	public String getUrl() {
606
		return url;
607
	}
608
	public void setUrl(String url) {
609
		this.url = url;
610
	}
611
	public File getFile() {
612
		return file;
613
	}
614
	public void setFile(File file) {
615
		this.file = file;
616
	}
617
	public String getErrMsg() {
618
		return errMsg;
619
	}
620
	public void setErrMsg(String errMsg) {
621
		this.errMsg = errMsg;
622
	}
623
	public String getId() {
624
		return id;
625
	}
626
	public void setId(String id) {
627
		this.id = id;
628
	}
629
	@Override
630
	public void setServletContext(ServletContext arg0) {
631
		// TODO Auto-generated method stub
632
 
633
	}
634
	@Override
635
	public void setServletResponse(HttpServletResponse response) {
636
		this.response = response;
637
	}
638
	@Override
639
	public void setServletRequest(HttpServletRequest request) {
640
		this.request = request;
641
		this.session = request.getSession();        
642
 
643
	}
644
	public static LineItem createLineItem(long itemId, double amount) throws CatalogServiceException, TException {
645
		LineItem lineItem = new LineItem();
646
		CatalogService.Client catalogClient = new CatalogClient().getClient();
647
		Item item = catalogClient.getItem(itemId);
648
		if(item.getId()==0){
649
			//in case item id is incorrect..
650
			return null;
651
		}
652
 
653
		lineItem.setProductGroup(item.getProductGroup());
654
		lineItem.setBrand(item.getBrand());
655
		lineItem.setModel_number(item.getModelNumber());
656
		lineItem.setModel_name(item.getModelName());
657
		lineItem.setExtra_info(item.getFeatureDescription());
658
		lineItem.setQuantity(1);
659
		lineItem.setItem_id(item.getId());
660
		lineItem.setUnit_weight(item.getWeight());
661
		lineItem.setTotal_weight(item.getWeight());
662
		lineItem.setUnit_price(amount);
663
		lineItem.setTotal_price(amount);
664
 
665
		if (item.getColor() == null || "NA".equals(item.getColor())) {
666
			lineItem.setColor("");
667
		} else {
668
			lineItem.setColor(item.getColor());
669
		}
670
		return lineItem;
671
	}
672
 
673
	public static void replacecommas (File file) throws IOException {
674
		BufferedReader br = null;
675
		File tempFile = new File("/tmp/formatted.csv");
676
		FileWriter fw = new FileWriter(tempFile);
677
		String line;	
678
		br = new BufferedReader(new FileReader(file));
679
		boolean replace = false;
680
		char [] lineChars; 
681
		while ((line = br.readLine()) != null) {
682
 
683
			if(line.contains("Ordered On")){
684
				continue;
685
			}
686
			else{
687
				lineChars = line.toCharArray();
688
 
689
				int count = 0;
690
				for(int i=0;i<lineChars.length;i++)  
691
				{
692
					if(lineChars[i]=='"'){
693
						count++;
694
					}
695
					if(count%2==1){
696
						replace=false;
697
					}
698
					if(count%2==0){
699
						replace=true;
700
					}
701
					if(replace && lineChars[i] == ','){
702
						lineChars[i]='$';
703
					}
704
				}
705
			}
706
			line = String.valueOf(lineChars);
707
			fw.write(line+"\n");
708
		}
709
		if (br != null)br.close();
710
		if (fw != null)fw.close();
711
	}		
712
 
8966 vikram.rag 713
	public static void createPayment(User user, String subOrderId, double amount) throws PaymentException, TException {
8961 vikram.rag 714
		in.shop2020.payments.PaymentService.Client client = new PaymentClient().getClient();
8967 vikram.rag 715
		logger.info("Creating payment for user id " + user.getUserId() + " Gateway id " + FLIPKART_GATEWAY_ID);
8966 vikram.rag 716
		logger.info("Long value of transaction id : " + Long.valueOf(transactionId));
8961 vikram.rag 717
		long paymentId = client.createPayment(user.getUserId(), amount, FLIPKART_GATEWAY_ID, Long.valueOf(transactionId), false);
8966 vikram.rag 718
		logger.info("transaction id : " + Long.valueOf(transactionId) + " Payment id : " + paymentId);
8961 vikram.rag 719
		client.updatePaymentDetails(paymentId, null, null, null, null, null, null, subOrderId, null, PaymentStatus.AUTHORIZED, null, null);
720
	}   
721
 
722
 
723
}