Subversion Repositories SmartDukaan

Rev

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