1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NFePHP\MDFe; |
4
|
|
|
|
5
|
|
|
use DOMDocument; |
6
|
|
|
use NFePHP\MDFe\Common\Standardize; |
7
|
|
|
use NFePHP\MDFe\Exception\DocumentsException; |
8
|
|
|
use NFePHP\Common\Strings; |
9
|
|
|
|
10
|
|
|
class Complements |
11
|
|
|
{ |
12
|
|
|
protected static $urlPortal = 'http://www.portalfiscal.inf.br/mdfe'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Authorize document adding his protocol |
16
|
|
|
* @param string $request |
17
|
|
|
* @param string $response |
18
|
|
|
* @return string |
19
|
|
|
*/ |
20
|
|
|
public static function toAuthorize($request, $response) |
21
|
|
|
{ |
22
|
|
|
$st = new Standardize(); |
23
|
|
|
$key = ucfirst($st->whichIs($request)); |
24
|
|
|
if ($key != 'MDFe' && $key != 'EventoMDFe') { |
25
|
|
|
//wrong document, this document is not able to recieve a protocol |
26
|
|
|
throw DocumentsException::wrongDocument(0, $key); |
27
|
|
|
} |
28
|
|
|
$func = "add" . $key . "Protocol"; |
29
|
|
|
return self::$func($request, $response); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Authorize MDFe |
34
|
|
|
* @param string $request |
35
|
|
|
* @param string $response |
36
|
|
|
* @return string |
37
|
|
|
* @throws InvalidArgumentException |
38
|
|
|
*/ |
39
|
|
|
protected static function addMDFeProtocol($request, $response) |
40
|
|
|
{ |
41
|
|
|
$req = new DOMDocument('1.0', 'UTF-8'); |
42
|
|
|
$req->preserveWhiteSpace = false; |
43
|
|
|
$req->formatOutput = false; |
44
|
|
|
$req->loadXML($request); |
45
|
|
|
|
46
|
|
|
$mdfe = $req->getElementsByTagName('MDFe')->item(0); |
47
|
|
|
$infMDFe = $req->getElementsByTagName('infMDFe')->item(0); |
48
|
|
|
$versao = $infMDFe->getAttribute("versao"); |
49
|
|
|
$digMDFe = $req->getElementsByTagName('DigestValue') |
50
|
|
|
->item(0) |
51
|
|
|
->nodeValue; |
52
|
|
|
|
53
|
|
|
$ret = new DOMDocument('1.0', 'UTF-8'); |
54
|
|
|
$ret->preserveWhiteSpace = false; |
55
|
|
|
$ret->formatOutput = false; |
56
|
|
|
$ret->loadXML($response); |
57
|
|
|
$retProt = $ret->getElementsByTagName('protMDFe')->item(0); |
58
|
|
|
if (!isset($retProt)) { |
59
|
|
|
throw DocumentsException::wrongDocument(3, "<protMDFe>"); |
60
|
|
|
} |
61
|
|
|
$infProt = $ret->getElementsByTagName('infProt')->item(0); |
62
|
|
|
$cStat = $infProt->getElementsByTagName('cStat')->item(0)->nodeValue; |
63
|
|
|
$xMotivo = $infProt->getElementsByTagName('xMotivo')->item(0)->nodeValue; |
64
|
|
|
$dig = $infProt->getElementsByTagName("digVal")->item(0); |
65
|
|
|
$digProt = '000'; |
66
|
|
|
if (isset($dig)) { |
67
|
|
|
$digProt = $dig->nodeValue; |
68
|
|
|
} |
69
|
|
|
//100 Autorizado |
70
|
|
|
if ($cStat != '100') { |
71
|
|
|
throw DocumentsException::wrongDocument(4, "[$cStat] $xMotivo"); |
72
|
|
|
} |
73
|
|
|
if ($digMDFe !== $digProt) { |
74
|
|
|
throw DocumentsException::wrongDocument(5, "O digest é diferente"); |
75
|
|
|
} |
76
|
|
|
return self::join( |
77
|
|
|
$req->saveXML($mdfe), |
78
|
|
|
$ret->saveXML($retProt), |
79
|
|
|
'mdfeProc', |
80
|
|
|
$versao |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Authorize Event |
86
|
|
|
* @param string $request |
87
|
|
|
* @param string $response |
88
|
|
|
* @return string |
89
|
|
|
* @throws InvalidArgumentException |
90
|
|
|
*/ |
91
|
|
|
protected static function addEventoMDFeProtocol($request, $response) |
92
|
|
|
{ |
93
|
|
|
$ev = new \DOMDocument('1.0', 'UTF-8'); |
94
|
|
|
$ev->preserveWhiteSpace = false; |
95
|
|
|
$ev->formatOutput = false; |
96
|
|
|
$ev->loadXML($request); |
97
|
|
|
//extrai tag evento do xml origem (solicitação) |
98
|
|
|
$event = $ev->getElementsByTagName('eventoMDFe')->item(0); |
99
|
|
|
$versao = $event->getAttribute('versao'); |
100
|
|
|
|
101
|
|
|
$ret = new \DOMDocument('1.0', 'UTF-8'); |
102
|
|
|
$ret->preserveWhiteSpace = false; |
103
|
|
|
$ret->formatOutput = false; |
104
|
|
|
$ret->loadXML($response); |
105
|
|
|
//extrai a rag retEvento da resposta (retorno da SEFAZ) |
106
|
|
|
$retEv = $ret->getElementsByTagName('retEventoMDFe')->item(0); |
107
|
|
|
$cStat = $retEv->getElementsByTagName('cStat')->item(0)->nodeValue; |
108
|
|
|
$xMotivo = $retEv->getElementsByTagName('xMotivo')->item(0)->nodeValue; |
109
|
|
|
$tpEvento = $retEv->getElementsByTagName('tpEvento')->item(0)->nodeValue; |
110
|
|
|
if ($tpEvento == '110111') { |
111
|
|
|
$node = 'procCancMDFe'; |
112
|
|
|
} elseif ($tpEvento == '110112') { |
113
|
|
|
$node = 'procEncMDFe'; |
114
|
|
|
} elseif ($tpEvento == '110114') { |
115
|
|
|
$node = 'procIncCondutor'; |
116
|
|
|
} elseif ($tpEvento == '110115') { |
117
|
|
|
$node = 'procIncDFe'; |
118
|
|
|
} else { |
119
|
|
|
throw DocumentsException::wrongDocument(4, "Evento não disponivel."); |
120
|
|
|
} |
121
|
|
|
if ($cStat != '135') { |
122
|
|
|
throw DocumentsException::wrongDocument(4, "[$cStat] $xMotivo"); |
123
|
|
|
} |
124
|
|
|
return self::join( |
125
|
|
|
$ev->saveXML($event), |
126
|
|
|
$ret->saveXML($retEv), |
127
|
|
|
$node, |
128
|
|
|
$versao |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Join the pieces of the source document with those of the answer |
134
|
|
|
* @param string $first |
135
|
|
|
* @param string $second |
136
|
|
|
* @param string $nodename |
137
|
|
|
* @param string $versao |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
protected static function join($first, $second, $nodename, $versao) |
141
|
|
|
{ |
142
|
|
|
$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" |
143
|
|
|
. "<$nodename versao=\"$versao\" " |
144
|
|
|
. "xmlns=\"" . self::$urlPortal . "\">"; |
145
|
|
|
$xml .= $first; |
146
|
|
|
$xml .= $second; |
147
|
|
|
$xml .= "</$nodename>"; |
148
|
|
|
return $xml; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Add cancel protocol to a autorized MDFE |
154
|
|
|
* if event is not a cancellation will return |
155
|
|
|
* the same autorized MDFE passing |
156
|
|
|
* NOTE: This action is not necessary, I use only for my needs to |
157
|
|
|
* leave the MDFE marked as Canceled in order to avoid mistakes |
158
|
|
|
* after its cancellation. |
159
|
|
|
* @param string $MDFE content of autorized MDFE XML |
|
|
|
|
160
|
|
|
* @param string $cancelamento content of SEFAZ response |
161
|
|
|
* @return string |
162
|
|
|
* @throws \InvalidArgumentException |
163
|
|
|
*/ |
164
|
|
|
public static function cancelRegister($mdfe, $cancelamento) |
165
|
|
|
{ |
166
|
|
|
$dommdfe = new DOMDocument('1.0', 'utf-8'); |
167
|
|
|
$dommdfe->formatOutput = false; |
168
|
|
|
$dommdfe->preserveWhiteSpace = false; |
169
|
|
|
$dommdfe->loadXML($mdfe); |
170
|
|
|
$proMDFe = $dommdfe->getElementsByTagName('protMDFe')->item(0); |
171
|
|
|
if (empty($proMDFe)) { |
172
|
|
|
//not protocoladed MDFe |
173
|
|
|
throw DocumentsException::wrongDocument(1); |
174
|
|
|
} |
175
|
|
|
$chaveMdfe = $proMDFe->getElementsByTagName('chMDFe')->item(0)->nodeValue; |
176
|
|
|
$domcanc = new DOMDocument('1.0', 'utf-8'); |
177
|
|
|
$domcanc->formatOutput = false; |
178
|
|
|
$domcanc->preserveWhiteSpace = false; |
179
|
|
|
$domcanc->loadXML($cancelamento); |
180
|
|
|
$eventos = $domcanc->getElementsByTagName('retEventoMDFe'); |
181
|
|
|
foreach ($eventos as $evento) { |
182
|
|
|
$infEvento = $evento->getElementsByTagName('infEvento')->item(0); |
183
|
|
|
$cStat = $infEvento->getElementsByTagName('cStat') |
184
|
|
|
->item(0) |
185
|
|
|
->nodeValue; |
186
|
|
|
$nProt = $infEvento->getElementsByTagName('nProt') |
187
|
|
|
->item(0) |
188
|
|
|
->nodeValue; |
189
|
|
|
$chaveEvento = $infEvento->getElementsByTagName('chMDFe') |
190
|
|
|
->item(0) |
191
|
|
|
->nodeValue; |
192
|
|
|
$tpEvento = $infEvento->getElementsByTagName('tpEvento') |
193
|
|
|
->item(0) |
194
|
|
|
->nodeValue; |
195
|
|
|
$proMDFe->getElementsByTagName('nProt') |
196
|
|
|
->item(0) |
197
|
|
|
->nodeValue = $nProt; |
198
|
|
|
if (in_array($cStat, ['135', '136', '155']) |
199
|
|
|
&& $tpEvento == '110111' |
200
|
|
|
&& $chaveEvento == $chaveMdfe |
201
|
|
|
) { |
202
|
|
|
$node = $dommdfe->importNode($evento, true); |
203
|
|
|
$dommdfe->documentElement->appendChild($node); |
204
|
|
|
break; |
205
|
|
|
} elseif (in_array($cStat, ['135', '136', '155']) |
206
|
|
|
&& $tpEvento == '110112' |
207
|
|
|
&& $chaveEvento == $chaveMdfe |
208
|
|
|
) { |
209
|
|
|
$node = $dommdfe->importNode($evento, true); |
210
|
|
|
$dommdfe->documentElement->appendChild($node); |
211
|
|
|
break; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
return $dommdfe->saveXML(); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public static function closeRegister($mdfe, $encerramento) |
218
|
|
|
{ |
219
|
|
|
return self::cancelRegister($mdfe, $encerramento); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.