@extends('print.layout') @section('title', 'Customer_Ledger_' . ($customer['customer_no'] ?? 'N/A') . '_' . ($dfrom ?? 'All') . '_to_' . ($dto ?? 'Current')) @section('content')
Customer Ledger
{{-- Customer Information --}}
Customer No: {{ $customer['customer_no'] ?? 'N/A' }}
Customer Name: {{ ($customer['fname'] ?? '') . ' ' . ($customer['lname'] ?? '') }}
Email: {{ $customer['email'] ?? 'N/A' }}
Phone: {{ $customer['phone'] ?? 'N/A' }}
Report Period: {{ $dfrom ?? 'All Time' }} to {{ $dto ?? 'Current' }}
Opening Balance: {{ number_format($oBalance ?? 0, 2) }}
Country: {{ $customer['coname'] ?? 'N/A' }}
Currency: {{ $customer['cuname'] ?? 'N/A' }}
{{-- Address Information --}} @if(isset($customer['address']) && !empty($customer['address']))

Address

{{ $customer['address'] }}
@endif {{-- Ledger Transactions Table --}} @if(isset($detail) && $detail->count() > 0)

Transaction History

@php $runningBalance = $oBalance ?? 0; @endphp @if($oBalance != 0) @endif @foreach($detail as $transaction) @php $debit = $transaction->debit ?? 0; $credit = $transaction->credit ?? 0; // For customer ledger: // - Deliveries (stored in debit) should display in Debit column // - Payments (stored in debit) should display in Credit column // - For order payments with cc_amount, use cc_amount instead of debit // - For general vouchers (transaction_type = 'generalVoucher'): // - Use cc_amount for display and balance // - If credit IS NULL: display cc_amount in Credit column (customer credit) // - If debit IS NULL: display cc_amount in Debit column (customer charge) $isPayment = isset($transaction->transaction_type) && $transaction->transaction_type == 'orderPayment'; $isGeneralVoucher = isset($transaction->transaction_type) && $transaction->transaction_type == 'generalVoucher'; if ($isGeneralVoucher && !empty($transaction->cc_amount)) { // General Voucher: stored in cc_amount // Determine column by strict NULL check: // - If credit is NULL: display cc_amount in Credit column (customer credit) // - If debit is NULL: display cc_amount in Debit column (customer charge) if (is_null($transaction->credit)) { // Credit Voucher (Credit to Customer) - credit IS NULL $displayDebit = 0; $displayCredit = $transaction->cc_amount; $runningBalance += $transaction->cc_amount; // Decreases customer liability (increases balance) } else if (is_null($transaction->debit)) { // Debit Voucher (Charge to Customer) - debit IS NULL $displayDebit = $transaction->cc_amount; $displayCredit = 0; $runningBalance -= $transaction->cc_amount; // Increases customer liability (reduces balance) } else { // Fallback: both have values, shouldn't happen but handle gracefully $displayDebit = 0; $displayCredit = 0; } } elseif ($isPayment) { // Payment: stored in debit, but display in credit column // Use cc_amount if available (customer currency), otherwise use debit (PKR) $displayDebit = 0; $displayCredit = !empty($transaction->cc_amount) ? $transaction->cc_amount : $debit; // For balance calculation, use cc_amount if available $amountToUse = !empty($transaction->cc_amount) ? $transaction->cc_amount : $debit; $runningBalance += $amountToUse; // Payment increases balance (reduces receivable) } else { // Delivery: stored in debit, display in debit column $displayDebit = $debit; $displayCredit = $credit; $runningBalance += $credit - $debit; } @endphp @endforeach
@endif {{-- Summary Section --}}
Total Sales: {{ number_format($detail->sum('debit') ?? 0, 2) }}
Total Payments: {{ number_format($detail->sum('credit') ?? 0, 2) }}
Current Balance: {{ number_format($balance ?? 0, 2) }}
{{-- Balance Status --}}

Account Status

@if($balance > 0) Credit Balance: Customer owes company {{ number_format(abs($balance), 2) }} @elseif($balance < 0) Debit Balance: Company owes customer {{ number_format(abs($balance), 2) }} @else Balanced Account: No outstanding amount @endif
@endsection