Subversion Repositories SmartDukaan

Rev

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

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