|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NFePHP\BPe; |
|
4
|
|
|
|
|
5
|
|
|
use NFePHP\BPe\Common\Make as MakeBase; |
|
6
|
|
|
use NFePHP\Common\Keys; |
|
7
|
|
|
|
|
8
|
|
|
class Make extends MakeBase |
|
9
|
|
|
{ |
|
10
|
|
|
protected $rootname = 'BPe'; |
|
11
|
|
|
protected $versao = '1.00'; |
|
12
|
|
|
protected $xmlns = 'http://www.portalfiscal.inf.br/bpe'; |
|
13
|
|
|
|
|
14
|
|
|
protected $available = [ |
|
15
|
|
|
'taginfbpe' => ['class' => Tags\InfBpe::class, 'type' => 'single', 'occurrence' => [0, 1]], |
|
16
|
|
|
'tagide' => ['class' => Tags\Ide::class, 'type' => 'single', 'occurrence' => [1, 1]], |
|
17
|
|
|
'tagemit' => ['class' => Tags\Emit::class, 'type' => 'single', 'occurrence' => [1, 1]], |
|
18
|
|
|
'tagenderemit' => ['class' => Tags\EnderEmit::class, 'type' => 'single', 'occurrence' => [1, 1]], |
|
19
|
|
|
'tagcomp' => ['class' => Tags\Comp::class, 'type' => 'single', 'occurrence' => [0, 1]], |
|
20
|
|
|
'tagendercomp' => ['class' => Tags\EnderComp::class, 'type' => 'single', 'occurrence' => [0, 1]], |
|
21
|
|
|
'tagagencia' => ['class' => Tags\Agencia::class, 'type' => 'single', 'occurrence' => [0, 1]], |
|
22
|
|
|
'tagenderagencia' => ['class' => Tags\EnderAgencia::class, 'type' => 'single', 'occurrence' => [0, 1]], |
|
23
|
|
|
'taginfbpesub' => ['class' => Tags\InfBpeSub::class, 'type' => 'single', 'occurrence' => [0, 1]], |
|
24
|
|
|
'taginfpassagem' => ['class' => Tags\InfPassagem::class, 'type' => 'single', 'occurrence' => [1, 1]], |
|
25
|
|
|
'taginfpassageiro' => ['class' => Tags\InfPassageiro::class, 'type' => 'single', 'occurrence' => [0, 1]], |
|
26
|
|
|
'taginfviagem' => ['class' => Tags\InfViagem::class, 'type' => 'multiple', 'occurrence' => [1, 9999]], |
|
27
|
|
|
'taginfvalorbpe' => ['class' => Tags\InfValorBPe::class, 'type' => 'single', 'occurrence' => [1, 1]], |
|
28
|
|
|
'tagcompvalor' => ['class' => Tags\CompValor::class, 'type' => 'multiple', 'occurrence' => [1, 999]], |
|
29
|
|
|
'tagicms' => ['class' => Tags\Icms::class, 'type' => 'single', 'occurrence' => [0, 1]], |
|
30
|
|
|
'tagicmsuffim' => ['class' => Tags\IcmsUfFim::class, 'type' => 'single', 'occurrence' => [0, 1]], |
|
31
|
|
|
'tagpag' => ['class' => Tags\Pag::class, 'type' => 'multiple', 'occurrence' => [1, 10]], |
|
32
|
|
|
'tagautxml' => ['class' => Tags\AutXML::class, 'type' => 'multiple', 'occurrence' => [0, 10]], |
|
33
|
|
|
'taginfadic' => ['class' => Tags\InfAdic::class, 'type' => 'single', 'occurrence' => [0, 1]], |
|
34
|
|
|
'tagresptec' => ['class' => Tags\RespTec::class, 'type' => 'single', 'occurrence' => [0, 1]], |
|
35
|
|
|
'taginfbpesupl' => ['class' => Tags\InfBPeSupl::class, 'type' => 'single', 'occurrence' => [0, 1]], |
|
36
|
|
|
]; |
|
37
|
|
|
|
|
38
|
|
|
public function __construct() |
|
39
|
|
|
{ |
|
40
|
|
|
parent::__construct(); |
|
41
|
|
|
$this->createEmptyProperties(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Convert all subclasse to DOMNodes and creates entire XML |
|
46
|
|
|
* NOTE: append order is very important |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
|
|
public function parse() |
|
50
|
|
|
{ |
|
51
|
|
|
//valida se algum objeto não foi passado ou se supera o limite estabelecido |
|
52
|
|
|
$this->validateDataObjects(); |
|
53
|
|
|
try { |
|
54
|
|
|
//infBPe tag OBRIGATÓRIA mas pode ser construida a partir de outras |
|
55
|
|
|
if (!empty($this->ide) && !empty($this->emit)) { |
|
56
|
|
|
//construir a chave e montar a tag |
|
57
|
|
|
$data = \DateTime::createFromFormat('Y-m-d\TH:i:sP', $this->ide->std->dhemi); |
|
|
|
|
|
|
58
|
|
|
$buildId = Keys::build( |
|
59
|
|
|
$this->ide->std->cuf, |
|
60
|
|
|
$data->format('y'), |
|
61
|
|
|
$data->format('m'), |
|
62
|
|
|
substr($this->emit->std->cnpj, 0, 14), |
|
|
|
|
|
|
63
|
|
|
'63', |
|
64
|
|
|
intval($this->ide->std->serie), |
|
65
|
|
|
intval($this->ide->std->nbp), |
|
66
|
|
|
intval($this->ide->std->tpemis), |
|
67
|
|
|
intval($this->ide->std->cbp) |
|
68
|
|
|
); |
|
69
|
|
|
if (empty($this->infbpe)) { |
|
70
|
|
|
$this->ide->std->cdv = substr($buildId, -1); |
|
71
|
|
|
$std = (object) ['Id' => $buildId, 'versao' => $this->versao]; |
|
72
|
|
|
$class = $this->available["taginfbpe"]['class']; |
|
73
|
|
|
$this->createProperty('infbpe', $this->loadTagClass($class, [$std])); |
|
74
|
|
|
$this->chave = $buildId; |
|
75
|
|
|
} else { |
|
76
|
|
|
if (!empty($this->infbpe->std->id)) { |
|
77
|
|
|
if ($buildId != $this->infbpe->std->id) { |
|
|
|
|
|
|
78
|
|
|
$this->errors[] = "Chave passada na tagInfBPe está incorreta! Chave correta [$buildId]."; |
|
79
|
|
|
} |
|
80
|
|
|
$this->chave = $buildId; |
|
81
|
|
|
} else { |
|
82
|
|
|
$this->infbpe->std->id = $buildId; |
|
83
|
|
|
$this->infbpe->std->versao = !empty($this->infbpe->std->versao) |
|
84
|
|
|
? $this->infbpe->std->versao |
|
85
|
|
|
: $this->versao; |
|
86
|
|
|
$this->ide->std->cdv = substr($buildId, -1); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
//cria o hashCSRT geralmente é necessário quando não é criada a chave |
|
91
|
|
|
//durante a montagem com a classe make |
|
92
|
|
|
if (!empty($this->resptec && $this->resptec->stdsubnode->csrt != null)) { |
|
93
|
|
|
$this->resptec->std->hashcsrt = $this->hashCSRT( |
|
|
|
|
|
|
94
|
|
|
$this->resptec->stdsubnode->csrt, |
|
95
|
|
|
$buildId |
|
|
|
|
|
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
//caso não seja criada a tag infBPe |
|
101
|
|
|
if (empty($this->infbpe)) { |
|
102
|
|
|
throw new \Exception('A Tag infBPE é obrigatória.'); |
|
103
|
|
|
} |
|
104
|
|
|
//cria o node infBPe |
|
105
|
|
|
$infBPe = $this->infbpe->toNode(); |
|
106
|
|
|
//cria Ide |
|
107
|
|
|
$this->appendNodeToParent($infBPe, $this->ide); |
|
108
|
|
|
//se existir a tag TTAR o endereço deve ser colocado antes dessa tag |
|
109
|
|
|
if (!empty($this->emit->std->tar)) { |
|
110
|
|
|
$node = $this->emit->toNode(); |
|
111
|
|
|
$subnode = $this->enderemit->toNode(); |
|
|
|
|
|
|
112
|
|
|
$this->dom->appChildBefore($node, $subnode, 'TAR'); |
|
113
|
|
|
$this->dom->appChild($infBPe, $node); |
|
114
|
|
|
} else { |
|
115
|
|
|
//cria emit e enderemit |
|
116
|
|
|
$this->appendNodeToParent($infBPe, $this->emit, $this->enderemit); |
|
117
|
|
|
} |
|
118
|
|
|
//cria comp |
|
119
|
|
|
$this->appendNodeToParent($infBPe, $this->comp, $this->endercomp); |
|
|
|
|
|
|
120
|
|
|
//cria agencia |
|
121
|
|
|
$this->appendNodeToParent($infBPe, $this->agencia, $this->enderagencia); |
|
|
|
|
|
|
122
|
|
|
//cria infBPeSub apenas se for um BPe de substituição |
|
123
|
|
|
if ($this->ide->std->tpbpe == 3) { |
|
124
|
|
|
$this->appendNodeToParent($infBPe, $this->infbpesub); |
|
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
//cria infPassagem |
|
127
|
|
|
$this->appendNodeToParent($infBPe, $this->infpassagem, $this->infpassageiro); |
|
|
|
|
|
|
128
|
|
|
//cria infViagem |
|
129
|
|
|
$this->appendNodeToParent($infBPe, $this->infviagem); |
|
|
|
|
|
|
130
|
|
|
//cria infValorBPe |
|
131
|
|
|
$this->appendNodeToParent($infBPe, $this->infvalorbpe, $this->compvalor); |
|
|
|
|
|
|
132
|
|
|
//cria imp |
|
133
|
|
|
$imp = $this->dom->createElement('imp'); |
|
134
|
|
|
//cria imp/ICMS |
|
135
|
|
|
$this->appendNodeToParent($imp, $this->icms); |
|
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
$this->dom->addChild( |
|
138
|
|
|
$imp, |
|
139
|
|
|
'vTotTrib', |
|
140
|
|
|
$this->icms->std->vtottrib, |
|
141
|
|
|
false, |
|
142
|
|
|
'' |
|
143
|
|
|
); |
|
144
|
|
|
$this->dom->addChild( |
|
145
|
|
|
$imp, |
|
146
|
|
|
'infAdFisco', |
|
147
|
|
|
$this->icms->std->infadfisco, |
|
148
|
|
|
false, |
|
149
|
|
|
'' |
|
150
|
|
|
); |
|
151
|
|
|
//cria imp/ICMSUFFIM |
|
152
|
|
|
$this->appendNodeToParent($imp, $this->icmsuffim); |
|
|
|
|
|
|
153
|
|
|
$this->dom->appChild($infBPe, $imp); |
|
154
|
|
|
//cria pag |
|
155
|
|
|
$this->appendNodeToParent($infBPe, $this->pag); |
|
|
|
|
|
|
156
|
|
|
//cria autXML |
|
157
|
|
|
$this->appendNodeToParent($infBPe, $this->autxml); |
|
|
|
|
|
|
158
|
|
|
//cria infAdic |
|
159
|
|
|
$this->appendNodeToParent($infBPe, $this->infadic); |
|
|
|
|
|
|
160
|
|
|
//cria infRespTec |
|
161
|
|
|
$this->appendNodeToParent($infBPe, $this->resptec); |
|
162
|
|
|
$this->dom->appChild($this->BPe, $infBPe); |
|
|
|
|
|
|
163
|
|
|
//cria infBPeSupl |
|
164
|
|
|
$this->appendNodeToParent($this->BPe, $this->infbpesupl); |
|
|
|
|
|
|
165
|
|
|
$this->dom->appendChild($this->BPe); |
|
166
|
|
|
$this->xml = null; |
|
167
|
|
|
$this->xml = $this->dom->saveXML(); |
|
168
|
|
|
return $this->xml; |
|
169
|
|
|
} catch (\ErrorException $e) { |
|
|
|
|
|
|
170
|
|
|
throw new \Exception($e); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Append tags in parent with subtags |
|
176
|
|
|
* @param \DOMElement $parent |
|
177
|
|
|
* @param \NFePHP\BPe\Factories\TagInterface $nodes | null |
|
178
|
|
|
* @param \NFePHP\BPe\Factories\TagInterface $subnode | null |
|
179
|
|
|
* @return void |
|
180
|
|
|
* @throws \Exception |
|
181
|
|
|
*/ |
|
182
|
|
|
protected function appendNodeToParent($parent, $nodes, $subnode = null) |
|
183
|
|
|
{ |
|
184
|
|
|
if (empty($nodes)) { |
|
185
|
|
|
return; |
|
186
|
|
|
} |
|
187
|
|
|
if (is_array($nodes)) { |
|
188
|
|
|
foreach ($nodes as $node) { |
|
189
|
|
|
$this->dom->appChild($parent, $node->toNode()); |
|
190
|
|
|
} |
|
191
|
|
|
} elseif (is_object($nodes)) { |
|
192
|
|
|
if ($subnode != null) { |
|
193
|
|
|
$novonode = $nodes->toNode(); |
|
194
|
|
|
if (is_array($subnode)) { |
|
195
|
|
|
foreach ($subnode as $sub) { |
|
196
|
|
|
$this->dom->appChild($novonode, $sub->toNode()); |
|
197
|
|
|
} |
|
198
|
|
|
} elseif (is_object($subnode)) { |
|
199
|
|
|
$this->dom->appChild($novonode, $subnode->toNode()); |
|
200
|
|
|
} |
|
201
|
|
|
$this->dom->appChild($parent, $novonode); |
|
202
|
|
|
} else { |
|
203
|
|
|
$this->dom->appChild($parent, $nodes->toNode()); |
|
204
|
|
|
} |
|
205
|
|
|
} else { |
|
206
|
|
|
throw new \Exception('Algo errado no código.'); |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: