Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

save invoice ID on credit memo when using API method salesRefundInvoiceV1 #11670

Merged
merged 8 commits into from
Nov 25, 2017
23 changes: 22 additions & 1 deletion app/code/Magento/Sales/Model/Order/Creditmemo.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
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\Api\InvoiceRepositoryInterface;
use Magento\Sales\Model\AbstractModel;
use Magento\Sales\Model\EntityInterface;

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

/**
* @var InvoiceRepository
*/
private $invoiceRepository;

/**
* @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 InvoiceRepository $invoiceRepository
* @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 = [],
InvoiceRepository $invoiceRepository = 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->invoiceRepository = $invoiceRepository;
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\Model\Order\Invoice && $this->getInvoiceId()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason not to use \Magento\Sales\Api\Data\InvoiceInterface here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. No there is no reason.

$this->setInvoice($this->getInvoiceRepository()->get($this->getInvoiceId()));
}
return $this->getData('invoice');
}

Expand All @@ -391,6 +404,7 @@ public function getInvoice()
public function setInvoice(Invoice $invoice)
{
$this->setData('invoice', $invoice);
$this->setInvoiceId($invoice->getId());
return $this;
}

Expand Down Expand Up @@ -1525,5 +1539,12 @@ public function setExtensionAttributes(\Magento\Sales\Api\Data\CreditmemoExtensi
return $this->_setExtensionAttributes($extensionAttributes);
}

/**
* @return InvoiceRepositoryInterface|mixed
* @deprecated
*/
private function getInvoiceRepository() {
return $this->invoiceRepository ?: ObjectManager::getInstance()->get(InvoiceRepositoryInterface::class);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid such methods creation and move this logic to constructor.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay

}
//@codeCoverageIgnoreEnd
}