|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NFePHP\NFe\Factories; |
|
4
|
|
|
|
|
5
|
|
|
use NFePHP\Common\Strings; |
|
6
|
|
|
use NFePHP\Common\Signer; |
|
7
|
|
|
use NFePHP\Common\Keys; |
|
8
|
|
|
use NFePHP\Common\TimeZoneByUF; |
|
9
|
|
|
use NFePHP\Common\UFList; |
|
10
|
|
|
use DateTime; |
|
11
|
|
|
|
|
12
|
|
|
class ContingencyNFe |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Corrects NFe fields when in contingency mode |
|
16
|
|
|
* @param string $xml NFe xml content |
|
17
|
|
|
* @param Contingency $contingency |
|
18
|
|
|
* @return string |
|
19
|
|
|
*/ |
|
20
|
|
|
public static function adjust($xml, Contingency $contingency) |
|
21
|
|
|
{ |
|
22
|
|
|
if ($contingency->type == '') { |
|
23
|
|
|
return $xml; |
|
24
|
|
|
} |
|
25
|
|
|
$xml = Signer::removeSignature($xml); |
|
26
|
|
|
|
|
27
|
|
|
$dom = new \DOMDocument('1.0', 'UTF-8'); |
|
28
|
|
|
$dom->preserveWhiteSpace = false; |
|
29
|
|
|
$dom->formatOutput = false; |
|
30
|
|
|
$dom->loadXML($xml); |
|
31
|
|
|
|
|
32
|
|
|
$ide = $dom->getElementsByTagName('ide')->item(0); |
|
33
|
|
|
$cUF = $ide->getElementsByTagName('cUF')->item(0)->nodeValue; |
|
34
|
|
|
$cNF = $ide->getElementsByTagName('cNF')->item(0)->nodeValue; |
|
35
|
|
|
$nNF = $ide->getElementsByTagName('nNF')->item(0)->nodeValue; |
|
36
|
|
|
$serie = $ide->getElementsByTagName('serie')->item(0)->nodeValue; |
|
37
|
|
|
$mod = $ide->getElementsByTagName('mod')->item(0)->nodeValue; |
|
38
|
|
|
$dtEmi = new DateTime($ide->getElementsByTagName('dhEmi')->item(0)->nodeValue); |
|
39
|
|
|
$ano = $dtEmi->format('y'); |
|
40
|
|
|
$mes = $dtEmi->format('m'); |
|
41
|
|
|
$tpEmis = (string) $contingency->tpEmis; |
|
42
|
|
|
$emit = $dom->getElementsByTagName('emit')->item(0); |
|
43
|
|
|
if (!empty($emit->getElementsByTagName('CNPJ')->item(0)->nodeValue)) { |
|
44
|
|
|
$doc = $emit->getElementsByTagName('CNPJ')->item(0)->nodeValue; |
|
45
|
|
|
} else { |
|
46
|
|
|
$doc = $emit->getElementsByTagName('CPF')->item(0)->nodeValue; |
|
47
|
|
|
} |
|
48
|
|
|
$motivo = trim(Strings::replaceUnacceptableCharacters($contingency->motive)); |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
$tz = TimeZoneByUF::get(UFList::getUFByCode($cUF)); |
|
51
|
|
|
$dt = new \DateTime(date("Y-m-d H:i:sP"), new \DateTimeZone($tz)); |
|
52
|
|
|
|
|
53
|
|
|
$dt->setTimestamp($contingency->timestamp); |
|
54
|
|
|
$ide->getElementsByTagName('tpEmis') |
|
55
|
|
|
->item(0) |
|
56
|
|
|
->nodeValue = $contingency->tpEmis; |
|
57
|
|
|
if (!empty($ide->getElementsByTagName('dhCont')->item(0)->nodeValue)) { |
|
58
|
|
|
$ide->getElementsByTagName('dhCont') |
|
59
|
|
|
->item(0) |
|
60
|
|
|
->nodeValue = $dt->format('Y-m-d\TH:i:sP'); |
|
61
|
|
|
} else { |
|
62
|
|
|
$dhCont = $dom->createElement('dhCont', $dt->format('Y-m-d\TH:i:sP')); |
|
63
|
|
|
$ide->appendChild($dhCont); |
|
64
|
|
|
} |
|
65
|
|
|
if (!empty($ide->getElementsByTagName('xJust')->item(0)->nodeValue)) { |
|
66
|
|
|
$ide->getElementsByTagName('xJust')->item(0)->nodeValue = $motivo; |
|
67
|
|
|
} else { |
|
68
|
|
|
$xJust = $dom->createElement('xJust', $motivo); |
|
69
|
|
|
$ide->appendChild($xJust); |
|
70
|
|
|
} |
|
71
|
|
|
//corrigir a chave |
|
72
|
|
|
$infNFe = $dom->getElementsByTagName('infNFe')->item(0); |
|
73
|
|
|
$chave = Keys::build( |
|
74
|
|
|
$cUF, |
|
75
|
|
|
$ano, |
|
76
|
|
|
$mes, |
|
77
|
|
|
$doc, |
|
78
|
|
|
$mod, |
|
79
|
|
|
$serie, |
|
80
|
|
|
$nNF, |
|
81
|
|
|
$tpEmis, |
|
82
|
|
|
$cNF |
|
83
|
|
|
); |
|
84
|
|
|
$ide->getElementsByTagName('cDV')->item(0)->nodeValue = substr($chave, -1); |
|
85
|
|
|
$infNFe->setAttribute('Id', 'NFe' . $chave); |
|
86
|
|
|
return Strings::clearXmlString($dom->saveXML(), true); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|