📄 inventoryservices.java
字号:
/* * $Id: InventoryServices.java 7053 2006-03-23 06:14:49Z jacopo $ * * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * THE USE OR OTHER DEALINGS IN THE SOFTWARE. */package org.ofbiz.product.inventory;import java.sql.Timestamp;import java.util.ArrayList;import java.util.Calendar;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.UtilDateTime;import org.ofbiz.base.util.UtilMisc;import org.ofbiz.entity.GenericDelegator;import org.ofbiz.entity.GenericEntityException;import org.ofbiz.entity.GenericValue;import org.ofbiz.entity.condition.EntityExpr;import org.ofbiz.entity.condition.EntityOperator;import org.ofbiz.service.DispatchContext;import org.ofbiz.service.GenericServiceException;import org.ofbiz.service.LocalDispatcher;import org.ofbiz.service.ServiceUtil;/** * Inventory Services * * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a> * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a> * @author <a href="mailto:tiz@sastau.it">Jacopo Cappellato</a> * @version $Rev: 7053 $ * @since 2.0 */public class InventoryServices { public final static String module = InventoryServices.class.getName(); public static Map prepareInventoryTransfer(DispatchContext dctx, Map context) { GenericDelegator delegator = dctx.getDelegator(); String inventoryItemId = (String) context.get("inventoryItemId"); Double xferQty = (Double) context.get("xferQty"); GenericValue inventoryItem = null; GenericValue newItem = null; GenericValue userLogin = (GenericValue) context.get("userLogin"); try { inventoryItem = delegator.findByPrimaryKey("InventoryItem", UtilMisc.toMap("inventoryItemId", inventoryItemId)); } catch (GenericEntityException e) { return ServiceUtil.returnError("Inventory item lookup problem [" + e.getMessage() + "]"); } if (inventoryItem == null) { return ServiceUtil.returnError("Cannot locate inventory item."); } try { Map results = ServiceUtil.returnSuccess(); String inventoryType = inventoryItem.getString("inventoryItemTypeId"); if (inventoryType.equals("NON_SERIAL_INV_ITEM")) { Double atp = inventoryItem.getDouble("availableToPromiseTotal"); Double qoh = inventoryItem.getDouble("quantityOnHandTotal"); if (atp == null) { return ServiceUtil.returnError("The request transfer amount is not available, there is no available to promise on the Inventory Item with ID " + inventoryItem.getString("inventoryItemId")); } if (qoh == null) { qoh = atp; } // first make sure we have enough to cover the request transfer amount if (xferQty.doubleValue() > atp.doubleValue()) { return ServiceUtil.returnError("The request transfer amount is not available, the available to promise [" + atp + "] is not sufficient for the desired transfer quantity [" + xferQty + "] on the Inventory Item with ID " + inventoryItem.getString("inventoryItemId")); } /* * atp < qoh - split and save the qoh - atp * xferQty < atp - split and save atp - xferQty * atp < qoh && xferQty < atp - split and save qoh - atp + atp - xferQty */ // at this point we have already made sure that the xferQty is less than or equals to the atp, so if less that just create a new inventory record for the quantity to be moved // NOTE: atp should always be <= qoh, so if xfer < atp, then xfer < qoh, so no need to check/handle that // however, if atp < qoh && atp == xferQty, then we still need to split; oh, but no need to check atp == xferQty in the second part because if it isn't greater and isn't less, then it is equal if (xferQty.doubleValue() < atp.doubleValue() || atp.doubleValue() < qoh.doubleValue()) { Double negXferQty = new Double(-xferQty.doubleValue()); // NOTE: new inventory items should always be created calling the // createInventoryItem service because in this way we are sure // that all the relevant fields are filled with default values. // However, the code here should work fine because all the values // for the new inventory item are inerited from the existing item. newItem = GenericValue.create(inventoryItem); newItem.set("availableToPromiseTotal", new Double(0)); newItem.set("quantityOnHandTotal", new Double(0)); String newSeqId = null; try { newSeqId = delegator.getNextSeqId("InventoryItem"); } catch (IllegalArgumentException e) { return ServiceUtil.returnError("ERROR: Could not get next sequence id for InventoryItem, cannot create item."); } newItem.set("inventoryItemId", newSeqId); newItem.create(); results.put("inventoryItemId", newItem.get("inventoryItemId")); // TODO: how do we get this here: "inventoryTransferId", inventoryTransferId Map createNewDetailMap = UtilMisc.toMap("availableToPromiseDiff", xferQty, "quantityOnHandDiff", xferQty, "inventoryItemId", newItem.get("inventoryItemId"), "userLogin", userLogin); Map createUpdateDetailMap = UtilMisc.toMap("availableToPromiseDiff", negXferQty, "quantityOnHandDiff", negXferQty, "inventoryItemId", inventoryItem.get("inventoryItemId"), "userLogin", userLogin); try { Map resultNew = dctx.getDispatcher().runSync("createInventoryItemDetail", createNewDetailMap); if (ServiceUtil.isError(resultNew)) { return ServiceUtil.returnError("Inventory Item Detail create problem in prepare inventory transfer", null, null, resultNew); } Map resultUpdate = dctx.getDispatcher().runSync("createInventoryItemDetail", createUpdateDetailMap); if (ServiceUtil.isError(resultNew)) { return ServiceUtil.returnError("Inventory Item Detail create problem in prepare inventory transfer", null, null, resultUpdate); } } catch (GenericServiceException e1) { return ServiceUtil.returnError("Inventory Item Detail create problem in prepare inventory transfer: [" + e1.getMessage() + "]"); } } else { results.put("inventoryItemId", inventoryItem.get("inventoryItemId")); } } else if (inventoryType.equals("SERIALIZED_INV_ITEM")) { if (!"INV_AVAILABLE".equals(inventoryItem.getString("statusId"))) { return ServiceUtil.returnError("Serialized inventory is not available for transfer."); } } // setup values so that no one will grab the inventory during the move // if newItem is not null, it is the item to be moved, otherwise the original inventoryItem is the one to be moved if (inventoryType.equals("NON_SERIAL_INV_ITEM")) { // set the transfered inventory item's atp to 0 and the qoh to the xferQty; at this point atp and qoh will always be the same, so we can safely zero the atp for now GenericValue inventoryItemToClear = newItem == null ? inventoryItem : newItem; inventoryItemToClear.refresh(); double atp = inventoryItemToClear.get("availableToPromiseTotal") == null ? 0 : inventoryItemToClear.getDouble("availableToPromiseTotal").doubleValue(); if (atp != 0) { Map createDetailMap = UtilMisc.toMap("availableToPromiseDiff", new Double(-atp), "inventoryItemId", inventoryItemToClear.get("inventoryItemId"), "userLogin", userLogin); try { Map result = dctx.getDispatcher().runSync("createInventoryItemDetail", createDetailMap); if (ServiceUtil.isError(result)) { return ServiceUtil.returnError("Inventory Item Detail create problem in complete inventory transfer", null, null, result); } } catch (GenericServiceException e1) { return ServiceUtil.returnError("Inventory Item Detail create problem in complete inventory transfer: [" + e1.getMessage() + "]"); } } } else if (inventoryType.equals("SERIALIZED_INV_ITEM")) { // set the status to avoid re-moving or something if (newItem != null) { newItem.refresh(); newItem.set("statusId", "INV_BEING_TRANSFERED"); newItem.store(); results.put("inventoryItemId", newItem.get("inventoryItemId")); } else { inventoryItem.refresh(); inventoryItem.set("statusId", "INV_BEING_TRANSFERED"); inventoryItem.store(); results.put("inventoryItemId", inventoryItem.get("inventoryItemId")); } } return results; } catch (GenericEntityException e) { return ServiceUtil.returnError("Inventory store/create problem [" + e.getMessage() + "]"); } } public static Map completeInventoryTransfer(DispatchContext dctx, Map context) { GenericDelegator delegator = dctx.getDelegator(); String inventoryTransferId = (String) context.get("inventoryTransferId"); GenericValue inventoryTransfer = null; GenericValue inventoryItem = null; GenericValue destinationFacility = null; GenericValue userLogin = (GenericValue) context.get("userLogin"); try { inventoryTransfer = delegator.findByPrimaryKey("InventoryTransfer", UtilMisc.toMap("inventoryTransferId", inventoryTransferId)); inventoryItem = inventoryTransfer.getRelatedOne("InventoryItem"); destinationFacility = inventoryTransfer.getRelatedOne("ToFacility"); } catch (GenericEntityException e) { return ServiceUtil.returnError("Inventory Item/Transfer lookup problem [" + e.getMessage() + "]"); } if (inventoryTransfer == null || inventoryItem == null) { return ServiceUtil.returnError("ERROR: Lookup of InventoryTransfer and/or InventoryItem failed!"); } String inventoryType = inventoryItem.getString("inventoryItemTypeId"); // set the fields on the transfer record if (inventoryTransfer.get("receiveDate") == null) { inventoryTransfer.set("receiveDate", UtilDateTime.nowTimestamp()); } if (inventoryType.equals("NON_SERIAL_INV_ITEM")) { // add an adjusting InventoryItemDetail so set ATP back to QOH: ATP = ATP + (QOH - ATP), diff = QOH - ATP double atp = inventoryItem.get("availableToPromiseTotal") == null ? 0 : inventoryItem.getDouble("availableToPromiseTotal").doubleValue(); double qoh = inventoryItem.get("quantityOnHandTotal") == null ? 0 : inventoryItem.getDouble("quantityOnHandTotal").doubleValue(); Map createDetailMap = UtilMisc.toMap("availableToPromiseDiff", new Double(qoh - atp), "inventoryItemId", inventoryItem.get("inventoryItemId"), "userLogin", userLogin); try { Map result = dctx.getDispatcher().runSync("createInventoryItemDetail", createDetailMap); if (ServiceUtil.isError(result)) { return ServiceUtil.returnError("Inventory Item Detail create problem in complete inventory transfer", null, null, result); } } catch (GenericServiceException e1) { return ServiceUtil.returnError("Inventory Item Detail create problem in complete inventory transfer: [" + e1.getMessage() + "]"); } try { inventoryItem.refresh(); } catch (GenericEntityException e) { return ServiceUtil.returnError("Inventory refresh problem [" + e.getMessage() + "]"); } } else if (inventoryType.equals("SERIALIZED_INV_ITEM")) { inventoryItem.set("statusId", "INV_AVAILABLE"); } // set the fields on the item Map updateInventoryItemMap = UtilMisc.toMap("inventoryItemId", inventoryItem.getString("inventoryItemId"), "facilityId", inventoryTransfer.get("facilityIdTo"), "containerId", inventoryTransfer.get("containerIdTo"),
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -