DisableMessage.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * @copyright Vertex. All rights reserved. https://www.vertexinc.com/
  4. * @author Mediotype https://www.mediotype.com/
  5. */
  6. namespace Vertex\Tax\Model\Config;
  7. use Magento\Framework\Exception\NoSuchEntityException;
  8. use Magento\Framework\UrlInterface;
  9. use Magento\Store\Api\Data\StoreInterface;
  10. use Magento\Store\Api\WebsiteRepositoryInterface;
  11. use Magento\Store\Model\ScopeInterface;
  12. use Magento\Store\Model\StoreManagerInterface;
  13. use Vertex\Tax\Model\Config;
  14. /**
  15. * Disable Vertex message
  16. *
  17. * Due to performance concerns, Vertex does not support displaying taxes in catalog prices at this time. This
  18. * notification is used to inform the admin user when Vertex is automatically disabled when such a setting is turned on.
  19. */
  20. class DisableMessage
  21. {
  22. /** @var array */
  23. private $affectedScopes = [];
  24. /** @var Config */
  25. private $config;
  26. /** @var StoreManagerInterface */
  27. private $storeManager;
  28. /** @var UrlInterface */
  29. private $urlBuilder;
  30. /** @var WebsiteRepositoryInterface */
  31. private $websiteRepository;
  32. /**
  33. * @param Config $config
  34. * @param StoreManagerInterface $storeManager
  35. * @param WebsiteRepositoryInterface $websiteRepository
  36. * @param UrlInterface $urlBuilder
  37. */
  38. public function __construct(
  39. Config $config,
  40. StoreManagerInterface $storeManager,
  41. WebsiteRepositoryInterface $websiteRepository,
  42. UrlInterface $urlBuilder
  43. ) {
  44. $this->config = $config;
  45. $this->storeManager = $storeManager;
  46. $this->websiteRepository = $websiteRepository;
  47. $this->urlBuilder = $urlBuilder;
  48. }
  49. /**
  50. * Add store to affected stores
  51. *
  52. * @param StoreInterface $store
  53. * @return void
  54. */
  55. private function addToAffectedScopes(StoreInterface $store)
  56. {
  57. try {
  58. $website = $this->websiteRepository->getById($store->getWebsiteId());
  59. } catch (NoSuchEntityException $e) {
  60. $website = null;
  61. }
  62. $websiteName = $website ? $website->getName() : 'Unknown Website';
  63. $this->affectedScopes[$store->getWebsiteId()] = $websiteName . ' (' . $store->getName() . ')';
  64. }
  65. /**
  66. * Retrieve a list of stores where these incompatible settings exist
  67. *
  68. * @return string[]
  69. */
  70. public function getAffectedScopes()
  71. {
  72. if (!empty($this->affectedScopes)) {
  73. return $this->affectedScopes;
  74. }
  75. foreach ($this->storeManager->getStores(true) as $store) {
  76. if ($this->isStoreAffected($store->getId())) {
  77. $this->addToAffectedScopes($store);
  78. }
  79. }
  80. return $this->affectedScopes;
  81. }
  82. /**
  83. * Retrieve the URL for modifying the configuration that has caused this
  84. *
  85. * @return string
  86. */
  87. private function getManageUrl()
  88. {
  89. return $this->urlBuilder->getUrl(
  90. 'adminhtml/system_config/edit',
  91. ['section' => 'tax', '_fragment' => 'tax_display-head']
  92. );
  93. }
  94. /**
  95. * Retrieve message text
  96. *
  97. * @param string|null $scopeId
  98. * @param bool $showAffectedStores
  99. * @return string
  100. */
  101. public function getMessage($scopeId = null, $showAffectedStores = false)
  102. {
  103. if (empty($this->getAffectedScopes())) {
  104. return '';
  105. }
  106. $scopesToShow = $this->affectedScopes;
  107. if (!$showAffectedStores && $scopeId !== null) {
  108. $scopesToShow = [];
  109. if (isset($this->affectedScopes[$scopeId])) {
  110. $scopesToShow = [$this->affectedScopes[$scopeId]];
  111. }
  112. }
  113. if (empty($scopesToShow)) {
  114. return '';
  115. }
  116. $html = '<p>'
  117. . __(
  118. 'Vertex Tax Calculation has been automatically disabled. ' .
  119. 'Display prices in Catalog must be set to "Excluding Tax" to use Vertex.'
  120. )
  121. . '</p><p>';
  122. if ($showAffectedStores) {
  123. $html .= (count($scopesToShow) > 1 ? __('Stores affected: ') : __('Store affected: '))
  124. . implode(', ', $scopesToShow)
  125. . '</p><p>';
  126. }
  127. $html .= __(
  128. 'Click here to go to <a href="%1">Price Display Settings</a> and change your settings.',
  129. $this->getManageUrl())
  130. . '</p>';
  131. return $html;
  132. }
  133. /**
  134. * Determine whether or not a store has incompatible settings
  135. *
  136. * @param string|null $scopeCode
  137. * @param $scopeType
  138. * @return bool
  139. */
  140. private function isStoreAffected($scopeCode = null, $scopeType = ScopeInterface::SCOPE_STORE)
  141. {
  142. return $this->config->isVertexActive($scopeCode, $scopeType)
  143. && $this->config->isDisplayPriceInCatalogEnabled($scopeCode, $scopeType);
  144. }
  145. }