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\Service; |
26
|
|
|
|
27
|
|
|
use Closure; |
28
|
|
|
use Derafu\Lib\Core\Helper\Rut; |
29
|
|
|
use Derafu\Lib\Core\Package\Prime\Component\Entity\Contract\EntityComponentInterface; |
30
|
|
|
use Derafu\Lib\Core\Package\Prime\Component\Template\Contract\DataHandlerInterface; |
31
|
|
|
use Derafu\Lib\Core\Package\Prime\Component\Template\Service\DataHandler; |
32
|
|
|
use Derafu\Lib\Core\Support\Store\Contract\RepositoryInterface; |
33
|
|
|
use libredte\lib\Core\Package\Billing\Component\Document\Contract\TipoDocumentoInterface; |
34
|
|
|
use libredte\lib\Core\Package\Billing\Component\Document\Entity\AduanaPais; |
35
|
|
|
use libredte\lib\Core\Package\Billing\Component\Document\Entity\AduanaTransporte; |
36
|
|
|
use libredte\lib\Core\Package\Billing\Component\Document\Entity\Comuna; |
37
|
|
|
use libredte\lib\Core\Package\Billing\Component\Document\Entity\FormaPago; |
38
|
|
|
use libredte\lib\Core\Package\Billing\Component\Document\Entity\Traslado; |
39
|
|
|
use libredte\lib\Core\Package\Billing\Component\Document\Exception\RendererException; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Servicio para traducir los datos de los documentos a su representación para |
43
|
|
|
* ser utilizada en la renderización del documento. |
44
|
|
|
*/ |
45
|
|
|
class TemplateDataHandler implements DataHandlerInterface |
46
|
|
|
{ |
47
|
|
|
/** |
48
|
|
|
* Mapa de handlers. |
49
|
|
|
* |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
private array $handlers; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Handler por defecto para manejar los casos. |
56
|
|
|
* |
57
|
|
|
* @var DataHandlerInterface |
58
|
|
|
*/ |
59
|
|
|
private DataHandlerInterface $handler; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Constructor del handler. |
63
|
|
|
* |
64
|
|
|
* @param EntityComponentInterface $entityComponent |
65
|
|
|
* @param DataHandlerInterface|null $handler |
66
|
|
|
*/ |
67
|
54 |
|
public function __construct( |
68
|
|
|
private EntityComponentInterface $entityComponent, |
69
|
|
|
DataHandlerInterface $handler = null |
70
|
|
|
) { |
71
|
54 |
|
$this->handler = $handler ?? new DataHandler(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @inheritDoc |
76
|
|
|
*/ |
77
|
54 |
|
public function handle(string $id, mixed $data): string |
78
|
|
|
{ |
79
|
|
|
// Si no hay valor asignado en los datos se entrega un string vacio. |
80
|
54 |
|
if (!$data) { |
81
|
18 |
|
return ''; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// Buscar el handler. |
85
|
54 |
|
$handler = $this->getHandler($id); |
86
|
|
|
|
87
|
|
|
// Ejecutar el handler sobre los datos para formatearlos. |
88
|
54 |
|
assert($this->handler instanceof DataHandler); |
89
|
54 |
|
return $this->handler->handle($id, $data, $handler); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Obtiene el handler de un campo a partir de su ID. |
94
|
|
|
* |
95
|
|
|
* @param string $id |
96
|
|
|
* @return Closure|RepositoryInterface |
97
|
|
|
*/ |
98
|
54 |
|
private function getHandler(string $id): Closure|RepositoryInterface |
99
|
|
|
{ |
100
|
54 |
|
if (!isset($this->handlers)) { |
101
|
54 |
|
$this->handlers = $this->createHandlers(); |
102
|
|
|
} |
103
|
|
|
|
104
|
54 |
|
if (!isset($this->handlers[$id])) { |
105
|
|
|
throw new RendererException(sprintf( |
106
|
|
|
'El formato para %s no está definido. Los disponibles son: %s.', |
107
|
|
|
$id, |
108
|
|
|
implode(', ', array_keys($this->handlers)) |
109
|
|
|
)); |
110
|
|
|
} |
111
|
|
|
|
112
|
54 |
|
if (is_string($this->handlers[$id]) && str_starts_with($this->handlers[$id], 'alias:')) { |
113
|
50 |
|
[$alias, $handler] = explode(':', $this->handlers[$id], 2); |
114
|
|
|
|
115
|
50 |
|
if (!isset($this->handlers[$handler])) { |
116
|
|
|
throw new RendererException(sprintf( |
117
|
|
|
'El alias %s del formato para %s no está definido. Los disponibles son: %s.', |
118
|
|
|
$handler, |
119
|
|
|
$id, |
120
|
|
|
implode(', ', array_keys($this->handlers)) |
121
|
|
|
)); |
122
|
|
|
} |
123
|
|
|
|
124
|
50 |
|
return $this->handlers[$handler]; |
125
|
|
|
} |
126
|
|
|
|
127
|
54 |
|
return $this->handlers[$id]; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Mapa de campos a handlers para los documentos tributarios electrónicos. |
132
|
|
|
* |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
54 |
|
private function createHandlers(): array |
136
|
|
|
{ |
137
|
54 |
|
return [ |
138
|
54 |
|
'RUTEmisor' => fn (string $rut) => Rut::formatFull($rut), |
139
|
54 |
|
'RUTRecep' => 'alias:RUTEmisor', |
140
|
54 |
|
'RUTTrans' => 'alias:RUTEmisor', |
141
|
54 |
|
'RUTChofer' => 'alias:RUTEmisor', |
142
|
54 |
|
'TipoDTE' => $this->entityComponent->getRepository( |
143
|
54 |
|
TipoDocumentoInterface::class |
144
|
54 |
|
), |
145
|
54 |
|
'TpoDocRef' => 'alias:TipoDTE', |
146
|
54 |
|
'CdgSIISucur' => fn (string $comuna) => |
147
|
53 |
|
$this->entityComponent->getRepository( |
148
|
53 |
|
Comuna::class |
149
|
53 |
|
)->find($comuna)->getDireccionRegional() |
150
|
54 |
|
, |
151
|
54 |
|
'FchEmis' => function (string $fecha) { |
152
|
54 |
|
$timestamp = strtotime($fecha); |
153
|
54 |
|
return date('d/m/Y', $timestamp); // TODO: Formato largo. |
154
|
54 |
|
}, |
155
|
54 |
|
'FchRef' => 'alias:FchEmis', |
156
|
54 |
|
'FchVenc' => 'alias:FchEmis', |
157
|
54 |
|
'PeriodoDesde' => function (string $fecha) { |
158
|
1 |
|
$timestamp = strtotime($fecha); |
159
|
1 |
|
return date('d/m/Y', $timestamp); |
160
|
54 |
|
}, |
161
|
54 |
|
'PeriodoHasta' => 'alias:PeriodoDesde', |
162
|
54 |
|
'FchResol' => 'alias:PeriodoDesde', |
163
|
54 |
|
'FmaPago' => $this->entityComponent->getRepository( |
164
|
54 |
|
FormaPago::class |
165
|
54 |
|
), |
166
|
54 |
|
'Nacionalidad' => $this->entityComponent->getRepository( |
167
|
54 |
|
AduanaPais::class |
168
|
54 |
|
), |
169
|
54 |
|
'CodPaisRecep' => 'alias:Nacionalidad', |
170
|
54 |
|
'IndTraslado' => $this->entityComponent->getRepository( |
171
|
54 |
|
Traslado::class |
172
|
54 |
|
), |
173
|
54 |
|
'CodViaTransp' => $this->entityComponent->getRepository( |
174
|
54 |
|
AduanaTransporte::class |
175
|
54 |
|
), |
176
|
54 |
|
]; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.