@@ -89,4 +89,99 @@ public function prepareInvoiceSimpleProductDataProvider()
89
89
'partial invoice ' => [1 ]
90
90
];
91
91
}
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
+ }
92
187
}
0 commit comments