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'), ]; } }