Subversion Repositories SmartDukaan

Rev

Rev 10139 | Rev 10226 | 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));
9825 kshitij.so 355
		Item item = catalogClient.getItem(Long.valueOf(itemId));
9780 kshitij.so 356
		MarketplacePercentage mpCosting = catalogClient.getCostingForMarketplace(7, Long.valueOf(itemId));
357
		if (mpCosting.getSource()==0){
358
			throw new CatalogServiceException();
359
		}
9725 kshitij.so 360
		MarketplaceItems mpItem = catalogClient.getMarketplacedetailsForItem(Long.valueOf(itemId),7);
9780 kshitij.so 361
		mpItem.setEmiFee(mpCosting.getEmiFee());
362
		mpItem.setReturnProvision(mpCosting.getReturnProvision());
363
		mpItem.setClosingFee(mpCosting.getClosingFee());
364
		mpItem.setServiceTax(mpCosting.getServiceTax());
365
		mpItem.setCommission(mpCosting.getCommission());
9725 kshitij.so 366
		InventoryClient inventoryServiceClient = new InventoryClient();
367
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
9825 kshitij.so 368
		Warehouse warehouse = inventoryClient.getWarehouse(Long.valueOf(warehouseId));
369
		/*if (!(warehouse.getWarehouseType()==WarehouseType.OURS && warehouse.getInventoryType() == InventoryType.GOOD)){
9725 kshitij.so 370
			throw new InventoryServiceException();
9780 kshitij.so 371
		}*/
372
		snapdealItem.setWarehouseId(Long.valueOf(warehouseId));
373
		snapdealItem.setIsListedOnSnapdeal(Boolean.valueOf(isListedOnSnapdeal));
374
		snapdealItem.setSuppressInventoryFeed(Boolean.valueOf(isSuppressInventoryFeed));
375
		snapdealItem.setSuppressPriceFeed(Boolean.valueOf(isSuppressPriceFeed));
376
		snapdealItem.setTransferPrice(Double.valueOf(transferPrice));
377
		snapdealItem.setExceptionPrice(Double.valueOf(exceptionPrice));
378
		snapdealItem.setSellingPrice(Double.valueOf(sellingPrice));
379
		snapdealItem.setCourierCost(Double.valueOf(courierCost));
380
		snapdealItem.setCommission(Double.valueOf(commission));
381
		snapdealItem.setServiceTax(Double.valueOf(serviceTax));
382
		snapdealItem.setMaxNlc(Double.valueOf(maxNlc));
383
		snapdealItem.setSkuAtSnapdeal(sdItemCode);
384
		snapdealItem.setSupc(supc);
9734 kshitij.so 385
		MarketplaceItems t_mpItem = updateMarketplaceItemDetails(mpItem);
9825 kshitij.so 386
		//double vat = catalogClientProd.getVatPercentageForItem(Long.valueOf(itemId), warehouse.getStateId(), Double.valueOf(sellingPrice));
387
		//t_mpItem.setVat(vat);
9780 kshitij.so 388
		snapdealItem.setUpdatedBy(getUserName());
389
		snapdealItem.setMarketplaceItems(t_mpItem);
390
 
9825 kshitij.so 391
		if(!compareParameters(t_mpItem,snapdealItem,warehouse,item.getWeight())){
9734 kshitij.so 392
			setComparsionResult("0");
393
			return "snapdeal-update-result";
394
		}
395
		else{
396
			setComparsionResult("1");
397
		}
9478 kshitij.so 398
 
9780 kshitij.so 399
		if(!catalogClient.addOrUpdateSnapdealItem(snapdealItem)){
9725 kshitij.so 400
			throw new CatalogServiceException();
401
		}
9780 kshitij.so 402
 
9725 kshitij.so 403
		if (t_mpItem.getCurrentSp()< t_mpItem.getMinimumPossibleSp()){
404
			sendAlert(t_mpItem.getItemId(),t_mpItem.getCurrentSp(),t_mpItem.getMinimumPossibleSp());
405
		}
10139 amar.kumar 406
		logger.info("\nUpdate Method Item Id "+itemId+"\nSelling Price "+sellingPrice+"\nWarehouse Id "+warehouseId+"\nMax Nlc "+maxNlc+"\nUpdated By: "+session.getAttribute(ReportsUtils.USER_NAME).toString());
10225 vikram.rag 407
		if(snapdealItem.getSellingPrice()!=Double.valueOf(sellingPrice)){
408
			UpdateSDPricingUsingPanel updatePriceOnSnapdeal = new UpdateSDPricingUsingPanel(Float.valueOf(sellingPrice),supc,item,timestamp);
409
			updatePriceOnSnapdeal.start();	
410
		}
9734 kshitij.so 411
		return "snapdeal-update-result";
9242 kshitij.so 412
	}
413
 
9923 kshitij.so 414
 
9825 kshitij.so 415
	public boolean compareParameters(MarketplaceItems mpItem, SnapdealItem snapdealItem, Warehouse warehouse,double weight) throws TException, JSONException, CatalogServiceException{
9734 kshitij.so 416
		double nlc =getNlcForWarehouse(snapdealItem.getWarehouseId(),snapdealItem.getItem_id());
417
		double vat = (snapdealItem.getSellingPrice()/(1+(mpItem.getVat()/100))-(nlc/(1+(mpItem.getVat()/100))))*(mpItem.getVat()/100);
418
		double inHouseCost = 15+vat+(mpItem.getReturnProvision()/100)*snapdealItem.getSellingPrice()+mpItem.getOtherCost();
9780 kshitij.so 419
		double lowest_possible_tp = nlc+inHouseCost;
420
		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));
421
		double commission =  ((mpItem.getCommission()/100)*snapdealItem.getSellingPrice());
422
		double service_tax = (mpItem.getServiceTax()/100)*(snapdealItem.getCommission()+snapdealItem.getCourierCost());
423
		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 424
		double vat_rate_prod = getVatRateForItem(warehouse,snapdealItem.getSellingPrice(),snapdealItem.getItem_id());
9780 kshitij.so 425
		JSONObject x = new JSONObject();
426
		x.put("NLC", nlc);
427
		x.put("VAT", vat);
9923 kshitij.so 428
		x.put("COURIER COST",getCourierCost(weight));
9780 kshitij.so 429
		x.put("LOWEST POS TP", lowest_possible_tp);
430
		x.put("TP", our_tp);
431
		x.put("COMMISSION", commission);
432
		x.put("SERVICE TAX", service_tax);
433
		x.put("LOWEST POS SP", lowest_possible_sp);
9923 kshitij.so 434
		x.put("VAT RATE",vat_rate_prod);
9780 kshitij.so 435
		logger.info("Snapdeal Backend validation "+x.toString());
436
		boolean compare_result=true;
9923 kshitij.so 437
 
9825 kshitij.so 438
		if(!(getCourierCost(weight)==snapdealItem.getCourierCost())){
439
			compare_result=false;
440
		}
9923 kshitij.so 441
 
442
		if(!(vat_rate_prod==mpItem.getVat())){
9825 kshitij.so 443
			compare_result=false;
444
		}
9923 kshitij.so 445
 
