@extends('layouts.app')
@section('title',($expense->exists?'Edit':'New').' Expense')
@section('page_title',($expense->exists?'Edit':'New').' Expense')
@section('content')
<div class="page-head"><div><h1>{{ $expense->exists?'Edit':'Record' }} Expense</h1><p>Recoverable and non-recoverable VAT are posted to separate accounts.</p></div><a class="btn btn-secondary" href="{{ $expense->exists?route('expenses.show',$expense):route('expenses.index') }}">Back</a></div>
<form method="post" action="{{ $expense->exists?route('expenses.update',$expense):route('expenses.store') }}">@csrf @if($expense->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',$expense->branch_id)==$b->id)>{{ $b->code }} - {{ $b->name }}</option>@endforeach</select></div><div class="field"><label>Expense Date *</label><input class="control" type="date" name="expense_date" value="{{ old('expense_date',optional($expense->expense_date)->format('Y-m-d')) }}" required></div>
<div class="field"><label>Payee / Party</label><select class="control" name="party_id"><option value="">No party</option>@foreach($parties as $p)<option value="{{ $p->id }}" @selected(old('party_id',$expense->party_id)==$p->id)>{{ $p->legal_name }}</option>@endforeach</select></div><div class="field"><label>Category *</label><input class="control" name="category" value="{{ old('category',$expense->category) }}" placeholder="Fuel, maintenance, office expense" required></div>
<div class="field full"><label>Description *</label><input class="control" name="description" value="{{ old('description',$expense->description) }}" required></div>
<div class="field"><label>Expense Account *</label><select class="control" name="expense_account_id" required><option value="">Select</option>@foreach($accounts as $a)<option value="{{ $a->id }}" @selected(old('expense_account_id',$expense->expense_account_id)==$a->id)>{{ $a->code }} - {{ $a->name }}</option>@endforeach</select></div><div class="field"><label>Payment Account *</label><select class="control" name="payment_account_id" required><option value="">Cash / bank / card account</option>@foreach($accounts as $a)<option value="{{ $a->id }}" @selected(old('payment_account_id',$expense->payment_account_id)==$a->id)>{{ $a->code }} - {{ $a->name }}</option>@endforeach</select></div>
<div class="field"><label>Tax Code</label><select class="control" name="tax_code_id"><option value="">No tax code</option>@foreach($taxCodes as $t)<option value="{{ $t->id }}" @selected(old('tax_code_id',$expense->tax_code_id)==$t->id)>{{ $t->code }} - {{ $t->name }}</option>@endforeach</select></div><div class="field"><label>Input VAT Account</label><select class="control" name="input_vat_account_id"><option value="">None</option>@foreach($accounts as $a)<option value="{{ $a->id }}" @selected(old('input_vat_account_id',$expense->input_vat_account_id)==$a->id)>{{ $a->code }} - {{ $a->name }}</option>@endforeach</select></div>
<div class="field"><label>Amount Before VAT *</label><input class="control calc-expense" type="number" step="0.0001" name="amount_before_vat" value="{{ old('amount_before_vat',$expense->amount_before_vat??0) }}" required></div><div class="field"><label>Recoverable VAT</label><input class="control calc-expense" type="number" step="0.0001" name="recoverable_vat" value="{{ old('recoverable_vat',$expense->recoverable_vat??0) }}"></div><div class="field"><label>Non-Recoverable VAT</label><input class="control calc-expense" type="number" step="0.0001" name="nonrecoverable_vat" value="{{ old('nonrecoverable_vat',$expense->nonrecoverable_vat??0) }}"></div><div class="field"><label>Calculated Total</label><input class="control" id="expense-total" value="0.00" readonly></div>
<div class="field"><label>Payment Method</label><select class="control" name="payment_method">@foreach(['cash','bank','card','credit_card','employee_paid','driver_paid'] as $m)<option value="{{ $m }}" @selected(old('payment_method',$expense->payment_method)===$m)>{{ ucwords(str_replace('_',' ',$m)) }}</option>@endforeach</select></div><div class="field"><label>Reimbursement</label><select class="control" name="reimbursement_status">@foreach(['not_applicable','pending','approved','paid'] as $s)<option value="{{ $s }}" @selected(old('reimbursement_status',$expense->reimbursement_status)===$s)>{{ ucwords(str_replace('_',' ',$s)) }}</option>@endforeach</select></div>
<div class="field"><label>Transport Job</label><select class="control" name="transport_job_id"><option value="">None</option>@foreach($jobs as $j)<option value="{{ $j->id }}" @selected(old('transport_job_id',$expense->transport_job_id)==$j->id)>{{ $j->job_number }}</option>@endforeach</select></div><div class="field"><label>Trip</label><select class="control" name="trip_id"><option value="">None</option>@foreach($trips as $t)<option value="{{ $t->id }}" @selected(old('trip_id',$expense->trip_id)==$t->id)>{{ $t->trip_number }}</option>@endforeach</select></div>
<div class="field"><label>Vehicle</label><select class="control" name="vehicle_id"><option value="">None</option>@foreach($vehicles as $v)<option value="{{ $v->id }}" @selected(old('vehicle_id',$expense->vehicle_id)==$v->id)>{{ $v->plate_number }}</option>@endforeach</select></div><div class="field"><label>Driver</label><select class="control" name="driver_id"><option value="">None</option>@foreach($drivers as $d)<option value="{{ $d->id }}" @selected(old('driver_id',$expense->driver_id)==$d->id)>{{ $d->name }}</option>@endforeach</select></div>
<div class="field"><label>Department</label><select class="control" name="department_id"><option value="">None</option>@foreach($departments as $d)<option value="{{ $d->id }}" @selected(old('department_id',$expense->department_id)==$d->id)>{{ $d->name }}</option>@endforeach</select></div><div class="field"><label>Cost Centre</label><select class="control" name="cost_center_id"><option value="">None</option>@foreach($costCenters as $c)<option value="{{ $c->id }}" @selected(old('cost_center_id',$expense->cost_center_id)==$c->id)>{{ $c->name }}</option>@endforeach</select></div>
<label class="checkbox"><input type="checkbox" name="is_billable" value="1" @checked(old('is_billable',$expense->is_billable))> Billable to customer</label>
</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>function expenseTotal(){let n=0;document.querySelectorAll('.calc-expense').forEach(i=>n+=Number(i.value||0));document.getElementById('expense-total').value=n.toFixed(2)}document.querySelectorAll('.calc-expense').forEach(i=>i.addEventListener('input',expenseTotal));expenseTotal();</script>@endpush
