Add department_goals_o object and enhance JSON export for goals module

- Introduce `department_goals_o` for managing department-specific goals with criteria.
- Enhance `goals_criteria` with `toArray` and `toJson` methods for canonical JSON export.
- Update `goals` to delegate JSON export to `goals_criteria`.
This commit is contained in:
Jeppe Bundgaard
2026-01-26 17:12:23 +01:00
parent 42d2889934
commit d1963b5299
3 changed files with 120 additions and 0 deletions
@@ -131,4 +131,41 @@ class goals_criteria implements goals_criteria_i
return $criteria;
}
/**
* Export the criteria as an associative array suitable for JSON encoding.
* Ensures a canonical schema matching fromJson expectations.
* @return array
*/
public function toArray(): array
{
// Normalize lists to identifiers only
$users = $this->users?->listCustomerNumbers() ?? [];
$departments = $this->departments?->listIDs() ?? [];
$products = $this->products?->listIDs() ?? [];
// Remove nulls and duplicates just in case
$users = array_values(array_unique(array_filter($users, fn($v) => $v !== null)));
$departments = array_values(array_unique(array_filter($departments, fn($v) => $v !== null)));
$products = array_values(array_unique(array_filter($products, fn($v) => $v !== null)));
return [
'type' => $this->type?->name ?? goals_criteria_type::NONE->name,
'target' => $this->target ?? 0,
'start' => ($this->start instanceof \DateTimeInterface) ? $this->start->format(DATE_ATOM) : null,
'end' => ($this->end instanceof \DateTimeInterface) ? $this->end->format(DATE_ATOM) : null,
'users' => $users,
'departments' => $departments,
'products' => $products,
];
}
/**
* Export the criteria to JSON
* @return string
*/
public function toJson(): string
{
return json_encode($this->toArray());
}
}