Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions InventorySales/Model/GetBackorder.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ class GetBackorder
*/
private $getBackorderQty;

/**
* Product backorder qty's checked
*
* @var array
*/
private $backorderQty = [];

/**
* @param ObjectFactory $objectFactory
* @param FormatInterface $format
Expand Down Expand Up @@ -161,6 +168,11 @@ public function execute(int $productId, $itemQty, $qtyToCheck, $scopeId): object
}
}
$backorderQty = $this->getBackorderQty->execute($productSku, (int)$stockId, $qty);
if (isset($this->backorderQty[$productSku])) {
$backorderQty -= $this->backorderQty[$productSku];
} else {
$this->backorderQty[$productSku] = $backorderQty;
}
if ($backorderQty > 0) {
$result->setItemBackorders($backorderQty);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ class BackOrderNotifyCustomerCondition implements IsProductSalableForRequestedQt
*/
private $getBackorderQty;

/**
* Product backorder qty's checked
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR introduces instance-level state in service classes, according to the Magento 2 coding guidelines "Services SHOULD be stateless".

*
* @var array
*/
private $backorderQty = [];

/**
* @param GetStockItemConfigurationInterface $getStockItemConfiguration
* @param GetStockItemDataInterface $getStockItemData @deprecated
Expand Down Expand Up @@ -81,6 +88,12 @@ public function execute(string $sku, int $stockId, float $requestedQty): Product
) {
$backorderQty = $this->getBackorderQty->execute($sku, $stockId, $requestedQty);

if (isset($this->backorderQty[$sku])) {
$backorderQty -= $this->backorderQty[$sku];
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the subtraction logic produces WRONG results.
It subtracts the previously stored backorder qty, resulting in 0 for the second item instead of showing the correct cumulative backorder.

for e.g.

Call Scenario Expected Actual with this code
1st Item A, qty=20, stock=10 backorder=10 backorder=10 ✓ (stored: 10)
2nd Item B, qty=20, stock=10 backorder=20 (cumulative) backorder=0 (10-10=0) ✗

} else {
$this->backorderQty[$sku] = $backorderQty;
}

if ($backorderQty > 0) {
$errors = [
$this->productSalabilityErrorFactory->create([
Expand Down