PickupManagement.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Model\Pickup;
  6. use Magento\Sales\Api\Data\OrderItemInterface;
  7. use Temando\Shipping\Model\PickupInterface;
  8. /**
  9. * Temando Pickup Management
  10. *
  11. * @package Temando\Shipping\Model
  12. * @author Benjamin Heuer <benjamin.heuer@netresearch.de>
  13. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  14. * @link https://www.temando.com/
  15. */
  16. class PickupManagement
  17. {
  18. /**
  19. * @var PickupInterface[]
  20. */
  21. private $pickups = [];
  22. /**
  23. * PickupManagement constructor.
  24. * @param PickupInterface[] $pickups
  25. */
  26. public function __construct(
  27. array $pickups = []
  28. ) {
  29. $this->pickups = $pickups;
  30. }
  31. /**
  32. * @param string $state
  33. * @return PickupInterface[]
  34. */
  35. public function getPickupsByState(string $state): array
  36. {
  37. $pickups = array_filter($this->pickups, function (PickupInterface $pickup) use ($state) {
  38. return $pickup->getState() === $state;
  39. });
  40. return $pickups;
  41. }
  42. /**
  43. * Check if a pickup fulfillment with given ID can be cancelled.
  44. *
  45. * @param string $pickupId
  46. * @return bool
  47. */
  48. public function canCancel(string $pickupId): bool
  49. {
  50. if (!isset($this->pickups[$pickupId])) {
  51. return false;
  52. }
  53. $pickup = $this->pickups[$pickupId];
  54. $canCancel = !in_array(
  55. $pickup->getState(),
  56. [PickupInterface::STATE_CANCELLED, PickupInterface::STATE_PICKED_UP]
  57. );
  58. return $canCancel;
  59. }
  60. /**
  61. * Check if a pickup fulfillment with given ID can be prepared for collection.
  62. *
  63. * @param string $pickupId
  64. * @return bool
  65. */
  66. public function canPrepare(string $pickupId): bool
  67. {
  68. if (!isset($this->pickups[$pickupId])) {
  69. return false;
  70. }
  71. $pickup = $this->pickups[$pickupId];
  72. $canPrepare = ($pickup->getState() == PickupInterface::STATE_REQUESTED);
  73. return $canPrepare;
  74. }
  75. /**
  76. * Check if a pickup fulfillment with given ID can be collected from location.
  77. *
  78. * @param string $pickupId
  79. * @return bool
  80. */
  81. public function canCollect(string $pickupId): bool
  82. {
  83. if (!isset($this->pickups[$pickupId])) {
  84. return false;
  85. }
  86. $pickup = $this->pickups[$pickupId];
  87. $canCollect = ($pickup->getState() == PickupInterface::STATE_READY);
  88. return $canCollect;
  89. }
  90. /**
  91. * Obtain a list of items that are ready for collection.
  92. * Return format: [<sku> => <qty>, <sku> => <qty>]
  93. *
  94. * @return mixed[]
  95. */
  96. public function getPreparedItems()
  97. {
  98. if (empty($this->pickups)) {
  99. return [];
  100. }
  101. $preparedPickups = $this->getPickupsByState(PickupInterface::STATE_READY);
  102. $preparedItems = array_reduce($preparedPickups, function ($carry, PickupInterface $pickup) {
  103. foreach ($pickup->getItems() as $sku => $quantity) {
  104. if (isset($carry[$sku])) {
  105. $carry[$sku]+= $quantity;
  106. } else {
  107. $carry[$sku] = $quantity;
  108. }
  109. }
  110. return $carry;
  111. }, []);
  112. return $preparedItems;
  113. }
  114. /**
  115. * Obtain a list of items that are not yet shipped, prepared, or collected.
  116. * Return format: [<sku> => <qty>, <sku> => <qty>]
  117. *
  118. * @param OrderItemInterface[] $orderItems
  119. * @return int[]
  120. */
  121. public function getOpenItems(array $orderItems)
  122. {
  123. $openItems = [];
  124. $preparedItems = $this->getPreparedItems();
  125. foreach ($orderItems as $orderItem) {
  126. if ($orderItem->getIsVirtual() || $orderItem->getParentItem()) {
  127. continue;
  128. }
  129. $sku = $orderItem->getSku();
  130. $qtyOrdered = $orderItem->getQtyOrdered();
  131. $qtyShipped = $orderItem->getQtyShipped();
  132. $qtyPrepared = isset($preparedItems[$sku]) ? $preparedItems[$sku] : 0;
  133. $openItems[$sku] = $qtyOrdered - $qtyShipped - $qtyPrepared;
  134. }
  135. $openItems = array_filter($openItems, function ($qty) {
  136. return $qty > 0;
  137. });
  138. return $openItems;
  139. }
  140. /**
  141. * Check if requested items can be fulfilled. Returns a subset of the requested
  142. * items in case some of them are already fulfilled.
  143. * Return format: [<sku> => <qty>, <sku> => <qty>]
  144. *
  145. * @param mixed[] $requestedItems Format: [<order_item_id> => <qty>, <order_item_id> => <qty>]
  146. * @param OrderItemInterface[] $orderItems
  147. * @return int[]
  148. */
  149. public function getRequestedItems(array $requestedItems, array $orderItems)
  150. {
  151. $openItems = $this->getOpenItems($orderItems);
  152. $acceptedItems = [];
  153. foreach ($orderItems as $orderItem) {
  154. if ($orderItem->getParentItem()) {
  155. continue;
  156. }
  157. $sku = $orderItem->getSku();
  158. $id = $orderItem->getId();
  159. $qtyRequested = isset($requestedItems[$id]) ? $requestedItems[$id] : 0;
  160. $qtyOpen = isset($openItems[$sku]) ? $openItems[$sku] : 0;
  161. $qtyRequested = min($qtyRequested, $qtyOpen);
  162. if ($qtyRequested < 1) {
  163. continue;
  164. }
  165. $acceptedItems[$sku] = $qtyRequested;
  166. }
  167. return $acceptedItems;
  168. }
  169. }