Subversion Repositories SmartDukaan

Rev

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

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