Subversion Repositories SmartDukaan

Rev

Rev 10110 | Rev 10112 | 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
		}
408
		return "flipkart-update-result";
409
	}
410
 
411
 
412
	public boolean compareParameters(MarketplaceItems mpItem, FlipkartItem flipkartItem, Warehouse warehouse,double weight) throws TException, JSONException, CatalogServiceException{
413
		double nlc =getNlcForWarehouse(flipkartItem.getWarehouseId(),flipkartItem.getItem_id());
414
		double vat = (mpItem.getCurrentSp()/(1+(mpItem.getVat()/100))-(nlc/(1+(mpItem.getVat()/100))))*(mpItem.getVat()/100);
415
		double inHouseCost = 15+vat+(mpItem.getReturnProvision()/100)*mpItem.getCurrentSp()+mpItem.getOtherCost();
416
		double lowest_possible_tp = nlc+inHouseCost;
417
		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));
418
		double commission =  ((mpItem.getCommission()/100)*mpItem.getCurrentSp());
419
		double service_tax = (mpItem.getServiceTax()/100)*(flipkartItem.getCommissionValue()+mpItem.getCourierCost());
420
		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));
421
		double vat_rate_prod = getVatRateForItem(warehouse,mpItem.getCurrentSp(),flipkartItem.getItem_id());
422
		JSONObject x = new JSONObject();
423
		x.put("NLC", nlc);
424
		x.put("VAT", vat);
425
		x.put("COURIER COST",getCourierCost(weight));
426
		x.put("LOWEST POS TP", lowest_possible_tp);
427
		x.put("TP", our_tp);
428
		x.put("COMMISSION", commission);
429
		x.put("SERVICE TAX", service_tax);
430
		x.put("LOWEST POS SP", lowest_possible_sp);
431
		x.put("VAT RATE",vat_rate_prod);
432
		logger.info("Flipkart Backend validation "+x.toString());
433
		boolean compare_result=true;
434
 
435
		if(!(getCourierCost(weight)==mpItem.getCourierCost())){
436
			compare_result=false;
437
		}
438
 
439
		if(!(vat_rate_prod==mpItem.getVat())){
440
			compare_result=false;
441
		}
442
 
443
		if(!(nlc==flipkartItem.getMaxNlc())){
444
			compare_result=false;
445
		}
446
		if(!(lowest_possible_tp-mpItem.getMinimumPossibleTp()>-1 && lowest_possible_tp-mpItem.getMinimumPossibleTp()<1)){
447
			compare_result=false;
448
		}
449
		if(!(our_tp-mpItem.getCurrentTp()>-1 && our_tp-mpItem.getCurrentTp()<1)){
450
			compare_result=false;
451
		}
452
		if(!(commission-flipkartItem.getCommissionValue()>-1 && commission-flipkartItem.getCommissionValue()<1)){
453
			compare_result=false;
454
		}
455
		if(!(service_tax-flipkartItem.getServiceTaxValue()>-1 && service_tax-flipkartItem.getServiceTaxValue()<1)){
456
			compare_result=false;
457
		}
458
		if(!(lowest_possible_sp-mpItem.getMinimumPossibleSp()>-1 && lowest_possible_sp-mpItem.getMinimumPossibleSp()<1)){
459
			compare_result=false;
460
		}
461
		return compare_result;
462
 
463
	}
464
 
465
	public double getVatRateForItem(Warehouse warehouse, double sellingPrice,long itemId) throws CatalogServiceException, TException{
466
		Client catalogClientProd = new CatalogClient().getClient();
467
		return catalogClientProd.getVatPercentageForItem(itemId, warehouse.getStateId(), sellingPrice);
468
	}
469
 
470
	public double getNlcForWarehouse(long warehouseId,long item_id) throws TException{
471
		InventoryClient inventoryServiceClient = new InventoryClient();
472
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
473
		return inventoryClient.getNlcForWarehouse(warehouseId, item_id);
474
	}
475
 
476
	public double getCourierCost(double weight){
477
		double cCost = 40.0;
478
		int slabs = (int) ((weight+.05 - .001)/(.5));
479
		for(int i=0;i<slabs;i++){
480
			cCost = cCost + 40;
481
		}
482
		return cCost;
483
	}
484
 
485
	public void getLastNDaySaleForItem(String itemId,String days) {
486
		try{
487
			InventoryClient inventoryServiceClient = new InventoryClient();
488
			in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
489
			List<OOSStatus> oosStatuses = inventoryClient.getOosStatusesForXDaysForItem(Long.valueOf(itemId), 8, Integer.valueOf(days));
490
			String lastNdaySale="";
491
			double avgSale = 0.0;
492
			long count = 0,sale = 0;
493
			for(OOSStatus oosStatus : oosStatuses){
494
				if(oosStatus.isIs_oos()){
495
					lastNdaySale += "X-";
496
				}else{
497
					lastNdaySale += oosStatus.getNum_orders() + "-";
498
					sale = sale + oosStatus.getNum_orders();
499
					count+=1;
500
				}
501
			}
502
			lastNdaySale = lastNdaySale.substring(0, lastNdaySale.length()-1);
503
			lastNdaySale += "\n";
504
			setnDaySale(lastNdaySale);
505
			if (count!=0){
506
				setAvgSale(String.valueOf(roundTwoDecimals(sale/(double)count)));
507
			}
508
			else{
509
				setAvgSale("0");
510
			}
511
		}
512
		catch (Exception e){
513
			setAvgSale("0");
514
			setnDaySale("Unable to fetch");
515
			logger.error("Unable to get last n day sale",e);
516
		}
517
 
518
	}
519
 
520
 
