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