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