521
	private MarketplaceItems updateMarketplaceItemDetails(MarketplaceItems mpItem){
522
		mpItem.setItemId(Long.valueOf(itemId));
523
		mpItem.setSource(8);
524
		mpItem.setVat(Double.valueOf(vat));
525
		mpItem.setCourierCost(Double.valueOf(courierCost));
526
		mpItem.setOtherCost(Double.valueOf(otherCost));
527
		mpItem.setCurrentSp(Double.valueOf(sellingPrice));
528
		mpItem.setCurrentTp(Double.valueOf(transferPrice));
529
		mpItem.setAutoDecrement(Boolean.valueOf(isAutoDecrement));
530
		mpItem.setAutoIncrement(Boolean.valueOf(isAutoIncrement));
531
		mpItem.setManualFavourite(Boolean.valueOf(isManualFavourite));
532
		mpItem.setMinimumPossibleSp(Double.valueOf(minPosSp));
533
		mpItem.setMinimumPossibleTp(Double.valueOf(minPosTp));
534
		mpItem.setMaximumSellingPrice(Double.valueOf(maxSp));
535
		return mpItem;
536
	}
537
 
538
	private void sendAlert(long itemId,double sp,double minSp){
539
		Item item;
540
		try{
541
			Client catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
542
			item= catalogClient.getItem(itemId);
543
		}
544
		catch(Exception e){
545
			e.printStackTrace();
546
			return;
547
		}
548
		String emailSubjectTxt="Flipkart SP is set below Min Possible SP";
549
		String[] sendTo=new String[]{ "kshitij.sood@saholic.com","rajneesh.arora@saholic.com","rajveer.singh@saholic.com","vikram.raghav@saholic.com"};
550
		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();
551
		logger.info(text);
552
		String emailFromAddress = "build@shop2020.in";
553
		String password = "cafe@nes";
554
		GmailUtils mailer = new GmailUtils();
555
		try {
556
			mailer.sendSSLMessage(sendTo, emailSubjectTxt, text, emailFromAddress, password, new ArrayList<File>());
557
		}
558
		catch (Exception e) {
559
			logger.info(e.toString());
560
		}
561
	}
562
 
563
	public String addNewItem() throws TException, NumberFormatException, InventoryServiceException, CatalogServiceException, JSONException{
564
		InventoryClient inventoryServiceClient = new InventoryClient();
565
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
566
		Client catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
567
		Item item = catalogClient.getItem(Long.valueOf(itemId));
568
		//Client catalogClientProd = new CatalogClient().getClient();
569
		MarketplacePercentage mpCosting = catalogClient.getCostingForMarketplace(8, Long.valueOf(itemId));
570
 
571
		if (mpCosting.getSource()==0){
572
			throw new CatalogServiceException();
573
		}
574
		Warehouse warehouse = inventoryClient.getWarehouse(Long.valueOf(warehouseId));
575
		/*if (!(warehouse.getWarehouseType()==WarehouseType.OURS && warehouse.getInventoryType() == InventoryType.GOOD)){
576
			throw new InventoryServiceException();
577
		}*/
578
		FlipkartItem flipkartItem = new FlipkartItem();
579
		flipkartItem.setItem_id(Long.valueOf(itemId));
580
		flipkartItem.setWarehouseId(Long.valueOf(warehouseId));
581
		flipkartItem.setIsListedOnFlipkart(Boolean.valueOf(isListedOnFlipkart));
582
		flipkartItem.setSuppressInventoryFeed(Boolean.valueOf(isSuppressInventoryFeed));
583
		flipkartItem.setSuppressPriceFeed(Boolean.valueOf(isSuppressPriceFeed));
584
		flipkartItem.setExceptionPrice(Double.valueOf(exceptionPrice));
585
		flipkartItem.setCommissionValue(Double.valueOf(commission));
586
		flipkartItem.setServiceTaxValue(Double.valueOf(serviceTax));
587
		flipkartItem.setMaxNlc(Double.valueOf(maxNlc));
588
		flipkartItem.setSkuAtFlipkart(fkItemCode);
589
		flipkartItem.setFlipkartSerialNumber(fkSerialNumber);
590
		flipkartItem.setUpdatedBy(getUserName());
591
 
592
		MarketplaceItems mpItem = new MarketplaceItems();
593
		MarketplaceItems t_mpItem = updateMarketplaceItemDetails(mpItem);
594
		/*double vat = catalogClientProd.getVatPercentageForItem(Long.valueOf(itemId), warehouse.getStateId(), Double.valueOf(sellingPrice));
595
		t_mpItem.setVat(vat);*/
596
		t_mpItem.setCommission(mpCosting.getCommission());
597
		t_mpItem.setServiceTax(mpCosting.getServiceTax());
598
		t_mpItem.setReturnProvision(mpCosting.getReturnProvision());
599
		t_mpItem.setEmiFee(mpCosting.getEmiFee());
600
		t_mpItem.setClosingFee(mpCosting.getClosingFee());
601
		if(!compareParameters(t_mpItem,flipkartItem,warehouse,item.getWeight())){
602
			setComparsionResult("0");
603
			return "flipkart-update-result";
604
		}
605
		else{
606
			setComparsionResult("1");
607
		}
608
		flipkartItem.setMarketplaceItems(t_mpItem);
609
		boolean result = catalogClient.addOrUpdateFlipkartItem(flipkartItem);
610
		if(!result){
611
			throw new CatalogServiceException();
612
		}
613
		if (t_mpItem.getCurrentSp()< t_mpItem.getMinimumPossibleSp()){
614
			sendAlert(t_mpItem.getItemId(),t_mpItem.getCurrentSp(),t_mpItem.getMinimumPossibleSp());
615
		}
616
		return "flipkart-update-result";
617
	}
618
 
619
 
620
	public String getAddNewItemForm(){
621
		return "flipkart-add-item";
622
	}
623
 
