@extends('layouts.admin') @section('title', 'Cost & Profit — ' . $sale->invoice_no) @section('content') @php $rows = $sale->items->map(function ($i) use ($alloc) { $parts = $alloc[$i->id] ?? collect(); $cost = $parts->isEmpty() ? round((float) $i->quantity * (float) $i->unit_cost, 2) : round($parts->sum(fn ($a) => (float) $a->quantity * (float) $a->unit_cost), 2); $rev = round((float) $i->line_total, 2); // before VAT return ['item' => $i, 'parts' => $parts, 'rev' => $rev, 'cost' => $cost, 'profit' => round($rev - $cost, 2)]; }); $totRev = $rows->sum('rev'); $totCost = $rows->sum('cost'); $totProfit = round($totRev - $totCost, 2); $margin = $totRev > 0 ? round($totProfit / $totRev * 100, 1) : 0; @endphp
Sales/ {{ $sale->invoice_no }}/ Cost & profit
@foreach ([ ['Sales (before VAT)', 'AED ' . number_format($totRev, 2), 'text-sky-500'], ['Cost (FIFO)', 'AED ' . number_format($totCost, 2), 'text-amber-500'], ['Profit', 'AED ' . number_format($totProfit, 2), $totProfit >= 0 ? 'text-emerald-500' : 'text-red-500'], ['Margin', $margin . '%', $totProfit >= 0 ? 'text-emerald-500' : 'text-red-500'], ] as [$l, $v, $c])
{{ $l }}
{{ $v }}
@endforeach
@foreach ($rows as $r) @php $i = $r['item']; @endphp
{{ $i->product_name }}
{{ $i->product_code }} · Sold {{ (float) $i->quantity }} {{ $i->unit }} at AED {{ number_format($i->unit_price, 2) }} (before VAT)
Sales AED {{ number_format($r['rev'], 2) }} · Cost AED {{ number_format($r['cost'], 2) }}
Profit AED {{ number_format($r['profit'], 2) }}
@forelse ($r['parts'] as $a) @php $b = $a->batch; @endphp @empty @endforelse
Taken from stock lot Lot date Supplier Qty taken Cost / unit Cost
@if ($b) {{ $b->purchase?->reference ?? 'Opening stock' }} @else No lot (product cost used) @endif {{ $b?->received_date?->format('d M Y') ?? '—' }} {{ $b?->purchase?->supplier?->name ?? '—' }} {{ (float) $a->quantity }} {{ number_format($a->unit_cost, 2) }} {{ number_format($a->quantity * $a->unit_cost, 2) }}
Sold before FIFO tracking — single cost recorded {{ (float) $i->quantity }} {{ number_format($i->unit_cost, 2) }} {{ number_format($r['cost'], 2) }}
@endforeach
← Back to sale
@endsection