@php $allItems = $quote->sections->flatMap->items; $grouped = $allItems->groupBy('material_type_id'); $types = \App\Models\MaterialType::orderBy('sort_order')->get()->keyBy('id'); $grandRetail = 0; @endphp @foreach($grouped as $typeId => $items) @php $typeName = $typeId ? ($types[$typeId]->name ?? 'Items') : 'Items'; $typeRetail = $items->sum(fn($i) => (float)($i->total_retail ?? ($i->qty * $i->retail_price))); $grandRetail += $typeRetail; @endphp @endforeach @php $gst = $grandRetail * 0.10; $total = $grandRetail + $gst; @endphp