624
	public String getItemDetailsInJson() throws NumberFormatException, CatalogServiceException, TException, JSONException, InventoryServiceException{
625
		Client catalogClientProd = new CatalogClient().getClient();
626
		Client catalogClient =new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
627
		FlipkartItem flipkartItem = catalogClient.getFlipkartItem(Long.valueOf(itemId));
628
		MarketplacePercentage mpCosting = catalogClient.getCostingForMarketplace(8, Long.valueOf(itemId));
629
		if (flipkartItem.getItem_id()!=0){
630
			throw new CatalogServiceException();
631
		}
632
		Item item = catalogClient.getItem(Long.valueOf(itemId));
633
		if (item.getId()==0){
634
			throw new CatalogServiceException();
635
		}
636
		InventoryClient inventoryServiceClient = new InventoryClient();
637
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
638
		Warehouse warehouse = inventoryClient.getWarehouse(Long.valueOf(warehouseId));
639
		/*if (!(warehouse.getWarehouseType()==WarehouseType.OURS && warehouse.getInventoryType() == InventoryType.GOOD)){
640
			throw new InventoryServiceException();
641
		}*/
642
		double maxNLC = inventoryClient.getNlcForWarehouse(Long.valueOf(warehouseId), Long.valueOf(itemId));
643
		double vat = catalogClientProd.getVatPercentageForItem(Long.valueOf(itemId), warehouse.getStateId(), Double.valueOf(sellingPrice));
644
		if (maxNLC==0){
645
			throw new InventoryServiceException();
646
		}
647
		itemObj = new JSONObject();
648
		itemObj.put("ItemId", item.getId());
649
		itemObj.put("Brand", item.getBrand());
650
		itemObj.put("ModelName", item.getModelName());
651
		itemObj.put("ModelNumber", item.getModelNumber());
652
		itemObj.put("Color", item.getColor());
653
		itemObj.put("Weight", item.getWeight());
654
		itemObj.put("Risky", item.isRisky());
655
		itemObj.put("Status", item.getItemStatus());
656
		itemObj.put("MRP", item.getMrp());
657
		itemObj.put("SellingPrice", item.getSellingPrice());
658
		itemObj.put("MaxNlc", maxNLC);
659
		itemObj.put("VatRate", vat);
660
		itemObj.put("CommissionRate", mpCosting.getCommission());
661
		itemObj.put("ServiceTaxRate", mpCosting.getServiceTax());
662
		itemObj.put("ReturnProvision", mpCosting.getReturnProvision());
663
		itemObj.put("EmiFee", mpCosting.getEmiFee());
664
		itemObj.put("ClosingFee", mpCosting.getClosingFee());
665
		return "item-details-json";
666
	}
667
 
668
	public String ke$ha() throws NumberFormatException, TException, JSONException, InventoryServiceException, CatalogServiceException{
669
		Client catalogClientProd = new CatalogClient().getClient();
670
		InventoryClient inventoryServiceClient = new InventoryClient();
671
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
672
		Warehouse warehouse = inventoryClient.getWarehouse(Long.valueOf(warehouseId));
673
		//if (warehouse.getWarehouseType()==WarehouseType.OURS && warehouse.getInventoryType() == InventoryType.GOOD){
674
		double maxNLC = inventoryClient.getNlcForWarehouse(Long.valueOf(warehouseId), Long.valueOf(itemId));
675
		double vat = catalogClientProd.getVatPercentageForItem(Long.valueOf(itemId), warehouse.getStateId(), Double.valueOf(sellingPrice));
676
		itemObj = new JSONObject();
677
		itemObj.put("ItemId", itemId);
678
		itemObj.put("WarehouseId", warehouseId);
679
		itemObj.put("MaxNlc", maxNLC);
680
		itemObj.put("VatRate", vat);
681
		/*}
682
		else{
683
			itemObj = new JSONObject();
684
			throw new InventoryServiceException();
685
		}*/
686
		return "item-details-json";
687
	}
688
 
689
	public ItemInventory getItemInventory(String itemId) throws NumberFormatException, InventoryServiceException, TException{
690
		try{
691
			InventoryClient inventoryServiceClient = new InventoryClient();
692
			in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
693
			in.shop2020.model.v1.inventory.ItemInventory thriftItemInventory = inventoryClient.getItemInventoryByItemId(Long.valueOf(itemId));
694
			return thriftItemInventory;
695
		}
696
		catch(Exception e){
697
			logger.error("Unable to get inventory info for item",e);
698
			return null;
699
		}
700
	}
701
 
702
 
703
 
704
	public Warehouse getWarehouse(String warehouseId,String itemId) throws NumberFormatException, TException, InventoryServiceException { 
705
		InventoryClient inventoryServiceClient = new InventoryClient();
706
		holdInventoryMap =inventoryServiceClient.getClient().getHeldInventoryMapForItem(Long.valueOf(itemId), Long.valueOf(warehouseId));
707
		return inventoryServiceClient.getClient().getWarehouse(Long.valueOf(warehouseId));
708
	}
709
 
710
	public Map<Integer, Long> getHoldInventoryMapForItem(){
711
		return holdInventoryMap;
712
	}
713
 
714
	public void changeHeldForSource() throws NumberFormatException, TException, InventoryServiceException{
715
		InventoryClient inventoryServiceClient = new InventoryClient();
716
		Warehouse warehouse = inventoryServiceClient.getClient().getWarehouse(Long.valueOf(warehouseId));
717
		if (!(warehouse.getWarehouseType()==WarehouseType.OURS && warehouse.getInventoryType() == InventoryType.GOOD)){
718
			throw new InventoryServiceException();
719
		}
720
		inventoryServiceClient.getClient().addUpdateHoldInventory(Long.valueOf(itemId),Long.valueOf(warehouseId),Long.valueOf(held),8);
721
	}
