1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Brazanation\Documents; |
4
|
|
|
|
5
|
|
|
final class NFeAccessKey extends AbstractDocument implements DocumentInterface |
6
|
|
|
{ |
7
|
|
|
const LABEL = 'NFeAccessKey'; |
8
|
|
|
|
9
|
|
|
const LENGTH = 44; |
10
|
|
|
|
11
|
|
|
const REGEX = '/([\d]{4})/'; |
12
|
|
|
|
13
|
|
|
const MASK = '$1 '; |
14
|
|
|
|
15
|
|
|
const MODEL = 55; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* NFeAccessKey constructor. |
19
|
|
|
* |
20
|
|
|
* @param $nfeKey |
21
|
|
|
*/ |
22
|
17 |
|
public function __construct($nfeKey) |
23
|
|
|
{ |
24
|
17 |
|
$nfeKey = preg_replace('/\D/', '', $nfeKey); |
25
|
17 |
|
parent::__construct($nfeKey, self::LENGTH, 1, self::LABEL); |
26
|
4 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Generates a valid NFe Access Key. |
30
|
|
|
* |
31
|
|
|
* @param int $state IBGE state code. |
32
|
|
|
* @param \DateTime $generatedAt Year and month when invoice was created. |
33
|
|
|
* @param Cnpj $cnpj Cnpj from issuer. |
34
|
|
|
* @param int $sequence Invoice sequence. |
35
|
|
|
* @param int $invoiceNumber Invoice number. |
36
|
|
|
* @param int $controlNumber Control number. |
37
|
|
|
* |
38
|
|
|
* @return NFeAccessKey |
39
|
|
|
*/ |
40
|
1 |
|
public static function generate( |
41
|
|
|
$state, |
42
|
|
|
\DateTime $generatedAt, |
43
|
|
|
Cnpj $cnpj, |
44
|
|
|
$sequence, |
45
|
|
|
$invoiceNumber, |
46
|
|
|
$controlNumber |
47
|
|
|
) { |
48
|
1 |
|
$yearMonth = $generatedAt->format('ym'); |
49
|
1 |
|
$sequence = str_pad($sequence, 3, 0, STR_PAD_LEFT); |
50
|
1 |
|
$model = self::MODEL; |
51
|
1 |
|
$invoiceNumber = str_pad($invoiceNumber, 9, 0, STR_PAD_LEFT); |
52
|
1 |
|
$controlNumber = str_pad($controlNumber, 9, 0, STR_PAD_LEFT); |
53
|
|
|
|
54
|
1 |
|
$baseNumber = "{$state}{$yearMonth}{$cnpj}{$model}{$sequence}{$invoiceNumber}{$controlNumber}"; |
55
|
|
|
|
56
|
1 |
|
$digit = self::calculateDigitFrom($baseNumber); |
57
|
|
|
|
58
|
1 |
|
return new self("{$baseNumber}{$digit}"); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
1 |
|
public function format() |
65
|
|
|
{ |
66
|
1 |
|
return trim(preg_replace(self::REGEX, self::MASK, "{$this}")); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
13 |
|
public function calculateDigit($baseNumber) |
73
|
|
|
{ |
74
|
13 |
|
return self::calculateDigitFrom($baseNumber); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Calculate check digit from base number. |
79
|
|
|
* |
80
|
|
|
* It is static because is used from generate static method. |
81
|
|
|
* |
82
|
|
|
* @param string $baseNumber Base numeric section to be calculate your digit. |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
13 |
|
private static function calculateDigitFrom($baseNumber) |
87
|
|
|
{ |
88
|
13 |
|
$calculator = new DigitCalculator($baseNumber); |
89
|
13 |
|
$calculator->useComplementaryInsteadOfModule(); |
90
|
13 |
|
$calculator->withModule(DigitCalculator::MODULE_11); |
91
|
13 |
|
$digit = $calculator->calculate(); |
92
|
|
|
|
93
|
13 |
|
return "{$digit}"; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|