100 lines
2.9 KiB
PHP
100 lines
2.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Filament\Widgets;
|
|
|
|
use App\Models\Visit;
|
|
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
|
|
use Filament\Widgets\StatsOverviewWidget\Stat;
|
|
use Livewire\Attributes\On;
|
|
|
|
class VisitStatsWidget extends BaseWidget
|
|
{
|
|
public ?string $fromDate = null;
|
|
public ?string $toDate = null;
|
|
public ?int $doctor = null;
|
|
public ?int $treatment = null;
|
|
public ?int $paymentType = null;
|
|
|
|
public function mount(): void
|
|
{
|
|
$this->fromDate = now()->toDateString();
|
|
$this->toDate = now()->toDateString();
|
|
}
|
|
|
|
#[On('visit-stats-updated')]
|
|
public function updateStats(
|
|
?string $fromDate,
|
|
?string $toDate,
|
|
?int $doctor,
|
|
?int $treatment,
|
|
?int $paymentType,
|
|
): void {
|
|
if (! $fromDate && ! $toDate) {
|
|
$this->fromDate = now()->toDateString();
|
|
$this->toDate = null;
|
|
} else {
|
|
$this->fromDate = $fromDate;
|
|
$this->toDate = $toDate;
|
|
}
|
|
$this->doctor = $doctor;
|
|
$this->treatment = $treatment;
|
|
$this->paymentType = $paymentType;
|
|
}
|
|
|
|
protected function buildQuery()
|
|
{
|
|
$query = Visit::query();
|
|
|
|
if ($this->fromDate) {
|
|
$fromDate = \Carbon\Carbon::parse($this->fromDate)->format('Y-m-d');
|
|
$query->whereRaw("SUBSTRING(visit_date, 1, 10) >= ?", [$fromDate]);
|
|
}
|
|
|
|
if ($this->toDate) {
|
|
$toDate = \Carbon\Carbon::parse($this->toDate)->format('Y-m-d');
|
|
$query->whereRaw("SUBSTRING(visit_date, 1, 10) <= ?", [$toDate]);
|
|
}
|
|
|
|
if ($this->doctor) {
|
|
$query->where('doctor', $this->doctor);
|
|
}
|
|
|
|
if ($this->treatment) {
|
|
$query->where('treatment', $this->treatment);
|
|
}
|
|
|
|
if ($this->paymentType) {
|
|
$query->where('payment_type', $this->paymentType);
|
|
}
|
|
|
|
return $query;
|
|
}
|
|
|
|
protected function getStats(): array
|
|
{
|
|
$query = $this->buildQuery();
|
|
|
|
$totalPaid = (int) (clone $query)->sum('paid_amount');
|
|
$totalCost = (int) (clone $query)->sum('treatment_cost');
|
|
$totalRemained = $totalCost - $totalPaid;
|
|
|
|
$currency = __('visits.fields.currency');
|
|
|
|
return [
|
|
Stat::make(__('visits.fields.paid_amount'), number_format($totalPaid) . ' ' . $currency)
|
|
->color('success')
|
|
->icon('heroicon-o-banknotes'),
|
|
|
|
Stat::make(__('visits.fields.treatment_cost'), number_format($totalCost) . ' ' . $currency)
|
|
->color('info')
|
|
->icon('heroicon-o-clipboard-document-list'),
|
|
|
|
Stat::make(__('visits.fields.remained_amount'), number_format($totalRemained) . ' ' . $currency)
|
|
->color($totalRemained > 0 ? 'danger' : 'success')
|
|
->icon('heroicon-o-arrow-trending-down'),
|
|
];
|
|
}
|
|
}
|