|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NFePHP\Common; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class to create and validate the identification keys of electronic documents |
|
7
|
|
|
* from SPED |
|
8
|
|
|
* @category NFePHP |
|
9
|
|
|
* @package NFePHP\Common\Keys |
|
10
|
|
|
* @copyright Copyright (c) 2008-2016 |
|
11
|
|
|
* @license http://www.gnu.org/licenses/lesser.html LGPL v3 |
|
12
|
|
|
* @author Roberto L. Machado <linux.rlm at gmail dot com> |
|
13
|
|
|
* @link http://github.com/nfephp-org/nfephp for the canonical source repository |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
class Keys |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Build 44 digits keys to NFe, NFCe, CTe and MDFe |
|
20
|
|
|
* @param string $cUF UF number |
|
21
|
|
|
* @param string $ano year |
|
22
|
|
|
* @param string $mes month |
|
23
|
|
|
* @param string $cnpj |
|
24
|
|
|
* @param string $mod model of document 55, 65, 57 etc |
|
25
|
|
|
* @param string $serie |
|
26
|
|
|
* @param string $numero document number |
|
27
|
|
|
* @param string $tpEmis emission type |
|
28
|
|
|
* @param string $codigo random number or document number |
|
29
|
|
|
* @return string |
|
30
|
|
|
*/ |
|
31
|
1 |
|
public static function build( |
|
32
|
|
|
$cUF, |
|
33
|
|
|
$ano, |
|
34
|
|
|
$mes, |
|
35
|
|
|
$cnpj, |
|
36
|
|
|
$mod, |
|
37
|
|
|
$serie, |
|
38
|
|
|
$numero, |
|
39
|
|
|
$tpEmis, |
|
40
|
|
|
$codigo = null |
|
41
|
|
|
) { |
|
42
|
1 |
|
if (empty($codigo)) { |
|
43
|
|
|
$codigo = self::random(); |
|
44
|
|
|
} |
|
45
|
1 |
|
$format = "%02d%02d%02d%s%02d%03d%09d%01d%08d"; |
|
46
|
1 |
|
$key = sprintf( |
|
47
|
|
|
$format, |
|
48
|
|
|
$cUF, |
|
49
|
|
|
$ano, |
|
50
|
|
|
$mes, |
|
51
|
|
|
$cnpj, |
|
52
|
|
|
$mod, |
|
53
|
|
|
$serie, |
|
54
|
|
|
$numero, |
|
55
|
|
|
$tpEmis, |
|
56
|
|
|
$codigo |
|
57
|
|
|
); |
|
58
|
1 |
|
return $key . self::verifyingDigit($key); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Verifies that the key provided is valid |
|
63
|
|
|
* @param string $key |
|
64
|
|
|
* @return boolean |
|
65
|
|
|
*/ |
|
66
|
2 |
|
public static function isValid($key) |
|
67
|
|
|
{ |
|
68
|
2 |
|
if (strlen($key) != 44) { |
|
69
|
|
|
return false; |
|
70
|
|
|
} |
|
71
|
2 |
|
$cDV = substr($key, -1); |
|
72
|
2 |
|
$calcDV = self::verifyingDigit(substr($key, 0, 43)); |
|
73
|
2 |
|
if ($cDV === $calcDV) { |
|
74
|
1 |
|
return true; |
|
75
|
|
|
} |
|
76
|
1 |
|
return false; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* This method calculates verifying digit |
|
81
|
|
|
* @param string $key |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
5 |
|
public static function verifyingDigit($key) |
|
85
|
|
|
{ |
|
86
|
5 |
|
if (strlen($key) != 43) { |
|
87
|
1 |
|
return ''; |
|
88
|
|
|
} |
|
89
|
4 |
|
$multipliers = [2, 3, 4, 5, 6, 7, 8, 9]; |
|
90
|
4 |
|
$iCount = 42; |
|
91
|
4 |
|
$weightedSum = 0; |
|
92
|
4 |
|
while ($iCount >= 0) { |
|
93
|
4 |
|
for ($mCount = 0; $mCount < 8 && $iCount >= 0; $mCount++) { |
|
94
|
4 |
|
$weightedSum += (substr($key, $iCount, 1) * $multipliers[$mCount]); |
|
95
|
4 |
|
$iCount--; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
4 |
|
$vdigit = 11 - ($weightedSum % 11); |
|
99
|
4 |
|
if ($vdigit > 9) { |
|
100
|
3 |
|
$vdigit = 0; |
|
101
|
|
|
} |
|
102
|
4 |
|
return (string) $vdigit; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Generate and return a 8 digits random number |
|
107
|
|
|
* for cNF tag |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
|
|
public static function random() |
|
111
|
|
|
{ |
|
112
|
|
|
return str_pad(mt_rand(0, 99999999), 8, '0', STR_PAD_LEFT); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|