11<?php
22/**
33 * This file is part of Modelo303 plugin for FacturaScripts
4- * Copyright (C) 2017-2025 Carlos Garcia Gomez <carlos@facturascripts.com>
4+ * Copyright (C) 2017-2026 Carlos Garcia Gomez <carlos@facturascripts.com>
55 *
66 * This program is free software: you can redistribute it and/or modify
77 * it under the terms of the GNU Lesser General Public License as
1616 * You should have received a copy of the GNU Lesser General Public License
1717 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1818 */
19+
1920namespace FacturaScripts \Plugins \Modelo303 \Lib ;
2021
21- use FacturaScripts \Core \DataSrc \ Impuestos ;
22+ use FacturaScripts \Core \Base \ DataBase ;
2223use FacturaScripts \Core \Tools ;
23- use FacturaScripts \Plugins \Modelo303 \Model \Join \PartidaImpuestoResumen ;
24+ use FacturaScripts \Dinamic \Model \Ejercicio ;
25+ use FacturaScripts \Dinamic \Model \Join \PartidaImpuestoResumen ;
2426
2527/**
2628 * Class to handle Modelo 303 tax form data.
@@ -46,8 +48,8 @@ class Modelo303
4648 private array $ casillaMap = [
4749 /*
4850 * IVA devengado (repercutido).
51+ * Ventas nacionales (régimen general)
4952 */
50- // Ventas nacionales (régimen general)
5153 'IVAREP ' => [
5254 '2 ' => ['base ' => '165 ' , 'cuota ' => '167 ' ],
5355 '4 ' => ['base ' => '01 ' , 'cuota ' => '03 ' ],
@@ -76,8 +78,8 @@ class Modelo303
7678
7779 /*
7880 * IVA soportado (deducible)
81+ * Compras nacionales (régimen general)
7982 */
80- // Compras nacionales (régimen general)
8183 'IVASOP ' => [
8284 '21 ' => ['base ' => '28 ' , 'cuota ' => '29 ' ],
8385 '10 ' => ['base ' => '28 ' , 'cuota ' => '29 ' ],
@@ -161,6 +163,25 @@ public function loadFromResumen(array $resumen): void
161163 $ this ->calculateTotals ();
162164 }
163165
166+ public static function treasury (string $ codejercicio , string $ period ): array
167+ {
168+ // comprobamos que el ejercicio existe
169+ $ exercise = new Ejercicio ();
170+ if (false === $ exercise ->load ($ codejercicio )) {
171+ return [];
172+ }
173+
174+ $ dataBase = new DataBase ();
175+ $ period = strtoupper ($ period );
176+ list ($ dateStart , $ dateEnd ) = static ::treasuryDates ($ exercise , $ period );
177+
178+ return [
179+ 'iva-repercutido ' => static ::treasurySaldoCuenta ('477% ' , $ dateStart , $ dateEnd , $ dataBase ),
180+ 'iva-soportado ' => static ::treasurySaldoCuenta ('472% ' , $ dateStart , $ dateEnd , $ dataBase ),
181+ 'iva-devolver ' => static ::treasurySaldoCuenta ('4700% ' , $ dateStart , $ dateEnd , $ dataBase ),
182+ ];
183+ }
184+
164185 /**
165186 * Add a tax movement to the model (base + quota by type and rate)
166187 * - Determine the correct square based on the type and tax rate.
@@ -239,4 +260,54 @@ private function calculateTotals(): void
239260 // Resultado régimen general
240261 $ this ->square ['46 ' ] = $ this ->square ['27 ' ] - $ this ->square ['45 ' ];
241262 }
263+
264+ protected static function treasurySaldoCuenta (string $ cuenta , string $ desde , string $ hasta , DataBase $ dataBase ): float
265+ {
266+ $ saldo = 0.0 ;
267+
268+ if ($ dataBase ->tableExists ('partidas ' )) {
269+ // calculamos el saldo de todos aquellos asientos que afecten a caja
270+ $ sql = "select sum(debe-haber) as total from partidas where codsubcuenta LIKE " . $ dataBase ->var2str ($ cuenta )
271+ . " and idasiento in (select idasiento from asientos where fecha >= " . $ dataBase ->var2str ($ desde )
272+ . " and fecha <= " . $ dataBase ->var2str ($ hasta ) . "); " ;
273+
274+ $ data = $ dataBase ->select ($ sql );
275+ if ($ data && $ data [0 ]['total ' ] !== null ) {
276+ $ saldo = floatval ($ data [0 ]['total ' ]);
277+ }
278+ }
279+
280+ return $ saldo ;
281+ }
282+
283+ protected static function treasuryDates (Ejercicio $ exercise , string $ period ): array
284+ {
285+ // si el periodo no es T1, T2, T3, T4 o Annual, se asume que es el primer trimestre
286+ if (!in_array ($ period , ['T1 ' , 'T2 ' , 'T3 ' , 'T4 ' , 'ANNUAL ' ])) {
287+ $ period = 'T1 ' ;
288+ }
289+
290+ return match ($ period ) {
291+ 'T1 ' => [
292+ date ('01-01-Y ' , strtotime ($ exercise ->fechainicio )),
293+ date ('31-03-Y ' , strtotime ($ exercise ->fechainicio ))
294+ ],
295+ 'T2 ' => [
296+ date ('01-04-Y ' , strtotime ($ exercise ->fechainicio )),
297+ date ('30-06-Y ' , strtotime ($ exercise ->fechainicio ))
298+ ],
299+ 'T3 ' => [
300+ date ('01-07-Y ' , strtotime ($ exercise ->fechainicio )),
301+ date ('30-09-Y ' , strtotime ($ exercise ->fechainicio ))
302+ ],
303+ 'ANNUAL ' => [
304+ date ('01-01-Y ' , strtotime ($ exercise ->fechainicio )),
305+ date ('31-12-Y ' , strtotime ($ exercise ->fechainicio ))
306+ ],
307+ default => [
308+ date ('01-10-Y ' , strtotime ($ exercise ->fechainicio )),
309+ date ('31-12-Y ' , strtotime ($ exercise ->fechainicio ))
310+ ],
311+ };
312+ }
242313}
0 commit comments