Subversion Repositories SmartDukaan

Rev

Rev 9811 | Rev 9825 | 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");
9811 vikram.rag 630
		rowhead.createCell((short) 1).setCellValue("PRODUCT");
631
		rowhead.createCell((short) 2).setCellValue("WAREHOUSE-ID");
632
		rowhead.createCell((short) 3).setCellValue("EXCEPTIONAL-PRICE");
633
		rowhead.createCell((short) 4).setCellValue("SNAPDEAL-LISTED");
634
		rowhead.createCell((short) 5).setCellValue("TRANSFER-PRICE");
635
		rowhead.createCell((short) 6).setCellValue("SELLING-PRICE");
636
		rowhead.createCell((short) 7).setCellValue("LOWEST POSSIBLE SP");
637
		rowhead.createCell((short) 8).setCellValue("LOWEST POSSIBLE TP");
638
		rowhead.createCell((short) 9).setCellValue("OTHER COST");
639
		rowhead.createCell((short) 10).setCellValue("COURIER-COST");
640
		rowhead.createCell((short) 11).setCellValue("COMMISION");
641
		rowhead.createCell((short) 12).setCellValue("SERVICE-TAX");
642
		rowhead.createCell((short) 13).setCellValue("Suppress Inventory Feed");
643
		rowhead.createCell((short) 14).setCellValue("Suppress Price Feed");
644
		rowhead.createCell((short) 15).setCellValue("AUTO FAVOURITE");
645
		rowhead.createCell((short) 16).setCellValue("MANUAL FAVOURITE");
646
		rowhead.createCell((short) 17).setCellValue("AUTO INCREMENT");
647
		rowhead.createCell((short) 18).setCellValue("AUTO DECREMENT");
648
		rowhead.createCell((short) 19).setCellValue("Max NLC");
649
		rowhead.createCell((short) 20).setCellValue("SKU @ Snapdeal");
650
		rowhead.createCell((short) 21).setCellValue("SUPC");
9780 kshitij.so 651
		Client catalogClient = null;
9242 kshitij.so 652
		List<SnapdealItemDetails> snapdealItems = null;
8739 vikram.rag 653
		try {
9780 kshitij.so 654
			catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
8739 vikram.rag 655
			snapdealItems = catalogClient.getAllSnapdealItems();
656
		} catch (Exception e) {
657
			e.printStackTrace();
658
		}
659
		int iterator=1;
9811 vikram.rag 660
		Item item;
661
		MarketplaceItems marketplaceItem;
