[-] BO: Fix some bugs on advanced stock management
This commit is contained in:
1 parent
399526e451
commit
10d148782a
6 files changed
+81
-75
No files matched your search
@@ -2402,7 +2402,7 @@ class AdminControllerCore extends Controller
|
|||||||
$this->identifier => $id_object,
|
$this->identifier => $id_object,
|
||||||
'id_shop' => (int)$id_shop,
|
'id_shop' => (int)$id_shop,
|
||||||
);
|
);
|
||||||
Db::getInstance()->insert($this->table.'_shop', $insert, false, true, Db::INSERT_IGNORE);
|
return Db::getInstance()->insert($this->table.'_shop', $insert, false, true, Db::INSERT_IGNORE);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function validateField($value, $field)
|
protected function validateField($value, $field)
|
||||||
|
|||||||
@@ -137,54 +137,69 @@ class StockAvailableCore extends ObjectModel
|
|||||||
|
|
||||||
// if it's a simple product
|
// if it's a simple product
|
||||||
if (empty($ids_product_attribute))
|
if (empty($ids_product_attribute))
|
||||||
$product_quantity = $manager->getProductRealQuantities($id_product, null, $warehouses, true);
|
|
||||||
|
|
||||||
// else this product has attributes, hence loops on $ids_product_attribute
|
|
||||||
foreach ($ids_product_attribute as $id_product_attribute)
|
|
||||||
{
|
{
|
||||||
$quantity = $manager->getProductRealQuantities($id_product, $id_product_attribute, $warehouses, true);
|
$allowed_warehouse_for_product = WareHouse::getProductWarehouseList((int)$id_product, 0, (int)$id_shop);
|
||||||
|
$allowed_warehouse_for_product_clean = array();
|
||||||
$query = new DbQuery();
|
foreach ($allowed_warehouse_for_product as $warehouse)
|
||||||
$query->select('COUNT(*)');
|
$allowed_warehouse_for_product_clean[] = (int)$warehouse['id_warehouse'];
|
||||||
$query->from('stock_available');
|
$allowed_warehouse_for_product_clean = array_intersect($allowed_warehouse_for_product_clean, $warehouses);
|
||||||
$query->where('id_product = '.(int)$id_product.' AND id_product_attribute = '.(int)$id_product_attribute.
|
$product_quantity = $manager->getProductRealQuantities($id_product, null, $allowed_warehouse_for_product_clean, true);
|
||||||
StockAvailable::addSqlShopRestriction(null, $id_shop));
|
}
|
||||||
|
// else this product has attributes, hence loops on $ids_product_attribute
|
||||||
if ((int)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query))
|
else
|
||||||
|
{
|
||||||
|
foreach ($ids_product_attribute as $id_product_attribute)
|
||||||
{
|
{
|
||||||
$query = array(
|
|
||||||
'table' => 'stock_available',
|
|
||||||
'data' => array('quantity' => $quantity),
|
|
||||||
'where' => 'id_product = '.(int)$id_product.' AND id_product_attribute = '.(int)$id_product_attribute.
|
|
||||||
StockAvailable::addSqlShopRestriction(null, $id_shop)
|
|
||||||
);
|
|
||||||
Db::getInstance()->update($query['table'], $query['data'], $query['where']);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$query = array(
|
|
||||||
'table' => 'stock_available',
|
|
||||||
'data' => array(
|
|
||||||
'quantity' => $quantity,
|
|
||||||
'depends_on_stock' => 1,
|
|
||||||
'out_of_stock' => $out_of_stock,
|
|
||||||
'id_product' => (int)$id_product,
|
|
||||||
'id_product_attribute' => (int)$id_product_attribute,
|
|
||||||
)
|
|
||||||
);
|
|
||||||
StockAvailable::addSqlShopParams($query['data']);
|
|
||||||
Db::getInstance()->insert($query['table'], $query['data']);
|
|
||||||
}
|
|
||||||
|
|
||||||
$product_quantity += $quantity;
|
$allowed_warehouse_for_combination = WareHouse::getProductWarehouseList((int)$id_product, (int)$id_product_attribute, (int)$id_shop);
|
||||||
|
$allowed_warehouse_for_combination_clean = array();
|
||||||
Hook::exec('actionUpdateQuantity',
|
foreach ($allowed_warehouse_for_combination as $warehouse)
|
||||||
array(
|
$allowed_warehouse_for_combination_clean[] = (int)$warehouse['id_warehouse'];
|
||||||
'id_product' => $id_product,
|
$allowed_warehouse_for_combination_clean = array_intersect($allowed_warehouse_for_combination_clean, $warehouses);
|
||||||
'id_product_attribute' => $id_product_attribute,
|
$quantity = $manager->getProductRealQuantities($id_product, $id_product_attribute, $allowed_warehouse_for_combination_clean, true);
|
||||||
'quantity' => $quantity
|
|
||||||
|
$query = new DbQuery();
|
||||||
|
$query->select('COUNT(*)');
|
||||||
|
$query->from('stock_available');
|
||||||
|
$query->where('id_product = '.(int)$id_product.' AND id_product_attribute = '.(int)$id_product_attribute.
|
||||||
|
StockAvailable::addSqlShopRestriction(null, $id_shop));
|
||||||
|
|
||||||
|
if ((int)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query))
|
||||||
|
{
|
||||||
|
$query = array(
|
||||||
|
'table' => 'stock_available',
|
||||||
|
'data' => array('quantity' => $quantity),
|
||||||
|
'where' => 'id_product = '.(int)$id_product.' AND id_product_attribute = '.(int)$id_product_attribute.
|
||||||
|
StockAvailable::addSqlShopRestriction(null, $id_shop)
|
||||||
|
);
|
||||||
|
Db::getInstance()->update($query['table'], $query['data'], $query['where']);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$query = array(
|
||||||
|
'table' => 'stock_available',
|
||||||
|
'data' => array(
|
||||||
|
'quantity' => $quantity,
|
||||||
|
'depends_on_stock' => 1,
|
||||||
|
'out_of_stock' => $out_of_stock,
|
||||||
|
'id_product' => (int)$id_product,
|
||||||
|
'id_product_attribute' => (int)$id_product_attribute,
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
StockAvailable::addSqlShopParams($query['data']);
|
||||||
|
Db::getInstance()->insert($query['table'], $query['data']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$product_quantity += $quantity;
|
||||||
|
|
||||||
|
Hook::exec('actionUpdateQuantity',
|
||||||
|
array(
|
||||||
|
'id_product' => $id_product,
|
||||||
|
'id_product_attribute' => $id_product_attribute,
|
||||||
|
'quantity' => $quantity
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// updates
|
// updates
|
||||||
|
|||||||
@@ -432,6 +432,8 @@ class StockManagerCore implements StockManagerInterface
|
|||||||
|
|
||||||
// casts for security reason
|
// casts for security reason
|
||||||
$ids_warehouse = array_map('intval', $ids_warehouse);
|
$ids_warehouse = array_map('intval', $ids_warehouse);
|
||||||
|
if (!count($ids_warehouse))
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
$ids_warehouse = array();
|
$ids_warehouse = array();
|
||||||
|
|||||||
@@ -127,27 +127,6 @@ class WarehouseCore extends ObjectModel
|
|||||||
return $res;
|
return $res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the shops associated to the current warehouse
|
|
||||||
*
|
|
||||||
* @param array $ids_shop Ids to set
|
|
||||||
*/
|
|
||||||
public function setShops($ids_shop)
|
|
||||||
{
|
|
||||||
if (!is_array($ids_shop))
|
|
||||||
$ids_shop = array();
|
|
||||||
|
|
||||||
$row_to_insert = array();
|
|
||||||
foreach ($ids_shop as $id_shop)
|
|
||||||
$row_to_insert[] = array($this->def['primary'] => $this->id, 'id_shop' => (int)$id_shop);
|
|
||||||
|
|
||||||
Db::getInstance()->execute('
|
|
||||||
DELETE FROM '._DB_PREFIX_.'warehouse_shop
|
|
||||||
WHERE '.$this->def['primary'].' = '.(int)$this->id);
|
|
||||||
|
|
||||||
Db::getInstance()->insert('warehouse_shop', $row_to_insert);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the carriers associated to the current warehouse
|
* Gets the carriers associated to the current warehouse
|
||||||
*
|
*
|
||||||
@@ -551,6 +530,13 @@ class WarehouseCore extends ObjectModel
|
|||||||
$res = call_user_func_array('array_intersect', $list);
|
$res = call_user_func_array('array_intersect', $list);
|
||||||
return $res;
|
return $res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function resetStockAvailable()
|
||||||
|
{
|
||||||
|
$products = WarehouseProductLocation::getProducts((int)$this->id);
|
||||||
|
foreach ($products as $product)
|
||||||
|
StockAvailable::synchronize((int)$product['id_product']);
|
||||||
|
}
|
||||||
|
|
||||||
/*********************************\
|
/*********************************\
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -132,4 +132,9 @@ class WarehouseProductLocationCore extends ObjectModel
|
|||||||
$collection->where('id_product', '=', (int)$id_product);
|
$collection->where('id_product', '=', (int)$id_product);
|
||||||
return $collection;
|
return $collection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function getProducts($id_warehouse)
|
||||||
|
{
|
||||||
|
return Db::getInstance()->executeS('SELECT DISTINCT id_product FROM '._DB_PREFIX_.'warehouse_product_location WHERE id_warehouse='.(int)$id_warehouse);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -436,10 +436,6 @@ class AdminWarehousesControllerCore extends AdminController
|
|||||||
$address->save();
|
$address->save();
|
||||||
}
|
}
|
||||||
|
|
||||||
// handles shops associations
|
|
||||||
if (Tools::isSubmit('ids_shops'))
|
|
||||||
$object->setShops(Tools::getValue('ids_shops'));
|
|
||||||
|
|
||||||
// handles carriers associations
|
// handles carriers associations
|
||||||
if (Tools::isSubmit('ids_carriers'))
|
if (Tools::isSubmit('ids_carriers'))
|
||||||
$object->setCarriers(Tools::getValue('ids_carriers'));
|
$object->setCarriers(Tools::getValue('ids_carriers'));
|
||||||
@@ -569,7 +565,6 @@ class AdminWarehousesControllerCore extends AdminController
|
|||||||
|
|
||||||
// removes associations with carriers/shops/products location
|
// removes associations with carriers/shops/products location
|
||||||
$obj->setCarriers(array());
|
$obj->setCarriers(array());
|
||||||
$obj->setShops(array());
|
|
||||||
$obj->resetProductsLocations();
|
$obj->resetProductsLocations();
|
||||||
|
|
||||||
return parent::processDelete();
|
return parent::processDelete();
|
||||||
@@ -586,16 +581,19 @@ class AdminWarehousesControllerCore extends AdminController
|
|||||||
if (!($obj = $this->loadObject(true)))
|
if (!($obj = $this->loadObject(true)))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// handles shops associations
|
|
||||||
if (Tools::isSubmit('ids_shops'))
|
|
||||||
$obj->setShops(Tools::getValue('ids_shops'));
|
|
||||||
|
|
||||||
// handles carriers associations
|
// handles carriers associations
|
||||||
$obj->setCarriers(Tools::getValue('ids_carriers'), array());
|
$obj->setCarriers(Tools::getValue('ids_carriers'), array());
|
||||||
|
|
||||||
return parent::processUpdate();
|
return parent::processUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function updateAssoShop($id_object)
|
||||||
|
{
|
||||||
|
parent::updateAssoShop($id_object);
|
||||||
|
if (!($obj = $this->loadObject(true)))
|
||||||
|
return;
|
||||||
|
$obj->resetStockAvailable();
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* @see AdminController::initProcess()
|
* @see AdminController::initProcess()
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in new issue
Block a user