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

Migrate Payment block of Order view page #15778

Merged
merged 13 commits into from
Oct 14, 2019
1 change: 1 addition & 0 deletions admin-dev/themes/new-theme/.webpack/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ module.exports = {
feature_form: './js/pages/feature/form',
order_message_form: './js/pages/order_message/form',
order_message: './js/pages/order_message',
order_view: './js/pages/order/view.js',
attachment: './js/pages/attachment',
},
output: {
Expand Down
28 changes: 28 additions & 0 deletions admin-dev/themes/new-theme/js/pages/order/OrderViewPageMap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* 2007-2019 PrestaShop SA 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
*/

export default {
orderPaymentDetailsBtn: '.js-payment-details-btn',
};
40 changes: 40 additions & 0 deletions admin-dev/themes/new-theme/js/pages/order/view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* 2007-2019 PrestaShop SA 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
*/

import OrderViewPageMap from './OrderViewPageMap';

const $ = window.$;

$(() => {
handlePaymentDetailsToggle();

function handlePaymentDetailsToggle() {
$(OrderViewPageMap.orderPaymentDetailsBtn).on('click', (event) => {
const $paymentDetailRow = $(event.currentTarget).closest('tr').next(':first');

$paymentDetailRow.toggleClass('d-none');
});
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* 2007-2019 PrestaShop SA 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\Form\ChoiceProvider;

use Module;
use PaymentModule;
use PrestaShop\PrestaShop\Core\Form\FormChoiceProviderInterface;
use Validate;

final class InstalledPaymentModulesChoiceProvider implements FormChoiceProviderInterface
{
private static $paymentModules;

/**
* {@inheritdoc}
*/
public function getChoices(): array
{
if (!self::$paymentModules) {
self::$paymentModules = [];

foreach (PaymentModule::getInstalledPaymentModules() as $payment) {
$module = Module::getInstanceByName($payment['name']);
if (Validate::isLoadedObject($module) && $module->active) {
self::$paymentModules[$module->name] = $module->displayName;
}
}
}

return self::$paymentModules;
}
}
1 change: 0 additions & 1 deletion src/Adapter/Order/CommandHandler/AddPaymentHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public function handle(AddPaymentCommand $command)
{
$order = $this->getOrderObject($command->getOrderId());

$amount = str_replace(',', '.', $command->getPaymentAmount());
$currency = new Currency($command->getPaymentCurrencyId()->getValue());
$orderHasInvoice = $order->hasInvoice();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class ChangeOrderCurrencyCommand
*/
public function __construct($orderId, $newCurrencyId)
{
$this->orderId = $orderId;
$this->newCurrencyId = $newCurrencyId;
$this->orderId = new OrderId($orderId);
$this->newCurrencyId = new CurrencyId($newCurrencyId);
}

/**
Expand Down
18 changes: 3 additions & 15 deletions src/Core/Domain/Order/Payment/Command/AddPaymentCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
use PrestaShop\Decimal\Number;
use PrestaShop\PrestaShop\Core\Domain\Currency\ValueObject\CurrencyId;
use PrestaShop\PrestaShop\Core\Domain\Order\Exception\OrderConstraintException;
use PrestaShop\PrestaShop\Core\Domain\Order\Exception\OrderException;
use PrestaShop\PrestaShop\Core\Domain\Order\ValueObject\OrderId;

/**
Expand Down Expand Up @@ -79,8 +78,8 @@ class AddPaymentCommand
* @param string $paymentMethod
* @param float $paymentAmount
* @param float $paymentCurrencyId
* @param null $orderInvoiceId
* @param string $transactionId
* @param int|null $orderInvoiceId
* @param string|null $transactionId transaction ID, usually payment ID from payment gateway
*/
public function __construct(
$orderId,
Expand All @@ -89,10 +88,9 @@ public function __construct(
$paymentAmount,
$paymentCurrencyId,
$orderInvoiceId = null,
$transactionId = ''
$transactionId = null
sarjon marked this conversation as resolved.
Show resolved Hide resolved
) {
$this->assertPaymentMethodIsGenericName($paymentMethod);
$this->assertTransactionIdIsString($transactionId);

$this->orderId = new OrderId($orderId);
$this->paymentDate = new DateTimeImmutable($paymentDate);
Expand Down Expand Up @@ -165,14 +163,4 @@ private function assertPaymentMethodIsGenericName($paymentMethod)
throw new OrderConstraintException('The selected payment method is invalid.');
}
}

/**
* @param string $transactionId
*/
private function assertTransactionIdIsString($transactionId)
{
if (!is_string($transactionId)) {
throw new OrderException('The transaction ID is invalid.');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use PrestaShop\PrestaShop\Core\Domain\Order\Command\AddCartRuleToOrderCommand;
use PrestaShop\PrestaShop\Core\Domain\Order\Command\BulkChangeOrderStatusCommand;
use PrestaShop\PrestaShop\Core\Domain\Order\Command\DuplicateOrderCartCommand;
use PrestaShop\PrestaShop\Core\Domain\Order\Command\ChangeOrderCurrencyCommand;
use PrestaShop\PrestaShop\Core\Domain\Order\Command\UpdateOrderStatusCommand;
use PrestaShop\PrestaShop\Core\Domain\Order\Exception\ChangeOrderStatusException;
use PrestaShop\PrestaShop\Core\Domain\Order\Exception\OrderException;
Expand All @@ -45,6 +46,7 @@
use PrestaShopBundle\Component\CsvResponse;
use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
use PrestaShopBundle\Form\Admin\Sell\Order\AddOrderCartRuleType;
use PrestaShopBundle\Form\Admin\Sell\Order\ChangeOrderCurrencyType;
use PrestaShopBundle\Form\Admin\Sell\Order\ChangeOrdersStatusType;
use PrestaShopBundle\Form\Admin\Sell\Order\OrderPaymentType;
use PrestaShopBundle\Form\Admin\Sell\Order\UpdateOrderStatusType;
Expand Down Expand Up @@ -250,13 +252,17 @@ public function viewAction(int $orderId): Response
], [
'id_order' => $orderId,
]);
$changeOrderCurrencyForm = $this->createForm(ChangeOrderCurrencyType::class, [], [
'current_currency_id' => $orderForViewing->getCurrencyId(),
]);

return $this->render('@PrestaShop/Admin/Sell/Order/Order/view.html.twig', [
'showContentHeader' => false,
'orderForViewing' => $orderForViewing,
'addOrderCartRuleForm' => $addOrderCartRuleForm->createView(),
'updateOrderStatusForm' => $updateOrderStatusForm->createView(),
'addOrderPaymentForm' => $addOrderPaymentForm->createView(),
'changeOrderCurrencyForm' => $changeOrderCurrencyForm->createView(),
]);
}

Expand Down Expand Up @@ -384,6 +390,40 @@ public function duplicateOrderCartAction(int $orderId)
);
}

/**
* @param int $orderId
* @param Request $request
*
* @return RedirectResponse
*/
public function changeCurrencyAction(int $orderId, Request $request): RedirectResponse
{
$changeOrderCurrencyForm = $this->createForm(ChangeOrderCurrencyType::class);
$changeOrderCurrencyForm->handleRequest($request);

if (!$changeOrderCurrencyForm->isSubmitted() || !$changeOrderCurrencyForm->isValid()) {
return $this->redirectToRoute('admin_orders_view', [
'orderId' => $orderId,
]);
}

$data = $changeOrderCurrencyForm->getData();

try {
$this->getCommandBus()->handle(
new ChangeOrderCurrencyCommand($orderId, (int) $data['new_currency_id'])
);

$this->addFlash('success', $this->trans('Successful update.', 'Admin.Notifications.Success'));
} catch (Exception $e) {
$this->addFlash('error', $this->getErrorMessageForException($e));
}

return $this->redirectToRoute('admin_orders_view', [
'orderId' => $orderId,
]);
}

/**
* @param OrderException $e
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
/**
* 2007-2019 PrestaShop SA 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 PrestaShopBundle\Form\Admin\Sell\Order;

use PrestaShop\PrestaShop\Core\Form\FormChoiceProviderInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ChangeOrderCurrencyType extends AbstractType
{
/**
* @var FormChoiceProviderInterface
*/
private $currencyChoiceProvider;

/**
* @param FormChoiceProviderInterface $currencyChoiceProvider
*/
public function __construct(FormChoiceProviderInterface $currencyChoiceProvider)
{
$this->currencyChoiceProvider = $currencyChoiceProvider;
}

/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('new_currency_id', ChoiceType::class, [
'choices' => $this->getCurrencyChoices($options['current_currency_id']),
])
;
}

/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver): void
{
$resolver
->setDefaults([
'current_currency_id' => null,
])
->setAllowedTypes('current_currency_id', ['int', 'null'])
;
}

/**
* @param int|null $currentCurrencyId
*
* @return array
*/
private function getCurrencyChoices(?int $currentCurrencyId): array
{
$choices = $this->currencyChoiceProvider->getChoices();

if (null === $currentCurrencyId) {
return $choices;
}

$currentCurrencyKey = array_search($currentCurrencyId, $choices, true);

if ($currentCurrencyKey) {
unset($choices[$currentCurrencyKey]);
}

return $choices;
}
}
Loading