|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is part of 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\Core\Lib; |
| 21 | + |
| 22 | +use FacturaScripts\Core\Base\DataBase; |
| 23 | +use FacturaScripts\Core\Lib\Accounting\BalanceRecalculator; |
| 24 | +use FacturaScripts\Core\Model\Asiento; |
| 25 | +use FacturaScripts\Core\Model\Cuenta; |
| 26 | +use FacturaScripts\Core\Model\Ejercicio; |
| 27 | +use FacturaScripts\Core\Model\Subcuenta; |
| 28 | +use FacturaScripts\Core\Tools; |
| 29 | +use FacturaScripts\Core\Where; |
| 30 | +use FacturaScripts\Test\Traits\DefaultSettingsTrait; |
| 31 | +use FacturaScripts\Test\Traits\LogErrorsTrait; |
| 32 | +use PHPUnit\Framework\TestCase; |
| 33 | + |
| 34 | +final class BalanceRecalculatorTest extends TestCase |
| 35 | +{ |
| 36 | + use DefaultSettingsTrait; |
| 37 | + use LogErrorsTrait; |
| 38 | + |
| 39 | + public static function setUpBeforeClass(): void |
| 40 | + { |
| 41 | + self::setDefaultSettings(); |
| 42 | + |
| 43 | + // nos aseguramos de que existe el ejercicio actual antes de instalar el plan contable |
| 44 | + $exercise = new Ejercicio(); |
| 45 | + $exercise->idempresa = Tools::settings('default', 'idempresa', 1); |
| 46 | + $exercise->loadFromDate(Tools::date()); |
| 47 | + |
| 48 | + self::installAccountingPlan(); |
| 49 | + self::removeTaxRegularization(); |
| 50 | + } |
| 51 | + |
| 52 | + public function testRecalculateSubaccountBalances(): void |
| 53 | + { |
| 54 | + // creamos un asiento con dos líneas |
| 55 | + $asiento = new Asiento(); |
| 56 | + $asiento->concepto = 'Test recalculo saldos'; |
| 57 | + $this->assertTrue($asiento->save(), 'asiento-cant-save'); |
| 58 | + |
| 59 | + $ejercicio = $asiento->getExercise(); |
| 60 | + $subcuenta1 = $this->getSampleSubaccount($ejercicio->codejercicio, 0); |
| 61 | + $subcuenta2 = $this->getSampleSubaccount($ejercicio->codejercicio, 1); |
| 62 | + $this->assertNotNull($subcuenta1, 'no-subaccount-1'); |
| 63 | + $this->assertNotNull($subcuenta2, 'no-subaccount-2'); |
| 64 | + |
| 65 | + $firstLine = $asiento->getNewLine(); |
| 66 | + $firstLine->codsubcuenta = $subcuenta1->codsubcuenta; |
| 67 | + $firstLine->concepto = 'Test linea 1'; |
| 68 | + $firstLine->debe = 100; |
| 69 | + $this->assertTrue($firstLine->save(), 'linea-cant-save-1'); |
| 70 | + |
| 71 | + $secondLine = $asiento->getNewLine(); |
| 72 | + $secondLine->codsubcuenta = $subcuenta2->codsubcuenta; |
| 73 | + $secondLine->concepto = 'Test linea 2'; |
| 74 | + $secondLine->haber = 100; |
| 75 | + $this->assertTrue($secondLine->save(), 'linea-cant-save-2'); |
| 76 | + |
| 77 | + // corrompemos los saldos en caché de las subcuentas |
| 78 | + $db = new DataBase(); |
| 79 | + $sql = 'UPDATE ' . Subcuenta::tableName() . ' SET debe = 999, haber = 888, saldo = 111' |
| 80 | + . ' WHERE idsubcuenta IN (' . $db->var2str($subcuenta1->idsubcuenta) |
| 81 | + . ', ' . $db->var2str($subcuenta2->idsubcuenta) . ')'; |
| 82 | + $this->assertTrue($db->exec($sql), 'cant-corrupt-subaccounts'); |
| 83 | + |
| 84 | + // recalculamos |
| 85 | + $this->assertTrue(BalanceRecalculator::run($ejercicio->codejercicio), 'recalculator-failed'); |
| 86 | + |
| 87 | + // comprobamos que los saldos vuelven a coincidir con las partidas |
| 88 | + $subcuenta1->load($subcuenta1->idsubcuenta); |
| 89 | + $this->assertEqualsWithDelta(100.0, $subcuenta1->debe, 0.001, 'sub1-debe-wrong'); |
| 90 | + $this->assertEqualsWithDelta(0.0, $subcuenta1->haber, 0.001, 'sub1-haber-wrong'); |
| 91 | + $this->assertEqualsWithDelta(100.0, $subcuenta1->saldo, 0.001, 'sub1-saldo-wrong'); |
| 92 | + |
| 93 | + $subcuenta2->load($subcuenta2->idsubcuenta); |
| 94 | + $this->assertEqualsWithDelta(0.0, $subcuenta2->debe, 0.001, 'sub2-debe-wrong'); |
| 95 | + $this->assertEqualsWithDelta(100.0, $subcuenta2->haber, 0.001, 'sub2-haber-wrong'); |
| 96 | + $this->assertEqualsWithDelta(-100.0, $subcuenta2->saldo, 0.001, 'sub2-saldo-wrong'); |
| 97 | + |
| 98 | + // eliminamos el asiento y comprobamos que el recálculo deja las subcuentas a cero |
| 99 | + $this->assertTrue($asiento->delete(), 'asiento-cant-delete'); |
| 100 | + $this->assertTrue(BalanceRecalculator::run($ejercicio->codejercicio), 'recalculator-failed-2'); |
| 101 | + |
| 102 | + $subcuenta1->load($subcuenta1->idsubcuenta); |
| 103 | + $this->assertEqualsWithDelta(0.0, $subcuenta1->debe, 0.001, 'sub1-debe-not-zero'); |
| 104 | + $this->assertEqualsWithDelta(0.0, $subcuenta1->saldo, 0.001, 'sub1-saldo-not-zero'); |
| 105 | + } |
| 106 | + |
| 107 | + public function testRecalculateAccountBalances(): void |
| 108 | + { |
| 109 | + // creamos un asiento con dos líneas |
| 110 | + $asiento = new Asiento(); |
| 111 | + $asiento->concepto = 'Test recalculo cuentas'; |
| 112 | + $this->assertTrue($asiento->save(), 'asiento-cant-save'); |
| 113 | + |
| 114 | + $ejercicio = $asiento->getExercise(); |
| 115 | + $subcuenta1 = $this->getSampleSubaccount($ejercicio->codejercicio, 0); |
| 116 | + $subcuenta2 = $this->getSampleSubaccount($ejercicio->codejercicio, 1); |
| 117 | + |
| 118 | + $firstLine = $asiento->getNewLine(); |
| 119 | + $firstLine->codsubcuenta = $subcuenta1->codsubcuenta; |
| 120 | + $firstLine->concepto = 'Test linea 1'; |
| 121 | + $firstLine->debe = 50; |
| 122 | + $this->assertTrue($firstLine->save(), 'linea-cant-save-1'); |
| 123 | + |
| 124 | + $secondLine = $asiento->getNewLine(); |
| 125 | + $secondLine->codsubcuenta = $subcuenta2->codsubcuenta; |
| 126 | + $secondLine->concepto = 'Test linea 2'; |
| 127 | + $secondLine->haber = 50; |
| 128 | + $this->assertTrue($secondLine->save(), 'linea-cant-save-2'); |
| 129 | + |
| 130 | + // corrompemos el saldo en caché de la cuenta de la primera subcuenta |
| 131 | + $db = new DataBase(); |
| 132 | + $sql = 'UPDATE ' . Cuenta::tableName() . ' SET debe = 777, haber = 666, saldo = 555' |
| 133 | + . ' WHERE idcuenta = ' . $db->var2str($subcuenta1->idcuenta); |
| 134 | + $this->assertTrue($db->exec($sql), 'cant-corrupt-account'); |
| 135 | + |
| 136 | + // recalculamos |
| 137 | + $this->assertTrue(BalanceRecalculator::run($ejercicio->codejercicio), 'recalculator-failed'); |
| 138 | + |
| 139 | + // la cuenta debe sumar los saldos de sus subcuentas |
| 140 | + $cuenta = new Cuenta(); |
| 141 | + $this->assertTrue($cuenta->load($subcuenta1->idcuenta), 'account-not-found'); |
| 142 | + |
| 143 | + $expectedDebe = 0.0; |
| 144 | + $expectedHaber = 0.0; |
| 145 | + foreach (Subcuenta::all([Where::eq('idcuenta', $cuenta->idcuenta)], [], 0, 0) as $sub) { |
| 146 | + $expectedDebe += $sub->debe; |
| 147 | + $expectedHaber += $sub->haber; |
| 148 | + } |
| 149 | + foreach ($cuenta->getChildren() as $child) { |
| 150 | + $expectedDebe += $child->debe; |
| 151 | + $expectedHaber += $child->haber; |
| 152 | + } |
| 153 | + $this->assertEqualsWithDelta($expectedDebe, $cuenta->debe, 0.001, 'account-debe-wrong'); |
| 154 | + $this->assertEqualsWithDelta($expectedHaber, $cuenta->haber, 0.001, 'account-haber-wrong'); |
| 155 | + $this->assertEqualsWithDelta($expectedDebe - $expectedHaber, $cuenta->saldo, 0.001, 'account-saldo-wrong'); |
| 156 | + |
| 157 | + // eliminamos |
| 158 | + $this->assertTrue($asiento->delete(), 'asiento-cant-delete'); |
| 159 | + } |
| 160 | + |
| 161 | + public function testUnusedSubaccountIsZeroed(): void |
| 162 | + { |
| 163 | + // necesitamos el ejercicio por defecto: lo obtenemos de un asiento temporal |
| 164 | + $asiento = new Asiento(); |
| 165 | + $asiento->concepto = 'Test subcuenta sin uso'; |
| 166 | + $this->assertTrue($asiento->save(), 'asiento-cant-save'); |
| 167 | + $codejercicio = $asiento->getExercise()->codejercicio; |
| 168 | + $this->assertTrue($asiento->delete(), 'asiento-cant-delete'); |
| 169 | + |
| 170 | + // buscamos una subcuenta sin partidas y le ponemos un saldo falso |
| 171 | + $subcuenta = $this->getSampleSubaccount($codejercicio, 2); |
| 172 | + $this->assertNotNull($subcuenta, 'no-subaccount'); |
| 173 | + |
| 174 | + $db = new DataBase(); |
| 175 | + $sql = 'UPDATE ' . Subcuenta::tableName() . ' SET debe = 123, haber = 45, saldo = 78' |
| 176 | + . ' WHERE idsubcuenta = ' . $db->var2str($subcuenta->idsubcuenta); |
| 177 | + $this->assertTrue($db->exec($sql), 'cant-corrupt-subaccount'); |
| 178 | + |
| 179 | + // recalculamos y comprobamos que vuelve a cero (no tiene partidas) |
| 180 | + $this->assertTrue(BalanceRecalculator::run($codejercicio), 'recalculator-failed'); |
| 181 | + |
| 182 | + $subcuenta->load($subcuenta->idsubcuenta); |
| 183 | + $this->assertEqualsWithDelta(0.0, $subcuenta->debe, 0.001, 'debe-not-zero'); |
| 184 | + $this->assertEqualsWithDelta(0.0, $subcuenta->haber, 0.001, 'haber-not-zero'); |
| 185 | + $this->assertEqualsWithDelta(0.0, $subcuenta->saldo, 0.001, 'saldo-not-zero'); |
| 186 | + } |
| 187 | + |
| 188 | + private function getSampleSubaccount(string $codejercicio, int $offset): ?Subcuenta |
| 189 | + { |
| 190 | + $where = [Where::eq('codejercicio', $codejercicio)]; |
| 191 | + foreach (Subcuenta::all($where, ['codsubcuenta' => 'ASC'], $offset, 1) as $item) { |
| 192 | + return $item; |
| 193 | + } |
| 194 | + |
| 195 | + return null; |
| 196 | + } |
| 197 | + |
| 198 | + protected function tearDown(): void |
| 199 | + { |
| 200 | + $this->logErrors(); |
| 201 | + } |
| 202 | +} |
0 commit comments