1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright © O2TI. All rights reserved. |
4
|
|
|
* |
5
|
|
|
* @author Bruno Elisei <[email protected]> |
6
|
|
|
* See COPYING.txt for license details. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace O2TI\TaxDocumentValidationBr\Model; |
10
|
|
|
|
11
|
|
|
use O2TI\TaxDocumentValidationBr\Helper\Config; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* ValidationBr - Add Validation Brazilian for Tax Document. |
15
|
|
|
*/ |
16
|
|
|
class ValidationBr |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var Config |
20
|
|
|
*/ |
21
|
|
|
protected $config; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param Config $config |
25
|
|
|
*/ |
26
|
|
|
public function __construct( |
27
|
|
|
Config $config |
28
|
|
|
) { |
29
|
|
|
$this->config = $config; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Change Rule Validation. |
34
|
|
|
* |
35
|
|
|
* @param array $fields |
36
|
|
|
* |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
|
|
public function addRuleValidation(array $fields): array |
40
|
|
|
{ |
41
|
|
|
foreach ($fields as $key => $data) { |
42
|
|
|
if ($key === 'vat_id') { |
43
|
|
|
if ($this->config->getConfigByVatId('enabled_cpf') && $this->config->getConfigByVatId('enabled_cnpj')) { |
44
|
|
|
$fields[$key]['validation'] = [ |
45
|
|
|
'required-entry' => 1, |
46
|
|
|
Config::VAT_CPF_OR_CNPJ => 1, |
47
|
|
|
]; |
48
|
|
|
} elseif ($this->config->getConfigByVatId('enabled_cpf')) { |
49
|
|
|
$fields[$key]['validation'] = [ |
50
|
|
|
'required-entry' => 1, |
51
|
|
|
Config::VAT_ONLY_CPF => 1, |
52
|
|
|
]; |
53
|
|
|
} elseif ($this->config->getConfigByVatId('enabled_cnpj')) { |
54
|
|
|
$fields[$key]['validation'] = [ |
55
|
|
|
'required-entry' => 1, |
56
|
|
|
Config::VAT_ONLY_CNPJ => 1, |
57
|
|
|
]; |
58
|
|
|
} |
59
|
|
|
} elseif ($key === 'taxvat') { |
60
|
|
|
if ($this->config->getConfigByTaxvat('enabled_cpf') |
61
|
|
|
&& $this->config->getConfigByTaxvat('enabled_cnpj') |
62
|
|
|
) { |
63
|
|
|
$fields[$key]['validation'] = [ |
64
|
|
|
'required-entry' => 1, |
65
|
|
|
Config::VAT_CPF_OR_CNPJ => 1, |
66
|
|
|
]; |
67
|
|
|
} elseif ($this->config->getConfigByTaxvat('enabled_cpf')) { |
68
|
|
|
$fields[$key]['validation'] = [ |
69
|
|
|
'required-entry' => 1, |
70
|
|
|
Config::VAT_ONLY_CPF => 1, |
71
|
|
|
]; |
72
|
|
|
} elseif ($this->config->getConfigByTaxvat('enabled_cnpj')) { |
73
|
|
|
$fields[$key]['validation'] = [ |
74
|
|
|
'required-entry' => 1, |
75
|
|
|
Config::VAT_ONLY_CNPJ => 1, |
76
|
|
|
]; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
continue; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $fields; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Add Tooltip. |
87
|
|
|
* |
88
|
|
|
* @param array $fields |
89
|
|
|
* |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
|
|
public function addTooltip(array $fields): array |
93
|
|
|
{ |
94
|
|
|
foreach ($fields as $key => $data) { |
95
|
|
|
if ($key === 'vat_id') { |
96
|
|
|
if ($this->config->getConfigByVatId('enabled_cpf') && $this->config->getConfigByVatId('enabled_cnpj')) { |
97
|
|
|
$fields[$key]['config']['tooltip'] = [ |
98
|
|
|
'description' => __('O CPF ou CNPJ é utilizado para envio e emissão de nota fiscal.'), |
99
|
|
|
]; |
100
|
|
|
} elseif ($this->config->getConfigByVatId('enabled_cpf')) { |
101
|
|
|
$fields[$key]['config']['tooltip'] = [ |
102
|
|
|
'description' => __('O CPF é utilizado para envio e emissão de nota fiscal.'), |
103
|
|
|
]; |
104
|
|
|
} elseif ($this->config->getConfigByVatId('enabled_cnpj')) { |
105
|
|
|
$fields[$key]['config']['tooltip'] = [ |
106
|
|
|
'description' => __('O CNPJ é utilizado para envio e emissão de nota fiscal.'), |
107
|
|
|
]; |
108
|
|
|
} |
109
|
|
|
} elseif ($key === 'taxvat') { |
110
|
|
|
if ($this->config->getConfigByTaxvat('enabled_cpf') |
111
|
|
|
&& $this->config->getConfigByTaxvat('enabled_cnpj') |
112
|
|
|
) { |
113
|
|
|
$fields[$key]['config']['tooltip'] = [ |
114
|
|
|
'description' => __('O CPF ou CNPJ é utilizado para envio e emissão de nota fiscal.'), |
115
|
|
|
]; |
116
|
|
|
} elseif ($this->config->getConfigByTaxvat('enabled_cpf')) { |
117
|
|
|
$fields[$key]['config']['tooltip'] = [ |
118
|
|
|
'description' => __('O CPF é utilizado para envio e emissão de nota fiscal.'), |
119
|
|
|
]; |
120
|
|
|
} elseif ($this->config->getConfigByTaxvat('enabled_cnpj')) { |
121
|
|
|
$fields[$key]['config']['tooltip'] = [ |
122
|
|
|
'description' => __('O CNPJ é utilizado para envio e emissão de nota fiscal.'), |
123
|
|
|
]; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
continue; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $fields; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|