Skip to content

Commit

Permalink
Merge pull request #28820 from Progi1984/issue14547
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieu-rolland committed Aug 4, 2022
2 parents 79b15c0 + 2a6f89c commit d1e14ec
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 33 deletions.
13 changes: 11 additions & 2 deletions classes/order/Order.php
Expand Up @@ -1905,11 +1905,19 @@ public function hasPayments(): bool
* @param Currency $currency
* @param string $date
* @param OrderInvoice $order_invoice
* @param int|null $id_employee
*
* @return bool
*/
public function addOrderPayment($amount_paid, $payment_method = null, $payment_transaction_id = null, $currency = null, $date = null, $order_invoice = null)
{
public function addOrderPayment(
$amount_paid,
$payment_method = null,
$payment_transaction_id = null,
$currency = null,
$date = null,
$order_invoice = null,
int $id_employee = null
) {
$order_payment = new OrderPayment();
$order_payment->order_reference = $this->reference;
$order_payment->id_currency = ($currency ? $currency->id : $this->id_currency);
Expand All @@ -1919,6 +1927,7 @@ public function addOrderPayment($amount_paid, $payment_method = null, $payment_t
$order_payment->payment_method = ($payment_method ? $payment_method : $this->payment);
$order_payment->transaction_id = $payment_transaction_id;
$order_payment->amount = (float) $amount_paid;
$order_payment->id_employee = $id_employee;
$order_payment->date_add = ($date ? $date : null);

// Add time to the date if needed
Expand Down
6 changes: 6 additions & 0 deletions classes/order/OrderPayment.php
Expand Up @@ -40,6 +40,11 @@ class OrderPaymentCore extends ObjectModel
public $card_holder;
public $date_add;

/**
* @var int|null
*/
public $id_employee;

/**
* @see ObjectModel::$definition
*/
Expand All @@ -58,6 +63,7 @@ class OrderPaymentCore extends ObjectModel
'card_expiration' => ['type' => self::TYPE_STRING, 'validate' => 'isAnything', 'size' => 254],
'card_holder' => ['type' => self::TYPE_STRING, 'validate' => 'isAnything', 'size' => 254],
'date_add' => ['type' => self::TYPE_DATE, 'validate' => 'isDate'],
'id_employee' => ['type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'allow_null' => true],
],
];

Expand Down
1 change: 1 addition & 0 deletions install-dev/data/db_structure.sql
Expand Up @@ -1591,6 +1591,7 @@ CREATE TABLE `PREFIX_order_payment` (
`card_expiration` CHAR(7) NULL,
`card_holder` VARCHAR(254) NULL,
`date_add` DATETIME NOT NULL,
`id_employee` INT NULL,
PRIMARY KEY (`id_order_payment`),
KEY `order_reference`(`order_reference`)
) ENGINE=ENGINE_TYPE DEFAULT CHARSET=utf8mb4 COLLATION;
Expand Down
3 changes: 2 additions & 1 deletion src/Adapter/Order/CommandHandler/AddPaymentHandler.php
Expand Up @@ -69,7 +69,8 @@ public function handle(AddPaymentCommand $command)
$command->getPaymentTransactionId(),
$currency,
$command->getPaymentDate()->format('Y-m-d H:i:s'),
$orderInvoice
$orderInvoice,
$command->getEmployeeId()->getValue()
);

if (!$paymentAdded) {
Expand Down
6 changes: 5 additions & 1 deletion src/Adapter/Order/QueryHandler/GetOrderForViewingHandler.php
Expand Up @@ -35,6 +35,7 @@
use Currency;
use Customer;
use DateTimeImmutable;
use Employee;
use Gender;
use Group;
use Module;
Expand Down Expand Up @@ -653,6 +654,8 @@ private function getOrderPayments(Order $order): OrderPaymentsForViewing
$invoiceNumber = $invoice ?
$invoice->getInvoiceNumberFormatted($this->contextLanguageId, $order->id_shop) :
null;
$employee = $payment->id_employee ? new Employee($payment->id_employee) : null;
$employeeName = $employee ? $employee->firstname . ' ' . $employee->lastname : null;

$orderPayments[] = new OrderPaymentForViewing(
$payment->id,
Expand All @@ -664,7 +667,8 @@ private function getOrderPayments(Order $order): OrderPaymentsForViewing
$payment->card_number,
$payment->card_brand,
$payment->card_expiration,
$payment->card_holder
$payment->card_holder,
$employeeName
);
}

Expand Down
37 changes: 34 additions & 3 deletions src/Core/Domain/Order/Payment/Command/AddPaymentCommand.php
Expand Up @@ -29,6 +29,7 @@
use DateTimeImmutable;
use PrestaShop\Decimal\DecimalNumber;
use PrestaShop\PrestaShop\Core\Domain\Currency\ValueObject\CurrencyId;
use PrestaShop\PrestaShop\Core\Domain\Employee\ValueObject\EmployeeId;
use PrestaShop\PrestaShop\Core\Domain\Order\Exception\NegativePaymentAmountException;
use PrestaShop\PrestaShop\Core\Domain\Order\Exception\OrderConstraintException;
use PrestaShop\PrestaShop\Core\Domain\Order\ValueObject\OrderId;
Expand Down Expand Up @@ -79,16 +80,22 @@ class AddPaymentCommand
private $transactionId;

/**
* @var null
* @var int|null
*/
private $orderInvoiceId;

/**
* @var EmployeeId
*/
protected $employeeId;

/**
* @param int $orderId
* @param string $paymentDate
* @param string $paymentMethod
* @param string $paymentAmount
* @param int $paymentCurrencyId
* @param int $employeeId
* @param int|null $orderInvoiceId
* @param string|null $transactionId transaction ID, usually payment ID from payment gateway
*/
Expand All @@ -98,6 +105,7 @@ public function __construct(
string $paymentMethod,
string $paymentAmount,
int $paymentCurrencyId,
int $employeeId,
?int $orderInvoiceId = null,
?string $transactionId = null
) {
Expand All @@ -110,6 +118,7 @@ public function __construct(
$this->paymentMethod = $paymentMethod;
$this->paymentAmount = $amount;
$this->paymentCurrencyId = new CurrencyId($paymentCurrencyId);
$this->employeeId = new EmployeeId($employeeId);
$this->orderInvoiceId = $orderInvoiceId;
$this->transactionId = $transactionId;
}
Expand Down Expand Up @@ -154,11 +163,22 @@ public function getPaymentCurrencyId()
return $this->paymentCurrencyId;
}

/**
* @return int|null
*/
public function getOrderInvoiceId()
{
return $this->orderInvoiceId;
}

/**
* @return EmployeeId
*/
public function getEmployeeId(): EmployeeId
{
return $this->employeeId;
}

/**
* @return string|null
*/
Expand All @@ -169,8 +189,12 @@ public function getPaymentTransactionId()

/**
* @param string $paymentMethod
*
* @return void
*
* @throws OrderConstraintException
*/
private function assertPaymentMethodIsGenericName($paymentMethod)
private function assertPaymentMethodIsGenericName(string $paymentMethod): void
{
if (empty($paymentMethod) || !preg_match(self::PATTERN_PAYMENT_METHOD_NAME, $paymentMethod)) {
throw new OrderConstraintException(
Expand All @@ -180,7 +204,14 @@ private function assertPaymentMethodIsGenericName($paymentMethod)
}
}

private function assertAmountIsPositive(DecimalNumber $amount)
/**
* @param DecimalNumber $amount
*
* @return void
*
* @throws NegativePaymentAmountException
*/
private function assertAmountIsPositive(DecimalNumber $amount): void
{
if ($amount->isNegative()) {
throw new NegativePaymentAmountException('The amount should be greater than 0.');
Expand Down
18 changes: 17 additions & 1 deletion src/Core/Domain/Order/QueryResult/OrderPaymentForViewing.php
Expand Up @@ -80,6 +80,11 @@ class OrderPaymentForViewing
*/
private $cardHolder;

/**
* @var string|null
*/
protected $employeeName;

/**
* @param int $paymentId
* @param DateTimeImmutable $date
Expand All @@ -91,6 +96,7 @@ class OrderPaymentForViewing
* @param string $cardBrand
* @param string $cardExpiration
* @param string $cardHolder
* @param string|null $employeeName
*/
public function __construct(
int $paymentId,
Expand All @@ -102,7 +108,8 @@ public function __construct(
string $cardNumber,
string $cardBrand,
string $cardExpiration,
string $cardHolder
string $cardHolder,
?string $employeeName = null
) {
$this->paymentId = $paymentId;
$this->date = $date;
Expand All @@ -114,6 +121,7 @@ public function __construct(
$this->cardBrand = $cardBrand;
$this->cardExpiration = $cardExpiration;
$this->cardHolder = $cardHolder;
$this->employeeName = $employeeName;
}

/**
Expand Down Expand Up @@ -195,4 +203,12 @@ public function getCardHolder(): string
{
return $this->cardHolder;
}

/**
* @return string|null
*/
public function getEmployeeName(): ?string
{
return $this->employeeName;
}
}
Expand Up @@ -1191,6 +1191,7 @@ public function addPaymentAction(int $orderId, Request $request): RedirectRespon
$data['payment_method'],
$data['amount'],
$data['id_currency'],
(int) $this->getContext()->employee->id,
$data['id_invoice'],
$data['transaction_id']
)
Expand Down
Expand Up @@ -44,6 +44,7 @@
<th class="table-head-transaction">{{ 'Transaction ID'|trans({}, 'Admin.Orderscustomers.Feature') }}</th>
<th class="table-head-amount">{{ 'Amount'|trans({}, 'Admin.Global') }}</th>
<th class="table-head-invoice">{{ 'Invoice'|trans({}, 'Admin.Global') }}</th>
<th class="table-head-employee">{{ 'Employee'|trans({}, 'Admin.Global') }}</th>
<th></th>
</tr>
</thead>
Expand All @@ -55,6 +56,7 @@
<td data-role="transaction-id-column">{{ payment.transactionId }}</td>
<td data-role="amount-column">{{ payment.amount }}</td>
<td data-role="invoice-column">{% if payment.invoiceNumber %}{{ payment.invoiceNumber }}{% endif %}</td>
<td data-role="invoice-column">{% if payment.employeeName %}{{ payment.employeeName }}{% else %}-{% endif %}</td>
<td class="text-right">
<button class="btn btn-sm btn-outline-secondary js-payment-details-btn">
{{ 'Details'|trans({}, 'Admin.Global') }}
Expand Down Expand Up @@ -120,6 +122,7 @@
{{ form_widget(addOrderPaymentForm.id_invoice) }}
</div>
</td>
<td></td>
<td>
<button type="submit" class="btn btn-primary btn-sm">{{ 'Add'|trans({}, 'Admin.Actions') }}</button>
</td>
Expand Down
Expand Up @@ -27,7 +27,7 @@
namespace Tests\Integration\Behaviour\Features\Context\Domain;

use Behat\Gherkin\Node\TableNode;
use DateTimeImmutable;
use Context;
use PHPUnit\Framework\Assert as Assert;
use PrestaShop\PrestaShop\Core\Domain\Order\Exception\NegativePaymentAmountException;
use PrestaShop\PrestaShop\Core\Domain\Order\Exception\OrderConstraintException;
Expand Down Expand Up @@ -60,6 +60,7 @@ public function addPaymentToOrderWithTheFollowingDetails(string $orderReference,
$data['payment_method'],
$data['amount'],
SharedStorage::getStorage()->get($data['currency']),
(int) Context::getContext()->employee->id,
isset($data['id_invoice']) ? (int) $data['id_invoice'] : null,
$data['transaction_id']
)
Expand Down Expand Up @@ -132,6 +133,15 @@ public function checkOrderPayment(string $orderReference, string $position, Tabl
unset($dataArray['date']);
}

if (isset($dataArray['employee'])) {
Assert::assertEquals(
$dataArray['employee'],
$orderPaymentForViewing->getEmployeeName()
);

unset($dataArray['employee']);
}

foreach ($dataArray as $key => $value) {
Assert::assertEquals(
$value,
Expand Down Expand Up @@ -160,6 +170,7 @@ public function addPaymentToOrderWithTheInvalidFollowingProperties(string $order
$data['payment_method'],
$data['amount'],
SharedStorage::getStorage()->get($data['currency']),
(int) Context::getContext()->employee->id,
isset($data['id_invoice']) ? (int) $data['id_invoice'] : null,
$data['transaction_id']
)
Expand Down Expand Up @@ -190,22 +201,6 @@ public function assertLastErrorIsInvalidPaymentMethod(): void
);
}

private function mapToOrderPaymentForViewing(int $paymentId, array $data)
{
return new OrderPaymentForViewing(
$paymentId,
new DateTimeImmutable($data['date']),
$data['payment_method'],
$data['transaction_id'],
$data['amount'],
isset($data['id_invoice']) ? (string) $data['id_invoice'] : null,
'',
'',
'',
''
);
}

/**
* @param TableNode $table
*
Expand Down
Expand Up @@ -82,6 +82,7 @@ Feature: Add payment to Order from Back Office (BO)
| paymentMethod | Payments by check |
| transactionId | test123 |
| amount | $6.00 |
| employee | Puff Daddy |
And order "bo_order1" should have the following details:
| total_paid_real | 6.000000 |

Expand All @@ -98,6 +99,7 @@ Feature: Add payment to Order from Back Office (BO)
| paymentMethod | Payments by check |
| transactionId | test123 |
| amount | €6.00 |
| employee | Puff Daddy |
And order "bo_order1" should have the following details:
| total_paid_real | 6.820000 |

Expand Down

0 comments on commit d1e14ec

Please sign in to comment.