ApplyDiscountOnPrices.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. use Magento\Tax\Model\Config;
  8. /**
  9. * Class allows to show admin notification about possible issues related to "Apply Discount On Prices" setting.
  10. *
  11. * Warning is displayed in case when "Catalog Prices" = "Excluding Tax"
  12. * AND "Apply Discount On Prices" = "Including Tax"
  13. * AND "Apply Customer Tax" = "After Discount"
  14. */
  15. class ApplyDiscountOnPrices implements \Magento\Tax\Model\System\Message\NotificationInterface
  16. {
  17. /**
  18. * @var \Magento\Store\Model\StoreManagerInterface
  19. */
  20. private $storeManager;
  21. /**
  22. * @var \Magento\Framework\UrlInterface
  23. */
  24. private $urlBuilder;
  25. /**
  26. * @var Config
  27. */
  28. private $taxConfig;
  29. /**
  30. * Stores with invalid display settings
  31. *
  32. * @var array
  33. */
  34. private $storesWithInvalidSettings;
  35. /**
  36. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  37. * @param \Magento\Framework\UrlInterface $urlBuilder
  38. * @param Config $taxConfig
  39. */
  40. public function __construct(
  41. \Magento\Store\Model\StoreManagerInterface $storeManager,
  42. \Magento\Framework\UrlInterface $urlBuilder,
  43. Config $taxConfig
  44. ) {
  45. $this->storeManager = $storeManager;
  46. $this->urlBuilder = $urlBuilder;
  47. $this->taxConfig = $taxConfig;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. * @codeCoverageIgnore
  52. */
  53. public function getIdentity()
  54. {
  55. return 'TAX_NOTIFICATION_APPLY_DISCOUNT';
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function isDisplayed()
  61. {
  62. if (!$this->taxConfig->isWrongApplyDiscountSettingIgnored() && $this->getStoresWithWrongSettings()) {
  63. return true;
  64. }
  65. return false;
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function getText()
  71. {
  72. $messageDetails = '';
  73. if ($this->isDisplayed()) {
  74. $messageDetails .= '<strong>';
  75. $messageDetails .= __('To apply the discount on prices including tax and apply the tax after discount,'
  76. . ' set Catalog Prices to “Including Tax”. ');
  77. $messageDetails .= '</strong><p>';
  78. $messageDetails .= __('Store(s) affected: ');
  79. $messageDetails .= implode(', ', $this->getStoresWithWrongSettings());
  80. $messageDetails .= '</p><p>';
  81. $messageDetails .= __(
  82. 'Click on the link to <a href="%1">ignore this notification</a>',
  83. $this->urlBuilder->getUrl('tax/tax/ignoreTaxNotification', ['section' => 'apply_discount'])
  84. );
  85. $messageDetails .= "</p>";
  86. }
  87. return $messageDetails;
  88. }
  89. /**
  90. * {@inheritdoc}
  91. * @codeCoverageIgnore
  92. */
  93. public function getSeverity()
  94. {
  95. return self::SEVERITY_CRITICAL;
  96. }
  97. /**
  98. * Return list of store names which have invalid settings.
  99. *
  100. * @return array
  101. */
  102. private function getStoresWithWrongSettings()
  103. {
  104. if (null !== $this->storesWithInvalidSettings) {
  105. return $this->storesWithInvalidSettings;
  106. }
  107. $this->storesWithInvalidSettings = [];
  108. $storeCollection = $this->storeManager->getStores(true);
  109. foreach ($storeCollection as $store) {
  110. if (!$this->checkSettings($store)) {
  111. $website = $store->getWebsite();
  112. $this->storesWithInvalidSettings[] = $website->getName() . ' (' . $store->getName() . ')';
  113. }
  114. }
  115. return $this->storesWithInvalidSettings;
  116. }
  117. /**
  118. * Check if settings are valid.
  119. *
  120. * @param null|int|bool|string|\Magento\Store\Model\Store $store $store
  121. * @return bool false if settings are incorrect
  122. */
  123. private function checkSettings($store = null)
  124. {
  125. return $this->taxConfig->priceIncludesTax($store)
  126. || !$this->taxConfig->applyTaxAfterDiscount($store)
  127. || !$this->taxConfig->discountTax($store);
  128. }
  129. }