Subversion Repositories SmartDukaan

Rev

Rev 8903 | Rev 9113 | 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.Amazonlisted;
4
import in.shop2020.model.v1.catalog.CatalogService.Client;
9110 vikram.rag 5
import in.shop2020.model.v1.catalog.CatalogServiceException;
6
import in.shop2020.model.v1.catalog.Item;
8739 vikram.rag 7
import in.shop2020.model.v1.catalog.SnapdealItem;
9110 vikram.rag 8
import in.shop2020.model.v1.catalog.SnapdealItemDetails;
8739 vikram.rag 9
import in.shop2020.model.v1.order.AmazonFbaSalesSnapshot;
8886 vikram.rag 10
import in.shop2020.model.v1.order.SnapdealOrder;
8739 vikram.rag 11
import in.shop2020.support.utils.ReportsUtils;
12
import in.shop2020.thrift.clients.CatalogClient;
13
import in.shop2020.thrift.clients.TransactionClient;
14
 
15
import java.io.BufferedInputStream;
16
import java.io.File;
17
import java.io.FileInputStream;
18
import java.io.FileNotFoundException;
19
import java.io.FileOutputStream;
20
import java.io.FileWriter;
21
import java.io.IOException;
22
import java.io.InputStream;
23
import java.util.ArrayList;
9110 vikram.rag 24
import java.util.Arrays;
8739 vikram.rag 25
import java.util.HashMap;
26
import java.util.List;
27
import java.util.Map;
28
 
29
import javax.servlet.ServletContext;
30
import javax.servlet.ServletOutputStream;
31
import javax.servlet.http.HttpServletRequest;
32
import javax.servlet.http.HttpServletResponse;
33
import javax.servlet.http.HttpSession;
34
 
35
import org.apache.commons.io.FileUtils;
36
import org.apache.commons.lang.xwork.StringUtils;
37
import org.apache.poi.hssf.usermodel.HSSFRow;
38
import org.apache.poi.hssf.usermodel.HSSFSheet;
39
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
40
import org.apache.poi.ss.usermodel.Cell;
41
import org.apache.struts2.convention.annotation.InterceptorRef;
42
import org.apache.struts2.convention.annotation.InterceptorRefs;
43
import org.apache.struts2.interceptor.ServletRequestAware;
44
import org.apache.struts2.interceptor.ServletResponseAware;
45
import org.apache.struts2.util.ServletContextAware;
46
import org.apache.thrift.TException;
47
import org.apache.thrift.transport.TTransportException;
48
import org.slf4j.Logger;
49
import org.slf4j.LoggerFactory;
50
 
51
import com.opensymphony.xwork2.ValidationAwareSupport;
52
 
53
public class SnapdealListController extends ValidationAwareSupport implements ServletRequestAware ,ServletResponseAware, ServletContextAware{
54
	private static Logger logger = LoggerFactory.getLogger(SnapdealListController.class);
55
 
56
 
57
	private HttpServletRequest request;
58
	private HttpServletResponse response;
59
	private HttpSession session;
60
	private ServletContext context;
61
	private String url;
62
	private String itemId;
63
	private String isSnapdealListed;
64
	private String exceptionPrice;
65
	private String warehouseId;
66
	private File file;
67
	private String errMsg;
68
	private String next;
69
	private String id;
9110 vikram.rag 70
	private String searchText;
71
	private List<SnapdealItemDetails> snapdealItems;
72
	private long searchCount;
73
	private long totalCount;
8739 vikram.rag 74
 
75
	public String index() {
76
		if (!ReportsUtils.canAccessReport((Long) session.getAttribute(ReportsUtils.ROLE),request.getServletPath())) {
77
			return "authfail";
78
		}
79
		return "index";
80
	}
9110 vikram.rag 81
 
82
	public String fetchItems() throws TException {
83
		Client CatalogClient = new CatalogClient().getClient();
84
		if (searchText.length() == 0) {
85
			snapdealItems = CatalogClient.getSnapdealItems(Long.valueOf(next), 10);
86
			totalCount = CatalogClient.getCountForSnapdealItems();
87
			setSearchCount(totalCount);
88
		} else {
89
			List<String> subString = Arrays.asList(searchText.split(" "));
90
			snapdealItems = CatalogClient.searchSnapdealItems(subString,Long.valueOf(next), 10);
91
			totalCount = CatalogClient.getCountForSnapdealItems();
92
			searchCount = CatalogClient.getSnapdealSearchResultCount(subString);
93
		}
94
		return "snapdeal-item-table";
95
	}
8739 vikram.rag 96
 
97
	public String uploadBulkSheet(){
98
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {
99
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);
100
			return "authfail";
101
		}
102
		return "snapdeal-bulk-upload";
103
	}
