Initial commit: Struktur aplikasi ilycats
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\{Matting, User, Cat};
|
||||
use App\Exports\MattingExport;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
use Barryvdh\DomPDF\Facade\Pdf;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class MattingReportController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
// 1. Eager Loading Lengkap (Sire, Dam, PIC, dan Detail Anak)
|
||||
$query = Matting::with(['sire', 'dam', 'pic', 'kittens.cat'])->withCount('kittens');
|
||||
// 2. Filter Nama Indukan (Sire atau Dam)
|
||||
if ($request->filled('parent_name')) {
|
||||
$name = $request->parent_name;
|
||||
$query->where(function($q) use ($name) {
|
||||
$q->whereHas('sire', fn($sq) => $sq->where('nama_kucing', 'like', "%{$name}%"))
|
||||
->orWhereHas('dam', fn($sq) => $sq->where('nama_kucing', 'like', "%{$name}%"));
|
||||
});
|
||||
}
|
||||
|
||||
// 3. Filter Ras Dinamis
|
||||
if ($request->filled('ras')) {
|
||||
$ras = $request->ras;
|
||||
$query->where(function($q) use ($ras) {
|
||||
$q->whereHas('sire', fn($sq) => $sq->where('ras', $ras))
|
||||
->orWhereHas('dam', fn($sq) => $sq->where('ras', $ras));
|
||||
});
|
||||
}
|
||||
|
||||
// 4. Filter Registry (ICA, CFA, WCF, dll)
|
||||
if ($request->filled('registry')) {
|
||||
$reg = $request->registry;
|
||||
$query->where(function($q) use ($reg) {
|
||||
$q->where('registry', $reg)
|
||||
->orWhereHas('sire', fn($sq) => $sq->where('registry', $reg))
|
||||
->orWhereHas('dam', fn($sq) => $sq->where('registry', $reg));
|
||||
});
|
||||
}
|
||||
|
||||
// 5. Filter Periode Perkawinan
|
||||
if ($request->filled('start_date') && $request->filled('end_date')) {
|
||||
$query->whereBetween('tgl_kawin_awal', [
|
||||
Carbon::parse($request->start_date)->startOfDay(),
|
||||
Carbon::parse($request->end_date)->endOfDay()
|
||||
]);
|
||||
}
|
||||
|
||||
// 6. Filter Status Induk Betina
|
||||
if ($request->filled('status')) {
|
||||
$status = $request->status;
|
||||
$query->whereHas('dam', fn($q) => $q->where('status', $status));
|
||||
}
|
||||
|
||||
// 7. Handle Export
|
||||
if ($request->filled('export')) {
|
||||
$data = $query->latest()->get();
|
||||
return $this->processExport($data, $request->export);
|
||||
}
|
||||
|
||||
// 8. Eksekusi dengan Pagination
|
||||
$mattings = $query->latest()->paginate(15)->withQueryString();
|
||||
|
||||
// 9. Data Pendukung UI (Agar Dropdown di Blade berfungsi)
|
||||
$breeds = Cat::distinct()->whereNotNull('ras')->pluck('ras');
|
||||
$registries = ['ICA', 'CFA', 'WCF', 'FFF', 'LOOF'];
|
||||
$statuses = ['Breeding', 'Hamil', 'Teradopsi', 'RIP'];
|
||||
|
||||
return view('pages.reports.matting.index', compact(
|
||||
'mattings', 'breeds', 'registries', 'statuses'
|
||||
));
|
||||
|
||||
}
|
||||
|
||||
protected function processExport($data, $format)
|
||||
{
|
||||
$fileName = "Lineage_Report_" . now()->format('Ymd_His');
|
||||
|
||||
if ($format === 'xlsx') {
|
||||
return Excel::download(new MattingExport($data), "{$fileName}.xlsx");
|
||||
}
|
||||
|
||||
// Penyempurnaan PDF: Tambahkan opsi font dan akses file lokal (untuk logo)
|
||||
$pdf = Pdf::loadView('pages.reports.matting.pdf', [
|
||||
'data' => $data,
|
||||
// Kirim data setting jika ingin menampilkan logo/branding dinamis di PDF
|
||||
'setting' => \App\Models\Setting::pluck('value', 'key')
|
||||
]);
|
||||
|
||||
return $pdf->setPaper('a4', 'landscape')
|
||||
->setOption([
|
||||
'tempDir' => public_path(),
|
||||
'chroot' => public_path(),
|
||||
])
|
||||
->setWarnings(false) // Menghindari PDF korup karena error kecil di blade
|
||||
->download("{$fileName}.pdf");
|
||||
}
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
// Eager loading mendalam untuk silsilah dan data anak
|
||||
$matting = Matting::with(['sire', 'dam', 'pic', 'kittens.cat'])->findOrFail($id);
|
||||
|
||||
return view('pages.reports.matting.show', compact('matting'));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user