1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ /**
6+ * LibreDTE: Cliente de API en PHP - Pruebas Unitarias.
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 GNU Lesser General Public License (LGPL) publicada
11+ * por la Fundación para el Software Libre, ya sea la versión 3 de la Licencia,
12+ * o (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 GNU Lesser General
17+ * Public License (LGPL) para obtener una información más detallada.
18+ *
19+ * Debería haber recibido una copia de la GNU Lesser General Public License
20+ * (LGPL) junto a este programa. En caso contrario, consulte
21+ * <http://www.gnu.org/licenses/lgpl.html>.
22+ */
23+
24+ namespace libredte \dte_facturacion ;
25+
26+ use libredte \api_client \ApiClient ;
27+ use libredte \api_client \ApiException ;
28+ use PHPUnit \Framework \TestCase ;
29+
30+ abstract class AbstractDteFacturacion extends TestCase
31+ {
32+ /**
33+ * Variable para desplegar resultados.
34+ *
35+ * @var bool
36+ */
37+ protected static $ verbose ;
38+
39+ /**
40+ * Variable instancia del API Client.
41+ *
42+ * @var ApiClient
43+ */
44+ protected static $ client ;
45+
46+ /**
47+ * RUT del emisor a utilizar.
48+ *
49+ * @var int
50+ */
51+ protected static $ emisor_rut ;
52+
53+ /**
54+ * Datos para producir el DTE temporal.
55+ *
56+ * @var array
57+ */
58+ private static $ datos = [
59+ 'Encabezado ' => [
60+ 'IdDoc ' => [
61+ 'TipoDTE ' => 33 ,
62+ ],
63+ 'Emisor ' => [
64+ 'RUTEmisor ' => null , // se reemplaza al preparar la clase
65+ ],
66+ 'Receptor ' => [
67+ 'RUTRecep ' => '60803000-K ' ,
68+ 'RznSocRecep ' => 'Servicio de Impuestos Internos (SII) ' ,
69+ 'GiroRecep ' => 'Administración Pública ' ,
70+ 'Contacto ' => '+56 2 3252 5575 ' ,
71+ 'CorreoRecep ' => 'facturacionmipyme@sii.cl ' ,
72+ 'DirRecep ' => 'Teatinos 120 ' ,
73+ 'CmnaRecep ' => 'Santiago ' ,
74+ ],
75+ ],
76+ 'Detalle ' => [
77+ [
78+ //'IndExe' => 1, // para items exentos
79+ 'NmbItem ' => 'Asesoría de LibreDTE ' ,
80+ 'QtyItem ' => 1 ,
81+ 'PrcItem ' => 1000 ,
82+ ],
83+ ],
84+ 'Referencia ' => [
85+ [
86+ 'TpoDocRef ' => 801 ,
87+ 'FolioRef ' => 'OC123 ' ,
88+ 'FchRef ' => '2015-10-01 ' ,
89+ ],
90+ ],
91+ ];
92+
93+ /**
94+ * Inicialización de variables y clases pre ejecución de tests.
95+ *
96+ * @return void
97+ */
98+ public static function setUpBeforeClass (): void
99+ {
100+ self ::$ verbose = (bool )env ('TEST_VERBOSE ' , 'false ' );
101+ self ::$ emisor_rut = (explode ('- ' , (string )env ('LIBREDTE_RUT ' ))[0 ]);
102+ self ::$ datos ['Encabezado ' ]['Emisor ' ]['RUTEmisor ' ] = env ('LIBREDTE_RUT ' );
103+ self ::$ client = new ApiClient ();
104+ }
105+
106+ /**
107+ * Método para listar DTEs temporales.
108+ *
109+ * @throws \libredte\api_client\ApiException
110+ *
111+ * @return array Listado de DTEs temporales entre 2015 y hoy.
112+ */
113+ protected function listarDteTemp (): array
114+ {
115+ # Se crea el filtro a utilizar, en este caso fechas de búsqueda.
116+ $ filtros = [
117+ 'fecha_desde ' => '2015-01-01 ' ,
118+ 'fecha_hasta ' => date ('Y-m-d ' ),
119+ ];
120+ # Se genera el recurso a consumir.
121+ $ resource = sprintf ('/dte/dte_tmps/buscar/%d ' , self ::$ emisor_rut );
122+ # Se envía la solicitud http y se guarda su respuesta.
123+ $ response = self ::$ client ->post ($ resource , $ filtros );
124+
125+ # Si el código http no es '200', arroja error ApiException.
126+ if ($ response ['status ' ]['code ' ] !== '200 ' ) {
127+ throw new ApiException ($ response ['body ' ], (int )$ response ['status ' ]['code ' ]);
128+ }
129+
130+ # Retorna la respuesta http.
131+ return $ response ;
132+ }
133+
134+ /**
135+ * Método para emitir un DTE temporal.
136+ *
137+ * @throws \libredte\api_client\ApiException
138+ *
139+ * @return array DTE temporal generado.
140+ */
141+ protected function emitirDteTemp (): array
142+ {
143+ # Se envía la solicitud http y se guarda su respuesta.
144+ $ response = self ::$ client ->post ('/dte/documentos/emitir ' , self ::$ datos );
145+
146+ # Si el código http no es '200', arroja error ApiException.
147+ if ($ response ['status ' ]['code ' ] !== '200 ' ) {
148+ throw new ApiException ($ response ['body ' ], (int )$ response ['status ' ]['code ' ]);
149+ }
150+
151+ # Retorna la respuesta http.
152+ return $ response ;
153+ }
154+ }
0 commit comments