9110 vikram.rag 104
 
8739 vikram.rag 105
	public void downloadSnapdealListings() throws IOException, TException{
106
		File file = new File("/tmp/snapdeal-bulk-upload-template.xls");
107
		HSSFWorkbook hwb=new HSSFWorkbook();
108
		HSSFSheet sheet =  hwb.createSheet("Snapdeal-Listings");
109
		HSSFRow rowhead=   sheet.createRow((short)0);
110
		rowhead.createCell((short) 0).setCellValue("ITEM-ID");
111
		rowhead.createCell((short) 1).setCellValue("WAREHOUSE-ID");
112
		rowhead.createCell((short) 2).setCellValue("EXCEPTIONAL-PRICE");
113
		rowhead.createCell((short) 3).setCellValue("SNAPDEAL-LISTED");
9110 vikram.rag 114
		rowhead.createCell((short) 4).setCellValue("TRANSFER-PRICE");
115
		rowhead.createCell((short) 5).setCellValue("SELLING-PRICE");
116
		rowhead.createCell((short) 6).setCellValue("COURIER-COST");
117
		rowhead.createCell((short) 7).setCellValue("COMMISION");
118
		rowhead.createCell((short) 8).setCellValue("SERVICE-TAX");
8739 vikram.rag 119
		CatalogClient catalogServiceClient = null;
9110 vikram.rag 120
		List<SnapdealItemDetails> snapdealItems = null;
8739 vikram.rag 121
		try {
8903 vikram.rag 122
			catalogServiceClient = new CatalogClient();
8739 vikram.rag 123
			in.shop2020.model.v1.catalog.CatalogService.Client catalogClient= catalogServiceClient.getClient();
124
			snapdealItems = catalogClient.getAllSnapdealItems();
125
		} catch (Exception e) {
126
			e.printStackTrace();
127
		}
128
		int iterator=1;
9110 vikram.rag 129
		for(SnapdealItemDetails snapdealItem:snapdealItems){
8739 vikram.rag 130
			HSSFRow row = sheet.createRow((short)iterator);
131
			row.createCell((short) 0).setCellValue(snapdealItem.getItem_id());
132
			row.createCell((short) 1).setCellValue(snapdealItem.getWarehouseId());
133
			row.createCell((short) 2).setCellValue(snapdealItem.getExceptionPrice());
134
			if(snapdealItem.isIsListedOnSnapdeal()){
135
				row.createCell((short) 3).setCellValue(1);
136
			}
137
			else{
138
				row.createCell((short) 3).setCellValue(0);
139
			}
9110 vikram.rag 140
			row.createCell((short) 4).setCellValue(snapdealItem.getTransferPrice());
141
			row.createCell((short) 5).setCellValue(snapdealItem.getSellingPrice());
142
			rowhead.createCell((short) 6).setCellValue(snapdealItem.getCourierCost());
143
			rowhead.createCell((short) 7).setCellValue(snapdealItem.getCommission());
144
			rowhead.createCell((short) 8).setCellValue(snapdealItem.getServiceTax());
8739 vikram.rag 145
			iterator++;
146
		}
9110 vikram.rag 147
 
8739 vikram.rag 148
		FileOutputStream fileOut = null;
149
		try {
150
			fileOut = new FileOutputStream(file);
151
		} catch (FileNotFoundException e) {
152
			// TODO Auto-generated catch block
153
			e.printStackTrace();
154
		}
155
		try {
156
			hwb.write(fileOut);
157
		} catch (IOException e) {
158
			// TODO Auto-generated catch block
159
			e.printStackTrace();
160
		}
161
		try {
162
			fileOut.close();
163
		} catch (IOException e) {
164
			// TODO Auto-generated catch block
165
			e.printStackTrace();
166
		}
167
		byte[] buffer = new byte[(int)file.length()];
168
		InputStream input = null;
169
		try {
170
			int totalBytesRead = 0;
171
			input = new BufferedInputStream(new FileInputStream(file));
172
			while(totalBytesRead < buffer.length){
173
				int bytesRemaining = buffer.length - totalBytesRead;
174
				int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
175
				if (bytesRead > 0){
176
					totalBytesRead = totalBytesRead + bytesRead;
177
				}
178
			}
179
		}
180
		finally {
181
			input.close();
182
			file.delete();
183
		}
184
 
185
		response.setHeader("Content-Disposition", "attachment; filename=\"Snapdeal-Bulk-Listings.xls\"");
186
		response.setContentType("application/octet-stream");
187
		ServletOutputStream sos;
188
		try {
189
			sos = response.getOutputStream();
190
			sos.write(buffer);
191
			sos.flush();
192
		} catch (IOException e) {
193
			System.out.println("Unable to stream the manifest file");
194
		}   
195
 
9110 vikram.rag 196
 
8739 vikram.rag 197
	}
