@extends('layouts.app')
@section('title','Manual Journal')
@section('page_title','Manual Journal Voucher')
@section('content')
<div class="page-head"><div><h1>{{ $voucher->exists?'Edit':'Create' }} Manual Journal</h1><p>Server validation rejects unbalanced lines and locked accounting periods.</p></div><a class="btn btn-secondary" href="{{ $voucher->exists?route('journals.vouchers.show',$voucher):route('journals.index') }}">Back</a></div>
<form method="post" action="{{ $voucher->exists?route('journals.vouchers.update',$voucher):route('journals.store') }}">@csrf @if($voucher->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',$voucher->branch_id)==$b->id)>{{ $b->code }} - {{ $b->name }}</option>@endforeach</select></div><div class="field"><label>Posting Date</label><input class="control" type="date" name="posting_date" value="{{ old('posting_date',optional($voucher->posting_date)->format('Y-m-d')) }}"></div><div class="field"><label>Document Date</label><input class="control" type="date" name="document_date" value="{{ old('document_date',optional($voucher->document_date)->format('Y-m-d')) }}"></div><div class="field"><label>Reference</label><input class="control" name="reference_number" value="{{ old('reference_number',$voucher->reference_number) }}"></div><div class="field full"><label>Description</label><textarea class="control" name="description" required>{{ old('description',$voucher->description) }}</textarea></div><div class="field"><label>Currency</label><input class="control" name="currency_code" value="{{ old('currency_code',$voucher->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',$voucher->exchange_rate??1) }}"></div></div></div></div>
<div class="card" style="margin-top:18px"><div class="card-head"><strong>Journal Lines</strong><button type="button" class="btn btn-secondary btn-sm" id="add-line">+ Line</button></div><div class="table-wrap"><table class="table"><thead><tr><th>Account</th><th>Party</th><th>Ledger</th><th>Description</th><th>Debit</th><th>Credit</th><th></th></tr></thead><tbody id="line-body"></tbody></table></div><div class="card-body"><div class="totals"><div class="total-row"><span>Debit Total</span><strong id="dr-total">0.00</strong></div><div class="total-row"><span>Credit Total</span><strong id="cr-total">0.00</strong></div><div class="total-row grand"><span>Difference</span><strong id="difference">0.00</strong></div></div></div><div class="card-head" style="justify-content:flex-end"><button class="btn btn-primary">Save Draft</button></div></div></form>
@endsection
@push('scripts')<script>
const accounts=`<option value="">Select</option>@foreach($accounts as $a)<option value="{{ $a->id }}">{{ addslashes($a->code.' - '.$a->name) }}</option>@endforeach`;const parties=`<option value="">None</option>@foreach($parties as $p)<option value="{{ $p->id }}">{{ addslashes($p->legal_name) }}</option>@endforeach`;const existing=@json(old('lines',$voucher->lines->map(fn($l)=>['account_id'=>$l->account_id,'party_id'=>$l->party_id,'party_ledger'=>$l->party_ledger,'description'=>$l->description,'debit'=>$l->debit,'credit'=>$l->credit])->values()->all()));let idx=0;function add(d={}){let i=idx++,tr=document.createElement('tr');tr.innerHTML=`<td><select class="control" name="lines[${i}][account_id]" required>${accounts}</select></td><td><select class="control" name="lines[${i}][party_id]">${parties}</select></td><td><select class="control" name="lines[${i}][party_ledger]"><option value="">None</option><option value="customer">Customer</option><option value="vendor">Vendor</option></select></td><td><input class="control" name="lines[${i}][description]" value="${String(d.description||'').replace(/"/g,'&quot;')}"></td><td><input class="control amount" type="number" step="0.0001" name="lines[${i}][debit]" value="${d.debit||0}"></td><td><input class="control amount" type="number" step="0.0001" name="lines[${i}][credit]" value="${d.credit||0}"></td><td><button type="button" class="btn btn-danger btn-sm" onclick="this.closest('tr').remove();totals()">×</button></td>`;document.getElementById('line-body').append(tr);tr.querySelector('[name$="[account_id]"]').value=d.account_id||'';tr.querySelector('[name$="[party_id]"]').value=d.party_id||'';tr.querySelector('[name$="[party_ledger]"]').value=d.party_ledger||'';tr.querySelectorAll('.amount').forEach(i=>i.oninput=totals);totals()}function totals(){let dr=0,cr=0;document.querySelectorAll('[name$="[debit]"]').forEach(i=>dr+=Number(i.value||0));document.querySelectorAll('[name$="[credit]"]').forEach(i=>cr+=Number(i.value||0));document.getElementById('dr-total').textContent=dr.toFixed(2);document.getElementById('cr-total').textContent=cr.toFixed(2);document.getElementById('difference').textContent=(dr-cr).toFixed(2)}document.getElementById('add-line').onclick=()=>add();(existing.length?existing:[{},{}]).forEach(add);
</script>@endpush
