29 lines
1.9 KiB
PHP
29 lines
1.9 KiB
PHP
<?php
|
|||
|
|
declare(strict_types=1);
|
||
|
|
require_once __DIR__ . '/ReportFilters.php';
|
||
|
|
require_once __DIR__ . '/ReportDataMapper.php';
|
||
|
|
require_once __DIR__ . '/ReportQuery.php';
|
||
|
|
|
||
|
|
final class TechnicianActivityReport implements ReportQuery
|
||
|
|
{
|
||
|
|
public function __construct(private readonly ?ReportFilters $filters = null, private readonly ?ReportDataMapper $mapper = null) {}
|
||
|
|
/** @param list<array<string,mixed>> $rows @return list<array<string,mixed>> */
|
||
|
|
public function build(array $rows, string $audience = 'internal'): array
|
||
|
|
{
|
||
|
|
$filters = $this->filters ?? new ReportFilters(); $mapper = $this->mapper ?? new ReportDataMapper(); $totals = [];
|
||
|
|
foreach ($rows as $row) {
|
||
|
|
if (!$filters->matches($row)) continue;
|
||
|
|
$key = (string)(int)($row['technician_id'] ?? 0) . ':' . (string)(int)($row['client_id'] ?? 0);
|
||
|
|
if (!isset($totals[$key])) $totals[$key] = ['technician_id' => (int)($row['technician_id'] ?? 0), 'technician_name' => (string)($row['technician_name'] ?? ''), 'client_id' => (int)($row['client_id'] ?? 0), 'client_name' => (string)($row['client_name'] ?? ''), 'hours' => 0.0, 'sla_hours' => 0.0];
|
||
|
|
$hours = max(0.0, (float)($row['hours'] ?? 0)); $totals[$key]['hours'] += $hours;
|
||
|
|
if (!empty($row['counts_toward_sla'])) $totals[$key]['sla_hours'] += $hours;
|
||
|
|
}
|
||
|
|
$result = array_values($totals);
|
||
|
|
foreach ($result as &$item) { $item['hours'] = round($item['hours'], 2); $item['sla_hours'] = round($item['sla_hours'], 2); if ($audience === 'client') $item = $mapper->clientActivity($item); }
|
||
|
|
unset($item);
|
||
|
|
usort($result, static fn(array $a,array $b): int => strcmp((string)($a['technician_name'] ?? ''), (string)($b['technician_name'] ?? '')) ?: ((int)($a['technician_id'] ?? 0) <=> (int)($b['technician_id'] ?? 0)));
|
||
|
|
return $result;
|
||
|
|
}
|
||
|
|
public function query(array $rows, string $audience = 'internal'): array { return $this->build($rows, $audience); }
|
||
|
|
}
|