Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
10097 kshitij.so 1
package in.shop2020.support.controllers;
2
 
3
import in.shop2020.model.v1.catalog.CatalogService.Client;
4
import in.shop2020.model.v1.catalog.CatalogServiceException;
5
import in.shop2020.model.v1.catalog.FlipkartItem;
6
import in.shop2020.model.v1.catalog.FlipkartItemDetails;
7
import in.shop2020.model.v1.catalog.Item;
8
import in.shop2020.model.v1.catalog.MarketplaceItems;
9
import in.shop2020.model.v1.catalog.MarketplacePercentage;
10
import in.shop2020.model.v1.inventory.InventoryServiceException;
11
import in.shop2020.model.v1.inventory.InventoryType;
12
import in.shop2020.model.v1.inventory.ItemInventory;
13
import in.shop2020.model.v1.inventory.OOSStatus;
14
import in.shop2020.model.v1.inventory.Warehouse;
15
import in.shop2020.model.v1.inventory.WarehouseType;
16
import in.shop2020.support.utils.ReportsUtils;
17
import in.shop2020.thrift.clients.CatalogClient;
18
import in.shop2020.thrift.clients.InventoryClient;
19
import in.shop2020.thrift.clients.TransactionClient;
20
import in.shop2020.utils.GmailUtils;
21
import javax.mail.MessagingException;
22
 
23
import java.io.BufferedInputStream;
24
import java.io.File;
25
import java.io.FileInputStream;
26
import java.io.FileNotFoundException;
27
import java.io.FileOutputStream;
28
import java.io.FileWriter;
29
import java.io.IOException;
30
import java.io.InputStream;
31
import java.text.DecimalFormat;
32
import java.util.ArrayList;
33
import java.util.Arrays;
34
import java.util.HashMap;
35
import java.util.List;
36
import java.util.Map;
37
 
38
import javax.servlet.ServletContext;
39
import javax.servlet.ServletOutputStream;
40
import javax.servlet.http.HttpServletRequest;
41
import javax.servlet.http.HttpServletResponse;
42
import javax.servlet.http.HttpSession;
43
 
44
import org.apache.commons.io.FileUtils;
45
import org.apache.commons.lang.xwork.StringUtils;
46
import org.apache.poi.hssf.usermodel.HSSFRow;
47
import org.apache.poi.hssf.usermodel.HSSFSheet;
48
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
49
import org.apache.poi.ss.usermodel.Cell;
50
import org.apache.struts2.convention.annotation.InterceptorRef;
51
import org.apache.struts2.convention.annotation.InterceptorRefs;
52
import org.apache.struts2.interceptor.ServletRequestAware;
53
import org.apache.struts2.interceptor.ServletResponseAware;
54
import org.apache.struts2.util.ServletContextAware;
55
import org.apache.thrift.TException;
56
import org.apache.thrift.transport.TTransportException;
57
import org.json.JSONException;
58
import org.json.JSONObject;
59
import org.slf4j.Logger;
60
import org.slf4j.LoggerFactory;
61
 
62
import com.opensymphony.xwork2.ValidationAwareSupport;
63
 
64
@SuppressWarnings({"unused","deprecation"})
65
 
66
@InterceptorRefs({
67
	@InterceptorRef("defaultStack"),
68
	@InterceptorRef("login")
69
})
70
 
71
public class FlipkartListController extends ValidationAwareSupport implements ServletRequestAware ,ServletResponseAware, ServletContextAware{
72
	/**
73
	 * 
74
	 */
75
	private static final long serialVersionUID = 1L;
76
 
77
 
78
	private static Logger logger = LoggerFactory.getLogger(FlipkartListController.class);
79
 
80
 
81
	private HttpServletRequest request;
82
	private HttpServletResponse response;
83
	private HttpSession session;
84
	private ServletContext context;
85
	private String url;
86
	private String itemId;
87
	private String exceptionPrice;
88
	private String warehouseId;
89
	private File file;
90
	private String errMsg;
91
	private String next;
92
	private String id;
93
	private String searchText;
94
	private List<FlipkartItemDetails> flipkartItems;
95
	private long searchCount;
96
	private long totalCount;
97
	private String sellingPrice;
98
	private String transferPrice;
99
	private String webisteMrp;
100
	private String webisteSellingPrice;
101
	private String isListedOnFlipkart;
102
	private String commission;
103
	private String serviceTax;
104
	private String courierCost;
105
	private JSONObject itemObj;
106
	private String isSuppressInventoryFeed;
107
	private String isSuppressPriceFeed;
108
	private String maxNlc;
109
	private String fkItemCode;
110
	private String supc;
111
	private String held;
112
	private String lastUpdatedInventoryTimestamp;
113
	private String lastCheckedTimestamp;
114
	private String otherCost;
115
	private String isAutoIncrement;
116
	private String isAutoDecrement;
117
	private String isManualFavourite;
118
	private String isAutoFavourite;
119
	private String currentSp;
120
	private String currentTp;
121
	private String minPosSp;
122
	private String minPosTp;
123
	private String vat;
124
	private String comparsionResult;
125
	private String maxSp;
126
	private String nDaySale;
127
	private String fkSerialNumber;
128
 
129
	public String getFkSerialNumber() {
130
		return fkSerialNumber;
131
	}
132
 
133
	public void setFkSerialNumber(String fkSerialNumber) {
134
		this.fkSerialNumber = fkSerialNumber;
135
	}
136
 
137
	public String getnDaySale() {
138
		return nDaySale;
139
	}
140
 
141
	public void setnDaySale(String nDaySale) {
142
		this.nDaySale = nDaySale;
143
	}
144
 
145
	private String avgSale;
146
 
147
	public String getAvgSale() {
148
		return avgSale;
149
	}
150
 
151
	public void setAvgSale(String avgSale) {
152
		this.avgSale = avgSale;
153
	}
154
 
155
	public String getMaxSp() {
156
		return maxSp;
157
	}
158
 
159
	public void setMaxSp(String maxSp) {
160
		this.maxSp = maxSp;
161
	}
162
 
163
	public String getComparsionResult() {
164
		return comparsionResult;
165
	}
166
 
167
	public void setComparsionResult(String comparsionResult) {
168
		this.comparsionResult = comparsionResult;
169
	}
170
 
171
	private Map<Integer, Long> holdInventoryMap;
172
 
173
	public String getVat() {
174
		return vat;
175
	}
176
 
177
	public void setVat(String vat) {
178
		this.vat = vat;
179
	}
180
 
181
	public String getOtherCost() {
182
		return otherCost;
183
	}
184
 
185
	public void setOtherCost(String otherCost) {
186
		this.otherCost = otherCost;
187
	}
188
 
189
 
190
	public String getCurrentSp() {
191
		return currentSp;
192
	}
193
 
194
	public void setCurrentSp(String currentSp) {
195
		this.currentSp = currentSp;
196
	}
197
 
198
	public String getCurrentTp() {
199
		return currentTp;
200
	}
201
 
202
	public void setCurrentTp(String currentTp) {
203
		this.currentTp = currentTp;
204
	}
205
 
206
 
207
	public String getLastCheckedTimestamp() {
208
		return lastCheckedTimestamp;
209
	}
210
 
211
	public String getLastUpdatedInventoryTimestamp() {
212
		return lastUpdatedInventoryTimestamp;
213
	}
214
 
215
	public void setLastUpdatedInventoryTimestamp(
216
			String lastUpdatedInventoryTimestamp) {
217
		this.lastUpdatedInventoryTimestamp = lastUpdatedInventoryTimestamp;
218
	}
219
 
220
	public void setLastCheckedTimestamp(
221
			String lastCheckedTimestamp) {
222
		this.lastCheckedTimestamp = lastCheckedTimestamp;
223
	}
224
 
225
	public String getIsAutoIncrement() {
226
		return isAutoIncrement;
227
	}
228
 
229
	public void setIsAutoIncrement(String isAutoIncrement) {
230
		this.isAutoIncrement = isAutoIncrement;
231
	}
232
 
233
	public String getIsAutoDecrement() {
234
		return isAutoDecrement;
235
	}
236
 
237
	public void setIsAutoDecrement(String isAutoDecrement) {
238
		this.isAutoDecrement = isAutoDecrement;
239
	}
240
 
241
	public String getIsManualFavourite() {
242
		return isManualFavourite;
243
	}
244
 
245
	public void setIsManualFavourite(String isManualFavourite) {
246
		this.isManualFavourite = isManualFavourite;
247
	}
248
 
249
	public String getIsAutoFavourite() {
250
		return isAutoFavourite;
251
	}
252
 
253
	public void setIsAutoFavourite(String isAutoFavourite) {
254
		this.isAutoFavourite = isAutoFavourite;
255
	}
256
 
257
	public String getMinPosSp() {
258
		return minPosSp;
259
	}
260
 
261
	public void setMinPosSp(String minPosSp) {
262
		this.minPosSp = minPosSp;
263
	}
264
 
265
	public String getMinPosTp() {
266
		return minPosTp;
267
	}
268
 
269
	public void setMinPosTp(String minPosTp) {
270
		this.minPosTp = minPosTp;
271
	}
272
 
273
	public String getHeld() {
274
		return held;
275
	}
276
 
277
	public void setHeld(String held) {
278
		this.held = held;
279
	}
280
 
281
	public String getSupc() {
282
		return supc;
283
	}
284
 
285
	public void setSupc(String supc) {
286
		this.supc = supc;
287
	}
288
 
289
	public String getFkItemCode() {
290
		return fkItemCode;
291
	}
292
 
293
	public void setFkItemCode(String fkItemCode) {
294
		this.fkItemCode = fkItemCode;
295
	}
296
 
297
	public String index() {
298
		if (!ReportsUtils.canAccessReport((Long) session.getAttribute(ReportsUtils.ROLE),request.getServletPath())) {
299
			return "authfail";
300
		}
301
		return "index";
302
	}
303
 
304
	public String edit() {
305
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1])) {
306
			return "authfail";
307
		}
