Subversion Repositories SmartDukaan

Rev

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