Subversion Repositories SmartDukaan

Rev

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