forked from magento/magento2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request magento#921 from magento-engcom/822-source-deducti…
…on-for-virtual MSI#822 Source deduction for virtual and downloadable products
- Loading branch information
Showing
18 changed files
with
1,544 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
app/code/Magento/InventoryShipping/Model/InventoryRequestFromInvoiceFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\InventoryShipping\Model; | ||
|
||
use Magento\Framework\Exception\InputException; | ||
use Magento\InventoryCatalog\Model\GetSkusByProductIdsInterface; | ||
use Magento\InventorySales\Model\StockByWebsiteIdResolver; | ||
use Magento\InventorySourceSelectionApi\Api\Data\InventoryRequestInterface; | ||
use Magento\InventorySourceSelectionApi\Api\Data\InventoryRequestInterfaceFactory; | ||
use Magento\InventorySourceSelectionApi\Api\Data\ItemRequestInterfaceFactory; | ||
use Magento\Sales\Api\Data\InvoiceInterface; | ||
use Magento\Sales\Api\Data\InvoiceItemInterface; | ||
use Magento\Sales\Api\Data\OrderItemInterface; | ||
use Traversable; | ||
|
||
/** | ||
* Creates instance of InventoryRequestInterface by given InvoiceInterface object. | ||
* Only virtual type items will be used. | ||
*/ | ||
class InventoryRequestFromInvoiceFactory | ||
{ | ||
/** | ||
* @var GetSkusByProductIdsInterface | ||
*/ | ||
private $getSkusByProductIds; | ||
|
||
/** | ||
* @var ItemRequestInterfaceFactory | ||
*/ | ||
private $itemRequestFactory; | ||
|
||
/** | ||
* @var InventoryRequestInterfaceFactory | ||
*/ | ||
private $inventoryRequestFactory; | ||
|
||
/** | ||
* @var StockByWebsiteIdResolver | ||
*/ | ||
private $stockByWebsiteIdResolver; | ||
|
||
/** | ||
* @param GetSkusByProductIdsInterface $getSkusByProductIds | ||
* @param ItemRequestInterfaceFactory $itemRequestFactory | ||
* @param StockByWebsiteIdResolver $stockByWebsiteIdResolver | ||
* @param InventoryRequestInterfaceFactory $inventoryRequestFactory | ||
*/ | ||
public function __construct( | ||
GetSkusByProductIdsInterface $getSkusByProductIds, | ||
ItemRequestInterfaceFactory $itemRequestFactory, | ||
StockByWebsiteIdResolver $stockByWebsiteIdResolver, | ||
InventoryRequestInterfaceFactory $inventoryRequestFactory | ||
) { | ||
$this->getSkusByProductIds = $getSkusByProductIds; | ||
$this->itemRequestFactory = $itemRequestFactory; | ||
$this->inventoryRequestFactory = $inventoryRequestFactory; | ||
$this->stockByWebsiteIdResolver = $stockByWebsiteIdResolver; | ||
} | ||
|
||
/** | ||
* @param InvoiceInterface $invoice | ||
* @return InventoryRequestInterface | ||
* @throws InputException | ||
*/ | ||
public function create(InvoiceInterface $invoice): InventoryRequestInterface | ||
{ | ||
$order = $invoice->getOrder(); | ||
$websiteId = $order->getStore()->getWebsiteId(); | ||
$stockId = (int)$this->stockByWebsiteIdResolver->get((int)$websiteId)->getStockId(); | ||
|
||
/** @var InventoryRequestInterface $inventoryRequest */ | ||
return $this->inventoryRequestFactory->create([ | ||
'stockId' => $stockId, | ||
'items' => $this->getSelectionRequestItems($invoice->getItems()) | ||
]); | ||
} | ||
|
||
/** | ||
* @param InvoiceItemInterface[]|Traversable $invoiceItems | ||
* @return array | ||
* @throws InputException | ||
*/ | ||
private function getSelectionRequestItems(Traversable $invoiceItems): array | ||
{ | ||
$selectionRequestItems = []; | ||
foreach ($invoiceItems as $invoiceItem) { | ||
if (!$invoiceItem->getOrderItem()->getIsVirtual()) { | ||
continue; | ||
} | ||
|
||
$itemSku = $invoiceItem->getSku() ?: $this->getSkusByProductIds->execute( | ||
[$invoiceItem->getProductId()] | ||
)[$invoiceItem->getProductId()]; | ||
$qty = $this->castQty($invoiceItem->getOrderItem(), $invoiceItem->getQty()); | ||
|
||
$selectionRequestItems[] = $this->itemRequestFactory->create([ | ||
'sku' => $itemSku, | ||
'qty' => $qty, | ||
]); | ||
} | ||
return $selectionRequestItems; | ||
} | ||
|
||
/** | ||
* @param OrderItemInterface $item | ||
* @param string|int|float $qty | ||
* @return float|int | ||
*/ | ||
private function castQty(OrderItemInterface $item, $qty) | ||
{ | ||
if ($item->getIsQtyDecimal()) { | ||
$qty = (double)$qty; | ||
} else { | ||
$qty = (int)$qty; | ||
} | ||
|
||
return $qty > 0 ? $qty : 0; | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
...ode/Magento/InventoryShipping/Model/SourceDeductionRequestsFromSourceSelectionFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\InventoryShipping\Model; | ||
|
||
use Magento\InventorySalesApi\Api\Data\SalesEventInterface; | ||
use Magento\InventoryShipping\Model\SourceDeduction\Request\ItemToDeductInterface; | ||
use Magento\InventoryShipping\Model\SourceDeduction\Request\ItemToDeductInterfaceFactory; | ||
use Magento\InventoryShipping\Model\SourceDeduction\Request\SourceDeductionRequestInterface; | ||
use Magento\InventoryShipping\Model\SourceDeduction\Request\SourceDeductionRequestInterfaceFactory; | ||
use Magento\InventorySourceSelectionApi\Api\Data\SourceSelectionItemInterface; | ||
use Magento\InventorySourceSelectionApi\Api\Data\SourceSelectionResultInterface; | ||
|
||
class SourceDeductionRequestsFromSourceSelectionFactory | ||
{ | ||
/** | ||
* @var SourceDeductionRequestInterfaceFactory | ||
*/ | ||
private $sourceDeductionRequestFactory; | ||
|
||
/** | ||
* @var ItemToDeductInterfaceFactory | ||
*/ | ||
private $itemToDeductFactory; | ||
|
||
/** | ||
* @param SourceDeductionRequestInterfaceFactory $sourceDeductionRequestFactory | ||
* @param ItemToDeductInterfaceFactory $itemToDeductFactory | ||
*/ | ||
public function __construct( | ||
SourceDeductionRequestInterfaceFactory $sourceDeductionRequestFactory, | ||
ItemToDeductInterfaceFactory $itemToDeductFactory | ||
) { | ||
$this->sourceDeductionRequestFactory = $sourceDeductionRequestFactory; | ||
$this->itemToDeductFactory = $itemToDeductFactory; | ||
} | ||
|
||
/** | ||
* @param SourceSelectionResultInterface $sourceSelectionResult | ||
* @param SalesEventInterface $salesEvent | ||
* @param int $websiteId | ||
* @return SourceDeductionRequestInterface[] | ||
*/ | ||
public function create( | ||
SourceSelectionResultInterface $sourceSelectionResult, | ||
SalesEventInterface $salesEvent, | ||
int $websiteId | ||
): array { | ||
$sourceDeductionRequests = []; | ||
foreach ($this->getItemsPerSource($sourceSelectionResult->getSourceSelectionItems()) as $sourceCode => $items) { | ||
/** @var SourceDeductionRequestInterface[] $sourceDeductionRequests */ | ||
$sourceDeductionRequests[] = $this->sourceDeductionRequestFactory->create([ | ||
'websiteId' => $websiteId, | ||
'sourceCode' => $sourceCode, | ||
'items' => $items, | ||
'salesEvent' => $salesEvent | ||
]); | ||
} | ||
return $sourceDeductionRequests; | ||
} | ||
|
||
/** | ||
* @param SourceSelectionItemInterface[] $sourceSelectionItems | ||
* @return ItemToDeductInterface[] | ||
*/ | ||
private function getItemsPerSource(array $sourceSelectionItems) | ||
{ | ||
$itemsPerSource = []; | ||
foreach ($sourceSelectionItems as $sourceSelectionItem) { | ||
if (!isset($itemsPerSource[$sourceSelectionItem->getSourceCode()])) { | ||
$itemsPerSource[$sourceSelectionItem->getSourceCode()] = []; | ||
} | ||
$itemsPerSource[$sourceSelectionItem->getSourceCode()][] = $this->itemToDeductFactory->create([ | ||
'sku' => $sourceSelectionItem->getSku(), | ||
'qty' => $sourceSelectionItem->getQtyToDeduct(), | ||
]); | ||
} | ||
return $itemsPerSource; | ||
} | ||
} |
Oops, something went wrong.