@@ -20,7 +20,8 @@ class ProcurementOperationalInsights
|
||||
$today = $instant->setTimezone(config('app.timezone'))->startOfDay();
|
||||
$total = $pending = $missingSubmission = $neededPast = $approved = $withoutQuotes = $withoutOrder = 0;
|
||||
$oldest = [];
|
||||
foreach ((clone $query)->reorder()->lazyById(200) as $record) {
|
||||
$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++;
|
||||
@@ -43,6 +44,19 @@ class ProcurementOperationalInsights
|
||||
}
|
||||
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;
|
||||
}
|
||||
@@ -51,6 +65,7 @@ class ProcurementOperationalInsights
|
||||
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]];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user