@extends('layouts.app')
@section('title',($quotation->exists?'Edit':'New').' Quotation') @section('page_title',($quotation->exists?'Edit':'New').' Quotation')
@section('content')<div class="page-header"><div><h1>{{ $quotation->exists?'Edit':'New' }} Quotation</h1><p class="muted">All amounts are recalculated with decimal arithmetic when saved.</p></div><a class="btn btn-secondary" href="{{ $quotation->exists?route('quotations.show',$quotation):route('quotations.index') }}">Back</a></div><form method="post" action="{{ $quotation->exists?route('quotations.update',$quotation):route('quotations.store') }}">@csrf @if($quotation->exists)@method('put')@endif<div class="card"><div class="card-header"><h3>Quotation Header</h3></div><div class="card-body"><div class="form-grid"><div class="field"><label>Branch *</label><select class="control" name="branch_id" required>@foreach($branches as $branch)<option value="{{ $branch->id }}" @selected(old('branch_id',$quotation->branch_id??auth()->user()->branch_id)==$branch->id)>{{ $branch->code }} - {{ $branch->name }}</option>@endforeach</select></div><div class="field"><label>Customer *</label><select class="control" name="party_id" required><option value="">Select customer</option>@foreach($customers as $customer)<option value="{{ $customer->id }}" @selected(old('party_id',$quotation->party_id)==$customer->id)>{{ $customer->legal_name }}</option>@endforeach</select></div><div class="field"><label>Contract</label><select class="control" name="contract_id"><option value="">No contract</option>@foreach($contracts as $contract)<option value="{{ $contract->id }}" @selected(old('contract_id',$quotation->contract_id)==$contract->id)>{{ $contract->contract_number }} - {{ $contract->title }}</option>@endforeach</select></div><div class="field"><label>Pricing</label><select class="control" id="pricing_mode" name="pricing_mode"><option value="exclusive" @selected(old('pricing_mode',$quotation->pricing_mode)==='exclusive')>VAT Exclusive</option><option value="inclusive" @selected(old('pricing_mode',$quotation->pricing_mode)==='inclusive')>VAT Inclusive</option></select></div><div class="field"><label>Quotation Date *</label><input class="control" type="date" name="quotation_date" value="{{ old('quotation_date',$quotation->quotation_date?->format('Y-m-d')) }}" required></div><div class="field"><label>Expiry Date</label><input class="control" type="date" name="expiry_date" value="{{ old('expiry_date',$quotation->expiry_date?->format('Y-m-d')) }}"></div><div class="field"><label>Currency</label><input class="control" name="currency_code" value="{{ old('currency_code',$quotation->currency_code??'AED') }}" maxlength="3" required></div><div class="field"><label>Exchange Rate</label><input class="control" type="number" step="0.00000001" name="exchange_rate" value="{{ old('exchange_rate',$quotation->exchange_rate??1) }}" required></div><div class="field span-2"><label>Payment Terms</label><input class="control" name="payment_terms" value="{{ old('payment_terms',$quotation->payment_terms) }}"></div></div></div></div>
<div class="card" style="margin-top:18px"><div class="card-header"><h3>Service Lines</h3><button class="btn btn-secondary btn-sm" type="button" id="add-line">+ Add Line</button></div><div class="table-wrap"><table id="lines-table"><thead><tr><th>Service / Description</th><th>Route</th><th>Vehicle</th><th>Qty</th><th>Unit</th><th>Rate</th><th>Discount</th><th>VAT</th><th class="text-right">Total</th><th></th></tr></thead><tbody id="line-body"></tbody></table></div><div class="card-body"><div class="totals"><div class="total-row"><span>Subtotal</span><strong id="subtotal-display">0.00</strong></div><div class="total-row"><span>Discount</span><strong id="discount-display">0.00</strong></div><div class="total-row"><span>VAT</span><strong id="vat-display">0.00</strong></div><div class="total-row grand"><span>Total</span><strong id="grand-display">0.00</strong></div></div></div></div>
<div class="card" style="margin-top:18px"><div class="card-body"><div class="form-grid"><div class="field"><label>Notes</label><textarea class="control" name="notes" rows="4">{{ old('notes',$quotation->notes) }}</textarea></div><div class="field"><label>Terms & Conditions</label><textarea class="control" name="terms" rows="4">{{ old('terms',$quotation->terms??auth()->user()->company?->invoice_terms) }}</textarea></div></div></div><div class="card-footer"><a class="btn btn-secondary" href="{{ route('quotations.index') }}">Cancel</a><button class="btn btn-primary">Save Draft</button></div></div></form>@endsection
@push('scripts')<script>
const serviceOptions=`<option value="">Service</option>@foreach($services as $x)<option value="{{ $x->id }}">{{ addslashes($x->code.' - '.$x->name) }}</option>@endforeach`;const routeOptions=`<option value="">Route</option>@foreach($routes as $x)<option value="{{ $x->id }}">{{ addslashes($x->code.' - '.$x->name) }}</option>@endforeach`;const taxOptions=`<option value="" data-rate="0">No VAT</option>@foreach($taxCodes as $x)<option value="{{ $x->id }}" data-rate="{{ (float)$x->rate }}">{{ addslashes($x->code.' - '.$x->rate.'%') }}</option>@endforeach`;const existing=@json(old('items',$quotation->items->map(fn($i)=>['service_type_id'=>$i->service_type_id,'route_id'=>$i->route_id,'vehicle_type'=>$i->vehicle_type,'description'=>$i->description,'quantity'=>$i->quantity,'unit'=>$i->unit,'unit_rate'=>$i->unit_rate,'discount_amount'=>$i->discount_amount,'tax_code_id'=>$i->tax_code_id])->values()->all()));let lineIndex=0;function esc(v){return String(v??'').replace(/[&<>'"]/g,c=>({'&':'&amp;','<':'&lt;','>':'&gt;',"'":'&#39;','"':'&quot;'}[c]))}function addLine(d={}){const i=lineIndex++,tr=document.createElement('tr');tr.innerHTML=`<td><select class="control" name="items[${i}][service_type_id]">${serviceOptions}</select><input class="control" style="margin-top:5px" name="items[${i}][description]" value="${esc(d.description||'Transport service')}" required></td><td><select class="control" name="items[${i}][route_id]">${routeOptions}</select></td><td><input class="control" name="items[${i}][vehicle_type]" value="${esc(d.vehicle_type)}"></td><td><input class="control calc" type="number" step="0.0001" name="items[${i}][quantity]" value="${d.quantity||1}" required></td><td><input class="control" name="items[${i}][unit]" value="${esc(d.unit||'Trip')}"></td><td><input class="control calc" type="number" step="0.0001" name="items[${i}][unit_rate]" value="${d.unit_rate||0}" required></td><td><input class="control calc" type="number" step="0.0001" name="items[${i}][discount_amount]" value="${d.discount_amount||0}"></td><td><select class="control tax-select" name="items[${i}][tax_code_id]">${taxOptions}</select></td><td class="line-total text-right">0.00</td><td><button type="button" class="btn btn-danger btn-sm remove">×</button></td>`;document.querySelector('#line-body').appendChild(tr);tr.querySelector(`[name="items[${i}][service_type_id]"]`).value=d.service_type_id||'';tr.querySelector(`[name="items[${i}][route_id]"]`).value=d.route_id||'';tr.querySelector('.tax-select').value=d.tax_code_id||'';tr.querySelectorAll('.calc,.tax-select').forEach(x=>x.addEventListener('input',calc));tr.querySelector('.remove').onclick=()=>{tr.remove();calc()};calc()}function calc(){let sub=0,disc=0,vat=0,total=0;document.querySelectorAll('#line-body tr').forEach(tr=>{const q=+tr.querySelector('[name$="[quantity]"]').value||0,r=+tr.querySelector('[name$="[unit_rate]"]').value||0,d=+tr.querySelector('[name$="[discount_amount]"]').value||0,rate=+tr.querySelector('.tax-select').selectedOptions[0]?.dataset.rate||0,g=Math.max(0,q*r-d);let v,t;if(document.querySelector('#pricing_mode').value==='inclusive'&&rate){v=g-g/(1+rate/100);t=g}else{v=g*rate/100;t=g+v}tr.querySelector('.line-total').textContent=t.toFixed(2);sub+=q*r;disc+=d;vat+=v;total+=t});document.querySelector('#subtotal-display').textContent=sub.toFixed(2);document.querySelector('#discount-display').textContent=disc.toFixed(2);document.querySelector('#vat-display').textContent=vat.toFixed(2);document.querySelector('#grand-display').textContent=total.toFixed(2)}document.querySelector('#add-line').onclick=()=>addLine();document.querySelector('#pricing_mode').onchange=calc;(existing.length?existing:[{}]).forEach(addLine);
</script>@endpush
