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 ed6569fa7e..bf0e6d1319 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; @@ -49,6 +50,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. @@ -1248,17 +1250,51 @@ nextProd: productsInStock.add(genericRecord); } } else { - List 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 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 productVariants = UtilGenerics.cast(variantResultOutput.get("assocProducts")); + for (GenericValue productVariant : productVariants) { + List 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 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); + } } } }