forked from PrestaShop/vatnumber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vatnumber.php
307 lines (274 loc) · 10.7 KB
/
vatnumber.php
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?php
/*
* 2007-2017 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2017 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
use Vilkas\VatNumber\Client\VatNumberClient;
if (!defined('_PS_VERSION_'))
exit;
class VatNumber extends TaxManagerModule
{
public function __construct()
{
$this->name = 'vatnumber';
$this->tab = 'billing_invoicing';
$this->version = '2.1.1';
$this->author = 'PrestaShop';
$this->need_instance = 0;
$this->tax_manager_class = 'VATNumberTaxManager';
$this->bootstrap = true;
parent::__construct();
$id_country = (int)Configuration::get('VATNUMBER_COUNTRY');
if ($id_country == 0)
$this->warning = $this->l('No default country set.');
$this->displayName = $this->l('European VAT number');
$this->description = $this->l('Enables you to enter the intra-community VAT number when creating the address. You must fill in the company field to allow entering the VAT number.');
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
}
public function install()
{
return
parent::install()
&& Configuration::updateValue('VATNUMBER_MANAGEMENT', 1)
&& $this->registerHook('actionValidateCustomerAddressForm');
}
public function uninstall()
{
return (parent::uninstall() && Configuration::updateValue('VATNUMBER_MANAGEMENT', 0));
}
public function enable($force_all = false)
{
parent::enable($force_all);
Configuration::updateValue('VATNUMBER_MANAGEMENT', 1);
}
public function disable($force_all = false)
{
parent::disable($force_all);
Configuration::updateValue('VATNUMBER_MANAGEMENT', 0);
}
public static function getPrefixIntracomVAT()
{
$intracom_array = array(
'AT' => 'AT',
//Austria
'BE' => 'BE',
//Belgium
'DK' => 'DK',
//Denmark
'FI' => 'FI',
//Finland
'FR' => 'FR',
//France
'FX' => 'FR',
//France métropolitaine
'DE' => 'DE',
//Germany
'GR' => 'EL',
//Greece
'IE' => 'IE',
//Irland
'IT' => 'IT',
//Italy
'LU' => 'LU',
//Luxembourg
'NL' => 'NL',
//Netherlands
'PT' => 'PT',
//Portugal
'ES' => 'ES',
//Spain
'SE' => 'SE',
//Sweden
'GB' => 'GB',
//United Kingdom
'CY' => 'CY',
//Cyprus
'EE' => 'EE',
//Estonia
'HU' => 'HU',
//Hungary
'LV' => 'LV',
//Latvia
'LT' => 'LT',
//Lithuania
'MT' => 'MT',
//Malta
'PL' => 'PL',
//Poland
'SK' => 'SK',
//Slovakia
'CZ' => 'CZ',
//Czech Republic
'SI' => 'SI',
//Slovenia
'RO' => 'RO',
//Romania
'BG' => 'BG',
//Bulgaria
'HR' => 'HR',
//Croatia
);
return $intracom_array;
}
public static function isApplicable($id_country)
{
return (((int)$id_country && array_key_exists(Country::getIsoById($id_country), self::getPrefixIntracomVAT())) ? 1 : 0);
}
public static function WebServiceCheck($vat_number)
{
if (empty($vat_number))
return array();
$vat_number = str_replace(' ', '', $vat_number);
$prefix = Tools::substr($vat_number, 0, 2);
if (array_search($prefix, self::getPrefixIntracomVAT()) === false)
return array(Tools::displayError('Invalid VAT number'));
$vat = Tools::substr($vat_number, 2);
// $newUrl = implode('/', ['https://ec.europa.eu/taxation_customs/vies/rest-api/ms', urlencode($prefix), 'vat', urlencode($vat)]);
// $ch = curl_init($newUrl);
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output = curl_exec($ch);
try {
$client = new VatNumberClient();
$result = $client->getVatInfo($prefix, $vat);
// $result = json_decode($output, true);
if ($result['isValid']) {
return [];
} else {
return [Tools::displayError('VAT number not found')];
}
} catch (Exception $e) {
return [Tools::displayError('VAT number validation failed')];
}
return [Tools::displayError('VAT number validation service unavailable')];
}
public function getContent()
{
$echo = '';
if (Tools::isSubmit('submitVatNumber')) {
if (Configuration::updateValue('VATNUMBER_COUNTRY', (int)Tools::getValue('VATNUMBER_COUNTRY')))
$echo .= $this->displayConfirmation($this->l('Your country has been updated.'));
if (Configuration::updateValue('VATNUMBER_CHECKING', (int)Tools::getValue('VATNUMBER_CHECKING')))
$echo .= ((bool)Tools::getValue('VATNUMBER_CHECKING') ? $this->displayConfirmation($this->l('The check of the VAT number with the WebService is now enabled.')) : $this->displayConfirmation($this->l('The check of the VAT number with the WebService is now disabled.')));
}
$echo .= $this->renderForm();
return $echo;
}
public function renderForm()
{
$countries = Country::getCountries($this->context->language->id);
$countries_fmt = array(
0 => array(
'id' => 0,
'name' => $this->l('-- Choose a country --')
)
);
foreach ($countries as $country)
$countries_fmt[] = array(
'id' => $country['id_country'],
'name' => $country['name']
);
$fields_form = array(
'form' => array(
'legend' => array(
'title' => $this->l('Settings'),
'icon' => 'icon-cogs'
),
'input' => array(
array(
'type' => 'select',
'label' => $this->l('Customers\' country'),
'desc' => $this->l('Filter customers\' country.'),
'name' => 'VATNUMBER_COUNTRY',
'required' => false,
'default_value' => (int)$this->context->country->id,
'options' => array(
'query' => $countries_fmt,
'id' => 'id',
'name' => 'name',
)
),
array(
'type' => 'switch',
'label' => $this->l('Enable checking of the VAT number with the web service'),
'name' => 'VATNUMBER_CHECKING',
'is_bool' => true,
'desc' => $this->l('The verification by the web service is slow. Enabling this option can slow down your shop.'),
'values' => array(
array(
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Enabled')
),
array(
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Disabled')
)
),
)
),
'submit' => array(
'title' => $this->l('Save'),
)
),
);
$helper = new HelperForm();
$helper->module = $this;
$helper->show_toolbar = false;
$helper->table = $this->table;
$lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
$helper->default_form_language = $lang->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
$this->fields_form = array();
$helper->identifier = $this->identifier;
$helper->submit_action = 'submitVatNumber';
$helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false) . '&configure=' . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->tpl_vars = array(
'fields_value' => $this->getConfigFieldsValues(),
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id
);
return $helper->generateForm(array($fields_form));
}
public function getConfigFieldsValues()
{
return array(
'VATNUMBER_COUNTRY' => Tools::getValue('VATNUMBER_COUNTRY', Configuration::get('VATNUMBER_COUNTRY')),
'VATNUMBER_CHECKING' => Tools::getValue('VATNUMBER_CHECKING', Configuration::get('VATNUMBER_CHECKING')),
);
}
public function hookActionValidateCustomerAddressForm(&$params)
{
$form = $params['form'];
$is_valid = true;
if (($vatNumber = $form->getField('vat_number')) && Configuration::get('VATNUMBER_MANAGEMENT') && Configuration::get('VATNUMBER_CHECKING')) {
$isAVatNumber = VatNumber::WebServiceCheck($vatNumber->getValue());
if (is_array($isAVatNumber) && count($isAVatNumber) > 0) {
$vatNumber->addError($isAVatNumber[0]);
$is_valid = false;
}
}
return $is_valid;
}
}