Files
compras/app/Services/ProcurementOperationalInsights.php
T
nala efd97e265b
tests / ci (push) Canceled after 0s
commit melhorias compras3
2026-11-22 10:56:58 +02:00

72 lines
4.0 KiB
PHP

<?php
namespace App\Services;
use App\Models\Requisition;
use Carbon\CarbonImmutable;
use Illuminate\Database\Eloquent\Builder;
class ProcurementOperationalInsights
{
/**
* The caller supplies the report's authorised, filtered records query with
* quotations_count and order_number projections, inside its read snapshot.
*
* @param Builder<Requisition> $query
* @return array<string,mixed>
*/
public function build(Builder $query, CarbonImmutable $instant): array
{
$today = $instant->setTimezone(config('app.timezone'))->startOfDay();
$total = $pending = $missingSubmission = $neededPast = $approved = $withoutQuotes = $withoutOrder = 0;
$oldest = [];
$approvalTimes = ['measured' => 0, 'unknown' => 0, 'total_seconds' => 0, 'min_seconds' => null, 'max_seconds' => null];
foreach ((clone $query)->with('steps:id,requisition_id,decision,decided_at')->reorder()->lazyById(200) as $record) {
$total++;
if (in_array($record->status, ['draft', 'pending'], true) && $record->needed_by !== null && $record->needed_by->toDateString() < $today->toDateString()) {
$neededPast++;
}
if ($record->status === 'pending') {
$pending++;
if ($record->submitted_at === null) {
$missingSubmission++;
} else {
$age = (int) $record->submitted_at->toImmutable()->setTimezone(config('app.timezone'))->startOfDay()->diffInDays($today);
if ($age >= 0) {
$oldest[] = ['id' => $record->id, 'number' => $record->number, 'submitted_at' => $record->submitted_at->toIso8601String(),
'days_since_submission' => $age, 'href' => '/requisicoes/'.$record->id.'/processo'];
usort($oldest, fn ($a, $b) => ($b['days_since_submission'] <=> $a['days_since_submission']) ?: ($a['id'] <=> $b['id']));
$oldest = array_slice($oldest, 0, 5);
} else {
$missingSubmission++;
}
}
}
if ($record->status === 'approved') {
$approved++;
$steps = $record->steps;
$valid = $record->submitted_at !== null && $steps->isNotEmpty() && $steps->every(fn ($step) => $step->decision === 'approved' && $step->decided_at !== null);
$dates = $valid ? $steps->map(fn ($step) => CarbonImmutable::parse($step->decided_at, config('app.timezone'))) : collect();
$end = $dates->max();
if (! $valid || $end === null || $end < $record->submitted_at || $end > $instant || $dates->contains(fn ($date) => $date < $record->submitted_at)) {
$approvalTimes['unknown']++;
} else {
$seconds = $end->getTimestamp() - $record->submitted_at->getTimestamp();
$approvalTimes['measured']++;
$approvalTimes['total_seconds'] += $seconds;
$approvalTimes['min_seconds'] = $approvalTimes['min_seconds'] === null ? $seconds : min($approvalTimes['min_seconds'], $seconds);
$approvalTimes['max_seconds'] = $approvalTimes['max_seconds'] === null ? $seconds : max($approvalTimes['max_seconds'], $seconds);
}
$withoutQuotes += (int) $record->getAttribute('quotations_count') === 0 ? 1 : 0;
$withoutOrder += $record->getAttribute('order_number') === null ? 1 : 0;
}
}
return ['method' => 'procurement_facts_v1', 'generated_at' => $instant->toIso8601String(), 'processes' => $total,
'pending' => ['count' => $pending, 'unknown_submission_age' => $missingSubmission, 'oldest' => $oldest],
'unapproved_past_needed_date' => $neededPast,
'approval_times' => $approvalTimes,
'approved' => ['count' => $approved, 'without_registered_quotes' => $withoutQuotes, 'without_visible_order' => $withoutOrder]];
}
}