📄 inventoryservices.java
字号:
"locationSeqId", inventoryTransfer.get("locationSeqIdTo"), "userLogin", userLogin); // if the destination facility's owner is different // from the inventory item's ownwer, // the inventory item is assigned to the new owner. if (destinationFacility != null && destinationFacility.get("ownerPartyId") != null) { String fromPartyId = inventoryItem.getString("ownerPartyId"); String toPartyId = destinationFacility.getString("ownerPartyId"); if (fromPartyId == null || !fromPartyId.equals(toPartyId)) { updateInventoryItemMap.put("ownerPartyId", toPartyId); } } try { Map result = dctx.getDispatcher().runSync("updateInventoryItem", updateInventoryItemMap); if (ServiceUtil.isError(result)) { return ServiceUtil.returnError("Inventory item store problem", null, null, result); } } catch (GenericServiceException exc) { return ServiceUtil.returnError("Inventory item store problem [" + exc.getMessage() + "]"); } // set the inventory transfer record to complete inventoryTransfer.set("statusId", "IXF_COMPLETE"); // store the entities try { inventoryTransfer.store(); } catch (GenericEntityException e) { return ServiceUtil.returnError("Inventory store problem [" + e.getMessage() + "]"); } return ServiceUtil.returnSuccess(); } public static Map cancelInventoryTransfer(DispatchContext dctx, Map context) { GenericDelegator delegator = dctx.getDelegator(); String inventoryTransferId = (String) context.get("inventoryTransferId"); GenericValue inventoryTransfer = null; GenericValue inventoryItem = null; GenericValue userLogin = (GenericValue) context.get("userLogin"); try { inventoryTransfer = delegator.findByPrimaryKey("InventoryTransfer", UtilMisc.toMap("inventoryTransferId", inventoryTransferId)); inventoryItem = inventoryTransfer.getRelatedOne("InventoryItem"); } 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"); // re-set the fields on the item 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 cancel inventory transfer", null, null, result); } } catch (GenericServiceException e1) { return ServiceUtil.returnError("Inventory Item Detail create problem in cancel inventory transfer: [" + e1.getMessage() + "]"); } } else if (inventoryType.equals("SERIALIZED_INV_ITEM")) { inventoryItem.set("statusId", "INV_AVAILABLE"); // store the entity try { inventoryItem.store(); } catch (GenericEntityException e) { return ServiceUtil.returnError("Inventory item store problem in cancel inventory transfer: [" + e.getMessage() + "]"); } } return ServiceUtil.returnSuccess(); } /** In spite of the generic name this does the very specific task of checking availability of all back-ordered items and sends notices, etc */ public static Map checkInventoryAvailability(DispatchContext dctx, Map context) { GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); GenericValue userLogin = (GenericValue) context.get("userLogin"); /* TODO: NOTE: This method has been updated, but testing requires many eyes. See http://jira.undersunconsulting.com/browse/OFBIZ-662 boolean skipThisNeedsUpdating = true; if (skipThisNeedsUpdating) { Debug.logWarning("NOT Running the checkInventoryAvailability service, no backorders or such will be automatically created; the reason is that this serice needs to be updated to use OrderItemShipGroup instead of OrderShipmentPreference which it currently does.", module); return ServiceUtil.returnSuccess(); } */ Map ordersToUpdate = new HashMap(); Map ordersToCancel = new HashMap(); // find all inventory items w/ a negative ATP List inventoryItems = null; try { List exprs = UtilMisc.toList(new EntityExpr("availableToPromiseTotal", EntityOperator.LESS_THAN, new Double(0))); inventoryItems = delegator.findByAnd("InventoryItem", exprs); } catch (GenericEntityException e) { Debug.logError(e, "Trouble getting inventory items", module); return ServiceUtil.returnError("Problem getting InventoryItem records"); } if (inventoryItems == null) { Debug.logInfo("No items out of stock; no backorders to worry about", module); return ServiceUtil.returnSuccess(); } Debug.log("OOS Inventory Items: " + inventoryItems.size(), module); Iterator itemsIter = inventoryItems.iterator(); while (itemsIter.hasNext()) { GenericValue inventoryItem = (GenericValue) itemsIter.next(); // get the incomming shipment information for the item List shipmentAndItems = null; try { List exprs = new ArrayList(); exprs.add(new EntityExpr("productId", EntityOperator.EQUALS, inventoryItem.get("productId"))); exprs.add(new EntityExpr("destinationFacilityId", EntityOperator.EQUALS, inventoryItem.get("facilityId"))); exprs.add(new EntityExpr("statusId", EntityOperator.NOT_EQUAL, "SHIPMENT_DELIVERED")); exprs.add(new EntityExpr("statusId", EntityOperator.NOT_EQUAL, "SHIPMENT_CANCELLED")); shipmentAndItems = delegator.findByAnd("ShipmentAndItem", exprs, UtilMisc.toList("estimatedArrivalDate")); } catch (GenericEntityException e) { Debug.logError(e, "Problem getting ShipmentAndItem records", module); return ServiceUtil.returnError("Problem getting ShipmentAndItem records"); } // get the reservations in order of newest first List reservations = null; try { reservations = inventoryItem.getRelated("OrderItemShipGrpInvRes", null, UtilMisc.toList("-reservedDatetime")); } catch (GenericEntityException e) { Debug.logError(e, "Problem getting related reservations", module); return ServiceUtil.returnError("Problem getting related reservations"); } if (reservations == null) { Debug.logWarning("No outstanding reservations for this inventory item, why is it negative then?", module); continue; } Debug.log("Reservations for item: " + reservations.size(), module); // available at the time of order double availableBeforeReserved = inventoryItem.getDouble("availableToPromiseTotal").doubleValue(); // go through all the reservations in order Iterator ri = reservations.iterator(); while (ri.hasNext()) { GenericValue reservation = (GenericValue) ri.next(); String orderId = reservation.getString("orderId"); String orderItemSeqId = reservation.getString("orderItemSeqId"); Timestamp promisedDate = reservation.getTimestamp("promisedDatetime"); Timestamp currentPromiseDate = reservation.getTimestamp("currentPromisedDate"); Timestamp actualPromiseDate = currentPromiseDate; if (actualPromiseDate == null) { if (promisedDate != null) { actualPromiseDate = promisedDate; } else { // fall back if there is no promised date stored actualPromiseDate = reservation.getTimestamp("reservedDatetime"); } } Debug.log("Promised Date: " + actualPromiseDate, module); // find the next possible ship date Timestamp nextShipDate = null; double availableAtTime = 0.00; Iterator si = shipmentAndItems.iterator(); while (si.hasNext()) { GenericValue shipmentItem = (GenericValue) si.next(); availableAtTime += shipmentItem.getDouble("quantity").doubleValue(); if (availableAtTime >= availableBeforeReserved) { nextShipDate = shipmentItem.getTimestamp("estimatedArrivalDate"); break; } } Debug.log("Next Ship Date: " + nextShipDate, module); // create a modified promise date (promise date - 1 day) Calendar pCal = Calendar.getInstance(); pCal.setTimeInMillis(actualPromiseDate.getTime()); pCal.add(Calendar.DAY_OF_YEAR, -1); Timestamp modifiedPromisedDate = new Timestamp(pCal.getTimeInMillis()); Timestamp now = UtilDateTime.nowTimestamp(); Debug.log("Promised Date + 1: " + modifiedPromisedDate, module); Debug.log("Now: " + now, module); // check the promised date vs the next ship date if (nextShipDate == null || nextShipDate.after(actualPromiseDate)) { if (nextShipDate == null && modifiedPromisedDate.after(now)) { // do nothing; we are okay to assume it will be shipped on time Debug.log("No ship date known yet, but promised date hasn't approached, assuming it will be here on time", module); } else { // we cannot ship by the promised date; need to notify the customer Debug.log("We won't ship on time, getting notification info", module); Map notifyItems = (Map) ordersToUpdate.get(orderId); if (notifyItems == null) { notifyItems = new HashMap(); } notifyItems.put(orderItemSeqId, nextShipDate); ordersToUpdate.put(orderId, notifyItems); // need to know if nextShipDate is more then 30 days after promised Calendar sCal = Calendar.getInstance(); sCal.setTimeInMillis(actualPromiseDate.getTime()); sCal.add(Calendar.DAY_OF_YEAR, 30); Timestamp farPastPromised = new Timestamp(sCal.getTimeInMillis()); // check to see if this is >30 days or second run, if so flag to cancel boolean needToCancel = false; if (nextShipDate == null || nextShipDate.after(farPastPromised)) { // we cannot ship until >30 days after promised; using cancel rule Debug.log("Ship date is >30 past the promised date", module); needToCancel = true; } else if (currentPromiseDate != null && actualPromiseDate.equals(currentPromiseDate)) { // this is the second notification; using cancel rule needToCancel = true; } // add the info to the cancel map if we need to schedule a cancel if (needToCancel) { // queue the item to be cancelled Debug.log("Flagging the item to auto-cancel", module); Map cancelItems = (Map) ordersToCancel.get(orderId); if (cancelItems == null) { cancelItems = new HashMap(); } cancelItems.put(orderItemSeqId, farPastPromised); ordersToCancel.put(orderId, cancelItems); } // store the updated promiseDate as the nextShipDate try { reservation.set("currentPromisedDate", nextShipDate); reservation.store(); } catch (GenericEntityException e) { Debug.logError(e, "Problem storing reservation : " + reservation, module); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -