Subversion Repositories SmartDukaan

Rev

Rev 10184 | Rev 10268 | 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");
10184 kshitij.so 739
		rowhead.createCell((short) 10).setCellValue("MARGIN");
740
		rowhead.createCell((short) 11).setCellValue("MARGING PERCENTAGE");
741
		rowhead.createCell((short) 12).setCellValue("OTHER COST");
742
		rowhead.createCell((short) 13).setCellValue("COURIER-COST");
743
		rowhead.createCell((short) 14).setCellValue("COMMISION PERCENTAGE");
744
		rowhead.createCell((short) 15).setCellValue("COMMISION VALUE");
745
		rowhead.createCell((short) 16).setCellValue("SERVICE TAX RATE");
746
		rowhead.createCell((short) 17).setCellValue("SERVICE-TAX VALUE");
747
		rowhead.createCell((short) 18).setCellValue("VAT PERCENTAGE");
748
		rowhead.createCell((short) 19).setCellValue("Suppress Inventory Feed");
749
		rowhead.createCell((short) 20).setCellValue("Suppress Price Feed");
750
		rowhead.createCell((short) 21).setCellValue("AUTO FAVOURITE");
751
		rowhead.createCell((short) 22).setCellValue("MANUAL FAVOURITE");
752
		rowhead.createCell((short) 23).setCellValue("AUTO INCREMENT");
753
		rowhead.createCell((short) 24).setCellValue("AUTO DECREMENT");
754
		rowhead.createCell((short) 25).setCellValue("Max NLC");
755
		rowhead.createCell((short) 26).setCellValue("SKU @ Flipkart");
756
		rowhead.createCell((short) 27).setCellValue("Flipkart Serial Number");
10097 kshitij.so 757
		Client catalogClient = null;
758
		List<FlipkartItemDetails> flipkartItems = null;
759
		try {
760
			catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
761
			flipkartItems = catalogClient.getAllFkItems();
762
		} catch (Exception e) {
763
			e.printStackTrace();
764
		}
765
		int iterator=1;
766
		Item item;
767
		MarketplaceItems marketplaceItem;
