Subversion Repositories SmartDukaan

Rev

Rev 10105 | Rev 10110 | 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
			}
939
 
940
			if (checkEmptyString(sheet.getRow(iterator).getCell(1)) && new_item){
941
				sb.append(sku + " New Listing - Warehouse Id not Present"+"\n");
942
				continue;
943
			}
944
 
945
			if (!checkEmptyString(sheet.getRow(iterator).getCell(1))){
946
				/*Warehouse warehouse = inventoryClient.getWarehouse(Long.valueOf(warehouseId));
947
				if (!(warehouse.getWarehouseType()==WarehouseType.OURS && warehouse.getInventoryType() == InventoryType.GOOD)){
948
					sb.append(sku + "Not acceptable warehouse"+"\n");
949
					continue;
950
				}*/
951
				long warehouseId = (long) sheet.getRow(iterator).getCell(1).getNumericCellValue();
952
				double maxNLC = inventoryClient.getNlcForWarehouse(warehouseId, item.getId());
953
				if (maxNLC==0){
954
					sb.append(sku + "Max Nlc can't be 0"+"\n");
955
					continue;
956
				}
957
				flipkartItem.setWarehouseId(warehouseId);
958
				flipkartItem.setMaxNlc(maxNLC);
959
			}
960
 
961
			if (!checkEmptyString(sheet.getRow(iterator).getCell(2))){
962
				double exceptionPrice = sheet.getRow(iterator).getCell(2).getNumericCellValue();
963
				flipkartItem.setExceptionPrice(exceptionPrice);
964
			}
965
 
966
			if (!checkEmptyString(sheet.getRow(iterator).getCell(3))){
967
				if ((long)sheet.getRow(iterator).getCell(3).getNumericCellValue()==1){
968
					flipkartItem.setIsListedOnFlipkart(true);
969
				}
970
				if ((long)sheet.getRow(iterator).getCell(3).getNumericCellValue()==0){
971
					flipkartItem.setIsListedOnFlipkart(false);
972
				}
973
			}
974
 
975
			double transferPrice = 0,sellingPrice,commission,courierCost =40,serviceTax,otherCost;
976
			if (checkEmptyString(sheet.getRow(iterator).getCell(4))&& new_item){
977
				sb.append(sku + "New Item -  Selling Price cannot be empty"+"\n");
978
				continue;
979
			}
980
			if (!checkEmptyString(sheet.getRow(iterator).getCell(4))){
981
				sellingPrice = sheet.getRow(iterator).getCell(4).getNumericCellValue();
982
				if(sellingPrice==0){
983
					sb.append(sku + " Selling Price cannot be zero"+"\n");
984
					continue;
985
				}
986
				marketplaceItem.setCurrentSp(sellingPrice);
987
			}
988
 
989
			if (!checkEmptyString(sheet.getRow(iterator).getCell(5))){
990
				otherCost = sheet.getRow(iterator).getCell(5).getNumericCellValue();
991
				marketplaceItem.setOtherCost(otherCost);
992
			}
993
 
994
 
995
			if (!checkEmptyString(sheet.getRow(iterator).getCell(6))){
996
				if ((long)sheet.getRow(iterator).getCell(6).getNumericCellValue()==1){
997
					flipkartItem.setSuppressPriceFeed(true);
998
				}
999
				if ((long)sheet.getRow(iterator).getCell(6).getNumericCellValue()==0){
1000
					flipkartItem.setSuppressPriceFeed(false);
1001
				}
1002
			}
1003
 
1004
			if (!checkEmptyString(sheet.getRow(iterator).getCell(7))){
1005
				if ((long)sheet.getRow(iterator).getCell(7).getNumericCellValue()==1){
1006
					flipkartItem.setSuppressInventoryFeed(true);
1007
				}
1008
				if ((long)sheet.getRow(iterator).getCell(7).getNumericCellValue()==0){
1009
					flipkartItem.setSuppressInventoryFeed(false);
1010
				}
1011
			}
1012
 
1013
			if (checkEmptyString(sheet.getRow(iterator).getCell(8)) && new_item){
1014
				sb.append(sku + " New Item -  SKU@Flipkart cannot be empty"+"\n");
1015
				continue;
1016
			}
1017
 
