From 6d87cc433fb9dba2aebcde31772f586f19a8e4aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Cuello=20Principal?= Date: Wed, 4 Nov 2026 15:53:54 +0100 Subject: [PATCH 1/2] =?UTF-8?q?Mejoras=20en=20el=20Informe=20Gr=C3=A1ficos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Añadida columna saldo con el arrastrado por fila - Añadida traducción de los títulos de la columna con el nombre del campo seleccionado. --- View/Master/htmlChart.html.twig | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/View/Master/htmlChart.html.twig b/View/Master/htmlChart.html.twig index d3e85bc..bc256da 100644 --- a/View/Master/htmlChart.html.twig +++ b/View/Master/htmlChart.html.twig @@ -20,13 +20,15 @@ - - + + + {% for item in source %} {% set total = total + item.ycol %} + {% set totalClass = s_name != 'difference' ? '' : (total > 0 ? 'text-success' : (total < 0 ? 'text-danger' : '')) %} + {% else %} - + {% endfor %} - {% if total > 0 %} + {% if source is not empty %} - + + {% endif %} From c554aceeab35cc2baebdd7454d41d8570f70831d Mon Sep 17 00:00:00 2001 From: Carlos Garcia Date: Tue, 24 Nov 2026 19:53:12 +0100 Subject: [PATCH 2/2] =?UTF-8?q?fix(gr=C3=A1ficos):=20mostrar=20el=20saldo?= =?UTF-8?q?=20solo=20cuando=20tiene=20sentido=20y=20traducir=20las=20cabec?= =?UTF-8?q?eras?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Muestra la columna de saldo solo al sumar o contar valores agrupados por periodos consecutivos. - Traduce las cabeceras con el nombre de la columna sin el prefijo de la tabla e indica la operación del eje Y. - Usa las columnas de cada informe en las tablas del informe comparado y de la diferencia. - Colorea el total del pie en la tabla de diferencia y unifica el formato de los números. Co-Authored-By: Claude Opus 5.5 --- Model/Report.php | 74 +++++++++++++++++ Test/main/ReportTest.php | 143 ++++++++++++++++++++++++++++++++ View/Master/htmlChart.html.twig | 52 +++++++----- 3 files changed, 250 insertions(+), 19 deletions(-) diff --git a/Model/Report.php b/Model/Report.php index 526f153..82fa115 100644 --- a/Model/Report.php +++ b/Model/Report.php @@ -23,6 +23,7 @@ use FacturaScripts\Core\Template\ModelTrait; use FacturaScripts\Core\Tools; use FacturaScripts\Core\Where; +use FacturaScripts\Dinamic\Model\Report as DinReport; /** * Descripcion de un informe o gráfico configurable @@ -159,6 +160,20 @@ public function getCustomSql(): string return $this->customSql; } + /** + * Indica si tiene sentido mostrar el saldo acumulado de los valores del eje Y: + * solo cuando se suman o cuentan valores agrupados por periodos consecutivos. + */ + public function canShowBalance(): bool + { + $additive = in_array($this->yoperation ?? '', ['', 'SUM', 'COUNT', 'COUNT_DISTINCT'], true); + $sequential = in_array($this->xoperation ?? '', [ + 'DAY', 'WEEK', 'MONTHS', 'YEAR', 'UNIXTIME_DAY', 'UNIXTIME_WEEK', 'UNIXTIME_MONTH', 'UNIXTIME_YEAR' + ], true); + + return $additive && $sequential; + } + public function clear(): void { parent::clear(); @@ -176,6 +191,16 @@ public function getChart() return new $className($this); } + public function getComparedReport(): ?Report + { + if (empty($this->compared)) { + return null; + } + + $report = new DinReport(); + return $report->load($this->compared) ? $report : null; + } + public function getFiledXName(): string { return $this->fieldXName ?: $this->xcolumn; @@ -239,6 +264,41 @@ public function getTable(): string return $data[0] ?? ''; } + public function getXColumnLabel(): string + { + return empty($this->xcolumn) ? Tools::trans('x-column') : $this->getColumnLabel($this->xcolumn); + } + + public function getYColumnLabel(): string + { + if (empty($this->ycolumn)) { + return Tools::trans('count'); + } + + $operation = match ($this->yoperation ?? '') { + 'AVERAGE' => 'average', + 'COUNT', 'COUNT_DISTINCT' => 'count', + 'MAXIMUM' => 'maximum', + 'MINIMUM' => 'minimum', + default => 'sum', + }; + + return $this->getColumnLabel($this->ycolumn) . ' (' . mb_strtolower(Tools::trans($operation)) . ')'; + } + + /** + * Devuelve los decimales con los que mostrar los valores del eje Y, + * o null para usar los decimales predeterminados. + */ + public function getYDecimals(): ?int + { + if (empty($this->ycolumn) || in_array($this->yoperation, ['COUNT', 'COUNT_DISTINCT'], true)) { + return 0; + } + + return null; + } + public function primaryDescriptionColumn(): string { return 'name'; @@ -261,4 +321,18 @@ public function test(): bool $this->yoperation = Tools::noHtml($this->yoperation); return parent::test(); } + + /** + * Devuelve el nombre traducido de una columna, sin el prefijo de la tabla. + * Las expresiones SQL se devuelven tal cual. + */ + private function getColumnLabel(string $column): string + { + if (false === (bool)preg_match('/^(?:\w+\.)?(\w+)$/', $column, $matches)) { + return $column; + } + + $translation = Tools::trans($matches[1]); + return $translation === $matches[1] ? ucfirst($matches[1]) : $translation; + } } diff --git a/Test/main/ReportTest.php b/Test/main/ReportTest.php index 30c27c1..7b6b36d 100644 --- a/Test/main/ReportTest.php +++ b/Test/main/ReportTest.php @@ -19,6 +19,8 @@ namespace FacturaScripts\Test\Plugins; +use FacturaScripts\Core\Html; +use FacturaScripts\Core\Tools; use FacturaScripts\Plugins\Informes\Model\Report; use FacturaScripts\Test\Traits\LogErrorsTrait; use PHPUnit\Framework\TestCase; @@ -133,6 +135,147 @@ public function testDeleteReportDeletesFilters(): void } } + public function testCanShowBalance(): void + { + $report = new Report(); + $report->xcolumn = 'fecha'; + $report->ycolumn = 'total'; + + // con sumas o recuentos agrupados por periodos consecutivos se muestra el saldo + foreach (['', 'SUM', 'COUNT', 'COUNT_DISTINCT'] as $yOperation) { + foreach (['DAY', 'WEEK', 'MONTHS', 'YEAR', 'UNIXTIME_MONTH'] as $xOperation) { + $report->xoperation = $xOperation; + $report->yoperation = $yOperation; + $this->assertTrue($report->canShowBalance(), $xOperation . ' / ' . $yOperation); + } + } + + // con medias, máximos o mínimos no se muestra el saldo + $report->xoperation = 'MONTHS'; + foreach (['AVERAGE', 'MAXIMUM', 'MINIMUM'] as $yOperation) { + $report->yoperation = $yOperation; + $this->assertFalse($report->canShowBalance(), $yOperation); + } + + // con agrupaciones cíclicas, por categorías o sin agrupar no se muestra el saldo + $report->yoperation = 'SUM'; + foreach (['', 'HOUR', 'MONTH', 'HAS_A_VALUE', 'DOES_NOT_HAVE_A_VALUE'] as $xOperation) { + $report->xoperation = $xOperation; + $this->assertFalse($report->canShowBalance(), $xOperation); + } + } + + public function testColumnLabels(): void + { + $report = new Report(); + + // sin columnas se usan las etiquetas genéricas + $this->assertEquals(Tools::trans('x-column'), $report->getXColumnLabel()); + $this->assertEquals(Tools::trans('count'), $report->getYColumnLabel()); + + // se traduce el nombre de la columna sin el prefijo de la tabla + $report->xcolumn = 'facturascli.fecha'; + $fecha = Tools::trans('fecha') === 'fecha' ? 'Fecha' : Tools::trans('fecha'); + $this->assertEquals($fecha, $report->getXColumnLabel()); + + // la columna Y incluye la operación + $report->ycolumn = 'total'; + $total = Tools::trans('total') === 'total' ? 'Total' : Tools::trans('total'); + $this->assertEquals($total . ' (' . mb_strtolower(Tools::trans('sum')) . ')', $report->getYColumnLabel()); + + $report->yoperation = 'AVERAGE'; + $this->assertEquals($total . ' (' . mb_strtolower(Tools::trans('average')) . ')', $report->getYColumnLabel()); + + // las expresiones SQL se muestran tal cual + $report->xcolumn = 'COALESCE(codcliente, codproveedor)'; + $this->assertEquals('COALESCE(codcliente, codproveedor)', $report->getXColumnLabel()); + } + + public function testGetYDecimals(): void + { + $report = new Report(); + + // los recuentos no tienen decimales + $this->assertSame(0, $report->getYDecimals()); + + $report->ycolumn = 'total'; + $report->yoperation = 'COUNT'; + $this->assertSame(0, $report->getYDecimals()); + + // el resto de operaciones usa los decimales predeterminados + $report->yoperation = 'SUM'; + $this->assertNull($report->getYDecimals()); + } + + public function testGetComparedReport(): void + { + // sin informe comparado devuelve null + $report = new Report(); + $report->name = 'Test Report'; + $report->table = 'test_table'; + $this->assertNull($report->getComparedReport()); + + // creamos el informe comparado + $compared = new Report(); + $compared->name = 'Test Compared Report'; + $compared->table = 'test_table'; + $this->assertTrue($compared->save()); + + // comprobamos que se carga el informe comparado + $report->compared = $compared->id; + $comparedReport = $report->getComparedReport(); + $this->assertNotNull($comparedReport); + $this->assertEquals($compared->id, $comparedReport->id); + + // si no existe el informe comparado devuelve null + $this->assertTrue($compared->delete()); + $this->assertNull($report->getComparedReport()); + } + + public function testRenderChartTables(): void + { + // creamos un informe y otro comparado que cuentan los informes por mes + $compared = new Report(); + $compared->name = 'Test Chart Compared'; + $compared->table = 'reports'; + $compared->type = Report::DEFAULT_TYPE; + $compared->xcolumn = 'creationdate'; + $compared->xoperation = 'MONTHS'; + $compared->ycolumn = 'id'; + $compared->yoperation = 'COUNT'; + $this->assertTrue($compared->save()); + + $report = new Report(); + $report->name = 'Test Chart Main'; + $report->table = 'reports'; + $report->type = Report::DEFAULT_TYPE; + $report->xcolumn = 'creationdate'; + $report->xoperation = 'MONTHS'; + $report->ycolumn = 'id'; + $report->yoperation = 'COUNT'; + $report->compared = $compared->id; + $this->assertTrue($report->save()); + + // las tres tablas (informe, comparado y diferencia) muestran el saldo + $balanceHeader = ''; + $yHeader = ''; + $html = Html::render('Master/htmlChart.html.twig', ['fsc' => ['views' => [['model' => $report]]]]); + $this->assertEquals(3, substr_count($html, $balanceHeader)); + $this->assertEquals(3, substr_count($html, $yHeader)); + + // si el comparado usa otra operación, su tabla y la diferencia no muestran el saldo + $compared->yoperation = 'AVERAGE'; + $this->assertTrue($compared->save()); + $html = Html::render('Master/htmlChart.html.twig', ['fsc' => ['views' => [['model' => $report]]]]); + $this->assertEquals(1, substr_count($html, $balanceHeader)); + $this->assertEquals(1, substr_count($html, $yHeader)); + $this->assertStringContainsString('', $html); + + // eliminamos los informes + $this->assertTrue($report->delete()); + $this->assertTrue($compared->delete()); + } + protected function tearDown(): void { $this->logErrors(); diff --git a/View/Master/htmlChart.html.twig b/View/Master/htmlChart.html.twig index bc256da..efaa10c 100644 --- a/View/Master/htmlChart.html.twig +++ b/View/Master/htmlChart.html.twig @@ -4,6 +4,7 @@ {% set firstView = fsc.views | first %} {% set model = firstView.model %} {% set chart = model.getChart() %} +{% set compared = model.getComparedReport() %}
{{ chart.render() | raw }} @@ -11,7 +12,23 @@
{% for s_name, source in chart.getDataSources() %} + {% if s_name == 'difference' %} + {% set sameColumns = compared is not null + and compared.getXColumnLabel() == model.getXColumnLabel() + and compared.getYColumnLabel() == model.getYColumnLabel() %} + {% set xLabel = sameColumns ? model.getXColumnLabel() : trans('x-column') %} + {% set yLabel = sameColumns ? model.getYColumnLabel() : trans('y-column') %} + {% set decimals = sameColumns ? model.getYDecimals() : null %} + {% set showBalance = sameColumns and model.canShowBalance() %} + {% else %} + {% set report = compared is not null and s_name == compared.name and s_name != model.name ? compared : model %} + {% set xLabel = report.getXColumnLabel() %} + {% set yLabel = report.getYColumnLabel() %} + {% set decimals = report.getYDecimals() %} + {% set showBalance = report.canShowBalance() %} + {% endif %} {% set total = 0 %} + {% set totalClass = '' %}
@@ -20,33 +37,28 @@
{{ trans('x-column') }}{{ trans('y-column') }}{{ model.xcolumn | capitalize }}{{ model.ycolumn ? model.ycolumn | capitalize : trans('y-column') }}{{ trans('balance') }}
{{ item.xcol }} @@ -40,18 +42,20 @@ {{ item.ycol }} {% endif %} {{ number(total) }}
{{ trans('no-data') }}{{ trans('no-data') }}
{{ trans('total') }}{{ number(total) }}{{ number(total) }}
' . Tools::trans('balance') . '' . htmlspecialchars($report->getYColumnLabel()) . '' . Tools::trans('y-column') . '
- - - + + + {% if showBalance %} + + {% endif %} {% for item in source %} {% set total = total + item.ycol %} {% set totalClass = s_name != 'difference' ? '' : (total > 0 ? 'text-success' : (total < 0 ? 'text-danger' : '')) %} + {% set itemClass = s_name != 'difference' ? '' : (item.ycol > 0 ? 'text-success' : (item.ycol < 0 ? 'text-danger' : '')) %} - - + + {% if showBalance %} + + {% endif %} {% else %} - + {% endfor %} @@ -54,8 +66,10 @@ - - + + {% if showBalance %} + + {% endif %} {% endif %} @@ -63,4 +77,4 @@ {% endfor %} - \ No newline at end of file +
{{ model.xcolumn | capitalize }}{{ model.ycolumn ? model.ycolumn | capitalize : trans('y-column') }}{{ trans('balance') }}{{ xLabel }}{{ yLabel }}{{ trans('balance') }}
{{ item.xcol }} - {% if s_name != 'difference' %} - {{ item.ycol }} - {% elseif item.ycol > 0 %} - {{ item.ycol }} - {% elseif item.ycol < 0 %} - {{ item.ycol }} - {% else %} - {{ item.ycol }} - {% endif %} - {{ number(total) }}{{ number(item.ycol, decimals) }}{{ number(total, decimals) }}
{{ trans('no-data') }}{{ trans('no-data') }}
{{ trans('total') }}{{ number(total) }}{{ number(total, decimals) }}