From 6fddfce5e7816db0737d766c9e0c7d230e8083db Mon Sep 17 00:00:00 2001 From: dSevere Date: Fri, 2 Dec 2011 15:23:01 +0000 Subject: [PATCH] // StockManager : bugs fix + improvement of StockManager utilisation in fucntion of global configuration --- classes/Cart.php | 61 +++++++++++-------- classes/Product.php | 17 +++--- classes/order/OrderHistory.php | 5 +- classes/stock/StockManager.php | 4 +- controllers/admin/AdminProductsController.php | 5 -- modules/mailalerts/mailalerts.php | 2 +- 6 files changed, 53 insertions(+), 41 deletions(-) diff --git a/classes/Cart.php b/classes/Cart.php index c99cdd588..6d714010d 100644 --- a/classes/Cart.php +++ b/classes/Cart.php @@ -1359,6 +1359,8 @@ class CartCore extends ObjectModel $warehouse_count_by_address = array(); $warehouse_carrier_list = array(); + $stock_management_active = Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT'); + foreach ($product_list as &$product) { if ((int)$product['id_address_delivery'] == 0) @@ -1369,32 +1371,41 @@ class CartCore extends ObjectModel $product['warehouse_list'] = array(); - $warehouse_list = Warehouse::getProductWarehouseList($product['id_product'], $product['id_product_attribute']); - // Does the product is in stock ? - // If yes, get only warehouse where the product is in stock - $warehouse_in_stock = array(); - $manager = StockManagerFactory::getManager(); - - foreach ($warehouse_list as $key => $warehouse) + if ($stock_management_active) { - $product_real_quantities = $manager->getProductRealQuantities( - $product['id_product'], - $product['id_product_attribute'], - array($warehouse['id_warehouse']), - true - ); + $warehouse_list = Warehouse::getProductWarehouseList($product['id_product'], $product['id_product_attribute']); + // Does the product is in stock ? + // If yes, get only warehouse where the product is in stock + $warehouse_in_stock = array(); + $manager = StockManagerFactory::getManager(); - if ($product_real_quantities > 0) - $warehouse_in_stock[] = $warehouse; - } + foreach ($warehouse_list as $key => $warehouse) + { + $product_real_quantities = $manager->getProductRealQuantities( + $product['id_product'], + $product['id_product_attribute'], + array($warehouse['id_warehouse']), + true + ); - if (!empty($warehouse_in_stock)) - { - $warehouse = $warehouse_in_stock; - $product['in_stock'] = true; + if ($product_real_quantities > 0) + $warehouse_in_stock[] = $warehouse; + } + + if (!empty($warehouse_in_stock)) + { + $warehouse_list = $warehouse_in_stock; + $product['in_stock'] = true; + } + else + $product['in_stock'] = false; } else - $product['in_stock'] = false; + { + //simulate default warehouse + $warehouse_list = array(0); + $product['in_stock'] = StockAvailable::getQuantityAvailableByProduct($product['id_product'], $product['id_product_attribute']) > 0; + } foreach ($warehouse_list as $warehouse) { @@ -1901,7 +1912,7 @@ class CartCore extends ObjectModel /** * Return shipping total of a specific carriers for the cart * - * @param int $id_carrier + * @param int $id_carrier * @param array $delivery_option Array of the delivery option for each address * @param booleal $useTax * @param Country $default_country @@ -1914,8 +1925,8 @@ class CartCore extends ObjectModel $total_shipping = 0; $delivery_option_list = $this->getDeliveryOptionList(); - - + + foreach ($delivery_option as $id_address => $key) { if (!isset($delivery_option_list[$id_address]) || !isset($delivery_option_list[$id_address][$key])) @@ -1931,7 +1942,7 @@ class CartCore extends ObjectModel return $total_shipping; } - + /** * Return shipping total diff --git a/classes/Product.php b/classes/Product.php index 22686f170..277016d7e 100644 --- a/classes/Product.php +++ b/classes/Product.php @@ -654,13 +654,16 @@ class ProductCore extends ObjectModel * - physical stock for this product * - supply order(s) for this product */ - $stock_manager = StockManagerFactory::getManager(); - $physical_quantity = $stock_manager->getProductPhysicalQuantities($this->id, 0); - $real_quantity = $stock_manager->getProductRealQuantities($this->id, 0); - if ($physical_quantity > 0) - return false; - if ($real_quantity > $physical_quantity) - return false; + if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT')) + { + $stock_manager = StockManagerFactory::getManager(); + $physical_quantity = $stock_manager->getProductPhysicalQuantities($this->id, 0); + $real_quantity = $stock_manager->getProductRealQuantities($this->id, 0); + if ($physical_quantity > 0) + return false; + if ($real_quantity > $physical_quantity) + return false; + } /* * @since 1.5.0 diff --git a/classes/order/OrderHistory.php b/classes/order/OrderHistory.php index 6412fa58e..b6519c818 100644 --- a/classes/order/OrderHistory.php +++ b/classes/order/OrderHistory.php @@ -94,7 +94,10 @@ class OrderHistoryCore extends ObjectModel if (!Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT') && !$isValidated AND $newOS->logable AND isset($oldOrderStatus) AND $oldOrderStatus AND $oldOrderStatus->id == Configuration::get('PS_OS_ERROR')) StockAvailable::updateQuantity($product['id_product'], $product['id_product_attribute'], (int)$product['cart_quantity']); - else if ($newOS->shipped == 1 && $oldOrderStatus->shipped == 0) // The product is removed from the physical stock. $id_warehouse is needed + // If order is shipped for the first time and + // if we use advanced stock management system, decrement stock preperly. + // The product is removed from the physical stock. $id_warehouse is needed + else if ($newOS->shipped == 1 && $oldOrderStatus->shipped == 0 && Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT')) { $manager = StockManagerFactory::getManager(); $warehouse = new Warehouse($id_warehouse); diff --git a/classes/stock/StockManager.php b/classes/stock/StockManager.php index 979abb5fd..e5e65c526 100644 --- a/classes/stock/StockManager.php +++ b/classes/stock/StockManager.php @@ -207,10 +207,10 @@ class StockManagerCore implements StockManagerInterface // Special case of a pack if (Pack::isPack($id_product)) { - $products_pack = Pack::getItems((int)$product['id_product'], (int)Configuration::get('PS_LANG_DEFAULT')); + $products_pack = Pack::getItems($id_product, (int)Configuration::get('PS_LANG_DEFAULT')); foreach ($products_pack as $product_pack) { - $pack_id_product_attribute = Product::getDefaultAttribute($tab_product_pack['id_product'], 1); //@TODO is there a better way to retrieve the product attribute assciated to the pack ? + $pack_id_product_attribute = Product::getDefaultAttribute($id_product_attribute, 1); //@TODO is there a better way to retrieve the product attribute assciated to the pack ? $this->removeProduct($product_pack->id, $pack_id_product_attribute, $product_pack->pack_quantity * $quantity, $warehouse, $id_order); } } diff --git a/controllers/admin/AdminProductsController.php b/controllers/admin/AdminProductsController.php index 9a89bf6b7..fd21c5eb0 100644 --- a/controllers/admin/AdminProductsController.php +++ b/controllers/admin/AdminProductsController.php @@ -2648,17 +2648,12 @@ class AdminProductsControllerCore extends AdminController $this->addJS(_PS_JS_DIR_.'tiny_mce/tiny_mce.js'); $this->addJS(_PS_JS_DIR_.'tinymce.inc.js'); - $currency = $this->context->currency; - $data->assign('languages',$languages); $this->object = $product; $this->display = 'edit'; $content = ''; - $has_attribute = $product->hasAttributes(); - // @FIXME Stock, need to use StockManagerFactory - $qty = 0; $cover = Product::getCover($product->id); $this->_applyTaxToEcotax($product); diff --git a/modules/mailalerts/mailalerts.php b/modules/mailalerts/mailalerts.php index 0e1fc9b90..198448166 100644 --- a/modules/mailalerts/mailalerts.php +++ b/modules/mailalerts/mailalerts.php @@ -327,7 +327,7 @@ class MailAlerts extends Module $sql = 'SELECT id_product, quantity FROM '._DB_PREFIX_.'stock_available WHERE id_product_attribute = '.(int)$params['id_product_attribute'] - .Context::getContext()->shop->addSqlRestriction(); + .StockAvailable::addSqlShopRestriction(); $result = Db::getInstance()->getRow($sql); if ($this->_customer_qty AND $result['quantity'] > 0)