Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions app/code/Magento/Sales/Model/Order/Creditmemo.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
namespace Magento\Sales\Model\Order;

use Magento\Framework\Api\AttributeValueFactory;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Sales\Api\Data\CreditmemoInterface;
use Magento\Sales\Model\AbstractModel;
use Magento\Sales\Model\EntityInterface;
use Magento\Sales\Model\Order\InvoiceFactory;

/**
* Order creditmemo model
Expand Down Expand Up @@ -114,6 +116,11 @@ class Creditmemo extends AbstractModel implements EntityInterface, CreditmemoInt
*/
protected $priceCurrency;

/**
* @var InvoiceFactory
*/
private $invoiceFactory;

/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
Expand All @@ -130,6 +137,7 @@ class Creditmemo extends AbstractModel implements EntityInterface, CreditmemoInt
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
* @param array $data
* @param InvoiceFactory $invoiceFactory
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand All @@ -147,7 +155,8 @@ public function __construct(
PriceCurrencyInterface $priceCurrency,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
array $data = [],
InvoiceFactory $invoiceFactory = null
) {
$this->_creditmemoConfig = $creditmemoConfig;
$this->_orderFactory = $orderFactory;
Expand All @@ -157,6 +166,7 @@ public function __construct(
$this->_commentFactory = $commentFactory;
$this->_commentCollectionFactory = $commentCollectionFactory;
$this->priceCurrency = $priceCurrency;
$this->invoiceFactory = $invoiceFactory ?: ObjectManager::getInstance()->get(InvoiceFactory::class);
parent::__construct(
$context,
$registry,
Expand Down Expand Up @@ -379,6 +389,9 @@ public function canRefund()
*/
public function getInvoice()
{
if (!$this->getData('invoice') instanceof \Magento\Sales\Api\Data\InvoiceInterface && $this->getInvoiceId()) {
$this->setInvoice($this->invoiceFactory->create()->load($this->getInvoiceId()));
}
return $this->getData('invoice');
}

Expand Down Expand Up @@ -1524,6 +1537,5 @@ public function setExtensionAttributes(\Magento\Sales\Api\Data\CreditmemoExtensi
{
return $this->_setExtensionAttributes($extensionAttributes);
}

//@codeCoverageIgnoreEnd
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
$object->setBillingAddressId($object->getOrder()->getBillingAddress()->getId());
}

if (!$object->getInvoiceId() && $object->getInvoice()) {
$object->setInvoiceId($object->getInvoice()->getId());
}

return parent::_beforeSave($object);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function testProcessIpnRequestPartialRefund()
/**
* Refund rest of order amount by Paypal Express IPN message service.
*
* @magentoDataFixture Magento/Paypal/_files/order_express_with_invoice_and_creditmemo.php
* @magentoDataFixture Magento/Paypal/_files/order_express_with_invoice_and_shipping.php
* @magentoConfigFixture current_store payment/paypal_express/active 1
* @magentoConfigFixture current_store paypal/general/merchant_country US
*/
Expand All @@ -147,7 +147,7 @@ public function testProcessIpnRequestRestRefund()
$creditmemoItems = $order->getCreditmemosCollection()->getItems();

$this->assertEquals(Order::STATE_CLOSED, $order->getState()) ;
$this->assertEquals(2, count($creditmemoItems));
$this->assertEquals(1, count($creditmemoItems));
$this->assertEquals(10, $order->getSubtotalRefunded());
$this->assertEquals(10, $order->getBaseSubtotalRefunded());
$this->assertEquals(20, $order->getShippingRefunded());
Expand Down