feat: improve visits filter UX and update currency label
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
<?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) {
|
||||
$query->whereDate('visit_date', '>=', $this->fromDate);
|
||||
}
|
||||
|
||||
if ($this->toDate) {
|
||||
$query->whereDate('visit_date', '<=', $this->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'),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user