768
		for(FlipkartItemDetails fkItem:flipkartItems){
769
			marketplaceItem = catalogClient.getMarketplacedetailsForItem(fkItem.getItem_id(), 8);
10184 kshitij.so 770
			//MarketplacePercentage mpCosting = catalogClient.getCostingForMarketplace(8, fkItem.getItem_id());
10097 kshitij.so 771
			HSSFRow row = sheet.createRow((short)iterator);
772
			row.createCell((short) 0).setCellValue(fkItem.getItem_id());
10156 amar.kumar 773
			row.createCell((short) 1).setCellValue(fkItem.getCategory());
774
			row.createCell((short) 2).setCellValue(getVaildName(fkItem.getBrand())+" "+getVaildName(fkItem.getModel_name())+" "+getVaildName(fkItem.getModel_number())+" "+getVaildName(fkItem.getColor()));
775
			row.createCell((short) 3).setCellValue(fkItem.getWarehouseId());
776
			row.createCell((short) 4).setCellValue(roundTwoDecimals(fkItem.getExceptionPrice()));
10097 kshitij.so 777
			if(fkItem.isIsListedOnFlipkart()){
10156 amar.kumar 778
				row.createCell((short) 5).setCellValue(1);
10097 kshitij.so 779
			}
780
			else{
10156 amar.kumar 781
				row.createCell((short) 5).setCellValue(0);
10097 kshitij.so 782
			}
10156 amar.kumar 783
			row.createCell((short) 6).setCellValue(roundTwoDecimals(marketplaceItem.getCurrentTp()));
784
			row.createCell((short) 7).setCellValue(roundTwoDecimals(marketplaceItem.getCurrentSp()));
785
			row.createCell((short) 8).setCellValue(roundTwoDecimals(marketplaceItem.getMinimumPossibleSp()));
786
			row.createCell((short) 9).setCellValue(roundTwoDecimals(marketplaceItem.getMinimumPossibleTp()));
10184 kshitij.so 787
			row.createCell((short) 10).setCellValue(roundTwoDecimals(roundTwoDecimals((marketplaceItem.getCurrentTp()-marketplaceItem.getMinimumPossibleTp()))));
10186 kshitij.so 788
			row.createCell((short) 11).setCellValue(roundTwoDecimals(roundTwoDecimals((marketplaceItem.getCurrentTp()-marketplaceItem.getMinimumPossibleTp())/marketplaceItem.getMinimumPossibleSp())*100));
10184 kshitij.so 789
			row.createCell((short) 12).setCellValue(roundTwoDecimals(marketplaceItem.getOtherCost()));
790
			row.createCell((short) 13).setCellValue(marketplaceItem.getCourierCost());
791
			row.createCell((short) 14).setCellValue(roundTwoDecimals(marketplaceItem.getCommission()));
792
			row.createCell((short) 15).setCellValue(roundTwoDecimals(fkItem.getCommission()));
793
			row.createCell((short) 16).setCellValue(roundTwoDecimals(marketplaceItem.getServiceTax()));
794
			row.createCell((short) 17).setCellValue(roundTwoDecimals(fkItem.getServiceTax()));
795
			row.createCell((short) 18).setCellValue(roundTwoDecimals(marketplaceItem.getVat()));
10097 kshitij.so 796
			if(fkItem.isSuppressInventoryFeed()){
10184 kshitij.so 797
				row.createCell((short) 19).setCellValue(1);
10097 kshitij.so 798
			}
799
			else{
10184 kshitij.so 800
				row.createCell((short) 19).setCellValue(0);
10097 kshitij.so 801
			}
802
			if(fkItem.isSuppressPriceFeed()){
10184 kshitij.so 803
				row.createCell((short) 20).setCellValue(1);
10097 kshitij.so 804
			}
805
			else{
10184 kshitij.so 806
				row.createCell((short) 20).setCellValue(0);
10097 kshitij.so 807
			}
808
			if(marketplaceItem.isAutoFavourite()){
10184 kshitij.so 809
				row.createCell((short) 21).setCellValue(1);
10097 kshitij.so 810
			}
811
			else{
10184 kshitij.so 812
				row.createCell((short) 21).setCellValue(0);
10097 kshitij.so 813
			}
814
			if(marketplaceItem.isManualFavourite()){
10184 kshitij.so 815
				row.createCell((short) 22).setCellValue(1);
10097 kshitij.so 816
			}
817
			else{
10184 kshitij.so 818
				row.createCell((short) 22).setCellValue(0);
10097 kshitij.so 819
			}
820
			if(marketplaceItem.isAutoIncrement()){
10184 kshitij.so 821
				row.createCell((short) 23).setCellValue(1);
10097 kshitij.so 822
			}
823
			else{
10184 kshitij.so 824
				row.createCell((short) 23).setCellValue(0);
10097 kshitij.so 825
			}
826
			if(marketplaceItem.isAutoDecrement()){
10184 kshitij.so 827
				row.createCell((short) 24).setCellValue(1);
10097 kshitij.so 828
			}
829
			else{
10184 kshitij.so 830
				row.createCell((short) 24).setCellValue(0);
10097 kshitij.so 831
			}
10184 kshitij.so 832
			row.createCell((short) 25).setCellValue(fkItem.getMaxNlc());
833
			row.createCell((short) 26).setCellValue(fkItem.getSkuAtFlipkart());
834
			row.createCell((short) 27).setCellValue(fkItem.getFlipkartSerialNumber());
10097 kshitij.so 835
			iterator++;
836
		}
837
 
838
		FileOutputStream fileOut = null;
839
		try {
840
			fileOut = new FileOutputStream(file);
841
		} catch (FileNotFoundException e) {
842
			// TODO Auto-generated catch block
843
			e.printStackTrace();
844
		}
845
		try {
846
			hwb.write(fileOut);
847
		} catch (IOException e) {
848
			// TODO Auto-generated catch block
849
			e.printStackTrace();
850
		}
851
		try {
852
			fileOut.close();
853
		} catch (IOException e) {
854
			// TODO Auto-generated catch block
855
			e.printStackTrace();
856
		}
857
		byte[] buffer = new byte[(int)file.length()];
858
		InputStream input = null;
859
		try {
860
			int totalBytesRead = 0;
861
			input = new BufferedInputStream(new FileInputStream(file));
862
			while(totalBytesRead < buffer.length){
863
				int bytesRemaining = buffer.length - totalBytesRead;
864
				int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
865
				if (bytesRead > 0){
866
					totalBytesRead = totalBytesRead + bytesRead;
867
				}
868
			}
869
		}
