@php // Put each department on a new page? $pageBreakEachDept = true; // Group by department_id (controller provides department_id in selects) $earningsByDept = ($summaryDataEarnings ?? collect())->groupBy('department_id'); $contributionsByDept = ($summaryDataContributions ?? collect())->groupBy('department_id'); $basicPaysByDept = ($summaryDataBasicPays ?? collect())->groupBy('department_id'); //dd($basicPaysByDept,$earningsByDept, $contributionsByDept); // Union of all department ids to ensure none are missed $deptIds = collect() ->merge($earningsByDept->keys()) ->merge($contributionsByDept->keys()) ->merge($basicPaysByDept->keys()) ->unique() ->values(); // Helper to safely get names from maps (maps are arrays like [id => ['name'=>..., 'unit_id'=>...]]) $getUnitName = function ($unitId) use ($unitMap) { return $unitMap[$unitId]['name'] ?? ($unitId ? ('Unit '.$unitId) : 'Unit ?'); }; $getDeptName = function ($deptId) use ($departmentMap) { return $departmentMap[$deptId]['name'] ?? ('Dept '.$deptId); }; $getDeptUnitId = function ($deptId) use ($departmentMap) { return $departmentMap[$deptId]['unit_id'] ?? null; }; // Sort department ids by Unit Name then Department Name $sortedDeptIds = $deptIds->sort(function($a, $b) use ($getUnitName, $getDeptName, $getDeptUnitId) { $ua = $getUnitName($getDeptUnitId($a)); $ub = $getUnitName($getDeptUnitId($b)); $an = $getDeptName($a); $bn = $getDeptName($b); return [$ua, $an] <=> [$ub, $bn]; }); // Overall totals (controller passes these) $overallTotalEmployees = $totalEmployees ?? 0; $overallTotalEarnings = $totalEarnings ?? 0; $overallTotalContributions = $totalContributions ?? 0; @endphp {{-- Optional watermark --}}
Watermark Logo
CONFIDENTIAL
Logo

Employee Earnings and Company Contribution By Cost Centre

{{ $pamPayRunPeriod->calendar_month }} - {{ $pamPayRunPeriod->calendar_year }}

{{ $pamEmpUnit->name ?? 'All Units' }}

Executive Summary
Payroll Name {{ $pamPayroll->short_description ?? 'N/A' }}
Period {{ \Carbon\Carbon::parse($pamPayRunPeriod->start_date)->format('Y-m-d') }} to {{ \Carbon\Carbon::parse($pamPayRunPeriod->end_date)->format('Y-m-d') }}
Total Employees {{ $overallTotalEmployees }}
Total Employees Earnings {{ number_format($overallTotalEarnings, 2, '.', ',') }}
Total Company Contributions {{ number_format($overallTotalContributions, 2, '.', ',') }}
{{-- ===== Department Sections ===== --}} @php $first = true; @endphp @foreach($sortedDeptIds as $deptId) @php $earnings = $earningsByDept[$deptId] ?? collect(); // rows: department_id, short_description, fixed_amount $deptContribs= $contributionsByDept[$deptId] ?? collect(); // rows: department_id, short_description, fixed_amount $deptBasics = $basicPaysByDept[$deptId] ?? collect(); // rows: department_id, short_description, fixed_amount // Names from maps $deptName = $getDeptName($deptId); $unitId = $getDeptUnitId($deptId); $unitName = $getUnitName($unitId); // Build "other earnings" by excluding BASIC short_descriptions present in $deptBasics $basicDescSet = $deptBasics->pluck('short_description')->filter()->values()->all(); $deptOtherEarnings = $earnings->reject(function ($row) use ($basicDescSet) { return in_array($row->short_description, $basicDescSet, true); }); // Totals $deptContribTotal = (float) $deptContribs->sum('fixed_amount'); $deptBasicsTotal = (float) $deptBasics->sum('fixed_amount'); $deptOtherEarnTotal = (float) $deptOtherEarnings->sum('fixed_amount'); $deptTotalEarnings = $deptBasicsTotal + $deptOtherEarnTotal; $deptGrandTotal = $deptTotalEarnings + $deptContribTotal; $deptEmployeeCount = (int) (($empCountsByDept ?? collect())[$deptId] ?? 0); @endphp @if($pageBreakEachDept && !$first)
@endif @php $first = false; @endphp

Cost Centre: {{ $unitName }} — {{ $deptName }}

Executive Summary — {{ $deptName }}
Total Employees (Cost Centre) {{ number_format($deptEmployeeCount) }}
Total Earnings (Employees) {{ number_format($deptTotalEarnings, 2, '.', ',') }}
  • Basic Pay {{ number_format($deptBasicsTotal, 2, '.', ',') }}
  • Other Earnings {{ number_format($deptOtherEarnTotal, 2, '.', ',') }}
Total Company Contributions {{ number_format($deptContribTotal, 2, '.', ',') }}
Department Grand Total {{ number_format($deptGrandTotal, 2, '.', ',') }}
@forelse($deptContribs as $row) @empty @endforelse
1. Contributions
Description Amount
{{ $row->short_description ?? 'Contribution' }} {{ number_format($row->fixed_amount ?? 0, 2, '.', ',') }}
No contributions
Subtotal {{ number_format($deptContribTotal, 2, '.', ',') }}
{{-- Basic Pay --}} @forelse($deptBasics as $row) @empty {{-- no basic rows --}} @endforelse {{-- Other Earnings --}} @forelse($deptOtherEarnings as $row) @empty @if($deptBasics->isEmpty()) @endif @endforelse
2. Earnings
Description Amount
{{ ($row->short_description ?? 'Basic Pay') }} {{ number_format($row->fixed_amount ?? 0, 2, '.', ',') }}
{{ $row->short_description ?? 'Earning' }} {{ number_format($row->fixed_amount ?? 0, 2, '.', ',') }}
No earnings
Subtotal {{ number_format($deptTotalEarnings, 2, '.', ',') }}
@endforeach
Prepared By


{{ auth()->user()->name ?? auth()->user()->username ?? 'N/A' }}

Date: {{ \Carbon\Carbon::now()->format('d-m-Y H:i') }}
Approved By


___________________________

Date: ___________
Confidential • Payroll Summary • {{ now()->format('F j, Y, g:i A') }}