9242 kshitij.so 662
		for(SnapdealItemDetails snapdealItem:snapdealItems){
9811 vikram.rag 663
			marketplaceItem = catalogClient.getMarketplacedetailsForItem(snapdealItem.getItem_id(), 7);
664
			try {
665
				item = catalogClient.getItem(snapdealItem.getItem_id());
666
			} catch (CatalogServiceException e) {
667
				e.printStackTrace();
668
				continue;
669
			}
8739 vikram.rag 670
			HSSFRow row = sheet.createRow((short)iterator);
671
			row.createCell((short) 0).setCellValue(snapdealItem.getItem_id());
9812 vikram.rag 672
			row.createCell((short) 1).setCellValue(getVaildName(item.getBrand())+" "+getVaildName(item.getModelName())+" "+getVaildName(item.getModelNumber())+" "+getVaildName(item.getColor()));
9811 vikram.rag 673
			row.createCell((short) 2).setCellValue(snapdealItem.getWarehouseId());
674
			row.createCell((short) 3).setCellValue(roundTwoDecimals(snapdealItem.getExceptionPrice()));
8739 vikram.rag 675
			if(snapdealItem.isIsListedOnSnapdeal()){
9811 vikram.rag 676
				row.createCell((short) 4).setCellValue(1);
8739 vikram.rag 677
			}
678
			else{
9811 vikram.rag 679
				row.createCell((short) 4).setCellValue(0);
8739 vikram.rag 680
			}
9811 vikram.rag 681
			row.createCell((short) 5).setCellValue(roundTwoDecimals(snapdealItem.getTransferPrice()));
682
			row.createCell((short) 6).setCellValue(roundTwoDecimals(snapdealItem.getSellingPrice()));
683
			row.createCell((short) 7).setCellValue(roundTwoDecimals(marketplaceItem.getMinimumPossibleSp()));
684
			row.createCell((short) 8).setCellValue(roundTwoDecimals(marketplaceItem.getMinimumPossibleTp()));
685
			row.createCell((short) 9).setCellValue(roundTwoDecimals(marketplaceItem.getOtherCost()));
686
			row.createCell((short) 10).setCellValue(snapdealItem.getCourierCost());
687
			row.createCell((short) 11).setCellValue(roundTwoDecimals(snapdealItem.getCommission()));
688
			row.createCell((short) 12).setCellValue(roundTwoDecimals(snapdealItem.getServiceTax()));
9780 kshitij.so 689
			if(snapdealItem.isSuppressInventoryFeed()){
690
				row.createCell((short) 13).setCellValue(1);
691
			}
692
			else{
693
				row.createCell((short) 13).setCellValue(0);
694
			}
9811 vikram.rag 695
			if(snapdealItem.isSuppressPriceFeed()){
9780 kshitij.so 696
				row.createCell((short) 14).setCellValue(1);
697
			}
698
			else{
699
				row.createCell((short) 14).setCellValue(0);
700
			}
9811 vikram.rag 701
			if(marketplaceItem.isAutoFavourite()){
9780 kshitij.so 702
				row.createCell((short) 15).setCellValue(1);
703
			}
704
			else{
705
				row.createCell((short) 15).setCellValue(0);
706
			}
9811 vikram.rag 707
			if(marketplaceItem.isManualFavourite()){
9780 kshitij.so 708
				row.createCell((short) 16).setCellValue(1);
709
			}
710
			else{
711
				row.createCell((short) 16).setCellValue(0);
712
			}
9811 vikram.rag 713
			if(marketplaceItem.isAutoIncrement()){
9780 kshitij.so 714
				row.createCell((short) 17).setCellValue(1);
715
			}
716
			else{
717
				row.createCell((short) 17).setCellValue(0);
718
			}
9811 vikram.rag 719
			if(marketplaceItem.isAutoDecrement()){
720
				row.createCell((short) 18).setCellValue(1);
721
			}
722
			else{
723
				row.createCell((short) 18).setCellValue(0);
724
			}
725
			row.createCell((short) 19).setCellValue(snapdealItem.getMaxNlc());
726
			row.createCell((short) 20).setCellValue(snapdealItem.getSkuAtSnapdeal());
727
			row.createCell((short) 21).setCellValue(snapdealItem.getSupc());
8739 vikram.rag 728
			iterator++;
729
		}
9242 kshitij.so 730
 
8739 vikram.rag 731
		FileOutputStream fileOut = null;
732
		try {
733
			fileOut = new FileOutputStream(file);
734
		} catch (FileNotFoundException e) {
735
			// TODO Auto-generated catch block
736
			e.printStackTrace();
737
		}
738
		try {
739
			hwb.write(fileOut);
740
		} catch (IOException e) {
741
			// TODO Auto-generated catch block
742
			e.printStackTrace();
743
		}
744
		try {
745
			fileOut.close();
746
		} catch (IOException e) {
747
			// TODO Auto-generated catch block
748
			e.printStackTrace();
749
		}
750
		byte[] buffer = new byte[(int)file.length()];
751
		InputStream input = null;
752
		try {
753
			int totalBytesRead = 0;
754
			input = new BufferedInputStream(new FileInputStream(file));
755
			while(totalBytesRead < buffer.length){
756
				int bytesRemaining = buffer.length - totalBytesRead;
757
				int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
758
				if (bytesRead > 0){
759
					totalBytesRead = totalBytesRead + bytesRead;
760
				}
761
			}
762
		}
