simedkom/app/Filament/Widgets/Charts/BudgetRealizationChart.php

76 lines
2.1 KiB
PHP

<?php
namespace App\Filament\Widgets\Charts;
use App\Models\CooperationPayment;
use BezhanSalleh\FilamentShield\Traits\HasWidgetShield;
use Filament\Widgets\ChartWidget;
use Filament\Widgets\Concerns\InteractsWithPageFilters;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
class BudgetRealizationChart extends ChartWidget
{
use HasWidgetShield, InteractsWithPageFilters;
protected ?string $heading = 'Realisasi Anggaran Kerjasama';
protected static ?int $sort = 10;
protected int|string|array $columnSpan = 1;
protected ?string $pollingInterval = null;
protected function getData(): array
{
$startDate = $this->filters['startDate'] ?? Carbon::now()->startOfYear();
$endDate = $this->filters['endDate'] ?? Carbon::now()->endOfYear();
$startDate = Carbon::parse($startDate)->startOfDay();
$endDate = Carbon::parse($endDate)->endOfDay();
$data = CooperationPayment::query()
->select(
DB::raw("DATE_FORMAT(payment_date, '%Y-%m') as month"),
DB::raw('SUM(amount) as total')
)
->whereBetween('payment_date', [$startDate, $endDate])
->groupBy('month')
->orderBy('month')
->get();
$labels = [];
$values = [];
$current = clone $startDate;
while ($current <= $endDate) {
$monthStr = $current->format('Y-m');
$labels[] = $current->translatedFormat('M Y');
$monthData = $data->where('month', $monthStr)->first();
$values[] = $monthData ? (int) $monthData->total : 0;
$current->addMonth();
}
return [
'datasets' => [
[
'label' => 'Total Pembayaran (Rp)',
'data' => $values,
'fill' => 'start',
'backgroundColor' => 'rgba(59, 130, 246, 0.1)',
'borderColor' => '#3B82F6',
'tension' => 0.3,
],
],
'labels' => $labels,
];
}
protected function getType(): string
{
return 'line';
}
}