Notifications.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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;
  7. /**
  8. * Notifications class
  9. */
  10. class Notifications implements \Magento\Framework\Notification\MessageInterface
  11. {
  12. /**
  13. * Store manager object
  14. *
  15. * @var \Magento\Store\Model\StoreManagerInterface
  16. * @deprecated 100.1.0
  17. */
  18. protected $storeManager;
  19. /**
  20. * @var \Magento\Framework\UrlInterface
  21. */
  22. protected $urlBuilder;
  23. /**
  24. * Tax configuration object
  25. *
  26. * @var \Magento\Tax\Model\Config
  27. */
  28. protected $taxConfig;
  29. /**
  30. * Stores with invalid display settings
  31. *
  32. * @var array
  33. * @deprecated 100.1.0
  34. * @see \Magento\Tax\Model\System\Message\Notification\RoundingErrors
  35. */
  36. protected $storesWithInvalidDisplaySettings;
  37. /**
  38. * Websites with invalid discount settings
  39. *
  40. * @var array
  41. * @deprecated 100.1.0
  42. * @see \Magento\Tax\Model\System\Message\Notification\DiscountErrors
  43. */
  44. protected $storesWithInvalidDiscountSettings;
  45. /**
  46. * @var NotificationInterface[]
  47. */
  48. private $notifications = [];
  49. /**
  50. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  51. * @param \Magento\Framework\UrlInterface $urlBuilder
  52. * @param \Magento\Tax\Model\Config $taxConfig
  53. * @param NotificationInterface[] $notifications
  54. */
  55. public function __construct(
  56. \Magento\Store\Model\StoreManagerInterface $storeManager,
  57. \Magento\Framework\UrlInterface $urlBuilder,
  58. \Magento\Tax\Model\Config $taxConfig,
  59. $notifications = []
  60. ) {
  61. $this->storeManager = $storeManager;
  62. $this->urlBuilder = $urlBuilder;
  63. $this->taxConfig = $taxConfig;
  64. $this->notifications = $notifications;
  65. }
  66. /**
  67. * Retrieve unique message identity
  68. *
  69. * @return string
  70. * @codeCoverageIgnore
  71. */
  72. public function getIdentity()
  73. {
  74. return md5('TAX_NOTIFICATION');
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function isDisplayed()
  80. {
  81. foreach ($this->notifications as $notification) {
  82. if ($notification->isDisplayed()) {
  83. return true;
  84. }
  85. }
  86. return false;
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function getText()
  92. {
  93. $messageDetails = '';
  94. foreach ($this->notifications as $notification) {
  95. $messageDetails .= $notification->getText();
  96. }
  97. $messageDetails .= '<p>';
  98. $messageDetails .= __('Please see <a href="%1">documentation</a> for more details. ', $this->getInfoUrl());
  99. $messageDetails .= __(
  100. 'Click here to go to <a href="%1">Tax Configuration</a> and change your settings.',
  101. $this->getManageUrl()
  102. );
  103. $messageDetails .= '</p>';
  104. return $messageDetails;
  105. }
  106. /**
  107. * Retrieve message severity
  108. *
  109. * @return int
  110. * @codeCoverageIgnore
  111. */
  112. public function getSeverity()
  113. {
  114. return self::SEVERITY_CRITICAL;
  115. }
  116. /**
  117. * Get URL for the tax notification documentation
  118. *
  119. * @return string
  120. */
  121. public function getInfoUrl()
  122. {
  123. return $this->taxConfig->getInfoUrl();
  124. }
  125. /**
  126. * Get URL to the admin tax configuration page
  127. *
  128. * @return string
  129. */
  130. public function getManageUrl()
  131. {
  132. return $this->urlBuilder->getUrl('adminhtml/system_config/edit/section/tax');
  133. }
  134. /**
  135. * Check if tax calculation type and price display settings are compatible
  136. *
  137. * Invalid settings if
  138. * Tax Calculation Method Based On 'Total' or 'Row'
  139. * and at least one Price Display Settings has 'Including and Excluding Tax' value
  140. *
  141. * @param null|int|bool|string|\Magento\Store\Model\Store $store $store
  142. * @return bool
  143. * @deprecated 100.1.3
  144. * @see \Magento\Tax\Model\System\Message\Notification\RoundingErrors::checkSettings
  145. */
  146. public function checkDisplaySettings($store = null)
  147. {
  148. if ($this->taxConfig->getAlgorithm($store) == \Magento\Tax\Model\Calculation::CALC_UNIT_BASE) {
  149. return true;
  150. }
  151. return $this->taxConfig->getPriceDisplayType($store) != \Magento\Tax\Model\Config::DISPLAY_TYPE_BOTH
  152. && $this->taxConfig->getShippingPriceDisplayType($store) != \Magento\Tax\Model\Config::DISPLAY_TYPE_BOTH
  153. && !$this->taxConfig->displayCartPricesBoth($store)
  154. && !$this->taxConfig->displayCartSubtotalBoth($store)
  155. && !$this->taxConfig->displayCartShippingBoth($store)
  156. && !$this->taxConfig->displaySalesPricesBoth($store)
  157. && !$this->taxConfig->displaySalesSubtotalBoth($store)
  158. && !$this->taxConfig->displaySalesShippingBoth($store);
  159. }
  160. /**
  161. * Check if tax discount settings are compatible
  162. *
  163. * Matrix for invalid discount settings is as follows:
  164. * Before Discount / Excluding Tax
  165. * Before Discount / Including Tax
  166. *
  167. * @param null|int|bool|string|\Magento\Store\Model\Store $store $store
  168. * @return bool
  169. * @deprecated 100.1.3
  170. * @see \Magento\Tax\Model\System\Message\Notification\DiscountErrors::checkSettings
  171. */
  172. public function checkDiscountSettings($store = null)
  173. {
  174. return $this->taxConfig->applyTaxAfterDiscount($store);
  175. }
  176. /**
  177. * Get URL to ignore tax notifications
  178. *
  179. * @param string $section
  180. * @return string
  181. * @deprecated 100.1.3
  182. */
  183. public function getIgnoreTaxNotificationUrl($section)
  184. {
  185. return $this->urlBuilder->getUrl('tax/tax/ignoreTaxNotification', ['section' => $section]);
  186. }
  187. /**
  188. * Return list of store names which have not compatible tax calculation type and price display settings.
  189. * Return true if settings are wrong for default store.
  190. *
  191. * @return array
  192. * @deprecated 100.1.3
  193. * @see \Magento\Tax\Model\System\Message\Notification\RoundingErrors::getStoresWithWrongSettings
  194. */
  195. public function getStoresWithWrongDisplaySettings()
  196. {
  197. $storeNames = [];
  198. $storeCollection = $this->storeManager->getStores(true);
  199. foreach ($storeCollection as $store) {
  200. if (!$this->checkDisplaySettings($store)) {
  201. $website = $store->getWebsite();
  202. $storeNames[] = $website->getName() . '(' . $store->getName() . ')';
  203. }
  204. }
  205. return $storeNames;
  206. }
  207. /**
  208. * Return list of store names where tax discount settings are compatible.
  209. * Return true if settings are wrong for default store.
  210. *
  211. * @return array
  212. * @deprecated 100.1.3
  213. * @see \Magento\Tax\Model\System\Message\Notification\DiscountErrors::getStoresWithWrongSettings
  214. */
  215. public function getStoresWithWrongDiscountSettings()
  216. {
  217. $storeNames = [];
  218. $storeCollection = $this->storeManager->getStores(true);
  219. foreach ($storeCollection as $store) {
  220. if (!$this->checkDiscountSettings($store)) {
  221. $website = $store->getWebsite();
  222. $storeNames[] = $website->getName() . '(' . $store->getName() . ')';
  223. }
  224. }
  225. return $storeNames;
  226. }
  227. }