Rev 5361 | Rev 5496 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.inventory.controllers;import in.shop2020.model.v1.catalog.InventoryType;import in.shop2020.model.v1.catalog.Item;import in.shop2020.model.v1.catalog.ItemType;import in.shop2020.model.v1.catalog.Warehouse;import in.shop2020.model.v1.catalog.WarehouseType;import in.shop2020.model.v1.order.LineItem;import in.shop2020.purchase.PurchaseOrder;import in.shop2020.purchase.PurchaseServiceException;import in.shop2020.thrift.clients.CatalogClient;import in.shop2020.thrift.clients.PurchaseClient;import in.shop2020.thrift.clients.WarehouseClient;import in.shop2020.utils.ModelUtils;import in.shop2020.warehouse.InventoryItem;import in.shop2020.warehouse.ScanType;import in.shop2020.warehouse.WarehouseService.Client;import in.shop2020.warehouse.WarehouseServiceException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.servlet.ServletContext;import org.apache.struts2.convention.annotation.Result;import org.apache.struts2.convention.annotation.Results;import org.apache.thrift.TException;import org.apache.thrift.transport.TTransportException;import org.slf4j.Logger;import org.slf4j.LoggerFactory;@SuppressWarnings("serial")@Results({ @Result(name = "redirect", type = "redirectAction", params = {"actionName", "warehouse" }) })public class PurchaseController extends BaseController {/****/private static final int NUM_BULK__SCAN_ITEMS = 10;private static Logger logger = LoggerFactory.getLogger(PurchaseController.class);private static enum ScanRecordType { VALID, BLANK };private ServletContext context;private String id;private String itemId;private String itemNo;private String errorMsg = "";private List<Item> items;private List<LineItem> lineItems;private String purchaseOrderId;private Warehouse warehouse;public String editNew() {purchaseOrderId = request.getParameter("poId");return "new";}public String create() {this.purchaseOrderId = request.getParameter("poId");long poId = Long.parseLong(purchaseOrderId);String invoiceNumber = request.getParameter("invoiceNo");String fc = request.getParameter("freightCharges").trim();double freightCharges = 0;if (fc != null && !fc.isEmpty())freightCharges = Double.parseDouble(fc);try {PurchaseClient purchaseClient = new PurchaseClient();in.shop2020.purchase.PurchaseService.Client client = purchaseClient.getClient();id = "" + client.startPurchase(poId, invoiceNumber, freightCharges);} catch (TTransportException e) {errorMsg = "Error while establishing connection to the warehouse server";logger.error(errorMsg, e);} catch (TException e) {errorMsg = "Error while scanning in the item";logger.error(errorMsg, e);} catch (PurchaseServiceException e) {errorMsg = e.getMessage();logger.error(errorMsg, e);}if (errorMsg.isEmpty())return "create";else {addActionError(errorMsg);return "new";}}public String show() {resetLineItems();setItemsFromPO(Long.parseLong(id));return SHOW;}private void resetLineItems() {lineItems = new ArrayList<LineItem>();for (int i = 0; i < NUM_BULK__SCAN_ITEMS; 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 update() {long id = Long.parseLong(this.id);setItemsFromPO(id);try {if (!areValidScans()) {return SHOW;}WarehouseClient warehouseClient = new WarehouseClient();Client client = warehouseClient.getClient();for (LineItem lineItem : lineItems) {if (ScanRecordType.BLANK.name().equals(lineItem.getExtra_info())) {continue;}if (ItemType.SERIALIZED.name().equals(lineItem.getProductGroup())) {InventoryItem inventoryItem = client.createSerializedInventoryItem(lineItem.getItem_id(), lineItem.getItem_number(), lineItem.getSerial_number(), id);client.scanSerializedItem(inventoryItem, ScanType.PURCHASE, PurchaseOrderController.WAREHOUSE_ID);}else {InventoryItem inventoryItem = client.createInventoryItem(lineItem.getItem_id(), new Double(lineItem.getQuantity()).longValue(), id);client.scan(inventoryItem, ScanType.PURCHASE, new Double(lineItem.getQuantity()).longValue(), PurchaseOrderController.WAREHOUSE_ID);}}resetLineItems();} catch (TTransportException e) {errorMsg = "Error while establishing connection to the warehouse server";logger.error(errorMsg, e);} catch (WarehouseServiceException e) {errorMsg = e.getMessage();logger.error(errorMsg, e);} catch (TException e) {errorMsg = "Error while scanning in the item";logger.error(errorMsg, e);}if (!errorMsg.isEmpty()) {addActionError(errorMsg);}return SHOW;}/*** @return* @throws TException* @throws NumberFormatException*/private boolean areValidScans() throws NumberFormatException, TException {boolean areValidScans = true;in.shop2020.purchase.PurchaseService.Client purchaseClient = new PurchaseClient().getClient();PurchaseOrder purchaseOrder = purchaseClient.getPurchaseOrderForPurchase(Long.parseLong(id));Map<Long, Long> itemsQuantityMapFromPO = new HashMap<Long, Long>();for (in.shop2020.purchase.LineItem lineItem : purchaseOrder.getLineitems()) {itemsQuantityMapFromPO.put(lineItem.getItemId(), (long) lineItem.getUnfulfilledQuantity());}Client warehouseClient = new WarehouseClient().getClient();for (LineItem lineItem : lineItems) {if (lineItem.getItem_id() == -1 && lineItem.getItem_number().isEmpty() && lineItem.getSerial_number().isEmpty()) {lineItem.setExtra_info(ScanRecordType.BLANK.name());}else {lineItem.setExtra_info(ScanRecordType.VALID.name());if (ItemType.SERIALIZED.name().equals(lineItem.getProductGroup())) {if (lineItem.getSerial_number().isEmpty()) {lineItem.setExtra_info("Serial number not present.");areValidScans = false;continue;}try {warehouseClient.getInventoryItem(lineItem.getSerial_number());lineItem.setExtra_info("Item scanned already.");areValidScans = false;continue;}catch (Exception e) {}}if (lineItem.getItem_id() == -1 && lineItem.getItem_number().isEmpty()) {lineItem.setExtra_info("Item not selected/Item number not present.");areValidScans = false;continue;}if (lineItem.getItem_id() == -1) {List<Long> itemIds = warehouseClient.getItemIds(lineItem.getItem_number());if (itemIds.isEmpty()) {lineItem.setExtra_info("Unknown item number");areValidScans = false;continue;}if (itemIds.size() > 0) {int numItemsInPO = 0;for (long itemId : itemIds) {if (itemsQuantityMapFromPO.containsKey(itemId)) {numItemsInPO++;lineItem.setItem_id(itemId);}}if (numItemsInPO > 1) {lineItem.setExtra_info("Multiple items found for given item Number. Choose item explicitly.");areValidScans = false;continue;}}}if (!itemsQuantityMapFromPO.containsKey(lineItem.getItem_id())) {lineItem.setExtra_info("Item not present in PO.");areValidScans = false;continue;}itemsQuantityMapFromPO.put(lineItem.getItem_id(),itemsQuantityMapFromPO.get(lineItem.getItem_id()) - new Double(lineItem.getQuantity()).longValue());if (itemsQuantityMapFromPO.get(lineItem.getItem_id()) < 0) {lineItem.setExtra_info("Item already fulfilled in PO.");areValidScans = false;continue;}}}return areValidScans;}private void setItemsFromPO(long id) {try {items = new ArrayList<Item>();in.shop2020.purchase.PurchaseService.Client purchaseClient = new PurchaseClient().getClient();PurchaseOrder purchaseOrder = purchaseClient.getPurchaseOrderForPurchase(id);in.shop2020.model.v1.catalog.InventoryService.Client catalogClient = new CatalogClient().getClient();for (in.shop2020.purchase.LineItem lineItem : purchaseOrder.getLineitems()) {Item item = new Item();item.setId(lineItem.getItemId());item.setBrand(lineItem.getBrand());item.setModelName(lineItem.getModelName());item.setModelNumber(lineItem.getModelNumber());item.setColor(lineItem.getColor());item.setProductGroup(catalogClient.getItem(lineItem.getItemId()).getType().name());items.add(item);}warehouse = catalogClient.getWarehouses(WarehouseType.OURS, InventoryType.GOOD, purchaseOrder.getSupplierId(), PurchaseOrderController.WAREHOUSE_ID, PurchaseOrderController.WAREHOUSE_ID).get(0);} catch (Exception e) {logger.error("Could not find items in PO with purchase: " + id, e);}}public String destroy() {long id = Long.parseLong(this.id);try {PurchaseClient warehouseClient = new PurchaseClient();in.shop2020.purchase.PurchaseService.Client client = warehouseClient.getClient();client.closePurchase(id);} catch (TTransportException e) {errorMsg = "Error while establishing connection to the warehouse server";logger.error(errorMsg, e);} catch (TException e) {errorMsg = "Error while scanning in the item";logger.error(errorMsg, e);} catch (PurchaseServiceException e) {errorMsg = e.getMessage();logger.error(errorMsg, e);}return "redirect";}public String createItemNumberMapping() {long itemIdLong = Long.parseLong(itemId);try {Client warehouseClient = new WarehouseClient().getClient();warehouseClient.createItemNumberMapping(itemNo, itemIdLong);} catch (TTransportException e) {logger.error("Could not create thrift client", e);} catch (TException e) {logger.error("Could not create item number mapping", e);}return show();}public String itemNumberMappingEditNew() {return "item-number-mapping";}public String getName(Item item){return ModelUtils.extractProductNameFromItem(item);}public void setId(String id) {this.id = id;}public String getId() {return id;}public String getErrorMessage() {return errorMsg;}public String getPurchaseOrderId() {return purchaseOrderId;}public String getServletContextPath() {return context.getContextPath();}public String getItemId() {return itemId;}public void setItemId(String itemId) {this.itemId = itemId;}public String getItemNo() {return itemNo;}public void setItemNo(String itemNo) {this.itemNo = itemNo;}public List<Item> getItems() {return items;}public void setItems(List<Item> items) {this.items = items;}public List<LineItem> getLineItems() {return lineItems;}public void setLineItems(List<LineItem> lineItems) {this.lineItems = lineItems;}public Warehouse getWarehouse() {return warehouse;}}