PickupComposite.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Model\ResourceModel\Pickup;
  6. use Magento\Sales\Api\Data\OrderInterface;
  7. use Magento\Sales\Api\Data\OrderSearchResultInterface;
  8. use Temando\Shipping\Model\PickupInterface;
  9. /**
  10. * Temando Pickup Order Data Aggregator
  11. *
  12. * @package Temando\Shipping\Model
  13. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  14. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  15. * @link https://www.temando.com/
  16. */
  17. class PickupComposite
  18. {
  19. /**
  20. * @var OrderSearchResultInterface
  21. */
  22. private $orderSearchResult;
  23. /**
  24. * PickupComposite constructor.
  25. * @param OrderSearchResultInterface $orderSearchResult
  26. */
  27. public function __construct(OrderSearchResultInterface $orderSearchResult)
  28. {
  29. $this->orderSearchResult = $orderSearchResult;
  30. }
  31. /**
  32. * Extract order from collection.
  33. *
  34. * @param int $orderId
  35. * @return OrderInterface|null
  36. */
  37. private function getOrder(int $orderId): ?OrderInterface
  38. {
  39. foreach ($this->orderSearchResult->getItems() as $order) {
  40. if ((int)$order->getEntityId() === $orderId) {
  41. return $order;
  42. }
  43. }
  44. return null;
  45. }
  46. /**
  47. * Add order details to pickup fulfillments
  48. *
  49. * @param PickupInterface|\Temando\Shipping\Model\Pickup $pickup
  50. * @param int $orderId
  51. * @return PickupInterface
  52. */
  53. public function aggregate(PickupInterface $pickup, ?int $orderId): PickupInterface
  54. {
  55. if (!$orderId) {
  56. return $pickup;
  57. }
  58. $order = $this->getOrder($orderId);
  59. if (!$order) {
  60. return $pickup;
  61. }
  62. /** @var \Magento\Sales\Model\Order\Address $shippingAddress */
  63. $shippingAddress = $order->getShippingAddress();
  64. $customerName = [
  65. $shippingAddress->getFirstname(),
  66. $shippingAddress->getMiddlename(),
  67. $shippingAddress->getLastname(),
  68. ];
  69. $customerName = array_filter($customerName);
  70. $pickup->addData([
  71. 'sales_order_id' => $order->getEntityId(),
  72. 'sales_order_increment_id' => $order->getIncrementId(),
  73. 'ordered_at_date' => $order->getCreatedAt(),
  74. 'customer_name' => implode(' ', $customerName),
  75. 'origin_location' => $pickup->getPickupLocation()->getName(),
  76. ]);
  77. return $pickup;
  78. }
  79. }