99namespace Magento \Sales \Model \Order ;
1010
1111use Magento \Framework \Api \AttributeValueFactory ;
12+ use Magento \Framework \App \ObjectManager ;
1213use Magento \Framework \Pricing \PriceCurrencyInterface ;
1314use Magento \Sales \Api \Data \CreditmemoInterface ;
15+ use Magento \Sales \Api \InvoiceRepositoryInterface ;
1416use Magento \Sales \Model \AbstractModel ;
1517use Magento \Sales \Model \EntityInterface ;
1618
@@ -114,6 +116,11 @@ class Creditmemo extends AbstractModel implements EntityInterface, CreditmemoInt
114116 */
115117 protected $ priceCurrency ;
116118
119+ /**
120+ * @var InvoiceRepository
121+ */
122+ private $ invoiceRepository ;
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 InvoiceRepository $invoiceRepository
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+ InvoiceRepository $ invoiceRepository = 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 ->invoiceRepository = $ invoiceRepository ;
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 \Model \Order \Invoice && $ this ->getInvoiceId ()) {
393+ $ this ->setInvoice ($ this ->getInvoiceRepository ()->get ($ this ->getInvoiceId ()));
394+ }
382395 return $ this ->getData ('invoice ' );
383396 }
384397
@@ -391,6 +404,7 @@ public function getInvoice()
391404 public function setInvoice (Invoice $ invoice )
392405 {
393406 $ this ->setData ('invoice ' , $ invoice );
407+ $ this ->setInvoiceId ($ invoice ->getId ());
394408 return $ this ;
395409 }
396410
@@ -1525,5 +1539,12 @@ public function setExtensionAttributes(\Magento\Sales\Api\Data\CreditmemoExtensi
15251539 return $ this ->_setExtensionAttributes ($ extensionAttributes );
15261540 }
15271541
1542+ /**
1543+ * @return InvoiceRepositoryInterface|mixed
1544+ * @deprecated
1545+ */
1546+ private function getInvoiceRepository () {
1547+ return $ this ->invoiceRepository ?: ObjectManager::getInstance ()->get (InvoiceRepositoryInterface::class);
1548+ }
15281549 //@codeCoverageIgnoreEnd
15291550}
0 commit comments