Search in sources :

Example 21 with GenericValue

use of org.apache.ofbiz.entity.GenericValue in project ofbiz-framework by apache.

the class ShipmentServices method fillShipmentStagingTables.

public static Map<String, Object> fillShipmentStagingTables(DispatchContext dctx, Map<String, ? extends Object> context) {
    Delegator delegator = dctx.getDelegator();
    String shipmentId = (String) context.get("shipmentId");
    Locale locale = (Locale) context.get("locale");
    GenericValue shipment = null;
    if (shipmentId != null) {
        try {
            shipment = EntityQuery.use(delegator).from("Shipment").where("shipmentId", shipmentId).queryOne();
        } catch (GenericEntityException e) {
            Debug.logError(e, module);
            return ServiceUtil.returnError(e.getMessage());
        }
    }
    if (shipment == null) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ProductShipmentNotFoundId", locale));
    }
    String shipmentStatusId = shipment.getString("statusId");
    if ("SHIPMENT_PACKED".equals(shipmentStatusId)) {
        GenericValue address = null;
        try {
            address = shipment.getRelatedOne("DestinationPostalAddress", false);
        } catch (GenericEntityException e) {
            Debug.logError(e, module);
            return ServiceUtil.returnError(e.getMessage());
        }
        if (address == null) {
            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ProductShipmentNoAddressFound", locale));
        }
        List<GenericValue> packages = null;
        try {
            packages = shipment.getRelated("ShipmentPackage", null, null, false);
        } catch (GenericEntityException e) {
            Debug.logError(e, module);
            return ServiceUtil.returnError(e.getMessage());
        }
        if (UtilValidate.isEmpty(packages)) {
            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ProductShipmentNoPackagesAvailable", locale));
        }
        List<GenericValue> routeSegs = null;
        try {
            routeSegs = shipment.getRelated("ShipmentRouteSegment", null, null, false);
        } catch (GenericEntityException e) {
            Debug.logError(e, module);
            return ServiceUtil.returnError(e.getMessage());
        }
        GenericValue routeSeg = EntityUtil.getFirst(routeSegs);
        // to store list
        List<GenericValue> toStore = new LinkedList<GenericValue>();
        // make the staging records
        GenericValue stageShip = delegator.makeValue("OdbcShipmentOut");
        stageShip.set("shipmentId", shipment.get("shipmentId"));
        stageShip.set("partyId", shipment.get("partyIdTo"));
        stageShip.set("carrierPartyId", routeSeg.get("carrierPartyId"));
        stageShip.set("shipmentMethodTypeId", routeSeg.get("shipmentMethodTypeId"));
        stageShip.set("toName", address.get("toName"));
        stageShip.set("attnName", address.get("attnName"));
        stageShip.set("address1", address.get("address1"));
        stageShip.set("address2", address.get("address2"));
        stageShip.set("directions", address.get("directions"));
        stageShip.set("city", address.get("city"));
        stageShip.set("postalCode", address.get("postalCode"));
        stageShip.set("postalCodeExt", address.get("postalCodeExt"));
        stageShip.set("countryGeoId", address.get("countryGeoId"));
        stageShip.set("stateProvinceGeoId", address.get("stateProvinceGeoId"));
        stageShip.set("numberOfPackages", Long.valueOf(packages.size()));
        stageShip.set("handlingInstructions", shipment.get("handlingInstructions"));
        toStore.add(stageShip);
        for (GenericValue shipmentPkg : packages) {
            GenericValue stagePkg = delegator.makeValue("OdbcPackageOut");
            stagePkg.set("shipmentId", shipmentPkg.get("shipmentId"));
            stagePkg.set("shipmentPackageSeqId", shipmentPkg.get("shipmentPackageSeqId"));
            stagePkg.set("orderId", shipment.get("primaryOrderId"));
            stagePkg.set("shipGroupSeqId", shipment.get("primaryShipGroupSeqId"));
            stagePkg.set("shipmentBoxTypeId", shipmentPkg.get("shipmentBoxTypeId"));
            stagePkg.set("weight", shipmentPkg.get("weight"));
            toStore.add(stagePkg);
        }
        try {
            delegator.storeAll(toStore);
        } catch (GenericEntityException e) {
            Debug.logError(e, module);
            return ServiceUtil.returnError(e.getMessage());
        }
    } else {
        Debug.logWarning("Shipment #" + shipmentId + " is not available for shipment; not setting in staging tables.", module);
    }
    return ServiceUtil.returnSuccess();
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) LinkedList(java.util.LinkedList)

Example 22 with GenericValue

use of org.apache.ofbiz.entity.GenericValue in project ofbiz-framework by apache.

the class ShipmentServices method removeShipmentEstimate.

