Skip to content

Commit 44c9096

Browse files
MAGETWO-83552: save invoice ID on credit memo when using API method salesRefundInvoiceV1 #11670
2 parents 6260a36 + 53931cb commit 44c9096

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
@@ -9,10 +9,12 @@
99
namespace Magento\Sales\Model\Order;
1010

1111
use Magento\Framework\Api\AttributeValueFactory;
12+
use Magento\Framework\App\ObjectManager;
1213
use Magento\Framework\Pricing\PriceCurrencyInterface;
1314
use Magento\Sales\Api\Data\CreditmemoInterface;
1415
use Magento\Sales\Model\AbstractModel;
1516
use Magento\Sales\Model\EntityInterface;
17+
use Magento\Sales\Model\Order\InvoiceFactory;
1618

1719
/**
1820
* Order creditmemo model
@@ -114,6 +116,11 @@ class Creditmemo extends AbstractModel implements EntityInterface, CreditmemoInt
114116
*/
115117
protected $priceCurrency;
116118

119+
/**
120+
* @var InvoiceFactory
121+
*/
122+
private $invoiceFactory;
123+
117124
/**
118125
* @param \Magento\Framework\Model\Context $context
119126
* @param \Magento\Framework\Registry $registry
@@ -130,6 +137,7 @@ class Creditmemo extends AbstractModel implements EntityInterface, CreditmemoInt
130137
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
131138
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
132139
* @param array $data
140+
* @param InvoiceFactory $invoiceFactory
133141
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
134142
*/
135143
public function __construct(
@@ -147,7 +155,8 @@ public function __construct(
147155
PriceCurrencyInterface $priceCurrency,
148156
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
149157
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
150-
array $data = []
158+
array $data = [],
159+
InvoiceFactory $invoiceFactory = null
151160
) {
152161
$this->_creditmemoConfig = $creditmemoConfig;
153162
$this->_orderFactory = $orderFactory;
@@ -157,6 +166,7 @@ public function __construct(
157166
$this->_commentFactory = $commentFactory;
158167
$this->_commentCollectionFactory = $commentCollectionFactory;
159168
$this->priceCurrency = $priceCurrency;
169+
$this->invoiceFactory = $invoiceFactory ?: ObjectManager::getInstance()->get(InvoiceFactory::class);
160170
parent::__construct(
161171
$context,
162172
$registry,
@@ -379,6 +389,9 @@ public function canRefund()
379389
*/
380390
public function getInvoice()
381391
{
392+
if (!$this->getData('invoice') instanceof \Magento\Sales\Api\Data\InvoiceInterface && $this->getInvoiceId()) {
393+
$this->setInvoice($this->invoiceFactory->create()->load($this->getInvoiceId()));
394+
}
382395
return $this->getData('invoice');
383396
}
384397

@@ -1524,6 +1537,5 @@ public function setExtensionAttributes(\Magento\Sales\Api\Data\CreditmemoExtensi
15241537
{
15251538
return $this->_setExtensionAttributes($extensionAttributes);
15261539
}
1527-
15281540
//@codeCoverageIgnoreEnd
15291541
}

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)