// Refactoring of Hook class

git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@11998 b9a71923-0436-4b27-9f14-aed3839534dd
This commit is contained in:
rMalie
2012-01-02 14:42:05 +00:00
parent d56b86297c
commit 50e4dff4e5
8 changed files with 457 additions and 319 deletions
+135 -122
View File
@@ -67,140 +67,153 @@ class OrderHistoryCore extends ObjectModel
public function changeIdOrderState($new_order_state, $id_order)
{
if ($new_order_state != NULL)
{
Hook::updateOrderStatus((int)($new_order_state), (int)$id_order);
$order = new Order((int)($id_order));
if (!$new_order_state)
return;
/* Best sellers */
$newOS = new OrderState((int)($new_order_state), $order->id_lang);
$oldOrderStatus = OrderHistory::getLastOrderState((int)$id_order);
$isValidated = $this->isValidated();
if (Validate::isLoadedObject($order))
foreach ($order->getProductsDetail() as $product)
$order = new Order($id_order);
$newOS = new OrderState((int)$new_order_state, $order->id_lang);
// Execute pre hooks
if ($newOS->id == Configuration::get('PS_OS_PAYMENT'))
Hook::exec('actionPaymentConfirmation', array(
'id_order' => (int)$order->id
));
Hook::exec('actionOrderStatusUpdate', array(
'newOrderStatus' => $newOS,
'id_order' => (int)$order->id
));
$oldOrderStatus = OrderHistory::getLastOrderState((int)$id_order);
$isValidated = $this->isValidated();
if (Validate::isLoadedObject($order))
foreach ($order->getProductsDetail() as $product)
{
/* If becoming logable => adding sale */
if ($newOS->logable
&& !($oldOrderStatus instanceof OrderState
&& $oldOrderStatus->logable))
{
/* If becoming logable => adding sale */
if ($newOS->logable
&& !($oldOrderStatus instanceof OrderState
&& $oldOrderStatus->logable))
{
ProductSale::addProductSale($product['product_id'], $product['product_quantity']);
}
/* If becoming unlogable => removing sale */
else if (!$newOS->logable
&& $oldOrderStatus instanceof OrderState
&& $oldOrderStatus->logable)
{
ProductSale::removeProductSale($product['product_id'], $product['product_quantity']);
// @since 1.5.0
if ($newOS->id == Configuration::get('PS_OS_ERROR') || $newOS->id == Configuration::get('PS_OS_CANCELED'))
StockAvailable::updateQuantity($product['product_id'], $product['product_attribute_id'], (int)$product['product_quantity'], $order->id_shop);
}
if ((!Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT') || (int)$product['advanced_stock_management'] != 1)
&& !$isValidated
&& $newOS->logable
&& $oldOrderStatus instanceof OrderState
&& $oldOrderStatus->id == Configuration::get('PS_OS_ERROR')
)
ProductSale::addProductSale($product['product_id'], $product['product_quantity']);
}
/* If becoming unlogable => removing sale */
else if (!$newOS->logable
&& $oldOrderStatus instanceof OrderState
&& $oldOrderStatus->logable)
{
ProductSale::removeProductSale($product['product_id'], $product['product_quantity']);
// @since 1.5.0
if ($newOS->id == Configuration::get('PS_OS_ERROR') || $newOS->id == Configuration::get('PS_OS_CANCELED'))
StockAvailable::updateQuantity($product['product_id'], $product['product_attribute_id'], (int)$product['product_quantity'], $order->id_shop);
// 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 instanceof OrderState
&& $oldOrderStatus->shipped == 0
&& Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT')
&& (int)$product['advanced_stock_management'] == 1)
{
$manager = StockManagerFactory::getManager();
$warehouse = new Warehouse($product['id_warehouse']);
$manager->removeProduct(
$product['product_id'],
$product['product_attribute_id'],
$warehouse,
$product['product_quantity'],
Configuration::get('PS_STOCK_CUSTOMER_ORDER_REASON'),
true,
(int)$id_order
);
if (StockAvailable::dependsOnStock($product['product_id'], $order->id_shop))
StockAvailable::synchronize($product['product_id']);
else
StockAvailable::updateQuantity($product['product_id'], $product['product_attribute_id'], -(int)$product['product_quantity'], $order->id_shop);
}
else if ($newOS->shipped == 0
&& $oldOrderStatus instanceof OrderState
&& $oldOrderStatus->shipped == 1
&& Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT')
&& (int)$product['advanced_stock_management'] == 1
)
{
$manager = StockManagerFactory::getManager();
$mvts = StockMvt::getNegativeStockMvts($order->id, $product['product_id'], $product['product_attribute_id'], $product['product_quantity']);
foreach ($mvts as $mvt)
{
$manager->addProduct(
$product['product_id'],
$product['product_attribute_id'],
new Warehouse($mvt['id_warehouse']),
$mvt['physical_quantity'],
null,
$mvt['price_te'],
true
);
}
if (StockAvailable::dependsOnStock($product['product_id'], $order->id_shop))
StockAvailable::synchronize($product['product_id']);
else
StockAvailable::updateQuantity($product['product_id'], $product['product_attribute_id'], (int)$product['physical_quantity'], $order->id_shop);
}
}
$this->id_order_state = (int)($new_order_state);
/* Change invoice number of order ? */
if (!Validate::isLoadedObject($newOS) OR !Validate::isLoadedObject($order))
die(Tools::displayError('Invalid new order state'));
/* The order is valid only if the invoice is available and the order is not cancelled */
$order->valid = $newOS->logable;
$order->update();
if ($newOS->invoice AND !$order->invoice_number)
$order->setInvoice();
// Set order as paid
if ($newOS->paid == 1)
{
$invoices = $order->getInvoicesCollection();
$payment_method = Module::getInstanceByName($order->module);
foreach ($invoices as $invoice)
if ((!Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT') || (int)$product['advanced_stock_management'] != 1)
&& !$isValidated
&& $newOS->logable
&& $oldOrderStatus instanceof OrderState
&& $oldOrderStatus->id == Configuration::get('PS_OS_ERROR')
)
StockAvailable::updateQuantity($product['product_id'], $product['product_attribute_id'], (int)$product['product_quantity'], $order->id_shop);
// 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 instanceof OrderState
&& $oldOrderStatus->shipped == 0
&& Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT')
&& (int)$product['advanced_stock_management'] == 1)
{
$rest_paid = $invoice->getRestPaid();
if ($rest_paid)
$manager = StockManagerFactory::getManager();
$warehouse = new Warehouse($product['id_warehouse']);
$manager->removeProduct(
$product['product_id'],
$product['product_attribute_id'],
$warehouse,
$product['product_quantity'],
Configuration::get('PS_STOCK_CUSTOMER_ORDER_REASON'),
true,
(int)$id_order
);
if (StockAvailable::dependsOnStock($product['product_id'], $order->id_shop))
StockAvailable::synchronize($product['product_id']);
else
StockAvailable::updateQuantity($product['product_id'], $product['product_attribute_id'], -(int)$product['product_quantity'], $order->id_shop);
}
else if ($newOS->shipped == 0
&& $oldOrderStatus instanceof OrderState
&& $oldOrderStatus->shipped == 1
&& Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT')
&& (int)$product['advanced_stock_management'] == 1
)
{
$manager = StockManagerFactory::getManager();
$mvts = StockMvt::getNegativeStockMvts($order->id, $product['product_id'], $product['product_attribute_id'], $product['product_quantity']);
foreach ($mvts as $mvt)
{
$payment = new OrderPayment();
$payment->id_order = $order->id;
$payment->id_order_invoice = $invoice->id;
$payment->id_currency = $order->id_currency;
$payment->amount = $rest_paid;
$payment->payment_method = $payment_method->displayName;
$payment->conversion_rate = 1;
$payment->save();
$manager->addProduct(
$product['product_id'],
$product['product_attribute_id'],
new Warehouse($mvt['id_warehouse']),
$mvt['physical_quantity'],
null,
$mvt['price_te'],
true
);
}
if (StockAvailable::dependsOnStock($product['product_id'], $order->id_shop))
StockAvailable::synchronize($product['product_id']);
else
StockAvailable::updateQuantity($product['product_id'], $product['product_attribute_id'], (int)$product['physical_quantity'], $order->id_shop);
}
}
// Update delivery date even if it was already set by another state change
if ($newOS->delivery)
$order->setDelivery();
Hook::postUpdateOrderStatus((int)($new_order_state), (int)($id_order));
$this->id_order_state = (int)($new_order_state);
/* Change invoice number of order ? */
if (!Validate::isLoadedObject($newOS) OR !Validate::isLoadedObject($order))
die(Tools::displayError('Invalid new order state'));
/* The order is valid only if the invoice is available and the order is not cancelled */
$order->valid = $newOS->logable;
$order->update();
if ($newOS->invoice AND !$order->invoice_number)
$order->setInvoice();
// Set order as paid
if ($newOS->paid == 1)
{
$invoices = $order->getInvoicesCollection();
$payment_method = Module::getInstanceByName($order->module);
foreach ($invoices as $invoice)
{
$rest_paid = $invoice->getRestPaid();
if ($rest_paid)
{
$payment = new OrderPayment();
$payment->id_order = $order->id;
$payment->id_order_invoice = $invoice->id;
$payment->id_currency = $order->id_currency;
$payment->amount = $rest_paid;
$payment->payment_method = $payment_method->displayName;
$payment->conversion_rate = 1;
$payment->save();
}
}
}
// Update delivery date even if it was already set by another state change
if ($newOS->delivery)
$order->setDelivery();
Hook::exec('actionOrderStatusPostUpdate', array(
'newOrderStatus' => $newOS,
'id_order' => (int)$order->id,
));
}
public static function getLastOrderState($id_order)