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#900 from magento-engcom/msi-846-quoteitemi…
…d-into-reservation Add OrderId into the Metadata attribute of Reservation entity
- Loading branch information
Showing
21 changed files
with
656 additions
and
97 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
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,68 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\InventorySales\Model; | ||
|
||
use Magento\InventorySalesApi\Api\Data\ItemToSellInterface; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
class ItemToSell implements ItemToSellInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $sku; | ||
|
||
/** | ||
* @var float | ||
*/ | ||
private $qty; | ||
|
||
/** | ||
* @param string $sku | ||
* @param float $qty | ||
*/ | ||
public function __construct(string $sku, float $qty) | ||
{ | ||
$this->sku = $sku; | ||
$this->qty = $qty; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getSku(): string | ||
{ | ||
return $this->sku; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getQuantity(): float | ||
{ | ||
return $this->qty; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function setSku(string $sku) | ||
{ | ||
$this->sku = $sku; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function setQuantity(float $qty) | ||
{ | ||
$this->qty = $qty; | ||
} | ||
} |
158 changes: 158 additions & 0 deletions
158
app/code/Magento/InventorySales/Model/PlaceReservationsForSalesEvent.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,158 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\InventorySales\Model; | ||
|
||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\InventoryCatalog\Model\GetProductTypesBySkusInterface; | ||
use Magento\InventoryCatalog\Model\GetSkusByProductIdsInterface; | ||
use Magento\InventoryConfiguration\Model\IsSourceItemsAllowedForProductTypeInterface; | ||
use Magento\InventoryReservations\Model\ReservationBuilderInterface; | ||
use Magento\InventoryReservationsApi\Api\AppendReservationsInterface; | ||
use Magento\InventorySalesApi\Api\Data\ItemToSellInterface; | ||
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface; | ||
use Magento\InventorySalesApi\Api\Data\SalesEventInterface; | ||
use Magento\InventorySalesApi\Api\IsProductSalableForRequestedQtyInterface; | ||
use Magento\InventorySalesApi\Api\PlaceReservationsForSalesEventInterface; | ||
use Magento\InventorySalesApi\Api\StockResolverInterface; | ||
use Magento\InventorySalesApi\Api\Data\ProductSalableResultInterface; | ||
use Magento\InventorySalesApi\Api\Data\ProductSalabilityErrorInterface; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
class PlaceReservationsForSalesEvent implements PlaceReservationsForSalesEventInterface | ||
{ | ||
/** | ||
* @var GetSkusByProductIdsInterface | ||
*/ | ||
private $getSkusByProductIds; | ||
|
||
/** | ||
* @var StockByWebsiteIdResolver | ||
*/ | ||
private $stockByWebsiteIdResolver; | ||
|
||
/** | ||
* @var ReservationBuilderInterface | ||
*/ | ||
private $reservationBuilder; | ||
|
||
/** | ||
* @var AppendReservationsInterface | ||
*/ | ||
private $appendReservations; | ||
|
||
/** | ||
* @var IsProductSalableForRequestedQtyInterface | ||
*/ | ||
private $isProductSalableForRequestedQty; | ||
|
||
/** | ||
* @var IsSourceItemsAllowedForProductTypeInterface | ||
*/ | ||
private $isSourceItemsAllowedForProductType; | ||
|
||
/* | ||
* @var GetProductTypesBySkusInterface | ||
*/ | ||
private $getProductTypesBySkus; | ||
|
||
/** | ||
* @var StockResolverInterface | ||
*/ | ||
private $stockResolver; | ||
|
||
public function __construct( | ||
GetSkusByProductIdsInterface $getSkusByProductIds, | ||
StockByWebsiteIdResolver $stockByWebsiteIdResolver, | ||
ReservationBuilderInterface $reservationBuilder, | ||
AppendReservationsInterface $appendReservations, | ||
IsProductSalableForRequestedQtyInterface $isProductSalableForRequestedQty, | ||
StockResolverInterface $stockResolver, | ||
IsSourceItemsAllowedForProductTypeInterface $isSourceItemsAllowedForProductType, | ||
GetProductTypesBySkusInterface $getProductTypesBySkus | ||
) { | ||
$this->getSkusByProductIds = $getSkusByProductIds; | ||
$this->stockByWebsiteIdResolver = $stockByWebsiteIdResolver; | ||
$this->reservationBuilder = $reservationBuilder; | ||
$this->appendReservations = $appendReservations; | ||
$this->isProductSalableForRequestedQty = $isProductSalableForRequestedQty; | ||
$this->stockResolver = $stockResolver; | ||
$this->isSourceItemsAllowedForProductType = $isSourceItemsAllowedForProductType; | ||
$this->getProductTypesBySkus = $getProductTypesBySkus; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function execute(array $items, SalesChannelInterface $salesChannel, SalesEventInterface $salesEvent) | ||
{ | ||
if (empty($items)) { | ||
return; | ||
} | ||
|
||
if (null === $salesChannel) { | ||
throw new LocalizedException(__('$salesChannel parameter is required')); | ||
} | ||
|
||
// TODO typecast needed because StockInterface::getStockId() returns string => fix StockInterface::getStockId? | ||
$stockId = (int)$this->stockResolver->get($salesChannel->getType(), $salesChannel->getCode())->getStockId(); | ||
|
||
$skus = array_map( | ||
function (ItemToSellInterface $item) { | ||
return $item->getSku(); | ||
}, $items | ||
); | ||
$productTypes = $this->getProductTypesBySkus->execute($skus); | ||
$this->checkItemsQuantity($items, $productTypes, $stockId); | ||
$reservations = []; | ||
/** @var ItemToSellInterface $item */ | ||
foreach ($items as $item) { | ||
$reservations[] = $this->reservationBuilder | ||
->setSku($item->getSku()) | ||
->setQuantity(-$item->getQuantity()) | ||
->setStockId($stockId) | ||
->setMetadata(sprintf( | ||
'%s:%s:%s', | ||
$salesEvent->getType(), | ||
$salesEvent->getObjectType(), | ||
$salesEvent->getObjectId() | ||
)) | ||
->build(); | ||
} | ||
$this->appendReservations->execute($reservations); | ||
} | ||
|
||
/** | ||
* Check is all items salable | ||
* | ||
* @return void | ||
* @throws LocalizedException | ||
*/ | ||
private function checkItemsQuantity(array $items, array $productTypes, int $stockId) | ||
{ | ||
/** @var ItemToSellInterface $item */ | ||
foreach ($items as $item) { | ||
if (false === $this->isSourceItemsAllowedForProductType->execute($productTypes[$item->getSku()])) { | ||
continue; | ||
} | ||
/** @var ProductSalableResultInterface $isSalable */ | ||
$isSalable = $this->isProductSalableForRequestedQty->execute( | ||
$item->getSku(), | ||
$stockId, | ||
$item->getQuantity() | ||
); | ||
if (false === $isSalable->isSalable()) { | ||
$errors = $isSalable->getErrors(); | ||
/** @var ProductSalabilityErrorInterface $errorMessage */ | ||
$errorMessage = array_pop($errors); | ||
throw new LocalizedException(__($errorMessage->getMessage())); | ||
} | ||
} | ||
} | ||
} |
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,67 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\InventorySales\Model; | ||
|
||
use Magento\InventorySalesApi\Api\Data\SalesEventInterface; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
class SalesEvent implements SalesEventInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $type; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $objectType; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $objectId; | ||
|
||
/** | ||
* @param string $type | ||
* @param string $objectType | ||
* @param string $objectId | ||
*/ | ||
public function __construct(string $type, string $objectType, string $objectId) | ||
{ | ||
$this->type = $type; | ||
$this->objectType = $objectType; | ||
$this->objectId = $objectId; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getType(): string | ||
{ | ||
return $this->type; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getObjectType(): string | ||
{ | ||
return $this->objectType; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getObjectId(): string | ||
{ | ||
return $this->objectId; | ||
} | ||
} |
Oops, something went wrong.