9780 kshitij.so 446
		if(!(nlc==snapdealItem.getMaxNlc())){
447
			compare_result=false;
448
		}
449
		if(!(lowest_possible_tp-mpItem.getMinimumPossibleTp()>-1 && lowest_possible_tp-mpItem.getMinimumPossibleTp()<1)){
450
			compare_result=false;
451
		}
452
		if(!(our_tp-snapdealItem.getTransferPrice()>-1 && our_tp-snapdealItem.getTransferPrice()<1)){
453
			compare_result=false;
454
		}
455
		if(!(commission-snapdealItem.getCommission()>-1 && commission-snapdealItem.getCommission()<1)){
456
			compare_result=false;
457
		}
458
		if(!(service_tax-snapdealItem.getServiceTax()>-1 && service_tax-snapdealItem.getServiceTax()<1)){
459
			compare_result=false;
460
		}
461
		if(!(lowest_possible_sp-mpItem.getMinimumPossibleSp()>-1 && lowest_possible_sp-mpItem.getMinimumPossibleSp()<1)){
462
			compare_result=false;
463
		}
464
		return compare_result;
465
 
9734 kshitij.so 466
	}
9923 kshitij.so 467
 
9825 kshitij.so 468
	public double getVatRateForItem(Warehouse warehouse, double sellingPrice,long itemId) throws CatalogServiceException, TException{
469
		Client catalogClientProd = new CatalogClient().getClient();
470
		return catalogClientProd.getVatPercentageForItem(itemId, warehouse.getStateId(), sellingPrice);
471
	}
9780 kshitij.so 472
 
9734 kshitij.so 473
	public double getNlcForWarehouse(long warehouseId,long item_id) throws TException{
474
		InventoryClient inventoryServiceClient = new InventoryClient();
475
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
476
		return inventoryClient.getNlcForWarehouse(warehouseId, item_id);
477
	}
9780 kshitij.so 478
 
9734 kshitij.so 479
	public double getCourierCost(double weight){
9825 kshitij.so 480
		double cCost = 45.0;
481
		int slabs = (int) ((weight+.05 - .001)/(.5));
9734 kshitij.so 482
		for(int i=0;i<slabs;i++){
483
			cCost = cCost + 35;
484
		}
485
		return cCost;
486
	}
487
 
9923 kshitij.so 488
	public void getLastNDaySaleForItem(String itemId,String days) {
489
		try{
490
			InventoryClient inventoryServiceClient = new InventoryClient();
491
			in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
492
			List<OOSStatus> oosStatuses = inventoryClient.getOosStatusesForXDaysForItem(Long.valueOf(itemId), 7, Integer.valueOf(days));
493
			String lastNdaySale="";
494
			double avgSale = 0.0;
495
			long count = 0,sale = 0;
496
			for(OOSStatus oosStatus : oosStatuses){
497
				if(oosStatus.isIs_oos()){
498
					lastNdaySale += "X-";
499
				}else{
500
					lastNdaySale += oosStatus.getNum_orders() + "-";
501
					sale = sale + oosStatus.getNum_orders();
502
					count+=1;
503
				}
504
			}
505
			lastNdaySale = lastNdaySale.substring(0, lastNdaySale.length()-1);
506
			lastNdaySale += "\n";
507
			setnDaySale(lastNdaySale);
508
			if (count!=0){
9924 kshitij.so 509
				setAvgSale(String.valueOf(roundTwoDecimals(sale/(double)count)));
9923 kshitij.so 510
			}
511
			else{
512
				setAvgSale("0");
513
			}
514
		}
515
		catch (Exception e){
516
			setAvgSale("0");
517
			setnDaySale("Unable to fetch");
518
			logger.error("Unable to get last n day sale",e);
519
		}
9734 kshitij.so 520
 
9923 kshitij.so 521
	}
522
 
523
 
9725 kshitij.so 524
	private MarketplaceItems updateMarketplaceItemDetails(MarketplaceItems mpItem){
525
		mpItem.setItemId(Long.valueOf(itemId));
526
		mpItem.setSource(7);
527
		mpItem.setVat(Double.valueOf(vat));
528
		mpItem.setCourierCost(Double.valueOf(courierCost));
529
		mpItem.setOtherCost(Double.valueOf(otherCost));
530
		mpItem.setCurrentSp(Double.valueOf(sellingPrice));
531
		mpItem.setCurrentTp(Double.valueOf(transferPrice));
532
		mpItem.setAutoDecrement(Boolean.valueOf(isAutoDecrement));
533
		mpItem.setAutoIncrement(Boolean.valueOf(isAutoIncrement));
534
		mpItem.setManualFavourite(Boolean.valueOf(isManualFavourite));
535
		mpItem.setMinimumPossibleSp(Double.valueOf(minPosSp));
536
		mpItem.setMinimumPossibleTp(Double.valueOf(minPosTp));
9923 kshitij.so 537
		mpItem.setMaximumSellingPrice(Double.valueOf(maxSp));
9725 kshitij.so 538
		return mpItem;
539
	}
540
 
541
	private void sendAlert(long itemId,double sp,double minSp){
542
		Item item;
543
		try{
544
			Client catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
545
			item= catalogClient.getItem(itemId);
546
		}
547
		catch(Exception e){
548
			e.printStackTrace();
549
			return;
550
		}
551
		String emailSubjectTxt="Snapdeal SP is set below Min Possible SP";
9729 kshitij.so 552
		String[] sendTo=new String[]{ "kshitij.sood@saholic.com","rajneesh.arora@saholic.com","rajveer.singh@saholic.com","vikram.raghav@saholic.com"};
9780 kshitij.so 553
		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 554
		logger.info(text);
555
		String emailFromAddress = "build@shop2020.in";
556
		String password = "cafe@nes";
557
		GmailUtils mailer = new GmailUtils();
558
		try {
9780 kshitij.so 559
			mailer.sendSSLMessage(sendTo, emailSubjectTxt, text, emailFromAddress, password, new ArrayList<File>());
9725 kshitij.so 560
		}
561
		catch (Exception e) {
9729 kshitij.so 562
			logger.info(e.toString());
9725 kshitij.so 563
		}
564
	}
565
 