308
		return "edit";
309
	}
310
 
311
	public String fetchItems() throws TException {
312
		Client CatalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
313
		if (searchText.length() == 0) {
314
			flipkartItems = CatalogClient.getFlipkartItems(Long.valueOf(next), 10);
315
			totalCount = CatalogClient.getCountForFlipkartItems();
316
			setSearchCount(totalCount);
317
		} else {
318
			List<String> subString = Arrays.asList(searchText.split(" "));
319
			flipkartItems = CatalogClient.searchFlipkartItems(subString,Long.valueOf(next), 10);
320
			totalCount = CatalogClient.getCountForFlipkartItems();
321
			searchCount = CatalogClient.getFlipkartSearchResultCount(subString);
322
		}
323
		return "flipkart-item-table";
324
	}
325
 
326
	public FlipkartItemDetails fetchItemDetail() throws NumberFormatException, TException {
327
		Client CatalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
328
		FlipkartItemDetails fkItemDetails = CatalogClient.getFlipkartItemDetails(Long.valueOf(id));
329
		lastUpdatedInventoryTimeStampForItem(fkItemDetails.getLastUpdatedInventoryTimestamp());
330
		return fkItemDetails;
331
	}
332
 
333
	public MarketplaceItems fetchMarketplaceDetail() throws NumberFormatException, TException {
334
		Client CatalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
335
		MarketplaceItems mpItemDetails = CatalogClient.getMarketplacedetailsForItem(Long.valueOf(id), 8);
336
		lastCheckedTimeStampForItem(mpItemDetails.getLastCheckedTimestamp());
337
		return mpItemDetails;
338
	}
339
 
340
	public void lastUpdatedInventoryTimeStampForItem(long timestamp){
341
		setLastUpdatedInventoryTimestamp(new java.util.Date(timestamp).toLocaleString());
342
	}
343
 
344
	public void lastCheckedTimeStampForItem(long timestamp){
345
		setLastCheckedTimestamp(new java.util.Date(timestamp).toLocaleString());
346
	}
347
 
348
	public String uploadBulkSheet(){
349
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
350
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
351
			return "authfail";
352
		}
353
		return "flipkart-bulk-upload";
354
	}
355
 
356
	public String update() throws NumberFormatException, TException, InventoryServiceException, CatalogServiceException, JSONException{
357
		Client catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
358
		//Client catalogClientProd = new CatalogClient().getClient();
359
		FlipkartItem flipkartItem = catalogClient.getFlipkartItem(Long.valueOf(itemId));
360
		Item item = catalogClient.getItem(Long.valueOf(itemId));
361
		MarketplacePercentage mpCosting = catalogClient.getCostingForMarketplace(8, Long.valueOf(itemId));
362
		if (mpCosting.getSource()==0){
363
			throw new CatalogServiceException();
364
		}
365
		MarketplaceItems mpItem = catalogClient.getMarketplacedetailsForItem(Long.valueOf(itemId),8);
366
		mpItem.setEmiFee(mpCosting.getEmiFee());
367
		mpItem.setReturnProvision(mpCosting.getReturnProvision());
368
		mpItem.setClosingFee(mpCosting.getClosingFee());
369
		mpItem.setServiceTax(mpCosting.getServiceTax());
370
		mpItem.setCommission(mpCosting.getCommission());
371
		InventoryClient inventoryServiceClient = new InventoryClient();
372
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
373
		Warehouse warehouse = inventoryClient.getWarehouse(Long.valueOf(warehouseId));
374
		/*if (!(warehouse.getWarehouseType()==WarehouseType.OURS && warehouse.getInventoryType() == InventoryType.GOOD)){
375
			throw new InventoryServiceException();
376
		}*/
377
		flipkartItem.setWarehouseId(Long.valueOf(warehouseId));
378
		flipkartItem.setIsListedOnFlipkart(Boolean.valueOf(isListedOnFlipkart));
379
		flipkartItem.setSuppressInventoryFeed(Boolean.valueOf(isSuppressInventoryFeed));
380
		flipkartItem.setSuppressPriceFeed(Boolean.valueOf(isSuppressPriceFeed));
381
		flipkartItem.setExceptionPrice(Double.valueOf(exceptionPrice));
382
		flipkartItem.setCommissionValue(Double.valueOf(commission));
383
		flipkartItem.setServiceTaxValue(Double.valueOf(serviceTax));
384
		flipkartItem.setMaxNlc(Double.valueOf(maxNlc));
385
		flipkartItem.setSkuAtFlipkart(fkItemCode);
386
		flipkartItem.setFlipkartSerialNumber(fkSerialNumber);
387
		MarketplaceItems t_mpItem = updateMarketplaceItemDetails(mpItem);
388
		//double vat = catalogClientProd.getVatPercentageForItem(Long.valueOf(itemId), warehouse.getStateId(), Double.valueOf(sellingPrice));
389
		//t_mpItem.setVat(vat);
390
		flipkartItem.setUpdatedBy(getUserName());
391
		flipkartItem.setMarketplaceItems(t_mpItem);
392
 
393
		if(!compareParameters(t_mpItem,flipkartItem,warehouse,item.getWeight())){
394
			setComparsionResult("0");
395
			return "flipkart-update-result";
396
		}
397
		else{
398
			setComparsionResult("1");
399
		}
400
 
401
		if(!catalogClient.addOrUpdateFlipkartItem(flipkartItem)){
402
			throw new CatalogServiceException();
403
		}
404
 
405
		if (t_mpItem.getCurrentSp()< t_mpItem.getMinimumPossibleSp()){
406
			sendAlert(t_mpItem.getItemId(),t_mpItem.getCurrentSp(),t_mpItem.getMinimumPossibleSp());
407
		}
10156 amar.kumar 408
		logger.info("\nUpdate Method Item Id "+itemId+"\nSelling Price "+sellingPrice+"\nWarehouse Id "+warehouseId+"\nMax Nlc "+maxNlc+"\nUpdated By: "+session.getAttribute(ReportsUtils.USER_NAME).toString());
10097 kshitij.so 409
		return "flipkart-update-result";
410
	}
411
 
412
 
