-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathBackOrderNotifyCustomerCondition.php
More file actions
113 lines (100 loc) · 4.12 KB
/
BackOrderNotifyCustomerCondition.php
File metadata and controls
113 lines (100 loc) · 4.12 KB
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
<?php
/**
* Copyright 2018 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);
namespace Magento\InventorySales\Model\IsProductSalableCondition;
use Magento\Framework\App\ObjectManager;
use Magento\InventoryConfigurationApi\Api\Data\StockItemConfigurationInterface;
use Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface;
use Magento\InventorySalesApi\Api\Data\ProductSalabilityErrorInterfaceFactory;
use Magento\InventorySalesApi\Api\Data\ProductSalableResultInterface;
use Magento\InventorySalesApi\Api\Data\ProductSalableResultInterfaceFactory;
use Magento\InventorySalesApi\Api\IsProductSalableForRequestedQtyInterface;
use Magento\InventorySalesApi\Model\GetStockItemDataInterface;
use Magento\InventorySalesApi\Api\GetProductSalableQtyInterface;
use Magento\InventorySales\Model\GetBackorderQty;
/**
* Get back order notify for customer condition
*
* @inheritdoc
*/
class BackOrderNotifyCustomerCondition implements IsProductSalableForRequestedQtyInterface
{
/**
* @var GetStockItemConfigurationInterface
*/
private $getStockItemConfiguration;
/**
* @var ProductSalableResultInterfaceFactory
*/
private $productSalableResultFactory;
/**
* @var ProductSalabilityErrorInterfaceFactory
*/
private $productSalabilityErrorFactory;
/**
* @var GetBackorderQty
*/
private $getBackorderQty;
/**
* Product backorder qty's checked
*
* @var array
*/
private $backorderQty = [];
/**
* @param GetStockItemConfigurationInterface $getStockItemConfiguration
* @param GetStockItemDataInterface $getStockItemData @deprecated
* @param ProductSalableResultInterfaceFactory $productSalableResultFactory
* @param ProductSalabilityErrorInterfaceFactory $productSalabilityErrorFactory
* @param GetProductSalableQtyInterface|null $getProductSalableQty @deprecated
* @param GetBackorderQty|null $getBackorderQty
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function __construct(
GetStockItemConfigurationInterface $getStockItemConfiguration,
GetStockItemDataInterface $getStockItemData,
ProductSalableResultInterfaceFactory $productSalableResultFactory,
ProductSalabilityErrorInterfaceFactory $productSalabilityErrorFactory,
?GetProductSalableQtyInterface $getProductSalableQty = null,
?GetBackorderQty $getBackorderQty = null
) {
$this->getStockItemConfiguration = $getStockItemConfiguration;
$this->productSalableResultFactory = $productSalableResultFactory;
$this->productSalabilityErrorFactory = $productSalabilityErrorFactory;
$this->getBackorderQty = $getBackorderQty
?? ObjectManager::getInstance()->get(GetBackorderQty::class);
}
/**
* @inheritdoc
*/
public function execute(string $sku, int $stockId, float $requestedQty): ProductSalableResultInterface
{
$stockItemConfiguration = $this->getStockItemConfiguration->execute($sku, $stockId);
if ($stockItemConfiguration->isManageStock()
&& $stockItemConfiguration->getBackorders() === StockItemConfigurationInterface::BACKORDERS_YES_NOTIFY
) {
$backorderQty = $this->getBackorderQty->execute($sku, $stockId, $requestedQty);
if (isset($this->backorderQty[$sku])) {
$backorderQty -= $this->backorderQty[$sku];
} else {
$this->backorderQty[$sku] = $backorderQty;
}
if ($backorderQty > 0) {
$errors = [
$this->productSalabilityErrorFactory->create([
'code' => 'back_order-not-enough',
'message' => __(
'We don\'t have as many quantity as you requested, '
. 'but we\'ll back order the remaining %1.',
$backorderQty * 1
)])
];
return $this->productSalableResultFactory->create(['errors' => $errors]);
}
}
return $this->productSalableResultFactory->create(['errors' => []]);
}
}