Subversion Repositories SmartDukaan

Rev

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