413
	public boolean compareParameters(MarketplaceItems mpItem, FlipkartItem flipkartItem, Warehouse warehouse,double weight) throws TException, JSONException, CatalogServiceException{
414
		double nlc =getNlcForWarehouse(flipkartItem.getWarehouseId(),flipkartItem.getItem_id());
415
		double vat = (mpItem.getCurrentSp()/(1+(mpItem.getVat()/100))-(nlc/(1+(mpItem.getVat()/100))))*(mpItem.getVat()/100);
416
		double inHouseCost = 15+vat+(mpItem.getReturnProvision()/100)*mpItem.getCurrentSp()+mpItem.getOtherCost();
417
		double lowest_possible_tp = nlc+inHouseCost;
418
		double our_tp =  mpItem.getCurrentSp()- mpItem.getCurrentSp()*(mpItem.getCommission()/100+mpItem.getEmiFee()/100)*(1+(mpItem.getServiceTax()/100))-(mpItem.getCourierCost()+mpItem.getClosingFee())*(1+(mpItem.getServiceTax()/100));
419
		double commission =  ((mpItem.getCommission()/100)*mpItem.getCurrentSp());
420
		double service_tax = (mpItem.getServiceTax()/100)*(flipkartItem.getCommissionValue()+mpItem.getCourierCost());
421
		double lowest_possible_sp = (nlc+(mpItem.getCourierCost()+mpItem.getClosingFee())*(1+(mpItem.getServiceTax()/100))*(1+(mpItem.getVat()/100))+(15+mpItem.getOtherCost())*(1+(mpItem.getVat())/100))/(1-(mpItem.getCommission()/100+mpItem.getEmiFee()/100)*(1+(mpItem.getServiceTax()/100))*(1+(mpItem.getVat())/100)-(mpItem.getReturnProvision()/100)*(1+(mpItem.getVat())/100));
422
		double vat_rate_prod = getVatRateForItem(warehouse,mpItem.getCurrentSp(),flipkartItem.getItem_id());
423
		JSONObject x = new JSONObject();
424
		x.put("NLC", nlc);
425
		x.put("VAT", vat);
426
		x.put("COURIER COST",getCourierCost(weight));
427
		x.put("LOWEST POS TP", lowest_possible_tp);
428
		x.put("TP", our_tp);
429
		x.put("COMMISSION", commission);
430
		x.put("SERVICE TAX", service_tax);
431
		x.put("LOWEST POS SP", lowest_possible_sp);
432
		x.put("VAT RATE",vat_rate_prod);
433
		logger.info("Flipkart Backend validation "+x.toString());
434
		boolean compare_result=true;
435
 
436
		if(!(getCourierCost(weight)==mpItem.getCourierCost())){
437
			compare_result=false;
438
		}
439
 
440
		if(!(vat_rate_prod==mpItem.getVat())){
441
			compare_result=false;
442
		}
443
 
444
		if(!(nlc==flipkartItem.getMaxNlc())){
445
			compare_result=false;
446
		}
447
		if(!(lowest_possible_tp-mpItem.getMinimumPossibleTp()>-1 && lowest_possible_tp-mpItem.getMinimumPossibleTp()<1)){
448
			compare_result=false;
449
		}
450
		if(!(our_tp-mpItem.getCurrentTp()>-1 && our_tp-mpItem.getCurrentTp()<1)){
451
			compare_result=false;
452
		}
453
		if(!(commission-flipkartItem.getCommissionValue()>-1 && commission-flipkartItem.getCommissionValue()<1)){
454
			compare_result=false;
455
		}
456
		if(!(service_tax-flipkartItem.getServiceTaxValue()>-1 && service_tax-flipkartItem.getServiceTaxValue()<1)){
457
			compare_result=false;
458
		}
459
		if(!(lowest_possible_sp-mpItem.getMinimumPossibleSp()>-1 && lowest_possible_sp-mpItem.getMinimumPossibleSp()<1)){
460
			compare_result=false;
461
		}
462
		return compare_result;
463
 
464
	}
465
 
466
	public double getVatRateForItem(Warehouse warehouse, double sellingPrice,long itemId) throws CatalogServiceException, TException{
467
		Client catalogClientProd = new CatalogClient().getClient();
468
		return catalogClientProd.getVatPercentageForItem(itemId, warehouse.getStateId(), sellingPrice);
469
	}
470
 
471
	public double getNlcForWarehouse(long warehouseId,long item_id) throws TException{
472
		InventoryClient inventoryServiceClient = new InventoryClient();
473
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
474
		return inventoryClient.getNlcForWarehouse(warehouseId, item_id);
475
	}
476
 
477
	public double getCourierCost(double weight){
478
		double cCost = 40.0;
479
		int slabs = (int) ((weight+.05 - .001)/(.5));
480
		for(int i=0;i<slabs;i++){
481
			cCost = cCost + 40;
482
		}
483
		return cCost;
484
	}
485
 
486
	public void getLastNDaySaleForItem(String itemId,String days) {
487
		try{
488
			InventoryClient inventoryServiceClient = new InventoryClient();
489
			in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
490
			List<OOSStatus> oosStatuses = inventoryClient.getOosStatusesForXDaysForItem(Long.valueOf(itemId), 8, Integer.valueOf(days));
491
			String lastNdaySale="";
492
			double avgSale = 0.0;
493
			long count = 0,sale = 0;
494
			for(OOSStatus oosStatus : oosStatuses){
495
				if(oosStatus.isIs_oos()){
496
					lastNdaySale += "X-";
497
				}else{
498
					lastNdaySale += oosStatus.getNum_orders() + "-";
499
					sale = sale + oosStatus.getNum_orders();
500
					count+=1;
501
				}
502
			}
503
			lastNdaySale = lastNdaySale.substring(0, lastNdaySale.length()-1);
504
			lastNdaySale += "\n";
505
			setnDaySale(lastNdaySale);
506
			if (count!=0){
507
				setAvgSale(String.valueOf(roundTwoDecimals(sale/(double)count)));
508
			}
509
			else{
510
				setAvgSale("0");
511
			}
512
		}
513
		catch (Exception e){
514
			setAvgSale("0");
515
			setnDaySale("Unable to fetch");
516
			logger.error("Unable to get last n day sale",e);
517
		}
518
 
519
	}
520
 
521
 
522
	private MarketplaceItems updateMarketplaceItemDetails(MarketplaceItems mpItem){
523
		mpItem.setItemId(Long.valueOf(itemId));
524
		mpItem.setSource(8);
525
		mpItem.setVat(Double.valueOf(vat));
526
		mpItem.setCourierCost(Double.valueOf(courierCost));
527
		mpItem.setOtherCost(Double.valueOf(otherCost));
528
		mpItem.setCurrentSp(Double.valueOf(sellingPrice));
529
		mpItem.setCurrentTp(Double.valueOf(transferPrice));
530
		mpItem.setAutoDecrement(Boolean.valueOf(isAutoDecrement));
531
		mpItem.setAutoIncrement(Boolean.valueOf(isAutoIncrement));
532
		mpItem.setManualFavourite(Boolean.valueOf(isManualFavourite));
533
		mpItem.setMinimumPossibleSp(Double.valueOf(minPosSp));
534
		mpItem.setMinimumPossibleTp(Double.valueOf(minPosTp));
535
		mpItem.setMaximumSellingPrice(Double.valueOf(maxSp));
536
		return mpItem;
537
	}
538
 
539
	private void sendAlert(long itemId,double sp,double minSp){
540
		Item item;
541
		try{
542
			Client catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
543
			item= catalogClient.getItem(itemId);
544
		}
545
		catch(Exception e){
546
			e.printStackTrace();
547
			return;
548
		}
549
		String emailSubjectTxt="Flipkart SP is set below Min Possible SP";
550
		String[] sendTo=new String[]{ "kshitij.sood@saholic.com","rajneesh.arora@saholic.com","rajveer.singh@saholic.com","vikram.raghav@saholic.com"};
551
		String text = "Item Details-\nItem Id: "+itemId+"\nProduct Name: "+item.getBrand()+" "+item.getModelName()+" "+item.getModelNumber()+" "+item.getColor()+"\nSellingPrice: "+sp+"\nMinPossibleSp: "+minSp+"\nUpdated By: "+session.getAttribute(ReportsUtils.USER_NAME).toString();
552
		logger.info(text);
553
		String emailFromAddress = "build@shop2020.in";
554
		String password = "cafe@nes";
555
		GmailUtils mailer = new GmailUtils();
556
		try {
557
			mailer.sendSSLMessage(sendTo, emailSubjectTxt, text, emailFromAddress, password, new ArrayList<File>());
558
		}
559
		catch (Exception e) {
560
			logger.info(e.toString());
561
		}
562
	}
