Files
minicrm/app/Filament/Widgets/AvgProcessingTimeWidget.php
phuongtc 8c6b71cb8a
Some checks failed
Tests / PHP 8.3 (push) Has been cancelled
Tests / PHP 8.4 (push) Has been cancelled
Tests / PHP 8.5 (push) Has been cancelled
feat: add i18n support for Vietnamese and English
2026-05-04 10:40:39 +00:00

71 lines
2.6 KiB
PHP

<?php
namespace App\Filament\Widgets;
use App\Models\Department;
use App\Models\Feedback;
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;
class AvgProcessingTimeWidget extends BaseWidget
{
protected function getStats(): array
{
$user = auth()->user();
$query = Feedback::query()
->whereNotNull('assigned_to')
->whereNotNull('updated_at');
if ($user->hasRole('manager') && ! $user->hasRole('admin')) {
$deptIds = Department::where('manager_id', $user->id)->pluck('id');
$query->whereIn('current_department_id', $deptIds);
}
$totalResolved = (clone $query)->whereIn('status', ['resolved', 'closed'])->count();
$avgMinutes = (clone $query)
->whereIn('status', ['resolved', 'closed'])
->selectRaw('AVG(strftime("%s", updated_at) - strftime("%s", created_at)) / 60 as avg_minutes')
->value('avg_minutes');
$totalPending = (clone $query)->whereNotIn('status', ['resolved', 'closed'])->count();
$escalatedCount = (clone $query)->where('is_escalated', true)->count();
$avgDisplay = $avgMinutes ? round($avgMinutes) . ' min' : 'N/A';
$avgDescription = $avgMinutes
? __('app.widget_avg_time_desc')
: __('app.widget_no_resolved_tickets');
return [
Stat::make(__('app.widget_resolved_tickets'), (string) $totalResolved)
->description(__('app.widget_total_resolved_closed'))
->descriptionIcon('heroicon-m-check-circle')
->color('success')
->chart([7, 3, 4, 5, 6, 8, $totalResolved]),
Stat::make(__('app.widget_avg_processing_time'), $avgDisplay)
->description($avgDescription)
->descriptionIcon('heroicon-m-clock')
->color('info'),
Stat::make(__('app.widget_pending_tickets'), (string) $totalPending)
->description(__('app.widget_still_in_progress'))
->descriptionIcon('heroicon-m-arrow-path')
->color('warning')
->chart([3, 5, 4, 6, 5, 4, $totalPending]),
Stat::make(__('app.widget_escalated'), (string) $escalatedCount)
->description(__('app.widget_escalated_desc'))
->descriptionIcon('heroicon-m-exclamation-triangle')
->color('danger'),
];
}
public static function canView(): bool
{
return auth()->user()?->hasPermissionTo('view-dashboard-widgets') ?? false;
}
}