-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathData.php
More file actions
executable file
·196 lines (166 loc) · 5.08 KB
/
Data.php
File metadata and controls
executable file
·196 lines (166 loc) · 5.08 KB
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
<?php
namespace SystemCode\BrazilCustomerAttributes\Helper;
use Magento\Store\Model\ScopeInterface;
/**
*
* Helper for validations and commons functions
*
*
* NOTICE OF LICENSE
*
* @category SystemCode
* @package Systemcode_BrazilCustomerAttributes
* @author Eduardo Diogo Dias <contato@systemcode.com.br>
* @copyright System Code LTDA-ME
* @license http://opensource.org/licenses/osl-3.0.php
*/
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
const ACCOUNT_SHARE_SCOPE_FLAG_PATH = 'customer/account_share/scope';
const COPY_CNPJ_SOCIAL_NAME = 'brazilcustomerattributes/cnpj/copy_firstname';
const COPY_CNPJ_TRADE_NAME = 'brazilcustomerattributes/cnpj/copy_lastname';
public function getConfig($config_path)
{
return $this->scopeConfig->getValue(
$config_path,
ScopeInterface::SCOPE_STORE
);
}
/**
* @param string $config_path
* @param null|mixed $storeId
*
* @return bool
*/
protected function isSetFlag(string $config_path, $storeId = null): bool
{
return $this->scopeConfig->isSetFlag(
$config_path,
ScopeInterface::SCOPE_STORE,
$storeId
);
}
function validateCPF($cpf) {
// Extrai somente os números
$cpf = preg_replace( '/[^0-9]/is', '', $cpf );
// Verifica se foi informada uma sequência de digitos repetidos. Ex: 111.111.111-11
$invalidos = array('00000000000', '11111111111', '22222222222', '33333333333', '44444444444', '55555555555', '66666666666', '77777777777', '88888888888', '99999999999');
if (in_array($cpf, $invalidos)){
return false;
}
// Verifica se foi informado todos os digitos corretamente
if (strlen($cpf) != 11) {
return false;
}
// Faz o calculo para validar o CPF
for ($t = 9; $t < 11; $t++) {
for ($d = 0, $c = 0; $c < $t; $c++) {
$d += $cpf[$c] * (($t + 1) - $c);
}
$d = ((10 * $d) % 11) % 10;
if ($cpf[$c] != $d) {
return false;
}
}
return true;
}
function validateCNPJ($cnpj) {
$cnpj = preg_replace('/[^0-9]/', '', (string) $cnpj);
if (strlen($cnpj) <> 14)
return false;
$sum = 0;
$sum += ($cnpj[0] * 5);
$sum += ($cnpj[1] * 4);
$sum += ($cnpj[2] * 3);
$sum += ($cnpj[3] * 2);
$sum += ($cnpj[4] * 9);
$sum += ($cnpj[5] * 8);
$sum += ($cnpj[6] * 7);
$sum += ($cnpj[7] * 6);
$sum += ($cnpj[8] * 5);
$sum += ($cnpj[9] * 4);
$sum += ($cnpj[10] * 3);
$sum += ($cnpj[11] * 2);
$d1 = $sum % 11;
$d1 = $d1 < 2 ? 0 : 11 - $d1;
$sum = 0;
$sum += ($cnpj[0] * 6);
$sum += ($cnpj[1] * 5);
$sum += ($cnpj[2] * 4);
$sum += ($cnpj[3] * 3);
$sum += ($cnpj[4] * 2);
$sum += ($cnpj[5] * 9);
$sum += ($cnpj[6] * 8);
$sum += ($cnpj[7] * 7);
$sum += ($cnpj[8] * 6);
$sum += ($cnpj[9] * 5);
$sum += ($cnpj[10] * 4);
$sum += ($cnpj[11] * 3);
$sum += ($cnpj[12] * 2);
$d2 = $sum % 11;
$d2 = $d2 < 2 ? 0 : 11 - $d2;
if ($cnpj[12] == $d1 && $cnpj[13] == $d2) {
return true;
}
else {
return false;
}
}
public function getRegionId($state){
$states = array(
"AC"=>"Acre",
"AL"=>"Alagoas",
"AM"=>"Amazonas",
"AP"=>"Amapá",
"BA"=>"Bahia",
"CE"=>"Ceará",
"DF"=>"Distrito Federal",
"ES"=>"Espírito Santo",
"GO"=>"Goiás",
"MA"=>"Maranhão",
"MT"=>"Mato Grosso",
"MS"=>"Mato Grosso do Sul",
"MG"=>"Minas Gerais",
"PA"=>"Pará",
"PB"=>"Paraíba",
"PR"=>"Paraná",
"PE"=>"Pernambuco",
"PI"=>"Piauí",
"RJ"=>"Rio de Janeiro",
"RN"=>"Rio Grande do Norte",
"RO"=>"Rondônia",
"RS"=>"Rio Grande do Sul",
"RR"=>"Roraima",
"SC"=>"Santa Catarina",
"SE"=>"Sergipe",
"SP"=>"São Paulo",
"TO"=>"Tocantins");
if($states[$state]){
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$region = $objectManager->create('Magento\Directory\Model\Region')
->loadByName($states[$state], "BR");
return $region->getId();
}
}
/**
* @return bool
*/
public function isAccountSharedByWebsite(): bool
{
return $this->isSetFlag(self::ACCOUNT_SHARE_SCOPE_FLAG_PATH);
}
/**
* @return bool
*/
public function copySocialName(): bool
{
return $this->isSetFlag(self::COPY_CNPJ_SOCIAL_NAME);
}
/**
* @return bool
*/
public function copyTradeName(): bool
{
return $this->isSetFlag(self::COPY_CNPJ_TRADE_NAME);
}
}