laravel-rs/app/Livewire/Dashboard/Statistics.php
Uchiha Bayu Senju 4654a2e350
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (push) Has been cancelled
Upload
2025-04-27 23:55:09 +07:00

64 lines
1.9 KiB
PHP

<?php
namespace App\Livewire\Dashboard;
use Carbon\Carbon;
use App\Models\Doctor;
use App\Models\Patient;
use App\Models\Payment;
use Livewire\Component;
use App\Models\Registration;
class Statistics extends Component
{
public $totalPatients;
public $totalDoctors;
public $todayRegistrations;
public $todayIncome;
public $weeklyIncome = [];
public $incomeChangePercentage;
public $incomeTrendStatus;
public function mount()
{
$this->totalPatients = Patient::count();
$this->totalDoctors = Doctor::count();
$this->todayRegistrations = Registration::whereDate('registration_date', Carbon::today())->count();
$this->todayIncome = Payment::whereDate('payment_date', Carbon::today())->sum('total_amount');
$dates = collect();
$today = Carbon::today();
for ($i = 6; $i >= 0; $i--) {
$date = $today->copy()->subDays($i);
$income = Payment::whereDate('payment_date', $date)->sum('total_amount');
$dates->push([
'date' => $date->format('Y-m-d'),
'income' => $income,
]);
}
$this->weeklyIncome = $dates;
$yesterdayIncome = Payment::whereDate('payment_date', Carbon::yesterday())->sum('total_amount');
if ($yesterdayIncome > 0) {
$this->incomeChangePercentage = (($this->todayIncome - $yesterdayIncome) / $yesterdayIncome) * 100;
if ($this->incomeChangePercentage > 0) {
$this->incomeTrendStatus = 'Naik';
} elseif ($this->incomeChangePercentage < 0) {
$this->incomeTrendStatus = 'Turun';
} else {
$this->incomeTrendStatus = 'Stabil';
}
} else {
$this->incomeChangePercentage = null;
$this->incomeTrendStatus = 'Tidak Ada Data';
}
}
public function render()
{
return view('livewire.dashboard.statistics');
}
}