Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare Command API for migrating Order view page #13712

Merged
merged 31 commits into from Jul 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
7ef1e87
Adds AddProductToOrder command/handler interface
sarjon May 3, 2019
6f3050e
Adds part of implementation for adding product to exiting order
sarjon May 6, 2019
c48314a
Finishes AddProductToOrder command/handler
sarjon May 6, 2019
6dc6cbe
Adds Behat tests for adding product to an existing order
sarjon May 7, 2019
7cce8ea
Adds DeleteProductFromOrder command/handler
sarjon May 7, 2019
3e5cdfe
Adds UpdateOrderStatus command/handler
sarjon May 7, 2019
b6181da
Adds UpdateProductInOrder command/handler
sarjon May 8, 2019
999226c
Adds ChangeOrderDeliveryAddress command/handler
sarjon May 8, 2019
af533cb
Adds ChangeOrderInvoiceAddress command/handler
sarjon May 8, 2019
43ac564
Adds ChangeOrderCurrency command/handler
sarjon May 8, 2019
baa832e
Adds AddOrderPayment command/handler
sarjon May 8, 2019
e4edf32
Adds GenerateOrderInvoice command/handler
sarjon May 8, 2019
860feee
Adds behat tests for generating order invoice
sarjon May 8, 2019
1cdbde6
Adds UpdateOrderShippingDetails command/handler
sarjon May 9, 2019
d5533a4
Adds RemoveCartRuleFromOrder command/handler
sarjon May 9, 2019
1ef830a
Adds AddCartRuleToOrder command/handler
sarjon May 9, 2019
59890f2
Fixes typo
sarjon May 9, 2019
68fd300
Renames RemoveCartRuleFromOrder to DeleteCartRuleFromOrder
sarjon May 9, 2019
d848dd7
Adds UpdateInvoiceNote command/handler
sarjon May 13, 2019
c0d76a4
Adds IssuePartialRefund command/handler
sarjon May 13, 2019
5eb0e4d
Moves GenerateOrderInvoice under Order/Invoice namespace
sarjon May 13, 2019
5cc3bc8
Updates naming
sarjon May 13, 2019
0f9d878
Moves AddPayment to Order/Payment subdomain
sarjon May 13, 2019
6ca1904
Fixes namespace
sarjon May 13, 2019
660e459
Adds Product sumbdomain for Order
sarjon May 15, 2019
23765d1
Applies CS fixer
sarjon May 24, 2019
5706c91
Applies CS fixer on tests
sarjon May 24, 2019
a465fb7
Applies minor fixes
sarjon May 27, 2019
562c4cc
Applies CS fixer
sarjon May 27, 2019
d74831d
Address @matks review comments
sarjon Jun 12, 2019
6731f80
Handle Add to Cart failure usecase where previous cart rules were emp…
matks Jul 19, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/Adapter/Order/AbstractOrderHandler.php
@@ -0,0 +1,55 @@
<?php
/**
* 2007-2019 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2019 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/

namespace PrestaShop\PrestaShop\Adapter\Order;

use Order;
use PrestaShop\PrestaShop\Core\Domain\Order\Exception\OrderNotFoundException;
use PrestaShop\PrestaShop\Core\Domain\Order\ValueObject\OrderId;

/**
* Reusable methods for Order subdomain command/query handlers.
*/
abstract class AbstractOrderHandler
{
/**
* @param OrderId $orderId
*
* @return Order
*/
protected function getOrderObject(OrderId $orderId)
{
$order = new Order($orderId->getValue());

if ($order->id !== $orderId->getValue()) {
throw new OrderNotFoundException(
sprintf('Order with id "%d" was not found.', $orderId->getValue())
);
}

return $order;
}
}
174 changes: 174 additions & 0 deletions src/Adapter/Order/CommandHandler/AbstractOrderCommandHandler.php
@@ -0,0 +1,174 @@
<?php
/**
* 2007-2019 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2019 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/

namespace PrestaShop\PrestaShop\Adapter\Order\CommandHandler;

use Configuration;
use Context;
use OrderDetail;
use Pack;
use PrestaShop\PrestaShop\Adapter\Order\AbstractOrderHandler;
use PrestaShop\PrestaShop\Adapter\StockManager;
use PrestaShop\PrestaShop\Core\Domain\Order\Exception\OrderException;
use Product;
use StockAvailable;
use StockManagerFactory;
use StockMvt;
use Warehouse;

/**
* Abstracts reusable functionality for Order subdomain handlers.
*
* @internal
*/
abstract class AbstractOrderCommandHandler extends AbstractOrderHandler
{
/**
* @param OrderDetail $orderDetail
* @param int $productQuantity
* @param bool $delete
*/
protected function reinjectQuantity(OrderDetail $orderDetail, $productQuantity, $delete = false)
matks marked this conversation as resolved.
Show resolved Hide resolved
{
// Reinject product
$reinjectableQuantity = (int) $orderDetail->product_quantity - (int) $orderDetail->product_quantity_reinjected;
$quantityToReinject = $productQuantity > $reinjectableQuantity ? $reinjectableQuantity : $productQuantity;

$product = new Product(
$orderDetail->product_id,
false,
(int) Context::getContext()->language->id,
(int) $orderDetail->id_shop
);

if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT')
&& $product->advanced_stock_management
&& $orderDetail->id_warehouse != 0
) {
$manager = StockManagerFactory::getManager();
$movements = StockMvt::getNegativeStockMvts(
$orderDetail->id_order,
$orderDetail->product_id,
$orderDetail->product_attribute_id,
$quantityToReinject
);

$leftToReinject = $quantityToReinject;
foreach ($movements as $movement) {
if ($leftToReinject > $movement['physical_quantity']) {
$quantityToReinject = $movement['physical_quantity'];
}

$leftToReinject -= $quantityToReinject;
if (Pack::isPack((int) $product->id)) {
// Gets items
if ($product->pack_stock_type == Pack::STOCK_TYPE_PRODUCTS_ONLY
|| $product->pack_stock_type == Pack::STOCK_TYPE_PACK_BOTH
|| ($product->pack_stock_type == Pack::STOCK_TYPE_DEFAULT
&& Configuration::get('PS_PACK_STOCK_TYPE') > 0)
) {
$products_pack = Pack::getItems((int) $product->id, (int) Configuration::get('PS_LANG_DEFAULT'));
// Foreach item
foreach ($products_pack as $product_pack) {
if ($product_pack->advanced_stock_management == 1) {
$manager->addProduct(
$product_pack->id,
$product_pack->id_pack_product_attribute,
new Warehouse($movement['id_warehouse']),
$product_pack->pack_quantity * $quantityToReinject,
null,
$movement['price_te']
);
}
}
}

if ($product->pack_stock_type == Pack::STOCK_TYPE_PACK_ONLY
|| $product->pack_stock_type == Pack::STOCK_TYPE_PACK_BOTH
|| (
$product->pack_stock_type == Pack::STOCK_TYPE_DEFAULT
&& (Configuration::get('PS_PACK_STOCK_TYPE') == Pack::STOCK_TYPE_PACK_ONLY
|| Configuration::get('PS_PACK_STOCK_TYPE') == Pack::STOCK_TYPE_PACK_BOTH)
)
) {
$manager->addProduct(
$orderDetail->product_id,
$orderDetail->product_attribute_id,
new Warehouse($movement['id_warehouse']),
$quantityToReinject,
null,
$movement['price_te']
);
}
} else {
$manager->addProduct(
$orderDetail->product_id,
$orderDetail->product_attribute_id,
new Warehouse($movement['id_warehouse']),
$quantityToReinject,
null,
$movement['price_te']
);
}
}

$productId = $orderDetail->product_id;

if ($delete) {
$orderDetail->delete();
}

StockAvailable::synchronize($productId);
} elseif ($orderDetail->id_warehouse == 0) {
StockAvailable::updateQuantity(
$orderDetail->product_id,
$orderDetail->product_attribute_id,
$quantityToReinject,
$orderDetail->id_shop,
true,
[
'id_order' => $orderDetail->id_order,
'id_stock_mvt_reason' => Configuration::get('PS_STOCK_CUSTOMER_RETURN_REASON'),
]
);

// sync all stock
(new StockManager())->updatePhysicalProductQuantity(
(int) $orderDetail->id_shop,
(int) Configuration::get('PS_OS_ERROR'),
(int) Configuration::get('PS_OS_CANCELED'),
null,
(int) $orderDetail->id_order
);

if ($delete) {
$orderDetail->delete();
}
} else {
throw new OrderException('This product cannot be re-stocked.');
}
}
}