public static Map<String, Object> removeShipmentEstimate(DispatchContext dctx, Map<String, ? extends Object> context) {
    Delegator delegator = dctx.getDelegator();
    String shipmentCostEstimateId = (String) context.get("shipmentCostEstimateId");
    Locale locale = (Locale) context.get("locale");
    GenericValue estimate = null;
    try {
        estimate = EntityQuery.use(delegator).from("ShipmentCostEstimate").where("shipmentCostEstimateId", shipmentCostEstimateId).queryOne();
        estimate.remove();
    } catch (GenericEntityException e) {
        Debug.logError(e, module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ProductShipmentCostEstimateRemoveError", UtilMisc.toMap("errorString", e.toString()), locale));
    }
    return ServiceUtil.returnSuccess();
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException)

Example 23 with GenericValue

use of org.apache.ofbiz.entity.GenericValue in project ofbiz-framework by apache.

the class ShipmentServices method updatePurchaseShipmentFromReceipt.

/**
 * Whenever a ShipmentReceipt is generated, check the Shipment associated
 * with it to see if all items were received. If so, change its status to
 * PURCH_SHIP_RECEIVED. The check is accomplished by counting the
 * products shipped (from ShipmentAndItem) and matching them with the
 * products received (from ShipmentReceipt).
 */
public static Map<String, Object> updatePurchaseShipmentFromReceipt(DispatchContext dctx, Map<String, ? extends Object> context) {
    Delegator delegator = dctx.getDelegator();
    LocalDispatcher dispatcher = dctx.getDispatcher();
    String shipmentId = (String) context.get("shipmentId");
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    try {
        List<GenericValue> shipmentReceipts = EntityQuery.use(delegator).from("ShipmentReceipt").where("shipmentId", shipmentId).queryList();
        if (shipmentReceipts.size() == 0)
            return ServiceUtil.returnSuccess();
        // If there are shipment receipts, the shipment must have been shipped, so set the shipment status to PURCH_SHIP_SHIPPED if it's only PURCH_SHIP_CREATED
        GenericValue shipment = EntityQuery.use(delegator).from("Shipment").where("shipmentId", shipmentId).queryOne();
        if ((!UtilValidate.isEmpty(shipment)) && "PURCH_SHIP_CREATED".equals(shipment.getString("statusId"))) {
            Map<String, Object> updateShipmentMap = dispatcher.runSync("updateShipment", UtilMisc.<String, Object>toMap("shipmentId", shipmentId, "statusId", "PURCH_SHIP_SHIPPED", "userLogin", userLogin));
            if (ServiceUtil.isError(updateShipmentMap)) {
                return updateShipmentMap;
            }
        }
        List<GenericValue> shipmentAndItems = EntityQuery.use(delegator).from("ShipmentAndItem").where("shipmentId", shipmentId, "statusId", "PURCH_SHIP_SHIPPED").queryList();
        if (shipmentAndItems.size() == 0) {
            return ServiceUtil.returnSuccess();
        }
        // store the quantity of each product shipped in a hashmap keyed to productId
        Map<String, BigDecimal> shippedCountMap = new HashMap<String, BigDecimal>();
        for (GenericValue item : shipmentAndItems) {
            BigDecimal shippedQuantity = item.getBigDecimal("quantity");
            BigDecimal quantity = shippedCountMap.get(item.getString("productId"));
            quantity = quantity == null ? shippedQuantity : shippedQuantity.add(quantity);
            shippedCountMap.put(item.getString("productId"), quantity);
        }
        // store the quantity of each product received in a hashmap keyed to productId
        Map<String, BigDecimal> receivedCountMap = new HashMap<String, BigDecimal>();
        for (GenericValue item : shipmentReceipts) {
            BigDecimal receivedQuantity = item.getBigDecimal("quantityAccepted");
            BigDecimal quantity = receivedCountMap.get(item.getString("productId"));
            quantity = quantity == null ? receivedQuantity : receivedQuantity.add(quantity);
            receivedCountMap.put(item.getString("productId"), quantity);
        }
        // let Map.equals do all the hard comparison work
        if (!shippedCountMap.equals(receivedCountMap)) {
            return ServiceUtil.returnSuccess();
        }
        // now update the shipment
        Map<String, Object> serviceResult = dispatcher.runSync("updateShipment", UtilMisc.<String, Object>toMap("shipmentId", shipmentId, "statusId", "PURCH_SHIP_RECEIVED", "userLogin", userLogin));
        if (ServiceUtil.isError(serviceResult)) {
            return ServiceUtil.returnError(ServiceUtil.getErrorMessage(serviceResult));
        }
    } catch (GenericEntityException e) {
        Debug.logError(e, module);
        return ServiceUtil.returnError(e.getMessage());
    } catch (GenericServiceException se) {
        Debug.logError(se, module);
        return ServiceUtil.returnError(se.getMessage());
    }
    return ServiceUtil.returnSuccess();
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) Delegator(org.apache.ofbiz.entity.Delegator) HashMap(java.util.HashMap) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) BigDecimal(java.math.BigDecimal)