722
 
723
	public void downloadFlipkartListings() throws IOException, TException{
724
		File file = new File("/tmp/flipkart-bulk-upload-template.xls");
725
		HSSFWorkbook hwb=new HSSFWorkbook();
726
		HSSFSheet sheet =  hwb.createSheet("Flipkart-Listings");
727
		HSSFRow rowhead=   sheet.createRow((short)0);
728
		rowhead.createCell((short) 0).setCellValue("ITEM-ID");
729
		rowhead.createCell((short) 1).setCellValue("PRODUCT");
730
		rowhead.createCell((short) 2).setCellValue("WAREHOUSE-ID");
731
		rowhead.createCell((short) 3).setCellValue("EXCEPTIONAL-PRICE");
732
		rowhead.createCell((short) 4).setCellValue("FLIPKART-LISTED");
733
		rowhead.createCell((short) 5).setCellValue("TRANSFER-PRICE");
734
		rowhead.createCell((short) 6).setCellValue("SELLING-PRICE");
735
		rowhead.createCell((short) 7).setCellValue("LOWEST POSSIBLE SP");
736
		rowhead.createCell((short) 8).setCellValue("LOWEST POSSIBLE TP");
737
		rowhead.createCell((short) 9).setCellValue("OTHER COST");
738
		rowhead.createCell((short) 10).setCellValue("COURIER-COST");
739
		rowhead.createCell((short) 11).setCellValue("COMMISION");
740
		rowhead.createCell((short) 12).setCellValue("SERVICE-TAX");
741
		rowhead.createCell((short) 13).setCellValue("Suppress Inventory Feed");
742
		rowhead.createCell((short) 14).setCellValue("Suppress Price Feed");
743
		rowhead.createCell((short) 15).setCellValue("AUTO FAVOURITE");
744
		rowhead.createCell((short) 16).setCellValue("MANUAL FAVOURITE");
745
		rowhead.createCell((short) 17).setCellValue("AUTO INCREMENT");
746
		rowhead.createCell((short) 18).setCellValue("AUTO DECREMENT");
747
		rowhead.createCell((short) 19).setCellValue("Max NLC");
748
		rowhead.createCell((short) 20).setCellValue("SKU @ Flipkart");
749
		rowhead.createCell((short) 21).setCellValue("Flipkart Serial Number");
750
		Client catalogClient = null;
751
		List<FlipkartItemDetails> flipkartItems = null;
752
		try {
753
			catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
754
			flipkartItems = catalogClient.getAllFkItems();
755
		} catch (Exception e) {
756
			e.printStackTrace();
757
		}
758
		int iterator=1;
759
		Item item;
760
		MarketplaceItems marketplaceItem;
761
		for(FlipkartItemDetails fkItem:flipkartItems){
762
			marketplaceItem = catalogClient.getMarketplacedetailsForItem(fkItem.getItem_id(), 8);
763
			HSSFRow row = sheet.createRow((short)iterator);
764
			row.createCell((short) 0).setCellValue(fkItem.getItem_id());
765
			row.createCell((short) 1).setCellValue(getVaildName(fkItem.getBrand())+" "+getVaildName(fkItem.getModel_name())+" "+getVaildName(fkItem.getModel_number())+" "+getVaildName(fkItem.getColor()));
766
			row.createCell((short) 2).setCellValue(fkItem.getWarehouseId());
767
			row.createCell((short) 3).setCellValue(roundTwoDecimals(fkItem.getExceptionPrice()));
768
			if(fkItem.isIsListedOnFlipkart()){
769
				row.createCell((short) 4).setCellValue(1);
770
			}
771
			else{
772
				row.createCell((short) 4).setCellValue(0);
773
			}
774
			row.createCell((short) 5).setCellValue(roundTwoDecimals(marketplaceItem.getCurrentTp()));
775
			row.createCell((short) 6).setCellValue(roundTwoDecimals(marketplaceItem.getCurrentSp()));
776
			row.createCell((short) 7).setCellValue(roundTwoDecimals(marketplaceItem.getMinimumPossibleSp()));
777
			row.createCell((short) 8).setCellValue(roundTwoDecimals(marketplaceItem.getMinimumPossibleTp()));
778
			row.createCell((short) 9).setCellValue(roundTwoDecimals(marketplaceItem.getOtherCost()));
779
			row.createCell((short) 10).setCellValue(marketplaceItem.getCourierCost());
780
			row.createCell((short) 11).setCellValue(roundTwoDecimals(fkItem.getCommission()));
781
			row.createCell((short) 12).setCellValue(roundTwoDecimals(fkItem.getServiceTax()));
782
			if(fkItem.isSuppressInventoryFeed()){
783
				row.createCell((short) 13).setCellValue(1);
784
			}
785
			else{
786
				row.createCell((short) 13).setCellValue(0);
787
			}
788
			if(fkItem.isSuppressPriceFeed()){
789
				row.createCell((short) 14).setCellValue(1);
790
			}
791
			else{
792
				row.createCell((short) 14).setCellValue(0);
793
			}
794
			if(marketplaceItem.isAutoFavourite()){
795
				row.createCell((short) 15).setCellValue(1);
796
			}
797
			else{
798
				row.createCell((short) 15).setCellValue(0);
799
			}
800
			if(marketplaceItem.isManualFavourite()){
801
				row.createCell((short) 16).setCellValue(1);
802
			}
803
			else{
804
				row.createCell((short) 16).setCellValue(0);
805
			}
806
			if(marketplaceItem.isAutoIncrement()){
807
				row.createCell((short) 17).setCellValue(1);
808
			}
809
			else{
810
				row.createCell((short) 17).setCellValue(0);
811
			}
812
			if(marketplaceItem.isAutoDecrement()){
813
				row.createCell((short) 18).setCellValue(1);
814
			}
815
			else{
816
				row.createCell((short) 18).setCellValue(0);
817
			}
818
			row.createCell((short) 19).setCellValue(fkItem.getMaxNlc());
819
			row.createCell((short) 20).setCellValue(fkItem.getSkuAtFlipkart());
10105 kshitij.so 820
			row.createCell((short) 21).setCellValue(fkItem.getFlipkartSerialNumber());
10097 kshitij.so 821
			iterator++;
822
		}
823
 
824
		FileOutputStream fileOut = null;
825
		try {
826
			fileOut = new FileOutputStream(file);
827
		} catch (FileNotFoundException e) {
828
			// TODO Auto-generated catch block
829
			e.printStackTrace();
830
		}
831
		try {
832
			hwb.write(fileOut);
833
		} catch (IOException e) {
834
			// TODO Auto-generated catch block
835
			e.printStackTrace();
836
		}
837
		try {
838
			fileOut.close();
839
		} catch (IOException e) {
840
			// TODO Auto-generated catch block
841
			e.printStackTrace();
842
		}
843
		byte[] buffer = new byte[(int)file.length()];
844
		InputStream input = null;
845
		try {
846
			int totalBytesRead = 0;
847
			input = new BufferedInputStream(new FileInputStream(file));
848
			while(totalBytesRead < buffer.length){
849
				int bytesRemaining = buffer.length - totalBytesRead;
850
				int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
851
				if (bytesRead > 0){
852
					totalBytesRead = totalBytesRead + bytesRead;
853
				}
854
			}
855
		}
856
		finally {
857
			input.close();
858
			file.delete();
859
		}
860
 
861
		response.setHeader("Content-Disposition", "attachment; filename=\"Flipkart-Bulk-Listings.xls\"");
862
		response.setContentType("application/octet-stream");
863
		ServletOutputStream sos;
864
		try {
865
			sos = response.getOutputStream();
866
			sos.write(buffer);
867
			sos.flush();
868
		} catch (IOException e) {
869
			System.out.println("Unable to stream the manifest file");
870
		}   
871
 
872
 
873
	}
