5
5
*/
6
6
namespace Magento \Sales \Model ;
7
7
8
+ use Magento \Catalog \Api \ProductRepositoryInterface ;
9
+ use Magento \Catalog \Model \Product \Visibility ;
8
10
use Magento \Config \Model \Config \Source \Nooptreq ;
9
11
use Magento \Directory \Model \Currency ;
12
+ use Magento \Directory \Model \CurrencyFactory ;
10
13
use Magento \Directory \Model \RegionFactory ;
11
14
use Magento \Directory \Model \ResourceModel \Region as RegionResource ;
12
15
use Magento \Framework \Api \AttributeValueFactory ;
16
+ use Magento \Framework \Api \ExtensionAttributesFactory ;
13
17
use Magento \Framework \Api \SearchCriteriaBuilder ;
14
18
use Magento \Framework \App \Config \ScopeConfigInterface ;
15
19
use Magento \Framework \App \ObjectManager ;
20
+ use Magento \Framework \Data \Collection \AbstractDb ;
16
21
use Magento \Framework \Exception \LocalizedException ;
17
22
use Magento \Framework \Locale \ResolverInterface ;
23
+ use Magento \Framework \Model \Context ;
24
+ use Magento \Framework \Model \ResourceModel \AbstractResource ;
18
25
use Magento \Framework \Pricing \PriceCurrencyInterface ;
26
+ use Magento \Framework \Registry ;
27
+ use Magento \Framework \Stdlib \DateTime \TimezoneInterface ;
19
28
use Magento \Sales \Api \Data \OrderInterface ;
20
29
use Magento \Sales \Api \Data \OrderItemInterface ;
21
30
use Magento \Sales \Api \Data \OrderStatusHistoryInterface ;
31
+ use Magento \Sales \Api \InvoiceManagementInterface ;
22
32
use Magento \Sales \Api \OrderItemRepositoryInterface ;
33
+ use Magento \Sales \Model \Order \Config ;
34
+ use Magento \Sales \Model \Order \CreditmemoValidator ;
23
35
use Magento \Sales \Model \Order \Payment ;
24
36
use Magento \Sales \Model \Order \ProductOption ;
37
+ use Magento \Sales \Model \Order \Status \HistoryFactory ;
25
38
use Magento \Sales \Model \ResourceModel \Order \Address \Collection ;
26
39
use Magento \Sales \Model \ResourceModel \Order \Creditmemo \Collection as CreditmemoCollection ;
27
40
use Magento \Sales \Model \ResourceModel \Order \Invoice \Collection as InvoiceCollection ;
28
41
use Magento \Sales \Model \ResourceModel \Order \Item \Collection as ItemCollection ;
42
+ use Magento \Sales \Model \ResourceModel \Order \Item \CollectionFactory ;
29
43
use Magento \Sales \Model \ResourceModel \Order \Payment \Collection as PaymentCollection ;
30
44
use Magento \Sales \Model \ResourceModel \Order \Shipment \Collection as ShipmentCollection ;
31
45
use Magento \Sales \Model \ResourceModel \Order \Shipment \Track \Collection as TrackCollection ;
32
46
use Magento \Sales \Model \ResourceModel \Order \Status \History \Collection as HistoryCollection ;
33
47
use Magento \Store \Model \ScopeInterface ;
34
48
use Magento \Framework \App \Area ;
35
49
use Magento \Sales \Model \Order \StatusLabel ;
50
+ use Magento \Store \Model \StoreManagerInterface ;
36
51
37
52
/**
38
53
* Order model
@@ -333,20 +348,25 @@ class Order extends AbstractModel implements EntityInterface, OrderInterface
333
348
private $ statusLabel ;
334
349
335
350
/**
336
- * @param \Magento\Framework\Model\Context $context
337
- * @param \Magento\Framework\Registry $registry
338
- * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
351
+ * @var ?CreditmemoValidator
352
+ */
353
+ private $ creditmemoValidator ;
354
+
355
+ /**
356
+ * @param Context $context
357
+ * @param Registry $registry
358
+ * @param ExtensionAttributesFactory $extensionFactory
339
359
* @param AttributeValueFactory $customAttributeFactory
340
- * @param \Magento\Framework\Stdlib\DateTime\ TimezoneInterface $timezone
341
- * @param \Magento\Store\Model\ StoreManagerInterface $storeManager
342
- * @param Order\ Config $orderConfig
343
- * @param \Magento\Catalog\Api\ ProductRepositoryInterface $productRepository
344
- * @param \Magento\Sales\Model\ResourceModel\Order\Item\ CollectionFactory $orderItemCollectionFactory
345
- * @param \Magento\Catalog\Model\Product\ Visibility $productVisibility
346
- * @param \Magento\Sales\Api\ InvoiceManagementInterface $invoiceManagement
347
- * @param \Magento\Directory\Model\ CurrencyFactory $currencyFactory
360
+ * @param TimezoneInterface $timezone
361
+ * @param StoreManagerInterface $storeManager
362
+ * @param Config $orderConfig
363
+ * @param ProductRepositoryInterface $productRepository
364
+ * @param CollectionFactory $orderItemCollectionFactory
365
+ * @param Visibility $productVisibility
366
+ * @param InvoiceManagementInterface $invoiceManagement
367
+ * @param CurrencyFactory $currencyFactory
348
368
* @param \Magento\Eav\Model\Config $eavConfig
349
- * @param Order\Status\ HistoryFactory $orderHistoryFactory
369
+ * @param HistoryFactory $orderHistoryFactory
350
370
* @param \Magento\Sales\Model\ResourceModel\Order\Address\CollectionFactory $addressCollectionFactory
351
371
* @param \Magento\Sales\Model\ResourceModel\Order\Payment\CollectionFactory $paymentCollectionFactory
352
372
* @param \Magento\Sales\Model\ResourceModel\Order\Status\History\CollectionFactory $historyCollectionFactory
@@ -357,8 +377,8 @@ class Order extends AbstractModel implements EntityInterface, OrderInterface
357
377
* @param ResourceModel\Order\CollectionFactory $salesOrderCollectionFactory
358
378
* @param PriceCurrencyInterface $priceCurrency
359
379
* @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productListFactory
360
- * @param \Magento\Framework\Model\ResourceModel\ AbstractResource|null $resource
361
- * @param \Magento\Framework\Data\Collection\ AbstractDb|null $resourceCollection
380
+ * @param AbstractResource|null $resource
381
+ * @param AbstractDb|null $resourceCollection
362
382
* @param array $data
363
383
* @param ResolverInterface|null $localeResolver
364
384
* @param ProductOption|null $productOption
@@ -368,8 +388,10 @@ class Order extends AbstractModel implements EntityInterface, OrderInterface
368
388
* @param RegionFactory|null $regionFactory
369
389
* @param RegionResource|null $regionResource
370
390
* @param StatusLabel|null $statusLabel
391
+ * @param CreditmemoValidator|null $creditmemoValidator
371
392
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
372
393
* @SuppressWarnings(PHPMD.NPathComplexity)
394
+ * @SuppressWarnings(PHPMD.CyclomaticComplexity)
373
395
*/
374
396
public function __construct (
375
397
\Magento \Framework \Model \Context $ context ,
@@ -406,7 +428,8 @@ public function __construct(
406
428
ScopeConfigInterface $ scopeConfig = null ,
407
429
RegionFactory $ regionFactory = null ,
408
430
RegionResource $ regionResource = null ,
409
- StatusLabel $ statusLabel = null
431
+ StatusLabel $ statusLabel = null ,
432
+ CreditmemoValidator $ creditmemoValidator = null
410
433
) {
411
434
$ this ->_storeManager = $ storeManager ;
412
435
$ this ->_orderConfig = $ orderConfig ;
@@ -439,6 +462,8 @@ public function __construct(
439
462
$ this ->regionResource = $ regionResource ?: ObjectManager::getInstance ()->get (RegionResource::class);
440
463
$ this ->regionItems = [];
441
464
$ this ->statusLabel = $ statusLabel ?: ObjectManager::getInstance ()->get (StatusLabel::class);
465
+ $ this ->creditmemoValidator = $ creditmemoValidator ?:
466
+ ObjectManager::getInstance ()->get (CreditmemoValidator::class);
442
467
parent ::__construct (
443
468
$ context ,
444
469
$ registry ,
@@ -744,18 +769,24 @@ private function canCreditmemoForZeroTotalRefunded($totalRefunded)
744
769
*/
745
770
private function canCreditmemoForZeroTotal ($ totalRefunded )
746
771
{
772
+ foreach ($ this ->getAllItems () as $ orderItem ) {
773
+ if ($ this ->creditmemoValidator ->canRefundItem ($ orderItem )) {
774
+ return true ;
775
+ }
776
+ }
777
+
747
778
$ totalPaid = $ this ->getTotalPaid ();
748
779
//check if total paid is less than grandtotal
749
780
$ checkAmtTotalPaid = $ totalPaid <= $ this ->getGrandTotal ();
750
781
//case when amount is due for invoice
751
782
$ hasDueAmount = $ this ->canInvoice () && ($ checkAmtTotalPaid );
752
783
//case when paid amount is refunded and order has creditmemo created
753
- $ creditmemos = ( $ this ->getCreditmemosCollection () === false ) ?
754
- true : ( $ this ->_memoCollectionFactory ->create ()->setOrderFilter ($ this )->getTotalCount () > 0 ) ;
784
+ $ creditmemos = $ this ->getCreditmemosCollection () === false ||
785
+ $ this ->_memoCollectionFactory ->create ()->setOrderFilter ($ this )->getTotalCount () > 0 ;
755
786
$ paidAmtIsRefunded = $ this ->getTotalRefunded () == $ totalPaid && $ creditmemos ;
756
- if (( $ hasDueAmount || $ paidAmtIsRefunded ) ||
757
- (! $ checkAmtTotalPaid &&
758
- abs ($ totalRefunded - $ this ->getAdjustmentNegative ()) < .0001 )) {
787
+ if ($ hasDueAmount ||
788
+ $ paidAmtIsRefunded ||
789
+ (! $ checkAmtTotalPaid && abs ($ totalRefunded - $ this ->getAdjustmentNegative ()) < .0001 )) {
759
790
return false ;
760
791
}
761
792
return true ;
0 commit comments