Skip to content

Commit

Permalink
feat(fulfilment): add order notes
Browse files Browse the repository at this point in the history
  • Loading branch information
joerivanveen committed Aug 1, 2023
1 parent 5a4f059 commit ae98602
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions src/Pdk/Order/Repository/PsPdkOrderRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Country;
use Customer;
use CustomerMessage;
use DateTimeImmutable;
use MyParcelNL\Pdk\App\Order\Collection\PdkOrderCollection;
use MyParcelNL\Pdk\App\Order\Collection\PdkOrderNoteCollection;
use MyParcelNL\Pdk\App\Order\Contract\PdkProductRepositoryInterface;
Expand Down Expand Up @@ -128,23 +129,47 @@ 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,
'paymentMethod' => $order->payment,
], $orderData)
], $orderData, [
'notes' => $this->getOrderNotes($order, $orderData['notes'] ?? []),
])
);
});
}

/**
* @param null|string $date
* @param string $fallback
*
* @return string
*/
private function getDate(?string $date, string $fallback): string
{
try {
return (new DateTimeImmutable($date))->format('Y-m-d H:i:s');
} catch (\Exception $e) {
return $fallback;
}
}

/**
* @param \Order $order
* @param array $existingNotes
*
* @return \MyParcelNL\Pdk\App\Order\Collection\PdkOrderNoteCollection
* @throws \Exception
*/
public function getOrderNotes(Order $order, array $existingNotes): PdkOrderNoteCollection
{
return $this->retrieve(
sprintf("notes_%s", $order->id),
function () use ($existingNotes, $order) {
$collection = new PdkOrderNoteCollection($existingNotes);
$orderNotes = new PdkOrderNoteCollection();
$orderDate = (new DateTimeImmutable($order->date_add))->format('Y-m-d H:i:s');

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

Expand All @@ -158,12 +183,12 @@ function () use ($existingNotes, $order) {
'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'],
'createdAt' => $this->getDate($customerNote['date_add'] ?? null, $orderDate),
'updatedAt' => $this->getDate($customerNote['date_upd'] ?? null, $orderDate),
]);
}

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

0 comments on commit ae98602

Please sign in to comment.