7
7
namespace Magento \Sales \Model \Order ;
8
8
9
9
use Magento \Framework \Api \AttributeValueFactory ;
10
+ use Magento \Framework \App \Config \ScopeConfigInterface ;
10
11
use Magento \Framework \App \ObjectManager ;
11
12
use Magento \Framework \Pricing \PriceCurrencyInterface ;
12
13
use Magento \Sales \Api \Data \CreditmemoInterface ;
14
+ use Magento \Sales \Api \OrderRepositoryInterface ;
13
15
use Magento \Sales \Model \AbstractModel ;
14
16
use Magento \Sales \Model \EntityInterface ;
15
- use Magento \Sales \Model \Order \InvoiceFactory ;
16
- use Magento \Framework \App \Config \ScopeConfigInterface ;
17
17
18
18
/**
19
19
* Order creditmemo model
@@ -126,6 +126,11 @@ class Creditmemo extends AbstractModel implements EntityInterface, CreditmemoInt
126
126
*/
127
127
private $ scopeConfig ;
128
128
129
+ /**
130
+ * @var OrderRepositoryInterface
131
+ */
132
+ private $ orderRepository ;
133
+
129
134
/**
130
135
* @param \Magento\Framework\Model\Context $context
131
136
* @param \Magento\Framework\Registry $registry
@@ -144,6 +149,7 @@ class Creditmemo extends AbstractModel implements EntityInterface, CreditmemoInt
144
149
* @param array $data
145
150
* @param InvoiceFactory $invoiceFactory
146
151
* @param ScopeConfigInterface $scopeConfig
152
+ * @param OrderRepositoryInterface $orderRepository
147
153
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
148
154
*/
149
155
public function __construct (
@@ -163,7 +169,8 @@ public function __construct(
163
169
\Magento \Framework \Data \Collection \AbstractDb $ resourceCollection = null ,
164
170
array $ data = [],
165
171
InvoiceFactory $ invoiceFactory = null ,
166
- ScopeConfigInterface $ scopeConfig = null
172
+ ScopeConfigInterface $ scopeConfig = null ,
173
+ OrderRepositoryInterface $ orderRepository = null
167
174
) {
168
175
$ this ->_creditmemoConfig = $ creditmemoConfig ;
169
176
$ this ->_orderFactory = $ orderFactory ;
@@ -175,6 +182,7 @@ public function __construct(
175
182
$ this ->priceCurrency = $ priceCurrency ;
176
183
$ this ->invoiceFactory = $ invoiceFactory ?: ObjectManager::getInstance ()->get (InvoiceFactory::class);
177
184
$ this ->scopeConfig = $ scopeConfig ?: ObjectManager::getInstance ()->get (ScopeConfigInterface::class);
185
+ $ this ->orderRepository = $ orderRepository ?? ObjectManager::getInstance ()->get (OrderRepositoryInterface::class);
178
186
parent ::__construct (
179
187
$ context ,
180
188
$ registry ,
@@ -237,8 +245,11 @@ public function setOrder(\Magento\Sales\Model\Order $order)
237
245
public function getOrder ()
238
246
{
239
247
if (!$ this ->_order instanceof \Magento \Sales \Model \Order) {
240
- $ this ->_order = $ this ->_orderFactory ->create ()->load ($ this ->getOrderId ());
248
+ $ this ->_order = $ this ->getOrderId () ?
249
+ $ this ->orderRepository ->get ($ this ->getOrderId ()) :
250
+ $ this ->_orderFactory ->create ();
241
251
}
252
+
242
253
return $ this ->_order ->setHistoryEntityName ($ this ->entityType );
243
254
}
244
255
@@ -449,6 +460,7 @@ public function canVoid()
449
460
* Retrieve Creditmemo states array
450
461
*
451
462
* @return array
463
+ * phpcs:disable Magento2.Functions.StaticFunction
452
464
*/
453
465
public static function getStates ()
454
466
{
@@ -461,11 +473,12 @@ public static function getStates()
461
473
}
462
474
return static ::$ _states ;
463
475
}
476
+ // phpcs:enable
464
477
465
478
/**
466
479
* Retrieve Creditmemo state name by state identifier
467
480
*
468
- * @param int $stateId
481
+ * @param int $stateId
469
482
* @return \Magento\Framework\Phrase
470
483
*/
471
484
public function getStateName ($ stateId = null )
0 commit comments