forked from NeoRazorX/facturascripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImpuesto.php
More file actions
261 lines (222 loc) · 8.24 KB
/
Impuesto.php
File metadata and controls
261 lines (222 loc) · 8.24 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
/**
* This file is part of FacturaScripts
* Copyright (C) 2013-2024 Carlos Garcia Gomez <carlos@facturascripts.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace FacturaScripts\Core\Model;
use FacturaScripts\Core\Base\DataBase\DataBaseWhere;
use FacturaScripts\Core\DataSrc\Impuestos;
use FacturaScripts\Core\Model\Base\ModelClass;
use FacturaScripts\Core\Model\Base\ModelTrait;
use FacturaScripts\Core\Tools;
use FacturaScripts\Dinamic\Model\Cuenta as DinCuenta;
use FacturaScripts\Dinamic\Model\Subcuenta as DinSubcuenta;
/**
* A tax (VAT) that can be associated to articles, delivery notes lines,
* invoices, etc.
*
* @author Carlos García Gómez <carlos@facturascripts.com>
*/
class Impuesto extends ModelClass
{
use ModelTrait;
const SPECIAL_TAX_IMPACTED_ACCOUNT = 'IVAREP';
const SPECIAL_TAX_SUPPORTED_ACCOUNT = 'IVASOP';
const TYPE_PERCENTAGE = 1;
const TYPE_FIXED_VALUE = 2;
/** @var bool */
public $activo;
/** @var string */
public $codimpuesto;
/**
* Código de la subcuenta de IVA repercutido.
* @var string
*/
public $codsubcuentarep;
/**
* Código de la subcuenta de recargo de equivalencia para el IVA repercutido.
* @var string
*/
public $codsubcuentarepre;
/**
* Código de la subcuenta de IVA soportado.
* @var string
*/
public $codsubcuentasop;
/**
* Código de la subcuenta de recargo de equivalencia para el IVA soportado.
* @var string
*/
public $codsubcuentasopre;
/** @var string */
public $descripcion;
/** @var string */
public $operacion;
/** @var int */
public $tipo;
/** @var float */
public $iva;
/** @var float */
public $recargo;
public function clear()
{
parent::clear();
$this->activo = true;
$this->iva = 0.0;
$this->recargo = 0.0;
$this->tipo = self::TYPE_PERCENTAGE;
}
public function delete(): bool
{
if ($this->isDefault()) {
Tools::log()->warning('cant-delete-default-tax');
return false;
}
if (false === parent::delete()) {
return false;
}
// limpiamos la caché
Impuestos::clear();
return true;
}
public function getInputSurchargeAccount(string $codejercicio): DinSubcuenta
{
// si tenemos una cuenta definida, la devolvemos
return $this->codsubcuentasopre ?
$this->getSubAccount($codejercicio, $this->codsubcuentasopre, static::SPECIAL_TAX_SUPPORTED_ACCOUNT) :
$this->getInputTaxAccount($codejercicio);
}
public function getInputTaxAccount(string $codejercicio): DinSubcuenta
{
// si tenemos una cuenta definida, la devolvemos
return $this->codsubcuentasop ?
$this->getSubAccount($codejercicio, $this->codsubcuentasop, static::SPECIAL_TAX_SUPPORTED_ACCOUNT) :
$this->getSpecialSubAccount($codejercicio, static::SPECIAL_TAX_SUPPORTED_ACCOUNT);
}
public function getOutputSurchargeAccount(string $codejercicio): DinSubcuenta
{
// si tenemos una cuenta definida, la devolvemos
return $this->codsubcuentarepre ?
$this->getSubAccount($codejercicio, $this->codsubcuentarepre, static::SPECIAL_TAX_IMPACTED_ACCOUNT) :
$this->getOutputTaxAccount($codejercicio);
}
public function getOutputTaxAccount(string $codejercicio): DinSubcuenta
{
// si tenemos una cuenta definida, la devolvemos
return $this->codsubcuentarep ?
$this->getSubAccount($codejercicio, $this->codsubcuentarep, static::SPECIAL_TAX_IMPACTED_ACCOUNT) :
$this->getSpecialSubAccount($codejercicio, static::SPECIAL_TAX_IMPACTED_ACCOUNT);
}
public function isDefault(): bool
{
return $this->codimpuesto === Tools::settings('default', 'codimpuesto');
}
public static function primaryColumn(): string
{
return 'codimpuesto';
}
public function save(): bool
{
if (false === parent::save()) {
return false;
}
// limpiamos la caché
Impuestos::clear();
return true;
}
public static function tableName(): string
{
return 'impuestos';
}
public function test(): bool
{
$this->codimpuesto = Tools::noHtml($this->codimpuesto);
if ($this->codimpuesto && 1 !== preg_match('/^[A-Z0-9_\+\.\-]{1,10}$/i', $this->codimpuesto)) {
Tools::log()->error(
'invalid-alphanumeric-code',
['%value%' => $this->codimpuesto, '%column%' => 'codimpuesto', '%min%' => '1', '%max%' => '10']
);
return false;
}
$this->codsubcuentarep = empty($this->codsubcuentarep) ? null : $this->codsubcuentarep;
$this->codsubcuentarepre = empty($this->codsubcuentarepre) ? null : $this->codsubcuentarepre;
$this->codsubcuentasop = empty($this->codsubcuentasop) ? null : $this->codsubcuentasop;
$this->codsubcuentasopre = empty($this->codsubcuentasopre) ? null : $this->codsubcuentasopre;
$this->descripcion = Tools::noHtml($this->descripcion);
return parent::test();
}
protected function getSpecialSubAccount(string $codejercicio, string $codcuentaesp): DinSubcuenta
{
// buscamos una subcuenta marcada con esa cuenta especial
$subcuenta = new DinSubcuenta();
$whereSubcuenta = [
new DataBaseWhere('codejercicio', $codejercicio),
new DataBaseWhere('codcuentaesp', $codcuentaesp),
];
if ($subcuenta->loadFromCode('', $whereSubcuenta)) {
return $subcuenta;
}
// no hay subcuenta especial, devolvemos la primera de la cuenta especial
$cuenta = new DinCuenta();
$whereCuenta = [
new DataBaseWhere('codejercicio', $codejercicio),
new DataBaseWhere('codcuentaesp', $codcuentaesp),
];
if ($cuenta->loadFromCode('', $whereCuenta)) {
foreach ($cuenta->getSubcuentas() as $subcuenta) {
return $subcuenta;
}
}
// no hemos encontrado la cuenta, la devolvemos vacía
return new DinSubcuenta();
}
protected function getSubAccount(string $codejercicio, string $codsubcuenta, string $codcuentaesp): DinSubcuenta
{
$subcuenta = new DinSubcuenta();
$whereSubcuenta = [
new DataBaseWhere('codejercicio', $codejercicio),
new DataBaseWhere('codsubcuenta', $codsubcuenta),
];
if ($subcuenta->loadFromCode('', $whereSubcuenta)) {
return $subcuenta;
}
// no hemos encontrado la subcuenta, la creamos, pero primero necesitamos la cuenta
$cuenta = new DinCuenta();
$whereCuenta = [
new DataBaseWhere('codejercicio', $codejercicio),
new DataBaseWhere('codcuentaesp', $codcuentaesp),
];
if ($cuenta->loadFromCode('', $whereCuenta)) {
// creamos la subcuenta
$subcuenta->codejercicio = $codejercicio;
$subcuenta->codcuenta = $cuenta->codcuenta;
$subcuenta->codsubcuenta = $codsubcuenta;
$subcuenta->descripcion = $this->descripcion;
$subcuenta->save();
return $subcuenta;
}
// no hemos encontrado la cuenta, la devolvemos vacía
return $subcuenta;
}
protected function saveInsert(array $values = []): bool
{
// si no se ha asignado un código, lo generamos
if (empty($this->codimpuesto)) {
$this->codimpuesto = (string)$this->newCode();
}
return parent::saveInsert($values);
}
}