Skip to content

Commit b127bcb

Browse files
committed
Merge remote-tracking branch 'origin/AC-15401' into spartans_pr_10112025
2 parents b0fb9f3 + ee9ca62 commit b127bcb

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

app/code/Magento/Sales/Model/Service/InvoiceService.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use Magento\Sales\Model\Order\Invoice;
1818

1919
/**
20-
* Class InvoiceService
20+
* Service for creating and managing invoices
2121
*
2222
* @api
2323
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -153,6 +153,7 @@ public function prepareInvoice(
153153
$totalQty = 0;
154154
$invoice = $this->orderConverter->toInvoice($order);
155155
$preparedItemsQty = $this->prepareItemsQty($order, $orderItemsQtyToInvoice);
156+
$originalEntityType = $order->getEntityType() ?? null;
156157

157158
foreach ($order->getAllItems() as $orderItem) {
158159
if (!$this->canInvoiceItem($orderItem, $preparedItemsQty)) {
@@ -177,6 +178,9 @@ public function prepareInvoice(
177178

178179
$invoice->setTotalQty($totalQty);
179180
$invoice->collectTotals();
181+
if ($originalEntityType) {
182+
$order->setHistoryEntityName($originalEntityType);
183+
}
180184
$order->getInvoiceCollection()->addItem($invoice);
181185

182186
return $invoice;
@@ -275,6 +279,7 @@ private function canInvoiceItem(OrderItemInterface $item, array $qtys): bool
275279
} else {
276280
return $item->getQtyToInvoice() > 0;
277281
}
282+
return false;
278283
}
279284

280285
/**

app/code/Magento/Sales/Test/Unit/Model/Service/InvoiceServiceTest.php

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2015 Adobe
3+
* Copyright 2014 Adobe
44
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
@@ -21,6 +21,10 @@
2121
use PHPUnit\Framework\MockObject\MockObject;
2222
use PHPUnit\Framework\TestCase;
2323

24+
/**
25+
*
26+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
27+
*/
2428
class InvoiceServiceTest extends TestCase
2529
{
2630
/**
@@ -212,4 +216,59 @@ public function testSetVoid()
212216

213217
$this->assertTrue($this->invoiceService->setVoid($id));
214218
}
219+
220+
public function testPrepareInvoiceSetsHistoryEntityNameWhenOriginalEntityTypePresent(): void
221+
{
222+
$orderRepository = $this->createMock(\Magento\Sales\Api\OrderRepositoryInterface::class);
223+
$orderConverter = $this->getMockBuilder(\Magento\Sales\Model\Convert\Order::class)
224+
->disableOriginalConstructor()->getMock();
225+
$serializer = $this->createMock(\Magento\Framework\Serialize\Serializer\Json::class);
226+
227+
$service = new InvoiceService(
228+
$this->repositoryMock,
229+
$this->commentRepositoryMock,
230+
$this->searchCriteriaBuilderMock,
231+
$this->filterBuilderMock,
232+
$this->invoiceNotifierMock,
233+
$orderRepository,
234+
$orderConverter,
235+
$serializer
236+
);
237+
238+
$order = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
239+
->disableOriginalConstructor()
240+
->onlyMethods(['getAllItems', 'getEntityType', 'setHistoryEntityName', 'getInvoiceCollection'])
241+
->getMock();
242+
243+
$invoice = $this->getMockBuilder(\Magento\Sales\Model\Order\Invoice::class)
244+
->disableOriginalConstructor()
245+
->onlyMethods(['setTotalQty', 'collectTotals'])
246+
->getMock();
247+
248+
$invoiceCollection = $this->getMockBuilder(\Magento\Framework\Data\Collection::class)
249+
->disableOriginalConstructor()
250+
->onlyMethods(['addItem'])
251+
->getMock();
252+
253+
$order->method('getAllItems')->willReturn([]);
254+
$order->method('getEntityType')->willReturn('order');
255+
$order->method('getInvoiceCollection')->willReturn($invoiceCollection);
256+
257+
$order->expects($this->once())
258+
->method('setHistoryEntityName')
259+
->with('order');
260+
261+
$orderConverter->expects($this->once())
262+
->method('toInvoice')
263+
->with($order)
264+
->willReturn($invoice);
265+
266+
$invoice->expects($this->once())->method('setTotalQty')->with(0);
267+
$invoice->expects($this->once())->method('collectTotals');
268+
269+
$invoiceCollection->expects($this->once())->method('addItem')->with($invoice);
270+
271+
$result = $service->prepareInvoice($order, []);
272+
$this->assertInstanceOf(\Magento\Sales\Api\Data\InvoiceInterface::class, $result);
273+
}
215274
}

0 commit comments

Comments
 (0)