QoL 81: Profit and Loss and Tax Collected charts follow the Monthly and Quarterly switch instead of always showing months

This commit is contained in:
johnnyq
2026-11-22 13:14:21 -05:00
parent 243a939dd2
commit e76594b054
4 changed files with 40 additions and 18 deletions

View File

@@ -28,6 +28,7 @@ Update from Maintenance > Update — Queue Update hands the job to cron, which u
- Pickers: as a page loaded, a picker's "nothing" choice showed as "- Anyone -" for a moment before it became "Anyone" - on the tickets list's filters and in pickers across the app and the client portal. It is "Anyone", in its quieter grey, from the start.
- Lists: the date filter box - on tickets, invoices, quotes, expenses, the audit log and the rest - was empty as the page loaded, then filled in with All Time or its dates a moment later. It says so from the start.
- Lists: the tabs along the top of the lists - Workstations, Network and Other on the assets list, a ticket's status, Admin > Users and the rest - and the tabs on an asset's, contact's or other record's page didn't change under the pointer. One that isn't picked now darkens its text when you point at it or tab to it.
- Reports: switching Profit & Loss or Tax Collected to Quarterly changed the table but not the chart, which stayed by month - and Tax Collected opens on Quarterly. The chart now goes by quarter or month as the table does; this year's quarter under way shows what it has so far.
### Developer Updates
@@ -45,6 +46,7 @@ Update from Maintenance > Update — Queue Update hands the job to cron, which u
- `rmmAlertsCleanup()`, from `cron/nightly_tasks.php`, deletes alerts cleared more than `RMM_ALERTS_KEEP_DAYS` (180) ago. Only open ones are read, so the minute's poll no longer scans every alert there has been.
- `js/itflow_select_early.js`, loaded in the `<head>` of `includes/header.php`, `client/includes/header.php`, `agent/client_onboarding.php`, `admin/getting_started.php` and `setup/index.php` (a file, as the portal allows no inline scripts), watches the page as it is parsed and takes the dashes off a `- None -` option of a `select.select2`, marking it `data-none="1"` - before the first paint, so the raw `<select>` shown until `js/itflow_select.js` runs in the footer already reads the same. It stops at `DOMContentLoaded`. `js/itflow_select.js` treats `data-none="1"` as the "nothing" choice alongside an empty value and the dashed label, and `css/itflow_custom.css` greys a single select showing it until the picker takes over (`:has(option:checked[data-none="1"])`).
- `dateFilterLabel($dtf, $dtt)` in `functions/format.php` gives what `js/date_filter.js` shows for a range - `All Time` for 1970-01-01 to 2099-12-31 or no dates, otherwise `Y-m-d — Y-m-d`, a date that isn't one taken as no bound. The 18 pages with a `#dateFilter` write it as the box's `value`, from the same expressions as their hidden `dtf` and `dtt`. `js/date_filter.js` empties the box before it starts Flatpickr, which would otherwise try to read those words as dates, and `setDisplay()` writes them back in the same step.
- `reportChartColumns($months, $columns, $months_shown)` in `functions/reports.php` sums a year's monthly amounts into `reportYearColumns()`'s columns for a chart, null for a column after `$months_shown` and the one under way summed as far as it has got. `agent/reports/profit_loss.php` and `agent/reports/tax_summary.php` use it, with the columns' labels, so their charts follow the Monthly / Quarterly switch.
## [26.12]

View File