198
 
199
	public void uploadsnapdealBulkSheet() throws IOException, TException{
200
		File fileToCreate = new File("/tmp/", "Snapdeal-bulk-upload.xls");
201
		FileUtils.copyFile(this.file, fileToCreate);
202
		FileInputStream iFile = new FileInputStream(new File(file.getAbsolutePath()));
203
		HSSFWorkbook workbook = new HSSFWorkbook(iFile);
204
		HSSFSheet sheet = workbook.getSheetAt(0);
8887 vikram.rag 205
		Client catalogClient=null;
8739 vikram.rag 206
		StringBuilder sb = new StringBuilder();
207
		try {
8903 vikram.rag 208
			catalogClient = new CatalogClient().getClient();
8739 vikram.rag 209
		} catch (TTransportException e) {
210
			e.printStackTrace();
211
		}
212
		for (int iterator=(sheet.getFirstRowNum()+1);iterator<=sheet.getLastRowNum();iterator++){
213
			SnapdealItem snapdealItem =null;
9110 vikram.rag 214
			Item item = null;
8739 vikram.rag 215
			Long sku;
216
			if (checkEmptyString(sheet.getRow(iterator).getCell(0))){
217
				continue;
218
			}
219
			else {
8791 kshitij.so 220
				sku=(long) sheet.getRow(iterator).getCell(0).getNumericCellValue();
8887 vikram.rag 221
				snapdealItem = catalogClient.getSnapdealItem(sku);
9110 vikram.rag 222
				try {
223
					item = catalogClient.getItem(sku);
224
				} catch (CatalogServiceException e) {
225
					sb.append(sku + " Item not Present"+"\n");
226
				}
8886 vikram.rag 227
				if(snapdealItem.getItem_id()==0){
228
					snapdealItem = new SnapdealItem();
229
					snapdealItem.setItem_id(sku); 
230
				}
8739 vikram.rag 231
			}
232
			if (!checkEmptyString(sheet.getRow(iterator).getCell(1))){
233
				long warehouseId = (long) sheet.getRow(iterator).getCell(1).getNumericCellValue();
234
				snapdealItem.setWarehouseId(warehouseId);
235
			}
236
 
237
			if (!checkEmptyString(sheet.getRow(iterator).getCell(2))){
238
				double exceptionPrice = sheet.getRow(iterator).getCell(2).getNumericCellValue();
239
				snapdealItem.setExceptionPrice(exceptionPrice);
240
			}
241
 
242
			if (!checkEmptyString(sheet.getRow(iterator).getCell(3))){
243
				if ((long)sheet.getRow(iterator).getCell(3).getNumericCellValue()==1){
244
					snapdealItem.setIsListedOnSnapdeal(true);
245
				}
246
				if ((long)sheet.getRow(iterator).getCell(3).getNumericCellValue()==0){
247
					snapdealItem.setIsListedOnSnapdeal(false);
248
				}
249
			}
9110 vikram.rag 250
			double transferPrice = 0,sellingPrice,commission,courierCost =45,serviceTax;
251
			if (!checkEmptyString(sheet.getRow(iterator).getCell(4))){
252
				transferPrice = sheet.getRow(iterator).getCell(4).getNumericCellValue();
253
				if(transferPrice==0){
254
					sb.append(sku + " Transfer Price cannot be zero"+"\n");
255
					continue;
256
				}
257
				snapdealItem.setTransferPrice(transferPrice);
258
			}
259
			double weight = item.getWeight();
260
			if(weight==0){
261
				sb.append(sku + " Please add weight"+"\n");
262
				continue;
263
			}
264
			int slabs = (int) (weight/.5);
265
 
266
			for(int i=0;i<slabs;i++){
267
				courierCost = courierCost + 35;
268
			}
269
			snapdealItem.setCourierCost(courierCost);
270
			sellingPrice = ((transferPrice + courierCost*1.1236)/(100 - 3.59*1.1236))*100;
271
			snapdealItem.setSellingPrice(sellingPrice);
272
			double commision_value = .0359*sellingPrice + courierCost;
273
			commission = ((.0359*sellingPrice + courierCost)/sellingPrice)*100;
274
			snapdealItem.setCommission(commission);
275
			serviceTax = (commision_value * .1236); 
276
			snapdealItem.setServiceTax(serviceTax);
8888 vikram.rag 277
			if(!catalogClient.addOrUpdateSnapdealItem(snapdealItem)){
8739 vikram.rag 278
				sb.append(sku + "\n");
279
			}	
280
		}
281
		//logger.info("Amazon Bulk Map "+amazonBulkUpdate.toString());
282
		File file = new File("/tmp/amazonbulk");
283
		FileWriter writer = new FileWriter(file);
284
		writer.append(sb.toString());
285
		writer.close();
286
		byte[] buffer = new byte[(int)file.length()];
287
		InputStream input = null;
288
		try {
289
			int totalBytesRead = 0;
290
			input = new BufferedInputStream(new FileInputStream(file));
291
			while(totalBytesRead < buffer.length){
292
				int bytesRemaining = buffer.length - totalBytesRead;
293
				int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
294
				if (bytesRead > 0){
295
					totalBytesRead = totalBytesRead + bytesRead;
296
				}
297
			}
298
		}
299
		finally {
300
			input.close();
301
			file.delete();
302
		}
303
 
304
		response.setHeader("Content-Type", "text/javascript");
305
 
306
		ServletOutputStream sos;
307
		try {
308
			sos = response.getOutputStream();
309
			sos.write(buffer);
310
			sos.flush();
311
		} catch (IOException e) {
312
			System.out.println("Unable to stream the manifest file");
313
		}   
314
	}