763
		finally {
764
			input.close();
765
			file.delete();
766
		}
767
 
768
		response.setHeader("Content-Disposition", "attachment; filename=\"Snapdeal-Bulk-Listings.xls\"");
769
		response.setContentType("application/octet-stream");
770
		ServletOutputStream sos;
771
		try {
772
			sos = response.getOutputStream();
773
			sos.write(buffer);
774
			sos.flush();
775
		} catch (IOException e) {
776
			System.out.println("Unable to stream the manifest file");
777
		}   
778
 
9242 kshitij.so 779
 
8739 vikram.rag 780
	}
781
 
9780 kshitij.so 782
	public void uploadsnapdealBulkSheet() throws IOException, TException, NumberFormatException, InventoryServiceException, CatalogServiceException{
8739 vikram.rag 783
		File fileToCreate = new File("/tmp/", "Snapdeal-bulk-upload.xls");
784
		FileUtils.copyFile(this.file, fileToCreate);
785
		FileInputStream iFile = new FileInputStream(new File(file.getAbsolutePath()));
786
		HSSFWorkbook workbook = new HSSFWorkbook(iFile);
787
		HSSFSheet sheet = workbook.getSheetAt(0);
9780 kshitij.so 788
		String updatedBy = getUserName();
8887 vikram.rag 789
		Client catalogClient=null;
9780 kshitij.so 790
		Client catalogClientProd=null;
8739 vikram.rag 791
		StringBuilder sb = new StringBuilder();
792
		try {
9242 kshitij.so 793
			catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
9780 kshitij.so 794
			catalogClientProd = new CatalogClient().getClient();
8739 vikram.rag 795
		} catch (TTransportException e) {
796
			e.printStackTrace();
797
		}
9478 kshitij.so 798
		InventoryClient inventoryServiceClient = new InventoryClient();
799
		in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
9780 kshitij.so 800
 
8739 vikram.rag 801
		for (int iterator=(sheet.getFirstRowNum()+1);iterator<=sheet.getLastRowNum();iterator++){
9780 kshitij.so 802
			if (sheet.getLastRowNum()-1>100){
803
				sb.append("Number of items to update can't be greater than 100");
804
				break;
805
			}
806
			SnapdealItem snapdealItem = null;
807
			MarketplaceItems marketplaceItem = null;
808
			MarketplacePercentage mpCosting = null;
9242 kshitij.so 809
			Item item = null;
8739 vikram.rag 810
			Long sku;
9595 kshitij.so 811
			boolean new_item = false;
8739 vikram.rag 812
			if (checkEmptyString(sheet.getRow(iterator).getCell(0))){
813
				continue;
814
			}
815
			else {
8791 kshitij.so 816
				sku=(long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
8887 vikram.rag 817
				snapdealItem = catalogClient.getSnapdealItem(sku);
9780 kshitij.so 818
				mpCosting = catalogClient.getCostingForMarketplace(7, sku);
819
				marketplaceItem  = catalogClient.getMarketplacedetailsForItem(sku, 7);
820
				marketplaceItem.setItemId(sku);
821
				marketplaceItem.setSource(7);
822
				marketplaceItem.setCommission(mpCosting.getCommission());
823
				marketplaceItem.setServiceTax(mpCosting.getServiceTax());
824
				marketplaceItem.setReturnProvision(mpCosting.getReturnProvision());
825
				marketplaceItem.setEmiFee(mpCosting.getEmiFee());
826
				marketplaceItem.setClosingFee(mpCosting.getClosingFee());
827
 
9242 kshitij.so 828
				try {
829
					item = catalogClient.getItem(sku);
830
					if (item.getId()==0){
9780 kshitij.so 831
						sb.append(sku + " Item not valid"+"\n");
9242 kshitij.so 832
						continue;
833
					}
834
				} catch (CatalogServiceException e) {
9780 kshitij.so 835
					sb.append(sku + " Item not valid"+"\n");
9242 kshitij.so 836
					continue;
837
				}
8886 vikram.rag 838
				if(snapdealItem.getItem_id()==0){
9595 kshitij.so 839
					new_item = true;
8886 vikram.rag 840
					snapdealItem = new SnapdealItem();
841
					snapdealItem.setItem_id(sku); 
842
				}
8739 vikram.rag 843
			}
9725 kshitij.so 844
 
9595 kshitij.so 845
			if (checkEmptyString(sheet.getRow(iterator).getCell(1)) && new_item){
846
				sb.append(sku + " New Listing - Warehouse Id not Present"+"\n");
847
				continue;
848
			}
9725 kshitij.so 849
 
8739 vikram.rag 850
			if (!checkEmptyString(sheet.getRow(iterator).getCell(1))){
9780 kshitij.so 851
				/*Warehouse warehouse = inventoryClient.getWarehouse(Long.valueOf(warehouseId));
9725 kshitij.so 852
				if (!(warehouse.getWarehouseType()==WarehouseType.OURS && warehouse.getInventoryType() == InventoryType.GOOD)){
853
					sb.append(sku + "Not acceptable warehouse"+"\n");
854
					continue;
9780 kshitij.so 855
				}*/
8739 vikram.rag 856
				long warehouseId = (long) sheet.getRow(iterator).getCell(1).getNumericCellValue();
9478 kshitij.so 857
				double maxNLC = inventoryClient.getNlcForWarehouse(warehouseId, item.getId());
9595 kshitij.so 858
				if (maxNLC==0){
859
					sb.append(sku + "Max Nlc can't be 0"+"\n");
860
					continue;
861
				}
8739 vikram.rag 862
				snapdealItem.setWarehouseId(warehouseId);
9478 kshitij.so 863
				snapdealItem.setMaxNlc(maxNLC);
8739 vikram.rag 864
			}
865
 
866
			if (!checkEmptyString(sheet.getRow(iterator).getCell(2))){
867
				double exceptionPrice = sheet.getRow(iterator).getCell(2).getNumericCellValue();
868
				snapdealItem.setExceptionPrice(exceptionPrice);
869
			}
870
 
871
			if (!checkEmptyString(sheet.getRow(iterator).getCell(3))){
872
				if ((long)sheet.getRow(iterator).getCell(3).getNumericCellValue()==1){
873
					snapdealItem.setIsListedOnSnapdeal(true);
874
				}
875
				if ((long)sheet.getRow(iterator).getCell(3).getNumericCellValue()==0){
876
					snapdealItem.setIsListedOnSnapdeal(false);
877
				}
878
			}
9780 kshitij.so 879
 
880
			double transferPrice = 0,sellingPrice,commission,courierCost =45,serviceTax,otherCost;
881
			if (checkEmptyString(sheet.getRow(iterator).getCell(4))&& new_item){
882
				sb.append(sku + "New Item -  Selling Price cannot be empty"+"\n");
9242 kshitij.so 883
				continue;
884
			}
885
			if (!checkEmptyString(sheet.getRow(iterator).getCell(4))){
9780 kshitij.so 886
				sellingPrice = sheet.getRow(iterator).getCell(4).getNumericCellValue();
887
				if(sellingPrice==0){
888
					sb.append(sku + " Selling Price cannot be zero"+"\n");
9242 kshitij.so 889
					continue;
890
				}
9780 kshitij.so 891
				snapdealItem.setSellingPrice(sellingPrice);
892
				marketplaceItem.setCurrentSp(sellingPrice);
9242 kshitij.so 893
			}
9780 kshitij.so 894
 
895
			if (!checkEmptyString(sheet.getRow(iterator).getCell(5))){
896
				otherCost = sheet.getRow(iterator).getCell(5).getNumericCellValue();
897
				marketplaceItem.setOtherCost(otherCost);
898
			}
9242 kshitij.so 899
 
9780 kshitij.so 900
 
901
			if (!checkEmptyString(sheet.getRow(iterator).getCell(6))){
902
				if ((long)sheet.getRow(iterator).getCell(6).getNumericCellValue()==1){
9242 kshitij.so 903
					snapdealItem.setSuppressPriceFeed(true);
904
				}
9780 kshitij.so 905
				if ((long)sheet.getRow(iterator).getCell(6).getNumericCellValue()==0){
9242 kshitij.so 906
					snapdealItem.setSuppressPriceFeed(false);
907
				}
908
			}
909
 
9780 kshitij.so 910
			if (!checkEmptyString(sheet.getRow(iterator).getCell(7))){
911
				if ((long)sheet.getRow(iterator).getCell(7).getNumericCellValue()==1){
9242 kshitij.so 912
					snapdealItem.setSuppressInventoryFeed(true);
913
				}
9780 kshitij.so 914
				if ((long)sheet.getRow(iterator).getCell(7).getNumericCellValue()==0){
9242 kshitij.so 915
					snapdealItem.setSuppressInventoryFeed(false);
916
				}
917
			}
9725 kshitij.so 918
 
9780 kshitij.so 919
			if (checkEmptyString(sheet.getRow(iterator).getCell(8)) && new_item){
920
				sb.append(sku + " New Item -  SKU@Snapdeal cannot be empty"+"\n");
921
				continue;
9478 kshitij.so 922
			}
9725 kshitij.so 923
 
9568 kshitij.so 924
			if (!checkEmptyString(sheet.getRow(iterator).getCell(8))){
9597 kshitij.so 925
				sheet.getRow(iterator).getCell(8).setCellType(Cell.CELL_TYPE_STRING);
9780 kshitij.so 926
				String snapdealItemCode = sheet.getRow(iterator).getCell(8).getStringCellValue();
927
				if (snapdealItemCode.length()==0 && new_item){
928
					sb.append(sku + " New Item -  SKU@Snapdeal cannot be empty"+"\n");
929
					continue;
930
				}
931
				snapdealItem.setSkuAtSnapdeal(snapdealItemCode);
932
			}
933
 
934
			if (checkEmptyString(sheet.getRow(iterator).getCell(9)) && new_item){
935
				sb.append(sku + " New Item -  SUPC code cannot be empty"+"\n");
936
				continue;
937
			}
938
 
939
			if (!checkEmptyString(sheet.getRow(iterator).getCell(9))){
940
				sheet.getRow(iterator).getCell(9).setCellType(Cell.CELL_TYPE_STRING);
941
				String supc = sheet.getRow(iterator).getCell(9).getStringCellValue();
942
				if (supc.length()==0 && new_item){
943
					sb.append(sku + "  New Item -  SUPC code cannot be empty"+"\n");
944
					continue;
945
				}
9568 kshitij.so 946
				snapdealItem.setSupc(supc);
947
			}
9242 kshitij.so 948
			double weight = item.getWeight();
949
 
9780 kshitij.so 950
 
951
			if(weight==0){
9242 kshitij.so 952
				sb.append(sku + " Please add weight"+"\n");
953
				continue;
9780 kshitij.so 954
			}
9242 kshitij.so 955
			if (weight!=0){
9780 kshitij.so 956
				weight = weight+.05;
9242 kshitij.so 957
				int slabs = (int) ((weight - .001)/(.5));
958
 
959
				for(int i=0;i<slabs;i++){
960
					courierCost = courierCost + 35;
961
				}
962
			}
963
 
964
			snapdealItem.setCourierCost(courierCost);
9780 kshitij.so 965
			marketplaceItem.setCourierCost(courierCost);
966
			transferPrice = roundTwoDecimals(snapdealItem.getSellingPrice()- snapdealItem.getSellingPrice()*(marketplaceItem.getCommission()/100+marketplaceItem.getEmiFee()/100)*(1+(marketplaceItem.getServiceTax()/100))-(snapdealItem.getCourierCost()+marketplaceItem.getClosingFee())*(1+(marketplaceItem.getServiceTax()/100)));
967
			snapdealItem.setTransferPrice(transferPrice);
968
			marketplaceItem.setCurrentTp(transferPrice);
969
			commission = roundTwoDecimals(((marketplaceItem.getCommission()/100)*snapdealItem.getSellingPrice()));
9242 kshitij.so 970
			snapdealItem.setCommission(commission);
9780 kshitij.so 971
			serviceTax = roundTwoDecimals((marketplaceItem.getServiceTax()/100)*(snapdealItem.getCommission()+snapdealItem.getCourierCost())); 
9242 kshitij.so 972
			snapdealItem.setServiceTax(serviceTax);
9780 kshitij.so 973
			double vatRate = catalogClientProd.getVatPercentageForItem(snapdealItem.getItem_id(),0,snapdealItem.getSellingPrice());
974
			marketplaceItem.setVat(vatRate);
975
			double vat = roundTwoDecimals((snapdealItem.getSellingPrice()/(1+(marketplaceItem.getVat()/100))-(snapdealItem.getMaxNlc()/(1+(marketplaceItem.getVat()/100))))*(marketplaceItem.getVat()/100));
976
			double inHouseCost = roundTwoDecimals(15+vat+(marketplaceItem.getReturnProvision()/100)*snapdealItem.getSellingPrice()+marketplaceItem.getOtherCost());
977
			double lowest_possible_tp = roundTwoDecimals(snapdealItem.getMaxNlc()+inHouseCost);
978
			marketplaceItem.setMinimumPossibleTp(lowest_possible_tp);
979
			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)));