563
 
564
	public String addNewItem() throws TException, NumberFormatException, InventoryServiceException, CatalogServiceException, JSONException{
565
		InventoryClient inventoryServiceClient = new InventoryClient();
566
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
567
		Client catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
568
		Item item = catalogClient.getItem(Long.valueOf(itemId));
569
		//Client catalogClientProd = new CatalogClient().getClient();
570
		MarketplacePercentage mpCosting = catalogClient.getCostingForMarketplace(8, Long.valueOf(itemId));
571
 
572
		if (mpCosting.getSource()==0){
573
			throw new CatalogServiceException();
574
		}
575
		Warehouse warehouse = inventoryClient.getWarehouse(Long.valueOf(warehouseId));
576
		/*if (!(warehouse.getWarehouseType()==WarehouseType.OURS && warehouse.getInventoryType() == InventoryType.GOOD)){
577
			throw new InventoryServiceException();
578
		}*/
579
		FlipkartItem flipkartItem = new FlipkartItem();
580
		flipkartItem.setItem_id(Long.valueOf(itemId));
581
		flipkartItem.setWarehouseId(Long.valueOf(warehouseId));
582
		flipkartItem.setIsListedOnFlipkart(Boolean.valueOf(isListedOnFlipkart));
583
		flipkartItem.setSuppressInventoryFeed(Boolean.valueOf(isSuppressInventoryFeed));
584
		flipkartItem.setSuppressPriceFeed(Boolean.valueOf(isSuppressPriceFeed));
585
		flipkartItem.setExceptionPrice(Double.valueOf(exceptionPrice));
586
		flipkartItem.setCommissionValue(Double.valueOf(commission));
587
		flipkartItem.setServiceTaxValue(Double.valueOf(serviceTax));
588
		flipkartItem.setMaxNlc(Double.valueOf(maxNlc));
589
		flipkartItem.setSkuAtFlipkart(fkItemCode);
590
		flipkartItem.setFlipkartSerialNumber(fkSerialNumber);
591
		flipkartItem.setUpdatedBy(getUserName());
592
 
593
		MarketplaceItems mpItem = new MarketplaceItems();
594
		MarketplaceItems t_mpItem = updateMarketplaceItemDetails(mpItem);
595
		/*double vat = catalogClientProd.getVatPercentageForItem(Long.valueOf(itemId), warehouse.getStateId(), Double.valueOf(sellingPrice));
596
		t_mpItem.setVat(vat);*/
597
		t_mpItem.setCommission(mpCosting.getCommission());
598
		t_mpItem.setServiceTax(mpCosting.getServiceTax());
599
		t_mpItem.setReturnProvision(mpCosting.getReturnProvision());
600
		t_mpItem.setEmiFee(mpCosting.getEmiFee());
601
		t_mpItem.setClosingFee(mpCosting.getClosingFee());
602
		if(!compareParameters(t_mpItem,flipkartItem,warehouse,item.getWeight())){
603
			setComparsionResult("0");
604
			return "flipkart-update-result";
605
		}
606
		else{
607
			setComparsionResult("1");
608
		}
609
		flipkartItem.setMarketplaceItems(t_mpItem);
610
		boolean result = catalogClient.addOrUpdateFlipkartItem(flipkartItem);
611
		if(!result){
612
			throw new CatalogServiceException();
613
		}
614
		if (t_mpItem.getCurrentSp()< t_mpItem.getMinimumPossibleSp()){
615
			sendAlert(t_mpItem.getItemId(),t_mpItem.getCurrentSp(),t_mpItem.getMinimumPossibleSp());
616
		}
617
		return "flipkart-update-result";
618
	}
619
 
620
 
621
	public String getAddNewItemForm(){
622
		return "flipkart-add-item";
623
	}
624
 
625
	public String getItemDetailsInJson() throws NumberFormatException, CatalogServiceException, TException, JSONException, InventoryServiceException{
626
		Client catalogClientProd = new CatalogClient().getClient();
627
		Client catalogClient =new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
628
		FlipkartItem flipkartItem = catalogClient.getFlipkartItem(Long.valueOf(itemId));
629
		MarketplacePercentage mpCosting = catalogClient.getCostingForMarketplace(8, Long.valueOf(itemId));
630
		if (flipkartItem.getItem_id()!=0){
631
			throw new CatalogServiceException();
632
		}
633
		Item item = catalogClient.getItem(Long.valueOf(itemId));
634
		if (item.getId()==0){
635
			throw new CatalogServiceException();
636
		}
637
		InventoryClient inventoryServiceClient = new InventoryClient();
638
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
639
		Warehouse warehouse = inventoryClient.getWarehouse(Long.valueOf(warehouseId));
640
		/*if (!(warehouse.getWarehouseType()==WarehouseType.OURS && warehouse.getInventoryType() == InventoryType.GOOD)){
641
			throw new InventoryServiceException();
642
		}*/
643
		double maxNLC = inventoryClient.getNlcForWarehouse(Long.valueOf(warehouseId), Long.valueOf(itemId));
644
		double vat = catalogClientProd.getVatPercentageForItem(Long.valueOf(itemId), warehouse.getStateId(), Double.valueOf(sellingPrice));
645
		if (maxNLC==0){
646
			throw new InventoryServiceException();
647
		}
648
		itemObj = new JSONObject();
649
		itemObj.put("ItemId", item.getId());
650
		itemObj.put("Brand", item.getBrand());
651
		itemObj.put("ModelName", item.getModelName());
652
		itemObj.put("ModelNumber", item.getModelNumber());
653
		itemObj.put("Color", item.getColor());
654
		itemObj.put("Weight", item.getWeight());
655
		itemObj.put("Risky", item.isRisky());
656
		itemObj.put("Status", item.getItemStatus());
657
		itemObj.put("MRP", item.getMrp());
658
		itemObj.put("SellingPrice", item.getSellingPrice());
659
		itemObj.put("MaxNlc", maxNLC);
660
		itemObj.put("VatRate", vat);
661
		itemObj.put("CommissionRate", mpCosting.getCommission());
662
		itemObj.put("ServiceTaxRate", mpCosting.getServiceTax());
663
		itemObj.put("ReturnProvision", mpCosting.getReturnProvision());
664
		itemObj.put("EmiFee", mpCosting.getEmiFee());
665
		itemObj.put("ClosingFee", mpCosting.getClosingFee());
666
		return "item-details-json";
667
	}
668
 
669
	public String ke$ha() throws NumberFormatException, TException, JSONException, InventoryServiceException, CatalogServiceException{
670
		Client catalogClientProd = new CatalogClient().getClient();
671
		InventoryClient inventoryServiceClient = new InventoryClient();
672
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
673
		Warehouse warehouse = inventoryClient.getWarehouse(Long.valueOf(warehouseId));
674
		//if (warehouse.getWarehouseType()==WarehouseType.OURS && warehouse.getInventoryType() == InventoryType.GOOD){
675
		double maxNLC = inventoryClient.getNlcForWarehouse(Long.valueOf(warehouseId), Long.valueOf(itemId));
676
		double vat = catalogClientProd.getVatPercentageForItem(Long.valueOf(itemId), warehouse.getStateId(), Double.valueOf(sellingPrice));
677
		itemObj = new JSONObject();
678
		itemObj.put("ItemId", itemId);
679
		itemObj.put("WarehouseId", warehouseId);
680
		itemObj.put("MaxNlc", maxNLC);
681
		itemObj.put("VatRate", vat);
682
		/*}
683
		else{
684
			itemObj = new JSONObject();
685
			throw new InventoryServiceException();
686
		}*/
687
		return "item-details-json";
688
	}
689
 
