feat: add company-specific dashboard stats and cooperation status chart with role-based filtering
This commit is contained in:
parent
4a1993a778
commit
64f521adcc
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Filament\Pages;
|
namespace App\Filament\Pages;
|
||||||
|
|
||||||
|
use App\Filament\Widgets\Charts\CooperationStatusChart;
|
||||||
use App\Filament\Widgets\Stats\OverviewStats;
|
use App\Filament\Widgets\Stats\OverviewStats;
|
||||||
use BezhanSalleh\FilamentShield\Traits\HasPageShield;
|
use BezhanSalleh\FilamentShield\Traits\HasPageShield;
|
||||||
use Filament\Pages\Dashboard as BaseDashboard;
|
use Filament\Pages\Dashboard as BaseDashboard;
|
||||||
@ -23,6 +24,7 @@ public function getWidgets(): array
|
|||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
OverviewStats::class,
|
OverviewStats::class,
|
||||||
|
CooperationStatusChart::class,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,6 +3,7 @@
|
|||||||
namespace App\Filament\Widgets\Charts;
|
namespace App\Filament\Widgets\Charts;
|
||||||
|
|
||||||
use App\Enums\CooperationStatus;
|
use App\Enums\CooperationStatus;
|
||||||
|
use App\Enums\RoleEnum;
|
||||||
use App\Models\Cooperation;
|
use App\Models\Cooperation;
|
||||||
use BezhanSalleh\FilamentShield\Traits\HasWidgetShield;
|
use BezhanSalleh\FilamentShield\Traits\HasWidgetShield;
|
||||||
use Filament\Widgets\ChartWidget;
|
use Filament\Widgets\ChartWidget;
|
||||||
@ -25,9 +26,17 @@ protected function getData(): array
|
|||||||
$startDate = $this->filters['startDate'] ?? null;
|
$startDate = $this->filters['startDate'] ?? null;
|
||||||
$endDate = $this->filters['endDate'] ?? null;
|
$endDate = $this->filters['endDate'] ?? null;
|
||||||
|
|
||||||
|
$user = auth()->user();
|
||||||
|
|
||||||
$data = Cooperation::query()
|
$data = Cooperation::query()
|
||||||
->when($startDate, fn ($q) => $q->whereDate('created_at', '>=', $startDate))
|
->when($startDate, fn ($q) => $q->whereDate('created_at', '>=', $startDate))
|
||||||
->when($endDate, fn ($q) => $q->whereDate('created_at', '<=', $endDate))
|
->when($endDate, fn ($q) => $q->whereDate('created_at', '<=', $endDate))
|
||||||
|
->when($user && $user->hasRole(RoleEnum::PERUSAHAAN->value), function ($query) use ($user) {
|
||||||
|
$partnerMediaId = $user->company?->partnerMedia?->id;
|
||||||
|
$query->whereHas('partnerMedia', function ($q) use ($partnerMediaId) {
|
||||||
|
$q->where('partner_media.id', $partnerMediaId);
|
||||||
|
});
|
||||||
|
})
|
||||||
->selectRaw('status, count(*) as total')
|
->selectRaw('status, count(*) as total')
|
||||||
->groupBy('status')
|
->groupBy('status')
|
||||||
->get();
|
->get();
|
||||||
|
|||||||
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
use App\Enums\IsActive;
|
use App\Enums\IsActive;
|
||||||
use App\Enums\IssueSentiment;
|
use App\Enums\IssueSentiment;
|
||||||
|
use App\Enums\RoleEnum;
|
||||||
use App\Models\Classification;
|
use App\Models\Classification;
|
||||||
use App\Models\CooperationPayment;
|
use App\Models\CooperationPayment;
|
||||||
use App\Models\DataChangeRequest;
|
use App\Models\DataChangeRequest;
|
||||||
@ -31,6 +32,12 @@ class OverviewStats extends BaseWidget
|
|||||||
|
|
||||||
protected function getStats(): array
|
protected function getStats(): array
|
||||||
{
|
{
|
||||||
|
$user = auth()->user();
|
||||||
|
|
||||||
|
if ($user && $user->hasRole(RoleEnum::PERUSAHAAN->value)) {
|
||||||
|
return $this->getCompanyStats($user);
|
||||||
|
}
|
||||||
|
|
||||||
$totalJournalists = Journalist::count();
|
$totalJournalists = Journalist::count();
|
||||||
$totalMedia = PartnerMedia::count();
|
$totalMedia = PartnerMedia::count();
|
||||||
$average = $totalMedia > 0 ? round($totalJournalists / $totalMedia, 1) : 0;
|
$average = $totalMedia > 0 ? round($totalJournalists / $totalMedia, 1) : 0;
|
||||||
@ -114,4 +121,51 @@ private function getRandomColor(): string
|
|||||||
{
|
{
|
||||||
return $this->availableColors[array_rand($this->availableColors)];
|
return $this->availableColors[array_rand($this->availableColors)];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getCompanyStats($user): array
|
||||||
|
{
|
||||||
|
$company = $user->company;
|
||||||
|
|
||||||
|
if (! $company) {
|
||||||
|
return [
|
||||||
|
Stat::make('Status Perusahaan', 'Belum Terdaftar')
|
||||||
|
->description('Silakan lengkapi profil perusahaan')
|
||||||
|
->color('danger'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$journalistCount = $company->journalists()->count();
|
||||||
|
$cooperationCount = 0;
|
||||||
|
|
||||||
|
if ($company->partnerMedia) {
|
||||||
|
$cooperationCount = $company->partnerMedia->cooperations()->count();
|
||||||
|
}
|
||||||
|
|
||||||
|
$verificationStatus = $company->verificationRequest?->status?->getLabel() ?? 'Belum Mengajukan';
|
||||||
|
$verificationColor = $company->verificationRequest?->status?->getColor() ?? 'gray';
|
||||||
|
|
||||||
|
$dataChangeRequestsCount = $company->dataChangeRequests()->pending()->count() + $user->dataChangeRequests()->pending()->count();
|
||||||
|
|
||||||
|
return [
|
||||||
|
Stat::make('Status Verifikasi', $verificationStatus)
|
||||||
|
->description('Status verifikasi perusahaan')
|
||||||
|
->descriptionIcon('heroicon-o-shield-check')
|
||||||
|
->color($verificationColor),
|
||||||
|
|
||||||
|
Stat::make('Total Jurnalis', $journalistCount)
|
||||||
|
->description('Jurnalis terdaftar')
|
||||||
|
->descriptionIcon('heroicon-o-users')
|
||||||
|
->color($this->getRandomColor()),
|
||||||
|
|
||||||
|
Stat::make('Total Kerjasama', $cooperationCount)
|
||||||
|
->description('Kerjasama diajukan/diikuti')
|
||||||
|
->descriptionIcon('heroicon-o-hand-raised')
|
||||||
|
->color($this->getRandomColor()),
|
||||||
|
|
||||||
|
Stat::make('Antrean Perubahan Data', $dataChangeRequestsCount)
|
||||||
|
->description('Perubahan data menunggu persetujuan')
|
||||||
|
->descriptionIcon('heroicon-o-document-text')
|
||||||
|
->color($this->getRandomColor()),
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -692,11 +692,11 @@ public function run(): void
|
|||||||
"name": "Perusahaan",
|
"name": "Perusahaan",
|
||||||
"guard_name": "web",
|
"guard_name": "web",
|
||||||
"permissions": [
|
"permissions": [
|
||||||
"View:Analytic",
|
|
||||||
|
|
||||||
"ViewAny:Cooperation",
|
"ViewAny:Cooperation",
|
||||||
"View:Cooperation",
|
"View:Cooperation",
|
||||||
|
|
||||||
|
"View:CooperationStatusChart",
|
||||||
|
|
||||||
"View:Company",
|
"View:Company",
|
||||||
|
|
||||||
"View:Dashboard",
|
"View:Dashboard",
|
||||||
@ -712,6 +712,8 @@ public function run(): void
|
|||||||
|
|
||||||
"View:Media",
|
"View:Media",
|
||||||
|
|
||||||
|
"View:OverviewStats",
|
||||||
|
|
||||||
"View:Verification"
|
"View:Verification"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user