980
			marketplaceItem.setMinimumPossibleSp(lowest_possible_sp);
981
			snapdealItem.setUpdatedBy(updatedBy);
982
			snapdealItem.setMarketplaceItems(marketplaceItem);
8888 vikram.rag 983
			if(!catalogClient.addOrUpdateSnapdealItem(snapdealItem)){
9780 kshitij.so 984
				sb.append(sku + " Service Error\n");
8739 vikram.rag 985
			}	
986
		}
9780 kshitij.so 987
		File file = new File("/tmp/snapdealbulk");
8739 vikram.rag 988
		FileWriter writer = new FileWriter(file);
989
		writer.append(sb.toString());
990
		writer.close();
991
		byte[] buffer = new byte[(int)file.length()];
992
		InputStream input = null;
993
		try {
994
			int totalBytesRead = 0;
995
			input = new BufferedInputStream(new FileInputStream(file));
996
			while(totalBytesRead < buffer.length){
997
				int bytesRemaining = buffer.length - totalBytesRead;
998
				int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
999
				if (bytesRead > 0){
1000
					totalBytesRead = totalBytesRead + bytesRead;
1001
				}
1002
			}
1003
		}
1004
		finally {
1005
			input.close();
1006
			file.delete();
1007
		}
1008
 
1009
		response.setHeader("Content-Type", "text/javascript");