1018
			if (!checkEmptyString(sheet.getRow(iterator).getCell(8))){
1019
				sheet.getRow(iterator).getCell(8).setCellType(Cell.CELL_TYPE_STRING);
1020
				String flipkartItemCode = sheet.getRow(iterator).getCell(8).getStringCellValue();
1021
				if (flipkartItemCode.length()==0 && new_item){
1022
					sb.append(sku + " New Item -  SKU@Flipkart cannot be empty"+"\n");
1023
					continue;
1024
				}
1025
				flipkartItem.setSkuAtFlipkart(flipkartItemCode);
1026
			}
1027
 
1028
			if (checkEmptyString(sheet.getRow(iterator).getCell(9)) && new_item){
1029
				sb.append(sku + " New Item -  Flipkart serial number cannot be empty"+"\n");
1030
				continue;
1031
			}
1032
 
1033
			if (!checkEmptyString(sheet.getRow(iterator).getCell(9))){
1034
				sheet.getRow(iterator).getCell(9).setCellType(Cell.CELL_TYPE_STRING);
1035
				String flipkartSerialNumber = sheet.getRow(iterator).getCell(9).getStringCellValue();
1036
				if (flipkartSerialNumber.length()==0 && new_item){
1037
					sb.append(sku + "  New Item -  Flipkart serial number cannot be empty"+"\n");
1038
					continue;
1039
				}
1040
				flipkartItem.setFlipkartSerialNumber(flipkartSerialNumber);
1041
			}
1042
 
1043
			double weight = item.getWeight();
1044
 
1045
 
1046
			if(weight==0){
1047
				sb.append(sku + " Please add weight"+"\n");
1048
				continue;
1049
			}
1050
			if (weight!=0){
1051
				weight = weight+.05;
1052
				int slabs = (int) ((weight - .001)/(.5));
1053
 
1054
				for(int i=0;i<slabs;i++){
1055
					courierCost = courierCost + 40;
1056
				}
1057
			}
10108 kshitij.so 1058
			warehouse = inventoryClient.getWarehouse(flipkartItem.getWarehouseId());
10097 kshitij.so 1059
			marketplaceItem.setCourierCost(courierCost);
1060
			transferPrice = roundTwoDecimals(marketplaceItem.getCurrentSp()- marketplaceItem.getCurrentSp()*(marketplaceItem.getCommission()/100+marketplaceItem.getEmiFee()/100)*(1+(marketplaceItem.getServiceTax()/100))-(marketplaceItem.getCourierCost()+marketplaceItem.getClosingFee())*(1+(marketplaceItem.getServiceTax()/100)));
1061
			marketplaceItem.setCurrentTp(transferPrice);
1062
			commission = roundTwoDecimals(((marketplaceItem.getCommission()/100)*marketplaceItem.getCurrentSp()));
1063
			flipkartItem.setCommissionValue(commission);
1064
			serviceTax = roundTwoDecimals((marketplaceItem.getServiceTax()/100)*(flipkartItem.getCommissionValue()+marketplaceItem.getCourierCost())); 
1065
			flipkartItem.setServiceTaxValue(serviceTax);
1066
			double vatRate = catalogClientProd.getVatPercentageForItem(flipkartItem.getItem_id(),warehouse.getStateId(),marketplaceItem.getCurrentSp());
1067
			marketplaceItem.setVat(vatRate);
1068
			double vat = roundTwoDecimals((marketplaceItem.getCurrentSp()/(1+(marketplaceItem.getVat()/100))-(flipkartItem.getMaxNlc()/(1+(marketplaceItem.getVat()/100))))*(marketplaceItem.getVat()/100));
1069
			double inHouseCost = roundTwoDecimals(15+vat+(marketplaceItem.getReturnProvision()/100)*marketplaceItem.getCurrentSp()+marketplaceItem.getOtherCost());
1070
			double lowest_possible_tp = roundTwoDecimals(flipkartItem.getMaxNlc()+inHouseCost);
1071
			marketplaceItem.setMinimumPossibleTp(lowest_possible_tp);
1072
			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)));
1073
			marketplaceItem.setMinimumPossibleSp(lowest_possible_sp);
1074
			flipkartItem.setUpdatedBy(updatedBy);
1075
			flipkartItem.setMarketplaceItems(marketplaceItem);
1076
			if(!catalogClient.addOrUpdateFlipkartItem(flipkartItem)){
1077
				sb.append(sku + " Service Error\n");
1078
			}	
1079
		}
1080
		File file = new File("/tmp/flipkartbulk");
1081
		FileWriter writer = new FileWriter(file);
1082
		writer.append(sb.toString());
1083
		writer.close();
