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

Implement discounts management in Order View page #15901

Merged
merged 28 commits into from
Oct 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
effae80
Implement updating order products
sarjon Oct 3, 2019
710a39a
Fix updating product in order
sarjon Oct 4, 2019
4e25db8
Disabling product modification when order is delivered
sarjon Oct 4, 2019
2661011
Add order prices dto
sarjon Oct 4, 2019
c1959dd
Fix syntax
sarjon Oct 4, 2019
e767b5e
Add order prices display
sarjon Oct 7, 2019
2dd27b3
Highlight product quantity
sarjon Oct 7, 2019
789d434
Add discounts list
sarjon Oct 7, 2019
3e1b768
Add removing cart rule from order functionality
sarjon Oct 7, 2019
02be2da
Update discount form with invoice data for orders with invoices
sarjon Oct 7, 2019
790319f
Add cart rule form toggling
sarjon Oct 8, 2019
0a334cd
Improvements to adding cart rule to order
sarjon Oct 8, 2019
d3f97ba
Add help text for discounts of type amount
sarjon Oct 10, 2019
c903972
Implement validation for Add cart rule action
sarjon Oct 10, 2019
2cfc8b4
Apply CS fixer
sarjon Oct 10, 2019
d65521e
Use discount type consts in JS
sarjon Oct 11, 2019
b97cae1
Naming update
sarjon Oct 11, 2019
c518ecb
Update form type naming
sarjon Oct 11, 2019
638f1fd
Update src/PrestaShopBundle/Resources/views/Admin/Sell/Order/Order/Bl…
sarjon Oct 11, 2019
9d9a7cd
Update src/PrestaShopBundle/Resources/views/Admin/Sell/Order/Order/Bl…
sarjon Oct 11, 2019
c4f4c6f
Update src/PrestaShopBundle/Resources/views/Admin/Sell/Order/Order/Bl…
sarjon Oct 11, 2019
99ecb2c
Update src/PrestaShopBundle/Resources/views/Admin/Sell/Order/Order/Bl…
sarjon Oct 11, 2019
d643ee2
Update src/PrestaShopBundle/Resources/views/Admin/Sell/Order/Order/Bl…
sarjon Oct 11, 2019
f90fcbe
Update src/PrestaShopBundle/Resources/views/Admin/Sell/Order/Order/Bl…
sarjon Oct 11, 2019
1738889
Rebase fixes
sarjon Oct 14, 2019
d2c12fe
Apply CS fixer
sarjon Oct 14, 2019
bd91ed6
Update wording
sarjon Oct 16, 2019
cadea7e
Remove unused form
sarjon Oct 17, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions admin-dev/themes/new-theme/js/pages/order/OrderViewPageMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,14 @@ export default {
privateNoteBlock: '.js-private-note-block',
privateNoteInput: '#private_note_note',
privateNoteSubmitBtn: '.js-private-note-btn',
updateOrderProductModal: '#updateOrderProductModal',
updateOrderProductPriceTaxExclInput: '#update_order_product_price_tax_excl',
updateOrderProductPriceTaxInclInput: '#update_order_product_price_tax_incl',
updateOrderProductQuantityInput: '#update_order_product_quantity',
addCartRuleModal: '#addOrderDiscountModal',
addCartRuleApplyOnAllInvoicesCheckbox: '#add_order_cart_rule_apply_on_all_invoices',
addCartRuleInvoiceIdSelect: '#add_order_cart_rule_invoice_id',
addCartRuleTypeSelect: '#add_order_cart_rule_type',
addCartRuleValueInput: '#add_order_cart_rule_value',
cartRuleHelpText: '.js-cart-rule-value-help',
};
54 changes: 54 additions & 0 deletions admin-dev/themes/new-theme/js/pages/order/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ import OrderViewPageMap from './OrderViewPageMap';
const $ = window.$;

