// StockAvailable : cleaned & fixed save()

git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@9414 b9a71923-0436-4b27-9f14-aed3839534dd
This commit is contained in:
bMancone committed 2011-10-18 12:10:26 +00:00
1 parent 39f37e465f
commit 6e2827418f
1 file changed
+33 -29
+33 -29
View File
@@ -91,7 +91,7 @@ class StockAvailableCore extends ObjectModel
{ {
if (is_null($id_shop)) if (is_null($id_shop))
$id_shop = Context::getContext()->shop->getID(true); $id_shop = Context::getContext()->shop->getID(true);
$query = new DbQuery(); $query = new DbQuery();
$query->select('id_stock_available'); $query->select('id_stock_available');
$query->from('stock_available'); $query->from('stock_available');
@@ -103,7 +103,6 @@ class StockAvailableCore extends ObjectModel
return (int)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query); return (int)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query);
} }
/** /**
* For a given id_product, synchronizes StockAvailable::quantity with Stock::usable_quantity * For a given id_product, synchronizes StockAvailable::quantity with Stock::usable_quantity
* *
@@ -156,17 +155,17 @@ class StockAvailableCore extends ObjectModel
} }
$query = array( $query = array(
'table' => 'stock_available', 'table' => 'stock_available',
'data' => array('quantity' => $total_quantity), 'data' => array('quantity' => $total_quantity),
'type' => 'UPDATE', 'type' => 'UPDATE',
'where' => 'id_product = '.(int)$id_product.' AND id_product_attribute = 0 AND id_shop = '.(int)$id_shop 'where' => 'id_product = '.(int)$id_product.' AND id_product_attribute = 0 AND id_shop = '.(int)$id_shop
); );
// saves quantities // saves quantities
Db::getInstance()->autoExecute($query['table'], $query['data'], $query['type'], $query['where']); Db::getInstance()->autoExecute($query['table'], $query['data'], $query['type'], $query['where']);
} }
} }
/** /**
* For a given id_product, sets if stock available depends on stock * 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)) if (is_null($id_shop))
$id_shop = Context::getContext()->shop->getID(true); $id_shop = Context::getContext()->shop->getID(true);
Db::getInstance()->autoExecute( Db::getInstance()->autoExecute(
'stock_available', 'stock_available',
array('depends_on_stock' => (boolean)$depends_on_stock), array('depends_on_stock' => (bool)$depends_on_stock),
'UPDATE', 'UPDATE',
'id_product = '.(int)$id_product.' AND id_shop = '.(int)$id_shop 'id_product = '.(int)$id_product.' AND id_shop = '.(int)$id_shop
); );
if($depends_on_stock) if ($depends_on_stock)
StockAvailable::synchronize($id_product); StockAvailable::synchronize($id_product);
} }
/** /**
* For a given id_product, sets if product is available out of stocks * 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)) if (is_null($id_shop))
$id_shop = Context::getContext()->shop->getID(true); $id_shop = Context::getContext()->shop->getID(true);
Db::getInstance()->autoExecute( Db::getInstance()->autoExecute(
'stock_available', 'stock_available',
array('out_of_stock' => (int)$out_of_stock), 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 'id_product = '.(int)$id_product.' AND id_shop = '.(int)$id_shop
); );
} }
/** /**
* For a given id_product and id_product_attribute, gets stock available * For a given id_product and id_product_attribute, gets stock available
* *
* @param int $id_product * @param int $id_product
* @param int $id_product_attribute * @param int $id_product_attribute
* @param int $id_shop * @param int $id_shop
* @return int
*/ */
public static function getStockAvailableForProduct($id_product, $id_product_attribute = null, $id_shop = null) public static function getStockAvailableForProduct($id_product, $id_product_attribute = null, $id_shop = null)
{ {
if (is_null($id_shop)) if (is_null($id_shop))
$id_shop = Context::getContext()->shop->getID(true); $id_shop = Context::getContext()->shop->getID(true);
$query = new DbQuery(); $query = new DbQuery();
$query->select('quantity'); $query->select('quantity');
$query->from('stock_available'); $query->from('stock_available');
@@ -229,32 +229,36 @@ class StockAvailableCore extends ObjectModel
$query->where('id_shop = '.(int)$id_shop); $query->where('id_shop = '.(int)$id_shop);
return (int)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query); 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; return false;
if ($this->id_product_attribute = 0) if ($this->id_product_attribute == 0)
return true; return true;
$id_stock_available = StockAvailable::getIdStockAvailable($this->id_product, 0, $this->id_shop); $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'); WHERE id_product = '.(int)$this->id_product.' AND id_shop = '.(int)$this->id_shop.' AND id_product_attribute <> 0');
if (!$id_stock_available) if (!$id_stock_available)
{ {
return (int)Db::getInstance()->execute('INSERT '._DB_PREFIX_.'stock_available return (int)Db::getInstance()->execute('
SET id_product = '.(int)$this->id_product.', id_product_attribute = 0, id_shop = '.(int)$this->id_shop.' INSERT '._DB_PREFIX_.'stock_available
quantity = '.(int)$total_quantity); SET id_product = '.(int)$this->id_product.', id_product_attribute = 0, id_shop = '.(int)$this->id_shop.', quantity = '.(int)$total_quantity);
} }
else 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.' SET quantity = '.(int)$total_quantity.'
WHERE id_stock_available = '.(int)$id_stock_available); WHERE id_stock_available = '.(int)$id_stock_available);
} }