UpdateQtyShipped.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Model\Sales\Service\Operation;
  6. use Magento\Framework\Exception\LocalizedException;
  7. use Magento\Sales\Api\OrderRepositoryInterface;
  8. use Magento\Sales\Api\ShipmentRepositoryInterface;
  9. use Temando\Shipping\Model\Shipment\PackageInterface;
  10. use Temando\Shipping\Model\Shipment\PackageItemInterface;
  11. use Temando\Shipping\Model\ShipmentInterface;
  12. /**
  13. * Temando Shipment Operation: Update Item Quantities.
  14. *
  15. * @package Temando\Shipping\Model
  16. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  17. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  18. * @link https://www.temando.com/
  19. */
  20. class UpdateQtyShipped implements ShipmentOperationInterface
  21. {
  22. /**
  23. * @var ShipmentRepositoryInterface
  24. */
  25. private $shipmentRepository;
  26. /**
  27. * @var OrderRepositoryInterface
  28. */
  29. private $orderRepository;
  30. /**
  31. * UpdateQtyShipped constructor.
  32. * @param ShipmentRepositoryInterface $shipmentRepository
  33. * @param OrderRepositoryInterface $orderRepository
  34. */
  35. public function __construct(
  36. ShipmentRepositoryInterface $shipmentRepository,
  37. OrderRepositoryInterface $orderRepository
  38. ) {
  39. $this->shipmentRepository = $shipmentRepository;
  40. $this->orderRepository = $orderRepository;
  41. }
  42. /**
  43. * Reduce the shipped items by the quantities contained in the cancelled shipment.
  44. *
  45. * @param ShipmentInterface $shipment
  46. * @param int $salesShipmentId
  47. * @throws LocalizedException
  48. */
  49. public function execute(ShipmentInterface $shipment, int $salesShipmentId): void
  50. {
  51. $salesShipment = $this->shipmentRepository->get($salesShipmentId);
  52. $salesOrder = $this->orderRepository->get($salesShipment->getOrderId());
  53. // collect all items from all included packages
  54. $packages = $shipment->getPackages() ?: [];
  55. $packageItems = array_reduce($packages, function (array $items, PackageInterface $package) {
  56. $items = array_merge($items, $package->getItems());
  57. return $items;
  58. }, []);
  59. // reduce each order item's qty shipped by the cancelled quantity
  60. array_walk($packageItems, function (PackageItemInterface $packageItem) use ($salesOrder) {
  61. $sku = $packageItem->getSku();
  62. $qty = $packageItem->getQty();
  63. /** @var \Magento\Sales\Model\Order $salesOrder */
  64. /** @var \Magento\Sales\Api\Data\OrderItemInterface $orderItem */
  65. foreach ($salesOrder->getAllVisibleItems() as $orderItem) {
  66. if ($orderItem->getParentItem()) {
  67. continue;
  68. }
  69. if ($orderItem->getSku() === $sku) {
  70. $qtyShipped = $orderItem->getQtyShipped() - $qty;
  71. $orderItem->setQtyShipped($qtyShipped);
  72. }
  73. }
  74. });
  75. $this->orderRepository->save($salesOrder);
  76. }
  77. }