@@ -102,16 +102,16 @@ if (reportExporting()) {
reportCsv("Profit and Loss $year", $csv_columns, $csv_rows);
}
// The chart goes a month at a time, whichever way the table is split
// The chart goes by month or quarter, as the table does - nothing for the months still to come
$months_shown = $current_year ? intval(date('n')) : 12;
$chart = ['income' => [], 'expenses' => [], 'profit' => []];
for ($m = 1; $m <= 12; $m++) {
$in = round($income_total['months'][$m], 2);
$out = round($expense_total['months'][$m], 2);
$chart['income'][] = $m <= $months_shown ? $in : null;
$chart['expenses'][] = $m <= $months_shown ? $out : null;
$chart['profit'][] = $m <= $months_shown ? round($in - $out, 2) : null;
}
$period = $view === 'quarter' ? 'quarter' : 'month';
$chart = [
'income' => reportChartColumns($income_total['months'], $columns, $months_shown),
'expenses' => reportChartColumns($expense_total['months'], $columns, $months_shown),
];
$chart['profit'] = array_map(function ($in, $out) {
return $in === null ? null : round($in - $out, 2);
}, $chart['income'], $chart['expenses']);
require_once "includes/inc_report_layout.php";
@@ -162,18 +162,18 @@ $span = count($columns) + 4;
'sub' => $margin_prev === null ? '<span class="text-body-tertiary">Nothing before to compare</span>' : escapeHtml(reportPercent($margin_prev)) . ' <span class="text-body-tertiary">' . escapeHtml($current_year ? 'same time last year' : 'in ' . ($year - 1)) . '</span>'],
]) ?>
<?= reportSectionOpen('Income, expenses and profit by month', escapeHtml((string) $year)) ?>
<?= reportSectionOpen("Income, expenses and profit by $period", escapeHtml((string) $year)) ?>
<div class="card-body">
<?= reportChart([
'type' => 'bar',
'labels' => array_map(function ($m) { return date('M', mktime(0, 0, 0, $m, 1)); }, range(1, 12)),
'labels' => array_column($columns, 0),
'series' => [
['label' => 'Income', 'data' => $chart['income'], 'tone' => 'success'],
['label' => 'Expenses', 'data' => $chart['expenses'], 'tone' => 'danger'],
['label' => 'Net profit', 'data' => $chart['profit'], 'tone' => 'accent', 'type' => 'line'],
],
'format' => 'money',
], 'lg', 'Income, expenses and net profit per month') ?>
], 'lg', "Income, expenses and net profit per $period") ?>
</div>
</div>

View File

@@ -148,9 +148,10 @@ if ($has_purchases) {
<?php } else { ?>
<?php if ($tax_total != 0) { ?>
<?= reportSectionOpen('Tax collected by month', escapeHtml((string) $year)) ?>
<?= reportSectionOpen('Tax collected by ' . ($view === 'quarter' ? 'quarter' : 'month'), escapeHtml((string) $year)) ?>
<div class="card-body">
<?php
// By month or quarter, as the table below is
$months_shown = $current_year ? intval(date('n')) : 12;
$series = [];
$i = 0;
@@ -158,17 +159,15 @@ if ($has_purchases) {
if (abs($part['total']) < 0.005) {
continue;
}
$series[] = ['label' => $label, 'data' => array_map(function ($m) use ($part, $months_shown) {
return $m <= $months_shown ? round($part['months'][$m], 2) : null;
}, range(1, 12)), 'tone' => reportPalette($i++), 'stack' => 'tax'];
$series[] = ['label' => $label, 'data' => reportChartColumns($part['months'], $columns, $months_shown), 'tone' => reportPalette($i++), 'stack' => 'tax'];
}
echo reportChart([
'type' => 'bar',
'labels' => array_map(function ($m) { return date('M', mktime(0, 0, 0, $m, 1)); }, range(1, 12)),
'labels' => array_column($columns, 0),
'series' => $series,
'stacked' => true,
'format' => 'money',
], 'sm', 'Tax collected per month, by part');
], 'sm', 'Tax collected per ' . ($view === 'quarter' ? 'quarter' : 'month') . ', by part');
?>
</div>
</div>

View File

@@ -1528,6 +1528,27 @@ function reportYearColumns($view) {
return $columns;
}
/**
* A year's amounts by month, summed into the report's columns for its chart - so it goes by
* quarter when the table does. A column after $months_shown (later this year) is null, so the
* chart draws nothing there; the one under way is summed as far as it has got.
*/
function reportChartColumns(array $months, array $columns, $months_shown) {
$out = [];
foreach ($columns as [$label, $first, $last]) {
if ($first > $months_shown) {
$out[] = null;
continue;
}
$sum = 0.0;
for ($m = $first; $m <= min($last, $months_shown); $m++) {
$sum += $months[$m] ?? 0;
}
$out[] = round($sum, 2);
}
return $out;
}
/** One category's amounts summed into the report's columns. */
function reportSumColumns(array $months, array $columns) {
$out = [];