Subversion Repositories SmartDukaan

Rev

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