BteEmitidas::obtenerHtmlBteEmitida()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 11
ccs 0
cts 10
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * API Gateway: Cliente de API en PHP.
7
 * Copyright (C) API Gateway <https://www.apigateway.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 apigatewaycl\api_client\sii;
25
26
use apigatewaycl\api_client\ApiBase;
27
use Psr\Http\Message\ResponseInterface;
28
29
/**
30
 * Módulo para la emisión de Boletas de Terceros Electrónicas del SII.
31
 *
32
 * Para más información sobre la API, consulte la `documentación completa de las
33
 * BTE <https://developers.apigateway.cl/#e08f50ab-5509-48ab-81ab-63fc8e5985e1>`_.
34
 */
35
class BteEmitidas extends ApiBase
36
{
37
    /**
38
     * Cliente específico para gestionar Boletas de Terceros Electrónicas (BTE) emitidas.
39
     *
40
     * Provee métodos para emitir, anular, y consultar información relacionada con BTEs.
41
     *
42
     * @param array $credenciales Credenciales de autenticación.
43
     * @param string|null $token Token de autenticación para la API.
44
     * @param string|null $url URL base para la API.
45
     */
46
    public function __construct(
47
        array $credenciales,
48
        string $token = null,
49
        string $url = null
50
    ) {
51
        parent::__construct(
52
            credenciales: $credenciales,
53
            token: $token,
54
            url: $url
55
        );
56
    }
57
58
    /**
59
     * Obtiene los documentos BTE emitidos por un emisor en un periodo específico.
60
     *
61
     * @param string $emisor RUT del emisor de las BTE.
62
     * @param string $periodo Período de las BTE emitidas.
63
     * @param int|null $pagina Número de página para paginación (opcional).
64
     * @return \Psr\Http\Message\ResponseInterface Respuesta JSON con
65
     * los documentos BTE.
66
     */
67
    public function listarBtesEmitidas(
68
        string $emisor,
69
        string $periodo,
70
        int $pagina = null
71
    ): ResponseInterface {
72
        $url = sprintf(
73
            '/sii/bte/emitidas/documentos/%s/%s',
74
            $emisor,
75
            $periodo
76
        );
77
        if ($pagina != null) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $pagina of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison !== instead.
Loading history...
78
            $url = sprintf(
79
                $url.'?pagina=%d',
80
                $pagina
81
            );
82
        }
83
        $body = [
84
            'auth' => $this->getAuthPass(),
85
        ];
86
        $response = $this->post(resource: $url, data: $body);
87
        return $response;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response could return the type null which is incompatible with the type-hinted return Psr\Http\Message\ResponseInterface. Consider adding an additional type-check to rule them out.
Loading history...
88
    }
89
90
    /**
91
     * Obtiene la representación HTML de una BTE emitida.
92
     *
93
     * @param string $codigo Código único de la BTE.
94
     * @return \Psr\Http\Message\ResponseInterface Contenido HTML de la BTE.
95
     */
96
    public function obtenerHtmlBteEmitida(string $codigo): ResponseInterface
97
    {
98
        $url = sprintf(
99
            '/sii/bte/emitidas/html/%s',
100
            $codigo
101
        );
102
        $body = [
103
            'auth' => $this->getAuthPass(),
104
        ];
105
        $response = $this->post(resource: $url, data: $body);
106
        return $response;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response could return the type null which is incompatible with the type-hinted return Psr\Http\Message\ResponseInterface. Consider adding an additional type-check to rule them out.
Loading history...
107
    }
108
109
    /**
110
     * Emite una nueva Boleta de Tercero Electrónica.
111
     *
112
     * @param array $datos Datos de la boleta a emitir.
113
     * @return \Psr\Http\Message\ResponseInterface Respuesta JSON con la
114
     * confirmación de la emisión de la BTE.
115
     */
116
    public function emitirBte(array $datos): ResponseInterface
117
    {
118
        $url = '/sii/bte/emitidas/emitir';
119
        $body = [
120
            'auth' => $this->getAuthPass(),
121
            'boleta' => $datos,
122
        ];
123
124
        $response = $this->post(resource: $url, data: $body);
125
        return $response;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response could return the type null which is incompatible with the type-hinted return Psr\Http\Message\ResponseInterface. Consider adding an additional type-check to rule them out.
Loading history...
126
    }
127
128
    /**
129
     * Anula una BTE emitida.
130
     *
131
     * @param string $emisor RUT del emisor de la boleta.
132
     * @param string $numero Número de la boleta.
133
     * @param int $causa Causa de anulación.
134
     * @param string|null $periodo Período de emisión de la boleta (opcional).
135
     * @return \Psr\Http\Message\ResponseInterface Respuesta JSON con la
136
     * confirmación de la anulación.
137
     */
138
    public function anularBteEmitida(
139
        string $emisor,
140
        string $numero,
141
        int $causa = 3,
142
        string $periodo = null
143
    ): ResponseInterface {
144
        $url = sprintf(
145
            '/sii/bhe/emitidas/anular/%s/%s?causa=%d',
146
            $emisor,
147
            $numero,
148
            $causa
149
        );
150
        if ($periodo != null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $periodo of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
151
            $url = $url.sprintf('?periodo=%s', $periodo);
152
        }
153
        $body = [
154
            'auth' => $this->getAuthPass(),
155
        ];
156
157
        $response = $this->post(resource: $url, data: $body);
158
        return $response;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response could return the type null which is incompatible with the type-hinted return Psr\Http\Message\ResponseInterface. Consider adding an additional type-check to rule them out.
Loading history...
159
    }
160
161
    /**
162
     * Obtiene la tasa de retención aplicada a un receptor por un emisor específico.
163
     *
164
     * @param string $emisor RUT del emisor de la boleta.
165
     * @param string $receptor RUT del receptor de la boleta.
166
     * @param string|null $periodo Período de emisión de la boleta (opcional).
167
     * @return \Psr\Http\Message\ResponseInterface Respuesta JSON con la tasa de retención.
168
     */
169
    public function obtenerTasaReceptorBte(
170
        string $emisor,
171
        string $receptor,
172
        string $periodo = null
173
    ): ResponseInterface {
174
        $url = sprintf(
175
            '/sii/bte/emitidas/receptor_tasa/%s/%s',
176
            $emisor,
177
            $receptor
178
        );
179
        if ($periodo != null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $periodo of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
180
            $url = $url.sprintf('?periodo=%s', $periodo);
181
        }
182
        $body = [
183
            'auth' => $this->getAuthPass(),
184
        ];
185
186
        $response = $this->post(resource: $url, data: $body);
187
        return $response;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response could return the type null which is incompatible with the type-hinted return Psr\Http\Message\ResponseInterface. Consider adding an additional type-check to rule them out.
Loading history...
188
    }
189
}
190