690
	public ItemInventory getItemInventory(String itemId) throws NumberFormatException, InventoryServiceException, TException{
691
		try{
692
			InventoryClient inventoryServiceClient = new InventoryClient();
693
			in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
694
			in.shop2020.model.v1.inventory.ItemInventory thriftItemInventory = inventoryClient.getItemInventoryByItemId(Long.valueOf(itemId));
695
			return thriftItemInventory;
696
		}
697
		catch(Exception e){
698
			logger.error("Unable to get inventory info for item",e);
699
			return null;
700
		}
701
	}
702
 
703
 
704
 
705
	public Warehouse getWarehouse(String warehouseId,String itemId) throws NumberFormatException, TException, InventoryServiceException { 
706
		InventoryClient inventoryServiceClient = new InventoryClient();
707
		holdInventoryMap =inventoryServiceClient.getClient().getHeldInventoryMapForItem(Long.valueOf(itemId), Long.valueOf(warehouseId));
708
		return inventoryServiceClient.getClient().getWarehouse(Long.valueOf(warehouseId));
709
	}
710
 
711
	public Map<Integer, Long> getHoldInventoryMapForItem(){
712
		return holdInventoryMap;
713
	}
714
 
715
	public void changeHeldForSource() throws NumberFormatException, TException, InventoryServiceException{
716
		InventoryClient inventoryServiceClient = new InventoryClient();
717
		Warehouse warehouse = inventoryServiceClient.getClient().getWarehouse(Long.valueOf(warehouseId));
718
		if (!(warehouse.getWarehouseType()==WarehouseType.OURS && warehouse.getInventoryType() == InventoryType.GOOD)){
719
			throw new InventoryServiceException();
720
		}
721
		inventoryServiceClient.getClient().addUpdateHoldInventory(Long.valueOf(itemId),Long.valueOf(warehouseId),Long.valueOf(held),8);
722
	}
723
 
724
	public void downloadFlipkartListings() throws IOException, TException{
725
		File file = new File("/tmp/flipkart-bulk-upload-template.xls");
726
		HSSFWorkbook hwb=new HSSFWorkbook();
727
		HSSFSheet sheet =  hwb.createSheet("Flipkart-Listings");
728
		HSSFRow rowhead=   sheet.createRow((short)0);
729
		rowhead.createCell((short) 0).setCellValue("ITEM-ID");
10156 amar.kumar 730
		rowhead.createCell((short) 1).setCellValue("CATEGORY CODE");
731
		rowhead.createCell((short) 2).setCellValue("PRODUCT");
732
		rowhead.createCell((short) 3).setCellValue("WAREHOUSE-ID");
733
		rowhead.createCell((short) 4).setCellValue("EXCEPTIONAL-PRICE");
734
		rowhead.createCell((short) 5).setCellValue("FLIPKART-LISTED");
735
		rowhead.createCell((short) 6).setCellValue("TRANSFER-PRICE");
736
		rowhead.createCell((short) 7).setCellValue("SELLING-PRICE");
737
		rowhead.createCell((short) 8).setCellValue("LOWEST POSSIBLE SP");
738
		rowhead.createCell((short) 9).setCellValue("LOWEST POSSIBLE TP");
739
		rowhead.createCell((short) 10).setCellValue("OTHER COST");
740
		rowhead.createCell((short) 11).setCellValue("COURIER-COST");
741
		rowhead.createCell((short) 12).setCellValue("COMMISION PERCENTAGE");
742
		rowhead.createCell((short) 13).setCellValue("COMMISION VALUE");
743
		rowhead.createCell((short) 14).setCellValue("SERVICE TAX RATE");
744
		rowhead.createCell((short) 15).setCellValue("SERVICE-TAX VALUE");
745
		rowhead.createCell((short) 16).setCellValue("Suppress Inventory Feed");
746
		rowhead.createCell((short) 17).setCellValue("Suppress Price Feed");
747
		rowhead.createCell((short) 18).setCellValue("AUTO FAVOURITE");
748
		rowhead.createCell((short) 19).setCellValue("MANUAL FAVOURITE");
749
		rowhead.createCell((short) 20).setCellValue("AUTO INCREMENT");
750
		rowhead.createCell((short) 21).setCellValue("AUTO DECREMENT");
751
		rowhead.createCell((short) 22).setCellValue("Max NLC");
752
		rowhead.createCell((short) 23).setCellValue("SKU @ Flipkart");
753
		rowhead.createCell((short) 24).setCellValue("Flipkart Serial Number");
10097 kshitij.so 754
		Client catalogClient = null;
755
		List<FlipkartItemDetails> flipkartItems = null;
756
		try {
757
			catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
758
			flipkartItems = catalogClient.getAllFkItems();
759
		} catch (Exception e) {
760
			e.printStackTrace();
761
		}
762
		int iterator=1;
763
		Item item;
764
		MarketplaceItems marketplaceItem;
765
		for(FlipkartItemDetails fkItem:flipkartItems){
766
			marketplaceItem = catalogClient.getMarketplacedetailsForItem(fkItem.getItem_id(), 8);
10156 amar.kumar 767
			MarketplacePercentage mpCosting = catalogClient.getCostingForMarketplace(8, fkItem.getItem_id());
10097 kshitij.so 768
			HSSFRow row = sheet.createRow((short)iterator);
769
			row.createCell((short) 0).setCellValue(fkItem.getItem_id());
10156 amar.kumar 770
			row.createCell((short) 1).setCellValue(fkItem.getCategory());
771
			row.createCell((short) 2).setCellValue(getVaildName(fkItem.getBrand())+" "+getVaildName(fkItem.getModel_name())+" "+getVaildName(fkItem.getModel_number())+" "+getVaildName(fkItem.getColor()));
772
			row.createCell((short) 3).setCellValue(fkItem.getWarehouseId());
773
			row.createCell((short) 4).setCellValue(roundTwoDecimals(fkItem.getExceptionPrice()));
10097 kshitij.so 774
			if(fkItem.isIsListedOnFlipkart()){
10156 amar.kumar 775
				row.createCell((short) 5).setCellValue(1);
10097 kshitij.so 776
			}
777
			else{
10156 amar.kumar 778
				row.createCell((short) 5).setCellValue(0);
10097 kshitij.so 779
			}
10156 amar.kumar 780
			row.createCell((short) 6).setCellValue(roundTwoDecimals(marketplaceItem.getCurrentTp()));
781
			row.createCell((short) 7).setCellValue(roundTwoDecimals(marketplaceItem.getCurrentSp()));
782
			row.createCell((short) 8).setCellValue(roundTwoDecimals(marketplaceItem.getMinimumPossibleSp()));
783
			row.createCell((short) 9).setCellValue(roundTwoDecimals(marketplaceItem.getMinimumPossibleTp()));
784
			row.createCell((short) 10).setCellValue(roundTwoDecimals(marketplaceItem.getOtherCost()));
785
			row.createCell((short) 11).setCellValue(marketplaceItem.getCourierCost());
786
			row.createCell((short) 12).setCellValue(roundTwoDecimals(mpCosting.getCommission()));
787
			row.createCell((short) 13).setCellValue(roundTwoDecimals(fkItem.getCommission()));
788
			row.createCell((short) 14).setCellValue(roundTwoDecimals(mpCosting.getServiceTax()));
789
			row.createCell((short) 15).setCellValue(roundTwoDecimals(fkItem.getServiceTax()));
10097 kshitij.so 790
			if(fkItem.isSuppressInventoryFeed()){
10156 amar.kumar 791
				row.createCell((short) 16).setCellValue(1);
10097 kshitij.so 792
			}
793
			else{
10156 amar.kumar 794
				row.createCell((short) 16).setCellValue(0);
10097 kshitij.so 795
			}
796
			if(fkItem.isSuppressPriceFeed()){
10156 amar.kumar 797
				row.createCell((short) 17).setCellValue(1);
10097 kshitij.so 798
			}
799
			else{
10156 amar.kumar 800
				row.createCell((short) 17).setCellValue(0);
10097 kshitij.so 801
			}
802
			if(marketplaceItem.isAutoFavourite()){
10156 amar.kumar 803
				row.createCell((short) 18).setCellValue(1);
10097 kshitij.so 804
			}
805
			else{
10156 amar.kumar 806
				row.createCell((short) 18).setCellValue(0);
10097 kshitij.so 807
			}
808
			if(marketplaceItem.isManualFavourite()){
10156 amar.kumar 809
				row.createCell((short) 19).setCellValue(1);
10097 kshitij.so 810
			}
811
			else{
10156 amar.kumar 812
				row.createCell((short) 19).setCellValue(0);
10097 kshitij.so 813
			}
814
			if(marketplaceItem.isAutoIncrement()){
10156 amar.kumar 815
				row.createCell((short) 20).setCellValue(1);
10097 kshitij.so 816
			}
817
			else{
10156 amar.kumar 818
				row.createCell((short) 20).setCellValue(0);
10097 kshitij.so 819
			}
820
			if(marketplaceItem.isAutoDecrement()){
10156 amar.kumar 821
				row.createCell((short) 21).setCellValue(1);
10097 kshitij.so 822
			}
823
			else{
10156 amar.kumar 824
				row.createCell((short) 21).setCellValue(0);
10097 kshitij.so 825
			}
10156 amar.kumar 826
			row.createCell((short) 22).setCellValue(fkItem.getMaxNlc());
827
			row.createCell((short) 23).setCellValue(fkItem.getSkuAtFlipkart());
828
			row.createCell((short) 24).setCellValue(fkItem.getFlipkartSerialNumber());
10097 kshitij.so 829
			iterator++;
830
		}
831
 
832
		FileOutputStream fileOut = null;
833
		try {
834
			fileOut = new FileOutputStream(file);
835
		} catch (FileNotFoundException e) {
836
			// TODO Auto-generated catch block
837
			e.printStackTrace();
838
		}
839
		try {
840
			hwb.write(fileOut);
841
		} catch (IOException e) {
842
			// TODO Auto-generated catch block
843
			e.printStackTrace();
844
		}
845
		try {
846
			fileOut.close();
847
		} catch (IOException e) {
848
			// TODO Auto-generated catch block
849
			e.printStackTrace();
850
		}
851
		byte[] buffer = new byte[(int)file.length()];
852
		InputStream input = null;
853
		try {
854
			int totalBytesRead = 0;
855
			input = new BufferedInputStream(new FileInputStream(file));
856
			while(totalBytesRead < buffer.length){
857
				int bytesRemaining = buffer.length - totalBytesRead;
858
				int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
859
				if (bytesRead > 0){
860
					totalBytesRead = totalBytesRead + bytesRead;
861
				}
862
			}
863
		}
864
		finally {
865
			input.close();
866
			file.delete();
867
		}
868
 
869
		response.setHeader("Content-Disposition", "attachment; filename=\"Flipkart-Bulk-Listings.xls\"");
870
		response.setContentType("application/octet-stream");
871
		ServletOutputStream sos;
872
		try {
873
			sos = response.getOutputStream();
874
			sos.write(buffer);
875
			sos.flush();
876
		} catch (IOException e) {
877
			System.out.println("Unable to stream the manifest file");
878
		}   
879
 
880
 
881
	}
