Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SourceDeductionProcessor.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
19 use Magento\InventorySalesApi\Api\Data\ItemToSellInterfaceFactory;
20 
25 {
29  private $isSingleSourceMode;
30 
34  private $defaultSourceProvider;
35 
39  private $getItemsToDeductFromShipment;
40 
44  private $sourceDeductionRequestFromShipmentFactory;
45 
49  private $sourceDeductionService;
50 
54  private $itemsToSellFactory;
55 
59  private $placeReservationsForSalesEvent;
60 
70  public function __construct(
71  IsSingleSourceModeInterface $isSingleSourceMode,
72  DefaultSourceProviderInterface $defaultSourceProvider,
73  GetItemsToDeductFromShipment $getItemsToDeductFromShipment,
74  SourceDeductionRequestFromShipmentFactory $sourceDeductionRequestFromShipmentFactory,
75  SourceDeductionServiceInterface $sourceDeductionService,
76  ItemToSellInterfaceFactory $itemsToSellFactory,
77  PlaceReservationsForSalesEventInterface $placeReservationsForSalesEvent
78  ) {
79  $this->isSingleSourceMode = $isSingleSourceMode;
80  $this->defaultSourceProvider = $defaultSourceProvider;
81  $this->getItemsToDeductFromShipment = $getItemsToDeductFromShipment;
82  $this->sourceDeductionRequestFromShipmentFactory = $sourceDeductionRequestFromShipmentFactory;
83  $this->sourceDeductionService = $sourceDeductionService;
84  $this->itemsToSellFactory = $itemsToSellFactory;
85  $this->placeReservationsForSalesEvent = $placeReservationsForSalesEvent;
86  }
87 
92  public function execute(EventObserver $observer)
93  {
95  $shipment = $observer->getEvent()->getShipment();
96  if ($shipment->getOrigData('entity_id')) {
97  return;
98  }
99 
100  if (!empty($shipment->getExtensionAttributes())
101  && !empty($shipment->getExtensionAttributes()->getSourceCode())) {
102  $sourceCode = $shipment->getExtensionAttributes()->getSourceCode();
103  } elseif ($this->isSingleSourceMode->execute()) {
104  $sourceCode = $this->defaultSourceProvider->getCode();
105  }
106 
107  $shipmentItems = $this->getItemsToDeductFromShipment->execute($shipment);
108 
109  if (!empty($shipmentItems)) {
110  $sourceDeductionRequest = $this->sourceDeductionRequestFromShipmentFactory->execute(
111  $shipment,
112  $sourceCode,
113  $shipmentItems
114  );
115  $this->sourceDeductionService->execute($sourceDeductionRequest);
116  $this->placeCompensatingReservation($sourceDeductionRequest);
117  }
118  }
119 
125  private function placeCompensatingReservation(SourceDeductionRequestInterface $sourceDeductionRequest): void
126  {
127  $items = [];
128  foreach ($sourceDeductionRequest->getItems() as $item) {
129  $items[] = $this->itemsToSellFactory->create([
130  'sku' => $item->getSku(),
131  'qty' => $item->getQty()
132  ]);
133  }
134  $this->placeReservationsForSalesEvent->execute(
135  $items,
136  $sourceDeductionRequest->getSalesChannel(),
137  $sourceDeductionRequest->getSalesEvent()
138  );
139  }
140 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$sourceCode
Definition: inventory.phtml:11
foreach($order->getItems() as $orderItem) $shipment
__construct(IsSingleSourceModeInterface $isSingleSourceMode, DefaultSourceProviderInterface $defaultSourceProvider, GetItemsToDeductFromShipment $getItemsToDeductFromShipment, SourceDeductionRequestFromShipmentFactory $sourceDeductionRequestFromShipmentFactory, SourceDeductionServiceInterface $sourceDeductionService, ItemToSellInterfaceFactory $itemsToSellFactory, PlaceReservationsForSalesEventInterface $placeReservationsForSalesEvent)
$items