870
		finally {
871
			input.close();
872
			file.delete();
873
		}
874
 
875
		response.setHeader("Content-Disposition", "attachment; filename=\"Flipkart-Bulk-Listings.xls\"");
876
		response.setContentType("application/octet-stream");
877
		ServletOutputStream sos;
878
		try {
879
			sos = response.getOutputStream();
880
			sos.write(buffer);
881
			sos.flush();
882
		} catch (IOException e) {
883
			System.out.println("Unable to stream the manifest file");
884
		}   
885
 
886
 
887
	}
888
 
889
 
890
	public void uploadflipkartBulkSheet() throws IOException, TException, NumberFormatException, InventoryServiceException, CatalogServiceException{
891
		File fileToCreate = new File("/tmp/", "flipkart-bulk-upload.xls");
892
		FileUtils.copyFile(this.file, fileToCreate);
893
		FileInputStream iFile = new FileInputStream(new File(file.getAbsolutePath()));
894
		HSSFWorkbook workbook = new HSSFWorkbook(iFile);
895
		HSSFSheet sheet = workbook.getSheetAt(0);
896
		String updatedBy = getUserName();
897
		Client catalogClient=null;
898
		Client catalogClientProd=null;
899
		StringBuilder sb = new StringBuilder();
900
		try {
901
			catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
902
			catalogClientProd = new CatalogClient().getClient();
903
		} catch (TTransportException e) {
904
			e.printStackTrace();
905
		}
906
		InventoryClient inventoryServiceClient = new InventoryClient();
907
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
908
 
909
		for (int iterator=(sheet.getFirstRowNum()+1);iterator<=sheet.getLastRowNum();iterator++){
910
			if (sheet.getLastRowNum()-1>100){
911
				sb.append("Number of items to update can't be greater than 100");
912
				break;
913
			}
914
			FlipkartItem flipkartItem = null;
915
			MarketplaceItems marketplaceItem = null;
916
			MarketplacePercentage mpCosting = null;
917
			Warehouse warehouse = null;
918
			Item item = null;
919
			Long sku;
920
			boolean new_item = false;
921
			if (checkEmptyString(sheet.getRow(iterator).getCell(0))){
922
				continue;
923
			}
924
			else {
925
				sku=(long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
926
				flipkartItem = catalogClient.getFlipkartItem(sku);
927
				mpCosting = catalogClient.getCostingForMarketplace(8, sku);
928
				marketplaceItem  = catalogClient.getMarketplacedetailsForItem(sku, 8);
929
				marketplaceItem.setItemId(sku);
930
				marketplaceItem.setSource(8);
931
				marketplaceItem.setCommission(mpCosting.getCommission());
932
				marketplaceItem.setServiceTax(mpCosting.getServiceTax());
933
				marketplaceItem.setReturnProvision(mpCosting.getReturnProvision());
934
				marketplaceItem.setEmiFee(mpCosting.getEmiFee());
935
				marketplaceItem.setClosingFee(mpCosting.getClosingFee());
936
 
937
				try {
938
					item = catalogClient.getItem(sku);
939
					if (item.getId()==0){
940
						sb.append(sku + " Item not valid"+"\n");
941
						continue;
942
					}
943
				} catch (CatalogServiceException e) {
944
					sb.append(sku + " Item not valid"+"\n");
945
					continue;
946
				}
947
				if(flipkartItem.getItem_id()==0){
948
					new_item = true;
949
					flipkartItem = new FlipkartItem();
950
					flipkartItem.setItem_id(sku); 
951
				}
952
			}
10111 kshitij.so 953
 
954
 
10097 kshitij.so 955
			if (checkEmptyString(sheet.getRow(iterator).getCell(1)) && new_item){
956
				sb.append(sku + " New Listing - Warehouse Id not Present"+"\n");
957
				continue;
958
			}
10111 kshitij.so 959
 
960
 
10097 kshitij.so 961
 
962
			if (!checkEmptyString(sheet.getRow(iterator).getCell(1))){
963
				/*Warehouse warehouse = inventoryClient.getWarehouse(Long.valueOf(warehouseId));
964
				if (!(warehouse.getWarehouseType()==WarehouseType.OURS && warehouse.getInventoryType() == InventoryType.GOOD)){
965
					sb.append(sku + "Not acceptable warehouse"+"\n");
966
					continue;
967
				}*/
968
				long warehouseId = (long) sheet.getRow(iterator).getCell(1).getNumericCellValue();
969
				double maxNLC = inventoryClient.getNlcForWarehouse(warehouseId, item.getId());
970
				if (maxNLC==0){
10114 kshitij.so 971
					sb.append(sku + " Max Nlc can't be 0"+"\n");
10097 kshitij.so 972
					continue;
973
				}
974
				flipkartItem.setWarehouseId(warehouseId);
975
				flipkartItem.setMaxNlc(maxNLC);
976
			}
977
 
978
			if (!checkEmptyString(sheet.getRow(iterator).getCell(2))){
979
				double exceptionPrice = sheet.getRow(iterator).getCell(2).getNumericCellValue();
980
				flipkartItem.setExceptionPrice(exceptionPrice);
981
			}
982
 
983
			if (!checkEmptyString(sheet.getRow(iterator).getCell(3))){
984
				if ((long)sheet.getRow(iterator).getCell(3).getNumericCellValue()==1){
985
					flipkartItem.setIsListedOnFlipkart(true);
986
				}
987
				if ((long)sheet.getRow(iterator).getCell(3).getNumericCellValue()==0){
988
					flipkartItem.setIsListedOnFlipkart(false);
989
				}
990
			}
991
 
992
			double transferPrice = 0,sellingPrice,commission,courierCost =40,serviceTax,otherCost;
993
			if (checkEmptyString(sheet.getRow(iterator).getCell(4))&& new_item){
994
				sb.append(sku + "New Item -  Selling Price cannot be empty"+"\n");
995
				continue;
996
			}
997
			if (!checkEmptyString(sheet.getRow(iterator).getCell(4))){
998
				sellingPrice = sheet.getRow(iterator).getCell(4).getNumericCellValue();
999
				if(sellingPrice==0){
1000
					sb.append(sku + " Selling Price cannot be zero"+"\n");
1001
					continue;
1002
				}
1003
				marketplaceItem.setCurrentSp(sellingPrice);
1004
			}
1005
 
1006
			if (!checkEmptyString(sheet.getRow(iterator).getCell(5))){
1007
				otherCost = sheet.getRow(iterator).getCell(5).getNumericCellValue();
1008
				marketplaceItem.setOtherCost(otherCost);
1009
			}
1010
 
1011
 
1012
			if (!checkEmptyString(sheet.getRow(iterator).getCell(6))){
1013
				if ((long)sheet.getRow(iterator).getCell(6).getNumericCellValue()==1){
1014
					flipkartItem.setSuppressPriceFeed(true);
1015
				}
1016
				if ((long)sheet.getRow(iterator).getCell(6).getNumericCellValue()==0){
1017
					flipkartItem.setSuppressPriceFeed(false);
1018
				}
1019
			}
1020
 
1021
			if (!checkEmptyString(sheet.getRow(iterator).getCell(7))){
1022
				if ((long)sheet.getRow(iterator).getCell(7).getNumericCellValue()==1){
1023
					flipkartItem.setSuppressInventoryFeed(true);
1024
				}
1025
				if ((long)sheet.getRow(iterator).getCell(7).getNumericCellValue()==0){
1026
					flipkartItem.setSuppressInventoryFeed(false);
1027
				}
1028
			}
1029
 
1030
			if (checkEmptyString(sheet.getRow(iterator).getCell(8)) && new_item){
1031
				sb.append(sku + " New Item -  SKU@Flipkart cannot be empty"+"\n");
1032
				continue;
1033
			}
1034
 
1035
			if (!checkEmptyString(sheet.getRow(iterator).getCell(8))){
1036
				sheet.getRow(iterator).getCell(8).setCellType(Cell.CELL_TYPE_STRING);
1037
				String flipkartItemCode = sheet.getRow(iterator).getCell(8).getStringCellValue();
1038
				if (flipkartItemCode.length()==0 && new_item){
1039
					sb.append(sku + " New Item -  SKU@Flipkart cannot be empty"+"\n");
1040
					continue;
1041
				}
1042
				flipkartItem.setSkuAtFlipkart(flipkartItemCode);
1043
			}
1044
 
1045
			if (checkEmptyString(sheet.getRow(iterator).getCell(9)) && new_item){
1046
				sb.append(sku + " New Item -  Flipkart serial number cannot be empty"+"\n");
1047
				continue;
1048
			}
1049
 
1050
			if (!checkEmptyString(sheet.getRow(iterator).getCell(9))){
1051
				sheet.getRow(iterator).getCell(9).setCellType(Cell.CELL_TYPE_STRING);
1052
				String flipkartSerialNumber = sheet.getRow(iterator).getCell(9).getStringCellValue();
1053
				if (flipkartSerialNumber.length()==0 && new_item){
1054
					sb.append(sku + "  New Item -  Flipkart serial number cannot be empty"+"\n");
1055
					continue;
1056
				}
1057
				flipkartItem.setFlipkartSerialNumber(flipkartSerialNumber);
1058
			}
1059
 
1060
			double weight = item.getWeight();
1061
 
1062
 
1063
			if(weight==0){
1064
				sb.append(sku + " Please add weight"+"\n");
1065
				continue;
1066
			}
1067
			if (weight!=0){
1068
				weight = weight+.05;
1069
				int slabs = (int) ((weight - .001)/(.5));
1070
 
1071
				for(int i=0;i<slabs;i++){
1072
					courierCost = courierCost + 40;
1073
				}
1074
			}
10108 kshitij.so 1075
			warehouse = inventoryClient.getWarehouse(flipkartItem.getWarehouseId());
10097 kshitij.so 1076
			marketplaceItem.setCourierCost(courierCost);
1077
			transferPrice = roundTwoDecimals(marketplaceItem.getCurrentSp()- marketplaceItem.getCurrentSp()*(marketplaceItem.getCommission()/100+marketplaceItem.getEmiFee()/100)*(1+(marketplaceItem.getServiceTax()/100))-(marketplaceItem.getCourierCost()+marketplaceItem.getClosingFee())*(1+(marketplaceItem.getServiceTax()/100)));
1078
			marketplaceItem.setCurrentTp(transferPrice);
1079
			commission = roundTwoDecimals(((marketplaceItem.getCommission()/100)*marketplaceItem.getCurrentSp()));
1080
			flipkartItem.setCommissionValue(commission);
1081
			serviceTax = roundTwoDecimals((marketplaceItem.getServiceTax()/100)*(flipkartItem.getCommissionValue()+marketplaceItem.getCourierCost())); 
1082
			flipkartItem.setServiceTaxValue(serviceTax);
1083
			double vatRate = catalogClientProd.getVatPercentageForItem(flipkartItem.getItem_id(),warehouse.getStateId(),marketplaceItem.getCurrentSp());
1084
			marketplaceItem.setVat(vatRate);
1085
			double vat = roundTwoDecimals((marketplaceItem.getCurrentSp()/(1+(marketplaceItem.getVat()/100))-(flipkartItem.getMaxNlc()/(1+(marketplaceItem.getVat()/100))))*(marketplaceItem.getVat()/100));
1086
			double inHouseCost = roundTwoDecimals(15+vat+(marketplaceItem.getReturnProvision()/100)*marketplaceItem.getCurrentSp()+marketplaceItem.getOtherCost());
1087
			double lowest_possible_tp = roundTwoDecimals(flipkartItem.getMaxNlc()+inHouseCost);
1088
			marketplaceItem.setMinimumPossibleTp(lowest_possible_tp);
1089
			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)));