1010
 
1011
		ServletOutputStream sos;
1012
		try {
1013
			sos = response.getOutputStream();
1014
			sos.write(buffer);
1015
			sos.flush();
1016
		} catch (IOException e) {
1017
			System.out.println("Unable to stream the manifest file");
1018
		}   
1019
	}
1020
 
1021
	public boolean checkEmptyString(Cell cell){
1022
		if (cell==null){
1023
			return true;
1024
		}
1025
		return false;
1026
	}
1027
 
9242 kshitij.so 1028
	double roundTwoDecimals(double d) {
1029
		DecimalFormat twoPlaces = new DecimalFormat("#.##");
1030
		return Double.valueOf(twoPlaces.format(d));
1031
	}
1032
 
1033
 
8739 vikram.rag 1034
	public String show() {
1035
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1])) {
1036
			return "authfail";
1037
		}
1038
 
1039
		if (StringUtils.equals(id, "snapdeal-options")){
1040
			return "snapdeal-options";
1041
		}
9242 kshitij.so 1042
		if (StringUtils.equals(id, "item-table")){
1043
			return "snapdeal-item-table";
1044
		}
8739 vikram.rag 1045
 
1046
		return "id";
1047
	}
1048
 
1049
	public void setId(String id) {
1050
		this.id = id;
1051
	}
