Skip to content

Commit

Permalink
Add employee info on the Payments tab when payment is registered
Browse files Browse the repository at this point in the history
  • Loading branch information
Progi1984 committed Jun 29, 2022
1 parent efa889c commit bcae42e
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 22 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
13 changes: 12 additions & 1 deletion src/Adapter/Order/CommandHandler/AddPaymentHandler.php
Expand Up @@ -39,6 +39,16 @@
*/
final class AddPaymentHandler extends AbstractOrderHandler implements AddPaymentHandlerInterface
{
/**
* @var int
*/
private $idEmployee;

public function __construct(int $idEmployee)
{
$this->idEmployee = $idEmployee;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -69,7 +79,8 @@ public function handle(AddPaymentCommand $command)
$command->getPaymentTransactionId(),
$currency,
$command->getPaymentDate()->format('Y-m-d H:i:s'),
$orderInvoice
$orderInvoice,
$this->idEmployee
);

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
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 @@ -74,6 +74,8 @@ services:

prestashop.adapter.order.command_handler.add_payment_handler:
class: PrestaShop\PrestaShop\Adapter\Order\CommandHandler\AddPaymentHandler
arguments:
- '@=service("prestashop.adapter.legacy.context").getContext().employee.id'
tags:
- name: tactician.handler
command: PrestaShop\PrestaShop\Core\Domain\Order\Payment\Command\AddPaymentCommand
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,6 @@
namespace Tests\Integration\Behaviour\Features\Context\Domain;

use Behat\Gherkin\Node\TableNode;
use DateTimeImmutable;
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 @@ -132,6 +131,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 @@ -190,22 +198,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 bcae42e

Please sign in to comment.