@extends('layouts.app')
@section('title',($payment->exists?'Edit':'New').' Payment')
@section('page_title',($payment->exists?'Edit':'New').' Payment Voucher')
@section('content')
<div class="page-head"><div><h1>{{ $payment->exists?'Edit':'New' }} Payment</h1><p>Allocate against vendor bills or record the remaining amount as a vendor advance.</p></div><a class="btn btn-secondary" href="{{ $payment->exists?route('payments.show',$payment):route('payments.index') }}">Back</a></div>
<form method="post" action="{{ $payment->exists?route('payments.update',$payment):route('payments.store') }}">@csrf @if($payment->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',$payment->branch_id??auth()->user()->branch_id)==$b->id)>{{ $b->code }} - {{ $b->name }}</option>@endforeach</select></div><div class="field"><label>Payment Type</label><select class="control" name="payment_type" id="document-type"><option value="vendor_payment" @selected(old('payment_type',$payment->payment_type)==='vendor_payment')>Vendor Payment</option><option value="customer_refund" @selected(old('payment_type',$payment->payment_type)==='customer_refund')>Customer Refund</option><option value="other_payment" @selected(old('payment_type',$payment->payment_type)==='other_payment')>Other Payment</option></select></div>
<div class="field"><label>Payee / 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',$payment->party_id)==$p->id)>{{ $p->legal_name }}</option>@endforeach</select></div><div class="field"><label>Payment Date</label><input class="control" type="date" name="payment_date" value="{{ old('payment_date',optional($payment->payment_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',$payment->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',$payment->payment_method)===$k)>{{ $v }}</option>@endforeach</select></div><div class="field"><label>Currency</label><input class="control" name="currency_code" value="{{ old('currency_code',$payment->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',$payment->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',$payment->cash_bank_account_id)==$a->id)>{{ $a->code }} - {{ $a->name }}</option>@endforeach</select></div><div class="field"><label>Payable / Counterparty Account</label><select class="control" name="payable_account_id" required><option value="">Select</option>@foreach($accounts as $a)<option value="{{ $a->id }}" @selected(old('payable_account_id',$payment->payable_account_id)==$a->id)>{{ $a->code }} - {{ $a->name }}</option>@endforeach</select></div><div class="field"><label>Vendor Advance Account</label><select class="control" name="vendor_advance_account_id"><option value="">Not required if fully allocated</option>@foreach($accounts as $a)<option value="{{ $a->id }}" @selected(old('vendor_advance_account_id',$payment->vendor_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',$payment->reference_number) }}"></div><div class="field full"><label>Description</label><textarea class="control" name="description">{{ old('description',$payment->description) }}</textarea></div>
</div></div></div>
<div class="card" style="margin-top:18px" id="allocation-card"><div class="card-head"><strong>Vendor Bill Allocation</strong><span class="muted">Enter amounts only for this vendor</span></div><div class="table-wrap"><table class="table"><thead><tr><th>Bill</th><th>Vendor Invoice</th><th>Vendor</th><th>Balance</th><th>Allocate</th></tr></thead><tbody>@php($saved=old('allocations',$payment->allocations->pluck('amount','vendor_bill_id')->all()))@foreach($bills as $bill)<tr class="allocation-row" data-party="{{ $bill->party_id }}"><td>{{ $bill->bill_number }}</td><td>{{ $bill->vendor_invoice_number }}</td><td>{{ $bill->party->legal_name }}</td><td>{{ number_format((float)$bill->grand_total-(float)$bill->paid_total-(float)$bill->allocated_total,2) }}</td><td><input class="control allocation" type="number" step="0.01" min="0" max="{{ (float)$bill->grand_total-(float)$bill->paid_total-(float)$bill->allocated_total }}" name="allocations[{{ $bill->id }}]" value="{{ $saved[$bill->id]??'' }}"></td></tr>@endforeach</tbody></table></div><div class="card-body text-right"><strong>Allocated: <span id="allocated-total">0.00</span> · Advance: <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('payments.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 vendor=type.value==='vendor_payment';card.style.display=vendor?'block':'none';document.querySelectorAll('.allocation-row').forEach(r=>r.style.display=vendor&&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
