@@ -79,71 +79,62 @@ protected function generarLibro(): void
7979 }
8080 }
8181
82- protected function libroIngresos ( ): void
82+ protected static function getExpenseBookBase ( array $ row ): float
8383 {
84- $ sql = "SELECT f.fecha, f.numero, f.codigo, f.cifnif, f.nombrecliente, "
85- . " f.observaciones, f.neto, f.totaliva, f.totalrecargo, f.total "
86- . " FROM facturascli f "
87- . " WHERE f.fecha >= " . $ this ->dataBase ->var2str ($ this ->desde )
88- . " AND f.fecha <= " . $ this ->dataBase ->var2str ($ this ->hasta )
89- . " AND f.idempresa = " . $ this ->dataBase ->var2str ($ this ->idempresa )
90- . " ORDER BY f.fecha ASC, CAST(f.numero AS UNSIGNED) ASC; " ;
91-
92- $ data = $ this ->dataBase ->select ($ sql );
93- if (empty ($ data )) {
94- Tools::log ()->warning ('no-data ' );
95- return ;
84+ $ base = (float )($ row ['line_baseimponible ' ] ?? 0 );
85+ if (self ::hasAmount ($ base )) {
86+ return $ base ;
9687 }
9788
98- $ this ->setTemplate (false );
99- header ("content-type:application/csv;charset=UTF-8 " );
100- $ filename = 'libro_ingresos_ ' . date ('Y-m-d_H-i-s ' ) . '.csv ' ;
101- header ('Content-Disposition: attachment; filename=" ' . $ filename . '" ' );
89+ // las rectificativas llevan el gasto al haber: lo tratamos como gasto negativo
90+ return (float )($ row ['debe ' ] ?? 0 ) - (float )($ row ['haber ' ] ?? 0 );
91+ }
10292
103- // Cabeceras del libro de ingresos
104- echo Tools::trans ('date ' ) . '; '
105- . Tools::trans ('invoice-number ' ) . '; '
106- . Tools::trans ('document ' ) . '; '
107- . 'NIF ' . '; '
108- . Tools::trans ('customer ' ) . '; '
109- . Tools::trans ('concept ' ) . '; '
110- . Tools::trans ('tax-base ' ) . '; '
111- . Tools::trans ('vat ' ) . '; '
112- . Tools::trans ('surcharge ' ) . '; '
113- . Tools::trans ('total ' ) . "\n" ;
93+ protected static function getExpenseBookLineAmounts (array $ row , float $ entryBaseTotal ): array
94+ {
95+ $ base = self ::getExpenseBookBase ($ row );
96+ $ invoiceTotal = (float )($ row ['invoice_total ' ] ?? 0 );
97+ $ invoiceVat = (float )($ row ['invoice_totaliva ' ] ?? 0 );
98+ $ invoiceSurcharge = (float )($ row ['invoice_totalrecargo ' ] ?? 0 );
11499
115- // Totales acumulados
116- $ totalNeto = 0 ;
117- $ totalIva = 0 ;
118- $ totalRecargo = 0 ;
119- $ totalGeneral = 0 ;
120- $ nfo = Tools::decimals ();
100+ if (self ::hasAmount ($ invoiceTotal ) || self ::hasAmount ($ invoiceVat ) || self ::hasAmount ($ invoiceSurcharge )) {
101+ $ ratio = self ::hasAmount ($ entryBaseTotal ) ? $ base / $ entryBaseTotal : 0.0 ;
102+ return [
103+ 'baseimponible ' => $ base ,
104+ 'iva ' => $ invoiceVat * $ ratio ,
105+ 'recargo ' => $ invoiceSurcharge * $ ratio ,
106+ 'gasto ' => $ invoiceTotal * $ ratio ,
107+ ];
108+ }
121109
122- // Líneas del libro
123- foreach ($ data as $ row ) {
124- echo $ row ['fecha ' ] . '; '
125- . $ row ['numero ' ] . '; '
126- . '" ' . $ row ['codigo ' ] . '"; '
127- . '" ' . $ row ['cifnif ' ] . '"; '
128- . '" ' . Tools::fixHtml ($ row ['nombrecliente ' ]) . '"; '
129- . '" ' . Tools::fixHtml ($ row ['observaciones ' ]) . '"; '
130- . number_format ($ row ['neto ' ], $ nfo , ', ' , '' ) . '; '
131- . number_format ($ row ['totaliva ' ], $ nfo , ', ' , '' ) . '; '
132- . number_format ($ row ['totalrecargo ' ], $ nfo , ', ' , '' ) . '; '
133- . number_format ($ row ['total ' ], $ nfo , ', ' , '' ) . "\n" ;
110+ $ iva = $ base * (float )($ row ['line_iva ' ] ?? 0 ) / 100 ;
111+ $ recargo = $ base * (float )($ row ['line_recargo ' ] ?? 0 ) / 100 ;
112+ return [
113+ 'baseimponible ' => $ base ,
114+ 'iva ' => $ iva ,
115+ 'recargo ' => $ recargo ,
116+ 'gasto ' => self ::hasAmount ($ iva ) || self ::hasAmount ($ recargo ) ? $ base + $ iva + $ recargo : 0.0 ,
117+ ];
118+ }
134119
135- $ totalNeto += $ row ['neto ' ];
136- $ totalIva += $ row ['totaliva ' ];
137- $ totalRecargo += $ row ['totalrecargo ' ];
138- $ totalGeneral += $ row ['total ' ];
139- }
120+ protected static function hasAmount (float $ amount ): bool
121+ {
122+ return abs ($ amount ) > 0.00001 ;
123+ }
140124
141- // Línea de totales
142- echo "\n" . strtoupper (Tools::trans ('totals ' )) . ';;;;;; '
143- . number_format ($ totalNeto , $ nfo , ', ' , '' ) . '; '
144- . number_format ($ totalIva , $ nfo , ', ' , '' ) . '; '
145- . number_format ($ totalRecargo , $ nfo , ', ' , '' ) . '; '
146- . number_format ($ totalGeneral , $ nfo , ', ' , '' ) . "\n" ;
125+ protected function iniFilters (): void
126+ {
127+ $ this ->desde = $ this ->request ->get ('desde ' , date ('Y ' ) . '-01-01 ' );
128+ $ this ->hasta = $ this ->request ->get ('hasta ' , date ('Y ' ) . '-12-31 ' );
129+ $ this ->idempresa = $ this ->request ->get ('idempresa ' , $ this ->user ->idempresa );
130+
131+ // Validar que la fecha desde sea anterior o igual a la fecha hasta
132+ if (strtotime ($ this ->desde ) > strtotime ($ this ->hasta )) {
133+ Tools::log ()->warning ('start-date-later-end-date ' );
134+ $ temp = $ this ->desde ;
135+ $ this ->desde = $ this ->hasta ;
136+ $ this ->hasta = $ temp ;
137+ }
147138 }
148139
149140 protected function libroGastos (): void
@@ -174,7 +165,7 @@ protected function libroGastos(): void
174165 . " AND (a.operacion IS NULL OR a.operacion NOT IN ( "
175166 . $ this ->dataBase ->var2str (Asiento::OPERATION_REGULARIZATION ) . ", "
176167 . $ this ->dataBase ->var2str (Asiento::OPERATION_CLOSING ) . ")) "
177- . " ORDER BY a.fecha ASC, CAST( a.numero AS UNSIGNED) ASC, p.codsubcuenta ASC; " ;
168+ . " ORDER BY a.fecha ASC, " . $ this -> dataBase -> castInteger ( ' a.numero ' ) . " ASC, p.codsubcuenta ASC; " ;
178169
179170 $ data = $ this ->dataBase ->select ($ sql );
180171 if (empty ($ data )) {
@@ -243,61 +234,70 @@ protected function libroGastos(): void
243234 . number_format ($ totalGasto , $ nfo , ', ' , '' ) . "\n" ;
244235 }
245236
246- protected static function getExpenseBookBase (array $ row ): float
247- {
248- $ base = (float )($ row ['line_baseimponible ' ] ?? 0 );
249- if (self ::hasAmount ($ base )) {
250- return $ base ;
251- }
252-
253- // las rectificativas llevan el gasto al haber: lo tratamos como gasto negativo
254- return (float )($ row ['debe ' ] ?? 0 ) - (float )($ row ['haber ' ] ?? 0 );
255- }
256-
257- protected static function getExpenseBookLineAmounts (array $ row , float $ entryBaseTotal ): array
237+ protected function libroIngresos (): void
258238 {
259- $ base = self ::getExpenseBookBase ($ row );
260- $ invoiceTotal = (float )($ row ['invoice_total ' ] ?? 0 );
261- $ invoiceVat = (float )($ row ['invoice_totaliva ' ] ?? 0 );
262- $ invoiceSurcharge = (float )($ row ['invoice_totalrecargo ' ] ?? 0 );
239+ $ sql = "SELECT f.fecha, f.numero, f.codigo, f.cifnif, f.nombrecliente, "
240+ . " f.observaciones, f.neto, f.totaliva, f.totalrecargo, f.total "
241+ . " FROM facturascli f "
242+ . " WHERE f.fecha >= " . $ this ->dataBase ->var2str ($ this ->desde )
243+ . " AND f.fecha <= " . $ this ->dataBase ->var2str ($ this ->hasta )
244+ . " AND f.idempresa = " . $ this ->dataBase ->var2str ($ this ->idempresa )
245+ . " ORDER BY f.fecha ASC, " . $ this ->dataBase ->castInteger ('f.numero ' ) . " ASC; " ;
263246
264- if (self ::hasAmount ($ invoiceTotal ) || self ::hasAmount ($ invoiceVat ) || self ::hasAmount ($ invoiceSurcharge )) {
265- $ ratio = self ::hasAmount ($ entryBaseTotal ) ? $ base / $ entryBaseTotal : 0.0 ;
266- return [
267- 'baseimponible ' => $ base ,
268- 'iva ' => $ invoiceVat * $ ratio ,
269- 'recargo ' => $ invoiceSurcharge * $ ratio ,
270- 'gasto ' => $ invoiceTotal * $ ratio ,
271- ];
247+ $ data = $ this ->dataBase ->select ($ sql );
248+ if (empty ($ data )) {
249+ Tools::log ()->warning ('no-data ' );
250+ return ;
272251 }
273252
274- $ iva = $ base * (float )($ row ['line_iva ' ] ?? 0 ) / 100 ;
275- $ recargo = $ base * (float )($ row ['line_recargo ' ] ?? 0 ) / 100 ;
276- return [
277- 'baseimponible ' => $ base ,
278- 'iva ' => $ iva ,
279- 'recargo ' => $ recargo ,
280- 'gasto ' => self ::hasAmount ($ iva ) || self ::hasAmount ($ recargo ) ? $ base + $ iva + $ recargo : 0.0 ,
281- ];
282- }
253+ $ this ->setTemplate (false );
254+ header ("content-type:application/csv;charset=UTF-8 " );
255+ $ filename = 'libro_ingresos_ ' . date ('Y-m-d_H-i-s ' ) . '.csv ' ;
256+ header ('Content-Disposition: attachment; filename=" ' . $ filename . '" ' );
283257
284- protected static function hasAmount (float $ amount ): bool
285- {
286- return abs ($ amount ) > 0.00001 ;
287- }
258+ // Cabeceras del libro de ingresos
259+ echo Tools::trans ('date ' ) . '; '
260+ . Tools::trans ('invoice-number ' ) . '; '
261+ . Tools::trans ('document ' ) . '; '
262+ . 'NIF ' . '; '
263+ . Tools::trans ('customer ' ) . '; '
264+ . Tools::trans ('concept ' ) . '; '
265+ . Tools::trans ('tax-base ' ) . '; '
266+ . Tools::trans ('vat ' ) . '; '
267+ . Tools::trans ('surcharge ' ) . '; '
268+ . Tools::trans ('total ' ) . "\n" ;
288269
289- protected function iniFilters (): void
290- {
291- $ this ->desde = $ this ->request ->get ('desde ' , date ('Y ' ) . '-01-01 ' );
292- $ this ->hasta = $ this ->request ->get ('hasta ' , date ('Y ' ) . '-12-31 ' );
293- $ this ->idempresa = $ this ->request ->get ('idempresa ' , $ this ->user ->idempresa );
270+ // Totales acumulados
271+ $ totalNeto = 0 ;
272+ $ totalIva = 0 ;
273+ $ totalRecargo = 0 ;
274+ $ totalGeneral = 0 ;
275+ $ nfo = Tools::decimals ();
294276
295- // Validar que la fecha desde sea anterior o igual a la fecha hasta
296- if (strtotime ($ this ->desde ) > strtotime ($ this ->hasta )) {
297- Tools::log ()->warning ('start-date-later-end-date ' );
298- $ temp = $ this ->desde ;
299- $ this ->desde = $ this ->hasta ;
300- $ this ->hasta = $ temp ;
277+ // Líneas del libro
278+ foreach ($ data as $ row ) {
279+ echo $ row ['fecha ' ] . '; '
280+ . $ row ['numero ' ] . '; '
281+ . '" ' . $ row ['codigo ' ] . '"; '
282+ . '" ' . $ row ['cifnif ' ] . '"; '
283+ . '" ' . Tools::fixHtml ($ row ['nombrecliente ' ]) . '"; '
284+ . '" ' . Tools::fixHtml ($ row ['observaciones ' ]) . '"; '
285+ . number_format ($ row ['neto ' ], $ nfo , ', ' , '' ) . '; '
286+ . number_format ($ row ['totaliva ' ], $ nfo , ', ' , '' ) . '; '
287+ . number_format ($ row ['totalrecargo ' ], $ nfo , ', ' , '' ) . '; '
288+ . number_format ($ row ['total ' ], $ nfo , ', ' , '' ) . "\n" ;
289+
290+ $ totalNeto += $ row ['neto ' ];
291+ $ totalIva += $ row ['totaliva ' ];
292+ $ totalRecargo += $ row ['totalrecargo ' ];
293+ $ totalGeneral += $ row ['total ' ];
301294 }
295+
296+ // Línea de totales
297+ echo "\n" . strtoupper (Tools::trans ('totals ' )) . ';;;;;; '
298+ . number_format ($ totalNeto , $ nfo , ', ' , '' ) . '; '
299+ . number_format ($ totalIva , $ nfo , ', ' , '' ) . '; '
300+ . number_format ($ totalRecargo , $ nfo , ', ' , '' ) . '; '
301+ . number_format ($ totalGeneral , $ nfo , ', ' , '' ) . "\n" ;
302302 }
303303}
0 commit comments