laravel-rs/app/Livewire/Dashboard/ReportGenerator.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

55 lines
1.6 KiB
PHP

<?php
namespace App\Livewire\Dashboard;
use Livewire\Component;
use App\Models\Patient;
use App\Models\Registration;
use App\Models\Payment;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\ReportExport;
use Barryvdh\DomPDF\Facade\Pdf;
use Carbon\Carbon;
class ReportGenerator extends Component
{
public $reportType = 'daily'; // daily, monthly, yearly
public $startDate;
public $endDate;
public $reportData = [];
public $generatedAt;
public function generate()
{
if (!$this->startDate || !$this->endDate) {
session()->flash('error', 'Silahkan pilih tanggal mulai dan tanggal akhir.');
return;
}
// Query data
$this->reportData = [
'totalPatients' => Patient::whereBetween('created_at', [$this->startDate, $this->endDate])->count(),
'totalRegistrations' => Registration::whereBetween('registration_date', [$this->startDate, $this->endDate])->count(),
'totalIncome' => Payment::whereBetween('payment_date', [$this->startDate, $this->endDate])->sum('total_amount'),
];
$this->generatedAt = now()->format('d-m-Y H:i:s');
}
public function exportExcel()
{
return Excel::download(new ReportExport($this->reportData), 'laporan.xlsx');
}
public function exportPDF()
{
$pdf = Pdf::loadView('exports.report-pdf', ['reportData' => $this->reportData]);
return response()->streamDownload(fn () => print($pdf->output()), 'laporan.pdf');
}
public function render()
{
return view('livewire.dashboard.report-generator');
}
}