diff --git a/admin-dev/themes/template/products/quantities.tpl b/admin-dev/themes/template/products/quantities.tpl index 200da4841..8088d8c6d 100644 --- a/admin-dev/themes/template/products/quantities.tpl +++ b/admin-dev/themes/template/products/quantities.tpl @@ -42,7 +42,8 @@
{l s='This interface allows you to manage the available quantities for sale of the current product and its combinations on the current shop.'}
-{l s='You can manually specify the quantities for the product / each product combinations, or choose to automatically determine these quantities based on your stock.'}
+{l s='You can choose to use the advanced stock management system for this product or not.'}
+{l s='You can manually specify the quantities for the product / each product combinations, or choose to automatically determine these quantities based on your stock (if advanced stock management is activated).'}
{l s='In this case, the quantities correspond to the quantitites of the real stock in the warehouses associated to the current shop or current group of shops.'}
{l s='It is not possible to manage quantities when you are managing a pack.'}
+{l s='It is not possible to manage quantities when : '}
diff --git a/classes/Pack.php b/classes/Pack.php index f69f89380..1e8691fb8 100644 --- a/classes/Pack.php +++ b/classes/Pack.php @@ -96,7 +96,7 @@ class PackCore extends Product foreach ($items as $item) { // Updated for 1.5.0 - if (Product::getQuantity($item->id) < $item->pack_quantity || !$item->isAvailableWhenOutOfStock((int)$item->out_of_stock)) + if (Product::getQuantity($item->id) < $item->pack_quantity || (Product::getQuantity($item->id) < $item->pack_quantity && !$item->isAvailableWhenOutOfStock((int)$item->out_of_stock))) return false; } return true; diff --git a/classes/Product.php b/classes/Product.php index fd65248c4..445138ba5 100644 --- a/classes/Product.php +++ b/classes/Product.php @@ -2599,9 +2599,11 @@ class ProductCore extends ObjectModel public static function getQuantity($id_product, $id_product_attribute = null, $cache_is_pack = null) { $lang = Configuration::get('PS_LANG_DEFAULT'); - if (((int)$cache_is_pack || ($cache_is_pack === null && Pack::isPack((int)$id_product, (int)$lang))) - && !Pack::isInStock((int)$id_product, (int)$lang)) - return 0; + if ((int)$cache_is_pack || ($cache_is_pack === null && Pack::isPack((int)$id_product))) + { + if (!Pack::isInStock((int)$id_product)) + return 0; + } // @since 1.5.0 return (StockAvailable::getQuantityAvailableByProduct($id_product, $id_product_attribute)); diff --git a/classes/stock/StockAvailable.php b/classes/stock/StockAvailable.php index f8fb49f3e..f1b721609 100644 --- a/classes/stock/StockAvailable.php +++ b/classes/stock/StockAvailable.php @@ -90,7 +90,6 @@ class StockAvailableCore extends ObjectModel $query->where('id_product_attribute = '.(int)$id_product_attribute); $query = StockAvailable::addSqlShopRestriction($query, $id_shop); - return (int)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query); } @@ -259,19 +258,44 @@ class StockAvailableCore extends ObjectModel if (is_null($id_product_attribute)) $id_product_attribute = 0; - $query = new DbQuery(); - $query->select('SUM(quantity)'); - $query->from('stock_available'); + // if product is a pack + if (Pack::isPack($id_product)) + { + $items = Pack::getItems((int)$id_product, Configuration::get('PS_LANG_DEFAULT')); - // if null, it's a product without attributes - if (!is_null($id_product)) - $query->where('id_product = '.(int)$id_product); - - $query->where('id_product_attribute = '.(int)$id_product_attribute); - - $query = StockAvailable::addSqlShopRestriction($query, $id_shop); - - return (int)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query); + // gets an array of quantities (quantity for the product / quantity in pack) + $quantities = array(); + foreach ($items as $item) + if (!$item->isAvailableWhenOutOfStock((int)$item->out_of_stock)) + $quantities[] = Product::getQuantity($item->id) / ($item->pack_quantity !== 0 ? $item->pack_quantity : 1); + + // gets the minimum + $quantity = $quantities[0]; + foreach ($quantities as $value) + { + if ($quantity > $value) + $quantity = $value; + } + + // returns the number of pack available + return $quantity; + } + else // else + { + $query = new DbQuery(); + $query->select('SUM(quantity)'); + $query->from('stock_available'); + + // if null, it's a product without attributes + if (!is_null($id_product)) + $query->where('id_product = '.(int)$id_product); + + $query->where('id_product_attribute = '.(int)$id_product_attribute); + + $query = StockAvailable::addSqlShopRestriction($query, $id_shop); + + return (int)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query); + } } /**