@extends('documents.procurement._layout', [ 'documentTitle' => 'Landed Cost Report', 'reference' => $shipment->shipment_reference, 'documentDate' => now()->format('d M Y'), ]) @section('header-meta')
Status: {{ ucwords(str_replace('_', ' ', $shipment->status)) }}
@if($shipment->actual_arrival)
Arrived: {{ \Carbon\Carbon::parse($shipment->actual_arrival)->format('d M Y') }}
@endif @endsection @section('content') @php $items = $shipment->items; // Build a lookup: sourcingTripItemId → allocations keyed by type $allocsByItemType = []; foreach ($items as $item) { $id = $item->sourcing_trip_item_id; $allocsByItemType[$id] = []; foreach ($item->costAllocations as $alloc) { $allocsByItemType[$id][$alloc->allocation_type] = (float)($alloc->allocated_per_unit_base ?? 0); } } $types = ['freight', 'insurance', 'clearing_fee', 'haulage', 'tax', 'other_charge']; $typeLabels = [ 'freight' => 'Freight', 'insurance' => 'Insurance', 'clearing_fee' => 'CFA Fee', 'haulage' => 'Haulage', 'tax' => 'Tax', 'other_charge' => 'Other', ]; $colTotals = array_fill_keys($types, 0); $colTotals['source_cost'] = 0; $colTotals['landed_total'] = 0; $colTotals['landed_unit'] = 0; @endphp {{-- Cost allocation method key --}} @if($items->isNotEmpty() && $items->first()->costAllocations->isNotEmpty()) @php $methodKey = collect($items->flatMap->costAllocations) ->pluck('method_used') ->unique() ->filter() ->values(); @endphp @if($methodKey->count())

Allocation method(s): {{ $methodKey->map(fn($m) => ucwords(str_replace('_', ' ', $m)))->join(', ') }}

@endif @endif {{-- freight --}} {{-- insurance --}} {{-- clearing_fee --}} {{-- haulage --}} {{-- tax --}} {{-- other --}} {{-- landed total --}} {{-- landed/unit --}} @foreach($types as $type) @endforeach @foreach($items as $i => $item) @php $tripItem = $item->sourcingTripItem; $id = $item->sourcing_trip_item_id; $allocs = $allocsByItemType[$id] ?? []; $sourceCostPerUnit = $tripItem?->unit_source_price_base ? (float)$tripItem->unit_source_price_base : 0; $allocSum = array_sum($allocs); $landedPerUnit = $sourceCostPerUnit + $allocSum; $landedTotal = $landedPerUnit * $item->quantity_shipped; $colTotals['source_cost'] += $sourceCostPerUnit * $item->quantity_shipped; $colTotals['landed_total'] += $landedTotal; foreach ($types as $t) { $colTotals[$t] += ($allocs[$t] ?? 0) * $item->quantity_shipped; } @endphp @foreach($types as $type) @endforeach @endforeach @foreach($types as $type) @endforeach
# Product Qty Src Cost/Unit{{ $typeLabels[$type] }}/uLanded Total Landed/Unit
{{ $i + 1 }} {{ $tripItem?->product?->name ?? '—' }} {{ number_format($item->quantity_shipped, 0) }} {{ $sourceCostPerUnit > 0 ? number_format($sourceCostPerUnit, 4) : '—' }} {{ isset($allocs[$type]) && $allocs[$type] > 0 ? number_format($allocs[$type], 4) : '—' }} {{ $landedTotal > 0 ? number_format($landedTotal, 2) : '—' }} {{ $landedPerUnit > 0 ? number_format($landedPerUnit, 4) : '—' }}
Totals (base currency) {{ $colTotals['source_cost'] > 0 ? number_format($colTotals['source_cost'], 2) : '—' }}{{ $colTotals[$type] > 0 ? number_format($colTotals[$type], 2) : '—' }}{{ $colTotals['landed_total'] > 0 ? number_format($colTotals['landed_total'], 2) : '—' }}

All figures are in the organisation's base currency. Source cost per unit converted at the trip's exchange rate.

@endsection