@extends('layouts.app')
@section('title','Receive Customer Payment')
@section('crumbs', '<a href="'.route('customer-payments.index').'">Receipts</a> · <b>New</b>')
@section('content')
<div class="card" style="max-width:860px"><div class="card-h"><h2>Receive payment (posts immediately)</h2><div class="sp"><span class="bdg b-info">{{ $docNo }}</span></div></div>
<div class="card-b"><form method="POST" action="{{ route('customer-payments.store') }}">@csrf
<div class="frow">
<div class="fld c6"><label>Customer *</label><select name="customer_id" required><option value="">—</option>@foreach($customers as $c)<option value="{{ $c->id }}" data-defcur="{{ $c->default_currency }}" {{ $record->customer_id==$c->id?'selected':'' }}>{{ $c->display_name }} ({{ money($c->current_balance) }})</option>@endforeach</select></div>
<div class="fld c6"><label>Against invoice</label><select name="invoice_id"><option value="">— On account —</option>@foreach($invoices as $i)<option value="{{ $i->id }}" data-cur="{{ $i->currency_code }}" {{ $record->invoice_id==$i->id?'selected':'' }}>{{ $i->invoice_no }} ({{ $i->currency_code }}) — due {{ money($i->balance_due) }}</option>@endforeach</select></div>
<div class="fld c3"><label>Date *</label><input type="date" name="payment_date" value="{{ $record->payment_date?->format('Y-m-d') ?? date('Y-m-d') }}" required></div>
@include('docs._fxfields', ['record' => $record, 'fx' => $fx, 'isNew' => true, 'lockCur' => !empty($record->invoice_id)])
<script>
(function(){var inv=document.querySelector('select[name=invoice_id]'),cur=document.querySelector('[data-fx-cur]');
if(!inv||!cur)return;
function sync(){var o=inv.options[inv.selectedIndex];var c=o&&o.dataset?o.dataset.cur:'';
var hid=document.querySelector('input[name=currency_code][type=hidden]');
if(c){cur.value=c;cur.dispatchEvent(new Event('change'));cur.disabled=true;
if(hid)hid.value=c;else{var h=document.createElement('input');h.type='hidden';h.name='currency_code';h.value=c;cur.parentNode.appendChild(h);}}
else{cur.disabled=false;if(hid)hid.remove();}}
inv.addEventListener('change',sync);sync();})();
</script>
<div class="fld c3"><label>Amount *</label><input type="number" step="0.01" name="amount" value="{{ $record->amount }}" required style="font-weight:800"></div>
<div class="fld c3"><label>Mode *</label><select name="payment_mode"><option value="bank-transfer" {{ $record->payment_mode==='bank-transfer'?'selected':'' }}>Bank transfer</option><option value="cash" {{ $record->payment_mode==='cash'?'selected':'' }}>Cash</option><option value="cheque" {{ $record->payment_mode==='cheque'?'selected':'' }}>Cheque</option><option value="momo" {{ $record->payment_mode==='momo'?'selected':'' }}>MoMo</option></select></div>
<div class="fld c3"><label>Into bank/cash</label><select name="bank_account_id"><option value="">—</option>@foreach($banks as $b)<option value="{{ $b->id }}">{{ $b->display_name }} ({{ $b->currency }})</option>@endforeach</select></div>
<div class="fld c4"><label>Cheque no.</label><input type="text" name="cheque_no"></div>
<div class="fld c4"><label>Reference</label><input type="text" name="reference"></div>
<div class="fld c4"><label>Received by</label><input type="text" name="received_by" value="{{ auth()->user()->name }}"></div>
<div class="fld c12"><label>Remarks</label><input type="text" name="remarks"></div>
</div>
<button class="btn btn-pri">💰 Post receipt</button> <a class="btn btn-ghost" href="{{ route('customer-payments.index') }}">Cancel</a>
</form></div></div>
@endsection