1052
 
9780 kshitij.so 1053
	public String getUserName(){
1054
		return session.getAttribute(ReportsUtils.USER_NAME).toString();
1055
	}
1056
 
8739 vikram.rag 1057
	public HttpServletRequest getRequest() {
1058
		logger.info("set request"+request.toString());
1059
		return request;
1060
	}
1061
 
1062
	public void setRequest(HttpServletRequest request) {
1063
		this.request = request;
1064
	}
1065
 
1066
	public HttpServletResponse getResponse() {
1067
		return response;
1068
	}
1069
 
1070
	public void setResponse(HttpServletResponse response) {
1071
		this.response = response;
1072
	}
1073
 
1074
	public HttpSession getSession() {
1075
		return session;
1076
	}
1077
 
1078
	public void setSession(HttpSession session) {
1079
		this.session = session;
1080
	}
1081
 
1082
	public ServletContext getContext() {
1083
		return context;
1084
	}
1085
 
1086
	public void setContext(ServletContext context) {
1087
		this.context = context;
1088
	}
1089
 
1090
	public String getUrl() {
1091
		return url;
1092
	}
1093
 
1094
	public void setUrl(String url) {
1095
		this.url = url;
1096
	}
1097
 
1098
	public String getItemId() {
1099
		return itemId;
1100
	}
