PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/magento/module-tax/Model/System/Message/Notifications.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 269 lines | 136 code | 24 blank | 109 comment | 23 complexity | 9916c49bd21c4b0bd53fad17751409cf MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. 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. */
  17. protected $storeManager;
  18. /**
  19. * @var \Magento\Framework\UrlInterface
  20. */
  21. protected $urlBuilder;
  22. /**
  23. * Tax configuration object
  24. *
  25. * @var \Magento\Tax\Model\Config
  26. */
  27. protected $taxConfig;
  28. /*
  29. * Stores with invalid display settings
  30. *
  31. * @var array
  32. */
  33. protected $storesWithInvalidDisplaySettings;
  34. /*
  35. * Websites with invalid discount settings
  36. *
  37. * @var array
  38. */
  39. protected $storesWithInvalidDiscountSettings;
  40. /**
  41. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  42. * @param \Magento\Framework\UrlInterface $urlBuilder
  43. * @param \Magento\Tax\Model\Config $taxConfig
  44. */
  45. public function __construct(
  46. \Magento\Store\Model\StoreManagerInterface $storeManager,
  47. \Magento\Framework\UrlInterface $urlBuilder,
  48. \Magento\Tax\Model\Config $taxConfig
  49. ) {
  50. $this->storeManager = $storeManager;
  51. $this->urlBuilder = $urlBuilder;
  52. $this->taxConfig = $taxConfig;
  53. }
  54. /**
  55. * Retrieve unique message identity
  56. *
  57. * @return string
  58. */
  59. public function getIdentity()
  60. {
  61. return md5('TAX_NOTIFICATION');
  62. }
  63. /**
  64. * Check if tax calculation type and price display settings are compatible
  65. *
  66. * Invalid settings if
  67. * Tax Calculation Method Based On 'Total' or 'Row'
  68. * and at least one Price Display Settings has 'Including and Excluding Tax' value
  69. *
  70. * @param null|int|bool|string|\Magento\Store\Model\Store $store $store
  71. * @return bool
  72. */
  73. public function checkDisplaySettings($store = null)
  74. {
  75. if ($this->taxConfig->getAlgorithm($store) == \Magento\Tax\Model\Calculation::CALC_UNIT_BASE) {
  76. return true;
  77. }
  78. return $this->taxConfig->getPriceDisplayType($store) != \Magento\Tax\Model\Config::DISPLAY_TYPE_BOTH
  79. && $this->taxConfig->getShippingPriceDisplayType($store) != \Magento\Tax\Model\Config::DISPLAY_TYPE_BOTH
  80. && !$this->taxConfig->displayCartPricesBoth($store)
  81. && !$this->taxConfig->displayCartSubtotalBoth($store)
  82. && !$this->taxConfig->displayCartShippingBoth($store)
  83. && !$this->taxConfig->displaySalesPricesBoth($store)
  84. && !$this->taxConfig->displaySalesSubtotalBoth($store)
  85. && !$this->taxConfig->displaySalesShippingBoth($store);
  86. }
  87. /**
  88. * Check if tax discount settings are compatible
  89. *
  90. * Matrix for invalid discount settings is as follows:
  91. * Before Discount / Excluding Tax
  92. * Before Discount / Including Tax
  93. *
  94. * @param null|int|bool|string|\Magento\Store\Model\Store $store $store
  95. * @return bool
  96. */
  97. public function checkDiscountSettings($store = null)
  98. {
  99. return $this->taxConfig->applyTaxAfterDiscount($store);
  100. }
  101. /**
  102. * Get URL for the tax notification documentation
  103. *
  104. * @return string
  105. */
  106. public function getInfoUrl()
  107. {
  108. return $this->taxConfig->getInfoUrl();
  109. }
  110. /**
  111. * Get URL to the admin tax configuration page
  112. *
  113. * @return string
  114. */
  115. public function getManageUrl()
  116. {
  117. return $this->urlBuilder->getUrl('adminhtml/system_config/edit/section/tax');
  118. }
  119. /**
  120. * Get URL to ignore tax notifications
  121. *
  122. * @param string $section
  123. * @return string
  124. */
  125. public function getIgnoreTaxNotificationUrl($section)
  126. {
  127. return $this->urlBuilder->getUrl('tax/tax/ignoreTaxNotification', ['section' => $section]);
  128. }
  129. /**
  130. * Return list of store names which have not compatible tax calculation type and price display settings.
  131. * Return true if settings are wrong for default store.
  132. *
  133. * @return array
  134. */
  135. public function getStoresWithWrongDisplaySettings()
  136. {
  137. $storeNames = [];
  138. $storeCollection = $this->storeManager->getStores(true);
  139. foreach ($storeCollection as $store) {
  140. if (!$this->checkDisplaySettings($store)) {
  141. $website = $store->getWebsite();
  142. $storeNames[] = $website->getName() . '(' . $store->getName() . ')';
  143. }
  144. }
  145. return $storeNames;
  146. }
  147. /**
  148. * Return list of store names where tax discount settings are compatible.
  149. * Return true if settings are wrong for default store.
  150. *
  151. * @return array
  152. */
  153. public function getStoresWithWrongDiscountSettings()
  154. {
  155. $storeNames = [];
  156. $storeCollection = $this->storeManager->getStores(true);
  157. foreach ($storeCollection as $store) {
  158. if (!$this->checkDiscountSettings($store)) {
  159. $website = $store->getWebsite();
  160. $storeNames[] = $website->getName() . '(' . $store->getName() . ')';
  161. }
  162. }
  163. return $storeNames;
  164. }
  165. /**
  166. * Check whether notification is displayed
  167. * Checks if any of these settings are being ignored or valid:
  168. * 1. Wrong discount settings
  169. * 2. Wrong display settings
  170. *
  171. * @return bool
  172. */
  173. public function isDisplayed()
  174. {
  175. // Check if we are ignoring all notifications
  176. if ($this->taxConfig->isWrongDisplaySettingsIgnored() && $this->taxConfig->isWrongDiscountSettingsIgnored()) {
  177. return false;
  178. }
  179. $this->storesWithInvalidDisplaySettings = $this->getStoresWithWrongDisplaySettings();
  180. $this->storesWithInvalidDiscountSettings = $this->getStoresWithWrongDiscountSettings();
  181. // Check if we have valid tax notifications
  182. if ((!empty($this->storesWithInvalidDisplaySettings) && !$this->taxConfig->isWrongDisplaySettingsIgnored())
  183. || (!empty($this->storesWithInvalidDiscountSettings) && !$this->taxConfig->isWrongDiscountSettingsIgnored())
  184. ) {
  185. return true;
  186. }
  187. return false;
  188. }
  189. /**
  190. * Build message text
  191. * Determine which notification and data to display
  192. *
  193. * @return string
  194. */
  195. public function getText()
  196. {
  197. $messageDetails = '';
  198. if (!empty($this->storesWithInvalidDisplaySettings) && !$this->taxConfig->isWrongDisplaySettingsIgnored()) {
  199. $messageDetails .= '<strong>';
  200. $messageDetails .= __('Warning tax configuration can result in rounding errors. ');
  201. $messageDetails .= '</strong><p>';
  202. $messageDetails .= __('Store(s) affected: ');
  203. $messageDetails .= implode(', ', $this->storesWithInvalidDisplaySettings);
  204. $messageDetails .= '</p><p>';
  205. $messageDetails .= __(
  206. 'Click on the link to <a href="%1">ignore this notification</a>',
  207. $this->getIgnoreTaxNotificationUrl('price_display')
  208. );
  209. $messageDetails .= "</p>";
  210. }
  211. if (!empty($this->storesWithInvalidDiscountSettings) && !$this->taxConfig->isWrongDiscountSettingsIgnored()) {
  212. $messageDetails .= '<strong>';
  213. $messageDetails .= __(
  214. 'Warning tax discount configuration might result in different discounts
  215. than a customer might expect. '
  216. );
  217. $messageDetails .= '</strong><p>';
  218. $messageDetails .= __('Store(s) affected: ');
  219. $messageDetails .= implode(', ', $this->storesWithInvalidDiscountSettings);
  220. $messageDetails .= '</p><p>';
  221. $messageDetails .= __(
  222. 'Click on the link to <a href="%1">ignore this notification</a>',
  223. $this->getIgnoreTaxNotificationUrl('discount')
  224. );
  225. $messageDetails .= "</p>";
  226. }
  227. $messageDetails .= '<p>';
  228. $messageDetails .= __('Please see <a href="%1">documentation</a> for more details. ', $this->getInfoUrl());
  229. $messageDetails .= __(
  230. 'Click here to go to <a href="%1">Tax Configuration</a> and change your settings.',
  231. $this->getManageUrl()
  232. );
  233. $messageDetails .= '</p>';
  234. return $messageDetails;
  235. }
  236. /**
  237. * Retrieve message severity
  238. *
  239. * @return int
  240. */
  241. public function getSeverity()
  242. {
  243. return self::SEVERITY_CRITICAL;
  244. }
  245. }