diff --git a/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartEvents.java b/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartEvents.java index 94279abe1d..f1192580cc 100644 --- a/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartEvents.java +++ b/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartEvents.java @@ -549,6 +549,21 @@ public class ShoppingCartEvents { Debug.logError(e.getMessage(), module); } } + + // check for alternative packing + if(ProductWorker.isAlternativePacking(delegator, productId , parentProductId)){ + GenericValue parentProduct = null; + try { + parentProduct = delegator.findByPrimaryKey("Product", UtilMisc.toMap("productId", parentProductId)); + } catch (GenericEntityException e) { + Debug.logError(e, "Error getting parent product", module); + } + BigDecimal piecesIncluded = BigDecimal.ZERO; + if(parentProduct != null){ + piecesIncluded = new BigDecimal(parentProduct.getLong("piecesIncluded")); + quantity = quantity.multiply(piecesIncluded); + } + } // Translate the parameters and add to the cart result = cartHelper.addToCart(catalogId, shoppingListId, shoppingListItemSeqId, productId, productCategoryId, diff --git a/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartItem.java b/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartItem.java index 1bfc7e065c..4c91ce2c21 100644 --- a/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartItem.java +++ b/applications/order/src/org/ofbiz/order/shoppingcart/ShoppingCartItem.java @@ -20,6 +20,7 @@ package org.ofbiz.order.shoppingcart; import java.math.BigDecimal; import java.math.MathContext; +import java.math.RoundingMode; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Arrays; @@ -1112,9 +1113,21 @@ public class ShoppingCartItem implements java.io.Serializable { if (partyId != null) { priceContext.put("partyId", partyId); } + // check alternative packaging + boolean isAlternativePacking = ProductWorker.isAlternativePacking(delegator, this.productId , this.getParentProductId()); + BigDecimal pieces = BigDecimal.ONE; + if(isAlternativePacking){ + GenericValue originalProduct = this.getParentProduct(); + if (originalProduct != null) pieces = new BigDecimal(originalProduct.getLong("piecesIncluded")); + priceContext.put("product", originalProduct); + this._parentProduct = null; + }else{ + priceContext.put("product", this.getProduct()); + } + priceContext.put("quantity", this.getQuantity()); priceContext.put("amount", this.getSelectedAmount()); - priceContext.put("product", this.getProduct()); + if (cart.getOrderType().equals("PURCHASE_ORDER")) { Map priceResult = dispatcher.runSync("calculatePurchasePrice", priceContext); if (ServiceUtil.isError(priceResult)) { @@ -1124,8 +1137,13 @@ public class ShoppingCartItem implements java.io.Serializable { if (!validPriceFound.booleanValue()) { throw new CartItemModifyException("Could not find a valid price for the product with ID [" + this.getProductId() + "] and supplier with ID [" + partyId + "], not adding to cart."); } - - this.setBasePrice(((BigDecimal) priceResult.get("price"))); + + if(isAlternativePacking){ + this.setBasePrice(((BigDecimal) priceResult.get("price")).divide(pieces, RoundingMode.HALF_UP)); + }else{ + this.setBasePrice(((BigDecimal) priceResult.get("price"))); + } + this.setDisplayPrice(this.basePrice); this.orderItemPriceInfos = (List) priceResult.get("orderItemPriceInfos"); } else { @@ -1169,21 +1187,41 @@ public class ShoppingCartItem implements java.io.Serializable { if (Boolean.FALSE.equals(validPriceFound)) { throw new CartItemModifyException("Could not find a valid price for the product with ID [" + this.getProductId() + "], not adding to cart."); } + + //set alternative product price + if(isAlternativePacking){ + int decimals = 2; + if (priceResult.get("listPrice") != null) { + this.listPrice = ((BigDecimal) priceResult.get("listPrice")).divide(pieces, decimals, RoundingMode.HALF_UP); + } - if (priceResult.get("listPrice") != null) { - this.listPrice = ((BigDecimal) priceResult.get("listPrice")); + if (priceResult.get("basePrice") != null) { + this.setBasePrice(((BigDecimal) priceResult.get("basePrice")).divide(pieces, decimals, RoundingMode.HALF_UP)); + } + + if (priceResult.get("price") != null) { + this.setDisplayPrice(((BigDecimal) priceResult.get("price")).divide(pieces, decimals, RoundingMode.HALF_UP)); + } + + if (priceResult.get("specialPromoPrice") != null) { + this.setSpecialPromoPrice(((BigDecimal) priceResult.get("specialPromoPrice")).divide(pieces, decimals, RoundingMode.HALF_UP)); + } + }else{ + if (priceResult.get("listPrice") != null) { + this.listPrice = ((BigDecimal) priceResult.get("listPrice")); + } + + if (priceResult.get("basePrice") != null) { + this.setBasePrice(((BigDecimal) priceResult.get("basePrice"))); + } + + if (priceResult.get("price") != null) { + this.setDisplayPrice(((BigDecimal) priceResult.get("price"))); + } + + this.setSpecialPromoPrice((BigDecimal) priceResult.get("specialPromoPrice")); } - - if (priceResult.get("basePrice") != null) { - this.setBasePrice(((BigDecimal) priceResult.get("basePrice"))); - } - - if (priceResult.get("price") != null) { - this.setDisplayPrice(((BigDecimal) priceResult.get("price"))); - } - - this.setSpecialPromoPrice((BigDecimal) priceResult.get("specialPromoPrice")); - + this.orderItemPriceInfos = (List) priceResult.get("orderItemPriceInfos"); // If product is configurable, the price is taken from the configWrapper. diff --git a/applications/order/webapp/ordermgr/WEB-INF/actions/entry/catalog/Product.groovy b/applications/order/webapp/ordermgr/WEB-INF/actions/entry/catalog/Product.groovy index ad893c3196..b1afb463a9 100644 --- a/applications/order/webapp/ordermgr/WEB-INF/actions/entry/catalog/Product.groovy +++ b/applications/order/webapp/ordermgr/WEB-INF/actions/entry/catalog/Product.groovy @@ -26,6 +26,7 @@ import org.ofbiz.product.catalog.*; import org.ofbiz.product.category.*; import org.ofbiz.product.product.ProductWorker; import org.ofbiz.product.product.ProductContentWrapper; +import org.ofbiz.entity.util.EntityUtil; contentPathPrefix = CatalogWorker.getContentPathPrefix(request); catalogName = CatalogWorker.getCatalogName(request); @@ -54,6 +55,16 @@ if (productId) { if (productId) { product = delegator.findByPrimaryKeyCache("Product", [productId : productId]); + // first make sure this isn't a virtual-variant that has an associated virtual product, if it does show that instead of the variant + if("Y".equals(product.isVirtual) && "Y".equals(product.isVariant)){ + virtualVariantProductAssocs = delegator.findByAndCache("ProductAssoc", ["productId": productId, "productAssocTypeId": "ALTERNATIVE_PACKAGE"], ["-fromDate"]); + virtualVariantProductAssocs = EntityUtil.filterByDate(virtualVariantProductAssocs); + if (virtualVariantProductAssocs) { + productAssoc = EntityUtil.getFirst(virtualVariantProductAssocs); + product = productAssoc.getRelatedOneCache("AssocProduct"); + } + } + // first make sure this isn't a variant that has an associated virtual product, if it does show that instead of the variant virtualProductId = ProductWorker.getVariantVirtualId(product); if (virtualProductId) { diff --git a/applications/order/webapp/ordermgr/WEB-INF/actions/entry/catalog/ProductDetail.groovy b/applications/order/webapp/ordermgr/WEB-INF/actions/entry/catalog/ProductDetail.groovy index 226bfc8f90..0282268d10 100644 --- a/applications/order/webapp/ordermgr/WEB-INF/actions/entry/catalog/ProductDetail.groovy +++ b/applications/order/webapp/ordermgr/WEB-INF/actions/entry/catalog/ProductDetail.groovy @@ -417,6 +417,50 @@ if (product) { if (variantPriceMap && variantPriceMap.basePrice) { variantPriceJS.append(" if (sku == \"" + variant.productId + "\") return \"" + numberFormat.format(variantPriceMap.basePrice) + "\"; "); } + + // make a list of virtual variants sku with requireAmount + virtualVariantsRes = dispatcher.runSync("getAssociatedProducts", [productIdTo : variant.productId, type : "ALTERNATIVE_PACKAGE", checkViewAllow : true, prodCatalogId : currentCatalogId]); + virtualVariants = virtualVariantsRes.assocProducts; + + if(virtualVariants){ + virtualVariants.each { virtualAssoc -> + virtual = virtualAssoc.getRelatedOne("MainProduct"); + // Get price from a virtual product + priceContext.product = virtual; + if (cart.isSalesOrder()) { + // sales order: run the "calculateProductPrice" service + virtualPriceMap = dispatcher.runSync("calculateProductPrice", priceContext); + BigDecimal calculatedPrice = (BigDecimal)virtualPriceMap.get("price"); + // Get the minimum quantity for variants if MINIMUM_ORDER_PRICE is set for variants. + virtualPriceMap.put("minimumQuantity", ShoppingCart.getMinimumOrderQuantity(delegator, calculatedPrice, virtual.get("productId"))); + Iterator treeMapIter = variantTree.entrySet().iterator(); + while (treeMapIter.hasNext()) { + Map.Entry entry = treeMapIter.next(); + if (entry.getValue() instanceof Map) { + Iterator entryIter = entry.getValue().entrySet().iterator(); + while (entryIter.hasNext()) { + Map.Entry innerentry = entryIter.next(); + if (virtual.get("productId").equals(innerentry.getValue().get(0))) { + virtualPriceMap.put("variantName", innerentry.getKey()); + virtualPriceMap.put("secondVariantName", entry.getKey()); + break; + } + } + } else if (UtilValidate.isNotEmpty(entry.getValue())) { + if (virtual.get("productId").equals(entry.getValue().get(0))) { + virtualPriceMap.put("variantName", entry.getKey()); + break; + } + } + } + variantPriceList.add(virtualPriceMap); + } else { + virtualPriceMap = dispatcher.runSync("calculatePurchasePrice", priceContext); + } + variantPriceJS.append(" if (sku == \"" + virtual.productId + "\") return \"" + numberFormat.format(virtualPriceMap.basePrice) + "\"; "); + } + + } } amt.append(" } "); variantPriceJS.append(" } "); diff --git a/applications/order/webapp/ordermgr/WEB-INF/actions/entry/catalog/ProductUomDropDownOnly.groovy b/applications/order/webapp/ordermgr/WEB-INF/actions/entry/catalog/ProductUomDropDownOnly.groovy new file mode 100644 index 0000000000..e0ed2d8d6b --- /dev/null +++ b/applications/order/webapp/ordermgr/WEB-INF/actions/entry/catalog/ProductUomDropDownOnly.groovy @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.ofbiz.base.util.*; + +product = delegator.findByPrimaryKey("Product", UtilMisc.toMap("productId", parameters.productId)); +if (product) { + productVirtualVariants = delegator.findByAndCache("ProductAssoc", UtilMisc.toMap("productIdTo", product.productId , "productAssocTypeId", "ALTERNATIVE_PACKAGE")); + if(productVirtualVariants){ + def mainProducts = []; + productVirtualVariants.each { virtualVariantKey -> + mainProductMap = [:]; + mainProduct = virtualVariantKey.getRelatedOneCache("MainProduct"); + quantityUom = mainProduct.getRelatedOneCache("QuantityUom"); + mainProductMap.productId = mainProduct.productId; + mainProductMap.piecesIncluded = mainProduct.piecesIncluded; + mainProductMap.uomDesc = quantityUom.description; + mainProducts.add(mainProductMap); + } + context.mainProducts = mainProducts; + } + context.product = product; +} \ No newline at end of file diff --git a/applications/order/webapp/ordermgr/WEB-INF/controller.xml b/applications/order/webapp/ordermgr/WEB-INF/controller.xml index e03f25b0b8..a042bb454e 100644 --- a/applications/order/webapp/ordermgr/WEB-INF/controller.xml +++ b/applications/order/webapp/ordermgr/WEB-INF/controller.xml @@ -1725,7 +1725,11 @@ under the License. - + + + + + diff --git a/applications/order/webapp/ordermgr/entry/catalog/ProductUomDropDownOnly.ftl b/applications/order/webapp/ordermgr/entry/catalog/ProductUomDropDownOnly.ftl new file mode 100644 index 0000000000..aa64e4a80f --- /dev/null +++ b/applications/order/webapp/ordermgr/entry/catalog/ProductUomDropDownOnly.ftl @@ -0,0 +1,26 @@ +<#-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--> +<#if product?exists && mainProducts?exists> + + \ No newline at end of file diff --git a/applications/order/webapp/ordermgr/entry/catalog/productdetail.ftl b/applications/order/webapp/ordermgr/entry/catalog/productdetail.ftl index 928c51bafb..e3e1ad2903 100644 --- a/applications/order/webapp/ordermgr/entry/catalog/productdetail.ftl +++ b/applications/order/webapp/ordermgr/entry/catalog/productdetail.ftl @@ -171,6 +171,9 @@ ${virtualJavaScript?if_exists} // using the selected index locate the sku var sku = document.forms["addform"].elements[name].options[indexSelected].value; + // display alternative packaging dropdown + ajaxUpdateArea("product_uom", "<@ofbizUrl>ProductUomDropDownOnly", "productId=" + sku); + // set the product ID setAddProductId(sku); @@ -259,6 +262,18 @@ ${virtualJavaScript?if_exists} block2.style.display = "none"; } + + function displayProductVirtualVariantId(variantId) { + document.addform.product_id.value = variantId; + var elem = document.getElementById('product_id_display'); + var txt = document.createTextNode(variantId); + if(elem.hasChildNodes()) { + elem.replaceChild(txt, elem.firstChild); + } else { + elem.appendChild(txt); + } + setVariantPrice(variantId); + } //--> @@ -454,6 +469,7 @@ ${virtualJavaScript?if_exists} +
diff --git a/applications/order/widget/ordermgr/OrderEntryCatalogScreens.xml b/applications/order/widget/ordermgr/OrderEntryCatalogScreens.xml index 8b25caa15e..83cbf3fb91 100644 --- a/applications/order/widget/ordermgr/OrderEntryCatalogScreens.xml +++ b/applications/order/widget/ordermgr/OrderEntryCatalogScreens.xml @@ -291,4 +291,24 @@ under the License. + + +
+ + @@ -505,6 +520,8 @@ ${virtualJavaScript?if_exists}
+ +