Fix Accounts new by month data retrieval

This commit is contained in:
Clemente Raposo 2025-01-29 23:09:28 +00:00 committed by Jack Anderson
parent cfda4d0941
commit b9b07a7eb3
2 changed files with 35 additions and 9 deletions

View file

@ -168,7 +168,8 @@ trait StatisticsHandlingTrait
* @param string $groupingField
* @param string $nameField
* @param string $valueField
* @param $defaultValues
* @param array $defaultValues
* @param bool $addEmpty
* @return Series|SeriesResult
*/
protected function buildMultiSeries(
@ -176,8 +177,9 @@ trait StatisticsHandlingTrait
string $groupingField,
string $nameField,
string $valueField,
array $defaultValues
) {
array $defaultValues,
bool $addEmpty = false
): Series|SeriesResult {
$seriesMap = [];
foreach ($result as $row) {
@ -206,6 +208,24 @@ trait StatisticsHandlingTrait
$seriesMap[$groupingFieldValue]['items'][$nameFieldValue]->value = $valueFieldValue;
}
if (empty($result) && $addEmpty) {
$series = new Series();
$series->name = '';
$series->series = [];
$seriesMap[''] = [
'series' => $series,
'items' => []
];
foreach ($defaultValues as $default) {
$item = new SeriesItem();
$item->name = $default;
$item->value = '0';
$seriesMap['']['items'][$default] = $item;
}
}
$series = new SeriesResult();
$series->multiSeries = [];

View file

@ -129,7 +129,7 @@ class AccountsNewByMonth extends LegacyHandler implements StatisticsProviderInte
$groupingFields = '';
$months = $this->getMonths();
$series = $this->buildMultiSeries($result, $groupingFields, $nameField, $valueField, $months);
$series = $this->buildMultiSeries($result, $groupingFields, $nameField, $valueField, $months, true);
$chartOptions = new ChartOptions();
$chartOptions->yAxisTickFormatting = true;
@ -157,8 +157,9 @@ class AccountsNewByMonth extends LegacyHandler implements StatisticsProviderInte
*/
protected function getMonths(): array
{
$currentYear = date('Y');
$currentMonth = date('n');
$currentDate = date('Y-m-d');
$currentYear = date('Y', strtotime($currentDate));
$currentMonth = date('n', strtotime($currentDate));
$months = [];
@ -194,11 +195,16 @@ class AccountsNewByMonth extends LegacyHandler implements StatisticsProviderInte
*/
protected function generateQuery(array $query): array
{
$lastYear = date('Y', strtotime('-1 year'));
$nextMonth = date('m', strtotime('first day of next month'));
$currentDate = date('Y-m-d');
$nextMonth = date('Y-m-d', strtotime($currentDate . ' first day of next month'));
$lastYear = date('Y-m-d', strtotime($nextMonth . ' -1 year'));
$startDate = date('Y-m-d', strtotime($lastYear . ' -1 day'));
$endDate = date('Y-m-d', strtotime($currentDate . ' +1 day'));
$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['where'] .= " AND accounts.date_entered > '$startDate' AND accounts.date_entered < '$endDate' ";
$query['order_by'] = '';
$query['group_by'] = "GROUP BY yearmonth";