82 lines
2.7 KiB
PHP
82 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\InstallmentApplication;
|
|
use App\Models\Payment;
|
|
use App\Models\PropertyAccount;
|
|
use App\Models\Task;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ReportService
|
|
{
|
|
protected function monthExpression(string $column): string
|
|
{
|
|
return DB::connection()->getDriverName() === 'sqlite'
|
|
? "strftime('%Y-%m', {$column})"
|
|
: "DATE_FORMAT({$column}, '%Y-%m')";
|
|
}
|
|
|
|
public function dashboardSummary(): array
|
|
{
|
|
return [
|
|
'new_applications' => InstallmentApplication::whereDate('created_at', today())->count(),
|
|
'approved_applications' => InstallmentApplication::where('status', 'aktif')->count(),
|
|
'failed_applications' => InstallmentApplication::where('status', 'gagal')->count(),
|
|
'cancelled_applications' => InstallmentApplication::where('status', 'batal')->count(),
|
|
'active_accounts' => InstallmentApplication::where('status', 'aktif')->count(),
|
|
'payments_received' => Payment::where('status', 'success')->sum('amount'),
|
|
'outstanding_arrears' => PropertyAccount::sum('tunggakan'),
|
|
'completed_accounts' => InstallmentApplication::where('status', 'selesai')->count(),
|
|
'public_users' => User::whereHas('role', fn ($query) => $query->where('slug', 'public'))->count(),
|
|
'open_tasks' => Task::where('status', 'belum_selesai')->count(),
|
|
];
|
|
}
|
|
|
|
public function monthlyApplications()
|
|
{
|
|
$monthExpression = $this->monthExpression('created_at');
|
|
|
|
return InstallmentApplication::selectRaw("{$monthExpression} as month, count(*) as total")
|
|
->groupBy('month')
|
|
->orderBy('month')
|
|
->get();
|
|
}
|
|
|
|
public function monthlyPayments()
|
|
{
|
|
$monthExpression = $this->monthExpression('created_at');
|
|
|
|
return Payment::where('status', 'success')
|
|
->selectRaw("{$monthExpression} as month, sum(amount) as total")
|
|
->groupBy('month')
|
|
->orderBy('month')
|
|
->get();
|
|
}
|
|
|
|
public function categoryBreakdown()
|
|
{
|
|
return InstallmentApplication::select('category', DB::raw('count(*) as total'))
|
|
->groupBy('category')
|
|
->get();
|
|
}
|
|
|
|
public function areaBreakdown()
|
|
{
|
|
return PropertyAccount::select('bandar', DB::raw('count(*) as total'))
|
|
->groupBy('bandar')
|
|
->orderByDesc('total')
|
|
->get();
|
|
}
|
|
|
|
public function staffPerformance()
|
|
{
|
|
return User::query()
|
|
->select('users.id', 'users.name')
|
|
->withCount(['reviewedApplications as total_reviewed'])
|
|
->whereHas('role', fn ($query) => $query->where('slug', 'staff'))
|
|
->get();
|
|
}
|
|
}
|