9780 kshitij.so 566
	public String addNewItem() throws TException, NumberFormatException, InventoryServiceException, CatalogServiceException, JSONException{
9725 kshitij.so 567
		InventoryClient inventoryServiceClient = new InventoryClient();
568
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
9780 kshitij.so 569
		Client catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
9825 kshitij.so 570
		Item item = catalogClient.getItem(Long.valueOf(itemId));
571
		//Client catalogClientProd = new CatalogClient().getClient();
9780 kshitij.so 572
		MarketplacePercentage mpCosting = catalogClient.getCostingForMarketplace(7, Long.valueOf(itemId));
9923 kshitij.so 573
 
9780 kshitij.so 574
		if (mpCosting.getSource()==0){
575
			throw new CatalogServiceException();
576
		}
9825 kshitij.so 577
		Warehouse warehouse = inventoryClient.getWarehouse(Long.valueOf(warehouseId));
578
		/*if (!(warehouse.getWarehouseType()==WarehouseType.OURS && warehouse.getInventoryType() == InventoryType.GOOD)){
9725 kshitij.so 579
			throw new InventoryServiceException();
9780 kshitij.so 580
		}*/
9242 kshitij.so 581
		SnapdealItem snapdealItem = new SnapdealItem();
582
		snapdealItem.setItem_id(Long.valueOf(itemId));
583
		snapdealItem.setWarehouseId(Long.valueOf(warehouseId));
584
		snapdealItem.setIsListedOnSnapdeal(Boolean.valueOf(isListedOnSnapdeal));
585
		snapdealItem.setSuppressInventoryFeed(Boolean.valueOf(isSuppressInventoryFeed));
586
		snapdealItem.setSuppressPriceFeed(Boolean.valueOf(isSuppressPriceFeed));
587
		snapdealItem.setTransferPrice(Double.valueOf(transferPrice));
588
		snapdealItem.setExceptionPrice(Double.valueOf(exceptionPrice));
589
		snapdealItem.setSellingPrice(Double.valueOf(sellingPrice));
590
		snapdealItem.setCourierCost(Double.valueOf(courierCost));
591
		snapdealItem.setCommission(Double.valueOf(commission));
592
		snapdealItem.setServiceTax(Double.valueOf(serviceTax));
9478 kshitij.so 593
		snapdealItem.setMaxNlc(Double.valueOf(maxNlc));
594
		snapdealItem.setSkuAtSnapdeal(sdItemCode);
9568 kshitij.so 595
		snapdealItem.setSupc(supc);
9780 kshitij.so 596
		snapdealItem.setUpdatedBy(getUserName());
9734 kshitij.so 597
 
9725 kshitij.so 598
		MarketplaceItems mpItem = new MarketplaceItems();
599
		MarketplaceItems t_mpItem = updateMarketplaceItemDetails(mpItem);
9825 kshitij.so 600
		/*double vat = catalogClientProd.getVatPercentageForItem(Long.valueOf(itemId), warehouse.getStateId(), Double.valueOf(sellingPrice));
601
		t_mpItem.setVat(vat);*/
9780 kshitij.so 602
		t_mpItem.setCommission(mpCosting.getCommission());
603
		t_mpItem.setServiceTax(mpCosting.getServiceTax());
604
		t_mpItem.setReturnProvision(mpCosting.getReturnProvision());
605
		t_mpItem.setEmiFee(mpCosting.getEmiFee());
606
		t_mpItem.setClosingFee(mpCosting.getClosingFee());
9825 kshitij.so 607
		if(!compareParameters(t_mpItem,snapdealItem,warehouse,item.getWeight())){
9780 kshitij.so 608
			setComparsionResult("0");
609
			return "snapdeal-update-result";
610
		}
611
		else{
612
			setComparsionResult("1");
613
		}
614
		snapdealItem.setMarketplaceItems(t_mpItem);
10225 vikram.rag 615
		Long timestamp = System.currentTimeMillis();
9780 kshitij.so 616
		boolean result = catalogClient.addOrUpdateSnapdealItem(snapdealItem);
9725 kshitij.so 617
		if(!result){
618
			throw new CatalogServiceException();
619
		}
620
		if (t_mpItem.getCurrentSp()< t_mpItem.getMinimumPossibleSp()){
621
			sendAlert(t_mpItem.getItemId(),t_mpItem.getCurrentSp(),t_mpItem.getMinimumPossibleSp());
622
		}
10225 vikram.rag 623
		UpdateSDPricingUsingPanel updatePriceOnSnapdeal = new UpdateSDPricingUsingPanel(Float.valueOf(sellingPrice),supc,item,timestamp);
624
		updatePriceOnSnapdeal.start();	
9780 kshitij.so 625
		return "snapdeal-update-result";
9242 kshitij.so 626
	}
627
 
9780 kshitij.so 628
 
9242 kshitij.so 629
	public String getAddNewItemForm(){
630
		return "snapdeal-add-item";
631
	}
632
 
9725 kshitij.so 633
	public String getItemDetailsInJson() throws NumberFormatException, CatalogServiceException, TException, JSONException, InventoryServiceException{
9825 kshitij.so 634
		Client catalogClientProd = new CatalogClient().getClient();
9242 kshitij.so 635
		Client catalogClient =new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
636
		SnapdealItem snapdealItem = catalogClient.getSnapdealItem(Long.valueOf(itemId));
9780 kshitij.so 637
		MarketplacePercentage mpCosting = catalogClient.getCostingForMarketplace(7, Long.valueOf(itemId));
9242 kshitij.so 638
		if (snapdealItem.getItem_id()!=0){
639
			throw new CatalogServiceException();
640
		}
641
		Item item = catalogClient.getItem(Long.valueOf(itemId));
642
		if (item.getId()==0){
643
			throw new CatalogServiceException();
644
		}
9478 kshitij.so 645
		InventoryClient inventoryServiceClient = new InventoryClient();
646
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
9825 kshitij.so 647
		Warehouse warehouse = inventoryClient.getWarehouse(Long.valueOf(warehouseId));
648
		/*if (!(warehouse.getWarehouseType()==WarehouseType.OURS && warehouse.getInventoryType() == InventoryType.GOOD)){
9725 kshitij.so 649
			throw new InventoryServiceException();
9780 kshitij.so 650
		}*/
651
		double maxNLC = inventoryClient.getNlcForWarehouse(Long.valueOf(warehouseId), Long.valueOf(itemId));
9825 kshitij.so 652
		double vat = catalogClientProd.getVatPercentageForItem(Long.valueOf(itemId), warehouse.getStateId(), Double.valueOf(sellingPrice));
9780 kshitij.so 653
		if (maxNLC==0){
654
			throw new InventoryServiceException();
9725 kshitij.so 655
		}
9242 kshitij.so 656
		itemObj = new JSONObject();
657
		itemObj.put("ItemId", item.getId());
658
		itemObj.put("Brand", item.getBrand());
659
		itemObj.put("ModelName", item.getModelName());
660
		itemObj.put("ModelNumber", item.getModelNumber());
661
		itemObj.put("Color", item.getColor());
662
		itemObj.put("Weight", item.getWeight());
663
		itemObj.put("Risky", item.isRisky());
664
		itemObj.put("Status", item.getItemStatus());
665
		itemObj.put("MRP", item.getMrp());
666
		itemObj.put("SellingPrice", item.getSellingPrice());
9478 kshitij.so 667
		itemObj.put("MaxNlc", maxNLC);
9825 kshitij.so 668
		itemObj.put("VatRate", vat);
9780 kshitij.so 669
		itemObj.put("CommissionRate", mpCosting.getCommission());
670
		itemObj.put("ServiceTaxRate", mpCosting.getServiceTax());
671
		itemObj.put("ReturnProvision", mpCosting.getReturnProvision());
672
		itemObj.put("EmiFee", mpCosting.getEmiFee());
673
		itemObj.put("ClosingFee", mpCosting.getClosingFee());
9242 kshitij.so 674
		return "item-details-json";
675
	}
676
 
