TotalsValidator.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model\Order\Creditmemo\Validation;
  7. use Magento\Framework\Pricing\PriceCurrencyInterface;
  8. use Magento\Sales\Model\ValidatorInterface;
  9. /**
  10. * Class TotalsValidator
  11. */
  12. class TotalsValidator implements ValidatorInterface
  13. {
  14. /**
  15. * @var PriceCurrencyInterface
  16. */
  17. private $priceCurrency;
  18. /**
  19. * TotalsValidator constructor.
  20. *
  21. * @param PriceCurrencyInterface $priceCurrency
  22. */
  23. public function __construct(PriceCurrencyInterface $priceCurrency)
  24. {
  25. $this->priceCurrency = $priceCurrency;
  26. }
  27. /**
  28. * @inheritDoc
  29. */
  30. public function validate($entity)
  31. {
  32. $messages = [];
  33. $baseOrderRefund = $this->priceCurrency->round(
  34. $entity->getOrder()->getBaseTotalRefunded() + $entity->getBaseGrandTotal()
  35. );
  36. if ($baseOrderRefund > $this->priceCurrency->round($entity->getOrder()->getBaseTotalPaid())) {
  37. $baseAvailableRefund = $entity->getOrder()->getBaseTotalPaid()
  38. - $entity->getOrder()->getBaseTotalRefunded();
  39. $messages[] = __(
  40. 'The most money available to refund is %1.',
  41. $baseAvailableRefund
  42. );
  43. }
  44. return $messages;
  45. }
  46. }