Fix #75 - Update new accounts by month chart

- Only show last 12 months
- Group by year-month
- Do not group by account type
This commit is contained in:
Clemente Raposo 2025-01-29 14:41:05 +00:00 committed by Jack Anderson
parent 30e8c749ba
commit 7194819ddf

View file

@ -124,9 +124,9 @@ class AccountsNewByMonth extends LegacyHandler implements StatisticsProviderInte
$result = $this->runQuery($query, $bean);
$nameField = 'month';
$nameField = 'yearmonth';
$valueField = 'value';
$groupingFields = 'name';
$groupingFields = '';
$months = $this->getMonths();
$series = $this->buildMultiSeries($result, $groupingFields, $nameField, $valueField, $months);
@ -134,6 +134,7 @@ class AccountsNewByMonth extends LegacyHandler implements StatisticsProviderInte
$chartOptions = new ChartOptions();
$chartOptions->yAxisTickFormatting = true;
$chartOptions->xAxisTicks = $months;
$chartOptions->legend = false;
$statistic = $this->buildSeriesResponse(self::KEY, 'int', $series, $chartOptions);
@ -156,7 +157,24 @@ class AccountsNewByMonth extends LegacyHandler implements StatisticsProviderInte
*/
protected function getMonths(): array
{
return [1,2,3,4,5,6,7,8,9,10,11,12];
$currentYear = date('Y');
$currentMonth = date('n');
$months = [];
for ($i = 0; $i < 12; $i++) {
$newMonth = $currentMonth - $i;
if ($newMonth <= 0) {
$newMonth += 12;
$newYear = $currentYear - 1;
} else {
$newYear = $currentYear;
}
$months[] = $newYear . '-' . str_pad($newMonth, 2, '0', STR_PAD_LEFT);
}
return array_reverse($months);
}
/**
@ -176,10 +194,13 @@ class AccountsNewByMonth extends LegacyHandler implements StatisticsProviderInte
*/
protected function generateQuery(array $query): array
{
$query['select'] = 'SELECT COUNT(accounts.name) as value, EXTRACT(MONTH FROM accounts.date_entered) as month, accounts.account_type as name';
$query['where'] .= ' AND accounts.account_type is not null ';
$lastYear = date('Y', strtotime('-1 year'));
$nextMonth = date('m', strtotime('first day of next month'));
$currentDate = date('Y-m-d');
$query['select'] = "SELECT COUNT(accounts.name) as value, CONCAT(EXTRACT(YEAR FROM accounts.date_entered), '-', LPAD(EXTRACT(MONTH FROM accounts.date_entered), 2, '0')) as yearmonth";
$query['where'] .= " AND accounts.date_entered >= '$lastYear-$nextMonth-01' AND accounts.date_entered <= '$currentDate' ";
$query['order_by'] = '';
$query['group_by'] = ' GROUP BY EXTRACT(MONTH FROM accounts.date_entered), accounts.account_type';
$query['group_by'] = "GROUP BY yearmonth";
return $query;
}