9825 kshitij.so 677
	public String ke$ha() throws NumberFormatException, TException, JSONException, InventoryServiceException, CatalogServiceException{
678
		Client catalogClientProd = new CatalogClient().getClient();
9478 kshitij.so 679
		InventoryClient inventoryServiceClient = new InventoryClient();
680
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
9825 kshitij.so 681
		Warehouse warehouse = inventoryClient.getWarehouse(Long.valueOf(warehouseId));
9780 kshitij.so 682
		//if (warehouse.getWarehouseType()==WarehouseType.OURS && warehouse.getInventoryType() == InventoryType.GOOD){
683
		double maxNLC = inventoryClient.getNlcForWarehouse(Long.valueOf(warehouseId), Long.valueOf(itemId));
9825 kshitij.so 684
		double vat = catalogClientProd.getVatPercentageForItem(Long.valueOf(itemId), warehouse.getStateId(), Double.valueOf(sellingPrice));
9780 kshitij.so 685
		itemObj = new JSONObject();
686
		itemObj.put("ItemId", itemId);
687
		itemObj.put("WarehouseId", warehouseId);
688
		itemObj.put("MaxNlc", maxNLC);
9825 kshitij.so 689
		itemObj.put("VatRate", vat);
9780 kshitij.so 690
		/*}
9725 kshitij.so 691
		else{
692
			itemObj = new JSONObject();
693
			throw new InventoryServiceException();
9780 kshitij.so 694
		}*/
9478 kshitij.so 695
		return "item-details-json";
696
	}
697
 
9725 kshitij.so 698
	public ItemInventory getItemInventory(String itemId) throws NumberFormatException, InventoryServiceException, TException{
699
		try{
700
			InventoryClient inventoryServiceClient = new InventoryClient();
701
			in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
702
			in.shop2020.model.v1.inventory.ItemInventory thriftItemInventory = inventoryClient.getItemInventoryByItemId(Long.valueOf(itemId));
703
			return thriftItemInventory;
704
		}
705
		catch(Exception e){
706
			logger.error("Unable to get inventory info for item",e);
707
			return null;
708
		}
709
	}
710
 
9734 kshitij.so 711
 
712
 
713
	public Warehouse getWarehouse(String warehouseId,String itemId) throws NumberFormatException, TException, InventoryServiceException { 
9725 kshitij.so 714
		InventoryClient inventoryServiceClient = new InventoryClient();
9734 kshitij.so 715
		holdInventoryMap =inventoryServiceClient.getClient().getHeldInventoryMapForItem(Long.valueOf(itemId), Long.valueOf(warehouseId));
9725 kshitij.so 716
		return inventoryServiceClient.getClient().getWarehouse(Long.valueOf(warehouseId));
717
	}
718
 
9734 kshitij.so 719
	public Map<Integer, Long> getHoldInventoryMapForItem(){
720
		return holdInventoryMap;
721
	}
722
 
9725 kshitij.so 723
	public void changeHeldForSource() throws NumberFormatException, TException, InventoryServiceException{
724
		InventoryClient inventoryServiceClient = new InventoryClient();
725
		Warehouse warehouse = inventoryServiceClient.getClient().getWarehouse(Long.valueOf(warehouseId));
726
		if (!(warehouse.getWarehouseType()==WarehouseType.OURS && warehouse.getInventoryType() == InventoryType.GOOD)){
727
			throw new InventoryServiceException();
728
		}
729
		inventoryServiceClient.getClient().addUpdateHoldInventory(Long.valueOf(itemId),Long.valueOf(warehouseId),Long.valueOf(held),7);
730
	}
731
 
