This is an automated email from the ASF dual-hosted git repository.

gsperi pushed a commit to branch release22.01
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git


The following commit(s) were added to refs/heads/release22.01 by this push:
     new ed5e12dda6 Fixed: ecomm. listing ProductFacility out of stock issue 
(OFBIZ-12359)
ed5e12dda6 is described below

commit ed5e12dda62de5c5e6191fb6f8504ac99f105e5c
Author: Giulio Speri <giulio.sp...@mpstyle.it>
AuthorDate: Sun Apr 17 12:32:06 2022 +0200

    Fixed: ecomm. listing ProductFacility out of stock issue (OFBIZ-12359)
    
    In method filterOutOfStockProducts added case for "virtual" products.
    In case a product isVirtual (= Y), then the available inventory count
    (lastInventoryCount) is summed up on all its product variants, across
    all the ProductFacility records, and then used to determine if the
    (parent/virtual) product is in stock or out of stock.
    
    This fix does not check if the facilityId of the ProductFacility
    records retrieved is associated and enabled to the ProductStore.
    
    Thanks Nicola Mazzoni for helping in anlyzing this issue.
---
 .../ofbiz/product/product/ProductWorker.java       | 54 ++++++++++++++++++----
 1 file changed, 45 insertions(+), 9 deletions(-)

diff --git 
a/applications/product/src/main/java/org/apache/ofbiz/product/product/ProductWorker.java
 
b/applications/product/src/main/java/org/apache/ofbiz/product/product/ProductWorker.java
index 36d3a441d1..f0d74458c6 100644
--- 
a/applications/product/src/main/java/org/apache/ofbiz/product/product/ProductWorker.java
+++ 
b/applications/product/src/main/java/org/apache/ofbiz/product/product/ProductWorker.java
@@ -34,6 +34,7 @@ import java.util.Set;
 import org.apache.ofbiz.base.util.Debug;
 import org.apache.ofbiz.base.util.GeneralException;
 import org.apache.ofbiz.base.util.UtilDateTime;
+import org.apache.ofbiz.base.util.UtilGenerics;
 import org.apache.ofbiz.base.util.UtilMisc;
 import org.apache.ofbiz.base.util.UtilValidate;
 import org.apache.ofbiz.common.geo.GeoWorker;
@@ -50,6 +51,7 @@ import 
org.apache.ofbiz.product.config.ProductConfigWrapper.ConfigOption;
 import org.apache.ofbiz.service.GenericServiceException;
 import org.apache.ofbiz.service.LocalDispatcher;
 import org.apache.ofbiz.service.ModelService;
+import org.apache.ofbiz.service.ServiceUtil;
 
 /**
  * Product Worker class to reduce code in JSPs.
@@ -1317,17 +1319,51 @@ public final class ProductWorker {
                         productsInStock.add(genericRecord);
                     }
                 } else {
-                    List<GenericValue> facilities = 
EntityQuery.use(delegator).from("ProductFacility").where("productId", 
productId).queryList();
-                    BigDecimal availableInventory = BigDecimal.ZERO;
-                    if (UtilValidate.isNotEmpty(facilities)) {
-                        for (GenericValue facility : facilities) {
-                            BigDecimal lastInventoryCount = 
facility.getBigDecimal("lastInventoryCount");
-                            if (lastInventoryCount != null) {
-                                availableInventory = 
lastInventoryCount.add(availableInventory);
+                    if ("Y".equals(product.getString("isVirtual"))) {
+                        BigDecimal availableInventory = BigDecimal.ZERO;
+                        try {
+                            Map<String, Object> variantResultOutput = 
dispatcher.runSync("getAllProductVariants",
+                                    UtilMisc.toMap("productId", productId));
+                            if (ServiceUtil.isError(variantResultOutput)) {
+                                Debug.logError("Error in retrieving all 
product variants for productId " + productId
+                                        + " Skip this product. Error is : " + 
ServiceUtil.getErrorMessage(variantResultOutput), MODULE);
+                                continue;
                             }
+                            List<GenericValue> productVariants = 
UtilGenerics.cast(variantResultOutput.get("assocProducts"));
+                            for (GenericValue productVariant : 
productVariants) {
+                                List<GenericValue> facilities = 
EntityQuery.use(delegator)
+                                        .from("ProductFacility")
+                                        .where("productId", 
productVariant.getString("productIdTo"))
+                                        .queryList();
+                                if (UtilValidate.isNotEmpty(facilities)) {
+                                    for (GenericValue facility : facilities) {
+                                        BigDecimal lastInventoryCount = 
facility.getBigDecimal("lastInventoryCount");
+                                        if (lastInventoryCount != null) {
+                                            availableInventory = 
lastInventoryCount.add(availableInventory);
+                                        }
+                                    }
+                                }
+                            }
+                            if (availableInventory.compareTo(BigDecimal.ZERO) 
> 0) {
+                                productsInStock.add(genericRecord);
+                            }
+                        } catch (GenericServiceException e) {
+                            Debug.logError(e, "Error getting all product 
variants for productId " + productId
+                                    + ", while filtering out of stock 
products.", MODULE);
                         }
-                        if (availableInventory.compareTo(BigDecimal.ZERO) > 0) 
{
-                            productsInStock.add(genericRecord);
+                    } else {
+                        List<GenericValue> facilities = 
EntityQuery.use(delegator).from("ProductFacility").where("productId", 
productId).queryList();
+                        BigDecimal availableInventory = BigDecimal.ZERO;
+                        if (UtilValidate.isNotEmpty(facilities)) {
+                            for (GenericValue facility : facilities) {
+                                BigDecimal lastInventoryCount = 
facility.getBigDecimal("lastInventoryCount");
+                                if (lastInventoryCount != null) {
+                                    availableInventory = 
lastInventoryCount.add(availableInventory);
+                                }
+                            }
+                            if (availableInventory.compareTo(BigDecimal.ZERO) 
> 0) {
+                                productsInStock.add(genericRecord);
+                            }
                         }
                     }
                 }

Reply via email to