StockState.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CatalogInventory\Model;
  7. use Magento\CatalogInventory\Api\StockConfigurationInterface;
  8. use Magento\CatalogInventory\Api\StockStateInterface;
  9. use Magento\CatalogInventory\Model\Spi\StockRegistryProviderInterface;
  10. use Magento\CatalogInventory\Model\Spi\StockStateProviderInterface;
  11. /**
  12. * Interface StockState
  13. */
  14. class StockState implements StockStateInterface
  15. {
  16. /**
  17. * @var StockStateProviderInterface
  18. */
  19. protected $stockStateProvider;
  20. /**
  21. * @var StockRegistryProviderInterface
  22. */
  23. protected $stockRegistryProvider;
  24. /**
  25. * @var StockConfigurationInterface
  26. */
  27. protected $stockConfiguration;
  28. /**
  29. * @param StockStateProviderInterface $stockStateProvider
  30. * @param StockRegistryProviderInterface $stockRegistryProvider
  31. * @param StockConfigurationInterface $stockConfiguration
  32. */
  33. public function __construct(
  34. StockStateProviderInterface $stockStateProvider,
  35. StockRegistryProviderInterface $stockRegistryProvider,
  36. StockConfigurationInterface $stockConfiguration
  37. ) {
  38. $this->stockStateProvider = $stockStateProvider;
  39. $this->stockRegistryProvider = $stockRegistryProvider;
  40. $this->stockConfiguration = $stockConfiguration;
  41. }
  42. /**
  43. * @param int $productId
  44. * @param int $scopeId
  45. * @return bool
  46. */
  47. public function verifyStock($productId, $scopeId = null)
  48. {
  49. // if ($scopeId === null) {
  50. $scopeId = $this->stockConfiguration->getDefaultScopeId();
  51. // }
  52. $stockItem = $this->stockRegistryProvider->getStockItem($productId, $scopeId);
  53. return $this->stockStateProvider->verifyStock($stockItem);
  54. }
  55. /**
  56. * @param int $productId
  57. * @param int $scopeId
  58. * @return bool
  59. */
  60. public function verifyNotification($productId, $scopeId = null)
  61. {
  62. // if ($scopeId === null) {
  63. $scopeId = $this->stockConfiguration->getDefaultScopeId();
  64. // }
  65. $stockItem = $this->stockRegistryProvider->getStockItem($productId, $scopeId);
  66. return $this->stockStateProvider->verifyNotification($stockItem);
  67. }
  68. /**
  69. * Check quantity
  70. *
  71. * @param int $productId
  72. * @param float $qty
  73. * @param int $scopeId
  74. * @exception \Magento\Framework\Exception\LocalizedException
  75. * @return bool
  76. */
  77. public function checkQty($productId, $qty, $scopeId = null)
  78. {
  79. // if ($scopeId === null) {
  80. $scopeId = $this->stockConfiguration->getDefaultScopeId();
  81. // }
  82. $stockItem = $this->stockRegistryProvider->getStockItem($productId, $scopeId);
  83. return $this->stockStateProvider->checkQty($stockItem, $qty);
  84. }
  85. /**
  86. * Returns suggested qty that satisfies qty increments and minQty/maxQty/minSaleQty/maxSaleQty conditions
  87. * or original qty if such value does not exist
  88. *
  89. * @param int $productId
  90. * @param float $qty
  91. * @param int $scopeId
  92. * @return float
  93. */
  94. public function suggestQty($productId, $qty, $scopeId = null)
  95. {
  96. // if ($scopeId === null) {
  97. $scopeId = $this->stockConfiguration->getDefaultScopeId();
  98. // }
  99. $stockItem = $this->stockRegistryProvider->getStockItem($productId, $scopeId);
  100. return $this->stockStateProvider->suggestQty($stockItem, $qty);
  101. }
  102. /**
  103. * Retrieve stock qty whether product is composite or no
  104. *
  105. * @param int $productId
  106. * @param int $scopeId
  107. * @return float
  108. */
  109. public function getStockQty($productId, $scopeId = null)
  110. {
  111. // if ($scopeId === null) {
  112. $scopeId = $this->stockConfiguration->getDefaultScopeId();
  113. // }
  114. $stockItem = $this->stockRegistryProvider->getStockItem($productId, $scopeId);
  115. return $this->stockStateProvider->getStockQty($stockItem);
  116. }
  117. /**
  118. * @param int $productId
  119. * @param float $qty
  120. * @param int $websiteId
  121. * @return \Magento\Framework\DataObject
  122. */
  123. public function checkQtyIncrements($productId, $qty, $websiteId = null)
  124. {
  125. // if ($websiteId === null) {
  126. $websiteId = $this->stockConfiguration->getDefaultScopeId();
  127. // }
  128. $stockItem = $this->stockRegistryProvider->getStockItem($productId, $websiteId);
  129. return $this->stockStateProvider->checkQtyIncrements($stockItem, $qty);
  130. }
  131. /**
  132. * @param int $productId
  133. * @param float $itemQty
  134. * @param float $qtyToCheck
  135. * @param float $origQty
  136. * @param int $scopeId
  137. * @return int
  138. */
  139. public function checkQuoteItemQty($productId, $itemQty, $qtyToCheck, $origQty, $scopeId = null)
  140. {
  141. // if ($scopeId === null) {
  142. $scopeId = $this->stockConfiguration->getDefaultScopeId();
  143. // }
  144. $stockItem = $this->stockRegistryProvider->getStockItem($productId, $scopeId);
  145. return $this->stockStateProvider->checkQuoteItemQty($stockItem, $itemQty, $qtyToCheck, $origQty);
  146. }
  147. }