Skip to content

Commit 6b76c07

Browse files
committed
Ajusta el libro de gastos en ReportBooks
1 parent aa02859 commit 6b76c07

2 files changed

Lines changed: 155 additions & 15 deletions

File tree

Controller/ReportBooks.php

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,18 @@ protected function libroIngresos(): void
148148
protected function libroGastos(): void
149149
{
150150
$sql = "SELECT a.fecha, a.numero,"
151+
. " a.idasiento,"
151152
. " COALESCE(f.numproveedor, p.documento) as documento,"
152153
. " p.codsubcuenta,"
153154
. " COALESCE(f.nombre, p.concepto) as concepto,"
154155
. " COALESCE(f.cifnif, p.cifnif) as cifnif,"
155-
. " COALESCE(NULLIF(p.baseimponible, 0), p.debe) as baseimponible,"
156-
. " COALESCE(NULLIF(p.baseimponible, 0) * COALESCE(p.iva, 0) / 100, f.totaliva, 0) as iva,"
157-
. " COALESCE(NULLIF(p.baseimponible, 0) * COALESCE(p.recargo, 0) / 100, f.totalrecargo, 0) as recargo,"
158-
. " COALESCE(NULLIF(p.baseimponible, 0) * (1 + COALESCE(p.iva, 0) / 100 + COALESCE(p.recargo, 0) / 100), f.total, p.debe) as total"
156+
. " COALESCE(p.baseimponible, 0) as line_baseimponible,"
157+
. " COALESCE(p.iva, 0) as line_iva,"
158+
. " COALESCE(p.recargo, 0) as line_recargo,"
159+
. " p.debe,"
160+
. " COALESCE(f.totaliva, 0) as invoice_totaliva,"
161+
. " COALESCE(f.totalrecargo, 0) as invoice_totalrecargo,"
162+
. " COALESCE(f.total, 0) as invoice_total"
159163
. " FROM asientos a"
160164
. " INNER JOIN partidas p ON a.idasiento = p.idasiento"
161165
. " LEFT JOIN facturasprov f ON a.idasiento = f.idasiento"
@@ -187,40 +191,88 @@ protected function libroGastos(): void
187191
. Tools::trans('tax-base') . ';'
188192
. Tools::trans('vat') . ';'
189193
. Tools::trans('surcharge') . ';'
190-
. Tools::trans('total') . "\n";
194+
. 'Gasto' . "\n";
191195

192196
// Totales acumulados
193197
$totalBase = 0;
194198
$totalIva = 0;
195199
$totalRecargo = 0;
196-
$totalGeneral = 0;
200+
$totalGasto = 0;
197201
$nfo = Tools::decimals();
202+
$entryBaseTotals = [];
203+
204+
foreach ($data as $row) {
205+
$entryBaseTotals[$row['idasiento']] = ($entryBaseTotals[$row['idasiento']] ?? 0.0)
206+
+ self::getExpenseBookBase($row);
207+
}
198208

199209
// Líneas del libro
200210
foreach ($data as $row) {
211+
$amounts = self::getExpenseBookLineAmounts(
212+
$row,
213+
$entryBaseTotals[$row['idasiento']] ?? 0.0
214+
);
201215
echo $row['fecha'] . ';'
202216
. $row['numero'] . ';'
203217
. '"' . $row['documento'] . '";'
204218
. '"' . $row['codsubcuenta'] . '";'
205219
. '"' . Tools::fixHtml($row['concepto']) . '";'
206220
. '"' . $row['cifnif'] . '";'
207-
. number_format($row['baseimponible'], $nfo, ',', '') . ';'
208-
. number_format($row['iva'], $nfo, ',', '') . ';'
209-
. number_format($row['recargo'], $nfo, ',', '') . ';'
210-
. number_format($row['total'], $nfo, ',', '') . "\n";
221+
. number_format($amounts['baseimponible'], $nfo, ',', '') . ';'
222+
. number_format($amounts['iva'], $nfo, ',', '') . ';'
223+
. number_format($amounts['recargo'], $nfo, ',', '') . ';'
224+
. number_format($amounts['gasto'], $nfo, ',', '') . "\n";
211225

212-
$totalBase += $row['baseimponible'];
213-
$totalIva += $row['iva'];
214-
$totalRecargo += $row['recargo'];
215-
$totalGeneral += $row['total'];
226+
$totalBase += $amounts['baseimponible'];
227+
$totalIva += $amounts['iva'];
228+
$totalRecargo += $amounts['recargo'];
229+
$totalGasto += $amounts['gasto'];
216230
}
217231

218232
// Línea de totales
219233
echo "\n" . strtoupper(Tools::trans('totals')) . ';;;;;;'
220234
. number_format($totalBase, $nfo, ',', '') . ';'
221235
. number_format($totalIva, $nfo, ',', '') . ';'
222236
. number_format($totalRecargo, $nfo, ',', '') . ';'
223-
. number_format($totalGeneral, $nfo, ',', '') . "\n";
237+
. number_format($totalGasto, $nfo, ',', '') . "\n";
238+
}
239+
240+
protected static function getExpenseBookBase(array $row): float
241+
{
242+
$base = (float)($row['line_baseimponible'] ?? 0);
243+
return self::hasAmount($base) ? $base : (float)($row['debe'] ?? 0);
244+
}
245+
246+
protected static function getExpenseBookLineAmounts(array $row, float $entryBaseTotal): array
247+
{
248+
$base = self::getExpenseBookBase($row);
249+
$invoiceTotal = (float)($row['invoice_total'] ?? 0);
250+
$invoiceVat = (float)($row['invoice_totaliva'] ?? 0);
251+
$invoiceSurcharge = (float)($row['invoice_totalrecargo'] ?? 0);
252+
253+
if (self::hasAmount($invoiceTotal) || self::hasAmount($invoiceVat) || self::hasAmount($invoiceSurcharge)) {
254+
$ratio = self::hasAmount($entryBaseTotal) ? $base / $entryBaseTotal : 0.0;
255+
return [
256+
'baseimponible' => $base,
257+
'iva' => $invoiceVat * $ratio,
258+
'recargo' => $invoiceSurcharge * $ratio,
259+
'gasto' => $invoiceTotal * $ratio,
260+
];
261+
}
262+
263+
$iva = $base * (float)($row['line_iva'] ?? 0) / 100;
264+
$recargo = $base * (float)($row['line_recargo'] ?? 0) / 100;
265+
return [
266+
'baseimponible' => $base,
267+
'iva' => $iva,
268+
'recargo' => $recargo,
269+
'gasto' => self::hasAmount($iva) || self::hasAmount($recargo) ? $base + $iva + $recargo : 0.0,
270+
];
271+
}
272+
273+
protected static function hasAmount(float $amount): bool
274+
{
275+
return abs($amount) > 0.00001;
224276
}
225277

