Skip to content

Commit

Permalink
wip(fulfilment): add order notes
Browse files Browse the repository at this point in the history
  • Loading branch information
joerivanveen committed Jul 31, 2023
1 parent bfd22ae commit 5a4f059
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 23 deletions.
2 changes: 2 additions & 0 deletions myparcelnl.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use MyParcelNL\Pdk\Facade\Installer;
use MyParcelNL\Pdk\Facade\Pdk;
use MyParcelNL\PrestaShop\Module\Hooks\HasPdkCheckoutHooks;
use MyParcelNL\PrestaShop\Module\Hooks\HasPdkOrderHooks;
use MyParcelNL\PrestaShop\Module\Hooks\HasPdkProductHooks;
use MyParcelNL\PrestaShop\Module\Hooks\HasPdkRenderHooks;
use MyParcelNL\PrestaShop\Module\Hooks\HasPdkScriptHooks;
Expand All @@ -24,6 +25,7 @@ final class MyParcelNL extends CarrierModule
use HasPdkProductHooks;
use HasPdkRenderHooks;
use HasPdkScriptHooks;
use HasPdkOrderHooks;

/**
* @deprecated
Expand Down
29 changes: 29 additions & 0 deletions src/Module/Hooks/HasPdkOrderHooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace MyParcelNL\PrestaShop\Module\Hooks;

use CustomerMessage;
use CustomerThread;
use MyParcelNL\Pdk\App\Api\Backend\PdkBackendActions;
use MyParcelNL\Pdk\Facade\Actions;

trait HasPdkOrderHooks
{
/**
* @throws \PrestaShopDatabaseException
* @throws \PrestaShopException
*/
public function hookActionObjectCustomerMessageAddAfter(array $params): void
{
if ($params['object'] instanceof CustomerMessage) {
$message = $params['object'];
$thread = new CustomerThread($message->id_customer_thread);

Actions::execute(PdkBackendActions::POST_ORDER_NOTES, [
'orderIds' => [$thread->id_order],
]);
}
}
}
53 changes: 30 additions & 23 deletions src/Pdk/Order/Repository/PsPdkOrderRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
use Customer;
use CustomerMessage;
use MyParcelNL\Pdk\App\Order\Collection\PdkOrderCollection;
use MyParcelNL\Pdk\App\Order\Collection\PdkOrderNoteCollection;
use MyParcelNL\Pdk\App\Order\Contract\PdkProductRepositoryInterface;
use MyParcelNL\Pdk\App\Order\Model\PdkOrder;
use MyParcelNL\Pdk\App\Order\Repository\AbstractPdkOrderRepository;
use MyParcelNL\Pdk\Base\Contract\CurrencyServiceInterface;
use MyParcelNL\Pdk\Base\Contract\WeightServiceInterface;
use MyParcelNL\Pdk\Facade\Logger;
use MyParcelNL\Pdk\Facade\Platform;
use MyParcelNL\Pdk\Fulfilment\Collection\OrderNoteCollection;
use MyParcelNL\Pdk\Fulfilment\Model\OrderNote;
use MyParcelNL\Pdk\Shipment\Model\CustomsDeclaration;
use MyParcelNL\Pdk\Shipment\Model\CustomsDeclarationItem;
use MyParcelNL\Pdk\Shipment\Model\Shipment;
Expand All @@ -27,7 +28,6 @@
use MyParcelNL\PrestaShop\Repository\PsOrderDataRepository;
use MyParcelNL\PrestaShop\Repository\PsOrderShipmentRepository;
use Order;
use OrderMessage;
use State;

class PsPdkOrderRepository extends AbstractPdkOrderRepository
Expand Down Expand Up @@ -128,6 +128,7 @@ public function get($input): PdkOrder
'shipmentPrice' => $this->currencyService->convertToCents($order->total_shipping_tax_incl),
'shipmentVat' => $this->currencyService->convertToCents($order->total_shipping_tax_excl),
'lines' => $this->createOrderLines($orderProducts),
'notes' => $this->getOrderNotes($order, $orderData['notes'] ?? []),
'customsDeclaration' => $this->createCustomsDeclaration($order, $orderProducts),
'invoiceId' => $order->id,
'invoiceDate' => $order->date_add,
Expand All @@ -137,28 +138,34 @@ public function get($input): PdkOrder
});
}

public function getOrderNotes(?string $externalIdentifier): OrderNoteCollection
public function getOrderNotes(Order $order, array $existingNotes): PdkOrderNoteCollection
{
$orderNoteCollection = new OrderNoteCollection();
$customerNotes = CustomerMessage::getMessagesByOrderId($externalIdentifier);

foreach ($customerNotes as $customerNote) {
$orderNoteCollection->push([
'note' => $customerNote['message'],
'author' => 'customer',
]);
}

$webshopNotes = OrderMessage::getOrderMessages($externalIdentifier);

foreach ($webshopNotes as $webshopNote) {
$orderNoteCollection->push([
'note' => $webshopNote['message'],
'author' => 'webshop',
]);
}

return $orderNoteCollection;
return $this->retrieve(
sprintf("notes_%s", $order->id),
function () use ($existingNotes, $order) {
$collection = new PdkOrderNoteCollection($existingNotes);
$orderNotes = new PdkOrderNoteCollection();

$customerNotes = CustomerMessage::getMessagesByOrderId($order->id);

foreach ($customerNotes as $customerNote) {
$author = '0' === ($customerNote['id_employee'] ?? '0')
? OrderNote::AUTHOR_CUSTOMER
: OrderNote::AUTHOR_WEBSHOP;

$orderNotes->push([
'apiIdentifier' => null,
'externalIdentifier' => $customerNote['id_customer_message'],
'note' => $customerNote['message'],
'author' => $author,
'createdAt' => $customerNote['date_add'], // todo what if format changes or key is empty?
'updatedAt' => $customerNote['date_upd'],
]);
}

return $collection->mergeByKey($orderNotes, 'externalIdentifier');
}
);
}

/**
Expand Down

0 comments on commit 5a4f059

Please sign in to comment.