Rev 7588 | Rev 10121 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.inventory.controllers;import in.shop2020.inventory.controllers.PurchaseController.ScanRecordType;import in.shop2020.model.v1.catalog.CatalogService;import in.shop2020.model.v1.catalog.CatalogServiceException;import in.shop2020.model.v1.catalog.Item;import in.shop2020.model.v1.catalog.ItemType;import in.shop2020.model.v1.inventory.InventoryServiceException;import in.shop2020.model.v1.inventory.Vendor;import in.shop2020.model.v1.inventory.InventoryService.Client;import in.shop2020.model.v1.inventory.Warehouse;import in.shop2020.model.v1.inventory.WarehouseType;import in.shop2020.model.v1.order.LineItem;import in.shop2020.purchase.PurchaseReturn;import in.shop2020.purchase.PurchaseServiceException;import in.shop2020.thrift.clients.CatalogClient;import in.shop2020.thrift.clients.InventoryClient;import in.shop2020.thrift.clients.PurchaseClient;import in.shop2020.thrift.clients.WarehouseClient;import in.shop2020.warehouse.InventoryItem;import in.shop2020.warehouse.Scan;import in.shop2020.warehouse.ScanType;import in.shop2020.warehouse.TransferLot;import in.shop2020.warehouse.WarehouseService;import in.shop2020.warehouse.WarehouseServiceException;import java.io.BufferedInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.HashMap;import java.util.Hashtable;import java.util.List;import java.util.Map;import java.util.Map.Entry;import java.util.Set;import java.util.ArrayList;import javax.servlet.ServletOutputStream;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;import org.apache.thrift.TException;import org.apache.thrift.transport.TTransportException;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class TransferLotController extends BaseController{/****/private static final long serialVersionUID = 3052114881399915348L;private Long id;private TransferLot transferLot;private List<TransferLot>transferLots;private String remoteTransferRefNumber;private String fromDate;private String toDate;private String errorMessage = "";private Calendar transferLotFromDate;private Calendar transferLotToDate;private Long originWarehouseId;private List<LineItem> lineItems;private Long destinationWarehouseId;private String output;private static Map<Long, Warehouse> warehouseMap;private static SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");private static Logger logger = LoggerFactory.getLogger(TransferLotController.class);static {warehouseMap = new HashMap<Long, Warehouse>();InventoryClient inventoryServiceClient;try {inventoryServiceClient = new InventoryClient();Client inventoryClient = inventoryServiceClient.getClient();List<Warehouse> warehouses;warehouses = inventoryClient.getAllWarehouses(true);for(Warehouse warehouse : warehouses){warehouseMap.put(warehouse.getId(), warehouse);}} catch (TTransportException e) {logger.error("Error in populating warehouse map", e);} catch (TException e) {logger.error("Error in populating warehouse map", e);} catch (InventoryServiceException isex) {logger.error("Error in populating warehouse map", isex);}}public String index(){if(warehouseMap==null||warehouseMap.size()<1){fetchWarehouseMap();}try{WarehouseService.Client warehouseClient = new WarehouseClient().getClient();transferLotFromDate = Calendar.getInstance();transferLotToDate = Calendar.getInstance();if(fromDate==null) {transferLotFromDate.add(Calendar.MONTH, -2);} else {transferLotFromDate.setTime(formatter.parse(fromDate));if(toDate!=null) {transferLotToDate.setTime(formatter.parse(toDate));}}transferLots = warehouseClient.getTransferLotsByDate(transferLotFromDate.getTimeInMillis(), transferLotToDate.getTimeInMillis());} catch(TException e){logger.error("Error in getting transfer lots", e);} catch(ParseException pex) {logger.error("Error in parsing time", pex);} catch(WarehouseServiceException wsex) {logger.error("Error in getting transfer lots", wsex);}return INDEX;}private void fetchWarehouseMap() {warehouseMap = new HashMap<Long, Warehouse>();InventoryClient inventoryServiceClient;try {inventoryServiceClient = new InventoryClient();Client inventoryClient = inventoryServiceClient.getClient();List<Warehouse> warehouses;warehouses = inventoryClient.getAllWarehouses(true);for(Warehouse warehouse : warehouses){warehouseMap.put(warehouse.getId(), warehouse);}} catch (TTransportException e) {logger.error("Error in populating warehouse map", e);} catch (TException e) {logger.error("Error in populating warehouse map", e);} catch (InventoryServiceException isex) {logger.error("Error in populating warehouse map", isex);}}public String create() {originWarehouseId = Long.parseLong(request.getParameter("originWarehouseId"));destinationWarehouseId = Long.parseLong(request.getParameter("destinationWarehouseId"));try {WarehouseService.Client warehouseClient = new WarehouseClient().getClient();id = warehouseClient.createTransferLot(originWarehouseId, destinationWarehouseId);} catch (TException tex) {logger.error("Error in creating transferlot between warehouseId " + originWarehouseId + " and " + destinationWarehouseId, tex);errorMessage = "Error in creating transferlot between warehouseId " + originWarehouseId + " and " + destinationWarehouseId;} catch (WarehouseServiceException wex) {logger.error("Error in creating transferlot between warehouseId " + originWarehouseId + " and " + destinationWarehouseId, wex);errorMessage = "Error in creating transferlot between warehouseId " + originWarehouseId + " and " + destinationWarehouseId;}return show();}public String show(){resetLineItems();return SHOW;}public String update() {try {if(!areValidScans()){errorMessage = "Invalid Scans";return show();}if(id == null || id==0) {errorMessage = "No transferLot-Id selected for Transfer";return "new";}List<InventoryItem> inventoryItems = new ArrayList<InventoryItem>();CatalogService.Client catalogClient = new CatalogClient().getClient();for (LineItem lineItem : lineItems) {if (lineItem.getItem_id() == 0) {continue;}InventoryItem inventoryItem = new InventoryItem();inventoryItem.setItemId(lineItem.getItem_id());inventoryItem.setCurrentQuantity(new Double(lineItem.getQuantity()).longValue());inventoryItem.setItemNumber(lineItem.getItem_number());if(catalogClient.getItem(lineItem.getItem_id()).getType()==ItemType.SERIALIZED) {inventoryItem.setSerialNumber(lineItem.getSerial_number());}inventoryItems.add(inventoryItem);}WarehouseService.Client warehouseClient = new WarehouseClient().getClient();warehouseClient.scanForTransferOut(inventoryItems, ScanType.WAREHOUSE_TRANSFER_OUT, id);} catch (TTransportException e) {errorMessage = "Error while establishing connection to the warehouse server";logger.error(errorMessage, e);} catch (WarehouseServiceException e) {errorMessage = e.getMessage();logger.error(errorMessage, e);} catch (TException e) {errorMessage = "Error while scanning in the item";logger.error(errorMessage, e);} catch (CatalogServiceException csex) {errorMessage = "Error while getting the serialized details of item";logger.error(errorMessage, csex);}return show();}public String editNew(){return "new";}private boolean areValidScans() throws NumberFormatException, TException {boolean areValidScans = true;return areValidScans;}private void resetLineItems() {lineItems = new ArrayList<LineItem>();for (int i = 0; i < 11; i++) {LineItem lineItem = new LineItem();lineItem.setId(i);lineItem.setExtra_info("");lineItem.setSerial_number("");lineItem.setItem_number("");lineItem.setQuantity(1);lineItem.setItem_id(-1);lineItems.add(lineItem);}}public String getTransferLotItems() {try {WarehouseService.Client warehouseClient = new WarehouseClient().getClient();Map<Long, Long> transferLotItems = warehouseClient.getItemsInTransferLot(id);CatalogService.Client catalogClient = new CatalogClient().getClient();String transferLotItemDiv = "<div id = 'transfer-lot-item-list-div' align='center'><table id = 'transfer-lot-item-list-table'><tr><th>Item</th><th>Quantity</th></tr>";for(Long itemId : transferLotItems.keySet()){Item item = catalogClient.getItem(itemId);transferLotItemDiv = transferLotItemDiv + "<tr><td>" + item.getBrand() + " " + item.getModelName() + " " + item.getModelNumber() + " " + item.getColor();transferLotItemDiv = transferLotItemDiv + "</td><td>" + transferLotItems.get(itemId) +"</td></tr>";}transferLotItemDiv = transferLotItemDiv + "</table></div>";setOutput(transferLotItemDiv);} catch (TException tex) {logger.error("Error in getting transfer lot items",tex);} catch (CatalogServiceException csex) {logger.error("Error in getting transfer lot items",csex);}return OUTPUT;}public String markTransferLotAsReceived() throws Exception {try{WarehouseService.Client warehouseClient = new WarehouseClient().getClient();warehouseClient.markItemsAsReceivedForTransferLot(id);warehouseClient.markTransferLotAsReceived(id, remoteTransferRefNumber);} catch(TException e){logger.error("Error in marking transfer lot as received",e);throw new Exception("Error in marking transfer lot as received");} catch(WarehouseServiceException wsex) {logger.error("Error in marking transfer lot as received",wsex);throw new Exception("Error in marking transfer lot as received");}return INDEX;}public void downloadFBASheet() {try {WarehouseService.Client warehouseClient = new WarehouseClient().getClient();Map<Long, Long> transferLotItems = warehouseClient.getItemsInTransferLot(id);File fbaSheetFile = new File("transfer-lot-"+id+"-sheet_"+new Date() + ".xls");FileOutputStream fStream = null;try {fStream = new FileOutputStream(fbaSheetFile);} catch (FileNotFoundException e1) {logger.error(e1.getMessage());}ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();Workbook wb = new HSSFWorkbook();Sheet fbaSheet = wb.createSheet("FBA Sheet");Row merchantShipmentName = fbaSheet.createRow((short)0);merchantShipmentName.createCell(0).setCellValue("MerchantShipmentName");Row labelPrepPreference = fbaSheet.createRow((short)1);labelPrepPreference.createCell(0).setCellValue("LabelPrepPreference");Row addressName = fbaSheet.createRow((short)2);addressName.createCell(0).setCellValue("AddressName");Row addressFieldOne = fbaSheet.createRow((short)3);addressFieldOne.createCell(0).setCellValue("AddressFieldOne");Row addressFieldTwo = fbaSheet.createRow((short)4);addressFieldTwo.createCell(0).setCellValue("AddressFieldTwo");Row addressCity = fbaSheet.createRow((short)5);addressCity.createCell(0).setCellValue("AddressCity");Row addressCountryCode = fbaSheet.createRow((short)6);addressCountryCode.createCell(0).setCellValue("AddressCountryCode");Row addressStateOrRegion = fbaSheet.createRow((short)7);addressStateOrRegion.createCell(0).setCellValue("AddressStateOrRegion");Row addressPostalCode = fbaSheet.createRow((short)8);addressPostalCode.createCell(0).setCellValue("AddressPostalCode");Row addressDistrict = fbaSheet.createRow((short)9);addressDistrict.createCell(0).setCellValue("AddressDistrict");Row blankRow = fbaSheet.createRow((short)10);//blankRow.createCell(0).setCellValue("AddressPostalCode");Row skuHeaders = fbaSheet.createRow((short)11);skuHeaders.createCell(0).setCellValue("MerchantSKU");skuHeaders.createCell(1).setCellValue("Quantity");int rowCount = 12;for(Entry<Long, Long> itemQuantity : transferLotItems.entrySet()) {Row newRow = fbaSheet.createRow((short)rowCount);newRow.createCell(0).setCellValue("FBA"+itemQuantity.getKey());newRow.createCell(1).setCellValue(itemQuantity.getValue());rowCount++;}try {wb.write(baosXLS);baosXLS.close();baosXLS.writeTo(fStream);fStream.flush();fStream.close();} catch(IOException e) {//TODO}byte[] buffer = null;buffer = new byte[(int) fbaSheetFile.length()];InputStream input = null;try {int totalBytesRead = 0;input = new BufferedInputStream(new FileInputStream(fbaSheetFile));while (totalBytesRead < buffer.length) {int bytesRemaining = buffer.length - totalBytesRead;// input.read() returns -1, 0, or more :int bytesRead = input.read(buffer, totalBytesRead,bytesRemaining);if (bytesRead > 0) {totalBytesRead = totalBytesRead + bytesRead;}}/* the above style is a bit tricky: it places bytes into the* 'buffer' array; 'buffer' is an output parameter; the while* loop usually has a single iteration only.*/} finally {input.close();}response.setContentType("application/vnd.ms-excel");response.setHeader("Content-disposition", "attachment; filename="+ fbaSheetFile.getName());ServletOutputStream sos = response.getOutputStream();sos.write(buffer);sos.flush();} catch (TException tex) {logger.error("Error in generating fba sheet for transfer Lot Id " + id,tex);} catch (FileNotFoundException fnfex) {logger.error("Error in generating fba sheet for transfer Lot Id " + id,fnfex);} catch (IOException ioex) {logger.error("Error in generating fba sheet for transfer Lot Id " + id,ioex);}}public String getWarehouseName(Long warehouseId){return warehouseMap.get(warehouseId).getDisplayName();}public boolean isTransferReceivable(TransferLot transferLot) {if(warehouseMap.get(transferLot.getDestinationWarehouseId()).getWarehouseType()==WarehouseType.OURS_THIRDPARTY||warehouseMap.get(transferLot.getDestinationWarehouseId()).getWarehouseType()==WarehouseType.THIRD_PARTY) {return false;} else {return true;}}public String getDateTime(long milliseconds) {Calendar cal = Calendar.getInstance();cal.setTimeInMillis(milliseconds);return formatter.format(cal.getTime());}public static void setWarehouseMap(Map<Long, Warehouse> warehouseMap) {TransferLotController.warehouseMap = warehouseMap;}public TransferLot getTransferLot() {return transferLot;}public void setTransferLot(TransferLot transferLot) {this.transferLot = transferLot;}public List<TransferLot> getTransferLots() {return transferLots;}public void setTransferLots(List<TransferLot> transferLots) {this.transferLots = transferLots;}public String getRemoteTransferRefNumber() {return remoteTransferRefNumber;}public void setRemoteTransferRefNumber(String remoteTransferRefNumber) {this.remoteTransferRefNumber = remoteTransferRefNumber;}public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getOutput() {return output;}public void setOutput(String output) {this.output = output;}public Long getOriginWarehouseId() {return originWarehouseId;}public void setOriginWarehouseId(Long originWarehouseId) {this.originWarehouseId = originWarehouseId;}public Long getDestinationWarehouseId() {return destinationWarehouseId;}public void setDestinationWarehouseId(Long destinationWarehouseId) {this.destinationWarehouseId = destinationWarehouseId;}public String getErrorMessage() {return errorMessage;}public void setErrorMsg(String errorMessage) {this.errorMessage = errorMessage;}public List<LineItem> getLineItems() {return lineItems;}public void setLineItems(List<LineItem> lineItems) {this.lineItems = lineItems;}}