<?php

namespace App\Http\Controllers;

use App\Models\Kampus;
use App\Models\Mahasiswa;
use App\Models\Prodi;
use App\Models\AngsuranDetail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Barryvdh\DomPDF\Facade\Pdf;

class LaporanAlokasiHerregistrasiController extends Controller
{
    public function index(Request $request)
    {
        $kampusList = Kampus::orderBy('nama')->get();

        // Filter
        $kampusId = $request->kampus_id;
        $prodiId = $request->prodi_id;
        $tahunAjaran = $request->tahun_ajaran;
        $startDate = $request->start_date;
        $endDate = $request->end_date;

        // Query mahasiswa yang sudah divalidasi dan memiliki angsuran valid
        $query = Mahasiswa::with(['kampus', 'prodi', 'angsuran'])
            ->where('validasi_status', 'valid')
            ->whereHas('angsuran', function ($q) {
                $q->where('status_rencana', 'valid');
            });

        if ($kampusId) {
            $query->where('kampus_id', $kampusId);
        }
        if ($prodiId) {
            $query->where('prodi_id', $prodiId);
        }
        if ($tahunAjaran) {
            $query->where('tahun_ajaran', $tahunAjaran);
        }
        if ($startDate) {
            $query->whereDate('created_at', '>=', $startDate);
        }
        if ($endDate) {
            $query->whereDate('created_at', '<=', $endDate);
        }

        $mahasiswas = $query->orderBy('tahun_ajaran', 'desc')->orderBy('created_at', 'desc')->paginate(15);

        // Inisialisasi total
        $totalSpb = 0;
        $totalSpp = 0;
        $totalUkt = 0;

        // Tambahkan data angsuran pertama dan semua biaya tetap ke setiap mahasiswa
        foreach ($mahasiswas as $mhs) {
            $angsuran = $mhs->angsuran;

            // Ambil semua biaya tetap dari ANGSURAN_DETAIL
            $biayaTetapList = [];
            $totalBiayaTetap = 0;

            if ($angsuran) {
                $biayaTetapDetails = AngsuranDetail::where('angsuran_id', $angsuran->id)
                    ->where('kode_biaya', 'like', 'BT-%')
                    ->where('dibayar', '>', 0)
                    ->where('pembayaran_valid', 'valid')
                    ->get();

                foreach ($biayaTetapDetails as $detail) {
                    $namaBiaya = preg_replace('/\s*\(Semester\s*\d+\)/', '', $detail->komponen);
                    $biayaTetapList[$namaBiaya] = $detail->dibayar;
                    $totalBiayaTetap += $detail->dibayar;
                }
            }

            $mhs->biaya_tetap_list = $biayaTetapList;

            if ($angsuran) {
                $mhs->spb1 = AngsuranDetail::where('angsuran_id', $angsuran->id)
                    ->where('kode_biaya', 'SPB_1')
                    ->where('dibayar', '>', 0)
                    ->where('pembayaran_valid', 'valid')
                    ->first();

                $mhs->spp1 = AngsuranDetail::where('angsuran_id', $angsuran->id)
                    ->where('kode_biaya', 'SPP1_1')
                    ->where('dibayar', '>', 0)
                    ->where('pembayaran_valid', 'valid')
                    ->first();

                $mhs->ukt1 = AngsuranDetail::where('angsuran_id', $angsuran->id)
                    ->where('kode_biaya', 'UKT1_1')
                    ->where('dibayar', '>', 0)
                    ->where('pembayaran_valid', 'valid')
                    ->first();

                $spbNominal = $mhs->spb1->dibayar ?? 0;
                $sppNominal = $mhs->spp1->dibayar ?? 0;
                $uktNominal = $mhs->ukt1->dibayar ?? 0;

                $totalSpb += $spbNominal;
                $totalSpp += $sppNominal;
                $totalUkt += $uktNominal;

                $mhs->total_herregistrasi = $spbNominal + $sppNominal + $uktNominal;
                $mhs->total_keseluruhan = $mhs->total_herregistrasi + $totalBiayaTetap;
            } else {
                $mhs->spb1 = null;
                $mhs->spp1 = null;
                $mhs->ukt1 = null;
                $mhs->total_herregistrasi = 0;
                $mhs->total_keseluruhan = $totalBiayaTetap;
            }
        }

        // Data untuk ringkasan (semua data)
        $ringkasanData = [];
        $totalBiayaHerregistrasi = 0;
        $totalBagiHasilKampus = 0;
        $totalBagiHasilK2 = 0;

        // Untuk menyimpan total per jenis biaya tetap
        $totalBiayaTetapPerJenis = [];
        $totalBagiHasilTetapKampus = [];
        $totalBagiHasilTetapK2 = [];

        foreach ($kampusList as $kampus) {
            $mhsQuery = Mahasiswa::where('kampus_id', $kampus->id)
                ->where('validasi_status', 'valid')
                ->whereHas('angsuran', function ($q) {
                    $q->where('status_rencana', 'valid');
                })
                ->when($tahunAjaran, fn($q) => $q->where('tahun_ajaran', $tahunAjaran))
                ->when($prodiId, fn($q) => $q->where('prodi_id', $prodiId))
                ->when($startDate, fn($q) => $q->whereDate('created_at', '>=', $startDate))
                ->when($endDate, fn($q) => $q->whereDate('created_at', '<=', $endDate));

            $mahasiswaKampus = $mhsQuery->get();
            $jumlahPendaftar = $mahasiswaKampus->count();

            if ($jumlahPendaftar > 0) {
                $bagiHasilKampus = json_decode($kampus->bagi_hasil, true) ?? ['kampus' => 70, 'k2' => 30];
                $persenKampus = $bagiHasilKampus['kampus'];
                $persenK2 = $bagiHasilKampus['k2'];

                $totalHerregistrasiKampus = 0;
                $detailPerJenjang = [];

                foreach ($mahasiswaKampus as $mhs) {
                    $prodi = $mhs->prodi;
                    $biayaAngsuranPertama = 0;

                    if ($prodi) {
                        $angsuran = $mhs->angsuran;
                        if ($angsuran) {
                            $spb1 = AngsuranDetail::where('angsuran_id', $angsuran->id)
                                ->where('kode_biaya', 'SPB_1')
                                ->where('dibayar', '>', 0)
                                ->where('pembayaran_valid', 'valid')
                                ->first();
                            if ($spb1)
                                $biayaAngsuranPertama += $spb1->dibayar;

                            $spp1 = AngsuranDetail::where('angsuran_id', $angsuran->id)
                                ->where('kode_biaya', 'SPP1_1')
                                ->where('dibayar', '>', 0)
                                ->where('pembayaran_valid', 'valid')
                                ->first();
                            if ($spp1)
                                $biayaAngsuranPertama += $spp1->dibayar;

                            $ukt1 = AngsuranDetail::where('angsuran_id', $angsuran->id)
                                ->where('kode_biaya', 'UKT1_1')
                                ->where('dibayar', '>', 0)
                                ->where('pembayaran_valid', 'valid')
                                ->first();
                            if ($ukt1)
                                $biayaAngsuranPertama += $ukt1->dibayar;
                        }
                    }

                    $totalHerregistrasiKampus += $biayaAngsuranPertama;

                    $jenjang = $prodi->jenjang ?? 'Unknown';
                    if (!isset($detailPerJenjang[$jenjang])) {
                        $detailPerJenjang[$jenjang] = [
                            'jumlah' => 0,
                            'total_biaya' => 0,
                            'rata_rata' => 0
                        ];
                    }
                    $detailPerJenjang[$jenjang]['jumlah']++;
                    $detailPerJenjang[$jenjang]['total_biaya'] += $biayaAngsuranPertama;
                }

                foreach ($detailPerJenjang as $jenjang => $detail) {
                    $detailPerJenjang[$jenjang]['rata_rata'] = $detail['total_biaya'] / max(1, $detail['jumlah']);
                }

                // Bagi hasil dari Total Biaya Herregistrasi
                $herregistrasiKampusBagiHasil = ($totalHerregistrasiKampus * $persenKampus) / 100;
                $herregistrasiK2BagiHasil = ($totalHerregistrasiKampus * $persenK2) / 100;

                // Proses semua biaya tetap
                $biayaTetapList = [];
                $totalBiayaTetapKampus = 0;
                $totalBagiHasilTetapKampusLocal = 0;
                $totalBagiHasilTetapK2Local = 0;

                $allAngsuranIds = $mahasiswaKampus->pluck('angsuran.id')->filter()->toArray();

                if (!empty($allAngsuranIds)) {
                    $biayaTetapDetails = AngsuranDetail::whereIn('angsuran_id', $allAngsuranIds)
                        ->where('kode_biaya', 'like', 'BT-%')
                        ->where('dibayar', '>', 0)
                        ->where('pembayaran_valid', 'valid')
                        ->get();

                    $groupedBiayaTetap = [];
                    foreach ($biayaTetapDetails as $detail) {
                        $namaBiaya = $detail->komponen;
                        $namaSederhana = preg_replace('/\s*\(Semester\s*\d+\)/', '', $namaBiaya);

                        if (!isset($groupedBiayaTetap[$namaSederhana])) {
                            $groupedBiayaTetap[$namaSederhana] = 0;
                        }
                        $groupedBiayaTetap[$namaSederhana] += $detail->dibayar;
                    }

                    foreach ($groupedBiayaTetap as $nama => $totalRupiah) {
                        $biayaTetapList[] = [
                            'nama' => $nama,
                            'per_mhs' => $totalRupiah / $jumlahPendaftar,
                            'total' => $totalRupiah,
                            'persen_kampus' => 100,
                            'persen_k2' => 0,
                            'bagi_hasil_kampus' => $totalRupiah,
                            'bagi_hasil_k2' => 0
                        ];

                        $totalBiayaTetapKampus += $totalRupiah;
                        $totalBagiHasilTetapKampusLocal += $totalRupiah;

                        if (!isset($totalBiayaTetapPerJenis[$nama])) {
                            $totalBiayaTetapPerJenis[$nama] = 0;
                            $totalBagiHasilTetapKampus[$nama] = 0;
                            $totalBagiHasilTetapK2[$nama] = 0;
                        }
                        $totalBiayaTetapPerJenis[$nama] += $totalRupiah;
                        $totalBagiHasilTetapKampus[$nama] += $totalRupiah;
                    }
                }

                $ringkasanData[$kampus->id] = [
                    'kampus' => $kampus,
                    'jumlah_pendaftar' => $jumlahPendaftar,
                    'detail_jenjang' => $detailPerJenjang,
                    'biaya_herregistrasi' => [
                        'total' => $totalHerregistrasiKampus,
                        'persen_kampus' => $persenKampus,
                        'persen_k2' => $persenK2,
                        'bagi_hasil_kampus' => $herregistrasiKampusBagiHasil,
                        'bagi_hasil_k2' => $herregistrasiK2BagiHasil
                    ],
                    'biaya_tetap' => $biayaTetapList,
                    'total_biaya_tetap' => $totalBiayaTetapKampus,
                    'total_biaya' => $totalHerregistrasiKampus + $totalBiayaTetapKampus,
                    'total_bagi_hasil_kampus' => $herregistrasiKampusBagiHasil + $totalBagiHasilTetapKampusLocal,
                    'total_bagi_hasil_k2' => $herregistrasiK2BagiHasil + $totalBagiHasilTetapK2Local
                ];

                $totalBiayaHerregistrasi += $totalHerregistrasiKampus;
                $totalBagiHasilKampus += $herregistrasiKampusBagiHasil;
                $totalBagiHasilK2 += $herregistrasiK2BagiHasil;
            }
        }

        // Hitung total semua biaya tetap dan bagi hasilnya
        $totalSemuaBiayaTetap = array_sum($totalBiayaTetapPerJenis);
        $totalSemuaBagiHasilTetapKampus = array_sum($totalBagiHasilTetapKampus);
        $totalSemuaBagiHasilTetapK2 = array_sum($totalBagiHasilTetapK2);

        // PPh 23 = 10% dari Total Biaya Herregistrasi x 2%
        $pph23 = ($totalBiayaHerregistrasi * 10 / 100) * 2 / 100;

        // Opsi filter
        $tahunAjaranOptions = Mahasiswa::where('validasi_status', 'valid')
            ->select('tahun_ajaran')
            ->distinct()
            ->whereNotNull('tahun_ajaran')
            ->orderBy('tahun_ajaran', 'desc')
            ->pluck('tahun_ajaran');

        $prodiOptions = [];
        if ($kampusId) {
            $prodiOptions = Prodi::where('kampus_id', $kampusId)->orderBy('nama')->get();
        }

        return view('laporan.alokasi-herregistrasi', compact(
            'mahasiswas',
            'ringkasanData',
            'kampusList',
            'prodiOptions',
            'tahunAjaranOptions',
            'kampusId',
            'prodiId',
            'tahunAjaran',
            'startDate',
            'endDate',
            'totalBiayaHerregistrasi',
            'totalBagiHasilKampus',
            'totalBagiHasilK2',
            'totalBiayaTetapPerJenis',
            'totalBagiHasilTetapKampus',
            'totalBagiHasilTetapK2',
            'totalSemuaBiayaTetap',
            'totalSemuaBagiHasilTetapKampus',
            'totalSemuaBagiHasilTetapK2',
            'pph23',
            'totalSpb',
            'totalSpp',
            'totalUkt'
        ));
    }