874
 
875
 
876
	public void uploadflipkartBulkSheet() throws IOException, TException, NumberFormatException, InventoryServiceException, CatalogServiceException{
877
		File fileToCreate = new File("/tmp/", "flipkart-bulk-upload.xls");
878
		FileUtils.copyFile(this.file, fileToCreate);
879
		FileInputStream iFile = new FileInputStream(new File(file.getAbsolutePath()));
880
		HSSFWorkbook workbook = new HSSFWorkbook(iFile);
881
		HSSFSheet sheet = workbook.getSheetAt(0);
882
		String updatedBy = getUserName();
883
		Client catalogClient=null;
884
		Client catalogClientProd=null;
885
		StringBuilder sb = new StringBuilder();
886
		try {
887
			catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
888
			catalogClientProd = new CatalogClient().getClient();
889
		} catch (TTransportException e) {
890
			e.printStackTrace();
891
		}
892
		InventoryClient inventoryServiceClient = new InventoryClient();
893
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
894
 
895
		for (int iterator=(sheet.getFirstRowNum()+1);iterator<=sheet.getLastRowNum();iterator++){
896
			if (sheet.getLastRowNum()-1>100){
897
				sb.append("Number of items to update can't be greater than 100");
898
				break;
899
			}
900
			FlipkartItem flipkartItem = null;
901
			MarketplaceItems marketplaceItem = null;
902
			MarketplacePercentage mpCosting = null;
903
			Warehouse warehouse = null;
904
			Item item = null;
905
			Long sku;
906
			boolean new_item = false;
907
			if (checkEmptyString(sheet.getRow(iterator).getCell(0))){
908
				continue;
909
			}
910
			else {
911
				sku=(long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
912
				flipkartItem = catalogClient.getFlipkartItem(sku);
913
				mpCosting = catalogClient.getCostingForMarketplace(8, sku);
914
				marketplaceItem  = catalogClient.getMarketplacedetailsForItem(sku, 8);
915
				marketplaceItem.setItemId(sku);
916
				marketplaceItem.setSource(8);
917
				marketplaceItem.setCommission(mpCosting.getCommission());
918
				marketplaceItem.setServiceTax(mpCosting.getServiceTax());
919
				marketplaceItem.setReturnProvision(mpCosting.getReturnProvision());
920
				marketplaceItem.setEmiFee(mpCosting.getEmiFee());
921
				marketplaceItem.setClosingFee(mpCosting.getClosingFee());
922
 
923
				try {
924
					item = catalogClient.getItem(sku);
925
					if (item.getId()==0){
926
						sb.append(sku + " Item not valid"+"\n");
927
						continue;
928
					}
929
				} catch (CatalogServiceException e) {
930
					sb.append(sku + " Item not valid"+"\n");
931
					continue;
932
				}
933
				if(flipkartItem.getItem_id()==0){
934
					new_item = true;
935
					flipkartItem = new FlipkartItem();
936
					flipkartItem.setItem_id(sku); 
937
				}
938
			}
10111 kshitij.so 939
 
940
			sheet.getRow(iterator).getCell(1).setCellType(Cell.CELL_TYPE_NUMERIC);
941
 
10097 kshitij.so 942
			if (checkEmptyString(sheet.getRow(iterator).getCell(1)) && new_item){
943
				sb.append(sku + " New Listing - Warehouse Id not Present"+"\n");
944
				continue;
945
			}
10111 kshitij.so 946
 
947
 
10097 kshitij.so 948
 
949
			if (!checkEmptyString(sheet.getRow(iterator).getCell(1))){
950
				/*Warehouse warehouse = inventoryClient.getWarehouse(Long.valueOf(warehouseId));
951
				if (!(warehouse.getWarehouseType()==WarehouseType.OURS && warehouse.getInventoryType() == InventoryType.GOOD)){
952
					sb.append(sku + "Not acceptable warehouse"+"\n");
953
					continue;
954
				}*/
955
				long warehouseId = (long) sheet.getRow(iterator).getCell(1).getNumericCellValue();
956
				double maxNLC = inventoryClient.getNlcForWarehouse(warehouseId, item.getId());
957
				if (maxNLC==0){
958
					sb.append(sku + "Max Nlc can't be 0"+"\n");
959
					continue;
960
				}
961
				flipkartItem.setWarehouseId(warehouseId);
962
				flipkartItem.setMaxNlc(maxNLC);
963
			}
964
 
965
			if (!checkEmptyString(sheet.getRow(iterator).getCell(2))){
966
				double exceptionPrice = sheet.getRow(iterator).getCell(2).getNumericCellValue();
967
				flipkartItem.setExceptionPrice(exceptionPrice);
968
			}
969
 
970
			if (!checkEmptyString(sheet.getRow(iterator).getCell(3))){
971
				if ((long)sheet.getRow(iterator).getCell(3).getNumericCellValue()==1){
972
					flipkartItem.setIsListedOnFlipkart(true);
973
				}
974
				if ((long)sheet.getRow(iterator).getCell(3).getNumericCellValue()==0){
975
					flipkartItem.setIsListedOnFlipkart(false);
976
				}
977
			}
978
 
979
			double transferPrice = 0,sellingPrice,commission,courierCost =40,serviceTax,otherCost;
980
			if (checkEmptyString(sheet.getRow(iterator).getCell(4))&& new_item){
981
				sb.append(sku + "New Item -  Selling Price cannot be empty"+"\n");
982
				continue;
983
			}
984
			if (!checkEmptyString(sheet.getRow(iterator).getCell(4))){
985
				sellingPrice = sheet.getRow(iterator).getCell(4).getNumericCellValue();
986
				if(sellingPrice==0){
987
					sb.append(sku + " Selling Price cannot be zero"+"\n");
988
					continue;
989
				}
990
				marketplaceItem.setCurrentSp(sellingPrice);
991
			}
992
 
993
			if (!checkEmptyString(sheet.getRow(iterator).getCell(5))){
994
				otherCost = sheet.getRow(iterator).getCell(5).getNumericCellValue();
995
				marketplaceItem.setOtherCost(otherCost);
996
			}
997
 
998
 
999
			if (!checkEmptyString(sheet.getRow(iterator).getCell(6))){
1000
				if ((long)sheet.getRow(iterator).getCell(6).getNumericCellValue()==1){
1001
					flipkartItem.setSuppressPriceFeed(true);
1002
				}
1003
				if ((long)sheet.getRow(iterator).getCell(6).getNumericCellValue()==0){
1004
					flipkartItem.setSuppressPriceFeed(false);
1005
				}
1006
			}
1007
 
1008
			if (!checkEmptyString(sheet.getRow(iterator).getCell(7))){
1009
				if ((long)sheet.getRow(iterator).getCell(7).getNumericCellValue()==1){
1010
					flipkartItem.setSuppressInventoryFeed(true);
1011
				}
1012
				if ((long)sheet.getRow(iterator).getCell(7).getNumericCellValue()==0){
1013
					flipkartItem.setSuppressInventoryFeed(false);
1014
				}
1015
			}
1016
 
1017
			if (checkEmptyString(sheet.getRow(iterator).getCell(8)) && new_item){
1018
				sb.append(sku + " New Item -  SKU@Flipkart cannot be empty"+"\n");
1019
				continue;
1020
			}
1021
 
1022
			if (!checkEmptyString(sheet.getRow(iterator).getCell(8))){
1023
				sheet.getRow(iterator).getCell(8).setCellType(Cell.CELL_TYPE_STRING);
1024
				String flipkartItemCode = sheet.getRow(iterator).getCell(8).getStringCellValue();
1025
				if (flipkartItemCode.length()==0 && new_item){
1026
					sb.append(sku + " New Item -  SKU@Flipkart cannot be empty"+"\n");
1027
					continue;
1028
				}
1029
				flipkartItem.setSkuAtFlipkart(flipkartItemCode);
1030
			}
1031
 
1032
			if (checkEmptyString(sheet.getRow(iterator).getCell(9)) && new_item){
1033
				sb.append(sku + " New Item -  Flipkart serial number cannot be empty"+"\n");
1034
				continue;
1035
			}
1036
 
1037
			if (!checkEmptyString(sheet.getRow(iterator).getCell(9))){
1038
				sheet.getRow(iterator).getCell(9).setCellType(Cell.CELL_TYPE_STRING);
1039
				String flipkartSerialNumber = sheet.getRow(iterator).getCell(9).getStringCellValue();
1040
				if (flipkartSerialNumber.length()==0 && new_item){
1041
					sb.append(sku + "  New Item -  Flipkart serial number cannot be empty"+"\n");
1042
					continue;
1043
				}
1044
				flipkartItem.setFlipkartSerialNumber(flipkartSerialNumber);
1045
			}
1046
 
1047
			double weight = item.getWeight();
1048
 
1049
 
1050
			if(weight==0){
1051
				sb.append(sku + " Please add weight"+"\n");
1052
				continue;
1053
			}
1054
			if (weight!=0){
1055
				weight = weight+.05;
1056
				int slabs = (int) ((weight - .001)/(.5));
1057
 
1058
				for(int i=0;i<slabs;i++){
1059
					courierCost = courierCost + 40;
1060
				}
1061
			}
10108 kshitij.so 1062
			warehouse = inventoryClient.getWarehouse(flipkartItem.getWarehouseId());
10097 kshitij.so 1063
			marketplaceItem.setCourierCost(courierCost);
1064
			transferPrice = roundTwoDecimals(marketplaceItem.getCurrentSp()- marketplaceItem.getCurrentSp()*(marketplaceItem.getCommission()/100+marketplaceItem.getEmiFee()/100)*(1+(marketplaceItem.getServiceTax()/100))-(marketplaceItem.getCourierCost()+marketplaceItem.getClosingFee())*(1+(marketplaceItem.getServiceTax()/100)));
1065
			marketplaceItem.setCurrentTp(transferPrice);
1066
			commission = roundTwoDecimals(((marketplaceItem.getCommission()/100)*marketplaceItem.getCurrentSp()));
1067
			flipkartItem.setCommissionValue(commission);
1068
			serviceTax = roundTwoDecimals((marketplaceItem.getServiceTax()/100)*(flipkartItem.getCommissionValue()+marketplaceItem.getCourierCost())); 
1069
			flipkartItem.setServiceTaxValue(serviceTax);
1070
			double vatRate = catalogClientProd.getVatPercentageForItem(flipkartItem.getItem_id(),warehouse.getStateId(),marketplaceItem.getCurrentSp());
1071
			marketplaceItem.setVat(vatRate);
10110 kshitij.so 1072
			logger.info("itemid is "+flipkartItem.getItem_id());
10097 kshitij.so 1073
			double vat = roundTwoDecimals((marketplaceItem.getCurrentSp()/(1+(marketplaceItem.getVat()/100))-(flipkartItem.getMaxNlc()/(1+(marketplaceItem.getVat()/100))))*(marketplaceItem.getVat()/100));
1074
			double inHouseCost = roundTwoDecimals(15+vat+(marketplaceItem.getReturnProvision()/100)*marketplaceItem.getCurrentSp()+marketplaceItem.getOtherCost());
1075
			double lowest_possible_tp = roundTwoDecimals(flipkartItem.getMaxNlc()+inHouseCost);
1076
			marketplaceItem.setMinimumPossibleTp(lowest_possible_tp);
1077
			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)));
