// Re-stock (Prices fixed/warehouse fixed)
This commit is contained in:
@@ -89,7 +89,7 @@ class OrderHistoryCore extends ObjectModel
|
||||
if ($newOS->logable AND (!$oldOrderStatus OR !$oldOrderStatus->logable))
|
||||
{
|
||||
ProductSale::addProductSale($product['id_product'], $product['cart_quantity']);
|
||||
StockAvailable::updateQuantity($product['id_product'], $product['id_product_attribute'], -(int)$product['cart_quantity']);
|
||||
|
||||
}
|
||||
/* If becoming unlogable => removing sale */
|
||||
elseif (!$newOS->logable AND ($oldOrderStatus AND $oldOrderStatus->logable))
|
||||
@@ -100,20 +100,41 @@ class OrderHistoryCore extends ObjectModel
|
||||
// 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
|
||||
// @TODO Checks $id_warehouse
|
||||
else if ($newOS->shipped == 1 && $oldOrderStatus->shipped == 0 && Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT'))
|
||||
{
|
||||
$manager = StockManagerFactory::getManager();
|
||||
$warehouse = new Warehouse($id_warehouse);
|
||||
|
||||
$manager->removeProduct($product['id_product'],
|
||||
$product['id_product_attribute'],
|
||||
$warehouse,
|
||||
$product['cart_quantity'],
|
||||
Configuration::get('PS_STOCK_CUSTOMER_ORDER_REASON'),
|
||||
true,
|
||||
(int)$id_order);
|
||||
$manager->removeProduct(
|
||||
$product['id_product'],
|
||||
$product['id_product_attribute'],
|
||||
$warehouse,
|
||||
$product['cart_quantity'],
|
||||
Configuration::get('PS_STOCK_CUSTOMER_ORDER_REASON'),
|
||||
true,
|
||||
(int)$id_order
|
||||
);
|
||||
}
|
||||
else if ($newOS->shipped == 0 && $oldOrderStatus->shipped == 1 && Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT'))
|
||||
{
|
||||
$manager = StockManagerFactory::getManager();
|
||||
$warehouse = new Warehouse($id_warehouse);
|
||||
|
||||
$mvts = StockMvt::getNegativeStockMvts($order->id, $product['id_product'], $product['id_product_attribute'], $product['cart_quantity']);
|
||||
foreach ($mvts as $mvt)
|
||||
{
|
||||
$manager->addProduct(
|
||||
$product['id_product'],
|
||||
$product['id_product_attribute'],
|
||||
new Warehouse($mvt['id_warehouse']),
|
||||
$mvt['physical_quantity'],
|
||||
null,
|
||||
$mvt['price_te'],
|
||||
true
|
||||
);
|
||||
}
|
||||
}
|
||||
// @todo If the old order states was "shipped" and the new is "not shipped" the stock is not decremented
|
||||
}
|
||||
|
||||
$this->id_order_state = (int)($new_order_state);
|
||||
|
||||
@@ -194,4 +194,40 @@ class StockMvtCore extends ObjectModel
|
||||
{
|
||||
Tools::displayAsDeprecated();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the negative stock mvts that corresponds to the given order, for the given product, in the given quantity
|
||||
*
|
||||
* @since 1.5.0
|
||||
* @param int $id_order
|
||||
* @param int $id_product
|
||||
* @param int $id_product_attribute
|
||||
* @param int $quantity
|
||||
* @return Array mvts
|
||||
*/
|
||||
public static function getNegativeStockMvts($id_order, $id_product, $id_product_attribute, $quantity)
|
||||
{
|
||||
$mvts = array();
|
||||
$quantity_total = 0;
|
||||
|
||||
$query = new DbQuery();
|
||||
$query->select('sm.*, s.id_warehouse');
|
||||
$query->from('stock_mvt sm');
|
||||
$query->innerJoin('stock s ON (s.id_stock = sm.id_stock)');
|
||||
$query->where('sm.sign = -1');
|
||||
$query->where('sm.id_order = '.(int)$id_order);
|
||||
$query->where('s.id_product = '.(int)$id_product.' AND s.id_product_attribute = '.(int)$id_product_attribute);
|
||||
$query->orderBy('date_add DESC');
|
||||
|
||||
$res = Db::getInstance(_PS_USE_SQL_SLAVE_)->query($query);
|
||||
while ($row = Db::getInstance(_PS_USE_SQL_SLAVE_)->nextRow($res))
|
||||
{
|
||||
if ($quantity_total >= $quantity)
|
||||
break;
|
||||
$quantity_total += (int)$row['physical_quantity'];
|
||||
$mvts[] = $row;
|
||||
}
|
||||
|
||||
return $mvts;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user