8739 vikram.rag 732
	public void downloadSnapdealListings() throws IOException, TException{
733
		File file = new File("/tmp/snapdeal-bulk-upload-template.xls");
734
		HSSFWorkbook hwb=new HSSFWorkbook();
735
		HSSFSheet sheet =  hwb.createSheet("Snapdeal-Listings");
736
		HSSFRow rowhead=   sheet.createRow((short)0);
737
		rowhead.createCell((short) 0).setCellValue("ITEM-ID");
9811 vikram.rag 738
		rowhead.createCell((short) 1).setCellValue("PRODUCT");
739
		rowhead.createCell((short) 2).setCellValue("WAREHOUSE-ID");
740
		rowhead.createCell((short) 3).setCellValue("EXCEPTIONAL-PRICE");
741
		rowhead.createCell((short) 4).setCellValue("SNAPDEAL-LISTED");
742
		rowhead.createCell((short) 5).setCellValue("TRANSFER-PRICE");
743
		rowhead.createCell((short) 6).setCellValue("SELLING-PRICE");
744
		rowhead.createCell((short) 7).setCellValue("LOWEST POSSIBLE SP");
745
		rowhead.createCell((short) 8).setCellValue("LOWEST POSSIBLE TP");
746
		rowhead.createCell((short) 9).setCellValue("OTHER COST");
747
		rowhead.createCell((short) 10).setCellValue("COURIER-COST");
748
		rowhead.createCell((short) 11).setCellValue("COMMISION");
749
		rowhead.createCell((short) 12).setCellValue("SERVICE-TAX");
750
		rowhead.createCell((short) 13).setCellValue("Suppress Inventory Feed");
751
		rowhead.createCell((short) 14).setCellValue("Suppress Price Feed");
752
		rowhead.createCell((short) 15).setCellValue("AUTO FAVOURITE");
753
		rowhead.createCell((short) 16).setCellValue("MANUAL FAVOURITE");
754
		rowhead.createCell((short) 17).setCellValue("AUTO INCREMENT");
755
		rowhead.createCell((short) 18).setCellValue("AUTO DECREMENT");
756
		rowhead.createCell((short) 19).setCellValue("Max NLC");
757
		rowhead.createCell((short) 20).setCellValue("SKU @ Snapdeal");
758
		rowhead.createCell((short) 21).setCellValue("SUPC");
9780 kshitij.so 759
		Client catalogClient = null;
9242 kshitij.so 760
		List<SnapdealItemDetails> snapdealItems = null;
8739 vikram.rag 761
		try {
9780 kshitij.so 762
			catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
8739 vikram.rag 763
			snapdealItems = catalogClient.getAllSnapdealItems();
764
		} catch (Exception e) {
765
			e.printStackTrace();
766
		}
767
		int iterator=1;
9811 vikram.rag 768
		Item item;
769
		MarketplaceItems marketplaceItem;
9242 kshitij.so 770
		for(SnapdealItemDetails snapdealItem:snapdealItems){
9811 vikram.rag 771
			marketplaceItem = catalogClient.getMarketplacedetailsForItem(snapdealItem.getItem_id(), 7);
772
			try {
773
				item = catalogClient.getItem(snapdealItem.getItem_id());
774
			} catch (CatalogServiceException e) {
775
				e.printStackTrace();
776
				continue;
777
			}
8739 vikram.rag 778
			HSSFRow row = sheet.createRow((short)iterator);
779
			row.createCell((short) 0).setCellValue(snapdealItem.getItem_id());
9812 vikram.rag 780
			row.createCell((short) 1).setCellValue(getVaildName(item.getBrand())+" "+getVaildName(item.getModelName())+" "+getVaildName(item.getModelNumber())+" "+getVaildName(item.getColor()));
9811 vikram.rag 781
			row.createCell((short) 2).setCellValue(snapdealItem.getWarehouseId());
782
			row.createCell((short) 3).setCellValue(roundTwoDecimals(snapdealItem.getExceptionPrice()));
8739 vikram.rag 783
			if(snapdealItem.isIsListedOnSnapdeal()){
9811 vikram.rag 784
				row.createCell((short) 4).setCellValue(1);
8739 vikram.rag 785
			}
786
			else{
9811 vikram.rag 787
				row.createCell((short) 4).setCellValue(0);
8739 vikram.rag 788
			}
9811 vikram.rag 789
			row.createCell((short) 5).setCellValue(roundTwoDecimals(snapdealItem.getTransferPrice()));
790
			row.createCell((short) 6).setCellValue(roundTwoDecimals(snapdealItem.getSellingPrice()));
791
			row.createCell((short) 7).setCellValue(roundTwoDecimals(marketplaceItem.getMinimumPossibleSp()));
792
			row.createCell((short) 8).setCellValue(roundTwoDecimals(marketplaceItem.getMinimumPossibleTp()));
793
			row.createCell((short) 9).setCellValue(roundTwoDecimals(marketplaceItem.getOtherCost()));
794
			row.createCell((short) 10).setCellValue(snapdealItem.getCourierCost());
795
			row.createCell((short) 11).setCellValue(roundTwoDecimals(snapdealItem.getCommission()));
796
			row.createCell((short) 12).setCellValue(roundTwoDecimals(snapdealItem.getServiceTax()));
9780 kshitij.so 797
			if(snapdealItem.isSuppressInventoryFeed()){
798
				row.createCell((short) 13).setCellValue(1);
799
			}
800
			else{
801
				row.createCell((short) 13).setCellValue(0);
802
			}
9811 vikram.rag 803
			if(snapdealItem.isSuppressPriceFeed()){
9780 kshitij.so 804
				row.createCell((short) 14).setCellValue(1);
805
			}
806
			else{
807
				row.createCell((short) 14).setCellValue(0);
808
			}
9811 vikram.rag 809
			if(marketplaceItem.isAutoFavourite()){
9780 kshitij.so 810
				row.createCell((short) 15).setCellValue(1);
811
			}
812
			else{
813
				row.createCell((short) 15).setCellValue(0);
814
			}
9811 vikram.rag 815
			if(marketplaceItem.isManualFavourite()){
9780 kshitij.so 816
				row.createCell((short) 16).setCellValue(1);
817
			}
818
			else{
819
				row.createCell((short) 16).setCellValue(0);
820
			}
9811 vikram.rag 821
			if(marketplaceItem.isAutoIncrement()){
9780 kshitij.so 822
				row.createCell((short) 17).setCellValue(1);
823
			}
824
			else{
825
				row.createCell((short) 17).setCellValue(0);
826
			}
9811 vikram.rag 827
			if(marketplaceItem.isAutoDecrement()){
828
				row.createCell((short) 18).setCellValue(1);
829
			}
830
			else{
831
				row.createCell((short) 18).setCellValue(0);
832
			}
833
			row.createCell((short) 19).setCellValue(snapdealItem.getMaxNlc());
834
			row.createCell((short) 20).setCellValue(snapdealItem.getSkuAtSnapdeal());
835
			row.createCell((short) 21).setCellValue(snapdealItem.getSupc());
8739 vikram.rag 836
			iterator++;
837
		}
9242 kshitij.so 838
 
8739 vikram.rag 839
		FileOutputStream fileOut = null;
840
		try {
841
			fileOut = new FileOutputStream(file);
842
		} catch (FileNotFoundException e) {
843
			// TODO Auto-generated catch block
844
			e.printStackTrace();
845
		}
846
		try {
847
			hwb.write(fileOut);
848
		} catch (IOException e) {
849
			// TODO Auto-generated catch block
850
			e.printStackTrace();
851
		}
852
		try {
853
			fileOut.close();
854
		} catch (IOException e) {
855
			// TODO Auto-generated catch block
856
			e.printStackTrace();
857
		}
858
		byte[] buffer = new byte[(int)file.length()];
859
		InputStream input = null;
860
		try {
861
			int totalBytesRead = 0;
862
			input = new BufferedInputStream(new FileInputStream(file));
863
			while(totalBytesRead < buffer.length){
864
				int bytesRemaining = buffer.length - totalBytesRead;
865
				int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
866
				if (bytesRead > 0){
867
					totalBytesRead = totalBytesRead + bytesRead;
868
				}
869
			}
870
		}
871
		finally {
872
			input.close();
873
			file.delete();
874
		}
875
 
876
		response.setHeader("Content-Disposition", "attachment; filename=\"Snapdeal-Bulk-Listings.xls\"");
877
		response.setContentType("application/octet-stream");
878
		ServletOutputStream sos;
879
		try {
880
			sos = response.getOutputStream();
881
			sos.write(buffer);
882
			sos.flush();
883
		} catch (IOException e) {
884
			System.out.println("Unable to stream the manifest file");
885
		}   
886
 
9242 kshitij.so 887
 
8739 vikram.rag 888
	}
889
 
9825 kshitij.so 890
 
