Rev 9729 | Rev 9811 | 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.MarketplaceItems;import in.shop2020.model.v1.catalog.SnapdealItem;import in.shop2020.model.v1.catalog.SnapdealItemDetails;import in.shop2020.model.v1.inventory.InventoryServiceException;import in.shop2020.model.v1.inventory.InventoryType;import in.shop2020.model.v1.inventory.ItemInventory;import in.shop2020.model.v1.inventory.Warehouse;import in.shop2020.model.v1.inventory.WarehouseType;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.InventoryClient;import in.shop2020.thrift.clients.TransactionClient;import in.shop2020.utils.GmailUtils;import javax.mail.MessagingException;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;private String maxNlc;private String sdItemCode;private String supc;private String held;private String lastUpdatedInventoryTimestamp;private String lastCheckedTimestamp;private String otherCost;private String isAutoIncrement;private String isAutoDecrement;private String isManualFavourite;private String isAutoFavourite;private String currentSp;private String currentTp;private String minPosSp;private String minPosTp;private String vat;private String comparsionResult;public String getComparsionResult() {return comparsionResult;}public void setComparsionResult(String comparsionResult) {this.comparsionResult = comparsionResult;}private Map<Integer, Long> holdInventoryMap;public String getVat() {return vat;}public void setVat(String vat) {this.vat = vat;}public String getOtherCost() {return otherCost;}public void setOtherCost(String otherCost) {this.otherCost = otherCost;}public String getCurrentSp() {return currentSp;}public void setCurrentSp(String currentSp) {this.currentSp = currentSp;}public String getCurrentTp() {return currentTp;}public void setCurrentTp(String currentTp) {this.currentTp = currentTp;}public String getLastCheckedTimestamp() {return lastCheckedTimestamp;}public String getLastUpdatedInventoryTimestamp() {return lastUpdatedInventoryTimestamp;}public void setLastUpdatedInventoryTimestamp(String lastUpdatedInventoryTimestamp) {this.lastUpdatedInventoryTimestamp = lastUpdatedInventoryTimestamp;}public void setLastCheckedTimestamp(String lastCheckedTimestamp) {this.lastCheckedTimestamp = lastCheckedTimestamp;}public String getIsAutoIncrement() {return isAutoIncrement;}public void setIsAutoIncrement(String isAutoIncrement) {this.isAutoIncrement = isAutoIncrement;}public String getIsAutoDecrement() {return isAutoDecrement;}public void setIsAutoDecrement(String isAutoDecrement) {this.isAutoDecrement = isAutoDecrement;}public String getIsManualFavourite() {return isManualFavourite;}public void setIsManualFavourite(String isManualFavourite) {this.isManualFavourite = isManualFavourite;}public String getIsAutoFavourite() {return isAutoFavourite;}public void setIsAutoFavourite(String isAutoFavourite) {this.isAutoFavourite = isAutoFavourite;}public String getMinPosSp() {return minPosSp;}public void setMinPosSp(String minPosSp) {this.minPosSp = minPosSp;}public String getMinPosTp() {return minPosTp;}public void setMinPosTp(String minPosTp) {this.minPosTp = minPosTp;}public String getHeld() {return held;}public void setHeld(String held) {this.held = held;}public String getSupc() {return supc;}public void setSupc(String supc) {this.supc = supc;}public String getSdItemCode() {return sdItemCode;}public void setSdItemCode(String sdItemCode) {this.sdItemCode = sdItemCode;}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();SnapdealItemDetails sdItemDetails = CatalogClient.getSnapdealItemDetails(Long.valueOf(id));lastUpdatedInventoryTimeStampForItem(sdItemDetails.getLastUpdatedInventoryTimestamp());return sdItemDetails;}public MarketplaceItems fetchMarketplaceDetail() throws NumberFormatException, TException {Client CatalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();MarketplaceItems mpItemDetails = CatalogClient.getMarketplacedetailsForItem(Long.valueOf(id), 7);lastCheckedTimeStampForItem(mpItemDetails.getLastCheckedTimestamp());return mpItemDetails;}public void lastUpdatedInventoryTimeStampForItem(long timestamp){setLastUpdatedInventoryTimestamp(new java.util.Date(timestamp).toLocaleString());}public void lastCheckedTimeStampForItem(long timestamp){setLastCheckedTimestamp(new java.util.Date(timestamp).toLocaleString());}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, InventoryServiceException, CatalogServiceException, JSONException{Client catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();SnapdealItem snapdealItem = catalogClient.getSnapdealItem(Long.valueOf(itemId));MarketplaceItems mpItem = catalogClient.getMarketplacedetailsForItem(Long.valueOf(itemId),7);InventoryClient inventoryServiceClient = new InventoryClient();in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();Warehouse warehouse = inventoryClient.getWarehouse(Long.valueOf(warehouseId));if (!(warehouse.getWarehouseType()==WarehouseType.OURS && warehouse.getInventoryType() == InventoryType.GOOD)){throw new InventoryServiceException();}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 (snapdealItem.getMaxNlc()!=Double.valueOf(maxNlc)){snapdealItem.setMaxNlc(Double.valueOf(maxNlc));commit = true;}if (!StringUtils.equalsIgnoreCase(snapdealItem.getSkuAtSnapdeal(), sdItemCode)){snapdealItem.setSkuAtSnapdeal(sdItemCode);commit=true;}if (!StringUtils.equalsIgnoreCase(snapdealItem.getSupc(), supc)){snapdealItem.setSupc(supc);commit=true;}MarketplaceItems t_mpItem = updateMarketplaceItemDetails(mpItem);if(!compareParameters(t_mpItem,snapdealItem)){setComparsionResult("0");return "snapdeal-update-result";}else{setComparsionResult("1");}if (commit){if(!catalogClient.addOrUpdateSnapdealItem(snapdealItem)){throw new CatalogServiceException();}}boolean result = catalogClient.updateMarketplaceAttributesForItem(t_mpItem);if(!result){throw new CatalogServiceException();}if (t_mpItem.getCurrentSp()< t_mpItem.getMinimumPossibleSp()){sendAlert(t_mpItem.getItemId(),t_mpItem.getCurrentSp(),t_mpItem.getMinimumPossibleSp());}return "snapdeal-update-result";}public boolean compareParameters(MarketplaceItems mpItem, SnapdealItem snapdealItem) throws TException, JSONException{double nlc =getNlcForWarehouse(snapdealItem.getWarehouseId(),snapdealItem.getItem_id());double vat = (snapdealItem.getSellingPrice()/(1+(mpItem.getVat()/100))-(nlc/(1+(mpItem.getVat()/100))))*(mpItem.getVat()/100);double inHouseCost = 15+vat+(mpItem.getReturnProvision()/100)*snapdealItem.getSellingPrice()+mpItem.getOtherCost();double lowest_possible_tp = nlc+inHouseCost;double our_tp = snapdealItem.getSellingPrice()- snapdealItem.getSellingPrice()*(mpItem.getCommission()/100)*(1+(mpItem.getServiceTax()/100))-snapdealItem.getCourierCost()*(1+(mpItem.getServiceTax()/100));double commission = ((mpItem.getCommission()/100)*snapdealItem.getSellingPrice());double service_tax = (mpItem.getServiceTax()/100)*(snapdealItem.getCommission()+snapdealItem.getCourierCost());double lowest_possible_sp = (nlc+(snapdealItem.getCourierCost()+0)*1.1236*(1+(mpItem.getVat()/100))+(15+mpItem.getOtherCost())*(1+(mpItem.getVat())/100))/(1-(mpItem.getCommission()/100+0)*1.1236*(1+(mpItem.getVat())/100)-.005*(1+(mpItem.getVat())/100));JSONObject x = new JSONObject();x.put("NLC", nlc);x.put("VAT", vat);x.put("LOWEST POS TP", lowest_possible_tp);x.put("TP", our_tp);x.put("COMMISSION", commission);x.put("SERVICE TAX", service_tax);x.put("LOWEST POS SP", lowest_possible_sp);logger.info("Snapdeal Backend validation "+x.toString());boolean compare_result=true;if(!(nlc==snapdealItem.getMaxNlc())){compare_result=false;}if(!(lowest_possible_tp-mpItem.getMinimumPossibleTp()>-1 && lowest_possible_tp-mpItem.getMinimumPossibleTp()<1)){compare_result=false;}if(!(our_tp-snapdealItem.getTransferPrice()>-1 && our_tp-snapdealItem.getTransferPrice()<1)){compare_result=false;}if(!(commission-snapdealItem.getCommission()>-1 && commission-snapdealItem.getCommission()<1)){compare_result=false;}if(!(service_tax-snapdealItem.getServiceTax()>-1 && service_tax-snapdealItem.getServiceTax()<1)){compare_result=false;}if(!(lowest_possible_sp-mpItem.getMinimumPossibleSp()>-1 && lowest_possible_sp-mpItem.getMinimumPossibleSp()<1)){compare_result=false;}return compare_result;}public double getNlcForWarehouse(long warehouseId,long item_id) throws TException{InventoryClient inventoryServiceClient = new InventoryClient();in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();return inventoryClient.getNlcForWarehouse(warehouseId, item_id);}public double getCourierCost(double weight){double cCost = 45;int slabs = (int) ((weight - .001)/(.5));for(int i=0;i<slabs;i++){cCost = cCost + 35;}return cCost;}private MarketplaceItems updateMarketplaceItemDetails(MarketplaceItems mpItem){mpItem.setItemId(Long.valueOf(itemId));mpItem.setSource(7);mpItem.setVat(Double.valueOf(vat));mpItem.setCourierCost(Double.valueOf(courierCost));mpItem.setOtherCost(Double.valueOf(otherCost));mpItem.setCurrentSp(Double.valueOf(sellingPrice));mpItem.setCurrentTp(Double.valueOf(transferPrice));mpItem.setAutoDecrement(Boolean.valueOf(isAutoDecrement));mpItem.setAutoIncrement(Boolean.valueOf(isAutoIncrement));mpItem.setManualFavourite(Boolean.valueOf(isManualFavourite));mpItem.setMinimumPossibleSp(Double.valueOf(minPosSp));mpItem.setMinimumPossibleTp(Double.valueOf(minPosTp));return mpItem;}private void sendAlert(long itemId,double sp,double minSp){Item item;try{Client catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();item= catalogClient.getItem(itemId);}catch(Exception e){e.printStackTrace();return;}String emailSubjectTxt="Snapdeal SP is set below Min Possible SP";String[] sendTo=new String[]{ "kshitij.sood@saholic.com","rajneesh.arora@saholic.com","rajveer.singh@saholic.com","vikram.raghav@saholic.com"};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();logger.info(text);String emailFromAddress = "build@shop2020.in";String password = "cafe@nes";GmailUtils mailer = new GmailUtils();try {//mailer.sendSSLMessage(sendTo, emailSubjectTxt, text, emailFromAddress, password, new ArrayList<File>());}catch (Exception e) {logger.info(e.toString());}}public void addNewItem() throws TException, NumberFormatException, InventoryServiceException, CatalogServiceException{InventoryClient inventoryServiceClient = new InventoryClient();in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();Warehouse warehouse = inventoryClient.getWarehouse(Long.valueOf(warehouseId));if (!(warehouse.getWarehouseType()==WarehouseType.OURS && warehouse.getInventoryType() == InventoryType.GOOD)){throw new InventoryServiceException();}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));snapdealItem.setMaxNlc(Double.valueOf(maxNlc));snapdealItem.setSkuAtSnapdeal(sdItemCode);snapdealItem.setSupc(supc);MarketplaceItems mpItem = new MarketplaceItems();MarketplaceItems t_mpItem = updateMarketplaceItemDetails(mpItem);Client catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();catalogClient.addOrUpdateSnapdealItem(snapdealItem);boolean result =catalogClient.updateMarketplaceAttributesForItem(t_mpItem);if(!result){throw new CatalogServiceException();}if (t_mpItem.getCurrentSp()< t_mpItem.getMinimumPossibleSp()){sendAlert(t_mpItem.getItemId(),t_mpItem.getCurrentSp(),t_mpItem.getMinimumPossibleSp());}}public String getAddNewItemForm(){return "snapdeal-add-item";}public String getItemDetailsInJson() throws NumberFormatException, CatalogServiceException, TException, JSONException, InventoryServiceException{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();}InventoryClient inventoryServiceClient = new InventoryClient();in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();Warehouse warehouse = inventoryClient.getWarehouse(Long.valueOf(warehouseId));if (!(warehouse.getWarehouseType()==WarehouseType.OURS && warehouse.getInventoryType() == InventoryType.GOOD)){throw new InventoryServiceException();}double maxNLC = inventoryClient.getNlcForWarehouse(Long.valueOf(warehouseId), Long.valueOf(itemId));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());itemObj.put("MaxNlc", maxNLC);return "item-details-json";}public String ke$ha() throws NumberFormatException, TException, JSONException, InventoryServiceException{InventoryClient inventoryServiceClient = new InventoryClient();in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();Warehouse warehouse = inventoryClient.getWarehouse(Long.valueOf(warehouseId));if (warehouse.getWarehouseType()==WarehouseType.OURS && warehouse.getInventoryType() == InventoryType.GOOD){double maxNLC = inventoryClient.getNlcForWarehouse(Long.valueOf(warehouseId), Long.valueOf(itemId));itemObj = new JSONObject();itemObj.put("ItemId", itemId);itemObj.put("WarehouseId", warehouseId);itemObj.put("MaxNlc", maxNLC);}else{itemObj = new JSONObject();throw new InventoryServiceException();}return "item-details-json";}public ItemInventory getItemInventory(String itemId) throws NumberFormatException, InventoryServiceException, TException{try{InventoryClient inventoryServiceClient = new InventoryClient();in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();in.shop2020.model.v1.inventory.ItemInventory thriftItemInventory = inventoryClient.getItemInventoryByItemId(Long.valueOf(itemId));return thriftItemInventory;}catch(Exception e){logger.error("Unable to get inventory info for item",e);return null;}}public Warehouse getWarehouse(String warehouseId,String itemId) throws NumberFormatException, TException, InventoryServiceException {InventoryClient inventoryServiceClient = new InventoryClient();holdInventoryMap =inventoryServiceClient.getClient().getHeldInventoryMapForItem(Long.valueOf(itemId), Long.valueOf(warehouseId));return inventoryServiceClient.getClient().getWarehouse(Long.valueOf(warehouseId));}public Map<Integer, Long> getHoldInventoryMapForItem(){return holdInventoryMap;}public void changeHeldForSource() throws NumberFormatException, TException, InventoryServiceException{InventoryClient inventoryServiceClient = new InventoryClient();Warehouse warehouse = inventoryServiceClient.getClient().getWarehouse(Long.valueOf(warehouseId));if (!(warehouse.getWarehouseType()==WarehouseType.OURS && warehouse.getInventoryType() == InventoryType.GOOD)){throw new InventoryServiceException();}inventoryServiceClient.getClient().addUpdateHoldInventory(Long.valueOf(itemId),Long.valueOf(warehouseId),Long.valueOf(held),7);}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");rowhead.createCell((short) 9).setCellValue("Suppress Inventory Feed");rowhead.createCell((short) 10).setCellValue("Suppress Price Feed");rowhead.createCell((short) 11).setCellValue("Max NLC");rowhead.createCell((short) 12).setCellValue("SKU @ Snapdeal");rowhead.createCell((short) 13).setCellValue("SUPC");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());row.createCell((short) 6).setCellValue(snapdealItem.getCourierCost());row.createCell((short) 7).setCellValue(snapdealItem.getCommission());row.createCell((short) 8).setCellValue(snapdealItem.getServiceTax());row.createCell((short) 9).setCellValue(snapdealItem.isSuppressInventoryFeed());row.createCell((short) 10).setCellValue(snapdealItem.isSuppressPriceFeed());row.createCell((short) 11).setCellValue(snapdealItem.getMaxNlc());row.createCell((short) 12).setCellValue(snapdealItem.getSkuAtSnapdeal());row.createCell((short) 13).setCellValue(snapdealItem.getSupc());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, NumberFormatException, InventoryServiceException{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();}InventoryClient inventoryServiceClient = new InventoryClient();in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();for (int iterator=(sheet.getFirstRowNum()+1);iterator<=sheet.getLastRowNum();iterator++){SnapdealItem snapdealItem =null;Item item = null;Long sku;boolean new_item = false;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){new_item = true;snapdealItem = new SnapdealItem();snapdealItem.setItem_id(sku);}}if (checkEmptyString(sheet.getRow(iterator).getCell(1)) && new_item){sb.append(sku + " New Listing - Warehouse Id not Present"+"\n");continue;}if (!checkEmptyString(sheet.getRow(iterator).getCell(1))){Warehouse warehouse = inventoryClient.getWarehouse(Long.valueOf(warehouseId));if (!(warehouse.getWarehouseType()==WarehouseType.OURS && warehouse.getInventoryType() == InventoryType.GOOD)){sb.append(sku + "Not acceptable warehouse"+"\n");continue;}long warehouseId = (long) sheet.getRow(iterator).getCell(1).getNumericCellValue();double maxNLC = inventoryClient.getNlcForWarehouse(warehouseId, item.getId());if (maxNLC==0){sb.append(sku + "Max Nlc can't be 0"+"\n");continue;}snapdealItem.setWarehouseId(warehouseId);snapdealItem.setMaxNlc(maxNLC);}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);}}if (!checkEmptyString(sheet.getRow(iterator).getCell(7))){sheet.getRow(iterator).getCell(7).setCellType(Cell.CELL_TYPE_STRING);String snapdealItemCode = sheet.getRow(iterator).getCell(7).getStringCellValue();snapdealItem.setSkuAtSnapdeal(snapdealItemCode);}if (!checkEmptyString(sheet.getRow(iterator).getCell(8))){sheet.getRow(iterator).getCell(8).setCellType(Cell.CELL_TYPE_STRING);String supc = sheet.getRow(iterator).getCell(8).getStringCellValue();snapdealItem.setSupc(supc);}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.USER_NAME).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 void setMaxNlc(String maxNlc) {this.maxNlc = maxNlc;}public String getMaxNlc() {return maxNlc;}public String getItemDetails(){return itemObj.toString();}public double getRoundOfValue(String val){return roundTwoDecimals(Double.valueOf(val));}}