     public function previewPdf(Request $request)
    {
        $selectedIds = $request->selected_ids ? explode(',', $request->selected_ids) : [];

        $kampusId = $request->kampus_id;
        $prodiId = $request->prodi_id;
        $tahunAjaran = $request->tahun_ajaran;
        $startDate = $request->start_date;
        $endDate = $request->end_date;

        $query = Mahasiswa::with(['kampus', 'prodi', 'angsuran'])
            ->where('validasi_status', 'valid')
            ->whereHas('angsuran', function ($q) {
                $q->where('status_rencana', 'valid');
            });

        if ($kampusId)
            $query->where('kampus_id', $kampusId);
        if ($prodiId)
            $query->where('prodi_id', $prodiId);
        if ($tahunAjaran)
            $query->where('tahun_ajaran', $tahunAjaran);
        if ($startDate)
            $query->whereDate('created_at', '>=', $startDate);
        if ($endDate)
            $query->whereDate('created_at', '<=', $endDate);

        if (!empty($selectedIds)) {
            $query->whereIn('id', $selectedIds);
        }

        $mahasiswas = $query->orderBy('tahun_ajaran', 'desc')->orderBy('created_at', 'desc')->get();

        if ($mahasiswas->isEmpty()) {
            return redirect()->back()->with('error', 'Tidak ada data yang dipilih untuk dicetak');
        }

        $totalSpb = 0;
        $totalSpp = 0;
        $totalUkt = 0;

        foreach ($mahasiswas as $mhs) {
            $angsuran = $mhs->angsuran;

            $biayaTetapList = [];
            $totalBiayaTetap = 0;

            if ($angsuran) {
                $biayaTetapDetails = AngsuranDetail::where('angsuran_id', $angsuran->id)
                    ->where('kode_biaya', 'like', 'BT-%')
                    ->where('dibayar', '>', 0)
                    ->where('pembayaran_valid', 'valid')
                    ->get();

                foreach ($biayaTetapDetails as $detail) {
                    $namaBiaya = preg_replace('/\s*\(Semester\s*\d+\)/', '', $detail->komponen);
                    $biayaTetapList[$namaBiaya] = $detail->dibayar;
                    $totalBiayaTetap += $detail->dibayar;
                }
            }

            $mhs->biaya_tetap_list = $biayaTetapList;

            if ($angsuran) {
                $mhs->spb1 = AngsuranDetail::where('angsuran_id', $angsuran->id)
                    ->where('kode_biaya', 'SPB_1')
                    ->where('dibayar', '>', 0)
                    ->where('pembayaran_valid', 'valid')
                    ->first();

                $mhs->spp1 = AngsuranDetail::where('angsuran_id', $angsuran->id)
                    ->where('kode_biaya', 'SPP1_1')
                    ->where('dibayar', '>', 0)
                    ->where('pembayaran_valid', 'valid')
                    ->first();

                $mhs->ukt1 = AngsuranDetail::where('angsuran_id', $angsuran->id)
                    ->where('kode_biaya', 'UKT1_1')
                    ->where('dibayar', '>', 0)
                    ->where('pembayaran_valid', 'valid')
                    ->first();

                $spbNominal = $mhs->spb1->dibayar ?? 0;
                $sppNominal = $mhs->spp1->dibayar ?? 0;
                $uktNominal = $mhs->ukt1->dibayar ?? 0;

                $totalSpb += $spbNominal;
                $totalSpp += $sppNominal;
                $totalUkt += $uktNominal;

                $mhs->total_herregistrasi = $spbNominal + $sppNominal + $uktNominal;
                $mhs->total_keseluruhan = $mhs->total_herregistrasi + $totalBiayaTetap;
            } else {
                $mhs->spb1 = null;
                $mhs->spp1 = null;
                $mhs->ukt1 = null;
                $mhs->total_herregistrasi = 0;
                $mhs->total_keseluruhan = $totalBiayaTetap;
            }
        }

        // Data untuk ringkasan (semua data)
        $ringkasanData = [];
        $totalBiayaHerregistrasi = 0;
        $totalBagiHasilKampus = 0;
        $totalBagiHasilK2 = 0;
        $totalBiayaTetapPerJenis = [];
        $totalBagiHasilTetapKampus = [];
        $totalBagiHasilTetapK2 = [];

        $kampusList = Kampus::orderBy('nama')->get();

        foreach ($kampusList as $kampus) {
            $mahasiswaKampus = $mahasiswas->where('kampus_id', $kampus->id);
            $jumlahPendaftar = $mahasiswaKampus->count();

            if ($jumlahPendaftar > 0) {
                $bagiHasilKampus = json_decode($kampus->bagi_hasil, true) ?? ['kampus' => 70, 'k2' => 30];
                $persenKampus = $bagiHasilKampus['kampus'];
                $persenK2 = $bagiHasilKampus['k2'];

                $totalHerregistrasiKampus = 0;
                $detailPerJenjang = [];

                foreach ($mahasiswaKampus as $mhs) {
                    $prodi = $mhs->prodi;
                    $biayaAngsuranPertama = 0;

                    if ($prodi) {
                        $angsuran = $mhs->angsuran;
                        if ($angsuran) {
                            $spb1 = AngsuranDetail::where('angsuran_id', $angsuran->id)
                                ->where('kode_biaya', 'SPB_1')
                                ->where('dibayar', '>', 0)
                                ->where('pembayaran_valid', 'valid')
                                ->first();
                            if ($spb1)
                                $biayaAngsuranPertama += $spb1->dibayar;

                            $spp1 = AngsuranDetail::where('angsuran_id', $angsuran->id)
                                ->where('kode_biaya', 'SPP1_1')
                                ->where('dibayar', '>', 0)
                                ->where('pembayaran_valid', 'valid')
                                ->first();
                            if ($spp1)
                                $biayaAngsuranPertama += $spp1->dibayar;

                            $ukt1 = AngsuranDetail::where('angsuran_id', $angsuran->id)
                                ->where('kode_biaya', 'UKT1_1')
                                ->where('dibayar', '>', 0)
                                ->where('pembayaran_valid', 'valid')
                                ->first();
                            if ($ukt1)
                                $biayaAngsuranPertama += $ukt1->dibayar;
                        }
                    }

                    $totalHerregistrasiKampus += $biayaAngsuranPertama;
                    $jenjang = $prodi->jenjang ?? 'Unknown';
                    if (!isset($detailPerJenjang[$jenjang])) {
                        $detailPerJenjang[$jenjang] = ['jumlah' => 0, 'total_biaya' => 0, 'rata_rata' => 0];
                    }
                    $detailPerJenjang[$jenjang]['jumlah']++;
                    $detailPerJenjang[$jenjang]['total_biaya'] += $biayaAngsuranPertama;
                }

                foreach ($detailPerJenjang as $jenjang => $detail) {
                    $detailPerJenjang[$jenjang]['rata_rata'] = $detail['total_biaya'] / max(1, $detail['jumlah']);
                }

                $herregistrasiKampusBagiHasil = ($totalHerregistrasiKampus * $persenKampus) / 100;
                $herregistrasiK2BagiHasil = ($totalHerregistrasiKampus * $persenK2) / 100;

                $biayaTetapList = [];
                $totalBiayaTetapKampus = 0;
                $totalBagiHasilTetapKampusLocal = 0;
                $totalBagiHasilTetapK2Local = 0;

                $allAngsuranIds = $mahasiswaKampus->pluck('angsuran.id')->filter()->toArray();

                if (!empty($allAngsuranIds)) {
                    $biayaTetapDetails = AngsuranDetail::whereIn('angsuran_id', $allAngsuranIds)
                        ->where('kode_biaya', 'like', 'BT-%')
                        ->where('dibayar', '>', 0)
                        ->where('pembayaran_valid', 'valid')
                        ->get();

                    $groupedBiayaTetap = [];
                    foreach ($biayaTetapDetails as $detail) {
                        $namaBiaya = $detail->komponen;
                        $namaSederhana = preg_replace('/\s*\(Semester\s*\d+\)/', '', $namaBiaya);

                        if (!isset($groupedBiayaTetap[$namaSederhana])) {
                            $groupedBiayaTetap[$namaSederhana] = 0;
                        }
                        $groupedBiayaTetap[$namaSederhana] += $detail->dibayar;
                    }

                    foreach ($groupedBiayaTetap as $nama => $totalRupiah) {
                        $biayaTetapList[] = [
                            'nama' => $nama,
                            'per_mhs' => $totalRupiah / $jumlahPendaftar,
                            'total' => $totalRupiah,
                            'persen_kampus' => 100,
                            'persen_k2' => 0,
                            'bagi_hasil_kampus' => $totalRupiah,
                            'bagi_hasil_k2' => 0
                        ];

                        $totalBiayaTetapKampus += $totalRupiah;
                        $totalBagiHasilTetapKampusLocal += $totalRupiah;

                        if (!isset($totalBiayaTetapPerJenis[$nama])) {
                            $totalBiayaTetapPerJenis[$nama] = 0;
                            $totalBagiHasilTetapKampus[$nama] = 0;
                            $totalBagiHasilTetapK2[$nama] = 0;
                        }
                        $totalBiayaTetapPerJenis[$nama] += $totalRupiah;
                        $totalBagiHasilTetapKampus[$nama] += $totalRupiah;
                    }
                }

                $ringkasanData[$kampus->id] = [
                    'kampus' => $kampus,
                    'jumlah_pendaftar' => $jumlahPendaftar,
                    'detail_jenjang' => $detailPerJenjang,
                    'biaya_herregistrasi' => [
                        'total' => $totalHerregistrasiKampus,
                        'persen_kampus' => $persenKampus,
                        'persen_k2' => $persenK2,
                        'bagi_hasil_kampus' => $herregistrasiKampusBagiHasil,
                        'bagi_hasil_k2' => $herregistrasiK2BagiHasil
                    ],
                    'biaya_tetap' => $biayaTetapList,
                    'total_biaya_tetap' => $totalBiayaTetapKampus,
                    'total_biaya' => $totalHerregistrasiKampus + $totalBiayaTetapKampus,
                    'total_bagi_hasil_kampus' => $herregistrasiKampusBagiHasil + $totalBagiHasilTetapKampusLocal,
                    'total_bagi_hasil_k2' => $herregistrasiK2BagiHasil
                ];

                $totalBiayaHerregistrasi += $totalHerregistrasiKampus;
                $totalBagiHasilKampus += $herregistrasiKampusBagiHasil;
                $totalBagiHasilK2 += $herregistrasiK2BagiHasil;
            }
        }

        $totalSemuaBiayaTetap = array_sum($totalBiayaTetapPerJenis);
        $totalSemuaBagiHasilTetapKampus = array_sum($totalBagiHasilTetapKampus);
        $totalSemuaBagiHasilTetapK2 = array_sum($totalBagiHasilTetapK2);
        $pph23 = ($totalBiayaHerregistrasi * 10 / 100) * 2 / 100;

        $data = [
            'mahasiswas' => $mahasiswas,
            'ringkasanData' => $ringkasanData,
            'totalBiayaHerregistrasi' => $totalBiayaHerregistrasi,
            'totalBagiHasilKampus' => $totalBagiHasilKampus,
            'totalBagiHasilK2' => $totalBagiHasilK2,
            'totalBiayaTetapPerJenis' => $totalBiayaTetapPerJenis,
            'totalBagiHasilTetapKampus' => $totalBagiHasilTetapKampus,
            'totalBagiHasilTetapK2' => $totalBagiHasilTetapK2,
            'totalSemuaBiayaTetap' => $totalSemuaBiayaTetap,
            'totalSemuaBagiHasilTetapKampus' => $totalSemuaBagiHasilTetapKampus,
            'totalSemuaBagiHasilTetapK2' => $totalSemuaBagiHasilTetapK2,
            'totalPendaftar' => $mahasiswas->count(),
            'filter' => compact('kampusId', 'prodiId', 'tahunAjaran', 'startDate', 'endDate'),
            'tanggal_cetak' => now(),
            'pph23' => $pph23,
            'totalSpb' => $totalSpb,
            'totalSpp' => $totalSpp,
            'totalUkt' => $totalUkt
        ];

        $pdf = Pdf::loadView('laporan.pdf.alokasi-herregistrasi', $data);
        $pdf->setPaper('a4', 'landscape');
        return $pdf->stream('laporan-alokasi-herregistrasi.pdf');
    }

}