882
 
883
 
884
	public void uploadflipkartBulkSheet() throws IOException, TException, NumberFormatException, InventoryServiceException, CatalogServiceException{
885
		File fileToCreate = new File("/tmp/", "flipkart-bulk-upload.xls");
886
		FileUtils.copyFile(this.file, fileToCreate);
887
		FileInputStream iFile = new FileInputStream(new File(file.getAbsolutePath()));
888
		HSSFWorkbook workbook = new HSSFWorkbook(iFile);
889
		HSSFSheet sheet = workbook.getSheetAt(0);
890
		String updatedBy = getUserName();
891
		Client catalogClient=null;
892
		Client catalogClientProd=null;
893
		StringBuilder sb = new StringBuilder();
894
		try {
895
			catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
896
			catalogClientProd = new CatalogClient().getClient();
897
		} catch (TTransportException e) {
898
			e.printStackTrace();
899
		}
900
		InventoryClient inventoryServiceClient = new InventoryClient();
901
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
902
 
903
		for (int iterator=(sheet.getFirstRowNum()+1);iterator<=sheet.getLastRowNum();iterator++){
904
			if (sheet.getLastRowNum()-1>100){
905
				sb.append("Number of items to update can't be greater than 100");
906
				break;
907
			}
908
			FlipkartItem flipkartItem = null;
909
			MarketplaceItems marketplaceItem = null;
910
			MarketplacePercentage mpCosting = null;
911
			Warehouse warehouse = null;
912
			Item item = null;
913
			Long sku;
914
			boolean new_item = false;
915
			if (checkEmptyString(sheet.getRow(iterator).getCell(0))){
916
				continue;
917
			}
918
			else {
919
				sku=(long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
920
				flipkartItem = catalogClient.getFlipkartItem(sku);
921
				mpCosting = catalogClient.getCostingForMarketplace(8, sku);
922
				marketplaceItem  = catalogClient.getMarketplacedetailsForItem(sku, 8);
923
				marketplaceItem.setItemId(sku);
924
				marketplaceItem.setSource(8);
925
				marketplaceItem.setCommission(mpCosting.getCommission());
926
				marketplaceItem.setServiceTax(mpCosting.getServiceTax());
927
				marketplaceItem.setReturnProvision(mpCosting.getReturnProvision());
928
				marketplaceItem.setEmiFee(mpCosting.getEmiFee());
929
				marketplaceItem.setClosingFee(mpCosting.getClosingFee());
930
 
931
				try {
932
					item = catalogClient.getItem(sku);
933
					if (item.getId()==0){
934
						sb.append(sku + " Item not valid"+"\n");
935
						continue;
936
					}
937
				} catch (CatalogServiceException e) {
938
					sb.append(sku + " Item not valid"+"\n");
939
					continue;
940
				}
941
				if(flipkartItem.getItem_id()==0){
942
					new_item = true;
943
					flipkartItem = new FlipkartItem();
944
					flipkartItem.setItem_id(sku); 
945
				}
946
			}
10111 kshitij.so 947
 
948
 
10097 kshitij.so 949
			if (checkEmptyString(sheet.getRow(iterator).getCell(1)) && new_item){
950
				sb.append(sku + " New Listing - Warehouse Id not Present"+"\n");
951
				continue;
952
			}
10111 kshitij.so 953
 
954
 
10097 kshitij.so 955
 
956
			if (!checkEmptyString(sheet.getRow(iterator).getCell(1))){
957
				/*Warehouse warehouse = inventoryClient.getWarehouse(Long.valueOf(warehouseId));
958
				if (!(warehouse.getWarehouseType()==WarehouseType.OURS && warehouse.getInventoryType() == InventoryType.GOOD)){
959
					sb.append(sku + "Not acceptable warehouse"+"\n");
960
					continue;
961
				}*/
962
				long warehouseId = (long) sheet.getRow(iterator).getCell(1).getNumericCellValue();
963
				double maxNLC = inventoryClient.getNlcForWarehouse(warehouseId, item.getId());
964
				if (maxNLC==0){
10114 kshitij.so 965
					sb.append(sku + " Max Nlc can't be 0"+"\n");
10097 kshitij.so 966
					continue;
967
				}
968
				flipkartItem.setWarehouseId(warehouseId);
969
				flipkartItem.setMaxNlc(maxNLC);
970
			}
971
 
972
			if (!checkEmptyString(sheet.getRow(iterator).getCell(2))){
973
				double exceptionPrice = sheet.getRow(iterator).getCell(2).getNumericCellValue();
974
				flipkartItem.setExceptionPrice(exceptionPrice);
975
			}
976
 
977
			if (!checkEmptyString(sheet.getRow(iterator).getCell(3))){
978
				if ((long)sheet.getRow(iterator).getCell(3).getNumericCellValue()==1){
979
					flipkartItem.setIsListedOnFlipkart(true);
980
				}
981
				if ((long)sheet.getRow(iterator).getCell(3).getNumericCellValue()==0){
982
					flipkartItem.setIsListedOnFlipkart(false);
983
				}
984
			}
985
 
986
			double transferPrice = 0,sellingPrice,commission,courierCost =40,serviceTax,otherCost;
987
			if (checkEmptyString(sheet.getRow(iterator).getCell(4))&& new_item){
988
				sb.append(sku + "New Item -  Selling Price cannot be empty"+"\n");
989
				continue;
990
			}
991
			if (!checkEmptyString(sheet.getRow(iterator).getCell(4))){
992
				sellingPrice = sheet.getRow(iterator).getCell(4).getNumericCellValue();
993
				if(sellingPrice==0){
994
					sb.append(sku + " Selling Price cannot be zero"+"\n");
995
					continue;
996
				}
997
				marketplaceItem.setCurrentSp(sellingPrice);
998
			}
999
 
1000
			if (!checkEmptyString(sheet.getRow(iterator).getCell(5))){
1001
				otherCost = sheet.getRow(iterator).getCell(5).getNumericCellValue();
1002
				marketplaceItem.setOtherCost(otherCost);
1003
			}
1004
 
1005
 
1006
			if (!checkEmptyString(sheet.getRow(iterator).getCell(6))){
1007
				if ((long)sheet.getRow(iterator).getCell(6).getNumericCellValue()==1){
1008
					flipkartItem.setSuppressPriceFeed(true);
1009
				}
1010
				if ((long)sheet.getRow(iterator).getCell(6).getNumericCellValue()==0){
1011
					flipkartItem.setSuppressPriceFeed(false);
1012
				}
1013
			}
1014
 
1015
			if (!checkEmptyString(sheet.getRow(iterator).getCell(7))){
1016
				if ((long)sheet.getRow(iterator).getCell(7).getNumericCellValue()==1){
1017
					flipkartItem.setSuppressInventoryFeed(true);
1018
				}
1019
				if ((long)sheet.getRow(iterator).getCell(7).getNumericCellValue()==0){
1020
					flipkartItem.setSuppressInventoryFeed(false);
1021
				}
1022
			}
1023
 
1024
			if (checkEmptyString(sheet.getRow(iterator).getCell(8)) && new_item){
1025
				sb.append(sku + " New Item -  SKU@Flipkart cannot be empty"+"\n");
1026
				continue;
1027
			}
1028
 
1029
			if (!checkEmptyString(sheet.getRow(iterator).getCell(8))){
1030
				sheet.getRow(iterator).getCell(8).setCellType(Cell.CELL_TYPE_STRING);
1031
				String flipkartItemCode = sheet.getRow(iterator).getCell(8).getStringCellValue();
1032
				if (flipkartItemCode.length()==0 && new_item){
1033
					sb.append(sku + " New Item -  SKU@Flipkart cannot be empty"+"\n");
1034
					continue;
1035
				}
1036
				flipkartItem.setSkuAtFlipkart(flipkartItemCode);
1037
			}
1038
 
1039
			if (checkEmptyString(sheet.getRow(iterator).getCell(9)) && new_item){
1040
				sb.append(sku + " New Item -  Flipkart serial number cannot be empty"+"\n");
1041
				continue;
1042
			}
1043
 
1044
			if (!checkEmptyString(sheet.getRow(iterator).getCell(9))){
1045
				sheet.getRow(iterator).getCell(9).setCellType(Cell.CELL_TYPE_STRING);
1046
				String flipkartSerialNumber = sheet.getRow(iterator).getCell(9).getStringCellValue();
1047
				if (flipkartSerialNumber.length()==0 && new_item){
1048
					sb.append(sku + "  New Item -  Flipkart serial number cannot be empty"+"\n");
1049
					continue;
1050
				}
1051
				flipkartItem.setFlipkartSerialNumber(flipkartSerialNumber);
1052
			}
1053
 
1054
			double weight = item.getWeight();
1055
 
1056
 
1057
			if(weight==0){
1058
				sb.append(sku + " Please add weight"+"\n");
1059
				continue;
1060
			}
1061
			if (weight!=0){
1062
				weight = weight+.05;
1063
				int slabs = (int) ((weight - .001)/(.5));
1064
 
1065
				for(int i=0;i<slabs;i++){
1066
					courierCost = courierCost + 40;
1067
				}
1068
			}
10108 kshitij.so 1069
			warehouse = inventoryClient.getWarehouse(flipkartItem.getWarehouseId());
10097 kshitij.so 1070
			marketplaceItem.setCourierCost(courierCost);
1071
			transferPrice = roundTwoDecimals(marketplaceItem.getCurrentSp()- marketplaceItem.getCurrentSp()*(marketplaceItem.getCommission()/100+marketplaceItem.getEmiFee()/100)*(1+(marketplaceItem.getServiceTax()/100))-(marketplaceItem.getCourierCost()+marketplaceItem.getClosingFee())*(1+(marketplaceItem.getServiceTax()/100)));
1072
			marketplaceItem.setCurrentTp(transferPrice);
1073
			commission = roundTwoDecimals(((marketplaceItem.getCommission()/100)*marketplaceItem.getCurrentSp()));
1074
			flipkartItem.setCommissionValue(commission);
1075
			serviceTax = roundTwoDecimals((marketplaceItem.getServiceTax()/100)*(flipkartItem.getCommissionValue()+marketplaceItem.getCourierCost())); 
1076
			flipkartItem.setServiceTaxValue(serviceTax);
1077
			double vatRate = catalogClientProd.getVatPercentageForItem(flipkartItem.getItem_id(),warehouse.getStateId(),marketplaceItem.getCurrentSp());
1078
			marketplaceItem.setVat(vatRate);
1079
			double vat = roundTwoDecimals((marketplaceItem.getCurrentSp()/(1+(marketplaceItem.getVat()/100))-(flipkartItem.getMaxNlc()/(1+(marketplaceItem.getVat()/100))))*(marketplaceItem.getVat()/100));
1080
			double inHouseCost = roundTwoDecimals(15+vat+(marketplaceItem.getReturnProvision()/100)*marketplaceItem.getCurrentSp()+marketplaceItem.getOtherCost());
1081
			double lowest_possible_tp = roundTwoDecimals(flipkartItem.getMaxNlc()+inHouseCost);
1082
			marketplaceItem.setMinimumPossibleTp(lowest_possible_tp);
1083
			double lowest_possible_sp = roundTwoDecimals((flipkartItem.getMaxNlc()+(marketplaceItem.getCourierCost()+marketplaceItem.getClosingFee())*(1+marketplaceItem.getServiceTax()/100)*(1+(marketplaceItem.getVat()/100))+(15+marketplaceItem.getOtherCost())*(1+(marketplaceItem.getVat())/100))/(1-(marketplaceItem.getCommission()/100+marketplaceItem.getEmiFee()/100)*(1+(marketplaceItem.getServiceTax()/100))*(1+(marketplaceItem.getVat())/100)-(marketplaceItem.getReturnProvision()/100)*(1+(marketplaceItem.getVat())/100)));
1084
			marketplaceItem.setMinimumPossibleSp(lowest_possible_sp);
1085
			flipkartItem.setUpdatedBy(updatedBy);
1086
			flipkartItem.setMarketplaceItems(marketplaceItem);
1087
			if(!catalogClient.addOrUpdateFlipkartItem(flipkartItem)){
1088
				sb.append(sku + " Service Error\n");
1089
			}	
1090
		}
1091
		File file = new File("/tmp/flipkartbulk");
1092
		FileWriter writer = new FileWriter(file);
1093
		writer.append(sb.toString());
1094
		writer.close();
1095
		byte[] buffer = new byte[(int)file.length()];
1096
		InputStream input = null;
1097
		try {
1098
			int totalBytesRead = 0;
1099
			input = new BufferedInputStream(new FileInputStream(file));
1100
			while(totalBytesRead < buffer.length){
1101
				int bytesRemaining = buffer.length - totalBytesRead;
1102
				int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
1103
				if (bytesRead > 0){
1104
					totalBytesRead = totalBytesRead + bytesRead;
1105
				}
1106
			}
1107
		}
1108
		finally {
1109
			input.close();
1110
			file.delete();
1111
		}
1112
 
1113
		response.setHeader("Content-Type", "text/javascript");
1114
 
1115
		ServletOutputStream sos;
1116
		try {
1117
			sos = response.getOutputStream();
1118
			sos.write(buffer);
1119
			sos.flush();
1120
		} catch (IOException e) {
1121
			System.out.println("Unable to stream the manifest file");
1122
		}   
1123
	}
1124
 
1125
	public boolean checkEmptyString(Cell cell){
10114 kshitij.so 1126
		if (cell==null || cell.getCellType() == Cell.CELL_TYPE_BLANK){
10097 kshitij.so 1127
			return true;
1128
		}
1129
		return false;
1130
	}
1131
 
1132
	double roundTwoDecimals(double d) {
1133
		DecimalFormat twoPlaces = new DecimalFormat("#.##");
1134
		return Double.valueOf(twoPlaces.format(d));
1135
	}
1136
 
1137
	/*String getLocation(String location){
1138
		logger.info("Inside trim"+location);
1139
		int loc = location.lastIndexOf(',');
1140
		logger.info("end result "+location.substring(loc+1));
1141
		return location.substring(loc+1);
1142
	}*/
1143
 
1144
 
1145
	public String show() {
1146
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1])) {
1147
			return "authfail";
1148
		}