1101
 
1102
	public void setItemId(String itemId) {
1103
		this.itemId = itemId;
1104
	}
1105
 
1106
	public String getIsSnapdealListed() {
1107
		return isSnapdealListed;
1108
	}
1109
 
1110
	public void setIsSnapdealListed(String isSnapdealListed) {
1111
		this.isSnapdealListed = isSnapdealListed;
1112
	}
1113
 
1114
	public String getExceptionPrice() {
1115
		return exceptionPrice;
1116
	}
1117
 
1118
	public void setExceptionPrice(String exceptionPrice) {
1119
		this.exceptionPrice = exceptionPrice;
1120
	}
1121
 
1122
	public String getWarehouseId() {
1123
		return warehouseId;
1124
	}
1125
 
1126
	public void setWarehouseId(String warehouseId) {
1127
		this.warehouseId = warehouseId;
1128
	}
1129
 
1130
	public File getFile() {
1131
		return file;
1132
	}
1133
 
1134
	public void setFile(File file) {
1135
		this.file = file;
1136
	}
1137
 
1138
	public String getErrMsg() {
1139
		return errMsg;
1140
	}
1141
 
1142
	public void setErrMsg(String errMsg) {
1143
		this.errMsg = errMsg;
1144
	}
1145
 
1146
	public String getNext() {
1147
		return next;
1148
	}
1149
 
1150
	public void setNext(String next) {
1151
		this.next = next;
1152
	}
1153
 
1154
	public String getId() {
1155
		return id;
1156
	}
1157
 
1158
	public void setServletRequest(HttpServletRequest req) {
1159
		this.request = req;
1160
		this.session = req.getSession();        
1161
	}
1162
 
1163
	public void setServletContext(ServletContext arg0) {
1164
		// TODO Auto-generated method stub
1165
 
1166
	}
1167
 
1168
	public void setServletResponse(HttpServletResponse response) {
1169
		this.response = response;
1170
	}
1171
 
9242 kshitij.so 1172
	public void setSearchText(String searchText) {
1173
		this.searchText = searchText;
1174
	}
1175
 
