Subversion Repositories SmartDukaan

Rev

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