9780 kshitij.so 891
	public void uploadsnapdealBulkSheet() throws IOException, TException, NumberFormatException, InventoryServiceException, CatalogServiceException{
8739 vikram.rag 892
		File fileToCreate = new File("/tmp/", "Snapdeal-bulk-upload.xls");
893
		FileUtils.copyFile(this.file, fileToCreate);
894
		FileInputStream iFile = new FileInputStream(new File(file.getAbsolutePath()));
895
		HSSFWorkbook workbook = new HSSFWorkbook(iFile);
896
		HSSFSheet sheet = workbook.getSheetAt(0);
9780 kshitij.so 897
		String updatedBy = getUserName();
8887 vikram.rag 898
		Client catalogClient=null;
9780 kshitij.so 899
		Client catalogClientProd=null;
8739 vikram.rag 900
		StringBuilder sb = new StringBuilder();
901
		try {
9242 kshitij.so 902
			catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
9780 kshitij.so 903
			catalogClientProd = new CatalogClient().getClient();
8739 vikram.rag 904
		} catch (TTransportException e) {
905
			e.printStackTrace();
906
		}
9478 kshitij.so 907
		InventoryClient inventoryServiceClient = new InventoryClient();
908
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
9825 kshitij.so 909
 
8739 vikram.rag 910
		for (int iterator=(sheet.getFirstRowNum()+1);iterator<=sheet.getLastRowNum();iterator++){
9780 kshitij.so 911
			if (sheet.getLastRowNum()-1>100){
912
				sb.append("Number of items to update can't be greater than 100");
913
				break;
914
			}
915
			SnapdealItem snapdealItem = null;
916
			MarketplaceItems marketplaceItem = null;
917
			MarketplacePercentage mpCosting = null;
9825 kshitij.so 918
			Warehouse warehouse = null;
9242 kshitij.so 919
			Item item = null;
8739 vikram.rag 920
			Long sku;
9595 kshitij.so 921
			boolean new_item = false;
8739 vikram.rag 922
			if (checkEmptyString(sheet.getRow(iterator).getCell(0))){
923
				continue;
924
			}
925
			else {
8791 kshitij.so 926
				sku=(long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
8887 vikram.rag 927
				snapdealItem = catalogClient.getSnapdealItem(sku);
9780 kshitij.so 928
				mpCosting = catalogClient.getCostingForMarketplace(7, sku);
929
				marketplaceItem  = catalogClient.getMarketplacedetailsForItem(sku, 7);
930
				marketplaceItem.setItemId(sku);
931
				marketplaceItem.setSource(7);
932
				marketplaceItem.setCommission(mpCosting.getCommission());
933
				marketplaceItem.setServiceTax(mpCosting.getServiceTax());
934
				marketplaceItem.setReturnProvision(mpCosting.getReturnProvision());
935
				marketplaceItem.setEmiFee(mpCosting.getEmiFee());
936
				marketplaceItem.setClosingFee(mpCosting.getClosingFee());
9825 kshitij.so 937
 
9242 kshitij.so 938
				try {
939
					item = catalogClient.getItem(sku);
940
					if (item.getId()==0){
9780 kshitij.so 941
						sb.append(sku + " Item not valid"+"\n");
9242 kshitij.so 942
						continue;
943
					}
944
				} catch (CatalogServiceException e) {
9780 kshitij.so 945
					sb.append(sku + " Item not valid"+"\n");
9242 kshitij.so 946
					continue;
947
				}
8886 vikram.rag 948
				if(snapdealItem.getItem_id()==0){
9595 kshitij.so 949
					new_item = true;
8886 vikram.rag 950
					snapdealItem = new SnapdealItem();
951
					snapdealItem.setItem_id(sku); 
952
				}
8739 vikram.rag 953
			}
9725 kshitij.so 954
 
9595 kshitij.so 955
			if (checkEmptyString(sheet.getRow(iterator).getCell(1)) && new_item){
956
				sb.append(sku + " New Listing - Warehouse Id not Present"+"\n");
957
				continue;
958
			}
9725 kshitij.so 959
 
8739 vikram.rag 960
			if (!checkEmptyString(sheet.getRow(iterator).getCell(1))){
9780 kshitij.so 961
				/*Warehouse warehouse = inventoryClient.getWarehouse(Long.valueOf(warehouseId));
9725 kshitij.so 962
				if (!(warehouse.getWarehouseType()==WarehouseType.OURS && warehouse.getInventoryType() == InventoryType.GOOD)){
963
					sb.append(sku + "Not acceptable warehouse"+"\n");
964
					continue;
9780 kshitij.so 965
				}*/
8739 vikram.rag 966
				long warehouseId = (long) sheet.getRow(iterator).getCell(1).getNumericCellValue();
9478 kshitij.so 967
				double maxNLC = inventoryClient.getNlcForWarehouse(warehouseId, item.getId());
9595 kshitij.so 968
				if (maxNLC==0){
969
					sb.append(sku + "Max Nlc can't be 0"+"\n");
970
					continue;
971
				}
8739 vikram.rag 972
				snapdealItem.setWarehouseId(warehouseId);
9478 kshitij.so 973
				snapdealItem.setMaxNlc(maxNLC);
8739 vikram.rag 974
			}
975
 
976
			if (!checkEmptyString(sheet.getRow(iterator).getCell(2))){
977
				double exceptionPrice = sheet.getRow(iterator).getCell(2).getNumericCellValue();
978
				snapdealItem.setExceptionPrice(exceptionPrice);
979
			}
980
 
981
			if (!checkEmptyString(sheet.getRow(iterator).getCell(3))){
982
				if ((long)sheet.getRow(iterator).getCell(3).getNumericCellValue()==1){
983
					snapdealItem.setIsListedOnSnapdeal(true);
984
				}
985
				if ((long)sheet.getRow(iterator).getCell(3).getNumericCellValue()==0){
986
					snapdealItem.setIsListedOnSnapdeal(false);
987
				}
988
			}
9825 kshitij.so 989
 
9780 kshitij.so 990
			double transferPrice = 0,sellingPrice,commission,courierCost =45,serviceTax,otherCost;
991
			if (checkEmptyString(sheet.getRow(iterator).getCell(4))&& new_item){
992
				sb.append(sku + "New Item -  Selling Price cannot be empty"+"\n");
9242 kshitij.so 993
				continue;
994
			}
995
			if (!checkEmptyString(sheet.getRow(iterator).getCell(4))){
9780 kshitij.so 996
				sellingPrice = sheet.getRow(iterator).getCell(4).getNumericCellValue();
997
				if(sellingPrice==0){
998
					sb.append(sku + " Selling Price cannot be zero"+"\n");
9242 kshitij.so 999
					continue;
1000
				}
9780 kshitij.so 1001
				snapdealItem.setSellingPrice(sellingPrice);
1002
				marketplaceItem.setCurrentSp(sellingPrice);
9242 kshitij.so 1003
			}
9825 kshitij.so 1004
 
9780 kshitij.so 1005
			if (!checkEmptyString(sheet.getRow(iterator).getCell(5))){
1006
				otherCost = sheet.getRow(iterator).getCell(5).getNumericCellValue();
1007
				marketplaceItem.setOtherCost(otherCost);
1008
			}
9242 kshitij.so 1009
 
9780 kshitij.so 1010
 
1011
			if (!checkEmptyString(sheet.getRow(iterator).getCell(6))){
1012
				if ((long)sheet.getRow(iterator).getCell(6).getNumericCellValue()==1){
9242 kshitij.so 1013
					snapdealItem.setSuppressPriceFeed(true);
1014
				}
9780 kshitij.so 1015
				if ((long)sheet.getRow(iterator).getCell(6).getNumericCellValue()==0){
9242 kshitij.so 1016
					snapdealItem.setSuppressPriceFeed(false);
1017
				}
1018
			}
1019
 
9780 kshitij.so 1020
			if (!checkEmptyString(sheet.getRow(iterator).getCell(7))){
1021
				if ((long)sheet.getRow(iterator).getCell(7).getNumericCellValue()==1){
9242 kshitij.so 1022
					snapdealItem.setSuppressInventoryFeed(true);
1023
				}
9780 kshitij.so 1024
				if ((long)sheet.getRow(iterator).getCell(7).getNumericCellValue()==0){
9242 kshitij.so 1025
					snapdealItem.setSuppressInventoryFeed(false);
1026
				}
1027
			}
9725 kshitij.so 1028
 
9780 kshitij.so 1029
			if (checkEmptyString(sheet.getRow(iterator).getCell(8)) && new_item){
1030
				sb.append(sku + " New Item -  SKU@Snapdeal cannot be empty"+"\n");
1031
				continue;
9478 kshitij.so 1032
			}
9725 kshitij.so 1033
 
9568 kshitij.so 1034
			if (!checkEmptyString(sheet.getRow(iterator).getCell(8))){
9597 kshitij.so 1035
				sheet.getRow(iterator).getCell(8).setCellType(Cell.CELL_TYPE_STRING);
9780 kshitij.so 1036
				String snapdealItemCode = sheet.getRow(iterator).getCell(8).getStringCellValue();
1037
				if (snapdealItemCode.length()==0 && new_item){
1038
					sb.append(sku + " New Item -  SKU@Snapdeal cannot be empty"+"\n");
1039
					continue;
1040
				}
1041
				snapdealItem.setSkuAtSnapdeal(snapdealItemCode);
1042
			}
9825 kshitij.so 1043
 
9780 kshitij.so 1044
			if (checkEmptyString(sheet.getRow(iterator).getCell(9)) && new_item){
1045
				sb.append(sku + " New Item -  SUPC code cannot be empty"+"\n");
1046
				continue;
1047
			}
1048
 
1049
			if (!checkEmptyString(sheet.getRow(iterator).getCell(9))){
1050
				sheet.getRow(iterator).getCell(9).setCellType(Cell.CELL_TYPE_STRING);
1051
				String supc = sheet.getRow(iterator).getCell(9).getStringCellValue();
1052
				if (supc.length()==0 && new_item){
1053
					sb.append(sku + "  New Item -  SUPC code cannot be empty"+"\n");
1054
					continue;
1055
				}
9568 kshitij.so 1056
				snapdealItem.setSupc(supc);
1057
			}
9242 kshitij.so 1058
			double weight = item.getWeight();
1059
 
9825 kshitij.so 1060
 
9780 kshitij.so 1061
			if(weight==0){
9242 kshitij.so 1062
				sb.append(sku + " Please add weight"+"\n");
1063
				continue;
9780 kshitij.so 1064
			}
9242 kshitij.so 1065
			if (weight!=0){
9780 kshitij.so 1066
				weight = weight+.05;
9242 kshitij.so 1067
				int slabs = (int) ((weight - .001)/(.5));
1068
 
1069
				for(int i=0;i<slabs;i++){
1070
					courierCost = courierCost + 35;
1071
				}
1072
			}
10225 vikram.rag 1073
 
10114 kshitij.so 1074
			warehouse = inventoryClient.getWarehouse(snapdealItem.getWarehouseId());
9242 kshitij.so 1075
			snapdealItem.setCourierCost(courierCost);
9780 kshitij.so 1076
			marketplaceItem.setCourierCost(courierCost);
1077
			transferPrice = roundTwoDecimals(snapdealItem.getSellingPrice()- snapdealItem.getSellingPrice()*(marketplaceItem.getCommission()/100+marketplaceItem.getEmiFee()/100)*(1+(marketplaceItem.getServiceTax()/100))-(snapdealItem.getCourierCost()+marketplaceItem.getClosingFee())*(1+(marketplaceItem.getServiceTax()/100)));
1078
			snapdealItem.setTransferPrice(transferPrice);
1079
			marketplaceItem.setCurrentTp(transferPrice);
1080
			commission = roundTwoDecimals(((marketplaceItem.getCommission()/100)*snapdealItem.getSellingPrice()));
9242 kshitij.so 1081
			snapdealItem.setCommission(commission);
9780 kshitij.so 1082
			serviceTax = roundTwoDecimals((marketplaceItem.getServiceTax()/100)*(snapdealItem.getCommission()+snapdealItem.getCourierCost())); 
9242 kshitij.so 1083
			snapdealItem.setServiceTax(serviceTax);
9825 kshitij.so 1084
			double vatRate = catalogClientProd.getVatPercentageForItem(snapdealItem.getItem_id(),warehouse.getStateId(),snapdealItem.getSellingPrice());
9780 kshitij.so 1085
			marketplaceItem.setVat(vatRate);
1086
			double vat = roundTwoDecimals((snapdealItem.getSellingPrice()/(1+(marketplaceItem.getVat()/100))-(snapdealItem.getMaxNlc()/(1+(marketplaceItem.getVat()/100))))*(marketplaceItem.getVat()/100));
1087
			double inHouseCost = roundTwoDecimals(15+vat+(marketplaceItem.getReturnProvision()/100)*snapdealItem.getSellingPrice()+marketplaceItem.getOtherCost());
1088
			double lowest_possible_tp = roundTwoDecimals(snapdealItem.getMaxNlc()+inHouseCost);
1089
			marketplaceItem.setMinimumPossibleTp(lowest_possible_tp);
1090
			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)));
