From 6e2827418fe159e258c6f6dc134fcf654daf17d6 Mon Sep 17 00:00:00 2001 From: bMancone Date: Tue, 18 Oct 2011 12:10:26 +0000 Subject: [PATCH] // StockAvailable : cleaned & fixed save() git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@9414 b9a71923-0436-4b27-9f14-aed3839534dd --- classes/stock/StockAvailable.php | 62 +++++++++++++++++--------------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/classes/stock/StockAvailable.php b/classes/stock/StockAvailable.php index 06a60ee75..1b8161e0a 100644 --- a/classes/stock/StockAvailable.php +++ b/classes/stock/StockAvailable.php @@ -91,7 +91,7 @@ class StockAvailableCore extends ObjectModel { if (is_null($id_shop)) $id_shop = Context::getContext()->shop->getID(true); - + $query = new DbQuery(); $query->select('id_stock_available'); $query->from('stock_available'); @@ -103,7 +103,6 @@ class StockAvailableCore extends ObjectModel return (int)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query); } - /** * For a given id_product, synchronizes StockAvailable::quantity with Stock::usable_quantity * @@ -156,17 +155,17 @@ class StockAvailableCore extends ObjectModel } $query = array( - 'table' => 'stock_available', - 'data' => array('quantity' => $total_quantity), - 'type' => 'UPDATE', - 'where' => 'id_product = '.(int)$id_product.' AND id_product_attribute = 0 AND id_shop = '.(int)$id_shop - ); + 'table' => 'stock_available', + 'data' => array('quantity' => $total_quantity), + 'type' => 'UPDATE', + 'where' => 'id_product = '.(int)$id_product.' AND id_product_attribute = 0 AND id_shop = '.(int)$id_shop + ); // saves quantities Db::getInstance()->autoExecute($query['table'], $query['data'], $query['type'], $query['where']); } } - + /** * For a given id_product, sets if stock available depends on stock * @@ -178,17 +177,17 @@ class StockAvailableCore extends ObjectModel { if (is_null($id_shop)) $id_shop = Context::getContext()->shop->getID(true); - + Db::getInstance()->autoExecute( 'stock_available', - array('depends_on_stock' => (boolean)$depends_on_stock), + array('depends_on_stock' => (bool)$depends_on_stock), 'UPDATE', 'id_product = '.(int)$id_product.' AND id_shop = '.(int)$id_shop ); - if($depends_on_stock) + if ($depends_on_stock) StockAvailable::synchronize($id_product); } - + /** * For a given id_product, sets if product is available out of stocks * @@ -200,7 +199,7 @@ class StockAvailableCore extends ObjectModel { if (is_null($id_shop)) $id_shop = Context::getContext()->shop->getID(true); - + Db::getInstance()->autoExecute( 'stock_available', array('out_of_stock' => (int)$out_of_stock), @@ -208,19 +207,20 @@ class StockAvailableCore extends ObjectModel 'id_product = '.(int)$id_product.' AND id_shop = '.(int)$id_shop ); } - + /** * For a given id_product and id_product_attribute, gets stock available * * @param int $id_product * @param int $id_product_attribute * @param int $id_shop + * @return int */ public static function getStockAvailableForProduct($id_product, $id_product_attribute = null, $id_shop = null) { if (is_null($id_shop)) $id_shop = Context::getContext()->shop->getID(true); - + $query = new DbQuery(); $query->select('quantity'); $query->from('stock_available'); @@ -229,32 +229,36 @@ class StockAvailableCore extends ObjectModel $query->where('id_shop = '.(int)$id_shop); return (int)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query); } - + /** - * After saving a quantity available, upgrade the total_quantity_available + * Upgrades total_quantity_available after having saved + * @see ObjectModel::save() */ - public function save($nullValues = false, $autodate = true) + public function save($null_values = false, $autodate = true) { - if (!parent::save($nullValues, $autodate)) + if (!parent::save($null_values, $autodate)) return false; - - if ($this->id_product_attribute = 0) + + if ($this->id_product_attribute == 0) return true; - + $id_stock_available = StockAvailable::getIdStockAvailable($this->id_product, 0, $this->id_shop); - - $total_quantity = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('SELECT sum(quantity) FROM '._DB_PREFIX_.'stock_available + + $total_quantity = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' + SELECT SUM(quantity) + FROM '._DB_PREFIX_.'stock_available WHERE id_product = '.(int)$this->id_product.' AND id_shop = '.(int)$this->id_shop.' AND id_product_attribute <> 0'); - + if (!$id_stock_available) { - return (int)Db::getInstance()->execute('INSERT '._DB_PREFIX_.'stock_available - SET id_product = '.(int)$this->id_product.', id_product_attribute = 0, id_shop = '.(int)$this->id_shop.' - quantity = '.(int)$total_quantity); + return (int)Db::getInstance()->execute(' + INSERT '._DB_PREFIX_.'stock_available + SET id_product = '.(int)$this->id_product.', id_product_attribute = 0, id_shop = '.(int)$this->id_shop.', quantity = '.(int)$total_quantity); } else { - return (int)Db::getInstance()->execute('UPDATE '._DB_PREFIX_.'stock_available + return (int)Db::getInstance()->execute(' + UPDATE '._DB_PREFIX_.'stock_available SET quantity = '.(int)$total_quantity.' WHERE id_stock_available = '.(int)$id_stock_available); }