1078
			marketplaceItem.setMinimumPossibleSp(lowest_possible_sp);
1079
			flipkartItem.setUpdatedBy(updatedBy);
1080
			flipkartItem.setMarketplaceItems(marketplaceItem);
1081
			if(!catalogClient.addOrUpdateFlipkartItem(flipkartItem)){
1082
				sb.append(sku + " Service Error\n");
1083
			}	
1084
		}
1085
		File file = new File("/tmp/flipkartbulk");
1086
		FileWriter writer = new FileWriter(file);
1087
		writer.append(sb.toString());
1088
		writer.close();
1089
		byte[] buffer = new byte[(int)file.length()];
1090
		InputStream input = null;
1091
		try {
1092
			int totalBytesRead = 0;
1093
			input = new BufferedInputStream(new FileInputStream(file));
1094
			while(totalBytesRead < buffer.length){
1095
				int bytesRemaining = buffer.length - totalBytesRead;
1096
				int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
1097
				if (bytesRead > 0){
1098
					totalBytesRead = totalBytesRead + bytesRead;
1099
				}
1100
			}
1101
		}
1102
		finally {
1103
			input.close();
1104
			file.delete();
1105
		}
1106
 
1107
		response.setHeader("Content-Type", "text/javascript");
1108
 
1109
		ServletOutputStream sos;
