-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdte.php
244 lines (217 loc) · 9.22 KB
/
dte.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
/**
* LibreDTE
* Copyright (C) SASCO SpA (https://sasco.cl)
*
* Este programa es software libre: usted puede redistribuirlo y/o
* modificarlo bajo los términos de la Licencia Pública General Affero de GNU
* publicada por la Fundación para el Software Libre, ya sea la versión
* 3 de la Licencia, o (a su elección) cualquier versión posterior de la
* misma.
*
* Este programa se distribuye con la esperanza de que sea útil, pero
* SIN GARANTÍA ALGUNA; ni siquiera la garantía implícita
* MERCANTIL o de APTITUD PARA UN PROPÓSITO DETERMINADO.
* Consulte los detalles de la Licencia Pública General Affero de GNU para
* obtener una información más detallada.
*
* Debería haber recibido una copia de la Licencia Pública General Affero de GNU
* junto a este programa.
* En caso contrario, consulte <http://www.gnu.org/licenses/agpl.html>.
*/
/******************************************************************************/
/* Configuración */
/******************************************************************************/
// errores
ini_set('display_errors', true);
error_reporting(E_ALL | E_STRICT | E_DEPRECATED);
// autenticación: si ambos son vacíos no se usa autenticación
$auth_user = '';
$auth_pass = '';
// autocarga composer
require('vendor/autoload.php');
/******************************************************************************/
/* Declaración de funciones */
/******************************************************************************/
// función que verifica las credenciales
function auth_check($user, $pass)
{
if (!$user && !$pass) {
return true;
}
$headers = apache_request_headers();
if (empty($headers['Authorization'])) {
return false;
}
list($basic, $Authorization) = explode(' ', $headers['Authorization']);
list($u, $p) = explode(':', base64_decode($Authorization));
return $u == $user && $p == $pass;
}
/******************************************************************************/
/* Declaración de clases */
/******************************************************************************/
// clase base para la generación del PDF (no modificar, si se desea cambiar algo
// hacerlo en la clase PDF sobreescribiendo métodos de esta clase)
// Si no quiere usar TCPDF, considere escribir por completo la clase PDF, sin
// extender ni usar esta clase
abstract class PDF_Base extends \TCPDF {
protected $options;
public function setOptions($options)
{
$this->options = array_merge([
'cedible' => 1,
'papelContinuo' => 0,
'compress' => false,
'webVerificacion' => 'www.sii.cl',
'copias_tributarias' => 1,
'copias_cedibles' => 1,
], $options);
}
public function download($file)
{
$this->Output($file, 'I');
}
public function Header()
{
}
public function Footer()
{
}
protected function num($n)
{
return number_format($n, 0, ',', '.');
}
protected function rut($r)
{
list($rut, $dv) = explode('-', $r);
return $this->num($rut).'-'.$dv;
}
protected function date($fecha)
{
list($Y, $m, $d) = explode('-', $fecha);
return $d.'/'.$m.'/'.$Y;
}
protected function getTED(\DomDocument $dom)
{
$TED = $dom->getElementsByTagName('TED')->item(0);
if (!$TED) {
return '<TED/>'; // permite funcionar con cotizaciones de LibreDTE
}
$ted_dom = new DomDocument;
$ted_dom->appendChild($ted_dom->importNode($TED, true));
$ted_dom->documentElement->removeAttributeNS('http://www.w3.org/2001/XMLSchema-instance', 'xsi');
$ted_dom->documentElement->removeAttributeNS('http://www.sii.cl/SiiDte', '');
$xml = $ted_dom->C14N();
$xml = preg_replace("/\>\n\s+\</", '><', $xml);
$xml = preg_replace("/\>\n\t+\</", '><', $xml);
$xml = preg_replace("/\>\n+\</", '><', $xml);
$xml = trim($xml);
return mb_detect_encoding($xml, ['UTF-8', 'ISO-8859-1']) != 'ISO-8859-1' ? utf8_decode($xml) : $xml;
}
}
// clase que genera el PDF, extiende la base, contiene sólo el método agregar
// que es el método que agrega el DTE al PDF. Este método agregar es el que debe ser modificado.
// Este es SÓLO un ejemplo, DEBE ser modificado
class PDF extends PDF_Base {
protected $documentos = [
33 => 'Factura electrónica',
];
public function agregar(\DomDocument $dom)
{
$this->AddPage();
// logo de la empresa
$h_img = 20;
$this->Image('img/logo.png', $this->getX(), $this->getY(), null, $h_img);
$this->setY($this->y + $h_img + 5);
// datos emisor
$this->Cell(0, 0, $dom->getElementsByTagName('RznSoc')->item(0)->textContent);
$this->Ln();
$this->Cell(0, 0, 'RUT: '.$this->rut($dom->getElementsByTagName('RUTEmisor')->item(0)->textContent));
$this->Ln();
$this->Cell(0, 0, 'Giro: '.$dom->getElementsByTagName('GiroEmis')->item(0)->textContent);
$this->Ln();
$this->Cell(0, 0, 'Dirección: '.$dom->getElementsByTagName('DirOrigen')->item(0)->textContent.', '.$dom->getElementsByTagName('CmnaOrigen')->item(0)->textContent);
$this->Ln();
$this->Ln();
// datos DTE
$this->Cell(0, 0, $this->documentos[$dom->getElementsByTagName('TipoDTE')->item(0)->textContent].' N° '.$dom->getElementsByTagName('Folio')->item(0)->textContent);
$this->Ln();
$this->Cell(0, 0, 'Fecha emisión: '.$this->date($dom->getElementsByTagName('FchEmis')->item(0)->textContent));
$this->Ln();
$this->Ln();
// datos receptor
$this->Cell(0, 0, 'Receptor: '.$dom->getElementsByTagName('RznSocRecep')->item(0)->textContent);
$this->Ln();
$this->Cell(0, 0, 'RUT: '.$this->rut($dom->getElementsByTagName('RUTRecep')->item(0)->textContent));
$this->Ln();
$this->Cell(0, 0, 'Giro: '.$dom->getElementsByTagName('GiroRecep')->item(0)->textContent);
$this->Ln();
$this->Cell(0, 0, 'Dirección: '.$dom->getElementsByTagName('DirRecep')->item(0)->textContent.', '.$dom->getElementsByTagName('CmnaRecep')->item(0)->textContent);
$this->Ln();
$this->Ln();
// detalle
$Detalle = $dom->getElementsByTagName('Detalle');
foreach ($Detalle as $d) {
$item = '';
$item .= $this->num($d->getElementsByTagName('QtyItem')->item(0)->textContent).' x ';
$item .= $d->getElementsByTagName('NmbItem')->item(0)->textContent;
$item .= ' ('.$this->num($d->getElementsByTagName('PrcItem')->item(0)->textContent).' c/u)';
$item .= ': $'.$this->num($d->getElementsByTagName('MontoItem')->item(0)->textContent);
$this->Cell(0, 0, $item);
$this->Ln();
}
$this->Ln();
// totales
$this->Cell(0, 0, 'Neto: $'.$this->num($dom->getElementsByTagName('MntNeto')->item(0)->textContent));
$this->Ln();
$this->Cell(0, 0, 'IVA: $'.$this->num($dom->getElementsByTagName('IVA')->item(0)->textContent));
$this->Ln();
$this->Cell(0, 0, 'Total: $'.$this->num($dom->getElementsByTagName('MntTotal')->item(0)->textContent));
$this->Ln();
$this->Ln();
// timbre
$ted = $this->getTED($dom);
$style = [
'border' => false,
'padding' => 0,
'hpadding' => 0,
'vpadding' => 0,
'module_width' => 1, // width of a single module in points
'module_height' => 1, // height of a single module in points
'fgcolor' => [0,0,0],
'bgcolor' => false, // [255,255,255]
'position' => 'S',
];
$ecl = version_compare(phpversion(), '7.0.0', '<') ? -1 : 5;
$this->write2DBarcode($ted, 'PDF417,,'.$ecl, $this->getX(), $this->getY(), 70, 0, $style, 'B');
$this->Ln();
$this->setFont('', 'B', 8);
$this->Cell(0, 0, 'Timbre Electrónico SII');
$this->Ln();
$this->Cell(0, 0, 'Resolución '.$dom->getElementsByTagName('NroResol')->item(0)->textContent.' de '.explode('-', $dom->getElementsByTagName('FchResol')->item(0)->textContent)[0]);
$this->Ln();
$this->Cell(0, 0, 'Verifique documento: www.sii.cl');
$this->Ln();
$this->Ln();
// imagen con pie de página de la factura
$this->Image('img/footer.png', $this->getX(), $this->getY(), 190, 'PNG');
}
}
/******************************************************************************/
/* Programa principal (main) */
/******************************************************************************/
// verificar si el usuario corresponde
if (!auth_check($auth_user, $auth_pass)) {
die('Usuario no autorizado');
}
// extraer datos y crear crear DomDocument
$datos = json_decode(file_get_contents('php://input'), true);
$xml = base64_decode($datos['xml']);
unset($datos['xml']);
$dom = new \DomDocument();
$dom->loadXML($xml);
// crear PDF
$pdf = new PDF();
$pdf->setOptions($datos);
$pdf->agregar($dom);
$pdf->download('dte.pdf');