1091
			marketplaceItem.setMinimumPossibleSp(lowest_possible_sp);
1092
			snapdealItem.setUpdatedBy(updatedBy);
1093
			snapdealItem.setMarketplaceItems(marketplaceItem);
9825 kshitij.so 1094
			//logger.info(snapdealItem.toString());
8888 vikram.rag 1095
			if(!catalogClient.addOrUpdateSnapdealItem(snapdealItem)){
9780 kshitij.so 1096
				sb.append(sku + " Service Error\n");
8739 vikram.rag 1097
			}	
1098
		}
9780 kshitij.so 1099
		File file = new File("/tmp/snapdealbulk");
8739 vikram.rag 1100
		FileWriter writer = new FileWriter(file);
1101
		writer.append(sb.toString());
1102
		writer.close();
1103
		byte[] buffer = new byte[(int)file.length()];
1104
		InputStream input = null;
1105
		try {
1106
			int totalBytesRead = 0;
1107
			input = new BufferedInputStream(new FileInputStream(file));
1108
			while(totalBytesRead < buffer.length){
1109
				int bytesRemaining = buffer.length - totalBytesRead;
1110
				int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
1111
				if (bytesRead > 0){
1112
					totalBytesRead = totalBytesRead + bytesRead;
1113
				}
1114
			}
1115
		}
1116
		finally {
1117
			input.close();
1118
			file.delete();
1119
		}
1120
 
1121
		response.setHeader("Content-Type", "text/javascript");
1122
 
1123
		ServletOutputStream sos;
1124
		try {
1125
			sos = response.getOutputStream();
1126
			sos.write(buffer);
1127
			sos.flush();
1128
		} catch (IOException e) {
1129
			System.out.println("Unable to stream the manifest file");
1130
		}   
1131
	}
1132
 
1133
	public boolean checkEmptyString(Cell cell){
10114 kshitij.so 1134
		if (cell==null || cell.getCellType() == Cell.CELL_TYPE_BLANK){
8739 vikram.rag 1135
			return true;
1136
		}
1137
		return false;
1138
	}
1139
 
9242 kshitij.so 1140
	double roundTwoDecimals(double d) {
1141
		DecimalFormat twoPlaces = new DecimalFormat("#.##");
1142
		return Double.valueOf(twoPlaces.format(d));
1143
	}
1144
 
9825 kshitij.so 1145
	/*String getLocation(String location){
1146
		logger.info("Inside trim"+location);
1147
		int loc = location.lastIndexOf(',');
1148
		logger.info("end result "+location.substring(loc+1));
1149
		return location.substring(loc+1);
1150
	}*/
9242 kshitij.so 1151
 
9825 kshitij.so 1152
 
8739 vikram.rag 1153
	public String show() {
1154
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1])) {
1155
			return "authfail";
1156
		}
1157
 
1158
		if (StringUtils.equals(id, "snapdeal-options")){
1159
			return "snapdeal-options";
1160
		}
9242 kshitij.so 1161
		if (StringUtils.equals(id, "item-table")){
1162
			return "snapdeal-item-table";
1163
		}
8739 vikram.rag 1164
 
1165
		return "id";
1166
	}
1167
 
1168
	public void setId(String id) {
1169
		this.id = id;
1170
	}
1171
 
