47 lines
2.7 KiB
PHP
47 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\PeopleProcess;
|
|
use App\Models\User;
|
|
use Carbon\CarbonImmutable;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
class PeopleDeadlineReport
|
|
{
|
|
/** @param Builder<PeopleProcess> $query
|
|
* @return array<string,mixed>
|
|
*/
|
|
public function build(Builder $query, User $actor, string $window = 'all'): array
|
|
{
|
|
$desk = app(PeopleWorkdesk::class)->build($query, $actor);
|
|
$today = CarbonImmutable::parse($desk['updated_at'])->setTimezone(config('app.timezone'))->startOfDay();
|
|
$items = [];
|
|
foreach (['tasks' => 'Tarefa', 'approvals' => 'Conferência/decisão', 'alerts' => 'Alerta do processo'] as $group => $label) {
|
|
foreach ($desk[$group] as $item) {
|
|
$due = $item['due_on'];
|
|
$days = $due === null ? null : max(0, (int) CarbonImmutable::parse($due, config('app.timezone'))->startOfDay()->diffInDays($today));
|
|
$items[] = ['id' => $group.':'.$item['id'], 'process_id' => $item['process_id'], 'reference' => $item['reference'], 'title' => $item['title'],
|
|
'category' => $label, 'label' => $item['label'], 'responsibility' => $group === 'alerts' ? 'Alerta do processo; sem atribuição pessoal' : 'Minha intervenção autorizada',
|
|
'due_on' => $due, 'days_overdue' => $days, 'href' => $item['href']];
|
|
}
|
|
}
|
|
$items = array_values(array_filter($items, fn ($item) => match ($window) {
|
|
'overdue' => ($item['days_overdue'] ?? 0) > 0,
|
|
'today' => $item['due_on'] === $today->toDateString(),
|
|
'future' => $item['due_on'] !== null && $item['due_on'] > $today->toDateString(),
|
|
'undated' => $item['due_on'] === null,
|
|
default => true,
|
|
}));
|
|
usort($items, fn ($a, $b) => (($b['days_overdue'] ?? -1) <=> ($a['days_overdue'] ?? -1)) ?: strcmp($a['due_on'] ?? '9999', $b['due_on'] ?? '9999') ?: strcmp($a['id'], $b['id']));
|
|
|
|
return ['as_of' => $today->toDateString(), 'window' => $window, 'processes_considered' => $desk['counts']['processes'],
|
|
'matching_processes' => count(array_unique(array_column($items, 'process_id'))), 'total_items' => count($items),
|
|
'tasks' => count(array_filter($items, fn ($item) => $item['category'] === 'Tarefa')),
|
|
'approvals' => count(array_filter($items, fn ($item) => $item['category'] === 'Conferência/decisão')),
|
|
'alerts' => count(array_filter($items, fn ($item) => $item['category'] === 'Alerta do processo')), 'items' => $items,
|
|
'without_deadline' => count(array_filter($items, fn ($item) => $item['due_on'] === null)),
|
|
'overdue_items' => count(array_filter($items, fn ($item) => ($item['days_overdue'] ?? 0) > 0))];
|
|
}
|
|
}
|