@php function variance($a, $b) { return $a - $b; } function renderItem($item, $period1Totals, $period2Totals, $level = 0) { $padding = str_repeat(" ", $level); $p1 = $period1Totals[$item->id] ?? $item->total ?? 0; $p2 = $period2Totals[$item->id] ?? $item->total ?? 0; echo ""; if(!empty($item->children)){ foreach($item->children as $child){ renderItem($child, $period1Totals, $period2Totals, $level + 1); } } } function getSubcategories($current, $sectionName) { $section = collect($current)->firstWhere('name', $sectionName); return collect($section->subcategories ?? []); } $totals1 = []; $totals2 = []; foreach($current['period1'] ?? [] as $section){ foreach($section->subcategories ?? [] as $sub){ $totals1[$sub->id] = $sub->total ?? 0; } } foreach($current['period2'] ?? [] as $section){ foreach($section->subcategories ?? [] as $sub){ $totals2[$sub->id] = $sub->total ?? 0; } } $sections = ['Sales Revenue','Direct Costs','Other Income','Operating Expenses','Interest Expenses','Income Tax Expenses']; @endphp @foreach($sections as $sec) @php $subcats1 = getSubcategories($current['period1'] ?? [], $sec); foreach($subcats1 as $subcat){ renderItem($subcat, $totals1, $totals2); } @endphp @endforeach @php $sales1 = getSubcategories($current['period1'] ?? [], 'Sales Revenue')->sum(fn($s)=>$totals1[$s->id]??0); $sales2 = getSubcategories($current['period2'] ?? [], 'Sales Revenue')->sum(fn($s)=>$totals2[$s->id]??0); $direct1 = getSubcategories($current['period1'] ?? [], 'Direct Costs')->sum(fn($s)=>$totals1[$s->id]??0); $direct2 = getSubcategories($current['period2'] ?? [], 'Direct Costs')->sum(fn($s)=>$totals2[$s->id]??0); $other1 = getSubcategories($current['period1'] ?? [], 'Other Income')->sum(fn($s)=>$totals1[$s->id]??0); $other2 = getSubcategories($current['period2'] ?? [], 'Other Income')->sum(fn($s)=>$totals2[$s->id]??0); $oper1 = getSubcategories($current['period1'] ?? [], 'Operating Expenses')->sum(fn($s)=>$totals1[$s->id]??0); $oper2 = getSubcategories($current['period2'] ?? [], 'Operating Expenses')->sum(fn($s)=>$totals2[$s->id]??0); $interest1 = getSubcategories($current['period1'] ?? [], 'Interest Expenses')->sum(fn($s)=>$totals1[$s->id]??0); $interest2 = getSubcategories($current['period2'] ?? [], 'Interest Expenses')->sum(fn($s)=>$totals2[$s->id]??0); $tax1 = getSubcategories($current['period1'] ?? [], 'Income Tax Expenses')->sum(fn($s)=>$totals1[$s->id]??0); $tax2 = getSubcategories($current['period2'] ?? [], 'Income Tax Expenses')->sum(fn($s)=>$totals2[$s->id]??0); $gross1 = $sales1 - $direct1; $gross2 = $sales2 - $direct2; $operating1 = $gross1 + $other1 - $oper1; $operating2 = $gross2 + $other2 - $oper2; $ebt1 = $operating1 - $interest1; $ebt2 = $operating2 - $interest2; $net1 = $ebt1 - $tax1; $net2 = $ebt2 - $tax2; @endphp
Income Statement Comparison - {{ $organisation->organisation_name }}
Particulars {{ $period1Label }} {{ $period2Label }} Variance
{$padding}{$item->name}" . (isset($item->code) ? " ({$item->code})" : "") . " {$p1} {$p2} " . variance($p1,$p2) . "
{{ $sec }}
Total {{ $sec }} {{ $subcats1->sum(fn($s) => $totals1[$s->id] ?? 0) }} {{ $subcats1->sum(fn($s) => $totals2[$s->id] ?? 0) }} {{ $subcats1->sum(fn($s) => ($totals1[$s->id] ?? 0) - ($totals2[$s->id] ?? 0)) }}
Gross Profit {{ $gross1 }} {{ $gross2 }} {{ variance($gross1,$gross2) }}
Operating Profit {{ $operating1 }} {{ $operating2 }} {{ variance($operating1,$operating2) }}
Earnings Before Tax (EBT) {{ $ebt1 }} {{ $ebt2 }} {{ variance($ebt1,$ebt2) }}
Net Profit / Loss {{ $net1 }} {{ $net2 }} {{ variance($net1,$net2) }}