// Fixed synchronize
git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@9314 b9a71923-0436-4b27-9f14-aed3839534dd
This commit is contained in:
1 parent
2e67942ba5
commit
74151b61b2
1 file changed
+29
-21
@@ -102,57 +102,65 @@ class StockAvailableCore extends ObjectModel
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* For a given id_product, synchronizes StockAvailable.quantity with Stock.usable_quantity
|
* For a given id_product, synchronizes StockAvailable::quantity with Stock::usable_quantity
|
||||||
*
|
*
|
||||||
* @param int $id_product
|
* @param int $id_product
|
||||||
*/
|
*/
|
||||||
public static function synchronize($id_product)
|
public static function synchronize($id_product)
|
||||||
{
|
{
|
||||||
|
// used var in algorithm
|
||||||
|
$ids_warehouse = array();
|
||||||
|
$ids_product_attribute = array();
|
||||||
|
|
||||||
|
// builds query to get all warehouses/shops
|
||||||
$query = new DbQuery();
|
$query = new DbQuery();
|
||||||
$query->select('id_warehouse, id_shop');
|
$query->select('id_warehouse, id_shop');
|
||||||
$query->from('warehouse_shop');
|
$query->from('warehouse_shop');
|
||||||
$query->orderBy('id_shop');
|
$query->orderBy('id_shop');
|
||||||
|
|
||||||
// gets warehouses id group by shop id
|
// queries to get warehouse ids grouped by shops
|
||||||
$ids_warehouse = array();
|
|
||||||
foreach (Db::getInstance()->executeS($query) as $row)
|
foreach (Db::getInstance()->executeS($query) as $row)
|
||||||
$ids_warehouse[$row['id_shop']][] = $row['id_warehouse'];
|
$ids_warehouse[$row['id_shop']][] = $row['id_warehouse'];
|
||||||
|
|
||||||
// gets all id_product_atribute
|
// gets all product attributes ids
|
||||||
$ids_product_attribute = array();
|
|
||||||
foreach (Product::getProductAttributesIds($id_product) as $id_product_attribute)
|
foreach (Product::getProductAttributesIds($id_product) as $id_product_attribute)
|
||||||
$ids_product_attribute[] = $id_product_attribute['id_product_attribute'];
|
$ids_product_attribute[] = $id_product_attribute['id_product_attribute'];
|
||||||
|
|
||||||
|
// loops on ids_warehouse to synchronize
|
||||||
foreach ($ids_warehouse as $id_shop => $warehouses)
|
foreach ($ids_warehouse as $id_shop => $warehouses)
|
||||||
{
|
{
|
||||||
$total_quantity = 0;
|
$total_quantity = 0;
|
||||||
|
|
||||||
// saves quantities for all products combinations
|
// if there are no product attributes
|
||||||
|
if (empty($ids_product_attribute))
|
||||||
|
$total_quantity = StockManagerFactory::getManager()->getProductRealQuantities($id_product, null, $warehouses, true);
|
||||||
|
|
||||||
|
// else loops on id_product_attribute and to get $total_quantity
|
||||||
foreach ($ids_product_attribute as $id_product_attribute)
|
foreach ($ids_product_attribute as $id_product_attribute)
|
||||||
{
|
{
|
||||||
$quantity = StockManagerFactory::getManager()->getProductRealQuantities($id_product, $id_product_attribute, $warehouses, true);
|
$quantity = StockManagerFactory::getManager()->getProductRealQuantities($id_product, $id_product_attribute, $warehouses, true);
|
||||||
|
|
||||||
Db::getInstance()->autoExecute(
|
$query = array(
|
||||||
'stock_available',
|
'table' => 'stock_available',
|
||||||
array('quantity' => $quantity),
|
'data' => array('quantity' => $quantity),
|
||||||
'UPDATE',
|
'type' => 'UPDATE',
|
||||||
'id_product = '.(int)$id_product.' AND id_product_attribute = '.(int)$id_product_attribute.' AND id_shop = '.(int)$id_shop
|
'where' => 'id_product = '.(int)$id_product.' AND id_product_attribute = '.(int)$id_product_attribute.' AND id_shop = '.(int)$id_shop
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Db::getInstance()->autoExecute($query['table'], $query['data'], $query['type'], $query['where']);
|
||||||
|
|
||||||
$total_quantity += $quantity;
|
$total_quantity += $quantity;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (empty($ids_product_attribute))
|
$query = array(
|
||||||
$total_quantity = StockManagerFactory::getManager()->getProductRealQuantities($id_product, null, $warehouses, true);
|
'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
|
// saves quantities
|
||||||
Db::getInstance()->autoExecute(
|
Db::getInstance()->autoExecute($query['table'], $query['data'], $query['type'], $query['where']);
|
||||||
'stock_available',
|
|
||||||
array('quantity' => $total_quantity),
|
|
||||||
'UPDATE',
|
|
||||||
'id_product = '.(int)$id_product.' AND id_product_attribute = 0 AND id_shop = '.(int)$id_shop
|
|
||||||
);
|
|
||||||
|
|
||||||
$total_quantity += $quantity;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user