Author: ashish
Date: Sat Oct 18 10:43:59 2014 New Revision: 1632750 URL: http://svn.apache.org/r1632750 Log: Applied patch from jira issue - OFBIZ-4528 - Out of stock products screw up the pagination during category browsing. =================================================================== Pagination is handled in getProductCategoryAndLimitedMembers Then the out of stock filtering is done in CategoryDetail.groovy. Hence the pagination is screwed up. Certain pages might show less records or no records based upon data condition. =================================================================== Thanks Arun for the contribution and Thanks Kiran for creating the issue. Modified: ofbiz/trunk/applications/order/webapp/ordermgr/WEB-INF/actions/entry/catalog/CategoryDetail.groovy ofbiz/trunk/applications/order/webapp/ordermgr/entry/catalog/categorydetail.ftl ofbiz/trunk/applications/product/servicedef/services_view.xml ofbiz/trunk/applications/product/src/org/ofbiz/product/category/CategoryServices.java ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductWorker.java Modified: ofbiz/trunk/applications/order/webapp/ordermgr/WEB-INF/actions/entry/catalog/CategoryDetail.groovy URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/webapp/ordermgr/WEB-INF/actions/entry/catalog/CategoryDetail.groovy?rev=1632750&r1=1632749&r2=1632750&view=diff ============================================================================== --- ofbiz/trunk/applications/order/webapp/ordermgr/WEB-INF/actions/entry/catalog/CategoryDetail.groovy (original) +++ ofbiz/trunk/applications/order/webapp/ordermgr/WEB-INF/actions/entry/catalog/CategoryDetail.groovy Sat Oct 18 10:43:59 2014 @@ -54,52 +54,20 @@ andMap = [productCategoryId : productCat limitView : limitView]; andMap.put("prodCatalogId", currentCatalogId); andMap.put("checkViewAllow", true); +// Prevents out of stock product to be displayed on site +productStore = ProductStoreWorker.getProductStore(request); +if (productStore) { + andMap.put("productStoreId", productStore.productStoreId); +} if (context.orderByFields) { andMap.put("orderByFields", context.orderByFields); } else { andMap.put("orderByFields", ["sequenceNum", "productId"]); } catResult = dispatcher.runSync("getProductCategoryAndLimitedMembers", andMap); - productCategory = catResult.productCategory; productCategoryMembers = catResult.productCategoryMembers; - -// Prevents out of stock product to be displayed on site -productStore = ProductStoreWorker.getProductStore(request); -if(productStore) { - if("N".equals(productStore.showOutOfStockProducts)) { - productsInStock = []; - productCategoryMembers.each { productCategoryMember -> - product = delegator.findOne("Product", [productId : productCategoryMember.productId], true); - boolean isMarketingPackage = EntityTypeUtil.hasParentType(delegator, "ProductType", "productTypeId", product.productTypeId, "parentTypeId", "MARKETING_PKG"); - context.isMarketingPackage = (isMarketingPackage? "true": "false"); - if (isMarketingPackage) { - resultOutput = dispatcher.runSync("getMktgPackagesAvailable", [productId : productCategoryMember.productId]); - availableInventory = resultOutput.availableToPromiseTotal; - if(availableInventory > 0) { - productsInStock.add(productCategoryMember); - } - } else { - facilities = delegator.findList("ProductFacility", EntityCondition.makeCondition([productId : productCategoryMember.productId]), null, null, null, false); - availableInventory = 0.0; - if (facilities) { - facilities.each { facility -> - lastInventoryCount = facility.lastInventoryCount; - if (lastInventoryCount != null) { - availableInventory += lastInventoryCount; - } - } - if (availableInventory > 0) { - productsInStock.add(productCategoryMember); - } - } - } - } - context.productCategoryMembers = productsInStock; - } else { - context.productCategoryMembers = productCategoryMembers; - } -} +context.productCategoryMembers = productCategoryMembers; context.productCategory = productCategory; context.viewIndex = catResult.viewIndex; context.viewSize = catResult.viewSize; Modified: ofbiz/trunk/applications/order/webapp/ordermgr/entry/catalog/categorydetail.ftl URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/webapp/ordermgr/entry/catalog/categorydetail.ftl?rev=1632750&r1=1632749&r2=1632750&view=diff ============================================================================== --- ofbiz/trunk/applications/order/webapp/ordermgr/entry/catalog/categorydetail.ftl (original) +++ ofbiz/trunk/applications/order/webapp/ordermgr/entry/catalog/categorydetail.ftl Sat Oct 18 10:43:59 2014 @@ -41,14 +41,14 @@ under the License. </script> <#macro paginationControls> - <#assign viewIndexMax = Static["java.lang.Math"].ceil((listSize - 1)?double / viewSize?double)> + <#assign viewIndexMax = Static["java.lang.Math"].ceil((listSize)?double / viewSize?double)> <#if (viewIndexMax?int > 0)> <div class="product-prevnext"> <select name="pageSelect" onchange="callDocumentByPaginate(this[this.selectedIndex].value);"> <option value="#">${uiLabelMap.CommonPage} ${viewIndex?int + 1} ${uiLabelMap.CommonOf} ${viewIndexMax}</option> <#if (viewIndex?int > 1)> - <#list 0..viewIndexMax as curViewNum> - <option value="${productCategoryId}~${viewSize}~${curViewNum?int}">${uiLabelMap.CommonGotoPage} ${curViewNum + 1}</option> + <#list 1..viewIndexMax as curViewNum> + <option value="${productCategoryId}~${viewSize}~${curViewNum-1?int}">${uiLabelMap.CommonGotoPage} ${curViewNum}</option> </#list> </#if> </select> Modified: ofbiz/trunk/applications/product/servicedef/services_view.xml URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/servicedef/services_view.xml?rev=1632750&r1=1632749&r2=1632750&view=diff ============================================================================== --- ofbiz/trunk/applications/product/servicedef/services_view.xml (original) +++ ofbiz/trunk/applications/product/servicedef/services_view.xml Sat Oct 18 10:43:59 2014 @@ -152,6 +152,7 @@ under the License. <attribute name="introductionDateLimit" type="java.sql.Timestamp" mode="IN" optional="true"/> <attribute name="releaseDateLimit" type="java.sql.Timestamp" mode="IN" optional="true"/> <attribute name="orderByFields" type="List" mode="IN" optional="true"/> + <attribute name="productStoreId" mode="IN" type="String" optional="true"/> <attribute name="productCategory" type="org.ofbiz.entity.GenericValue" mode="OUT" optional="true"/> <attribute name="productCategoryMembers" type="java.util.Collection" mode="OUT" optional="true"/> <!-- this list will only contain the limited members if limitView=true --> <attribute name="viewIndex" type="Integer" mode="OUT" optional="false"/> <!-- this is a 1 based index, ie the first results are in index 1 --> Modified: ofbiz/trunk/applications/product/src/org/ofbiz/product/category/CategoryServices.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/src/org/ofbiz/product/category/CategoryServices.java?rev=1632750&r1=1632749&r2=1632750&view=diff ============================================================================== --- ofbiz/trunk/applications/product/src/org/ofbiz/product/category/CategoryServices.java (original) +++ ofbiz/trunk/applications/product/src/org/ofbiz/product/category/CategoryServices.java Sat Oct 18 10:43:59 2014 @@ -21,7 +21,9 @@ package org.ofbiz.product.category; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.io.Writer; +import java.math.BigDecimal; import java.sql.Timestamp; +import java.util.ArrayList; import java.util.List; import java.util.Locale; import java.util.Map; @@ -34,6 +36,7 @@ import javolution.util.FastMap; import net.sf.json.JSONObject; import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.GeneralException; import org.ofbiz.base.util.UtilDateTime; import org.ofbiz.base.util.UtilGenerics; import org.ofbiz.base.util.UtilMisc; @@ -47,9 +50,13 @@ import org.ofbiz.entity.condition.Entity import org.ofbiz.entity.model.ModelEntity; import org.ofbiz.entity.util.EntityFindOptions; import org.ofbiz.entity.util.EntityListIterator; +import org.ofbiz.entity.util.EntityTypeUtil; import org.ofbiz.entity.util.EntityUtil; import org.ofbiz.product.catalog.CatalogWorker; +import org.ofbiz.product.product.ProductWorker; import org.ofbiz.service.DispatchContext; +import org.ofbiz.service.GenericServiceException; +import org.ofbiz.service.LocalDispatcher; import org.ofbiz.service.ServiceUtil; /** @@ -211,6 +218,7 @@ public class CategoryServices { public static Map<String, Object> getProductCategoryAndLimitedMembers(DispatchContext dctx, Map<String, ? extends Object> context) { Delegator delegator = dctx.getDelegator(); + LocalDispatcher dispatcher = dctx.getDispatcher(); String productCategoryId = (String) context.get("productCategoryId"); boolean limitView = ((Boolean) context.get("limitView")).booleanValue(); int defaultViewSize = ((Integer) context.get("defaultViewSize")).intValue(); @@ -236,7 +244,7 @@ public class CategoryServices { } Timestamp nowTimestamp = UtilDateTime.nowTimestamp(); - + int viewIndex = 0; try { viewIndex = Integer.valueOf((String) context.get("viewIndexString")).intValue(); @@ -271,7 +279,18 @@ public class CategoryServices { lowIndex = 0; highIndex = 0; } - + Boolean filterOutOfStock = false ; + try { + String productStoreId = (String) context.get("productStoreId"); + if (UtilValidate.isNotEmpty(productStoreId)) { + GenericValue productStore = delegator.findOne("ProductStore", UtilMisc.toMap("productStoreId", productStoreId), false); + if (productStore != null && "N".equals(productStore.getString("showOutOfStockProducts"))) { + filterOutOfStock = true; + } + } + } catch (GenericEntityException e) { + Debug.logWarning(e.getMessage(), module); + } List<GenericValue> productCategoryMembers = null; if (productCategory != null) { try { @@ -292,7 +311,16 @@ public class CategoryServices { if (!filterConditions.isEmpty()) { productCategoryMembers = EntityUtil.filterByCondition(productCategoryMembers, EntityCondition.makeCondition(filterConditions, EntityOperator.AND)); } - + + // filter out of stock products + if (filterOutOfStock) { + try { + productCategoryMembers = ProductWorker.filterOutOfStockProducts(productCategoryMembers, dispatcher, delegator); + } catch (GeneralException e) { + Debug.logWarning("Problem filtering out of stock products :"+e.getMessage(), module); + } + + } // filter out the view allow before getting the sublist if (UtilValidate.isNotEmpty(viewProductCategoryId)) { productCategoryMembers = CategoryWorker.filterProductsInCategory(delegator, productCategoryMembers, viewProductCategoryId); @@ -370,7 +398,15 @@ public class CategoryServices { lowIndex = 1; highIndex = listSize; } - + // filter out of stock products + if (filterOutOfStock) { + try { + productCategoryMembers = ProductWorker.filterOutOfStockProducts(productCategoryMembers, dispatcher, delegator); + listSize = productCategoryMembers.size(); + } catch (GeneralException e) { + Debug.logWarning("Problem filtering out of stock products :"+e.getMessage(), module); + } + } // null safety if (productCategoryMembers == null) { productCategoryMembers = FastList.newInstance(); @@ -398,7 +434,7 @@ public class CategoryServices { if (productCategoryMembers != null) result.put("productCategoryMembers", productCategoryMembers); return result; } - + // Please note : the structure of map in this function is according to the JSON data map of the jsTree @SuppressWarnings("unchecked") public static void getChildCategoryTree(HttpServletRequest request, HttpServletResponse response){ Modified: ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductWorker.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductWorker.java?rev=1632750&r1=1632749&r2=1632750&view=diff ============================================================================== --- ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductWorker.java (original) +++ ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductWorker.java Sat Oct 18 10:43:59 2014 @@ -21,6 +21,7 @@ package org.ofbiz.product.product; import java.math.BigDecimal; import java.math.MathContext; import java.sql.Timestamp; +import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; @@ -32,6 +33,7 @@ import javolution.util.FastMap; import javolution.util.FastSet; import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.GeneralException; import org.ofbiz.base.util.UtilDateTime; import org.ofbiz.base.util.UtilMisc; import org.ofbiz.base.util.UtilValidate; @@ -1205,4 +1207,43 @@ nextProd: return false; } + + // Method to filter-out out of stock products + public static List<GenericValue> filterOutOfStockProducts (List<GenericValue> productsToFilter, LocalDispatcher dispatcher, Delegator delegator) throws GeneralException { + ArrayList<GenericValue> productsInStock = new ArrayList<GenericValue>(); + if (UtilValidate.isNotEmpty(productsToFilter)) { + for (GenericValue genericRecord : productsToFilter) { + String productId = genericRecord.getString("productId"); + GenericValue product = null; + product = delegator.findOne("Product", UtilMisc.toMap("productId", productId), true); + Boolean isMarketingPackage = EntityTypeUtil.hasParentType(delegator, "ProductType", "productTypeId", product.getString("productTypeId"), "parentTypeId", "MARKETING_PKG"); + + if ( UtilValidate.isNotEmpty(isMarketingPackage) && isMarketingPackage) { + Map<String, Object> resultOutput = new FastMap<String, Object>(); + resultOutput = dispatcher.runSync("getMktgPackagesAvailable", UtilMisc.toMap("productId" ,productId)); + Debug.logWarning("Error getting available marketing package.", module); + + BigDecimal availableInventory = (BigDecimal) resultOutput.get("availableToPromiseTotal"); + if(availableInventory.compareTo(BigDecimal.ZERO) > 0) { + productsInStock.add(genericRecord); + } + } else { + List<GenericValue> facilities = delegator.findList("ProductFacility", EntityCondition.makeCondition("productId",EntityOperator.EQUALS, productId), null, null, null, false); + 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); + } + } + } + } + } + return productsInStock; + } } |
Free forum by Nabble | Edit this page |