Example 24 with GenericValue

use of org.apache.ofbiz.entity.GenericValue in project ofbiz-framework by apache.

the class IssuanceTest method testMultipleInventoryItemIssuance.

public void testMultipleInventoryItemIssuance() throws Exception {
    String facilityId = "WebStoreWarehouse";
    String productId = "GZ-2644";
    String orderId = "DEMO81015";
    String orderItemSeqId = "00001";
    String shipGroupSeqId = "00001";
    String shipmentItemSeqId = "00001";
    PackingSession packSession = new PackingSession(dispatcher, userLogin, facilityId, null, orderId, shipGroupSeqId);
    packSession.addOrIncreaseLine(orderId, orderItemSeqId, shipGroupSeqId, productId, BigDecimal.valueOf(6L), 1, BigDecimal.valueOf(1000L), false);
    String shipmentId = packSession.complete(false);
    GenericValue orderHeader = EntityQuery.use(delegator).from("OrderHeader").where("orderId", orderId).cache().queryOne();
    // Test the OrderShipment is correct
    List<GenericValue> orderShipments = orderHeader.getRelated("OrderShipment", null, null, false);
    assertFalse("No OrderShipment for order", UtilValidate.isEmpty(orderShipments));
    assertEquals("Incorrect number of OrderShipments for order", 1, orderShipments.size());
    GenericValue orderShipment = orderShipments.get(0);
    assertEquals(orderItemSeqId, orderShipment.getString("orderItemSeqId"));
    assertEquals(shipGroupSeqId, orderShipment.getString("shipGroupSeqId"));
    assertEquals(shipmentId, orderShipment.getString("shipmentId"));
    assertEquals(shipmentItemSeqId, orderShipment.getString("shipmentItemSeqId"));
    BigDecimal actual = orderShipment.getBigDecimal("quantity");
    assertTrue("Incorrect quantity in OrderShipment. Expected 6.00000 actual " + actual, actual.compareTo(BigDecimal.valueOf(6L)) == 0);
    // Test the ItemIssuances are correct
    List<GenericValue> itemIssuances = orderHeader.getRelated("ItemIssuance", null, UtilMisc.toList("inventoryItemId"), false);
    assertFalse("No ItemIssuances for order", UtilValidate.isEmpty(itemIssuances));
    assertEquals("Incorrect number of ItemIssuances for order", 2, itemIssuances.size());
    GenericValue itemIssuance = itemIssuances.get(0);
    assertEquals(orderItemSeqId, itemIssuance.getString("orderItemSeqId"));
    assertEquals(shipGroupSeqId, itemIssuance.getString("shipGroupSeqId"));
    assertEquals(shipmentId, itemIssuance.getString("shipmentId"));
    assertEquals(shipmentItemSeqId, itemIssuance.getString("shipmentItemSeqId"));
    assertEquals("9001", itemIssuance.getString("inventoryItemId"));
    actual = itemIssuance.getBigDecimal("quantity");
    assertTrue("Incorrect quantity in ItemIssuance. Expected 5.00000 actual " + actual, actual.compareTo(BigDecimal.valueOf(5L)) == 0);
    itemIssuance = itemIssuances.get(1);
    assertEquals(orderItemSeqId, itemIssuance.getString("orderItemSeqId"));
    assertEquals(shipGroupSeqId, itemIssuance.getString("shipGroupSeqId"));
    assertEquals(shipmentId, itemIssuance.getString("shipmentId"));
    assertEquals(shipmentItemSeqId, itemIssuance.getString("shipmentItemSeqId"));
    assertEquals("9025", itemIssuance.getString("inventoryItemId"));
    actual = itemIssuance.getBigDecimal("quantity");
    assertTrue("Incorrect quantity in ItemIssuance. Expected 1.00000 actual " + actual, actual.compareTo(BigDecimal.valueOf(1L)) == 0);
    // Test reservations have been removed
    List<GenericValue> reservations = orderHeader.getRelated("OrderItemShipGrpInvRes", null, null, false);
    assertTrue("Reservations exist for order - should have been deleted", UtilValidate.isEmpty(reservations));
    // Test order header status is now ORDER_COMPLETED
    assertEquals(orderHeader.getString("statusId"), "ORDER_COMPLETED");
    // Test order items status are now ITEM_COMPLETED
    List<GenericValue> orderItems = orderHeader.getRelated("OrderItem", null, null, false);
    for (GenericValue orderItem : orderItems) {
        assertEquals("ITEM_COMPLETED", orderItem.getString("statusId"));
    }
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) PackingSession(org.apache.ofbiz.shipment.packing.PackingSession) BigDecimal(java.math.BigDecimal)