1149
 
1150
		if (StringUtils.equals(id, "flipkart-list-options")){
1151
			return "flipkart-list-options";
1152
		}
1153
		if (StringUtils.equals(id, "item-table")){
1154
			return "flipkart-item-table";
1155
		}
1156
 
1157
		return "id";
1158
	}
1159
 
1160
	public void setId(String id) {
1161
		this.id = id;
1162
	}
1163
 
1164
	public String getUserName(){
1165
		return session.getAttribute(ReportsUtils.USER_NAME).toString();
1166
	}
1167
 
1168
	public HttpServletRequest getRequest() {
1169
		logger.info("set request"+request.toString());
1170
		return request;
1171
	}
1172
 
1173
	public void setRequest(HttpServletRequest request) {
1174
		this.request = request;
1175
	}
1176
 
1177
	public HttpServletResponse getResponse() {
1178
		return response;
1179
	}
1180
 
1181
	public void setResponse(HttpServletResponse response) {
1182
		this.response = response;
1183
	}
1184
 
1185
	public HttpSession getSession() {
1186
		return session;
1187
	}
1188
 
1189
	public void setSession(HttpSession session) {
1190
		this.session = session;
1191
	}
1192
 
1193
	public ServletContext getContext() {
1194
		return context;
1195
	}