1176
	public String getSearchText() {
1177
		return searchText;
1178
	}
1179
 
1180
	public long getSearchCount() {
1181
		return searchCount;
1182
	}
1183
 
1184
	public long getTotalCount() {
1185
		return totalCount;
1186
	}
1187
 
1188
	public void setSearchCount(long count) {
1189
		this.searchCount = count;
1190
	}
1191
 
1192
	public List<SnapdealItemDetails> getSnapdealItems(){
1193
		return snapdealItems;
1194
	}
1195
 
1196
	public void setSellingPrice(String sellingPrice) {
1197
		this.sellingPrice = sellingPrice;
1198
	}
1199
 
1200
	public String getSellingPrice() {
1201
		return sellingPrice;
1202
	}
1203
 
1204
	public void setTransferPrice(String transferPrice) {
1205
		this.transferPrice = transferPrice;
1206
	}
1207
 
1208
	public String getTransferPrice() {
1209
		return transferPrice;
1210
	}
1211
 
1212
	public void setWebisteMrp(String webisteMrp) {
1213
		this.webisteMrp = webisteMrp;
1214
	}
1215
 
1216
	public String getWebisteMrp() {
1217
		return webisteMrp;
1218
	}
1219
 
1220
	public void setWebisteSellingPrice(String webisteSellingPrice) {
1221
		this.webisteSellingPrice = webisteSellingPrice;
1222
	}
1223
 
1224
	public String getWebisteSellingPrice() {
1225
		return webisteSellingPrice;
1226
	}
1227
 
1228
	public void setIsListedOnSnapdeal(String isListedOnSnapdeal) {
1229
		this.isListedOnSnapdeal = isListedOnSnapdeal;
1230
	}
1231
 
1232
	public String getIsSuppressInventoryFeed() {
1233
		return isSuppressInventoryFeed;
1234
	}
1235
 
1236
	public void setIsSuppressInventoryFeed(String isSuppressInventoryFeed) {
1237
		this.isSuppressInventoryFeed = isSuppressInventoryFeed;
1238
	}
1239
 
1240
	public String getIsSuppressPriceFeed() {
1241
		return isSuppressPriceFeed;
1242
	}
1243
 
1244
	public void setIsSuppressPriceFeed(String isSuppressPriceFeed) {
1245
		this.isSuppressPriceFeed = isSuppressPriceFeed;
1246
	}
1247
 
1248
	public String getIsListedOnSnapdeal() {
1249
		return isListedOnSnapdeal;
1250
	}
1251
 
1252
	public void setCommission(String commission) {
1253
		this.commission = commission;
1254
	}
1255
 
1256
	public String getCommission() {
1257
		return commission;
1258
	}
1259
 
1260
	public void setServiceTax(String serviceTax) {
1261
		this.serviceTax = serviceTax;
1262
	}
1263
 
1264
	public String getServiceTax() {
1265
		return serviceTax;
1266
	}
1267
 
1268
	public void setCourierCost(String courierCost) {
1269
		this.courierCost = courierCost;
1270
	}
1271
 
1272
	public String getCourierCost() {
1273
		return courierCost;
1274
	}
1275
 
9478 kshitij.so 1276
	public void setMaxNlc(String maxNlc) {
1277
		this.maxNlc = maxNlc;
1278
	}
1279
 
1280
	public String getMaxNlc() {
1281
		return maxNlc;
1282
	}
1283
 
9242 kshitij.so 1284
	public String getItemDetails(){
1285
		return itemObj.toString();
1286
	}
1287
 
9725 kshitij.so 1288
	public double getRoundOfValue(String val){
1289
		return roundTwoDecimals(Double.valueOf(val));
1290
	}
9812 vikram.rag 1291
 
1292
	public String getVaildName(String name){
1293
		return name!=null?name:"";
1294
	}
9242 kshitij.so 1295
 
1296
 
9725 kshitij.so 1297
 
9242 kshitij.so 1298
}