Skip to content

Commit f774303

Browse files
committed
MAGETWO-99371: Wrong calculation of invoiced items
- Issue testing scenario covering
1 parent 6a9860f commit f774303

File tree

2 files changed

+100
-5
lines changed

2 files changed

+100
-5
lines changed

dev/tests/integration/testsuite/Magento/Sales/Model/Service/InvoiceServiceTest.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,99 @@ public function prepareInvoiceSimpleProductDataProvider()
8989
'partial invoice' => [1]
9090
];
9191
}
92+
93+
/**
94+
* Checks if ordered and invoiced qty of bundle product does match.
95+
*
96+
* @param array $qtyToInvoice
97+
* @param array $qtyInvoiced
98+
* @param string $errorMsg
99+
* @return void
100+
* @throws \Magento\Framework\Exception\LocalizedException
101+
* @magentoDataFixture Magento/Sales/_files/order_with_bundle.php
102+
* @dataProvider bundleProductQtyOrderedDataProvider
103+
*/
104+
public function testPrepareInvoiceBundleProduct(
105+
array $qtyToInvoice,
106+
array $qtyInvoiced,
107+
string $errorMsg
108+
): void
109+
{
110+
/** @var Order $order */
111+
$order = Bootstrap::getObjectManager()->create(Order::class)
112+
->load('100000001', 'increment_id');
113+
114+
$predefinedQtyToInvoice = $this->getPredefinedQtyToInvoice($order, $qtyToInvoice);
115+
$invoice = $this->invoiceService->prepareInvoice($order, $predefinedQtyToInvoice);
116+
117+
foreach ($invoice->getItems() as $invoiceItem) {
118+
if (isset($qtyInvoiced[$invoiceItem->getSku()])) {
119+
$this->assertEquals(
120+
$qtyInvoiced[$invoiceItem->getSku()],
121+
$invoiceItem->getQty(),
122+
sprintf($errorMsg, $invoiceItem->getSku())
123+
);
124+
}
125+
}
126+
}
127+
128+
/**
129+
* Data provider for invoice creation with and w/o predefined qty to invoice.
130+
*
131+
* @return array
132+
*/
133+
public function bundleProductQtyOrderedDataProvider(): array
134+
{
135+
return [
136+
'Create invoice w/o predefined qty' => [
137+
'Qty to invoice' => [],
138+
'Qty ordered' => [
139+
'bundle_1' => 2,
140+
'bundle_simple_1' => 10,
141+
],
142+
'Error msg' => 'Invoiced qty for product %s does not match.',
143+
],
144+
'Create invoice with predefined qty' => [
145+
'Qty to invoice' => [
146+
'bundle_1' => 2,
147+
'bundle_simple_1' => 10,
148+
],
149+
'Qty ordered' => [
150+
'bundle_1' => 2,
151+
'bundle_simple_1' => 10,
152+
],
153+
'Error msg' => 'Invoiced qty for product %s does not match.',
154+
],
155+
'Create invoice with partial predefined qty for bundle' => [
156+
'Qty to invoice' => [
157+
'bundle_1' => 1,
158+
],
159+
'Qty ordered' => [
160+
'bundle_1' => 1,
161+
'bundle_simple_1' => 5,
162+
],
163+
'Error msg' => 'Invoiced qty for product %s does not match.',
164+
],
165+
];
166+
}
167+
168+
/**
169+
* Associate product qty to invoice to order item id.
170+
*
171+
* @param Order $order
172+
* @param array $qtyToInvoice
173+
* @return array
174+
*/
175+
private function getPredefinedQtyToInvoice(Order $order, array $qtyToInvoice): array
176+
{
177+
$predefinedQtyToInvoice = [];
178+
179+
foreach ($order->getAllItems() as $orderItem) {
180+
if (array_key_exists($orderItem->getSku(), $qtyToInvoice)) {
181+
$predefinedQtyToInvoice[$orderItem->getId()] = $qtyToInvoice[$orderItem->getSku()];
182+
}
183+
}
184+
185+
return $predefinedQtyToInvoice;
186+
}
92187
}

dev/tests/integration/testsuite/Magento/Sales/_files/order_with_bundle.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818

1919
$orderItems = [
2020
[
21+
OrderItemInterface::SKU => 'bundle_1',
22+
OrderItemInterface::NAME => 'bundle_1',
2123
OrderItemInterface::PRODUCT_ID => 2,
2224
OrderItemInterface::BASE_PRICE => 100,
2325
OrderItemInterface::ORDER_ID => $order->getId(),
2426
OrderItemInterface::QTY_ORDERED => 2,
25-
OrderItemInterface::QTY_INVOICED => 2,
2627
OrderItemInterface::PRICE => 100,
2728
OrderItemInterface::ROW_TOTAL => 102,
2829
OrderItemInterface::PRODUCT_TYPE => 'bundle',
@@ -35,18 +36,17 @@
3536
],
3637
'children' => [
3738
[
39+
OrderItemInterface::SKU => 'bundle_simple_1',
40+
OrderItemInterface::NAME => 'bundle_simple_1',
3841
OrderItemInterface::PRODUCT_ID => 13,
3942
OrderItemInterface::ORDER_ID => $order->getId(),
4043
OrderItemInterface::QTY_ORDERED => 10,
41-
OrderItemInterface::QTY_INVOICED => 10,
4244
OrderItemInterface::BASE_PRICE => 90,
4345
OrderItemInterface::PRICE => 90,
4446
OrderItemInterface::ROW_TOTAL => 92,
4547
OrderItemInterface::PRODUCT_TYPE => 'simple',
4648
'product_options' => [
47-
'bundle_selection_attributes' => [
48-
'qty' => 2,
49-
],
49+
'bundle_selection_attributes' => '{"qty":5}',
5050
],
5151
],
5252
],

0 commit comments

Comments
 (0)