1084
		byte[] buffer = new byte[(int)file.length()];
1085
		InputStream input = null;
1086
		try {
1087
			int totalBytesRead = 0;
1088
			input = new BufferedInputStream(new FileInputStream(file));
1089
			while(totalBytesRead < buffer.length){
1090
				int bytesRemaining = buffer.length - totalBytesRead;
1091
				int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
1092
				if (bytesRead > 0){
1093
					totalBytesRead = totalBytesRead + bytesRead;
1094
				}
1095
			}
1096
		}
1097
		finally {
1098
			input.close();
1099
			file.delete();
1100
		}
1101
 
1102
		response.setHeader("Content-Type", "text/javascript");
1103
 
1104
		ServletOutputStream sos;
1105
		try {
1106
			sos = response.getOutputStream();
1107
			sos.write(buffer);
1108
			sos.flush();
1109
		} catch (IOException e) {
1110
			System.out.println("Unable to stream the manifest file");
1111
		}   
1112
	}
1113
 
1114
	public boolean checkEmptyString(Cell cell){
1115
		if (cell==null){
1116
			return true;
1117
		}
1118
		return false;
1119
	}
1120
 
1121
	double roundTwoDecimals(double d) {
1122
		DecimalFormat twoPlaces = new DecimalFormat("#.##");
1123
		return Double.valueOf(twoPlaces.format(d));
1124
	}
1125
 
1126
	/*String getLocation(String location){
1127
		logger.info("Inside trim"+location);
1128
		int loc = location.lastIndexOf(',');
1129
		logger.info("end result "+location.substring(loc+1));
1130
		return location.substring(loc+1);
1131
	}*/
1132
 
1133
 
1134
	public String show() {
1135
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1])) {
1136
			return "authfail";
1137
		}
1138
 
1139
		if (StringUtils.equals(id, "flipkart-list-options")){
1140
			return "flipkart-list-options";
1141
		}
1142
		if (StringUtils.equals(id, "item-table")){
1143
			return "flipkart-item-table";
1144
		}
1145
 
1146
		return "id";
1147
	}
1148
 
1149
	public void setId(String id) {
1150
		this.id = id;
1151
	}
1152
 
1153
	public String getUserName(){
1154
		return session.getAttribute(ReportsUtils.USER_NAME).toString();
1155
	}
1156
 
1157
	public HttpServletRequest getRequest() {
1158
		logger.info("set request"+request.toString());
1159
		return request;
1160
	}
1161
 
1162
	public void setRequest(HttpServletRequest request) {
1163
		this.request = request;
1164
	}
1165
 
1166
	public HttpServletResponse getResponse() {
1167
		return response;
1168
	}
1169
 
1170
	public void setResponse(HttpServletResponse response) {
1171
		this.response = response;
1172
	}
1173
 
1174
	public HttpSession getSession() {
1175
		return session;
1176
	}
1177
 
1178
	public void setSession(HttpSession session) {
1179
		this.session = session;
1180
	}
1181
 
1182
	public ServletContext getContext() {
1183
		return context;
1184
	}
1185
 
1186
	public void setContext(ServletContext context) {
1187
		this.context = context;
1188
	}
1189
 
1190
	public String getUrl() {
1191
		return url;
1192
	}
1193
 
1194
	public void setUrl(String url) {
1195
		this.url = url;
1196
	}
1197
 
1198
	public String getItemId() {
1199
		return itemId;
1200
	}
1201
 
1202
	public void setItemId(String itemId) {
1203
		this.itemId = itemId;
1204
	}
1205
 
1206
	public String getExceptionPrice() {
1207
		return exceptionPrice;
1208
	}
1209
 
1210
	public void setExceptionPrice(String exceptionPrice) {
1211
		this.exceptionPrice = exceptionPrice;
1212
	}
1213
 
1214
	public String getWarehouseId() {
1215
		return warehouseId;
1216
	}
1217
 
1218
	public void setWarehouseId(String warehouseId) {
1219
		this.warehouseId = warehouseId;
1220
	}
1221
 
1222
	public File getFile() {
1223
		return file;
1224
	}
1225
 
1226
	public void setFile(File file) {
1227
		this.file = file;
1228
	}
1229
 
1230
	public String getErrMsg() {
1231
		return errMsg;
1232
	}
1233
 
1234
	public void setErrMsg(String errMsg) {
1235
		this.errMsg = errMsg;
1236
	}
1237
 
1238
	public String getNext() {
1239
		return next;
1240
	}
