Skip to content

Commit 126d3c3

Browse files
authored
MDEE-445: Export invoice items in SalesOrdersDataExporter (#267)
* MDEE-462: Fix multiLineStreet value is overridden by each address * MDEE-445: Export invoice data to order aggregates
1 parent 4774f3e commit 126d3c3

17 files changed

+89
-23
lines changed

SalesOrdersDataExporter/Model/Provider/Items/OrderItemAdjustment.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ public function __construct(
3030
}
3131

3232
/**
33-
* @param array $values
33+
* Retrieves and processes order items.
34+
*
35+
* @param array $values
3436
* @return array
3537
*/
3638
public function get(array $values): array
@@ -46,18 +48,20 @@ public function get(array $values): array
4648
}
4749

4850
/**
49-
* @param array $row
51+
* Processes a single row of order item data.
52+
*
53+
* @param array $row
5054
* @return bool
5155
*/
5256
private function processRow(array $row) : bool
5357
{
54-
if (!isset($row['productType'], $row['product_options'])) {
58+
if (!isset($row['productType'], $row['productOptions'])) {
5559
return false;
5660
}
5761

5862
$result = false;
5963
if ($row['productType'] === self::PRODUCT_TYPE_BUNDLE) {
60-
$productOptions = $this->serializer->unserialize($row['product_options']);
64+
$productOptions = $this->serializer->unserialize($row['productOptions']);
6165
$result = isset($productOptions['shipment_type'])
6266
&& ((int)$productOptions['shipment_type'] === 0);
6367
}

SalesOrdersDataExporter/Model/Provider/MultiLineStreet.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ class MultiLineStreet
1313

1414
/**
1515
* Splits the 'street' field (only when present) using newline char as separator.
16-
* @param array $values containing 'street' field
16+
*
17+
* @param array $values containing 'street' field
1718
* @return array containing 'multi_line_street' field
1819
*/
1920
public function get(array $values): array
@@ -24,6 +25,7 @@ public function get(array $values): array
2425
foreach ((explode("\n", $value['street'])) as $streetLine) {
2526
$output[] = [
2627
'commerceOrderId' => $value['commerceOrderId'],
28+
'addressType' => $value['addressType'],
2729
'multiLineStreet' => $streetLine
2830
];
2931
}

SalesOrdersDataExporter/Tests/Integration/AbstractOrderFeedTest.php renamed to SalesOrdersDataExporter/Test/Integration/AbstractOrderFeedTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
declare(strict_types=1);
88

9-
namespace Magento\SalesOrdersDataExporter\Tests\Integration;
9+
namespace Magento\SalesOrdersDataExporter\Test\Integration;
1010

1111
use Magento\Catalog\Api\ProductRepositoryInterface;
1212
use Magento\DataExporter\Model\FeedInterface;

SalesOrdersDataExporter/Tests/Integration/CreateOrderTest.php renamed to SalesOrdersDataExporter/Test/Integration/CreateOrderTest.php

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\SalesOrdersDataExporter\Tests\Integration;
8+
namespace Magento\SalesOrdersDataExporter\Test\Integration;
99

1010
use Magento\Sales\Api\Data\OrderInterface;
1111
use Magento\Sales\Api\Data\OrderInterfaceFactory;
@@ -20,7 +20,7 @@
2020
use Magento\Sales\Model\Order\Payment\Transaction\Repository as TransactionRepository;
2121

2222
/**
23-
* Tests for orders data exporter functionality
23+
* Test for orders data exporter functionality
2424
*
2525
* @magentoDbIsolation enabled
2626
*/
@@ -280,8 +280,7 @@ public function orderFullWorkflowDataProvider(): array
280280
'order_data',
281281
'items',
282282
'shipments',
283-
'invoice',
284-
'creditmemo'
283+
'invoice'
285284
]
286285
]
287286
];
@@ -373,6 +372,9 @@ public function orderWithAdditionalInformationDataProvider(): array
373372
/**
374373
* @param array $expectedOrderData
375374
* @param array $feedData
375+
*
376+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
377+
* @SuppressWarnings(PHPMD.NPathComplexity)
376378
*/
377379
private function checkFields(array $expectedOrderData, array $feedData): void
378380
{
@@ -401,9 +403,11 @@ private function checkFields(array $expectedOrderData, array $feedData): void
401403
}
402404
if ($expectedField === 'invoices') {
403405
$uncheckedIds = \array_flip(array_keys($expectedData));
404-
foreach ($feedData[$expectedField] as $itemData) {
405-
$this->checkFields($expectedData[$itemData['entityId']], $itemData);
406-
unset($uncheckedIds[$itemData['entityId']]);
406+
foreach ($feedData['invoices'] as $itemData) {
407+
$invoiceId = $itemData['entityId'];
408+
$expectedInvoiceData = $expectedData[$invoiceId];
409+
$this->checkFields($expectedInvoiceData, $itemData);
410+
unset($uncheckedIds[$invoiceId]);
407411
}
408412
self::assertEmpty($uncheckedIds, "Some invoice items are missed in feed");
409413
continue;
@@ -442,7 +446,7 @@ private function checkFields(array $expectedOrderData, array $feedData): void
442446
)
443447
);
444448
} else {
445-
$this->checkFields($expectedOrderData[$expectedField], $feedData[$expectedField]);
449+
$this->checkFields($expectedData, $feedData[$expectedField]);
446450
}
447451
}
448452
}
@@ -661,16 +665,28 @@ private function getExpectedOrderItemsData(OrderInterface $order): array
661665
private function getExpectedInvoicesData(OrderInterface $order): array
662666
{
663667
$invoices = [];
664-
/** @var Invoice $invoiceItem */
665-
foreach ($order->getInvoiceCollection() as $invoiceItem) {
666-
$invoiceId = $invoiceItem->getId();
668+
669+
/** @var Invoice $invoice */
670+
foreach ($order->getInvoiceCollection() as $invoice) {
671+
$invoiceId = $invoice->getId();
672+
$invoiceItems = [];
673+
/** @var Invoice\Item $invoiceItem */
674+
foreach ($invoice->getItems() as $invoiceItem) {
675+
$orderItemId = $invoiceItem->getOrderItemId();
676+
$itemUuid = $this->uuidResource->getAssignedIds([$orderItemId], 'order_item')[$orderItemId];
677+
$invoiceItems[$orderItemId] = [
678+
'orderItemId' => ['id' => $itemUuid],
679+
'qtyInvoiced' => $invoiceItem->getQty()
680+
];
681+
}
667682
$invoices[$invoiceId] = [
668683
'entityId' => $invoiceId,
669684
'isUsedForRefund' => false,
670-
'grandTotal' => $invoiceItem->getBaseGrandTotal()
685+
'grandTotal' => $invoice->getBaseGrandTotal(),
686+
'createdAt' => $this->convertDate($invoice->getCreatedAt()),
687+
'invoiceItems' => $invoiceItems
671688
];
672689
}
673-
674690
return $invoices;
675691
}
676692

@@ -737,7 +753,7 @@ private function getExpectedShipmentData(OrderInterface $order): array
737753

738754
$shipmentId = $orderShipment->getId();
739755
$shipmentUuid = $this->uuidResource->getAssignedIds([$shipmentId], 'order_shipment')[$shipmentId];
740-
$expectedOrderData['shipments'][$shipmentUuid] = [
756+
$shipments[$shipmentUuid] = [
741757
'shipmentId' => ['id' => $shipmentUuid],
742758
'createdAt' => $this->convertDate($orderShipment->getCreatedAt()),
743759
'updatedAt' => $this->convertDate($orderShipment->getUpdatedAt())

SalesOrdersDataExporter/etc/di.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@
6565
<argument name="queryName" xsi:type="string">salesInvoicesV2</argument>
6666
</arguments>
6767
</virtualType>
68+
<virtualType name="Magento\SalesOrdersDataExporter\Model\Provider\InvoiceItem" type="Magento\DataExporter\Model\Provider\QueryDataProvider">
69+
<arguments>
70+
<argument name="queryName" xsi:type="string">salesInvoiceItemV2</argument>
71+
</arguments>
72+
</virtualType>
6873
<virtualType name="Magento\SalesOrdersDataExporter\Model\Provider\CreditMemos" type="Magento\DataExporter\Model\Provider\QueryDataProvider">
6974
<arguments>
7075
<argument name="queryName" xsi:type="string">salesCreditMemosV2</argument>

SalesOrdersDataExporter/etc/et_schema.xml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@
164164
<field name="multiLineStreet" type="String" repeated="true"
165165
provider="Magento\SalesOrdersDataExporter\Model\Provider\MultiLineStreet">
166166
<using field="commerceOrderId" />
167+
<using field="addressType" />
167168
</field>
168169
<field name="city" type="String" />
169170
<field name="country" type="String" />
@@ -178,6 +179,21 @@
178179
<field name="entityId" type="ID" />
179180
<field name="isUsedForRefund" type="Boolean" />
180181
<field name="grandTotal" type="Float" />
182+
<field name="createdAt" type="String"
183+
provider="Magento\DataExporter\Model\Provider\DateConverter">
184+
<using field="entityId" />
185+
</field>
186+
<field name="invoiceItems" type="InvoiceItem" repeated="true"
187+
provider="Magento\SalesOrdersDataExporter\Model\Provider\InvoiceItem">
188+
<using field="invoiceId" />
189+
</field>
190+
</record>
191+
<record name="InvoiceItem">
192+
<field name="orderItemId" type="UUID"
193+
provider="Magento\SalesOrdersDataExporter\Model\Provider\Uuid\OrderItem">
194+
<using field="order_item_id" />
195+
</field>
196+
<field name="qtyInvoiced" type="Float" />
181197
</record>
182198
<record name="CreditMemoV2">
183199
<field name="creditMemoId" type="UUID"
@@ -269,7 +285,7 @@
269285
</record>
270286
<record name="ShipmentItem">
271287
<field name="orderItemId" type="UUID"
272-
provider="Magento\SalesOrdersDataExporter\Model\Provider\Uuid\CreditMemoItem"> <!--UUID
288+
provider="Magento\SalesOrdersDataExporter\Model\Provider\Uuid\ShipmentItem"> <!--UUID
273289
of original order item -->
274290
<using field="order_item_id" />
275291
</field>

SalesOrdersDataExporter/etc/query.xml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,33 @@
122122
<query name="salesInvoicesV2">
123123
<source name="sales_invoice">
124124
<attribute name="entity_id" alias="entityId" />
125+
<attribute name="entity_id" alias="invoiceId" />
125126
<attribute name="order_id" alias="commerceOrderId" />
126127
<attribute name="is_used_for_refund" alias="isUsedForRefund" />
127128
<attribute name="base_grand_total" alias="grandTotal" />
129+
<attribute name="created_at" alias="createdAt" />
128130
<filter glue="and">
129131
<condition attribute="order_id" operator="in" type="placeholder">commerceOrderId</condition>
130132
</filter>
131133
</source>
132134
</query>
135+
<query name="salesInvoiceItemV2">
136+
<source name="sales_invoice_item">
137+
<attribute name="qty" alias="qtyInvoiced" />
138+
<attribute name="parent_id" alias="invoiceId" />
139+
<attribute name="order_item_id" />
140+
<filter glue="and">
141+
<condition attribute="parent_id" operator="in" type="placeholder">invoiceId</condition>
142+
</filter>
143+
<link-source name="data_exporter_uuid" link-type="left">
144+
<attribute name="uuid" />
145+
<using glue="and">
146+
<condition attribute="entity_id" operator="eq" type="identifier">order_item_id</condition>
147+
<condition attribute="type" operator="eq" type="value">order_item</condition>
148+
</using>
149+
</link-source>
150+
</source>
151+
</query>
133152
<query name="salesShipmentV2">
134153
<source name="sales_shipment">
135154
<attribute name="created_at" alias="createdAt" />
@@ -200,9 +219,10 @@
200219
<attribute name="country_id" alias="country" />
201220
<attribute name="firstname" />
202221
<attribute name="lastname" />
222+
<attribute name="address_type" alias="addressType" />
203223
<filter glue="and">
204-
<condition attribute="address_type" operator="eq" type="placeholder">addressType</condition>
205224
<condition attribute="parent_id" operator="in" type="placeholder">commerceOrderId</condition>
225+
<condition attribute="address_type" operator="eq" type="placeholder">addressType</condition>
206226
</filter>
207227
</source>
208228
</query>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
);
4545

4646
$qtyOrdered = 2;
47+
$productOptions = array('color' => 'red', 'size' => 'medium');
4748
/** @var \Magento\Sales\Model\Order\Item $orderItem */
4849
$orderConfigurableItem = $objectManager->create(\Magento\Sales\Model\Order\Item::class);
4950
$orderConfigurableItem->setProductId($configurableProduct->getId())->setQtyOrdered($qtyOrdered);
@@ -52,6 +53,7 @@
5253
$orderConfigurableItem->setRowTotal($configurableProduct->getPrice());
5354
$orderConfigurableItem->setParentItemId(null);
5455
$orderConfigurableItem->setProductType('configurable');
56+
$orderConfigurableItem->setProductOptions($productOptions);
5557

5658
if ($configurableProduct->getExtensionAttributes()
5759
&& (array)$configurableProduct->getExtensionAttributes()->getConfigurableProductLinks()
@@ -64,6 +66,7 @@
6466
$orderItem->setPrice($simpleProduct->getPrice());
6567
$orderItem->setRowTotal($simpleProduct->getPrice());
6668
$orderItem->setProductType('simple');
69+
$orderItem->setProductOptions($productOptions);
6770
// duplicate behavior with simple product associated with configurable one that happens during order process.
6871
$orderItem->setProductId($simpleProduct->getId())->setQtyOrdered($qtyOrdered);
6972
$orderItem->setParentItemId($orderConfigurableItem->getId());

dev/tests/integration/testsuite/Magento/SalesDataExporter/_files/order_full_work_flow.php renamed to dev/tests/integration/testsuite/Magento/SalesOrdersDataExporter/_files/order_full_work_flow.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
/** @var \Magento\Sales\Api\Data\OrderItemInterface $orderItem */
5050
$orderItem = array_values($orderItems)[0];
5151

52-
/** @var \Magento\Sales\Api\Data\ShipmentItemCreationInterface $shipmentItem */
52+
/** @var \Magento\Sales\Api\Data\InvoiceItemCreationInterface $invoiceItem */
5353
$invoiceItem = $objectManager->create(\Magento\Sales\Api\Data\InvoiceItemCreationInterface::class);
5454
$invoiceItem->setOrderItemId($orderItem->getItemId());
5555
$invoiceItem->setQty($orderItem->getQtyOrdered());

0 commit comments

Comments
 (0)