Rev 9113 | Rev 9568 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.support.controllers;import in.shop2020.model.v1.catalog.CatalogService.Client;import in.shop2020.model.v1.catalog.CatalogServiceException;import in.shop2020.model.v1.catalog.Item;import in.shop2020.model.v1.catalog.SnapdealItem;import in.shop2020.model.v1.catalog.SnapdealItemDetails;import in.shop2020.model.v1.order.AmazonFbaSalesSnapshot;import in.shop2020.model.v1.order.SnapdealOrder;import in.shop2020.support.utils.ReportsUtils;import in.shop2020.thrift.clients.CatalogClient;import in.shop2020.thrift.clients.TransactionClient;import java.io.BufferedInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.text.DecimalFormat;import java.util.ArrayList;import java.util.Arrays;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.servlet.ServletContext;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.apache.commons.io.FileUtils;import org.apache.commons.lang.xwork.StringUtils;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.struts2.convention.annotation.InterceptorRef;import org.apache.struts2.convention.annotation.InterceptorRefs;import org.apache.struts2.interceptor.ServletRequestAware;import org.apache.struts2.interceptor.ServletResponseAware;import org.apache.struts2.util.ServletContextAware;import org.apache.thrift.TException;import org.apache.thrift.transport.TTransportException;import org.json.JSONException;import org.json.JSONObject;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.opensymphony.xwork2.ValidationAwareSupport;@SuppressWarnings("unused")@InterceptorRefs({@InterceptorRef("defaultStack"),@InterceptorRef("login")})public class SnapdealListController extends ValidationAwareSupport implements ServletRequestAware ,ServletResponseAware, ServletContextAware{/****/private static final long serialVersionUID = 1L;private static Logger logger = LoggerFactory.getLogger(SnapdealListController.class);private HttpServletRequest request;private HttpServletResponse response;private HttpSession session;private ServletContext context;private String url;private String itemId;private String isSnapdealListed;private String exceptionPrice;private String warehouseId;private File file;private String errMsg;private String next;private String id;private String searchText;private List<SnapdealItemDetails> snapdealItems;private long searchCount;private long totalCount;private String sellingPrice;private String transferPrice;private String webisteMrp;private String webisteSellingPrice;private String isListedOnSnapdeal;private String commission;private String serviceTax;private String courierCost;private JSONObject itemObj;private String isSuppressInventoryFeed;private String isSuppressPriceFeed;public String index() {if (!ReportsUtils.canAccessReport((Long) session.getAttribute(ReportsUtils.ROLE),request.getServletPath())) {return "authfail";}return "index";}public String edit() {if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1])) {return "authfail";}return "edit";}public String fetchItems() throws TException {Client CatalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();if (searchText.length() == 0) {snapdealItems = CatalogClient.getSnapdealItems(Long.valueOf(next), 10);totalCount = CatalogClient.getCountForSnapdealItems();setSearchCount(totalCount);} else {List<String> subString = Arrays.asList(searchText.split(" "));snapdealItems = CatalogClient.searchSnapdealItems(subString,Long.valueOf(next), 10);totalCount = CatalogClient.getCountForSnapdealItems();searchCount = CatalogClient.getSnapdealSearchResultCount(subString);}return "snapdeal-item-table";}public SnapdealItemDetails fetchItemDetail() throws NumberFormatException, TException {Client CatalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();return CatalogClient.getSnapdealItemDetails(Long.valueOf(id));}public String uploadBulkSheet(){if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0])) {logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1].split("!")[0]);return "authfail";}return "snapdeal-bulk-upload";}public String update() throws NumberFormatException, TException{Client catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();SnapdealItem snapdealItem = catalogClient.getSnapdealItem(Long.valueOf(itemId));boolean commit = false;if (snapdealItem.getWarehouseId()!=Long.valueOf(warehouseId)){snapdealItem.setWarehouseId(Long.valueOf(warehouseId));commit = true;}if (snapdealItem.isIsListedOnSnapdeal()!=Boolean.valueOf(isListedOnSnapdeal)){snapdealItem.setIsListedOnSnapdeal(Boolean.valueOf(isListedOnSnapdeal));commit = true;}if (snapdealItem.isSuppressInventoryFeed()!=Boolean.valueOf(isSuppressInventoryFeed)){snapdealItem.setSuppressInventoryFeed(Boolean.valueOf(isSuppressInventoryFeed));commit = true;}if (snapdealItem.isSuppressPriceFeed()!=Boolean.valueOf(isSuppressPriceFeed)){snapdealItem.setSuppressPriceFeed(Boolean.valueOf(isSuppressPriceFeed));commit = true;}if (snapdealItem.getTransferPrice()!=Double.valueOf(transferPrice)){snapdealItem.setTransferPrice(Double.valueOf(transferPrice));commit = true;}if (snapdealItem.getExceptionPrice()!=Double.valueOf(exceptionPrice)){snapdealItem.setExceptionPrice(Double.valueOf(exceptionPrice));commit = true;}if (snapdealItem.getSellingPrice()!=Double.valueOf(sellingPrice)){snapdealItem.setSellingPrice(Double.valueOf(sellingPrice));commit = true;}if (snapdealItem.getCourierCost()!=Double.valueOf(courierCost)){snapdealItem.setCourierCost(Double.valueOf(courierCost));commit = true;}if (snapdealItem.getCommission()!=Double.valueOf(commission)){snapdealItem.setCommission(Double.valueOf(commission));commit = true;}if (snapdealItem.getServiceTax()!=Double.valueOf(serviceTax)){snapdealItem.setServiceTax(Double.valueOf(serviceTax));commit = true;}if (commit){catalogClient.addOrUpdateSnapdealItem(snapdealItem);}return "index";}public String addNewItem() throws TException{SnapdealItem snapdealItem = new SnapdealItem();snapdealItem.setItem_id(Long.valueOf(itemId));snapdealItem.setWarehouseId(Long.valueOf(warehouseId));snapdealItem.setIsListedOnSnapdeal(Boolean.valueOf(isListedOnSnapdeal));snapdealItem.setSuppressInventoryFeed(Boolean.valueOf(isSuppressInventoryFeed));snapdealItem.setSuppressPriceFeed(Boolean.valueOf(isSuppressPriceFeed));snapdealItem.setTransferPrice(Double.valueOf(transferPrice));snapdealItem.setExceptionPrice(Double.valueOf(exceptionPrice));snapdealItem.setSellingPrice(Double.valueOf(sellingPrice));snapdealItem.setCourierCost(Double.valueOf(courierCost));snapdealItem.setCommission(Double.valueOf(commission));snapdealItem.setServiceTax(Double.valueOf(serviceTax));Client catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();catalogClient.addOrUpdateSnapdealItem(snapdealItem);return "index";}public String getAddNewItemForm(){return "snapdeal-add-item";}public String getItemDetailsInJson() throws NumberFormatException, CatalogServiceException, TException, JSONException{Client catalogClient =new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();SnapdealItem snapdealItem = catalogClient.getSnapdealItem(Long.valueOf(itemId));if (snapdealItem.getItem_id()!=0){throw new CatalogServiceException();}Item item = catalogClient.getItem(Long.valueOf(itemId));if (item.getId()==0){throw new CatalogServiceException();}itemObj = new JSONObject();itemObj.put("ItemId", item.getId());itemObj.put("Brand", item.getBrand());itemObj.put("ModelName", item.getModelName());itemObj.put("ModelNumber", item.getModelNumber());itemObj.put("Color", item.getColor());itemObj.put("Weight", item.getWeight());itemObj.put("Risky", item.isRisky());itemObj.put("Status", item.getItemStatus());itemObj.put("MRP", item.getMrp());itemObj.put("SellingPrice", item.getSellingPrice());return "item-details-json";}public void downloadSnapdealListings() throws IOException, TException{File file = new File("/tmp/snapdeal-bulk-upload-template.xls");HSSFWorkbook hwb=new HSSFWorkbook();HSSFSheet sheet = hwb.createSheet("Snapdeal-Listings");HSSFRow rowhead= sheet.createRow((short)0);rowhead.createCell((short) 0).setCellValue("ITEM-ID");rowhead.createCell((short) 1).setCellValue("WAREHOUSE-ID");rowhead.createCell((short) 2).setCellValue("EXCEPTIONAL-PRICE");rowhead.createCell((short) 3).setCellValue("SNAPDEAL-LISTED");rowhead.createCell((short) 4).setCellValue("TRANSFER-PRICE");rowhead.createCell((short) 5).setCellValue("SELLING-PRICE");rowhead.createCell((short) 6).setCellValue("COURIER-COST");rowhead.createCell((short) 7).setCellValue("COMMISION");rowhead.createCell((short) 8).setCellValue("SERVICE-TAX");CatalogClient catalogServiceClient = null;List<SnapdealItemDetails> snapdealItems = null;try {catalogServiceClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port");in.shop2020.model.v1.catalog.CatalogService.Client catalogClient= catalogServiceClient.getClient();snapdealItems = catalogClient.getAllSnapdealItems();} catch (Exception e) {e.printStackTrace();}int iterator=1;for(SnapdealItemDetails snapdealItem:snapdealItems){HSSFRow row = sheet.createRow((short)iterator);row.createCell((short) 0).setCellValue(snapdealItem.getItem_id());row.createCell((short) 1).setCellValue(snapdealItem.getWarehouseId());row.createCell((short) 2).setCellValue(snapdealItem.getExceptionPrice());if(snapdealItem.isIsListedOnSnapdeal()){row.createCell((short) 3).setCellValue(1);}else{row.createCell((short) 3).setCellValue(0);}row.createCell((short) 4).setCellValue(snapdealItem.getTransferPrice());row.createCell((short) 5).setCellValue(snapdealItem.getSellingPrice());rowhead.createCell((short) 6).setCellValue(snapdealItem.getCourierCost());rowhead.createCell((short) 7).setCellValue(snapdealItem.getCommission());rowhead.createCell((short) 8).setCellValue(snapdealItem.getServiceTax());iterator++;}FileOutputStream fileOut = null;try {fileOut = new FileOutputStream(file);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {hwb.write(fileOut);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {fileOut.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}byte[] buffer = new byte[(int)file.length()];InputStream input = null;try {int totalBytesRead = 0;input = new BufferedInputStream(new FileInputStream(file));while(totalBytesRead < buffer.length){int bytesRemaining = buffer.length - totalBytesRead;int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining);if (bytesRead > 0){totalBytesRead = totalBytesRead + bytesRead;}}}finally {input.close();file.delete();}response.setHeader("Content-Disposition", "attachment; filename=\"Snapdeal-Bulk-Listings.xls\"");response.setContentType("application/octet-stream");ServletOutputStream sos;try {sos = response.getOutputStream();sos.write(buffer);sos.flush();} catch (IOException e) {System.out.println("Unable to stream the manifest file");}}public void uploadsnapdealBulkSheet() throws IOException, TException{File fileToCreate = new File("/tmp/", "Snapdeal-bulk-upload.xls");FileUtils.copyFile(this.file, fileToCreate);FileInputStream iFile = new FileInputStream(new File(file.getAbsolutePath()));HSSFWorkbook workbook = new HSSFWorkbook(iFile);HSSFSheet sheet = workbook.getSheetAt(0);Client catalogClient=null;StringBuilder sb = new StringBuilder();try {catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();} catch (TTransportException e) {e.printStackTrace();}for (int iterator=(sheet.getFirstRowNum()+1);iterator<=sheet.getLastRowNum();iterator++){SnapdealItem snapdealItem =null;Item item = null;Long sku;if (checkEmptyString(sheet.getRow(iterator).getCell(0))){continue;}else {sku=(long) sheet.getRow(iterator).getCell(0).getNumericCellValue();snapdealItem = catalogClient.getSnapdealItem(sku);try {item = catalogClient.getItem(sku);if (item.getId()==0){sb.append(sku + " Item not Present"+"\n");continue;}} catch (CatalogServiceException e) {sb.append(sku + " Item not Present"+"\n");continue;}if(snapdealItem.getItem_id()==0){snapdealItem = new SnapdealItem();snapdealItem.setItem_id(sku);}}if (!checkEmptyString(sheet.getRow(iterator).getCell(1))){long warehouseId = (long) sheet.getRow(iterator).getCell(1).getNumericCellValue();snapdealItem.setWarehouseId(warehouseId);}if (!checkEmptyString(sheet.getRow(iterator).getCell(2))){double exceptionPrice = sheet.getRow(iterator).getCell(2).getNumericCellValue();snapdealItem.setExceptionPrice(exceptionPrice);}if (!checkEmptyString(sheet.getRow(iterator).getCell(3))){if ((long)sheet.getRow(iterator).getCell(3).getNumericCellValue()==1){snapdealItem.setIsListedOnSnapdeal(true);}if ((long)sheet.getRow(iterator).getCell(3).getNumericCellValue()==0){snapdealItem.setIsListedOnSnapdeal(false);}}double transferPrice = 0,sellingPrice,commission,courierCost =45,serviceTax;if (checkEmptyString(sheet.getRow(iterator).getCell(4))){sb.append(sku + " Transfer Price cannot be empty"+"\n");continue;}if (!checkEmptyString(sheet.getRow(iterator).getCell(4))){transferPrice = sheet.getRow(iterator).getCell(4).getNumericCellValue();if(transferPrice==0){sb.append(sku + " Transfer Price cannot be zero"+"\n");continue;}snapdealItem.setTransferPrice(transferPrice);}if (!checkEmptyString(sheet.getRow(iterator).getCell(5))){if ((long)sheet.getRow(iterator).getCell(5).getNumericCellValue()==1){snapdealItem.setSuppressPriceFeed(true);}if ((long)sheet.getRow(iterator).getCell(5).getNumericCellValue()==0){snapdealItem.setSuppressPriceFeed(false);}}if (!checkEmptyString(sheet.getRow(iterator).getCell(6))){if ((long)sheet.getRow(iterator).getCell(6).getNumericCellValue()==1){snapdealItem.setSuppressInventoryFeed(true);}if ((long)sheet.getRow(iterator).getCell(6).getNumericCellValue()==0){snapdealItem.setSuppressInventoryFeed(false);}}double weight = item.getWeight();/**TODO Uncomment this for pricing feed.* if(weight==0){sb.append(sku + " Please add weight"+"\n");continue;}*/if (weight!=0){int slabs = (int) ((weight - .001)/(.5));for(int i=0;i<slabs;i++){courierCost = courierCost + 35;}}snapdealItem.setCourierCost(courierCost);sellingPrice = roundTwoDecimals(((transferPrice + courierCost*1.1236)/(100 - 3.59*1.1236))*100);snapdealItem.setSellingPrice(sellingPrice);double commision_value = roundTwoDecimals(.0359*sellingPrice + courierCost);commission = roundTwoDecimals(((.0359*sellingPrice + courierCost)/sellingPrice)*100);snapdealItem.setCommission(commission);serviceTax = roundTwoDecimals((commision_value * .1236));snapdealItem.setServiceTax(serviceTax);if(!catalogClient.addOrUpdateSnapdealItem(snapdealItem)){sb.append(sku + "\n");}}//logger.info("Amazon Bulk Map "+amazonBulkUpdate.toString());File file = new File("/tmp/amazonbulk");FileWriter writer = new FileWriter(file);writer.append(sb.toString());writer.close();byte[] buffer = new byte[(int)file.length()];InputStream input = null;try {int totalBytesRead = 0;input = new BufferedInputStream(new FileInputStream(file));while(totalBytesRead < buffer.length){int bytesRemaining = buffer.length - totalBytesRead;int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining);if (bytesRead > 0){totalBytesRead = totalBytesRead + bytesRead;}}}finally {input.close();file.delete();}response.setHeader("Content-Type", "text/javascript");ServletOutputStream sos;try {sos = response.getOutputStream();sos.write(buffer);sos.flush();} catch (IOException e) {System.out.println("Unable to stream the manifest file");}}public boolean checkEmptyString(Cell cell){if (cell==null){return true;}return false;}double roundTwoDecimals(double d) {DecimalFormat twoPlaces = new DecimalFormat("#.##");return Double.valueOf(twoPlaces.format(d));}public String show() {logger.info("Before fetching role");logger.info(request.getSession().toString());logger.info(ReportsUtils.ROLE);logger.info(session.getAttribute(ReportsUtils.ROLE).toString());logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1]);if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getRequestURI().substring(request.getContextPath().length()).split("/")[1])) {logger.info(request.getRequestURI().substring(request.getContextPath().length()).split("/")[1]);return "authfail";}if (StringUtils.equals(id, "snapdeal-options")){return "snapdeal-options";}if (StringUtils.equals(id, "item-table")){return "snapdeal-item-table";}return "id";}public void setId(String id) {this.id = id;}public HttpServletRequest getRequest() {logger.info("set request"+request.toString());return request;}public void setRequest(HttpServletRequest request) {this.request = request;}public HttpServletResponse getResponse() {return response;}public void setResponse(HttpServletResponse response) {this.response = response;}public HttpSession getSession() {return session;}public void setSession(HttpSession session) {logger.info("set session"+session.toString());this.session = session;}public ServletContext getContext() {return context;}public void setContext(ServletContext context) {this.context = context;}public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}public String getItemId() {return itemId;}public void setItemId(String itemId) {this.itemId = itemId;}public String getIsSnapdealListed() {return isSnapdealListed;}public void setIsSnapdealListed(String isSnapdealListed) {this.isSnapdealListed = isSnapdealListed;}public String getExceptionPrice() {return exceptionPrice;}public void setExceptionPrice(String exceptionPrice) {this.exceptionPrice = exceptionPrice;}public String getWarehouseId() {return warehouseId;}public void setWarehouseId(String warehouseId) {this.warehouseId = warehouseId;}public File getFile() {return file;}public void setFile(File file) {this.file = file;}public String getErrMsg() {return errMsg;}public void setErrMsg(String errMsg) {this.errMsg = errMsg;}public String getNext() {return next;}public void setNext(String next) {this.next = next;}public String getId() {return id;}public void setServletRequest(HttpServletRequest req) {this.request = req;this.session = req.getSession();}public void setServletContext(ServletContext arg0) {// TODO Auto-generated method stub}public void setServletResponse(HttpServletResponse response) {this.response = response;}public void setSearchText(String searchText) {this.searchText = searchText;}public String getSearchText() {return searchText;}public long getSearchCount() {return searchCount;}public long getTotalCount() {return totalCount;}public void setSearchCount(long count) {this.searchCount = count;}public List<SnapdealItemDetails> getSnapdealItems(){return snapdealItems;}public void setSellingPrice(String sellingPrice) {this.sellingPrice = sellingPrice;}public String getSellingPrice() {return sellingPrice;}public void setTransferPrice(String transferPrice) {this.transferPrice = transferPrice;}public String getTransferPrice() {return transferPrice;}public void setWebisteMrp(String webisteMrp) {this.webisteMrp = webisteMrp;}public String getWebisteMrp() {return webisteMrp;}public void setWebisteSellingPrice(String webisteSellingPrice) {this.webisteSellingPrice = webisteSellingPrice;}public String getWebisteSellingPrice() {return webisteSellingPrice;}public void setIsListedOnSnapdeal(String isListedOnSnapdeal) {this.isListedOnSnapdeal = isListedOnSnapdeal;}public String getIsSuppressInventoryFeed() {return isSuppressInventoryFeed;}public void setIsSuppressInventoryFeed(String isSuppressInventoryFeed) {this.isSuppressInventoryFeed = isSuppressInventoryFeed;}public String getIsSuppressPriceFeed() {return isSuppressPriceFeed;}public void setIsSuppressPriceFeed(String isSuppressPriceFeed) {this.isSuppressPriceFeed = isSuppressPriceFeed;}public String getIsListedOnSnapdeal() {return isListedOnSnapdeal;}public void setCommission(String commission) {this.commission = commission;}public String getCommission() {return commission;}public void setServiceTax(String serviceTax) {this.serviceTax = serviceTax;}public String getServiceTax() {return serviceTax;}public void setCourierCost(String courierCost) {this.courierCost = courierCost;}public String getCourierCost() {return courierCost;}public String getItemDetails(){return itemObj.toString();}}