@extends('layouts.app')
@section('title',($receipt->exists?'Edit':'New').' Receipt')
@section('page_title',($receipt->exists?'Edit':'New').' Receipt Voucher')
@section('content')
<div class="page-head"><div><h1>{{ $receipt->exists?'Edit':'New' }} Receipt</h1><p>Allocate against invoices or leave the remaining amount as a customer advance.</p></div><a class="btn btn-secondary" href="{{ $receipt->exists?route('receipts.show',$receipt):route('receipts.index') }}">Back</a></div>
<form method="post" action="{{ $receipt->exists?route('receipts.update',$receipt):route('receipts.store') }}">@csrf @if($receipt->exists)@method('put')@endif
<div class="card"><div class="card-body"><div class="form-grid">
<div class="field"><label>Branch</label><select class="control" name="branch_id">@foreach($branches as $b)<option value="{{ $b->id }}" @selected(old('branch_id',$receipt->branch_id??auth()->user()->branch_id)==$b->id)>{{ $b->code }} - {{ $b->name }}</option>@endforeach</select></div><div class="field"><label>Receipt Type</label><select class="control" name="receipt_type" id="document-type"><option value="customer_receipt" @selected(old('receipt_type',$receipt->receipt_type)==='customer_receipt')>Customer Receipt</option><option value="vendor_refund" @selected(old('receipt_type',$receipt->receipt_type)==='vendor_refund')>Vendor Refund / Recovery</option><option value="other_receipt" @selected(old('receipt_type',$receipt->receipt_type)==='other_receipt')>Other Receipt</option></select></div>
<div class="field"><label>Party</label><select class="control" name="party_id" id="party-select" required><option value="">Select party</option>@foreach($parties as $p)<option value="{{ $p->id }}" @selected(old('party_id',$receipt->party_id)==$p->id)>{{ $p->legal_name }}</option>@endforeach</select></div><div class="field"><label>Receipt Date</label><input class="control" type="date" name="receipt_date" value="{{ old('receipt_date',optional($receipt->receipt_date)->format('Y-m-d')) }}" required></div><div class="field"><label>Amount</label><input class="control" type="number" step="0.01" name="amount" id="amount" value="{{ old('amount',$receipt->amount) }}" required></div><div class="field"><label>Payment Method</label><select class="control" name="payment_method">@foreach(['cash'=>'Cash','bank_transfer'=>'Bank Transfer','cheque'=>'Cheque','card'=>'Card','online'=>'Online Payment','other'=>'Other'] as $k=>$v)<option value="{{ $k }}" @selected(old('payment_method',$receipt->payment_method)===$k)>{{ $v }}</option>@endforeach</select></div><div class="field"><label>Currency</label><input class="control" name="currency_code" value="{{ old('currency_code',$receipt->currency_code??'AED') }}"></div><div class="field"><label>Exchange Rate</label><input class="control" type="number" step="0.00000001" name="exchange_rate" value="{{ old('exchange_rate',$receipt->exchange_rate??1) }}"></div>
<div class="field"><label>Cash / Bank Account</label><select class="control" name="cash_bank_account_id" required><option value="">Select</option>@foreach($accounts as $a)<option value="{{ $a->id }}" @selected(old('cash_bank_account_id',$receipt->cash_bank_account_id)==$a->id)>{{ $a->code }} - {{ $a->name }}</option>@endforeach</select></div><div class="field"><label>Receivable / Counterparty Account</label><select class="control" name="receivable_account_id" required><option value="">Select</option>@foreach($accounts as $a)<option value="{{ $a->id }}" @selected(old('receivable_account_id',$receipt->receivable_account_id)==$a->id)>{{ $a->code }} - {{ $a->name }}</option>@endforeach</select></div><div class="field"><label>Customer Advance Account</label><select class="control" name="customer_advance_account_id"><option value="">Not required if fully allocated</option>@foreach($accounts as $a)<option value="{{ $a->id }}" @selected(old('customer_advance_account_id',$receipt->customer_advance_account_id)==$a->id)>{{ $a->code }} - {{ $a->name }}</option>@endforeach</select></div><div class="field"><label>Cheque / Bank Reference</label><input class="control" name="reference_number" value="{{ old('reference_number',$receipt->reference_number) }}"></div><div class="field full"><label>Description</label><textarea class="control" name="description">{{ old('description',$receipt->description) }}</textarea></div>
</div></div></div>
<div class="card" style="margin-top:18px" id="allocation-card"><div class="card-head"><strong>Customer Invoice Allocation</strong><span class="muted">Enter amounts only for this customer</span></div><div class="table-wrap"><table class="table"><thead><tr><th>Invoice</th><th>Customer</th><th>Date</th><th>Balance</th><th>Allocate</th></tr></thead><tbody>@php($saved=old('allocations',$receipt->allocations->pluck('amount','customer_invoice_id')->all()))@foreach($invoices as $inv)<tr class="allocation-row" data-party="{{ $inv->party_id }}"><td>{{ $inv->invoice_number }}</td><td>{{ $inv->party->legal_name }}</td><td>{{ $inv->issue_date->format('d M Y') }}</td><td>{{ number_format((float)$inv->grand_total-(float)$inv->paid_total-(float)$inv->allocated_total,2) }}</td><td><input class="control allocation" type="number" step="0.01" min="0" max="{{ (float)$inv->grand_total-(float)$inv->paid_total-(float)$inv->allocated_total }}" name="allocations[{{ $inv->id }}]" value="{{ $saved[$inv->id]??'' }}"></td></tr>@endforeach</tbody></table></div><div class="card-body text-right"><strong>Allocated: <span id="allocated-total">0.00</span> · Unallocated: <span id="unallocated-total">0.00</span></strong></div></div>
<div class="card" style="margin-top:18px"><div class="card-head" style="justify-content:flex-end"><a class="btn btn-secondary" href="{{ route('receipts.index') }}">Cancel</a><button class="btn btn-primary">Save Draft</button></div></div></form>
@endsection
@push('scripts')<script>const party=document.getElementById('party-select'),type=document.getElementById('document-type'),card=document.getElementById('allocation-card'),amount=document.getElementById('amount');function refresh(){const customer=type.value==='customer_receipt';card.style.display=customer?'block':'none';document.querySelectorAll('.allocation-row').forEach(r=>r.style.display=customer&&r.dataset.party===party.value?'table-row':'none');calc()}function calc(){let t=0;document.querySelectorAll('.allocation-row').forEach(r=>{if(r.style.display!=='none')t+=parseFloat(r.querySelector('.allocation').value)||0});document.getElementById('allocated-total').textContent=t.toFixed(2);document.getElementById('unallocated-total').textContent=Math.max(0,(parseFloat(amount.value)||0)-t).toFixed(2)}party.onchange=refresh;type.onchange=refresh;amount.oninput=calc;document.querySelectorAll('.allocation').forEach(i=>i.oninput=calc);refresh();</script>@endpush
