1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* LibreDTE: Biblioteca PHP (Núcleo). |
7
|
|
|
* Copyright (C) LibreDTE <https://www.libredte.cl> |
8
|
|
|
* |
9
|
|
|
* Este programa es software libre: usted puede redistribuirlo y/o modificarlo |
10
|
|
|
* bajo los términos de la Licencia Pública General Affero de GNU publicada por |
11
|
|
|
* la Fundación para el Software Libre, ya sea la versión 3 de la Licencia, o |
12
|
|
|
* (a su elección) cualquier versión posterior de la misma. |
13
|
|
|
* |
14
|
|
|
* Este programa se distribuye con la esperanza de que sea útil, pero SIN |
15
|
|
|
* GARANTÍA ALGUNA; ni siquiera la garantía implícita MERCANTIL o de APTITUD |
16
|
|
|
* PARA UN PROPÓSITO DETERMINADO. Consulte los detalles de la Licencia Pública |
17
|
|
|
* General Affero de GNU para obtener una información más detallada. |
18
|
|
|
* |
19
|
|
|
* Debería haber recibido una copia de la Licencia Pública General Affero de |
20
|
|
|
* GNU junto a este programa. |
21
|
|
|
* |
22
|
|
|
* En caso contrario, consulte <http://www.gnu.org/licenses/agpl.html>. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace libredte\lib\Core\Package\Billing\Component\Document\Support; |
26
|
|
|
|
27
|
|
|
use Derafu\Certificate\Contract\CertificateInterface; |
28
|
|
|
use Derafu\Config\Contract\OptionsInterface; |
29
|
|
|
use Derafu\Config\Trait\OptionsAwareTrait; |
30
|
|
|
use Derafu\Xml\Contract\XmlDocumentInterface; |
31
|
|
|
use libredte\lib\Core\Package\Billing\Component\Document\Contract\DocumentBagInterface; |
32
|
|
|
use libredte\lib\Core\Package\Billing\Component\Document\Contract\DocumentInterface; |
33
|
|
|
use libredte\lib\Core\Package\Billing\Component\Document\Contract\TipoDocumentoInterface; |
34
|
|
|
use libredte\lib\Core\Package\Billing\Component\Document\Exception\DocumentException; |
35
|
|
|
use libredte\lib\Core\Package\Billing\Component\Identifier\Contract\CafInterface; |
36
|
|
|
use libredte\lib\Core\Package\Billing\Component\TradingParties\Contract\EmisorInterface; |
37
|
|
|
use libredte\lib\Core\Package\Billing\Component\TradingParties\Contract\ReceptorInterface; |
38
|
|
|
use LogicException; |
39
|
|
|
use stdClass; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Contenedor de datos del documento tributario electrónico. |
43
|
|
|
* |
44
|
|
|
* Permite "mover" un documento, junto a otros datos asociados, por métodos de |
45
|
|
|
* manera sencilla y, sobre todo, extensible. |
46
|
|
|
*/ |
47
|
|
|
class DocumentBag implements DocumentBagInterface |
48
|
|
|
{ |
49
|
|
|
use OptionsAwareTrait; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Reglas de esquema de las opciones del documento. |
53
|
|
|
* |
54
|
|
|
* Acá solo se indicarán los índices que deben pueden existir en las |
55
|
|
|
* opciones. No se define el esquema de cada opción pues cada clase que |
56
|
|
|
* utilice estas opciones deberá resolver y validar sus propias opciones. |
57
|
|
|
* |
58
|
|
|
* @var array |
59
|
|
|
*/ |
60
|
|
|
protected array $optionsSchema = [ |
61
|
|
|
'builder' => [ |
62
|
|
|
'types' => 'array', |
63
|
|
|
'default' => [], |
64
|
|
|
], |
65
|
|
|
'normalizer' => [ |
66
|
|
|
'types' => 'array', |
67
|
|
|
'default' => [], |
68
|
|
|
], |
69
|
|
|
'parser' => [ |
70
|
|
|
'types' => 'array', |
71
|
|
|
'default' => [], |
72
|
|
|
], |
73
|
|
|
'renderer' => [ |
74
|
|
|
'types' => 'array', |
75
|
|
|
'default' => [], |
76
|
|
|
], |
77
|
|
|
'sanitizer' => [ |
78
|
|
|
'types' => 'array', |
79
|
|
|
'default' => [], |
80
|
|
|
], |
81
|
|
|
'validator' => [ |
82
|
|
|
'types' => 'array', |
83
|
|
|
'default' => [], |
84
|
|
|
], |
85
|
|
|
]; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Datos originales de entrada que se utilizarán para construir el |
89
|
|
|
* documento tributario. |
90
|
|
|
* |
91
|
|
|
* El formato de estos datos puede ser cualquiera soportado por los parsers. |
92
|
|
|
* |
93
|
|
|
* @var string|null |
94
|
|
|
*/ |
95
|
|
|
private ?string $inputData; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Datos de entrada procesados (parseados). |
99
|
|
|
* |
100
|
|
|
* Están en el formato estándar de LibreDTE. Que es básicamente el oficial |
101
|
|
|
* del SII. |
102
|
|
|
* |
103
|
|
|
* Estos son los datos que se usarán para construir el documento. Estos |
104
|
|
|
* datos no están normaliados, solo parseados. |
105
|
|
|
* |
106
|
|
|
* @var array|null |
107
|
|
|
*/ |
108
|
|
|
private ?array $parsedData; |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Datos normalizados del documento tributario. |
112
|
|
|
* |
113
|
|
|
* Son los datos con todos sus campos necesarios ya determinados, calculados |
114
|
|
|
* y validados. |
115
|
|
|
* |
116
|
|
|
* La estructura de estos datos depende de los normalizadores. |
117
|
|
|
* |
118
|
|
|
* Importante: si se desactiva la normalización este arreglo contendrá lo |
119
|
|
|
* mismo que $parsedData pues no se tocarán los datos de entrada procesados. |
120
|
|
|
* |
121
|
|
|
* @var array|null |
122
|
|
|
*/ |
123
|
|
|
private ?array $normalizedData; |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Datos de LibreDTE asociados al documento tributario. |
127
|
|
|
* |
128
|
|
|
* Estos son datos que LibreDTE utiliza asociados al documento pero no son |
129
|
|
|
* parte de la estructura oficial que utiliza el SII. |
130
|
|
|
* |
131
|
|
|
* Por ejemplo se puede incluir: |
132
|
|
|
* |
133
|
|
|
* - Tags de facturas en PDF de boletas. Ejemplo: TermPagoGlosa. |
134
|
|
|
* - Datos adicionales para los PDF. Ejemplo: historial. |
135
|
|
|
* |
136
|
|
|
* @var array|null |
137
|
|
|
*/ |
138
|
|
|
private ?array $libredteData; |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Instancia del documento XML asociada al DTE. |
142
|
|
|
* |
143
|
|
|
* @var XmlDocumentInterface|null |
144
|
|
|
*/ |
145
|
|
|
private ?XmlDocumentInterface $xmlDocument; |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Código de Asignación de Folios (CAF) para timbrar el Documento Tributario |
149
|
|
|
* Electrónico (DTE) que se generará. |
150
|
|
|
* |
151
|
|
|
* @var CafInterface|null |
152
|
|
|
*/ |
153
|
|
|
private ?CafInterface $caf; |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Certificado digital (firma electrónica) para la firma del documento. |
157
|
|
|
* |
158
|
|
|
* @var CertificateInterface|null |
159
|
|
|
*/ |
160
|
|
|
private ?CertificateInterface $certificate; |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Entidad con el documento tributario electrónico generado. |
164
|
|
|
* |
165
|
|
|
* @var DocumentInterface|null |
166
|
|
|
*/ |
167
|
|
|
private ?DocumentInterface $document; |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Entidad que representa al tipo de documento tributario que está contenido |
171
|
|
|
* en esta bolsa. |
172
|
|
|
* |
173
|
|
|
* @var TipoDocumentoInterface|null |
174
|
|
|
*/ |
175
|
|
|
private ?TipoDocumentoInterface $documentType = null; |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Emisor del documento tributario. |
179
|
|
|
* |
180
|
|
|
* @var EmisorInterface|null |
181
|
|
|
*/ |
182
|
|
|
private ?EmisorInterface $emisor = null; |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Receptor del documento tributario. |
186
|
|
|
* |
187
|
|
|
* @var ReceptorInterface|null |
188
|
|
|
*/ |
189
|
|
|
private ?ReceptorInterface $receptor = null; |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Arreglo con la estructura del nodo TED del documento. |
193
|
|
|
* |
194
|
|
|
* @var array|null |
195
|
|
|
*/ |
196
|
|
|
private ?array $timbre = null; |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Arreglo con los datos normalizados consolidados con el timbre y la firma |
200
|
|
|
* si existen en la bolsa. |
201
|
|
|
* |
202
|
|
|
* @var array|null |
203
|
|
|
*/ |
204
|
|
|
private ?array $data = null; |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Constructor del contenedor. |
208
|
|
|
* |
209
|
|
|
* Recibe los datos en diferentes formatos para pasarlos a los setters que |
210
|
|
|
* los normalizan y asignan al contenedor. |
211
|
|
|
* |
212
|
|
|
* @param string|array|stdClass|null $inputData |
213
|
|
|
* @param array|null $parsedData |
214
|
|
|
* @param array|null $normalizedData |
215
|
|
|
* @param array|null $libredteData |
216
|
|
|
* @param array|OptionsInterface $options |
217
|
|
|
* @param XmlDocumentInterface|null $xmlDocument |
218
|
|
|
* @param CafInterface|null $caf |
219
|
|
|
* @param CertificateInterface|null $certificate |
220
|
|
|
* @param DocumentInterface|null $document |
221
|
|
|
* @param TipoDocumentoInterface|null $documentType |
222
|
|
|
* @param EmisorInterface|null $emisor |
223
|
|
|
* @param ReceptorInterface|null $receptor |
224
|
|
|
*/ |
225
|
57 |
|
public function __construct( |
226
|
|
|
string|array|stdClass|null $inputData = null, |
227
|
|
|
array|null $parsedData = null, |
228
|
|
|
array|null $normalizedData = null, |
229
|
|
|
array|null $libredteData = null, |
230
|
|
|
array|OptionsInterface|null $options = null, |
231
|
|
|
XmlDocumentInterface|null $xmlDocument = null, |
232
|
|
|
CafInterface|null $caf = null, |
233
|
|
|
CertificateInterface|null $certificate = null, |
234
|
|
|
DocumentInterface|null $document = null, |
235
|
|
|
TipoDocumentoInterface|null $documentType = null, |
236
|
|
|
EmisorInterface|null $emisor = null, |
237
|
|
|
ReceptorInterface|null $receptor = null |
238
|
|
|
) { |
239
|
57 |
|
$this |
240
|
57 |
|
->setInputData($inputData) |
241
|
57 |
|
->setParsedData($parsedData) |
242
|
57 |
|
->setNormalizedData($normalizedData) |
243
|
57 |
|
->setLibredteData($libredteData) |
244
|
57 |
|
->setOptions($options ?? []) |
245
|
57 |
|
->setXmlDocument($xmlDocument) |
246
|
57 |
|
->setCaf($caf) |
247
|
57 |
|
->setCertificate($certificate) |
248
|
57 |
|
->setDocument($document) |
249
|
57 |
|
->setDocumentType($documentType) |
250
|
57 |
|
->setEmisor($emisor) |
251
|
57 |
|
->setReceptor($receptor) |
252
|
57 |
|
; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* {@inheritDoc} |
257
|
|
|
*/ |
258
|
57 |
|
public function setInputData(string|array|stdClass|null $inputData): static |
259
|
|
|
{ |
260
|
57 |
|
if ($inputData === null) { |
|
|
|
|
261
|
53 |
|
$this->inputData = null; |
262
|
|
|
|
263
|
53 |
|
return $this; |
264
|
|
|
} |
265
|
|
|
|
266
|
57 |
|
if (!is_string($inputData)) { |
|
|
|
|
267
|
4 |
|
$inputData = json_encode($inputData); |
268
|
|
|
} |
269
|
|
|
|
270
|
57 |
|
$this->inputData = $inputData; |
271
|
|
|
|
272
|
57 |
|
return $this; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* {@inheritDoc} |
277
|
|
|
*/ |
278
|
57 |
|
public function getInputData(): ?string |
279
|
|
|
{ |
280
|
57 |
|
return $this->inputData; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* {@inheritDoc} |
285
|
|
|
*/ |
286
|
57 |
|
public function setParsedData(?array $parsedData): static |
287
|
|
|
{ |
288
|
57 |
|
$this->parsedData = $parsedData; |
289
|
|
|
|
290
|
57 |
|
return $this; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* {@inheritDoc} |
295
|
|
|
*/ |
296
|
56 |
|
public function getParsedData(): ?array |
297
|
|
|
{ |
298
|
56 |
|
return $this->parsedData; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* {@inheritDoc} |
303
|
|
|
*/ |
304
|
57 |
|
public function setNormalizedData(?array $normalizedData): static |
305
|
|
|
{ |
306
|
57 |
|
$this->normalizedData = $normalizedData; |
307
|
|
|
|
308
|
57 |
|
return $this; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* {@inheritDoc} |
313
|
|
|
*/ |
314
|
53 |
|
public function getNormalizedData(): ?array |
315
|
|
|
{ |
316
|
53 |
|
return $this->normalizedData; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* {@inheritDoc} |
321
|
|
|
*/ |
322
|
57 |
|
public function setLibredteData(?array $libredteData): static |
323
|
|
|
{ |
324
|
57 |
|
$this->libredteData = $libredteData; |
325
|
|
|
|
326
|
57 |
|
return $this; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* {@inheritDoc} |
331
|
|
|
*/ |
332
|
52 |
|
public function getLibredteData(): ?array |
333
|
|
|
{ |
334
|
52 |
|
return $this->libredteData; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* {@inheritDoc} |
339
|
|
|
*/ |
340
|
57 |
|
public function getParserOptions(): array |
341
|
|
|
{ |
342
|
57 |
|
return $this->getOptions()->get('parser')?->all() ?? []; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* {@inheritDoc} |
347
|
|
|
*/ |
348
|
53 |
|
public function getBuilderOptions(): array |
349
|
|
|
{ |
350
|
53 |
|
return $this->getOptions()->get('builder')?->all() ?? []; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* {@inheritDoc} |
355
|
|
|
*/ |
356
|
53 |
|
public function getNormalizerOptions(): array |
357
|
|
|
{ |
358
|
53 |
|
return $this->getOptions()->get('normalizer')?->all() ?? []; |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* {@inheritDoc} |
363
|
|
|
*/ |
364
|
|
|
public function getSanitizerOptions(): array |
365
|
|
|
{ |
366
|
|
|
return $this->getOptions()->get('sanitizer')?->all() ?? []; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* {@inheritDoc} |
371
|
|
|
*/ |
372
|
|
|
public function getValidatorOptions(): array |
373
|
|
|
{ |
374
|
|
|
return $this->getOptions()->get('validator')?->all() ?? []; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* {@inheritDoc} |
379
|
|
|
*/ |
380
|
53 |
|
public function getRendererOptions(): array |
381
|
|
|
{ |
382
|
53 |
|
return $this->getOptions()->get('renderer')?->all() ?? []; |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* {@inheritDoc} |
387
|
|
|
*/ |
388
|
57 |
|
public function setXmlDocument(?XmlDocumentInterface $xmlDocument): static |
389
|
|
|
{ |
390
|
57 |
|
$this->xmlDocument = $xmlDocument; |
391
|
|
|
|
392
|
57 |
|
return $this; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* {@inheritDoc} |
397
|
|
|
*/ |
398
|
53 |
|
public function getXmlDocument(): ?XmlDocumentInterface |
399
|
|
|
{ |
400
|
53 |
|
return $this->xmlDocument; |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* {@inheritDoc} |
405
|
|
|
*/ |
406
|
57 |
|
public function setCaf(?CafInterface $caf): static |
407
|
|
|
{ |
408
|
57 |
|
$this->caf = $caf; |
409
|
|
|
|
410
|
57 |
|
return $this; |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
/** |
414
|
|
|
* {@inheritDoc} |
415
|
|
|
*/ |
416
|
53 |
|
public function getCaf(): ?CafInterface |
417
|
|
|
{ |
418
|
53 |
|
return $this->caf; |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* {@inheritDoc} |
423
|
|
|
*/ |
424
|
57 |
|
public function setCertificate(?CertificateInterface $certificate): static |
425
|
|
|
{ |
426
|
57 |
|
$this->certificate = $certificate; |
427
|
|
|
|
428
|
57 |
|
return $this; |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* {@inheritDoc} |
433
|
|
|
*/ |
434
|
53 |
|
public function getCertificate(): ?CertificateInterface |
435
|
|
|
{ |
436
|
53 |
|
return $this->certificate; |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** |
440
|
|
|
* {@inheritDoc} |
441
|
|
|
*/ |
442
|
57 |
|
public function setDocument(?DocumentInterface $document): static |
443
|
|
|
{ |
444
|
57 |
|
$this->document = $document; |
445
|
|
|
|
446
|
57 |
|
return $this; |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
/** |
450
|
|
|
* {@inheritDoc} |
451
|
|
|
*/ |
452
|
53 |
|
public function getDocument(): ?DocumentInterface |
453
|
|
|
{ |
454
|
53 |
|
return $this->document; |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
/** |
458
|
|
|
* {@inheritDoc} |
459
|
|
|
*/ |
460
|
57 |
|
public function setDocumentType(?TipoDocumentoInterface $documentType): static |
461
|
|
|
{ |
462
|
57 |
|
$this->documentType = $documentType; |
463
|
|
|
|
464
|
57 |
|
return $this; |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
/** |
468
|
|
|
* {@inheritDoc} |
469
|
|
|
*/ |
470
|
54 |
|
public function setTipoDocumento(?TipoDocumentoInterface $tipoDocumento): static |
471
|
|
|
{ |
472
|
54 |
|
return $this->setDocumentType($tipoDocumento); |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
/** |
476
|
|
|
* {@inheritDoc} |
477
|
|
|
*/ |
478
|
56 |
|
public function getDocumentType(): ?TipoDocumentoInterface |
479
|
|
|
{ |
480
|
56 |
|
return $this->documentType; |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
/** |
484
|
|
|
* {@inheritDoc} |
485
|
|
|
*/ |
486
|
56 |
|
public function getTipoDocumento(): ?TipoDocumentoInterface |
487
|
|
|
{ |
488
|
56 |
|
return $this->getDocumentType(); |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
/** |
492
|
|
|
* {@inheritDoc} |
493
|
|
|
*/ |
494
|
56 |
|
public function getDocumentTypeId(): ?int |
495
|
|
|
{ |
496
|
56 |
|
$TipoDTE = $this->parsedData['Encabezado']['IdDoc']['TipoDTE'] |
497
|
56 |
|
?? $this->normalizedData['Encabezado']['IdDoc']['TipoDTE'] |
498
|
56 |
|
?? $this->xmlDocument?->query('//Encabezado/IdDoc/TipoDTE') |
499
|
56 |
|
?? $this->document?->getCodigo() |
500
|
54 |
|
?? null |
501
|
56 |
|
; |
502
|
|
|
|
503
|
56 |
|
if (!$TipoDTE) { |
504
|
2 |
|
throw new DocumentException( |
505
|
2 |
|
'Falta indicar el tipo de documento (TipoDTE) en los datos del DTE.' |
506
|
2 |
|
); |
507
|
|
|
} |
508
|
|
|
|
509
|
54 |
|
return (int) $TipoDTE; |
510
|
|
|
} |
511
|
|
|
|
512
|
|
|
/** |
513
|
|
|
* {@inheritDoc} |
514
|
|
|
*/ |
515
|
56 |
|
public function getCodigoTipoDocumento(): ?int |
516
|
|
|
{ |
517
|
56 |
|
return $this->getDocumentTypeId(); |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
/** |
521
|
|
|
* {@inheritDoc} |
522
|
|
|
*/ |
523
|
57 |
|
public function setEmisor(?EmisorInterface $emisor): static |
524
|
|
|
{ |
525
|
57 |
|
$this->emisor = $emisor; |
526
|
|
|
|
527
|
57 |
|
return $this; |
528
|
|
|
} |
529
|
|
|
|
530
|
|
|
/** |
531
|
|
|
* {@inheritDoc} |
532
|
|
|
*/ |
533
|
53 |
|
public function getEmisor(): ?EmisorInterface |
534
|
|
|
{ |
535
|
53 |
|
return $this->emisor; |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
/** |
539
|
|
|
* {@inheritDoc} |
540
|
|
|
*/ |
541
|
57 |
|
public function setReceptor(?ReceptorInterface $receptor): static |
542
|
|
|
{ |
543
|
57 |
|
$this->receptor = $receptor; |
544
|
|
|
|
545
|
57 |
|
return $this; |
546
|
|
|
} |
547
|
|
|
|
548
|
|
|
/** |
549
|
|
|
* {@inheritDoc} |
550
|
|
|
*/ |
551
|
53 |
|
public function getReceptor(): ?ReceptorInterface |
552
|
|
|
{ |
553
|
53 |
|
return $this->receptor; |
554
|
|
|
} |
555
|
|
|
|
556
|
|
|
/** |
557
|
|
|
* {@inheritDoc} |
558
|
|
|
*/ |
559
|
53 |
|
public function setTimbre(?array $timbre): static |
560
|
|
|
{ |
561
|
53 |
|
$this->timbre = $timbre; |
562
|
|
|
|
563
|
53 |
|
return $this; |
564
|
|
|
} |
565
|
|
|
|
566
|
|
|
/** |
567
|
|
|
* {@inheritDoc} |
568
|
|
|
*/ |
569
|
53 |
|
public function getTimbre(): ?array |
570
|
|
|
{ |
571
|
53 |
|
return $this->timbre; |
572
|
|
|
} |
573
|
|
|
|
574
|
|
|
/** |
575
|
|
|
* {@inheritDoc} |
576
|
|
|
*/ |
577
|
53 |
|
public function getData(): ?array |
578
|
|
|
{ |
579
|
|
|
// Si los datos ya estaban generados se entregan. |
580
|
53 |
|
if ($this->data !== null) { |
581
|
|
|
return $this->data; |
582
|
|
|
} |
583
|
|
|
|
584
|
|
|
// Si no hay datos normalizados se entrega `null`. |
585
|
53 |
|
if (!$this->getNormalizedData()) { |
586
|
|
|
return null; |
587
|
|
|
} |
588
|
|
|
|
589
|
|
|
// Se arma la estructura del nodo Documento. |
590
|
53 |
|
$tagXml = $this->getTipoDocumento()->getTagXml()->getNombre(); |
591
|
53 |
|
$this->data = [ |
592
|
53 |
|
'DTE' => [ |
593
|
53 |
|
'@attributes' => [ |
594
|
53 |
|
'version' => '1.0', |
595
|
53 |
|
'xmlns' => 'http://www.sii.cl/SiiDte', |
596
|
53 |
|
], |
597
|
53 |
|
$tagXml => array_merge( |
598
|
53 |
|
[ |
599
|
53 |
|
'@attributes' => [ |
600
|
53 |
|
'ID' => $this->getId(), |
601
|
53 |
|
], |
602
|
53 |
|
], |
603
|
53 |
|
$this->getNormalizedData(), |
604
|
53 |
|
(array) $this->getTimbre(), |
605
|
53 |
|
), |
606
|
53 |
|
//'Signature' => '', // Se agrega al firmar (NO INCLUIR ACÁ). |
607
|
53 |
|
], |
608
|
53 |
|
]; |
609
|
|
|
|
610
|
|
|
// Se entrega la estructura con los datos. |
611
|
53 |
|
return $this->data; |
612
|
|
|
} |
613
|
|
|
|
614
|
|
|
/** |
615
|
|
|
* {@inheritDoc} |
616
|
|
|
*/ |
617
|
53 |
|
public function getDocumentData(): ?array |
618
|
|
|
{ |
619
|
53 |
|
if (!isset($this->document)) { |
620
|
|
|
return null; |
621
|
|
|
} |
622
|
|
|
|
623
|
53 |
|
$documentData = $this->document->getDatos(); |
|
|
|
|
624
|
53 |
|
$documentExtra = $this->libredteData['extra']['dte'] ?? null; |
625
|
|
|
|
626
|
53 |
|
if (empty($documentExtra)) { |
627
|
53 |
|
return $documentData; |
628
|
|
|
} |
629
|
|
|
|
630
|
|
|
return array_replace_recursive($documentData, $documentExtra); |
631
|
|
|
} |
632
|
|
|
|
633
|
|
|
/** |
634
|
|
|
* {@inheritDoc} |
635
|
|
|
*/ |
636
|
53 |
|
public function getDocumentExtra(): ?array |
637
|
|
|
{ |
638
|
53 |
|
if (!isset($this->document)) { |
639
|
|
|
return null; |
640
|
|
|
} |
641
|
|
|
|
642
|
53 |
|
$extra = $this->libredteData['extra'] ?? null; |
643
|
|
|
|
644
|
53 |
|
if (empty($extra)) { |
645
|
53 |
|
return null; |
646
|
|
|
} |
647
|
|
|
|
648
|
|
|
unset($extra['dte']); |
649
|
|
|
|
650
|
|
|
return $extra; |
651
|
|
|
} |
652
|
|
|
|
653
|
|
|
/** |
654
|
|
|
* {@inheritDoc} |
655
|
|
|
*/ |
656
|
53 |
|
public function getDocumentStamp(): ?string |
657
|
|
|
{ |
658
|
53 |
|
return $this->document?->getTED(); |
659
|
|
|
} |
660
|
|
|
|
661
|
|
|
/** |
662
|
|
|
* {@inheritDoc} |
663
|
|
|
*/ |
664
|
53 |
|
public function getDocumentAuth(): ?array |
665
|
|
|
{ |
666
|
53 |
|
return $this->emisor?->getAutorizacionDte()?->toArray(); |
667
|
|
|
} |
668
|
|
|
|
669
|
|
|
/** |
670
|
|
|
* {@inheritDoc} |
671
|
|
|
*/ |
672
|
53 |
|
public function getId(): string |
673
|
|
|
{ |
674
|
53 |
|
$folio = $this->getFolio(); |
675
|
|
|
|
676
|
53 |
|
if (is_int($folio)) { |
677
|
53 |
|
return sprintf( |
678
|
53 |
|
'LibreDTE_%s_T%03dF%09d', |
679
|
53 |
|
$this->getNormalizedData()['Encabezado']['Emisor']['RUTEmisor'], |
680
|
53 |
|
$this->getNormalizedData()['Encabezado']['IdDoc']['TipoDTE'], |
681
|
53 |
|
$folio |
682
|
53 |
|
); |
683
|
|
|
} else { |
684
|
48 |
|
return sprintf( |
685
|
48 |
|
'LibreDTE_%s_%03d-%s', |
686
|
48 |
|
$this->getNormalizedData()['Encabezado']['Emisor']['RUTEmisor'], |
687
|
48 |
|
$this->getNormalizedData()['Encabezado']['IdDoc']['TipoDTE'], |
688
|
48 |
|
$folio |
689
|
48 |
|
); |
690
|
|
|
} |
691
|
|
|
} |
692
|
|
|
|
693
|
|
|
/** |
694
|
|
|
* {@inheritDoc} |
695
|
|
|
*/ |
696
|
52 |
|
public function setFolio(int $folio): static |
697
|
|
|
{ |
698
|
52 |
|
if ($this->getXmlDocument()) { |
699
|
|
|
throw new LogicException( |
700
|
|
|
'No es posible asignar el folio si ya se generó el documento XML.' |
701
|
|
|
); |
702
|
|
|
} |
703
|
|
|
|
704
|
52 |
|
$parsedData = $this->getParsedData(); |
705
|
52 |
|
$normalizedData = $this->getNormalizedData(); |
706
|
|
|
|
707
|
52 |
|
if ($parsedData === null && $normalizedData === null) { |
708
|
|
|
throw new LogicException( |
709
|
|
|
'No es posible asignar el folio si no existen datos parseados o normalizados.' |
710
|
|
|
); |
711
|
|
|
} |
712
|
|
|
|
713
|
52 |
|
if ($parsedData !== null) { |
714
|
52 |
|
$parsedData['Encabezado']['IdDoc']['Folio'] = $folio; |
715
|
52 |
|
$this->setParsedData($parsedData); |
716
|
|
|
} |
717
|
|
|
|
718
|
52 |
|
if ($normalizedData !== null) { |
719
|
52 |
|
$normalizedData['Encabezado']['IdDoc']['Folio'] = $folio; |
720
|
52 |
|
$this->setNormalizedData($normalizedData); |
721
|
|
|
} |
722
|
|
|
|
723
|
52 |
|
return $this; |
724
|
|
|
} |
725
|
|
|
|
726
|
|
|
/** |
727
|
|
|
* {@inheritDoc} |
728
|
|
|
*/ |
729
|
53 |
|
public function getFolio(): int|string|null |
730
|
|
|
{ |
731
|
53 |
|
$data = $this->getNormalizedData() ?? $this->getParsedData(); |
732
|
|
|
|
733
|
53 |
|
$folio = $data['Encabezado']['IdDoc']['Folio']; |
734
|
|
|
|
735
|
53 |
|
if (!$folio) { |
736
|
48 |
|
return null; |
737
|
|
|
} |
738
|
|
|
|
739
|
53 |
|
return is_numeric($folio) ? (int) $folio : (string) $folio; |
740
|
|
|
} |
741
|
|
|
|
742
|
|
|
/** |
743
|
|
|
* {@inheritDoc} |
744
|
|
|
*/ |
745
|
52 |
|
public function withCaf(CafInterface $caf): DocumentBagInterface |
746
|
|
|
{ |
747
|
52 |
|
$class = static::class; |
748
|
|
|
|
749
|
52 |
|
return new $class( |
750
|
52 |
|
inputData: $this->getInputData(), |
751
|
52 |
|
parsedData: $this->getParsedData(), |
752
|
52 |
|
normalizedData: $this->getNormalizedData(), |
753
|
52 |
|
libredteData: $this->getLibredteData(), |
754
|
52 |
|
options: $this->getOptions(), |
755
|
52 |
|
caf: $caf, |
756
|
52 |
|
certificate: $this->getCertificate(), |
757
|
52 |
|
documentType: $this->getDocumentType(), |
758
|
52 |
|
emisor: $this->getEmisor(), |
759
|
52 |
|
receptor: $this->getReceptor() |
760
|
52 |
|
); |
761
|
|
|
} |
762
|
|
|
|
763
|
|
|
/** |
764
|
|
|
* {@inheritDoc} |
765
|
|
|
*/ |
766
|
52 |
|
public function withCertificate( |
767
|
|
|
CertificateInterface $certificate |
768
|
|
|
): DocumentBagInterface { |
769
|
52 |
|
$class = static::class; |
770
|
|
|
|
771
|
52 |
|
return new $class( |
772
|
52 |
|
inputData: $this->getInputData(), |
773
|
52 |
|
parsedData: $this->getParsedData(), |
774
|
52 |
|
normalizedData: $this->getNormalizedData(), |
775
|
52 |
|
libredteData: $this->getLibredteData(), |
776
|
52 |
|
options: $this->getOptions(), |
777
|
52 |
|
caf: $this->getCaf(), |
778
|
52 |
|
certificate: $certificate, |
779
|
52 |
|
documentType: $this->getDocumentType(), |
780
|
52 |
|
emisor: $this->getEmisor(), |
781
|
52 |
|
receptor: $this->getReceptor() |
782
|
52 |
|
); |
783
|
|
|
} |
784
|
|
|
|
785
|
|
|
/** |
786
|
|
|
* {@inheritDoc} |
787
|
|
|
*/ |
788
|
54 |
|
public function getAlias(): string |
789
|
|
|
{ |
790
|
54 |
|
return $this->getTipoDocumento()?->getAlias() |
791
|
54 |
|
?? ( |
792
|
54 |
|
$this->getTipoDocumento()?->getCodigo() |
793
|
1 |
|
? 'documento_' . $this->getTipoDocumento()->getCodigo() |
794
|
1 |
|
: null |
795
|
54 |
|
) |
796
|
54 |
|
?? $this->getParsedData()['Encabezado']['IdDoc']['TipoDTE'] |
797
|
54 |
|
?? 'documento_desconocido' |
798
|
54 |
|
; |
799
|
|
|
} |
800
|
|
|
} |
801
|
|
|
|