diff --git a/classes/order/OrderHistory.php b/classes/order/OrderHistory.php index ce87b8138..3e2cb1574 100644 --- a/classes/order/OrderHistory.php +++ b/classes/order/OrderHistory.php @@ -160,108 +160,109 @@ class OrderHistoryCore extends ObjectModel if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT')) $manager = StockManagerFactory::getManager(); // foreach products of the order - foreach ($order->getProductsDetail() as $product) - { - // if becoming logable => adds sale - if ($new_os->logable && !$old_os->logable) + if (Validate::isLoadedObject($old_os)) + foreach ($order->getProductsDetail() as $product) { - ProductSale::addProductSale($product['product_id'], $product['product_quantity']); - // @since 1.5.0 - Stock Management - if (!Pack::isPack($product['product_id']) && - ($old_os->id == Configuration::get('PS_OS_ERROR') || $old_os->id == Configuration::get('PS_OS_CANCELED')) && - !StockAvailable::dependsOnStock($product['id_product'], (int)$order->id_shop)) - StockAvailable::updateQuantity($product['product_id'], $product['product_attribute_id'], -(int)$product['product_quantity'], $order->id_shop); - } - // if becoming unlogable => removes sale - elseif (!$new_os->logable && $old_os->logable) - { - ProductSale::removeProductSale($product['product_id'], $product['product_quantity']); - - // @since 1.5.0 - Stock Management - if (!Pack::isPack($product['product_id']) && - ($new_os->id == Configuration::get('PS_OS_ERROR') || $new_os->id == Configuration::get('PS_OS_CANCELED')) && - !StockAvailable::dependsOnStock($product['id_product'])) - StockAvailable::updateQuantity($product['product_id'], $product['product_attribute_id'], (int)$product['product_quantity'], $order->id_shop); - } - // if waiting for payment => payment error/canceled - elseif (!$new_os->logable && !$old_os->logable && - ($new_os->id == Configuration::get('PS_OS_ERROR') || $new_os->id == Configuration::get('PS_OS_CANCELED')) && - !StockAvailable::dependsOnStock($product['id_product'])) - StockAvailable::updateQuantity($product['product_id'], $product['product_attribute_id'], (int)$product['product_quantity'], $order->id_shop); - // @since 1.5.0 : if the order is being shipped and this products uses the advanced stock management : - // decrements the physical stock using $id_warehouse - if ($new_os->shipped == 1 && $old_os->shipped == 0 && - Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT') && - Warehouse::exists($product['id_warehouse']) && - $manager != null && - ((int)$product['advanced_stock_management'] == 1 || Pack::usesAdvancedStockManagement($product['product_id']))) - { - // gets the warehouse - $warehouse = new Warehouse($product['id_warehouse']); - - // decrements the stock (if it's a pack, the StockManager does what is needed) - $manager->removeProduct( - $product['product_id'], - $product['product_attribute_id'], - $warehouse, - $product['product_quantity'], - Configuration::get('PS_STOCK_CUSTOMER_ORDER_REASON'), - true, - (int)$order->id - ); - } - // @since.1.5.0 : if the order was shipped, and is not anymore, we need to restock products - elseif ($new_os->shipped == 0 && $old_os->shipped == 1 && - Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT') && - Warehouse::exists($product['id_warehouse']) && - $manager != null && - ((int)$product['advanced_stock_management'] == 1 || Pack::usesAdvancedStockManagement($product['product_id']))) - { - // if the product is a pack, we restock every products in the pack using the last negative stock mvts - if (Pack::isPack($product['product_id'])) + // if becoming logable => adds sale + if ($new_os->logable && !$old_os->logable) { - $pack_products = Pack::getItems($product['product_id'], Configuration::get('PS_LANG_DEFAULT', null, null, $order->id_shop)); - foreach ($pack_products as $pack_product) + ProductSale::addProductSale($product['product_id'], $product['product_quantity']); + // @since 1.5.0 - Stock Management + if (!Pack::isPack($product['product_id']) && + ($old_os->id == Configuration::get('PS_OS_ERROR') || $old_os->id == Configuration::get('PS_OS_CANCELED')) && + !StockAvailable::dependsOnStock($product['id_product'], (int)$order->id_shop)) + StockAvailable::updateQuantity($product['product_id'], $product['product_attribute_id'], -(int)$product['product_quantity'], $order->id_shop); + } + // if becoming unlogable => removes sale + elseif (!$new_os->logable && $old_os->logable) + { + ProductSale::removeProductSale($product['product_id'], $product['product_quantity']); + + // @since 1.5.0 - Stock Management + if (!Pack::isPack($product['product_id']) && + ($new_os->id == Configuration::get('PS_OS_ERROR') || $new_os->id == Configuration::get('PS_OS_CANCELED')) && + !StockAvailable::dependsOnStock($product['id_product'])) + StockAvailable::updateQuantity($product['product_id'], $product['product_attribute_id'], (int)$product['product_quantity'], $order->id_shop); + } + // if waiting for payment => payment error/canceled + elseif (!$new_os->logable && !$old_os->logable && + ($new_os->id == Configuration::get('PS_OS_ERROR') || $new_os->id == Configuration::get('PS_OS_CANCELED')) && + !StockAvailable::dependsOnStock($product['id_product'])) + StockAvailable::updateQuantity($product['product_id'], $product['product_attribute_id'], (int)$product['product_quantity'], $order->id_shop); + // @since 1.5.0 : if the order is being shipped and this products uses the advanced stock management : + // decrements the physical stock using $id_warehouse + if ($new_os->shipped == 1 && $old_os->shipped == 0 && + Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT') && + Warehouse::exists($product['id_warehouse']) && + $manager != null && + ((int)$product['advanced_stock_management'] == 1 || Pack::usesAdvancedStockManagement($product['product_id']))) + { + // gets the warehouse + $warehouse = new Warehouse($product['id_warehouse']); + + // decrements the stock (if it's a pack, the StockManager does what is needed) + $manager->removeProduct( + $product['product_id'], + $product['product_attribute_id'], + $warehouse, + $product['product_quantity'], + Configuration::get('PS_STOCK_CUSTOMER_ORDER_REASON'), + true, + (int)$order->id + ); + } + // @since.1.5.0 : if the order was shipped, and is not anymore, we need to restock products + elseif ($new_os->shipped == 0 && $old_os->shipped == 1 && + Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT') && + Warehouse::exists($product['id_warehouse']) && + $manager != null && + ((int)$product['advanced_stock_management'] == 1 || Pack::usesAdvancedStockManagement($product['product_id']))) + { + // if the product is a pack, we restock every products in the pack using the last negative stock mvts + if (Pack::isPack($product['product_id'])) { - if ($pack_product->advanced_stock_management == 1) + $pack_products = Pack::getItems($product['product_id'], Configuration::get('PS_LANG_DEFAULT', null, null, $order->id_shop)); + foreach ($pack_products as $pack_product) { - $mvts = StockMvt::getNegativeStockMvts($order->id, $pack_product->id, 0, $pack_product->pack_quantity * $product['product_quantity']); - foreach ($mvts as $mvt) + if ($pack_product->advanced_stock_management == 1) { - $manager->addProduct( - $pack_product->id, - 0, - new Warehouse($mvt['id_warehouse']), - $mvt['physical_quantity'], - null, - $mvt['price_te'], - true - ); + $mvts = StockMvt::getNegativeStockMvts($order->id, $pack_product->id, 0, $pack_product->pack_quantity * $product['product_quantity']); + foreach ($mvts as $mvt) + { + $manager->addProduct( + $pack_product->id, + 0, + new Warehouse($mvt['id_warehouse']), + $mvt['physical_quantity'], + null, + $mvt['price_te'], + true + ); + } + if (!StockAvailable::dependsOnStock($product['id_product'])) + StockAvailable::updateQuantity($pack_product->id, 0, (int)$pack_product->pack_quantity * $product['product_quantity'], $order->id_shop); } - if (!StockAvailable::dependsOnStock($product['id_product'])) - StockAvailable::updateQuantity($pack_product->id, 0, (int)$pack_product->pack_quantity * $product['product_quantity'], $order->id_shop); + } + } + // else, it's not a pack, re-stock using the last negative stock mvts + else + { + $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 + ); } } } - // else, it's not a pack, re-stock using the last negative stock mvts - else - { - $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 - ); - } - } } - } } $this->id_order_state = (int)$new_order_state;