DiscountErrors.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Model\System\Message\Notification;
  7. /**
  8. * This class allows to display notification in the admin panel about possible discount errors.
  9. *
  10. * Discount errors may be caused by tax settings misconfiguration.
  11. */
  12. class DiscountErrors implements \Magento\Tax\Model\System\Message\NotificationInterface
  13. {
  14. /**
  15. * @var \Magento\Store\Model\StoreManagerInterface
  16. */
  17. private $storeManager;
  18. /**
  19. * @var \Magento\Framework\UrlInterface
  20. */
  21. private $urlBuilder;
  22. /**
  23. * @var \Magento\Tax\Model\Config
  24. */
  25. private $taxConfig;
  26. /**
  27. * Websites with invalid discount settings
  28. *
  29. * @var array
  30. */
  31. private $storesWithInvalidSettings;
  32. /**
  33. * Initialize dependencies
  34. *
  35. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  36. * @param \Magento\Framework\UrlInterface $urlBuilder
  37. * @param \Magento\Tax\Model\Config $taxConfig
  38. */
  39. public function __construct(
  40. \Magento\Store\Model\StoreManagerInterface $storeManager,
  41. \Magento\Framework\UrlInterface $urlBuilder,
  42. \Magento\Tax\Model\Config $taxConfig
  43. ) {
  44. $this->storeManager = $storeManager;
  45. $this->urlBuilder = $urlBuilder;
  46. $this->taxConfig = $taxConfig;
  47. }
  48. /**
  49. * {@inheritdoc}
  50. * @codeCoverageIgnore
  51. */
  52. public function getIdentity()
  53. {
  54. return 'TAX_NOTIFICATION_DISCOUNT_ERRORS';
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function isDisplayed()
  60. {
  61. if (!$this->taxConfig->isWrongDiscountSettingsIgnored() && $this->getStoresWithWrongSettings()) {
  62. return true;
  63. }
  64. return false;
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function getText()
  70. {
  71. $messageDetails = '';
  72. if (!empty($this->getStoresWithWrongSettings()) && !$this->taxConfig->isWrongDiscountSettingsIgnored()) {
  73. $messageDetails .= '<strong>';
  74. $messageDetails .= __('With customer tax applied “Before Discount”,'
  75. . ' the final discount calculation may not match customers’ expectations. ');
  76. $messageDetails .= '</strong><p>';
  77. $messageDetails .= __('Store(s) affected: ');
  78. $messageDetails .= implode(', ', $this->getStoresWithWrongSettings());
  79. $messageDetails .= '</p><p>';
  80. $messageDetails .= __(
  81. 'Click on the link to <a href="%1">ignore this notification</a>',
  82. $this->urlBuilder->getUrl('tax/tax/ignoreTaxNotification', ['section' => 'discount'])
  83. );
  84. $messageDetails .= "</p>";
  85. }
  86. return $messageDetails;
  87. }
  88. /**
  89. * {@inheritdoc}
  90. * @codeCoverageIgnore
  91. */
  92. public function getSeverity()
  93. {
  94. return self::SEVERITY_CRITICAL;
  95. }
  96. /**
  97. * Check if tax discount settings are compatible
  98. *
  99. * Matrix for invalid discount settings is as follows:
  100. * Before Discount / Excluding Tax
  101. * Before Discount / Including Tax
  102. *
  103. * @param null|int|bool|string|\Magento\Store\Model\Store $store $store
  104. * @return bool
  105. */
  106. private function checkSettings($store = null)
  107. {
  108. return $this->taxConfig->applyTaxAfterDiscount($store);
  109. }
  110. /**
  111. * Return list of store names where tax discount settings are compatible.
  112. * Return true if settings are wrong for default store.
  113. *
  114. * @return array
  115. */
  116. private function getStoresWithWrongSettings()
  117. {
  118. if (null !== $this->storesWithInvalidSettings) {
  119. return $this->storesWithInvalidSettings;
  120. }
  121. $this->storesWithInvalidSettings = [];
  122. $storeCollection = $this->storeManager->getStores(true);
  123. foreach ($storeCollection as $store) {
  124. if (!$this->checkSettings($store)) {
  125. $website = $store->getWebsite();
  126. $this->storesWithInvalidSettings[] = $website->getName() . ' (' . $store->getName() . ')';
  127. }
  128. }
  129. return $this->storesWithInvalidSettings;
  130. }
  131. }