77namespace Magento \Sales \Model \Order ;
88
99use Magento \Framework \Api \AttributeValueFactory ;
10+ use Magento \Framework \App \ObjectManager ;
1011use Magento \Framework \Pricing \PriceCurrencyInterface ;
1112use Magento \Sales \Api \Data \CreditmemoInterface ;
1213use Magento \Sales \Model \AbstractModel ;
1314use 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}
0 commit comments