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