1090
			marketplaceItem.setMinimumPossibleSp(lowest_possible_sp);
1091
			flipkartItem.setUpdatedBy(updatedBy);
1092
			flipkartItem.setMarketplaceItems(marketplaceItem);
1093
			if(!catalogClient.addOrUpdateFlipkartItem(flipkartItem)){
1094
				sb.append(sku + " Service Error\n");
1095
			}	
1096
		}
1097
		File file = new File("/tmp/flipkartbulk");
1098
		FileWriter writer = new FileWriter(file);
1099
		writer.append(sb.toString());
1100
		writer.close();
1101
		byte[] buffer = new byte[(int)file.length()];
1102
		InputStream input = null;
1103
		try {
1104
			int totalBytesRead = 0;
1105
			input = new BufferedInputStream(new FileInputStream(file));
1106
			while(totalBytesRead < buffer.length){
1107
				int bytesRemaining = buffer.length - totalBytesRead;
1108
				int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
1109
				if (bytesRead > 0){
1110
					totalBytesRead = totalBytesRead + bytesRead;
1111
				}
1112
			}
1113
		}
1114
		finally {
1115
			input.close();
1116
			file.delete();
1117
		}
1118
 
1119
		response.setHeader("Content-Type", "text/javascript");
1120
 
1121
		ServletOutputStream sos;
1122
		try {
1123
			sos = response.getOutputStream();
1124
			sos.write(buffer);
1125
			sos.flush();
1126
		} catch (IOException e) {
1127
			System.out.println("Unable to stream the manifest file");
1128
		}   
1129
	}
1130
 
1131
	public boolean checkEmptyString(Cell cell){
10114 kshitij.so 1132
		if (cell==null || cell.getCellType() == Cell.CELL_TYPE_BLANK){
10097 kshitij.so 1133
			return true;
1134
		}
1135
		return false;
1136
	}
1137
 
1138
	double roundTwoDecimals(double d) {
1139
		DecimalFormat twoPlaces = new DecimalFormat("#.##");
1140
		return Double.valueOf(twoPlaces.format(d));
1141
	}
1142
 
1143
	/*String getLocation(String location){
1144
		logger.info("Inside trim"+location);
1145
		int loc = location.lastIndexOf(',');
1146
		logger.info("end result "+location.substring(loc+1));
1147
		return location.substring(loc+1);
1148
	}*/
1149
 
1150
 
1151
	public String show() {
1152
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1])) {
1153
			return "authfail";
1154
		}
1155
 
1156
		if (StringUtils.equals(id, "flipkart-list-options")){
1157
			return "flipkart-list-options";
1158
		}