1241
 
1242
	public void setNext(String next) {
1243
		this.next = next;
1244
	}
1245
 
1246
	public String getId() {
1247
		return id;
1248
	}
1249
 
1250
	public void setServletRequest(HttpServletRequest req) {
1251
		this.request = req;
1252
		this.session = req.getSession();        
1253
	}
1254
 
1255
	public void setServletContext(ServletContext arg0) {
1256
		// TODO Auto-generated method stub
1257
 
1258
	}
1259
 
1260
	public void setServletResponse(HttpServletResponse response) {
1261
		this.response = response;
1262
	}
1263
 
1264
	public void setSearchText(String searchText) {
1265
		this.searchText = searchText;
1266
	}
1267
 
1268
	public String getSearchText() {
1269
		return searchText;
1270
	}
1271
 
1272
	public long getSearchCount() {
1273
		return searchCount;
1274
	}
1275
 
1276
	public long getTotalCount() {
1277
		return totalCount;
1278
	}
1279
 
1280
	public void setSearchCount(long count) {
1281
		this.searchCount = count;
1282
	}
1283
 
1284
	public List<FlipkartItemDetails> getFlipkartItems(){
1285
		return flipkartItems;
1286
	}
1287
 
1288
	public void setSellingPrice(String sellingPrice) {
1289
		this.sellingPrice = sellingPrice;
1290
	}
1291
 
1292
	public String getSellingPrice() {
1293
		return sellingPrice;
1294
	}
1295
 
1296
	public void setTransferPrice(String transferPrice) {
1297
		this.transferPrice = transferPrice;
1298
	}
1299
 
1300
	public String getTransferPrice() {
1301
		return transferPrice;
1302
	}
1303
 
1304
	public void setWebisteMrp(String webisteMrp) {
1305
		this.webisteMrp = webisteMrp;
1306
	}
1307
 
1308
	public String getWebisteMrp() {
1309
		return webisteMrp;
1310
	}
1311
 
1312
	public void setWebisteSellingPrice(String webisteSellingPrice) {
1313
		this.webisteSellingPrice = webisteSellingPrice;
1314
	}
1315
 
1316
	public String getWebisteSellingPrice() {
1317
		return webisteSellingPrice;
1318
	}
1319
 
1320
	public void setIsListedOnFlipkart(String isListedOnFlipkart) {
1321
		this.isListedOnFlipkart = isListedOnFlipkart;
1322
	}
1323
 
1324
	public String getIsSuppressInventoryFeed() {
1325
		return isSuppressInventoryFeed;
1326
	}
1327
 
1328
	public void setIsSuppressInventoryFeed(String isSuppressInventoryFeed) {
1329
		this.isSuppressInventoryFeed = isSuppressInventoryFeed;
1330
	}
1331
 
1332
	public String getIsSuppressPriceFeed() {
1333
		return isSuppressPriceFeed;
1334
	}
1335
 
1336
	public void setIsSuppressPriceFeed(String isSuppressPriceFeed) {
1337
		this.isSuppressPriceFeed = isSuppressPriceFeed;
1338
	}
1339
 
1340
	public String getIsListedOnFlipkart() {
1341
		return isListedOnFlipkart;
1342
	}
1343
 
1344
	public void setCommission(String commission) {
1345
		this.commission = commission;
1346
	}
1347
 
1348
	public String getCommission() {
1349
		return commission;
1350
	}
1351
 
1352
	public void setServiceTax(String serviceTax) {
1353
		this.serviceTax = serviceTax;
1354
	}
1355
 
1356
	public String getServiceTax() {
1357
		return serviceTax;
1358
	}
1359
 
1360
	public void setCourierCost(String courierCost) {
1361
		this.courierCost = courierCost;
1362
	}
1363
 
1364
	public String getCourierCost() {
1365
		return courierCost;
1366
	}
1367
 
1368
	public void setMaxNlc(String maxNlc) {
1369
		this.maxNlc = maxNlc;
1370
	}
1371
 
1372
	public String getMaxNlc() {
1373
		return maxNlc;
1374
	}
1375
 
1376
	public String getItemDetails(){
1377
		return itemObj.toString();
1378
	}
1379
 
1380
	public double getRoundOfValue(String val){
1381
		return roundTwoDecimals(Double.valueOf(val));
1382
	}
1383
 
1384
	public String getVaildName(String name){
1385
		return name!=null?name:"";
1386
	}
1387
 
1388
 
1389
 
1390
}