{{-- resources/views/customers/show.blade.php --}} @extends('layouts.app') @section('title', $customer->name) @section('content') @php $opening = (float) ($customer->opening_balance ?? 0); $totalSales = isset($sales) ? (float) $sales->sum('total') : 0; $totalPaid = isset($sales) ? (float) $sales->sum('paid') : 0; $totalReturns = (float) $returns->sum('total'); $currentDue = $opening + $totalSales - $totalPaid - $totalReturns; @endphp

{{ $customer->name }}

Phone: {{ $customer->phone ?? '-' }} | Address: {{ $customer->address ?? '-' }}
Opening Balance: {{ number_format($opening, 2) }}
Total Sales: {{ number_format($totalSales, 2) }}
Total Paid: {{ number_format($totalPaid, 2) }}
Total Returns: {{ number_format($totalReturns, 2) }}
Current Due: {{ number_format($currentDue, 2) }}
← Back to Customers {{-- 🔸 Sales History --}}
Sales History
@if($sales->isEmpty())

No sales for this customer yet.

@else @foreach($sales as $sale) @php $collapseId = 'sale_items_' . $sale->id; @endphp @endforeach
Date Invoice Total Paid Due Status Next Due Items
{{ $sale->date ? \Carbon\Carbon::parse($sale->date)->format('d-m-Y') : '-' }} {{ $sale->invoice_no }} {{ number_format($sale->total ?? 0, 2) }} {{ number_format($sale->paid ?? 0, 2) }} {{ number_format($sale->due ?? 0, 2) }} @if($sale->status === 'paid') Paid @else Due @endif {{ $sale->next_due_date ? \Carbon\Carbon::parse($sale->next_due_date)->format('d-m-Y') : '-' }}
@if($sale->items->isEmpty()) No items found. @else
@foreach($sale->items as $it) @endforeach
Product Qty Price Line Total
{{ $it->product->name ?? 'Deleted product' }} {{ $it->qty }} {{ number_format((float)($it->price ?? 0), 2) }} {{ number_format((float)(($it->line_total ?? 0) ?: (($it->qty ?? 0) * ($it->price ?? 0))), 2) }}
@endif
@endif
{{-- 🔹 Payment History (same style: Click to view details) --}}
Payment History
@if($payments->isEmpty())

No payments from this customer yet.

@else @foreach($payments as $pay) @php $payCollapse = 'pay_details_' . $pay->id; @endphp @endforeach
Date Method Amount Ref No Note Details
{{ $pay->date ? \Carbon\Carbon::parse($pay->date)->format('d-m-Y') : '-' }} {{ $pay->method ?? '-' }} {{ number_format((float)($pay->amount ?? 0), 2) }} {{ $pay->ref_no ?? '-' }} {{ $pay->note ?? '-' }}
Date: {{ $pay->date ? \Carbon\Carbon::parse($pay->date)->format('d-m-Y') : '-' }}
Method: {{ $pay->method ?? '-' }}
Amount: {{ number_format((float)($pay->amount ?? 0), 2) }}
Ref: {{ $pay->ref_no ?? '-' }}
Note: {{ $pay->note ?? '-' }}
@endif
{{-- 🔥 Return History (same style: Click to view items) --}}
Return History
@if($returns->isEmpty())

No return entries for this customer.

@else @foreach($returns as $r) @php $retCollapse = 'ret_items_' . $r->id; @endphp @endforeach
Date Type Total Note Items
@if($r->date) {{ $r->date instanceof \Carbon\Carbon ? $r->date->format('d-m-Y') : \Carbon\Carbon::parse($r->date)->format('d-m-Y') }} @else - @endif {{ $r->type ?? 'customer' }} {{ number_format((float)($r->total ?? 0), 2) }} {{ $r->note ?? '-' }}
@php // ✅ Supports both: $r->items (grouped return) OR single-row return ($r->product) $hasItemsRelation = isset($r->items); @endphp @if($hasItemsRelation && $r->items && $r->items->count())
@foreach($r->items as $it) @endforeach
Product Qty Unit Price Total
{{ $it->product->name ?? 'Deleted product' }} {{ $it->qty }} {{ number_format((float)($it->unit_price ?? 0), 2) }} {{ number_format((float)($it->total ?? (($it->qty ?? 0) * ($it->unit_price ?? 0))), 2) }}
@else {{-- fallback (your old return structure: one row = one product) --}}
Product Qty Unit Price Total
{{ optional($r->product)->name ?? '—' }} {{ $r->qty ?? 0 }} {{ number_format((float)($r->unit_price ?? 0), 2) }} {{ number_format((float)($r->total ?? 0), 2) }}
@endif
@endif
@endsection