1159
		if (StringUtils.equals(id, "item-table")){
1160
			return "flipkart-item-table";
1161
		}
1162
 
1163
		return "id";
1164
	}
1165
 
1166
	public void setId(String id) {
1167
		this.id = id;
1168
	}
1169
 
1170
	public String getUserName(){
1171
		return session.getAttribute(ReportsUtils.USER_NAME).toString();
1172
	}
1173
 
1174
	public HttpServletRequest getRequest() {
1175
		logger.info("set request"+request.toString());
1176
		return request;
1177
	}
1178
 
1179
	public void setRequest(HttpServletRequest request) {
1180
		this.request = request;
1181
	}
1182
 
1183
	public HttpServletResponse getResponse() {
1184
		return response;
1185
	}
1186
 
1187
	public void setResponse(HttpServletResponse response) {
1188
		this.response = response;
1189
	}
1190
 
1191
	public HttpSession getSession() {
1192
		return session;
1193
	}
1194
 
1195
	public void setSession(HttpSession session) {
1196
		this.session = session;
1197
	}
1198
 
1199
	public ServletContext getContext() {
1200
		return context;
1201
	}
1202
 
1203
	public void setContext(ServletContext context) {
1204
		this.context = context;
1205
	}
1206
 
1207
	public String getUrl() {
1208
		return url;
1209
	}
1210
 
1211
	public void setUrl(String url) {
1212
		this.url = url;
1213
	}
1214
 
1215
	public String getItemId() {
1216
		return itemId;
1217
	}
1218
 
1219
	public void setItemId(String itemId) {
1220
		this.itemId = itemId;
1221
	}
1222
 
1223
	public String getExceptionPrice() {
1224
		return exceptionPrice;
1225
	}
1226
 
1227
	public void setExceptionPrice(String exceptionPrice) {
1228
		this.exceptionPrice = exceptionPrice;
1229
	}
1230
 
1231
	public String getWarehouseId() {
1232
		return warehouseId;
1233
	}
1234
 
1235
	public void setWarehouseId(String warehouseId) {
1236
		this.warehouseId = warehouseId;
1237
	}
1238
 
1239
	public File getFile() {
1240
		return file;
1241
	}
1242
 
1243
	public void setFile(File file) {
1244
		this.file = file;
1245
	}
1246
 
1247
	public String getErrMsg() {
1248
		return errMsg;
1249
	}
1250
 
1251
	public void setErrMsg(String errMsg) {
1252
		this.errMsg = errMsg;
1253
	}
1254
 
1255
	public String getNext() {
1256
		return next;
1257
	}
1258
 
1259
	public void setNext(String next) {
1260
		this.next = next;
1261
	}
1262
 