Example 25 with GenericValue

use of org.apache.ofbiz.entity.GenericValue in project ofbiz-framework by apache.

the class PackingSessionLine method issueItemToShipment.

protected void issueItemToShipment(String shipmentId, String picklistBinId, GenericValue userLogin, BigDecimal quantity, LocalDispatcher dispatcher) throws GeneralException {
    if (quantity == null) {
        quantity = this.getQuantity();
    }
    Map<String, Object> issueMap = new HashMap<String, Object>();
    issueMap.put("shipmentId", shipmentId);
    issueMap.put("orderId", this.getOrderId());
    issueMap.put("orderItemSeqId", this.getOrderItemSeqId());
    issueMap.put("shipGroupSeqId", this.getShipGroupSeqId());
    issueMap.put("inventoryItemId", this.getInventoryItemId());
    issueMap.put("quantity", quantity);
    issueMap.put("userLogin", userLogin);
    Map<String, Object> issueResp = dispatcher.runSync("issueOrderItemShipGrpInvResToShipment", issueMap);
    if (ServiceUtil.isError(issueResp)) {
        throw new GeneralException(ServiceUtil.getErrorMessage(issueResp));
    }
    String shipmentItemSeqId = (String) issueResp.get("shipmentItemSeqId");
    if (shipmentItemSeqId == null) {
        throw new GeneralException("Issue item did not return a valid shipmentItemSeqId!");
    } else {
        this.setShipmentItemSeqId(shipmentItemSeqId);
    }
    if (picklistBinId != null) {
        // find the pick list item
        Debug.logInfo("Looking up picklist item for bin ID #" + picklistBinId, module);
        Delegator delegator = dispatcher.getDelegator();
        Map<String, Object> itemLookup = new HashMap<String, Object>();
        itemLookup.put("picklistBinId", picklistBinId);
        itemLookup.put("orderId", this.getOrderId());
        itemLookup.put("orderItemSeqId", this.getOrderItemSeqId());
        itemLookup.put("shipGroupSeqId", this.getShipGroupSeqId());
        itemLookup.put("inventoryItemId", this.getInventoryItemId());
        GenericValue plItem = EntityQuery.use(delegator).from("PicklistItem").where(itemLookup).queryOne();
        if (plItem != null) {
            Debug.logInfo("Found picklist bin: " + plItem, module);
            BigDecimal itemQty = plItem.getBigDecimal("quantity");
            if (itemQty.compareTo(quantity) == 0) {
                // set to complete
                itemLookup.put("itemStatusId", "PICKITEM_COMPLETED");
            } else {
                itemLookup.put("itemStatusId", "PICKITEM_CANCELLED");
            }
            itemLookup.put("userLogin", userLogin);
            Map<String, Object> itemUpdateResp = dispatcher.runSync("updatePicklistItem", itemLookup);
            if (ServiceUtil.isError(itemUpdateResp)) {
                throw new GeneralException(ServiceUtil.getErrorMessage(issueResp));
            }
        } else {
            Debug.logInfo("No item was found for lookup: " + itemLookup, module);
        }
    } else {
        Debug.logWarning("*** NO Picklist Bin ID set; cannot update picklist status!", module);
    }
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GeneralException(org.apache.ofbiz.base.util.GeneralException) Delegator(org.apache.ofbiz.entity.Delegator) HashMap(java.util.HashMap) BigDecimal(java.math.BigDecimal)

Aggregations

GenericValue (org.apache.ofbiz.entity.GenericValue)1422 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)871 Delegator (org.apache.ofbiz.entity.Delegator)721 Locale (java.util.Locale)505 HashMap (java.util.HashMap)463 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)370 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)356 BigDecimal (java.math.BigDecimal)338 LinkedList (java.util.LinkedList)312 Timestamp (java.sql.Timestamp)202 GeneralException (org.apache.ofbiz.base.util.GeneralException)168 Map (java.util.Map)155 IOException (java.io.IOException)116 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)97 HttpSession (javax.servlet.http.HttpSession)89 ArrayList (java.util.ArrayList)69 Security (org.apache.ofbiz.security.Security)69 EntityListIterator (org.apache.ofbiz.entity.util.EntityListIterator)59 List (java.util.List)56 EntityExpr (org.apache.ofbiz.entity.condition.EntityExpr)52