315
 
316
	public boolean checkEmptyString(Cell cell){
317
		if (cell==null){
318
			return true;
319
		}
320
		return false;
321
	}
322
 
323
	public String show() {
324
		logger.info("Before fetching role");
325
		logger.info(request.getSession().toString());
326
		logger.info(ReportsUtils.ROLE);
327
		logger.info(session.getAttribute(ReportsUtils.ROLE).toString());
328
		logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1]);
329
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1])) {
330
			logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1]);
331
			return "authfail";
332
		}
333
 
334
		if (StringUtils.equals(id, "snapdeal-options")){
335
			return "snapdeal-options";
336
		}
9110 vikram.rag 337
		if (StringUtils.equals(id, "item-table")){
338
			return "snapdeal-item-table";
339
		}
8739 vikram.rag 340
 
341
		return "id";
342
	}
343
 
344
	public void setId(String id) {
345
		logger.info(id);
346
		this.id = id;
347
	}
348
 
349
	public HttpServletRequest getRequest() {
350
		logger.info("set request"+request.toString());
351
		return request;
352
	}
353
 
354
	public void setRequest(HttpServletRequest request) {
355
		this.request = request;
356
	}
357
 
358
	public HttpServletResponse getResponse() {
359
		return response;
360
	}
361
 
362
	public void setResponse(HttpServletResponse response) {
363
		this.response = response;
364
	}
365
 
366
	public HttpSession getSession() {
367
		return session;
368
	}
369
 
370
	public void setSession(HttpSession session) {
371
		logger.info("set session"+session.toString());
372
		this.session = session;
373
	}
374
 
375
	public ServletContext getContext() {
376
		return context;
377
	}
378
 
379
	public void setContext(ServletContext context) {
380
		this.context = context;
381
	}
382
 
383
	public String getUrl() {
384
		return url;
385
	}
386
 
387
	public void setUrl(String url) {
388
		this.url = url;
389
	}
390
 
391
	public String getItemId() {
392
		return itemId;
393
	}
394
 
395
	public void setItemId(String itemId) {
396
		this.itemId = itemId;
397
	}
398
 
399
	public String getIsSnapdealListed() {
400
		return isSnapdealListed;
401
	}
402
 
403
	public void setIsSnapdealListed(String isSnapdealListed) {
404
		this.isSnapdealListed = isSnapdealListed;
405
	}
406
 
407
	public String getExceptionPrice() {
408
		return exceptionPrice;
409
	}
410
 
411
	public void setExceptionPrice(String exceptionPrice) {
412
		this.exceptionPrice = exceptionPrice;
413
	}
414
 
415
	public String getWarehouseId() {
416
		return warehouseId;
417
	}
418
 
419
	public void setWarehouseId(String warehouseId) {
420
		this.warehouseId = warehouseId;
421
	}
422
 
423
	public File getFile() {
424
		return file;
425
	}
426
 
427
	public void setFile(File file) {
428
		this.file = file;
429
	}
430
 
431
	public String getErrMsg() {
432
		return errMsg;
433
	}
434
 
435
	public void setErrMsg(String errMsg) {
436
		this.errMsg = errMsg;
437
	}
438
 
439
	public String getNext() {
440
		return next;
441
	}
442
 
443
	public void setNext(String next) {
444
		this.next = next;
445
	}
446
 
447
	public String getId() {
448
		return id;
449
	}
450
 
451
	public void setServletRequest(HttpServletRequest req) {
452
		this.request = req;
453
		this.session = req.getSession();        
454
	}
455
 
456
	public void setServletContext(ServletContext arg0) {
457
		// TODO Auto-generated method stub
458
 
459
	}
460
 
461
	public void setServletResponse(HttpServletResponse response) {
462
		this.response = response;
463
	}
9110 vikram.rag 464
 
465
	public void setSearchText(String searchText) {
466
		this.searchText = searchText;
467
	}
8739 vikram.rag 468
 
9110 vikram.rag 469
	public String getSearchText() {
470
		return searchText;
471
	}
472
 
473
	public long getSearchCount() {
474
		return searchCount;
475
	}
476
 
477
	public long getTotalCount() {
478
		return totalCount;
479
	}
480
 
481
	public void setSearchCount(long count) {
482
		this.searchCount = count;
483
	}
484
 
485
 
486
 
8739 vikram.rag 487
}