1110
		try {
1111
			sos = response.getOutputStream();
1112
			sos.write(buffer);
1113
			sos.flush();
1114
		} catch (IOException e) {
1115
			System.out.println("Unable to stream the manifest file");
1116
		}   
1117
	}
1118
 
1119
	public boolean checkEmptyString(Cell cell){
1120
		if (cell==null){
1121
			return true;
1122
		}
1123
		return false;
1124
	}
1125
 
1126
	double roundTwoDecimals(double d) {
1127
		DecimalFormat twoPlaces = new DecimalFormat("#.##");
1128
		return Double.valueOf(twoPlaces.format(d));
1129
	}
1130
 
1131
	/*String getLocation(String location){
1132
		logger.info("Inside trim"+location);
1133
		int loc = location.lastIndexOf(',');
1134
		logger.info("end result "+location.substring(loc+1));
1135
		return location.substring(loc+1);
1136
	}*/
1137
 
1138
 
1139
	public String show() {
1140
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1])) {
1141
			return "authfail";
1142
		}
1143
 
1144
		if (StringUtils.equals(id, "flipkart-list-options")){
1145
			return "flipkart-list-options";
1146
		}
1147
		if (StringUtils.equals(id, "item-table")){
1148
			return "flipkart-item-table";
1149
		}
1150
 
1151
		return "id";
1152
	}
1153
 
1154
	public void setId(String id) {
1155
		this.id = id;
1156
	}
1157
 
1158
	public String getUserName(){
1159
		return session.getAttribute(ReportsUtils.USER_NAME).toString();
1160
	}
1161
 
1162
	public HttpServletRequest getRequest() {
1163
		logger.info("set request"+request.toString());
1164
		return request;
1165
	}
1166
 
1167
	public void setRequest(HttpServletRequest request) {
1168
		this.request = request;
1169
	}
1170
 
1171
	public HttpServletResponse getResponse() {
1172
		return response;
1173
	}
1174
 
1175
	public void setResponse(HttpServletResponse response) {
1176
		this.response = response;
1177
	}
1178
 
1179
	public HttpSession getSession() {
1180
		return session;
1181
	}
1182
 
1183
	public void setSession(HttpSession session) {
1184
		this.session = session;
1185
	}
1186
 
1187
	public ServletContext getContext() {
1188
		return context;
1189
	}
1190
 
1191
	public void setContext(ServletContext context) {
1192
		this.context = context;
1193
	}
