Configuration.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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\Helper\Minsaleqty as MinsaleqtyHelper;
  9. use Magento\Framework\App\Config\ScopeConfigInterface;
  10. use Magento\Catalog\Model\ProductTypes\ConfigInterface;
  11. use Magento\Store\Model\ScopeInterface;
  12. use Magento\Store\Model\StoreManagerInterface;
  13. /**
  14. * Class Configuration
  15. */
  16. class Configuration implements StockConfigurationInterface
  17. {
  18. /**
  19. * Default website id
  20. */
  21. const DEFAULT_WEBSITE_ID = 1;
  22. /**
  23. * Inventory options config path
  24. */
  25. const XML_PATH_GLOBAL = 'cataloginventory/options/';
  26. /**
  27. * Subtract config path
  28. */
  29. const XML_PATH_CAN_SUBTRACT = 'cataloginventory/options/can_subtract';
  30. /**
  31. * Back in stock config path
  32. */
  33. const XML_PATH_CAN_BACK_IN_STOCK = 'cataloginventory/options/can_back_in_stock';
  34. /**
  35. * Item options config path
  36. */
  37. const XML_PATH_ITEM = 'cataloginventory/item_options/';
  38. /**
  39. * Max qty config path
  40. */
  41. const XML_PATH_MIN_QTY = 'cataloginventory/item_options/min_qty';
  42. /**
  43. * Min sale qty config path
  44. */
  45. const XML_PATH_MIN_SALE_QTY = 'cataloginventory/item_options/min_sale_qty';
  46. /**
  47. * Max sale qty config path
  48. */
  49. const XML_PATH_MAX_SALE_QTY = 'cataloginventory/item_options/max_sale_qty';
  50. /**
  51. * Back orders config path
  52. */
  53. const XML_PATH_BACKORDERS = 'cataloginventory/item_options/backorders';
  54. /**
  55. * Notify stock config path
  56. */
  57. const XML_PATH_NOTIFY_STOCK_QTY = 'cataloginventory/item_options/notify_stock_qty';
  58. /**
  59. * Manage stock config path
  60. */
  61. const XML_PATH_MANAGE_STOCK = 'cataloginventory/item_options/manage_stock';
  62. /**
  63. * Enable qty increments config path
  64. */
  65. const XML_PATH_ENABLE_QTY_INCREMENTS = 'cataloginventory/item_options/enable_qty_increments';
  66. /**
  67. * Qty increments config path
  68. */
  69. const XML_PATH_QTY_INCREMENTS = 'cataloginventory/item_options/qty_increments';
  70. /**
  71. * Show out of stock config path
  72. */
  73. const XML_PATH_SHOW_OUT_OF_STOCK = 'cataloginventory/options/show_out_of_stock';
  74. /**
  75. * Auto return config path
  76. */
  77. const XML_PATH_ITEM_AUTO_RETURN = 'cataloginventory/item_options/auto_return';
  78. /**
  79. * Path to configuration option 'Display product stock status'
  80. */
  81. const XML_PATH_DISPLAY_PRODUCT_STOCK_STATUS = 'cataloginventory/options/display_product_stock_status';
  82. /**
  83. * Threshold qty config path
  84. */
  85. const XML_PATH_STOCK_THRESHOLD_QTY = 'cataloginventory/options/stock_threshold_qty';
  86. /**
  87. * @var ConfigInterface
  88. */
  89. protected $config;
  90. /**
  91. * Core store config
  92. *
  93. * @var ScopeConfigInterface
  94. */
  95. protected $scopeConfig;
  96. /**
  97. * @var MinsaleqtyHelper
  98. */
  99. protected $minsaleqtyHelper;
  100. /**
  101. * All product types registry in scope of quantity availability
  102. *
  103. * @var array
  104. */
  105. protected $isQtyTypeIds;
  106. /**
  107. * @var StoreManagerInterface
  108. */
  109. protected $storeManager;
  110. /**
  111. * Configuration constructor.
  112. *
  113. * @param ConfigInterface $config
  114. * @param ScopeConfigInterface $scopeConfig
  115. * @param MinsaleqtyHelper $minsaleqtyHelper
  116. * @param StoreManagerInterface $storeManager
  117. */
  118. public function __construct(
  119. ConfigInterface $config,
  120. ScopeConfigInterface $scopeConfig,
  121. MinsaleqtyHelper $minsaleqtyHelper,
  122. StoreManagerInterface $storeManager
  123. ) {
  124. $this->config = $config;
  125. $this->scopeConfig = $scopeConfig;
  126. $this->minsaleqtyHelper = $minsaleqtyHelper;
  127. $this->storeManager = $storeManager;
  128. }
  129. /**
  130. * @inheritdoc
  131. */
  132. public function getDefaultScopeId()
  133. {
  134. // TODO: should be fixed in MAGETWO-46043
  135. // "0" is id of admin website, which is used in backend during save entity
  136. return 0;
  137. }
  138. /**
  139. * Is Qty Type Ids
  140. *
  141. * @param int|null $filter
  142. * @return array
  143. */
  144. public function getIsQtyTypeIds($filter = null)
  145. {
  146. if (null === $this->isQtyTypeIds) {
  147. $this->isQtyTypeIds = [];
  148. foreach ($this->config->getAll() as $typeId => $typeConfig) {
  149. $this->isQtyTypeIds[$typeId] = isset($typeConfig['is_qty']) ? $typeConfig['is_qty'] : false;
  150. }
  151. }
  152. $result = $this->isQtyTypeIds;
  153. if ($filter !== null) {
  154. foreach ($result as $key => $value) {
  155. if ($value !== $filter) {
  156. unset($result[$key]);
  157. }
  158. }
  159. }
  160. return $result;
  161. }
  162. /**
  163. * Is Qty
  164. *
  165. * @param int $productTypeId
  166. * @return bool
  167. */
  168. public function isQty($productTypeId)
  169. {
  170. $result = $this->getIsQtyTypeIds();
  171. return isset($result[$productTypeId]) ? $result[$productTypeId] : false;
  172. }
  173. /**
  174. * Check if is possible subtract value from item qty
  175. *
  176. * @param null|string|bool|int|\Magento\Store\Model\Store $store
  177. * @return bool
  178. */
  179. public function canSubtractQty($store = null)
  180. {
  181. return $this->scopeConfig->isSetFlag(
  182. self::XML_PATH_CAN_SUBTRACT,
  183. ScopeInterface::SCOPE_STORE,
  184. $store
  185. );
  186. }
  187. /**
  188. * Get Min Qty
  189. *
  190. * @param null|string|bool|int|\Magento\Store\Model\Store $store
  191. * @return float
  192. */
  193. public function getMinQty($store = null)
  194. {
  195. return (float)$this->scopeConfig->getValue(
  196. self::XML_PATH_MIN_QTY,
  197. ScopeInterface::SCOPE_STORE,
  198. $store
  199. );
  200. }
  201. /**
  202. * Get Min Sale Qty
  203. *
  204. * @param null|string|bool|int|\Magento\Store\Model\Store $store
  205. * @param int $customerGroupId
  206. * @return float
  207. */
  208. public function getMinSaleQty($store = null, $customerGroupId = null)
  209. {
  210. return (float)$this->minsaleqtyHelper->getConfigValue($customerGroupId, $store);
  211. }
  212. /**
  213. * Get Max Sale Qty
  214. *
  215. * @param null|string|bool|int|\Magento\Store\Model\Store $store
  216. * @return float|null
  217. */
  218. public function getMaxSaleQty($store = null)
  219. {
  220. return (float)$this->scopeConfig->getValue(
  221. self::XML_PATH_MAX_SALE_QTY,
  222. ScopeInterface::SCOPE_STORE,
  223. $store
  224. );
  225. }
  226. /**
  227. * Get Notify Stock Qty
  228. *
  229. * @param null|string|bool|int|\Magento\Store\Model\Store $store
  230. * @return float
  231. */
  232. public function getNotifyStockQty($store = null)
  233. {
  234. return (float) $this->scopeConfig->getValue(
  235. self::XML_PATH_NOTIFY_STOCK_QTY,
  236. ScopeInterface::SCOPE_STORE,
  237. $store
  238. );
  239. }
  240. /**
  241. * Retrieve whether Quantity Increments is enabled
  242. *
  243. * @param null|string|bool|int|\Magento\Store\Model\Store $store
  244. * @return bool
  245. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  246. */
  247. public function getEnableQtyIncrements($store = null)
  248. {
  249. return $this->scopeConfig->isSetFlag(
  250. self::XML_PATH_ENABLE_QTY_INCREMENTS,
  251. ScopeInterface::SCOPE_STORE,
  252. $store
  253. );
  254. }
  255. /**
  256. * Get Qty Increments
  257. *
  258. * @param null|string|bool|int|\Magento\Store\Model\Store $store
  259. * @return float
  260. */
  261. public function getQtyIncrements($store = null)
  262. {
  263. return (float)$this->scopeConfig->getValue(
  264. self::XML_PATH_QTY_INCREMENTS,
  265. ScopeInterface::SCOPE_STORE,
  266. $store
  267. );
  268. }
  269. /**
  270. * Retrieve backorders status
  271. *
  272. * @param null|string|bool|int|\Magento\Store\Model\Store $store
  273. * @return int
  274. */
  275. public function getBackorders($store = null)
  276. {
  277. return (int) $this->scopeConfig->getValue(
  278. self::XML_PATH_BACKORDERS,
  279. ScopeInterface::SCOPE_STORE,
  280. $store
  281. );
  282. }
  283. /**
  284. * Retrieve Manage Stock data wrapper
  285. *
  286. * @param null|string|bool|int|\Magento\Store\Model\Store $store
  287. * @return int
  288. */
  289. public function getManageStock($store = null)
  290. {
  291. return (int) $this->scopeConfig->isSetFlag(
  292. self::XML_PATH_MANAGE_STOCK,
  293. ScopeInterface::SCOPE_STORE,
  294. $store
  295. );
  296. }
  297. /**
  298. * Retrieve can Back in stock
  299. *
  300. * @param null|string|bool|int|\Magento\Store\Model\Store $store
  301. * @return bool
  302. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  303. */
  304. public function getCanBackInStock($store = null)
  305. {
  306. return $this->scopeConfig->isSetFlag(
  307. self::XML_PATH_CAN_BACK_IN_STOCK,
  308. ScopeInterface::SCOPE_STORE,
  309. $store
  310. );
  311. }
  312. /**
  313. * Display out of stock products option
  314. *
  315. * @param null|string|bool|int|\Magento\Store\Model\Store $store
  316. * @return bool
  317. */
  318. public function isShowOutOfStock($store = null)
  319. {
  320. return $this->scopeConfig->isSetFlag(
  321. self::XML_PATH_SHOW_OUT_OF_STOCK,
  322. ScopeInterface::SCOPE_STORE,
  323. $store
  324. );
  325. }
  326. /**
  327. * Check if credit memo items auto return option is enabled
  328. *
  329. * @param null|string|bool|int|\Magento\Store\Model\Store $store
  330. * @return bool
  331. */
  332. public function isAutoReturnEnabled($store = null)
  333. {
  334. return $this->scopeConfig->isSetFlag(
  335. self::XML_PATH_ITEM_AUTO_RETURN,
  336. ScopeInterface::SCOPE_STORE,
  337. $store
  338. );
  339. }
  340. /**
  341. * Display product stock status. Shows if it is necessary to show product stock status in stock/out of stock.
  342. *
  343. * @param null|string|bool|int|\Magento\Store\Model\Store $store
  344. * @return bool
  345. */
  346. public function isDisplayProductStockStatus($store = null)
  347. {
  348. return $this->scopeConfig->isSetFlag(
  349. self::XML_PATH_DISPLAY_PRODUCT_STOCK_STATUS,
  350. ScopeInterface::SCOPE_STORE,
  351. $store
  352. );
  353. }
  354. /**
  355. * Get Default Config Value
  356. *
  357. * @param string $field
  358. * @param null|string|bool|int|\Magento\Store\Model\Store $store
  359. * @return string|null
  360. */
  361. public function getDefaultConfigValue($field, $store = null)
  362. {
  363. return $this->scopeConfig->getValue(
  364. self::XML_PATH_ITEM . $field,
  365. ScopeInterface::SCOPE_STORE,
  366. $store
  367. );
  368. }
  369. /**
  370. * Get Stock Threshold Qty
  371. *
  372. * @param null|string|bool|int|\Magento\Store\Model\Store $store
  373. * @return string|null
  374. */
  375. public function getStockThresholdQty($store = null)
  376. {
  377. return $this->scopeConfig->getValue(
  378. self::XML_PATH_STOCK_THRESHOLD_QTY,
  379. ScopeInterface::SCOPE_STORE,
  380. $store
  381. );
  382. }
  383. /**
  384. * Retrieve inventory item options (used in config)
  385. *
  386. * @return string[]
  387. */
  388. public function getConfigItemOptions()
  389. {
  390. return [
  391. 'min_qty',
  392. 'backorders',
  393. 'min_sale_qty',
  394. 'max_sale_qty',
  395. 'notify_stock_qty',
  396. 'manage_stock',
  397. 'enable_qty_increments',
  398. 'qty_increments',
  399. 'is_decimal_divided'
  400. ];
  401. }
  402. }