RoundingErrors.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 rounding errors.
  9. *
  10. * Rounding errors may be caused by tax settings misconfiguration.
  11. */
  12. class RoundingErrors 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. * Stores with invalid display settings
  28. *
  29. * @var array
  30. */
  31. private $storesWithInvalidSettings;
  32. /**
  33. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  34. * @param \Magento\Framework\UrlInterface $urlBuilder
  35. * @param \Magento\Tax\Model\Config $taxConfig
  36. */
  37. public function __construct(
  38. \Magento\Store\Model\StoreManagerInterface $storeManager,
  39. \Magento\Framework\UrlInterface $urlBuilder,
  40. \Magento\Tax\Model\Config $taxConfig
  41. ) {
  42. $this->storeManager = $storeManager;
  43. $this->urlBuilder = $urlBuilder;
  44. $this->taxConfig = $taxConfig;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. * @codeCoverageIgnore
  49. */
  50. public function getIdentity()
  51. {
  52. return 'TAX_NOTIFICATION_ROUNDING_ERRORS';
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function isDisplayed()
  58. {
  59. if (!$this->taxConfig->isWrongDisplaySettingsIgnored() && $this->getStoresWithWrongSettings()) {
  60. return true;
  61. }
  62. return false;
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function getText()
  68. {
  69. $messageDetails = '';
  70. if (!empty($this->getStoresWithWrongSettings()) && !$this->taxConfig->isWrongDisplaySettingsIgnored()) {
  71. $messageDetails .= '<strong>';
  72. $messageDetails .= __('Your current tax configuration may result in rounding errors. ');
  73. $messageDetails .= '</strong><p>';
  74. $messageDetails .= __('Store(s) affected: ');
  75. $messageDetails .= implode(', ', $this->getStoresWithWrongSettings());
  76. $messageDetails .= '</p><p>';
  77. $messageDetails .= __(
  78. 'Click on the link to <a href="%1">ignore this notification</a>',
  79. $this->urlBuilder->getUrl('tax/tax/ignoreTaxNotification', ['section' => 'price_display'])
  80. );
  81. $messageDetails .= "</p>";
  82. }
  83. return $messageDetails;
  84. }
  85. /**
  86. * {@inheritdoc}
  87. * @codeCoverageIgnore
  88. */
  89. public function getSeverity()
  90. {
  91. return self::SEVERITY_CRITICAL;
  92. }
  93. /**
  94. * Check if tax calculation type and price display settings are compatible
  95. *
  96. * Invalid settings if
  97. * Tax Calculation Method Based On 'Total' or 'Row'
  98. * and at least one Price Display Settings has 'Including and Excluding Tax' value
  99. *
  100. * @param null|int|bool|string|\Magento\Store\Model\Store $store $store
  101. * @return bool
  102. */
  103. private function checkSettings($store = null)
  104. {
  105. if ($this->taxConfig->getAlgorithm($store) == \Magento\Tax\Model\Calculation::CALC_UNIT_BASE) {
  106. return true;
  107. }
  108. return $this->taxConfig->getPriceDisplayType($store) != \Magento\Tax\Model\Config::DISPLAY_TYPE_BOTH
  109. && $this->taxConfig->getShippingPriceDisplayType($store) != \Magento\Tax\Model\Config::DISPLAY_TYPE_BOTH
  110. && !$this->taxConfig->displayCartPricesBoth($store)
  111. && !$this->taxConfig->displayCartSubtotalBoth($store)
  112. && !$this->taxConfig->displayCartShippingBoth($store)
  113. && !$this->taxConfig->displaySalesPricesBoth($store)
  114. && !$this->taxConfig->displaySalesSubtotalBoth($store)
  115. && !$this->taxConfig->displaySalesShippingBoth($store);
  116. }
  117. /**
  118. * Return list of store names which have not compatible tax calculation type and price display settings.
  119. * Return true if settings are wrong for default store.
  120. *
  121. * @return array
  122. */
  123. private function getStoresWithWrongSettings()
  124. {
  125. if (null !== $this->storesWithInvalidSettings) {
  126. return $this->storesWithInvalidSettings;
  127. }
  128. $this->storesWithInvalidSettings = [];
  129. $storeCollection = $this->storeManager->getStores(true);
  130. foreach ($storeCollection as $store) {
  131. if (!$this->checkSettings($store)) {
  132. $website = $store->getWebsite();
  133. $this->storesWithInvalidSettings[] = $website->getName() . ' (' . $store->getName() . ')';
  134. }
  135. }
  136. return $this->storesWithInvalidSettings;
  137. }
  138. }