diff --git a/myparcelnl.php b/myparcelnl.php index 8b840328..8dd1d108 100644 --- a/myparcelnl.php +++ b/myparcelnl.php @@ -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; @@ -24,6 +25,7 @@ final class MyParcelNL extends CarrierModule use HasPdkProductHooks; use HasPdkRenderHooks; use HasPdkScriptHooks; + use HasPdkOrderHooks; /** * @deprecated diff --git a/src/Module/Hooks/HasPdkOrderHooks.php b/src/Module/Hooks/HasPdkOrderHooks.php new file mode 100644 index 00000000..3fa502bd --- /dev/null +++ b/src/Module/Hooks/HasPdkOrderHooks.php @@ -0,0 +1,29 @@ +id_customer_thread); + + Actions::execute(PdkBackendActions::POST_ORDER_NOTES, [ + 'orderIds' => [$thread->id_order], + ]); + } + } +} diff --git a/src/Pdk/Order/Repository/PsPdkOrderRepository.php b/src/Pdk/Order/Repository/PsPdkOrderRepository.php index 2b6ea139..1806e3f8 100644 --- a/src/Pdk/Order/Repository/PsPdkOrderRepository.php +++ b/src/Pdk/Order/Repository/PsPdkOrderRepository.php @@ -9,6 +9,7 @@ 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; @@ -16,7 +17,7 @@ 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; @@ -27,7 +28,6 @@ use MyParcelNL\PrestaShop\Repository\PsOrderDataRepository; use MyParcelNL\PrestaShop\Repository\PsOrderShipmentRepository; use Order; -use OrderMessage; use State; class PsPdkOrderRepository extends AbstractPdkOrderRepository @@ -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, @@ -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'); + } + ); } /**