1194
 
1195
	public String getUrl() {
1196
		return url;
1197
	}
1198
 
1199
	public void setUrl(String url) {
1200
		this.url = url;
1201
	}
1202
 
1203
	public String getItemId() {
1204
		return itemId;
1205
	}
1206
 
1207
	public void setItemId(String itemId) {
1208
		this.itemId = itemId;
1209
	}
1210
 
1211
	public String getExceptionPrice() {
1212
		return exceptionPrice;
1213
	}
1214
 
1215
	public void setExceptionPrice(String exceptionPrice) {
1216
		this.exceptionPrice = exceptionPrice;
1217
	}
1218
 
1219
	public String getWarehouseId() {
1220
		return warehouseId;
1221
	}
1222
 
1223
	public void setWarehouseId(String warehouseId) {
1224
		this.warehouseId = warehouseId;
1225
	}
1226
 
1227
	public File getFile() {
1228
		return file;
1229
	}
1230
 
1231
	public void setFile(File file) {
1232
		this.file = file;
1233
	}
1234
 
1235
	public String getErrMsg() {
1236
		return errMsg;
1237
	}
1238
 
1239
	public void setErrMsg(String errMsg) {
1240
		this.errMsg = errMsg;
1241
	}
1242
 
1243
	public String getNext() {
1244
		return next;
1245
	}
1246
 
1247
	public void setNext(String next) {
1248
		this.next = next;
1249
	}
1250
 
1251
	public String getId() {
1252
		return id;
1253
	}
1254
 
1255
	public void setServletRequest(HttpServletRequest req) {
1256
		this.request = req;
1257
		this.session = req.getSession();        
1258
	}
1259
 
1260
	public void setServletContext(ServletContext arg0) {
1261
		// TODO Auto-generated method stub
1262
 
1263
	}
1264
 
1265
	public void setServletResponse(HttpServletResponse response) {
1266
		this.response = response;
1267
	}
1268
 
1269
	public void setSearchText(String searchText) {
1270
		this.searchText = searchText;
1271
	}
1272
 
1273
	public String getSearchText() {
1274
		return searchText;
1275
	}
1276
 
1277
	public long getSearchCount() {
1278
		return searchCount;
1279
	}
1280
 
1281
	public long getTotalCount() {
1282
		return totalCount;
1283
	}
1284
 
1285
	public void setSearchCount(long count) {
1286
		this.searchCount = count;
1287
	}
1288
 
1289
	public List<FlipkartItemDetails> getFlipkartItems(){
1290
		return flipkartItems;
1291
	}
1292
 
1293
	public void setSellingPrice(String sellingPrice) {
1294
		this.sellingPrice = sellingPrice;
1295
	}
1296
 
1297
	public String getSellingPrice() {
1298
		return sellingPrice;
1299
	}
1300
 
1301
	public void setTransferPrice(String transferPrice) {
1302
		this.transferPrice = transferPrice;
1303
	}
1304
 
1305
	public String getTransferPrice() {
1306
		return transferPrice;
1307
	}
1308
 
1309
	public void setWebisteMrp(String webisteMrp) {
1310
		this.webisteMrp = webisteMrp;
1311
	}
1312
 
1313
	public String getWebisteMrp() {
1314
		return webisteMrp;
1315
	}
1316
 
1317
	public void setWebisteSellingPrice(String webisteSellingPrice) {
1318
		this.webisteSellingPrice = webisteSellingPrice;
1319
	}
1320
 
1321
	public String getWebisteSellingPrice() {
1322
		return webisteSellingPrice;
1323
	}
1324
 
1325
	public void setIsListedOnFlipkart(String isListedOnFlipkart) {
1326
		this.isListedOnFlipkart = isListedOnFlipkart;
1327
	}
1328
 
1329
	public String getIsSuppressInventoryFeed() {
1330
		return isSuppressInventoryFeed;
1331
	}
1332
 
1333
	public void setIsSuppressInventoryFeed(String isSuppressInventoryFeed) {
1334
		this.isSuppressInventoryFeed = isSuppressInventoryFeed;
1335
	}
1336
 
1337
	public String getIsSuppressPriceFeed() {
1338
		return isSuppressPriceFeed;
1339
	}
1340
 
1341
	public void setIsSuppressPriceFeed(String isSuppressPriceFeed) {
1342
		this.isSuppressPriceFeed = isSuppressPriceFeed;
1343
	}
1344
 
1345
	public String getIsListedOnFlipkart() {
1346
		return isListedOnFlipkart;
1347
	}
1348
 
1349
	public void setCommission(String commission) {
1350
		this.commission = commission;
1351
	}
1352
 
1353
	public String getCommission() {
1354
		return commission;
1355
	}
1356
 
1357
	public void setServiceTax(String serviceTax) {
1358
		this.serviceTax = serviceTax;
1359
	}
1360
 
1361
	public String getServiceTax() {
1362
		return serviceTax;
1363
	}
1364
 
1365
	public void setCourierCost(String courierCost) {
1366
		this.courierCost = courierCost;
1367
	}
1368
 
1369
	public String getCourierCost() {
1370
		return courierCost;
1371
	}
1372
 
1373
	public void setMaxNlc(String maxNlc) {
1374
		this.maxNlc = maxNlc;
1375
	}
1376
 
1377
	public String getMaxNlc() {
1378
		return maxNlc;
1379
	}
1380
 
1381
	public String getItemDetails(){
1382
		return itemObj.toString();
1383
	}
1384
 
1385
	public double getRoundOfValue(String val){
1386
		return roundTwoDecimals(Double.valueOf(val));
1387
	}
1388
 
1389
	public String getVaildName(String name){
1390
		return name!=null?name:"";
1391
	}
1392
 
1393
 
1394
 
1395
}