GetReservationsQuantity.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\InventoryReservations\Model\ResourceModel;
  8. use Magento\Framework\App\ResourceConnection;
  9. use Magento\InventoryReservationsApi\Model\ReservationInterface;
  10. use Magento\InventoryReservationsApi\Model\GetReservationsQuantityInterface;
  11. /**
  12. * @inheritdoc
  13. */
  14. class GetReservationsQuantity implements GetReservationsQuantityInterface
  15. {
  16. /**
  17. * @var ResourceConnection
  18. */
  19. private $resource;
  20. /**
  21. * @param ResourceConnection $resource
  22. */
  23. public function __construct(
  24. ResourceConnection $resource
  25. ) {
  26. $this->resource = $resource;
  27. }
  28. /**
  29. * @inheritdoc
  30. */
  31. public function execute(string $sku, int $stockId): float
  32. {
  33. $connection = $this->resource->getConnection();
  34. $reservationTable = $this->resource->getTableName('inventory_reservation');
  35. $select = $connection->select()
  36. ->from($reservationTable, [ReservationInterface::QUANTITY => 'SUM(' . ReservationInterface::QUANTITY . ')'])
  37. ->where(ReservationInterface::SKU . ' = ?', $sku)
  38. ->where(ReservationInterface::STOCK_ID . ' = ?', $stockId)
  39. ->limit(1);
  40. $reservationQty = $connection->fetchOne($select);
  41. if (false === $reservationQty) {
  42. $reservationQty = 0;
  43. }
  44. return (float)$reservationQty;
  45. }
  46. }