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