Skip to content

Commit

Permalink
Merge pull request magento#900 from magento-engcom/msi-846-quoteitemi…
Browse files Browse the repository at this point in the history
…d-into-reservation

Add OrderId into the Metadata attribute of Reservation entity
  • Loading branch information
maghamed authored Apr 14, 2018
2 parents 201bc31 + 418f846 commit 45bd957
Show file tree
Hide file tree
Showing 21 changed files with 656 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public function getQtyIncrements(): float
{
$qtyIncrements = $this->stockItem->getQtyIncrements();
if (false === $qtyIncrements) {
return 1;
return 0;
}
return $qtyIncrements;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,8 @@ private function isQuantityIncrementCheckFailed(
float $requestedQty
) : bool {
// Qty Increments
if ($this->mathDivision->getExactDivision($requestedQty, $this->configuration->getQtyIncrements()) !== 0
|| $this->mathDivision->getExactDivision(
$requestedQty,
$stockItemConfiguration->getQtyIncrements()
) !== 0) {
$qtyIncrements = $stockItemConfiguration->getQtyIncrements();
if ($qtyIncrements !== (float)0 && $this->mathDivision->getExactDivision($requestedQty, $qtyIncrements) !== 0) {
return true;
}
return false;
Expand Down
68 changes: 68 additions & 0 deletions app/code/Magento/InventorySales/Model/ItemToSell.php
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;
}
}
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()));
}
}
}
}
67 changes: 67 additions & 0 deletions app/code/Magento/InventorySales/Model/SalesEvent.php
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;
}
}
Loading

0 comments on commit 45bd957

Please sign in to comment.