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\Identifier\Support; |
26
|
|
|
|
27
|
|
|
use libredte\lib\Core\Package\Billing\Component\Document\Contract\TipoDocumentoInterface; |
28
|
|
|
use libredte\lib\Core\Package\Billing\Component\Identifier\Contract\CafBagInterface; |
29
|
|
|
use libredte\lib\Core\Package\Billing\Component\Identifier\Contract\CafInterface; |
30
|
|
|
use libredte\lib\Core\Package\Billing\Component\Identifier\Exception\CafException; |
31
|
|
|
use libredte\lib\Core\Package\Billing\Component\TradingParties\Contract\EmisorInterface; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Contenedor de datos del archivo CAF de un documento tributario electrónico. |
35
|
|
|
* |
36
|
|
|
* Permite "mover" un CAF, junto a otros datos asociados, por métodos de |
37
|
|
|
* manera sencilla y, sobre todo, extensible. |
38
|
|
|
*/ |
39
|
|
|
class CafBag implements CafBagInterface |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* Listado de folios disponibles en el CAF. |
43
|
|
|
* |
44
|
|
|
* Estos son los folios que se pueden usar. |
45
|
|
|
* |
46
|
|
|
* @var int[] |
47
|
|
|
*/ |
48
|
|
|
private array $foliosDisponibles; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Constructor de la bolsa del CAF. |
52
|
|
|
* |
53
|
|
|
* @param CafInterface $caf |
54
|
|
|
* @param EmisorInterface $emisor |
55
|
|
|
* @param TipoDocumentoInterface $tipoDocumento |
56
|
|
|
*/ |
57
|
61 |
|
public function __construct( |
58
|
|
|
private readonly CafInterface $caf, |
59
|
|
|
private readonly EmisorInterface $emisor, |
60
|
|
|
private readonly TipoDocumentoInterface $tipoDocumento |
61
|
|
|
) { |
62
|
61 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritDoc} |
66
|
|
|
*/ |
67
|
61 |
|
public function getCaf(): CafInterface |
68
|
|
|
{ |
69
|
61 |
|
return $this->caf; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritDoc} |
74
|
|
|
*/ |
75
|
|
|
public function getEmisor(): EmisorInterface |
76
|
|
|
{ |
77
|
|
|
return $this->emisor; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritDoc} |
82
|
|
|
*/ |
83
|
2 |
|
public function getTipoDocumento(): TipoDocumentoInterface |
84
|
|
|
{ |
85
|
2 |
|
return $this->tipoDocumento; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritDoc} |
90
|
|
|
*/ |
91
|
|
|
public function setFoliosDisponibles(array $foliosDisponibles): static |
92
|
|
|
{ |
93
|
|
|
$this->foliosDisponibles = $foliosDisponibles; |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritDoc} |
100
|
|
|
*/ |
101
|
|
|
public function getFoliosDisponibles(): array |
102
|
|
|
{ |
103
|
|
|
$this->ensureFoliosDisponibles(); |
104
|
|
|
|
105
|
|
|
return $this->foliosDisponibles; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritDoc} |
110
|
|
|
*/ |
111
|
|
|
public function getSiguienteFolio(): int |
112
|
|
|
{ |
113
|
|
|
$this->ensureFoliosDisponibles(); |
114
|
|
|
|
115
|
|
|
$folio = array_shift($this->foliosDisponibles); |
116
|
|
|
|
117
|
|
|
if ($folio === null) { |
118
|
|
|
throw new CafException(sprintf( |
119
|
|
|
'El CAF del documento %s de %s que comienza en %d y termina en %d no tiene folios disponibles.', |
120
|
|
|
$this->getTipoDocumento()->getNombre(), |
121
|
|
|
$this->getEmisor()->getRazonSocial(), |
122
|
|
|
$this->getCaf()->getFolioDesde(), |
123
|
|
|
$this->getCaf()->getFolioHasta() |
124
|
|
|
)); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $folio; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Asegura que existan folios disponibles asignados. |
132
|
|
|
* |
133
|
|
|
* Si no existen asignados se asignan según el rango del CAF. |
134
|
|
|
* |
135
|
|
|
* @return void |
136
|
|
|
*/ |
137
|
|
|
private function ensureFoliosDisponibles(): void |
138
|
|
|
{ |
139
|
|
|
if (!isset($this->foliosDisponibles)) { |
140
|
|
|
$desde = $this->getCaf()->getFolioDesde(); |
141
|
|
|
$hasta = $this->getCaf()->getFolioHasta(); |
142
|
|
|
$this->foliosDisponibles = range($desde, $hasta); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|