$(() => {
const DISCOUNT_TYPE_AMOUNT = 'amount';
const DISCOUNT_TYPE_PERCENT = 'percent';
const DISCOUNT_TYPE_FREE_SHIPPING = 'free_shipping';

handlePaymentDetailsToggle();
handlePrivateNoteChange();

Expand All @@ -36,6 +40,9 @@ $(() => {
togglePrivateNoteBlock();
});

initAddCartRuleFormHandler();
initAddProductFormHandler();

function handlePaymentDetailsToggle() {
$(OrderViewPageMap.orderPaymentDetailsBtn).on('click', (event) => {
const $paymentDetailRow = $(event.currentTarget).closest('tr').next(':first');
Expand Down Expand Up @@ -69,4 +76,51 @@ $(() => {
$submitBtn.prop('disabled', !note);
});
}

function initAddProductFormHandler() {
const $modal = $(OrderViewPageMap.updateOrderProductModal);

$modal.on('click', '.js-order-product-update-btn', (event) => {
const $btn = $(event.currentTarget);

$modal.find('.js-update-product-name').text($btn.data('product-name'));
$modal.find(OrderViewPageMap.updateOrderProductPriceTaxExclInput).val($btn.data('product-price-tax-excl'));
$modal.find(OrderViewPageMap.updateOrderProductPriceTaxInclInput).val($btn.data('product-price-tax-incl'));
$modal.find(OrderViewPageMap.updateOrderProductQuantityInput).val($btn.data('product-quantity'));
$modal.find('form').attr('action', $btn.data('update-url'));
});
}

function initAddCartRuleFormHandler() {
const $modal = $(OrderViewPageMap.addCartRuleModal);
const $form = $modal.find('form');
const $valueHelp = $modal.find(OrderViewPageMap.cartRuleHelpText);
const $invoiceSelect = $modal.find(OrderViewPageMap.addCartRuleInvoiceIdSelect);
const $valueInput = $form.find(OrderViewPageMap.addCartRuleValueInput);
const $valueFormGroup = $valueInput.closest('.form-group');

$form.find(OrderViewPageMap.addCartRuleApplyOnAllInvoicesCheckbox).on('change', (event) => {
const isChecked = $(event.currentTarget).is(':checked');

$invoiceSelect.attr('disabled', isChecked);
});

$form.find(OrderViewPageMap.addCartRuleTypeSelect).on('change', (event) => {
const selectedCartRuleType = $(event.currentTarget).val();

if (selectedCartRuleType === DISCOUNT_TYPE_AMOUNT) {
$valueHelp.removeClass('d-none');
} else {
$valueHelp.addClass('d-none');
}

if (selectedCartRuleType === DISCOUNT_TYPE_FREE_SHIPPING) {
$valueFormGroup.addClass('d-none');
$valueInput.attr('disabled', true);
} else {
$valueFormGroup.removeClass('d-none');
$valueInput.attr('disabled', false);
}
});
}
});
10 changes: 5 additions & 5 deletions src/Adapter/Order/CommandHandler/AddCartRuleToOrderHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ final class AddCartRuleToOrderHandler extends AbstractOrderHandler implements Ad
/**
* {@inheritdoc}
*/
public function handle(AddCartRuleToOrderCommand $command)
public function handle(AddCartRuleToOrderCommand $command): void
{
$order = $this->getOrderObject($command->getOrderId());

// If the discount is for only one invoice
if ($order->hasInvoice() && null === $command->getOrderInvoiceId()) {
$orderInvoice = new OrderInvoice($command->getOrderInvoiceId());
if ($order->hasInvoice() && null !== $command->getOrderInvoiceId()) {
$orderInvoice = new OrderInvoice($command->getOrderInvoiceId()->getValue());
if (!Validate::isLoadedObject($orderInvoice)) {
throw new OrderException('Can\'t load Order Invoice object');
}
Expand Down Expand Up @@ -111,7 +111,7 @@ public function handle(AddCartRuleToOrderCommand $command)
);
}
} else {
throw new OrderException('The discount value is invalid.');
throw new OrderException('Percentage discount value cannot be higher than 100%.');
}

break;
Expand Down Expand Up @@ -215,7 +215,7 @@ public function handle(AddCartRuleToOrderCommand $command)
$cartRuleObj = new CartRule();
$cartRuleObj->date_from = date('Y-m-d H:i:s', strtotime('-1 hour', strtotime($order->date_add)));
$cartRuleObj->date_to = date('Y-m-d H:i:s', strtotime('+1 hour'));
$cartRuleObj->name[Configuration::get('PS_LANG_DEFAULT')] = Tools::getValue('discount_name');
$cartRuleObj->name[Configuration::get('PS_LANG_DEFAULT')] = $command->getCartRuleName();
$cartRuleObj->quantity = 0;
$cartRuleObj->quantity_per_user = 1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use OrderDetail;
use OrderInvoice;
use PrestaShop\PrestaShop\Adapter\Order\AbstractOrderHandler;
use PrestaShop\PrestaShop\Core\Domain\Order\Exception\CannotEditDeliveredOrderProductException;
use PrestaShop\PrestaShop\Core\Domain\Order\Product\Command\UpdateProductInOrderCommand;
use PrestaShop\PrestaShop\Core\Domain\Order\Product\CommandHandler\UpdateProductInOrderHandlerInterface;
use PrestaShop\PrestaShop\Core\Domain\Order\Exception\OrderException;
Expand Down Expand Up @@ -79,8 +80,8 @@ public function handle(UpdateProductInOrderCommand $command)
$product_quantity = $command->getQuantity();

// @todo: use https://github.com/PrestaShop/decimal for price computations
$product_price_tax_incl = Tools::ps_round(Tools::getValue('product_price_tax_incl'), 2);
$product_price_tax_excl = Tools::ps_round(Tools::getValue('product_price_tax_excl'), 2);
$product_price_tax_incl = Tools::ps_round($command->getPriceTaxIncluded(), 2);
matks marked this conversation as resolved.
Show resolved Hide resolved
$product_price_tax_excl = Tools::ps_round($command->getPriceTaxExcluded(), 2);
$total_products_tax_incl = $product_price_tax_incl * $product_quantity;
$total_products_tax_excl = $product_price_tax_excl * $product_quantity;

Expand Down Expand Up @@ -215,7 +216,7 @@ private function assertProductCanBeUpdated(

// We can't edit a delivered order
if ($order->hasBeenDelivered()) {
throw new OrderException('You cannot edit a delivered order.');
throw new CannotEditDeliveredOrderProductException('You cannot edit a delivered order.');
}

if (null !== $orderInvoice && $orderInvoice->id_order != $order->id) {
Expand All @@ -233,7 +234,7 @@ private function assertProductCanBeUpdated(
}

if (!is_array($command->getQuantity())
&& !Validate::isUnsignedInt(Tools::getValue('product_quantity'))
&& !Validate::isUnsignedInt($command->getQuantity())
) {
throw new OrderException('Invalid quantity');
}
Expand Down
91 changes: 86 additions & 5 deletions src/Adapter/Order/QueryHandler/GetOrderForViewingHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
use PrestaShop\PrestaShop\Core\Domain\Order\QueryHandler\GetOrderForViewingHandlerInterface;
use PrestaShop\PrestaShop\Core\Domain\Order\QueryResult\OrderCarrierForViewing;
use PrestaShop\PrestaShop\Core\Domain\Order\QueryResult\OrderCustomerForViewing;
use PrestaShop\PrestaShop\Core\Domain\Order\QueryResult\OrderDiscountForViewing;
use PrestaShop\PrestaShop\Core\Domain\Order\QueryResult\OrderDiscountsForViewing;
use PrestaShop\PrestaShop\Core\Domain\Order\QueryResult\OrderDocumentForViewing;
use PrestaShop\PrestaShop\Core\Domain\Order\QueryResult\OrderDocumentsForViewing;
use PrestaShop\PrestaShop\Core\Domain\Order\QueryResult\OrderForViewing;
Expand All @@ -60,6 +62,7 @@
use PrestaShop\PrestaShop\Core\Domain\Order\QueryResult\OrderMessagesForViewing;
use PrestaShop\PrestaShop\Core\Domain\Order\QueryResult\OrderPaymentForViewing;
use PrestaShop\PrestaShop\Core\Domain\Order\QueryResult\OrderPaymentsForViewing;
use PrestaShop\PrestaShop\Core\Domain\Order\QueryResult\OrderPricesForViewing;
use PrestaShop\PrestaShop\Core\Domain\Order\QueryResult\OrderProductForViewing;
use PrestaShop\PrestaShop\Core\Domain\Order\QueryResult\OrderProductsForViewing;
use PrestaShop\PrestaShop\Core\Domain\Order\QueryResult\OrderReturnForViewing;
Expand Down Expand Up @@ -122,16 +125,21 @@ public function handle(GetOrderForViewing $query): OrderForViewing
{
$order = $this->getOrder($query->getOrderId());

$taxMethod = $order->getTaxCalculationMethod() == PS_TAX_EXC ?
$this->translator->trans('Tax excluded', [], 'Admin.Global') :
$this->translator->trans('Tax included', [], 'Admin.Global');
$isTaxIncluded = $order->getTaxCalculationMethod() == PS_TAX_INC;

$taxMethod = $isTaxIncluded ?
$this->translator->trans('Tax included', [], 'Admin.Global') :
$this->translator->trans('Tax excluded', [], 'Admin.Global');
matks marked this conversation as resolved.
Show resolved Hide resolved

return new OrderForViewing(
(int) $order->id,
(int) $order->id_currency,
$order->reference,
$taxMethod,
$isTaxIncluded,
(bool) $order->valid,
$order->hasInvoice(),
$order->hasBeenDelivered(),
$this->getOrderCustomer($order),
$this->getOrderShippingAddress($order),
$this->getOrderInvoiceAddress($order),
Expand All @@ -141,7 +149,9 @@ public function handle(GetOrderForViewing $query): OrderForViewing
$this->getOrderShipping($order),
$this->getOrderReturns($order),
$this->getOrderPayments($order),
$this->getOrderMessages($order)
$this->getOrderMessages($order),
$this->getOrderPrices($order),
$this->getOrderDiscounts($order)
);
}

Expand Down Expand Up @@ -379,6 +389,7 @@ private function getOrderProducts(Order $order): OrderProductsForViewing
$totalPriceFormatted = Tools::displayPrice($totalPrice, $currency);

$productsForViewing[] = new OrderProductForViewing(
$product['id_order_detail'],
$product['product_id'],
$product['product_name'],
$product['reference'],
Expand All @@ -387,7 +398,9 @@ private function getOrderProducts(Order $order): OrderProductsForViewing
$unitPriceFormatted,
$totalPriceFormatted,
$product['current_stock'],
$this->imageTagSourceParser->parse($product['image_tag'])
$this->imageTagSourceParser->parse($product['image_tag']),
Tools::ps_round($product['unit_price_tax_excl'], 2),
Tools::ps_round($product['unit_price_tax_incl'], 2)
);
}

Expand Down Expand Up @@ -707,4 +720,72 @@ private function getOrderMessages(Order $order): OrderMessagesForViewing

return new OrderMessagesForViewing($messages);
}

private function getOrderPrices(Order $order): OrderPricesForViewing
{
$currency = new Currency($order->id_currency);
$customer = $order->getCustomer();

$isTaxExcluded = $order->getTaxCalculationMethod() == PS_TAX_EXC;

$shipping_refundable_tax_excl = $order->total_shipping_tax_excl;
$shipping_refundable_tax_incl = $order->total_shipping_tax_incl;
$slips = OrderSlip::getOrdersSlip($customer->id, $order->id);
foreach ($slips as $slip) {
$shipping_refundable_tax_excl -= $slip['total_shipping_tax_excl'];
$shipping_refundable_tax_incl -= $slip['total_shipping_tax_incl'];
}

if ($isTaxExcluded) {
$productsPrice = (float) $order->total_products;
$discountsAmount = (float) $order->total_discounts_tax_excl;
$wrappingPrice = (float) $order->total_wrapping_tax_excl;
$shippingPrice = (float) $order->total_shipping_tax_excl;
$shippingRefundable = max(0, $shipping_refundable_tax_excl);
} else {
$productsPrice = (float) $order->total_products_wt;
$discountsAmount = (float) $order->total_discounts_tax_incl;
$wrappingPrice = (float) $order->total_wrapping_tax_incl;
$shippingPrice = (float) $order->total_shipping_tax_incl;
$shippingRefundable = max(0, $shipping_refundable_tax_incl);
}

$taxesAmount = $order->total_paid_tax_incl - $order->total_paid_tax_excl;
$totalAmount = (float) $order->total_paid_tax_incl;

return new OrderPricesForViewing(
$productsPrice,
$discountsAmount,
$wrappingPrice,
$shippingPrice,
$shippingRefundable,
$taxesAmount,
$totalAmount,
Tools::displayPrice($productsPrice, $currency),
Tools::displayPrice($discountsAmount, $currency),
Tools::displayPrice($wrappingPrice, $currency),
Tools::displayPrice($shippingPrice, $currency),
Tools::displayPrice($shippingRefundable, $currency),
Tools::displayPrice($taxesAmount, $currency),
Tools::displayPrice($totalAmount, $currency)
);
}

private function getOrderDiscounts(Order $order): OrderDiscountsForViewing
{
$currency = new Currency($order->id_currency);
$discounts = $order->getCartRules();
$discountsForViewing = [];

foreach ($discounts as $discount) {
$discountsForViewing[] = new OrderDiscountForViewing(
(int) $discount['id_cart_rule'],
$discount['name'],
(float) $discount['value'],
Tools::displayPrice($discount['value'], $currency)
);
}

return new OrderDiscountsForViewing($discountsForViewing);
}
}
Loading