@@ -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
0 commit comments