1+ <?php
2+
3+ /**
4+ * LibreDTE: Cliente de API en PHP - Pruebas Unitarias.
5+ * Copyright (C) LibreDTE <https://www.libredte.cl>
6+ *
7+ * Este programa es software libre: usted puede redistribuirlo y/o modificarlo
8+ * bajo los términos de la GNU Lesser General Public License (LGPL) publicada
9+ * por la Fundación para el Software Libre, ya sea la versión 3 de la Licencia,
10+ * o (a su elección) cualquier versión posterior de la misma.
11+ *
12+ * Este programa se distribuye con la esperanza de que sea útil, pero SIN
13+ * GARANTÍA ALGUNA; ni siquiera la garantía implícita MERCANTIL o de APTITUD
14+ * PARA UN PROPÓSITO DETERMINADO. Consulte los detalles de la GNU Lesser General
15+ * Public License (LGPL) para obtener una información más detallada.
16+ *
17+ * Debería haber recibido una copia de la GNU Lesser General Public License
18+ * (LGPL) junto a este programa. En caso contrario, consulte
19+ * <http://www.gnu.org/licenses/lgpl.html>.
20+ */
21+
22+ use PHPUnit \Framework \TestCase ;
23+ use libredte \api_client \ApiClient ;
24+ use libredte \api_client \ApiException ;
25+
26+ class EmitirDteTemporalTest extends TestCase
27+ {
28+ # TODO: Revisar código de testing de DTE Temporal.
29+ protected static $ verbose ;
30+ protected static $ client ;
31+ protected static $ emisor_rut ;
32+
33+ private static $ datos = [
34+ 'Encabezado ' => [
35+ 'IdDoc ' => [
36+ 'TipoDTE ' => 33 ,
37+ ],
38+ 'Emisor ' => [
39+ 'RUTEmisor ' => null , // se reemplaza al preparar la clase
40+ ],
41+ 'Receptor ' => [
42+ 'RUTRecep ' => '60803000-K ' ,
43+ 'RznSocRecep ' => 'Servicio de Impuestos Internos (SII) ' ,
44+ 'GiroRecep ' => 'Administración Pública ' ,
45+ 'Contacto ' => '+56 2 3252 5575 ' ,
46+ 'CorreoRecep ' => 'facturacionmipyme@sii.cl ' ,
47+ 'DirRecep ' => 'Teatinos 120 ' ,
48+ 'CmnaRecep ' => 'Santiago ' ,
49+ ],
50+ ],
51+ 'Detalle ' => [
52+ [
53+ //'IndExe' => 1, // para items exentos
54+ 'NmbItem ' => 'Asesoría de LibreDTE ' ,
55+ 'QtyItem ' => 1 ,
56+ 'PrcItem ' => 1000 ,
57+ ],
58+ ],
59+ 'Referencia ' => [
60+ [
61+ 'TpoDocRef ' => 801 ,
62+ 'FolioRef ' => 'OC123 ' ,
63+ 'FchRef ' => '2015-10-01 ' ,
64+ ]
65+ ],
66+ ];
67+
68+ public static function setUpBeforeClass (): void
69+ {
70+ self ::$ verbose = env ('TEST_VERBOSE ' , false );
71+ self ::$ emisor_rut = (int )(explode ('- ' , (string )env ('LIBREDTE_RUT ' ))[0 ]);
72+ self ::$ datos ['Encabezado ' ]['Emisor ' ]['RUTEmisor ' ] = env ('LIBREDTE_RUT ' );
73+ self ::$ client = new ApiClient ();
74+ }
75+
76+ private function _buscar_temps (): array
77+ {
78+ $ filtros = [
79+ 'fecha_desde ' => '2015-01-01 ' ,
80+ 'fecha_hasta ' => date ('Y-m-d ' ),
81+ ];
82+ $ resource = sprintf ('/dte/dte_tmps/buscar/%d ' , self ::$ emisor_rut );
83+ $ response = self ::$ client ->post ($ resource , $ filtros );
84+ if ($ response ['status ' ]['code ' ] != 200 ) {
85+ throw new ApiException ($ response ['body ' ], $ response ['status ' ]['code ' ]);
86+ }
87+ return $ response ['body ' ];
88+ }
89+
90+ public function test_dte_temp ()
91+ {
92+ $ lista_dtes = $ this ->_buscar_temps ();
93+ if (self ::$ verbose ) {
94+ echo "\n" ."test_dte_buscar_temps() \n" ;
95+ foreach ($ lista_dtes as $ dte ){
96+ echo "DTE: " .'T ' .$ dte ['dte ' ].'F ' .$ dte ['codigo ' ]."\n" ;
97+ #foreach($dte as $clave => $valor){
98+ # echo $clave." : ".$valor."\n";
99+ #}
100+ }
101+ }
102+ $ dte_temporal = $ this ->_emitir_dte_temporal ();
103+ $ info_dte = $ this ->_buscar_documento_temporal ($ lista_dtes );
104+ $ this ->_descargar_pdf_temp ($ dte_temporal );
105+ $ cobro = $ this ->_buscar_cobro_asociado_dte_temp ($ dte_temporal );
106+ $ this ->_buscar_datos_cobro_con_estado ($ cobro );
107+ $ this ->_eliminar_dte_temporal ($ dte_temporal );
108+ }
109+
110+ private function _buscar_documento_temporal ($ lista_dte ): array
111+ {
112+ try {
113+ $ documento = $ lista_dte [0 ];
114+
115+ $ resource = sprintf (
116+ '/dte/dte_tmps/info/%d/%d/%s/%d ' ,
117+ $ documento ['receptor ' ],
118+ $ documento ['dte ' ],
119+ $ documento ['codigo ' ],
120+ self ::$ emisor_rut ,
121+ );
122+
123+ $ response = self ::$ client ->get ($ resource );
124+
125+ $ this ->assertTrue (true );
126+ if (self ::$ verbose ) {
127+ $ dte_id = 'T ' .$ documento ['dte ' ].'F ' .$ documento ['codigo ' ];
128+ echo "\n" ,'test_dte_buscar_documento_temporal() dte_id ' ,$ dte_id ,"\n" ;
129+ echo "\n" ,'test_dte_buscar_documento_temporal() dte_fecha ' ,$ documento ['fecha ' ],"\n" ;
130+ echo "\n" ,'test_dte_buscar_documento_temporal() dte_total ' ,$ documento ['total ' ],"\n" ;
131+ }
132+ return $ response ['body ' ];
133+ } catch (ApiException $ e ) {
134+ $ this ->fail (sprintf ('[ApiException %d] %s ' , $ e ->getCode (), $ e ->getMessage ()));
135+ }
136+ }
137+
138+ private function _emitir_dte_temporal (): array
139+ {
140+ try {
141+ $ response = self ::$ client ->post ('/dte/documentos/emitir ' , self ::$ datos );
142+ if ($ response ['status ' ]['code ' ] != 200 ) {
143+ throw new ApiException ($ response ['body ' ], $ response ['status ' ]['code ' ]);
144+ }
145+ $ this ->assertEquals (200 , $ response ['status ' ]['code ' ]);
146+ if (self ::$ verbose ) {
147+ echo "\n" ,'test_dte_temp() emitir_dte_temporal ' ,json_encode ($ response ['body ' ]),"\n" ;
148+ }
149+ return $ response ['body ' ];
150+ } catch (ApiException $ e ) {
151+ $ this ->fail (sprintf ('[ApiException %d] %s ' , $ e ->getCode (), $ e ->getMessage ()));
152+ }
153+ }
154+
155+ private function _descargar_pdf_temp (array $ dte_temp ): void
156+ {
157+ $ resource = sprintf (
158+ '/dte/dte_tmps/pdf/%d/%d/%s/%d ' ,
159+ $ dte_temp ['receptor ' ],
160+ $ dte_temp ['dte ' ],
161+ $ dte_temp ['codigo ' ],
162+ $ dte_temp ['emisor ' ],
163+ );
164+ try {
165+ $ response = self ::$ client ->get ($ resource );
166+ if ($ response ['status ' ]['code ' ] != 200 ) {
167+ throw new ApiException ($ response ['body ' ], $ response ['status ' ]['code ' ]);
168+ }
169+ $ this ->assertEquals (200 , $ response ['status ' ]['code ' ]);
170+ $ filename = __DIR__ . '/ ' .str_replace ('.php ' , '.pdf ' , basename (__FILE__ ));
171+ file_put_contents ($ filename , $ response ['body ' ]);
172+ unlink ($ filename );
173+ if (self ::$ verbose ) {
174+ echo "\n" ,'test_dte_temp() pdf ' ,$ filename ,"\n" ;
175+ }
176+ } catch (ApiException $ e ) {
177+ $ this ->fail (sprintf ('[ApiException %d] %s ' , $ e ->getCode (), $ e ->getMessage ()));
178+ }
179+ }
180+
181+ private function _buscar_cobro_asociado_dte_temp (array $ dte_temp )
182+ {
183+ $ resource = sprintf (
184+ '/dte/dte_tmps/cobro/%d/%d/%s/%d ' ,
185+ $ dte_temp ['receptor ' ],
186+ $ dte_temp ['dte ' ],
187+ $ dte_temp ['codigo ' ],
188+ $ dte_temp ['emisor ' ],
189+ );
190+ try {
191+ $ response = self ::$ client ->get ($ resource );
192+ if ($ response ['status ' ]['code ' ] != 200 ) {
193+ throw new ApiException ($ response ['body ' ], $ response ['status ' ]['code ' ]);
194+ }
195+ $ this ->assertEquals (200 , $ response ['status ' ]['code ' ]);
196+ if (self ::$ verbose ) {
197+ echo "\n" ,'test_dte_temp() cobro_dte_temp ' ,json_encode ($ response ['body ' ]),"\n" ;
198+ }
199+ return $ response ['body ' ];
200+ } catch (ApiException $ e ) {
201+ $ this ->fail (sprintf ('[ApiException %d] %s ' , $ e ->getCode (), $ e ->getMessage ()));
202+ }
203+ }
204+
205+ private function _buscar_datos_cobro_con_estado (array $ cobro )
206+ {
207+ $ resource = sprintf (
208+ '/pagos/cobros/info/%s/%d ' ,
209+ $ cobro ['codigo ' ],
210+ $ cobro ['emisor ' ],
211+ );
212+ try {
213+ $ response = self ::$ client ->get ($ resource );
214+ if ($ response ['status ' ]['code ' ] != 200 ) {
215+ throw new ApiException ($ response ['body ' ], $ response ['status ' ]['code ' ]);
216+ }
217+ $ this ->assertEquals (200 , $ response ['status ' ]['code ' ]);
218+ if (self ::$ verbose ) {
219+ echo "\n" ,'test_dte_temp() datos_cobro ' ,json_encode ($ response ['body ' ]),"\n" ;
220+ }
221+ } catch (ApiException $ e ) {
222+ $ this ->fail (sprintf ('[ApiException %d] %s ' , $ e ->getCode (), $ e ->getMessage ()));
223+ }
224+ }
225+
226+ private function _eliminar_dte_temporal (array $ dte_temp )
227+ {
228+ $ resource = sprintf (
229+ '/dte/dte_tmps/eliminar/%d/%d/%s/%d ' ,
230+ $ dte_temp ['receptor ' ],
231+ $ dte_temp ['dte ' ],
232+ $ dte_temp ['codigo ' ],
233+ $ dte_temp ['emisor ' ],
234+ );
235+ try {
236+ $ response = self ::$ client ->get ($ resource );
237+ if ($ response ['status ' ]['code ' ] != 200 ) {
238+ throw new ApiException ($ response ['body ' ], $ response ['status ' ]['code ' ]);
239+ }
240+ $ this ->assertEquals (200 , $ response ['status ' ]['code ' ]);
241+ if (self ::$ verbose ) {
242+ echo "\n" ,'test_dte_temp() eliminar_dte ' ,json_encode ($ response ['body ' ]),"\n" ;
243+ }
244+ } catch (ApiException $ e ) {
245+ $ this ->fail (sprintf ('[ApiException %d] %s ' , $ e ->getCode (), $ e ->getMessage ()));
246+ }
247+ }
248+ }
0 commit comments