1263
	public String getId() {
1264
		return id;
1265
	}
1266
 
1267
	public void setServletRequest(HttpServletRequest req) {
1268
		this.request = req;
1269
		this.session = req.getSession();        
1270
	}
1271
 
1272
	public void setServletContext(ServletContext arg0) {
1273
		// TODO Auto-generated method stub
1274
 
1275
	}
1276
 
1277
	public void setServletResponse(HttpServletResponse response) {
1278
		this.response = response;
1279
	}
1280
 
1281
	public void setSearchText(String searchText) {
1282
		this.searchText = searchText;
1283
	}
1284
 
1285
	public String getSearchText() {
1286
		return searchText;
1287
	}
1288
 
1289
	public long getSearchCount() {
1290
		return searchCount;
1291
	}
1292
 
1293
	public long getTotalCount() {
1294
		return totalCount;
1295
	}
1296
 
1297
	public void setSearchCount(long count) {
1298
		this.searchCount = count;
1299
	}
1300
 
1301
	public List<FlipkartItemDetails> getFlipkartItems(){
1302
		return flipkartItems;
1303
	}
1304
 
1305
	public void setSellingPrice(String sellingPrice) {
1306
		this.sellingPrice = sellingPrice;
1307
	}
1308
 
1309
	public String getSellingPrice() {
1310
		return sellingPrice;
1311
	}
1312
 
1313
	public void setTransferPrice(String transferPrice) {
1314
		this.transferPrice = transferPrice;
1315
	}
1316
 
1317
	public String getTransferPrice() {
1318
		return transferPrice;
1319
	}
1320
 
1321
	public void setWebisteMrp(String webisteMrp) {
1322
		this.webisteMrp = webisteMrp;
1323
	}
1324
 
1325
	public String getWebisteMrp() {
1326
		return webisteMrp;
1327
	}
1328
 
1329
	public void setWebisteSellingPrice(String webisteSellingPrice) {
1330
		this.webisteSellingPrice = webisteSellingPrice;
1331
	}
1332
 
1333
	public String getWebisteSellingPrice() {
1334
		return webisteSellingPrice;
1335
	}
1336
 
1337
	public void setIsListedOnFlipkart(String isListedOnFlipkart) {
1338
		this.isListedOnFlipkart = isListedOnFlipkart;
1339
	}
1340
 
1341
	public String getIsSuppressInventoryFeed() {
1342
		return isSuppressInventoryFeed;
1343
	}
1344
 
1345
	public void setIsSuppressInventoryFeed(String isSuppressInventoryFeed) {
1346
		this.isSuppressInventoryFeed = isSuppressInventoryFeed;
1347
	}
1348
 
1349
	public String getIsSuppressPriceFeed() {
1350
		return isSuppressPriceFeed;
1351
	}
1352
 
1353
	public void setIsSuppressPriceFeed(String isSuppressPriceFeed) {
1354
		this.isSuppressPriceFeed = isSuppressPriceFeed;
1355
	}
1356
 
1357
	public String getIsListedOnFlipkart() {
1358
		return isListedOnFlipkart;
1359
	}
1360
 
1361
	public void setCommission(String commission) {
1362
		this.commission = commission;
1363
	}
1364
 
1365
	public String getCommission() {
1366
		return commission;
1367
	}
1368
 
1369
	public void setServiceTax(String serviceTax) {
1370
		this.serviceTax = serviceTax;
1371
	}
1372
 
1373
	public String getServiceTax() {
1374
		return serviceTax;
1375
	}
1376
 
1377
	public void setCourierCost(String courierCost) {
1378
		this.courierCost = courierCost;
1379
	}
1380
 
1381
	public String getCourierCost() {
1382
		return courierCost;
1383
	}
1384
 
1385
	public void setMaxNlc(String maxNlc) {
1386
		this.maxNlc = maxNlc;
1387
	}
1388
 
1389
	public String getMaxNlc() {
1390
		return maxNlc;
1391
	}
1392
 
1393
	public String getItemDetails(){
1394
		return itemObj.toString();
1395
	}
1396
 
1397
	public double getRoundOfValue(String val){
1398
		return roundTwoDecimals(Double.valueOf(val));
1399
	}
1400
 
1401
	public String getVaildName(String name){
1402
		return name!=null?name:"";
1403
	}
1404
 
1405
 
1406
 
1407
}