Skip to content

Commit aefd089

Browse files
⏫ Forwardport of #11670 to 2.3-develop branch
Applied pull request patch https://github.com/magento/magento2/pull/11670.patch (created by @ajpevers) based on commit(s): 1. cfd92c4 2. 94ae23d 3. 9145209 4. ce20c0a 5. 23f315c 6. 72533ad 7. 1d341ef 8. f2ba426 Fixed GitHub Issues in 2.3-develop branch: - #11669: API salesRefundInvoiceV1 does no save invoice ID on credit memo (reported by @ajpevers)
1 parent 8e77e2f commit aefd089

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

app/code/Magento/Sales/Model/Order/Creditmemo.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
namespace Magento\Sales\Model\Order;
88

99
use Magento\Framework\Api\AttributeValueFactory;
10+
use Magento\Framework\App\ObjectManager;
1011
use Magento\Framework\Pricing\PriceCurrencyInterface;
1112
use Magento\Sales\Api\Data\CreditmemoInterface;
1213
use Magento\Sales\Model\AbstractModel;
1314
use Magento\Sales\Model\EntityInterface;
15+
use Magento\Sales\Model\Order\InvoiceFactory;
1416

1517
/**
1618
* Order creditmemo model
@@ -112,6 +114,11 @@ class Creditmemo extends AbstractModel implements EntityInterface, CreditmemoInt
112114
*/
113115
protected $priceCurrency;
114116

117+
/**
118+
* @var InvoiceFactory
119+
*/
120+
private $invoiceFactory;
121+
115122
/**
116123
* @param \Magento\Framework\Model\Context $context
117124
* @param \Magento\Framework\Registry $registry
@@ -128,6 +135,7 @@ class Creditmemo extends AbstractModel implements EntityInterface, CreditmemoInt
128135
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
129136
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
130137
* @param array $data
138+
* @param InvoiceFactory $invoiceFactory
131139
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
132140
*/
133141
public function __construct(
@@ -145,7 +153,8 @@ public function __construct(
145153
PriceCurrencyInterface $priceCurrency,
146154
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
147155
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
148-
array $data = []
156+
array $data = [],
157+
InvoiceFactory $invoiceFactory = null
149158
) {
150159
$this->_creditmemoConfig = $creditmemoConfig;
151160
$this->_orderFactory = $orderFactory;
@@ -155,6 +164,7 @@ public function __construct(
155164
$this->_commentFactory = $commentFactory;
156165
$this->_commentCollectionFactory = $commentCollectionFactory;
157166
$this->priceCurrency = $priceCurrency;
167+
$this->invoiceFactory = $invoiceFactory ?: ObjectManager::getInstance()->get(InvoiceFactory::class);
158168
parent::__construct(
159169
$context,
160170
$registry,
@@ -377,6 +387,9 @@ public function canRefund()
377387
*/
378388
public function getInvoice()
379389
{
390+
if (!$this->getData('invoice') instanceof \Magento\Sales\Api\Data\InvoiceInterface && $this->getInvoiceId()) {
391+
$this->setInvoice($this->invoiceFactory->create()->load($this->getInvoiceId()));
392+
}
380393
return $this->getData('invoice');
381394
}
382395

@@ -1505,6 +1518,5 @@ public function setExtensionAttributes(\Magento\Sales\Api\Data\CreditmemoExtensi
15051518
{
15061519
return $this->_setExtensionAttributes($extensionAttributes);
15071520
}
1508-
15091521
//@codeCoverageIgnoreEnd
15101522
}

app/code/Magento/Sales/Model/ResourceModel/Order/Creditmemo.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
5050
$object->setBillingAddressId($object->getOrder()->getBillingAddress()->getId());
5151
}
5252

53+
if (!$object->getInvoiceId() && $object->getInvoice()) {
54+
$object->setInvoiceId($object->getInvoice()->getId());
55+
}
56+
5357
return parent::_beforeSave($object);
5458
}
5559
}

dev/tests/integration/testsuite/Magento/Paypal/Model/IpnTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function testProcessIpnRequestPartialRefund()
123123
/**
124124
* Refund rest of order amount by Paypal Express IPN message service.
125125
*
126-
* @magentoDataFixture Magento/Paypal/_files/order_express_with_invoice_and_creditmemo.php
126+
* @magentoDataFixture Magento/Paypal/_files/order_express_with_invoice_and_shipping.php
127127
* @magentoConfigFixture current_store payment/paypal_express/active 1
128128
* @magentoConfigFixture current_store paypal/general/merchant_country US
129129
*/
@@ -147,7 +147,7 @@ public function testProcessIpnRequestRestRefund()
147147
$creditmemoItems = $order->getCreditmemosCollection()->getItems();
148148

149149
$this->assertEquals(Order::STATE_CLOSED, $order->getState()) ;
150-
$this->assertEquals(2, count($creditmemoItems));
150+
$this->assertEquals(1, count($creditmemoItems));
151151
$this->assertEquals(10, $order->getSubtotalRefunded());
152152
$this->assertEquals(10, $order->getBaseSubtotalRefunded());
153153
$this->assertEquals(20, $order->getShippingRefunded());

0 commit comments

Comments
 (0)