@php
// Recursive function to render subcategories with indentation
function renderItem($item, $level = 0) {
$padding = $level * 20; // 20px per level
@endphp
|
{{ $item->name }} @if(isset($item->code)) ({{ $item->code }}) @endif
|
{{ number_format($item->total ?? 0, 2) }} |
@php
if(!empty($item->children)){
foreach($item->children as $child){
renderItem($child, $level + 1);
}
}
}
@endphp
@php
// Helper to get subcategories from current data
function getSubcategories($current, $sectionName){
// Flatten all items across sections
$items = collect($current)
->flatMap(fn($section) => $section->items ?? []);
// Find the matching item by name
$item = $items->firstWhere('name', $sectionName);
// Return its subcategories
return collect($item->subcategories ?? []);
}
// Extract subcategories
$salesSubcats = getSubcategories($current, 'Sales Revenue');
$directSubcats = getSubcategories($current, 'Direct Costs');
$otherIncomeSubcats = getSubcategories($current, 'Other Income');
$operatingSubcats = getSubcategories($current, 'Operating Expenses');
$interestSubcats = getSubcategories($current, 'Interest Expenses');
$taxSubcats = getSubcategories($current, 'Taxes Expenses'); // ✅ use "Taxes Expenses" as per your JSON
// Compute totals
$salesTotal = $salesSubcats->sum(fn($s) => $s->total ?? 0);
$directTotal = $directSubcats->sum(fn($s) => $s->total ?? 0);
$otherIncomeTotal = $otherIncomeSubcats->sum(fn($s) => $s->total ?? 0);
$operatingTotal = $operatingSubcats->sum(fn($s) => $s->total ?? 0);
$interestTotal = $interestSubcats->sum(fn($s) => $s->total ?? 0);
$taxTotal = $taxSubcats->sum(fn($s) => $s->total ?? 0);
// Calculations
$grossProfit = $salesTotal - $directTotal;
$operatingProfit = $grossProfit + $otherIncomeTotal - $operatingTotal;
$ebt = $operatingProfit - $interestTotal;
$netProfit = $ebt - $taxTotal;
@endphp
@foreach($salesSubcats as $subcat)
@php renderItem($subcat); @endphp
@endforeach
| Total Sales Revenue |
{{ number_format($salesTotal,2) }} |
@foreach($directSubcats as $subcat)
@php renderItem($subcat); @endphp
@endforeach
| Total Direct Costs |
{{ number_format($directTotal,2) }} |
| Gross Profit |
{{ number_format($grossProfit,2) }} |
@foreach($otherIncomeSubcats as $subcat)
@php renderItem($subcat); @endphp
@endforeach
| Total Other Income |
{{ number_format($otherIncomeTotal,2) }} |
@foreach($operatingSubcats as $subcat)
@php renderItem($subcat); @endphp
@endforeach
| Total Operating Expenses |
{{ number_format($operatingTotal,2) }} |
| Operating Profit |
{{ number_format($operatingProfit,2) }} |
@foreach($interestSubcats as $subcat)
@php renderItem($subcat); @endphp
@endforeach
| Total Interest Expenses |
{{ number_format($interestTotal,2) }} |
| Earnings Before Tax (EBT) |
{{ number_format($ebt,2) }} |
@foreach($taxSubcats as $subcat)
@php renderItem($subcat); @endphp
@endforeach
| Total Income Tax Expenses |
{{ number_format($taxTotal,2) }} |
| Net Profit / Loss |
{{ number_format($netProfit,2) }} |