Subversion Repositories SmartDukaan

Rev

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