Skip to content

Commit

Permalink
MC-23216: Unable to create Credit Memo
Browse files Browse the repository at this point in the history
- Fix undefined index caused by mismatch of SKU case when creating credit memo
  • Loading branch information
bubasuma committed Nov 20, 2019
1 parent 470cd8c commit 89b5f02
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ public function execute(

$itemsTdDeliver = [];
foreach ($inventoryRequest->getItems() as $item) {
$itemsTdDeliver[$item->getSku()] = $item->getQty();
$normalizedSku = $this->normalizeSku($item->getSku());
$itemsTdDeliver[$normalizedSku] = $item->getQty();
}

$sortedSourceCodes = [];
Expand All @@ -110,17 +111,20 @@ public function execute(
);

foreach ($sourceItems as $sourceItem) {
$normalizedSku = $this->normalizeSku($sourceItem->getSku());
$sourceItemQtyAvailable = $this->getSourceItemQtyAvailable->execute($sourceItem);
$qtyToDeduct = min($sourceItemQtyAvailable, $itemsTdDeliver[$sourceItem->getSku()] ?? 0.0);

$sourceItemSelections[] = $this->sourceSelectionItemFactory->create([
'sourceCode' => $sourceItem->getSourceCode(),
'sku' => $sourceItem->getSku(),
'qtyToDeduct' => $qtyToDeduct,
'qtyAvailable' => $sourceItemQtyAvailable
]);
$qtyToDeduct = min($sourceItemQtyAvailable, $itemsTdDeliver[$normalizedSku] ?? 0.0);

$sourceItemSelections[] = $this->sourceSelectionItemFactory->create(
[
'sourceCode' => $sourceItem->getSourceCode(),
'sku' => $sourceItem->getSku(),
'qtyToDeduct' => $qtyToDeduct,
'qtyAvailable' => $sourceItemQtyAvailable
]
);

$itemsTdDeliver[$sourceItem->getSku()] -= $qtyToDeduct;
$itemsTdDeliver[$normalizedSku] -= $qtyToDeduct;
}

$isShippable = true;
Expand All @@ -138,4 +142,17 @@ public function execute(
]
);
}

/**
* Convert SKU to lowercase
*
* Normalize SKU by converting it to lowercase.
*
* @param string $sku
* @return string
*/
private function normalizeSku(string $sku): string
{
return mb_convert_case($sku, MB_CASE_LOWER, 'UTF-8');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,12 @@ public function testShouldReturnDefaultResults(
$requestItems[] = $this->itemRequestFactory->create($requestItemData);
}

$inventoryRequest = $this->inventoryRequestFactory->create([
'stockId' => $stockId,
'items' => $requestItems
]);
$inventoryRequest = $this->inventoryRequestFactory->create(
[
'stockId' => $stockId,
'items' => $requestItems
]
);

$sortedSources = [];
foreach ($sortedSourcesCodes as $sortedSourceCode) {
Expand All @@ -185,4 +187,28 @@ public function testShouldReturnDefaultResults(
self::assertSame((float) $expected[$key]['avail'], $selectionItem->getQtyAvailable());
}
}

/**
* Check that "Undefined index" exception is not thrown when SKU case does not match
*
* SKU is not updated in source item table if the new SKU value is insensitively equal to the old value
*
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
*/
public function testShouldNotThrowExceptionIfSkuCaseDoesNotMatch()
{
$requestItems[] = $this->itemRequestFactory->create(['sku' => 'sku-1', 'qty' => 15]);
$inventoryRequest = $this->inventoryRequestFactory->create(
[
'stockId' => 10,
'items' => $requestItems
]
);
$sortedSources = [];
$sortedSources[] = $this->sourceRepository->get('eu-1');
$this->assertNotNull($this->subject->execute($inventoryRequest, $sortedSources));
}
}

0 comments on commit 89b5f02

Please sign in to comment.