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 Derafu\Lib\Core\Enum\Currency; |
28
|
|
|
use Derafu\Lib\Core\Helper\Date; |
29
|
|
|
use Derafu\Lib\Core\Helper\Rut; |
30
|
|
|
use Derafu\Lib\Core\Package\Prime\Component\Entity\Contract\EntityComponentInterface; |
31
|
|
|
use Derafu\Lib\Core\Package\Prime\Component\Template\Abstract\AbstractTemplateDataHandler; |
32
|
|
|
use Derafu\Lib\Core\Package\Prime\Component\Template\Contract\DataHandlerInterface; |
33
|
|
|
use libredte\lib\Core\Package\Billing\Component\Document\Contract\TipoDocumentoInterface; |
34
|
|
|
use libredte\lib\Core\Package\Billing\Component\Document\Entity\AduanaMoneda; |
35
|
|
|
use libredte\lib\Core\Package\Billing\Component\Document\Entity\AduanaPais; |
36
|
|
|
use libredte\lib\Core\Package\Billing\Component\Document\Entity\AduanaTransporte; |
37
|
|
|
use libredte\lib\Core\Package\Billing\Component\Document\Entity\Comuna; |
38
|
|
|
use libredte\lib\Core\Package\Billing\Component\Document\Entity\FormaPago; |
39
|
|
|
use libredte\lib\Core\Package\Billing\Component\Document\Entity\ImpuestoAdicionalRetencion; |
40
|
|
|
use libredte\lib\Core\Package\Billing\Component\Document\Entity\MedioPago; |
41
|
|
|
use libredte\lib\Core\Package\Billing\Component\Document\Entity\TagXml; |
42
|
|
|
use libredte\lib\Core\Package\Billing\Component\Document\Entity\Traslado; |
43
|
|
|
use TCPDF2DBarcode; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Servicio para traducir los datos de los documentos a su representación para |
47
|
|
|
* ser utilizada en la renderización del documento. |
48
|
|
|
*/ |
49
|
|
|
class TemplateDataHandler extends AbstractTemplateDataHandler implements DataHandlerInterface |
50
|
|
|
{ |
51
|
|
|
/** |
52
|
|
|
* Constructor del handler. |
53
|
|
|
* |
54
|
|
|
* @param EntityComponentInterface $entityComponent |
55
|
|
|
*/ |
56
|
54 |
|
public function __construct( |
57
|
|
|
private EntityComponentInterface $entityComponent |
58
|
|
|
) { |
59
|
54 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Mapa de campos a handlers para los documentos tributarios electrónicos. |
63
|
|
|
* |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
54 |
|
protected function createHandlers(): array |
67
|
|
|
{ |
68
|
54 |
|
return [ |
69
|
|
|
// Tipos de documento. |
70
|
54 |
|
'TipoDTE' => $this->entityComponent->getRepository( |
71
|
54 |
|
TipoDocumentoInterface::class |
72
|
54 |
|
), |
73
|
54 |
|
'TpoDocRef' => 'alias:TipoDTE', |
74
|
|
|
// RUT. |
75
|
54 |
|
'RUTEmisor' => fn (string $rut) => Rut::formatFull($rut), |
76
|
54 |
|
'RUTRecep' => 'alias:RUTEmisor', |
77
|
54 |
|
'RUTSolicita' => 'alias:RUTEmisor', |
78
|
54 |
|
'RUTTrans' => 'alias:RUTEmisor', |
79
|
54 |
|
'RUTChofer' => 'alias:RUTEmisor', |
80
|
|
|
// Comuna. |
81
|
54 |
|
'CdgSIISucur' => fn (string $comuna) => |
82
|
52 |
|
$this->entityComponent->getRepository( |
83
|
52 |
|
Comuna::class |
84
|
52 |
|
)->find($comuna)->getDireccionRegional() |
85
|
54 |
|
, |
86
|
54 |
|
'CiudadOrigen' => fn (string $comuna) => |
87
|
|
|
$this->entityComponent->getRepository( |
88
|
|
|
Comuna::class |
89
|
|
|
)->find($comuna)->getCiudad() |
90
|
54 |
|
, |
91
|
54 |
|
'CiudadRecep' => 'alias:CiudadOrigen', |
92
|
|
|
// Fechas largas. |
93
|
54 |
|
'FchEmis' => fn (string $fecha) => Date::formatSpanish($fecha), |
94
|
54 |
|
'FchRef' => 'alias:FchEmis', |
95
|
54 |
|
'FchVenc' => 'alias:FchEmis', |
96
|
|
|
// Fechas cortas. |
97
|
54 |
|
'PeriodoDesde' => function (string $fecha) { |
98
|
2 |
|
$timestamp = strtotime($fecha); |
99
|
2 |
|
return date('d/m/Y', $timestamp); |
100
|
54 |
|
}, |
101
|
54 |
|
'PeriodoHasta' => 'alias:PeriodoDesde', |
102
|
54 |
|
'FchPago' => 'alias:PeriodoDesde', |
103
|
|
|
// Solo año de una fecha. |
104
|
54 |
|
'FchResol' => fn (string $fecha) => explode('-', $fecha, 2)[0], |
105
|
|
|
// Datos de Aduana. |
106
|
54 |
|
'Aduana' => function (string $tagXmlAndValue) { |
107
|
|
|
[$tagXml, $value] = explode(':', $tagXmlAndValue); |
108
|
|
|
$xmlTagEntity = $this->entityComponent->getRepository( |
109
|
|
|
TagXml::class |
110
|
|
|
)->find($tagXml); |
111
|
|
|
$name = $xmlTagEntity->getGlosa(); |
112
|
|
|
$entityClass = $xmlTagEntity->getEntity(); |
113
|
|
|
if ($entityClass) { |
114
|
|
|
$description = $this->entityComponent->getRepository( |
115
|
|
|
$entityClass |
116
|
|
|
)->find($value)->getGlosa(); |
117
|
|
|
} else { |
118
|
|
|
$description = $this->handle($tagXml, $value); |
119
|
|
|
} |
120
|
|
|
if ($name && !in_array($description, [false, null, ''], true)) { |
121
|
|
|
return $name . ': ' . $description; |
122
|
|
|
} |
123
|
|
|
return ''; |
124
|
54 |
|
}, |
125
|
54 |
|
'TotItems' => 'alias:Number', |
126
|
|
|
// Otros datos que se mapean de un código a su glosa usando un |
127
|
|
|
// repositorio. |
128
|
54 |
|
'TipoImp' => $this->entityComponent->getRepository( |
129
|
54 |
|
ImpuestoAdicionalRetencion::class |
130
|
54 |
|
), |
131
|
54 |
|
'MedioPago' => $this->entityComponent->getRepository( |
132
|
54 |
|
MedioPago::class |
133
|
54 |
|
), |
134
|
54 |
|
'FmaPago' => $this->entityComponent->getRepository( |
135
|
54 |
|
FormaPago::class |
136
|
54 |
|
), |
137
|
54 |
|
'Nacionalidad' => $this->entityComponent->getRepository( |
138
|
54 |
|
AduanaPais::class |
139
|
54 |
|
), |
140
|
54 |
|
'CodPaisRecep' => 'alias:Nacionalidad', |
141
|
54 |
|
'IndTraslado' => $this->entityComponent->getRepository( |
142
|
54 |
|
Traslado::class |
143
|
54 |
|
), |
144
|
54 |
|
'CodViaTransp' => $this->entityComponent->getRepository( |
145
|
54 |
|
AduanaTransporte::class |
146
|
54 |
|
), |
147
|
|
|
// Timbre Electrónico del Documento (TED). |
148
|
54 |
|
'TED' => function (string $timbre) { |
149
|
54 |
|
$pdf417 = new TCPDF2DBarcode($timbre, 'PDF417,,5'); |
150
|
54 |
|
$png = $pdf417->getBarcodePngData(1, 1, [0,0,0]); |
151
|
54 |
|
return 'data:image/png;base64,' . base64_encode($png); |
152
|
54 |
|
}, |
153
|
|
|
// Montos sin decimales y formato de Chile en separadores. |
154
|
54 |
|
'Number' => function (int|float|string $num) { |
155
|
|
|
$num = round((float) $num); |
156
|
|
|
return number_format($num, 0, ',', '.'); |
157
|
54 |
|
}, |
158
|
|
|
// Montos según moneda. |
159
|
54 |
|
'MontoMoneda' => function (string $value) { |
160
|
|
|
[$codigo, $num] = explode(':', $value); |
161
|
|
|
$result = $this->entityComponent->getRepository( |
162
|
|
|
AduanaMoneda::class |
163
|
|
|
)->findBy(['glosa' => $codigo]); |
164
|
|
|
$moneda = ($result[0] ?? null)?->getCurrency() ?? Currency::XXX; |
165
|
|
|
return $moneda->format((float) $num); |
166
|
54 |
|
}, |
167
|
54 |
|
]; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|