1196
 
1197
	public void setContext(ServletContext context) {
1198
		this.context = context;
1199
	}
1200
 
1201
	public String getUrl() {
1202
		return url;
1203
	}
1204
 
1205
	public void setUrl(String url) {
1206
		this.url = url;
1207
	}
1208
 
1209
	public String getItemId() {
1210
		return itemId;
1211
	}
1212
 
1213
	public void setItemId(String itemId) {
1214
		this.itemId = itemId;
1215
	}
1216
 
1217
	public String getExceptionPrice() {
1218
		return exceptionPrice;
1219
	}
1220
 
1221
	public void setExceptionPrice(String exceptionPrice) {
1222
		this.exceptionPrice = exceptionPrice;
1223
	}
1224
 
1225
	public String getWarehouseId() {
1226
		return warehouseId;
1227
	}
1228
 
1229
	public void setWarehouseId(String warehouseId) {
1230
		this.warehouseId = warehouseId;
1231
	}
1232
 
1233
	public File getFile() {
1234
		return file;
1235
	}
1236
 
1237
	public void setFile(File file) {
1238
		this.file = file;
1239
	}
1240
 
1241
	public String getErrMsg() {
1242
		return errMsg;
1243
	}
1244
 
1245
	public void setErrMsg(String errMsg) {
1246
		this.errMsg = errMsg;
1247
	}
1248
 
1249
	public String getNext() {
1250
		return next;
1251
	}
1252
 
1253
	public void setNext(String next) {
1254
		this.next = next;
1255
	}
1256
 
1257
	public String getId() {
1258
		return id;
1259
	}
1260
 
1261
	public void setServletRequest(HttpServletRequest req) {
1262
		this.request = req;
1263
		this.session = req.getSession();        
1264
	}
1265
 
1266
	public void setServletContext(ServletContext arg0) {
1267
		// TODO Auto-generated method stub
1268
 
1269
	}
1270
 
1271
	public void setServletResponse(HttpServletResponse response) {
1272
		this.response = response;
1273
	}
1274
 
1275
	public void setSearchText(String searchText) {
1276
		this.searchText = searchText;
1277
	}
1278
 
1279
	public String getSearchText() {
1280
		return searchText;
1281
	}
1282
 
1283
	public long getSearchCount() {
1284
		return searchCount;
1285
	}
1286
 
1287
	public long getTotalCount() {
1288
		return totalCount;
1289
	}
1290
 
1291
	public void setSearchCount(long count) {
1292
		this.searchCount = count;
1293
	}
1294
 
1295
	public List<FlipkartItemDetails> getFlipkartItems(){
1296
		return flipkartItems;
1297
	}
1298
 
1299
	public void setSellingPrice(String sellingPrice) {
1300
		this.sellingPrice = sellingPrice;
1301
	}
1302
 
1303
	public String getSellingPrice() {
1304
		return sellingPrice;
1305
	}
1306
 
1307
	public void setTransferPrice(String transferPrice) {
1308
		this.transferPrice = transferPrice;
1309
	}
1310
 
1311
	public String getTransferPrice() {
1312
		return transferPrice;
1313
	}
1314
 
1315
	public void setWebisteMrp(String webisteMrp) {
1316
		this.webisteMrp = webisteMrp;
1317
	}
1318
 
1319
	public String getWebisteMrp() {
1320
		return webisteMrp;
1321
	}
1322
 
1323
	public void setWebisteSellingPrice(String webisteSellingPrice) {
1324
		this.webisteSellingPrice = webisteSellingPrice;
1325
	}
1326
 
1327
	public String getWebisteSellingPrice() {
1328
		return webisteSellingPrice;
1329
	}
1330
 
1331
	public void setIsListedOnFlipkart(String isListedOnFlipkart) {
1332
		this.isListedOnFlipkart = isListedOnFlipkart;
1333
	}
1334
 
1335
	public String getIsSuppressInventoryFeed() {
1336
		return isSuppressInventoryFeed;
1337
	}
1338
 
1339
	public void setIsSuppressInventoryFeed(String isSuppressInventoryFeed) {
1340
		this.isSuppressInventoryFeed = isSuppressInventoryFeed;
1341
	}
1342
 
1343
	public String getIsSuppressPriceFeed() {
1344
		return isSuppressPriceFeed;
1345
	}
1346
 
1347
	public void setIsSuppressPriceFeed(String isSuppressPriceFeed) {
1348
		this.isSuppressPriceFeed = isSuppressPriceFeed;
1349
	}
1350
 
1351
	public String getIsListedOnFlipkart() {
1352
		return isListedOnFlipkart;
1353
	}
1354
 
1355
	public void setCommission(String commission) {
1356
		this.commission = commission;
1357
	}
1358
 
1359
	public String getCommission() {
1360
		return commission;
1361
	}
1362
 
1363
	public void setServiceTax(String serviceTax) {
1364
		this.serviceTax = serviceTax;
1365
	}
1366
 
1367
	public String getServiceTax() {
1368
		return serviceTax;
1369
	}
1370
 
1371
	public void setCourierCost(String courierCost) {
1372
		this.courierCost = courierCost;
1373
	}
1374
 
1375
	public String getCourierCost() {
1376
		return courierCost;
1377
	}
1378
 
1379
	public void setMaxNlc(String maxNlc) {
1380
		this.maxNlc = maxNlc;
1381
	}
1382
 
1383
	public String getMaxNlc() {
1384
		return maxNlc;
1385
	}
1386
 
1387
	public String getItemDetails(){
1388
		return itemObj.toString();
1389
	}
1390
 
1391
	public double getRoundOfValue(String val){
1392
		return roundTwoDecimals(Double.valueOf(val));
1393
	}
1394
 
1395
	public String getVaildName(String name){
1396
		return name!=null?name:"";
1397
	}
1398
 
1399
 
1400
 
1401
}