226278
protected function iniFilters(): void

Test/main/ReportBooksTest.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
/**
3+
* This file is part of Informes plugin for FacturaScripts
4+
* Copyright (C) 2026 Carlos Garcia Gomez <carlos@facturascripts.com>
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Lesser General Public License as
8+
* published by the Free Software Foundation, either version 3 of the
9+
* License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
namespace FacturaScripts\Test\Plugins;
21+
22+
use FacturaScripts\Plugins\Informes\Controller\ReportBooks;
23+
use PHPUnit\Framework\TestCase;
24+
25+
final class ReportBooksTest extends TestCase
26+
{
27+
public function testExpenseWithoutInvoiceStaysInTaxBase(): void
28+
{
29+
$amounts = ReportBooksTestAccess::getLineAmounts([
30+
'line_baseimponible' => 0,
31+
'line_iva' => 0,
32+
'line_recargo' => 0,
33+
'debe' => 1540.25,
34+
'invoice_total' => 0,
35+
'invoice_totaliva' => 0,
36+
'invoice_totalrecargo' => 0,
37+
], 1540.25);
38+
39+
$this->assertEquals(1540.25, $amounts['baseimponible']);
40+
$this->assertEquals(0.0, $amounts['iva']);
41+
$this->assertEquals(0.0, $amounts['recargo']);
42+
$this->assertEquals(0.0, $amounts['gasto']);
43+
}
44+
45+
public function testInvoiceAmountsAreProratedByExpenseBase(): void
46+
{
47+
$amounts = ReportBooksTestAccess::getLineAmounts([
48+
'line_baseimponible' => 0,
49+
'line_iva' => 0,
50+
'line_recargo' => 0,
51+
'debe' => 60,
52+
'invoice_total' => 121,
53+
'invoice_totaliva' => 21,
54+
'invoice_totalrecargo' => 0,
55+
], 100);
56+
57+
$this->assertEquals(60.0, $amounts['baseimponible']);
58+
$this->assertEquals(12.6, $amounts['iva']);
59+
$this->assertEquals(0.0, $amounts['recargo']);
60+
$this->assertEquals(72.6, $amounts['gasto']);
61+
}
62+
63+
public function testManualVatLineKeepsComputedTotal(): void
64+
{
65+
$amounts = ReportBooksTestAccess::getLineAmounts([
66+
'line_baseimponible' => 100,
67+
'line_iva' => 21,
68+
'line_recargo' => 5.2,
69+
'debe' => 100,
70+
'invoice_total' => 0,
71+
'invoice_totaliva' => 0,
72+
'invoice_totalrecargo' => 0,
73+
], 100);
74+
75+
$this->assertEquals(100.0, $amounts['baseimponible']);
76+
$this->assertEquals(21.0, $amounts['iva']);
77+
$this->assertEquals(5.2, $amounts['recargo']);
78+
$this->assertEquals(126.2, $amounts['gasto']);
79+
}
80+
}
81+
82+
class ReportBooksTestAccess extends ReportBooks
83+
{
84+
public static function getLineAmounts(array $row, float $entryBaseTotal): array
85+
{
86+
return parent::getExpenseBookLineAmounts($row, $entryBaseTotal);
87+
}
88+
}

0 commit comments

Comments
 (0)