9780 kshitij.so 1172
	public String getUserName(){
1173
		return session.getAttribute(ReportsUtils.USER_NAME).toString();
1174
	}
1175
 
8739 vikram.rag 1176
	public HttpServletRequest getRequest() {
1177
		logger.info("set request"+request.toString());
1178
		return request;
1179
	}
1180
 
1181
	public void setRequest(HttpServletRequest request) {
1182
		this.request = request;
1183
	}
1184
 
1185
	public HttpServletResponse getResponse() {
1186
		return response;
1187
	}
1188
 
1189
	public void setResponse(HttpServletResponse response) {
1190
		this.response = response;
1191
	}
1192
 
1193
	public HttpSession getSession() {
1194
		return session;
1195
	}
1196
 
1197
	public void setSession(HttpSession session) {
1198
		this.session = session;
1199
	}
1200
 
1201
	public ServletContext getContext() {
1202
		return context;
1203
	}
1204
 
1205
	public void setContext(ServletContext context) {
1206
		this.context = context;
1207
	}
1208
 
1209
	public String getUrl() {
1210
		return url;
1211
	}
1212
 
1213
	public void setUrl(String url) {
1214
		this.url = url;
1215
	}
1216
 
1217
	public String getItemId() {
1218
		return itemId;
1219
	}
1220
 
1221
	public void setItemId(String itemId) {
1222
		this.itemId = itemId;
1223
	}
1224
 
1225
	public String getIsSnapdealListed() {
1226
		return isSnapdealListed;
1227
	}
1228
 
1229
	public void setIsSnapdealListed(String isSnapdealListed) {
1230
		this.isSnapdealListed = isSnapdealListed;
1231
	}
1232
 
1233
	public String getExceptionPrice() {
1234
		return exceptionPrice;
1235
	}
1236
 
1237
	public void setExceptionPrice(String exceptionPrice) {
1238
		this.exceptionPrice = exceptionPrice;
1239
	}
1240
 
1241
	public String getWarehouseId() {
1242
		return warehouseId;
1243
	}
1244
 
1245
	public void setWarehouseId(String warehouseId) {
1246
		this.warehouseId = warehouseId;
1247
	}
1248
 
1249
	public File getFile() {
1250
		return file;
1251
	}
1252
 
1253
	public void setFile(File file) {
1254
		this.file = file;
1255
	}
1256
 
1257
	public String getErrMsg() {
1258
		return errMsg;
1259
	}
1260
 
1261
	public void setErrMsg(String errMsg) {
1262
		this.errMsg = errMsg;
1263
	}
1264
 
1265
	public String getNext() {
1266
		return next;
1267
	}
1268
 
1269
	public void setNext(String next) {
1270
		this.next = next;
1271
	}
1272
 
1273
	public String getId() {
1274
		return id;
1275
	}
1276
 
1277
	public void setServletRequest(HttpServletRequest req) {
1278
		this.request = req;
1279
		this.session = req.getSession();        
1280
	}
1281
 
1282
	public void setServletContext(ServletContext arg0) {
1283
		// TODO Auto-generated method stub
1284
 
1285
	}
1286
 
1287
	public void setServletResponse(HttpServletResponse response) {
1288
		this.response = response;
1289
	}
1290
 
9242 kshitij.so 1291
	public void setSearchText(String searchText) {
1292
		this.searchText = searchText;
1293
	}
1294
 
1295
	public String getSearchText() {
1296
		return searchText;
1297
	}
1298
 
1299
	public long getSearchCount() {
1300
		return searchCount;
1301
	}
1302
 
1303
	public long getTotalCount() {
1304
		return totalCount;
1305
	}
1306
 
1307
	public void setSearchCount(long count) {
1308
		this.searchCount = count;
1309
	}
1310
 
1311
	public List<SnapdealItemDetails> getSnapdealItems(){
1312
		return snapdealItems;
1313
	}
1314
 
1315
	public void setSellingPrice(String sellingPrice) {
1316
		this.sellingPrice = sellingPrice;
1317
	}
1318
 
1319
	public String getSellingPrice() {
1320
		return sellingPrice;
1321
	}
1322
 
1323
	public void setTransferPrice(String transferPrice) {
1324
		this.transferPrice = transferPrice;
1325
	}
1326
 
1327
	public String getTransferPrice() {
1328
		return transferPrice;
1329
	}
1330
 
1331
	public void setWebisteMrp(String webisteMrp) {
1332
		this.webisteMrp = webisteMrp;
1333
	}
1334
 
1335
	public String getWebisteMrp() {
1336
		return webisteMrp;
1337
	}
1338
 
1339
	public void setWebisteSellingPrice(String webisteSellingPrice) {
1340
		this.webisteSellingPrice = webisteSellingPrice;
1341
	}
1342
 
1343
	public String getWebisteSellingPrice() {
1344
		return webisteSellingPrice;
1345
	}
1346
 
1347
	public void setIsListedOnSnapdeal(String isListedOnSnapdeal) {
1348
		this.isListedOnSnapdeal = isListedOnSnapdeal;
1349
	}
1350
 
1351
	public String getIsSuppressInventoryFeed() {
1352
		return isSuppressInventoryFeed;
1353
	}
1354
 
1355
	public void setIsSuppressInventoryFeed(String isSuppressInventoryFeed) {
1356
		this.isSuppressInventoryFeed = isSuppressInventoryFeed;
1357
	}
1358
 
1359
	public String getIsSuppressPriceFeed() {
1360
		return isSuppressPriceFeed;
1361
	}
1362
 
1363
	public void setIsSuppressPriceFeed(String isSuppressPriceFeed) {
1364
		this.isSuppressPriceFeed = isSuppressPriceFeed;
1365
	}
1366
 
1367
	public String getIsListedOnSnapdeal() {
1368
		return isListedOnSnapdeal;
1369
	}
1370
 
1371
	public void setCommission(String commission) {
1372
		this.commission = commission;
1373
	}
1374
 
1375
	public String getCommission() {
1376
		return commission;
1377
	}
1378
 
1379
	public void setServiceTax(String serviceTax) {
1380
		this.serviceTax = serviceTax;
1381
	}
1382
 
1383
	public String getServiceTax() {
1384
		return serviceTax;
1385
	}
1386
 
1387
	public void setCourierCost(String courierCost) {
1388
		this.courierCost = courierCost;
1389
	}
1390
 
1391
	public String getCourierCost() {
1392
		return courierCost;
1393
	}
1394
 
9478 kshitij.so 1395
	public void setMaxNlc(String maxNlc) {
1396
		this.maxNlc = maxNlc;
1397
	}
1398
 
1399
	public String getMaxNlc() {
1400
		return maxNlc;
1401
	}
1402
 
9242 kshitij.so 1403
	public String getItemDetails(){
1404
		return itemObj.toString();
1405
	}
1406
 
9725 kshitij.so 1407
	public double getRoundOfValue(String val){
1408
		return roundTwoDecimals(Double.valueOf(val));
1409
	}
9923 kshitij.so 1410
 
9812 vikram.rag 1411
	public String getVaildName(String name){
1412
		return name!=null?name:"";
1413
	}
9242 kshitij.so 1414
 
1415
 
9725 kshitij.so 1416
 
9242 kshitij.so 1417
}