<?php $__env->startSection('title',($bill->exists?'Edit':'New').' Vendor Bill'); ?>
<?php $__env->startSection('page_title',($bill->exists?'Edit':'New').' Vendor Bill'); ?>
<?php $__env->startSection('content'); ?>
<div class="page-head"><div><h1><?php echo e($bill->exists?'Edit':'New'); ?> Vendor Bill</h1><p>Duplicate vendor invoice numbers are blocked for each vendor.</p></div><a class="btn btn-secondary" href="<?php echo e($bill->exists?route('bills.show',$bill):route('bills.index')); ?>">Back</a></div>
<form method="post" action="<?php echo e($bill->exists?route('bills.update',$bill):route('bills.store')); ?>"><?php echo csrf_field(); ?> <?php if($bill->exists): ?><?php echo method_field('put'); ?><?php endif; ?>
<div class="card"><div class="card-head"><strong>Bill Header</strong></div><div class="card-body"><div class="form-grid">
<div class="field"><label>Branch *</label><select class="control" name="branch_id"><?php $__currentLoopData = $branches; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $branch): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><option value="<?php echo e($branch->id); ?>" <?php if(old('branch_id',$bill->branch_id ?? auth()->user()->branch_id)==$branch->id): echo 'selected'; endif; ?>><?php echo e($branch->code); ?> - <?php echo e($branch->name); ?></option><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?></select></div><div class="field"><label>Vendor *</label><select class="control" name="party_id" required><option value="">Select vendor</option><?php $__currentLoopData = $vendors; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $vendor): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><option value="<?php echo e($vendor->id); ?>" <?php if(old('party_id',$bill->party_id)==$vendor->id): echo 'selected'; endif; ?>><?php echo e($vendor->legal_name); ?><?php echo e($vendor->trn?' · TRN '.$vendor->trn:''); ?></option><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?></select></div>
<div class="field"><label>Vendor Invoice Number *</label><input class="control" name="vendor_invoice_number" value="<?php echo e(old('vendor_invoice_number',$bill->vendor_invoice_number)); ?>" required></div><div class="field"><label>Pricing</label><select class="control" name="pricing_mode" id="pricing_mode"><option value="exclusive" <?php if(old('pricing_mode',$bill->pricing_mode)==='exclusive'): echo 'selected'; endif; ?>>VAT Exclusive</option><option value="inclusive" <?php if(old('pricing_mode',$bill->pricing_mode)==='inclusive'): echo 'selected'; endif; ?>>VAT Inclusive</option></select></div>
<div class="field"><label>Invoice Date</label><input class="control" type="date" name="invoice_date" value="<?php echo e(old('invoice_date',optional($bill->invoice_date)->format('Y-m-d'))); ?>" required></div><div class="field"><label>Date of Supply</label><input class="control" type="date" name="supply_date" value="<?php echo e(old('supply_date',optional($bill->supply_date)->format('Y-m-d'))); ?>"></div><div class="field"><label>Due Date</label><input class="control" type="date" name="due_date" value="<?php echo e(old('due_date',optional($bill->due_date)->format('Y-m-d'))); ?>"></div><div class="field"><label>Currency / Exchange Rate</label><div style="display:flex;gap:8px"><input class="control" name="currency_code" value="<?php echo e(old('currency_code',$bill->currency_code ?? 'AED')); ?>"><input class="control" type="number" step="0.00000001" name="exchange_rate" value="<?php echo e(old('exchange_rate',$bill->exchange_rate ?? 1)); ?>"></div></div>
<div class="field"><label>Transport Job</label><select class="control" name="transport_job_id"><option value="">None</option><?php $__currentLoopData = $jobs; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $job): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><option value="<?php echo e($job->id); ?>" <?php if(old('transport_job_id',$bill->transport_job_id)==$job->id): echo 'selected'; endif; ?>><?php echo e($job->job_number); ?></option><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?></select></div><div class="field"><label>Trip</label><select class="control" name="trip_id"><option value="">None</option><?php $__currentLoopData = $trips; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $trip): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><option value="<?php echo e($trip->id); ?>" <?php if(old('trip_id',$bill->trip_id)==$trip->id): echo 'selected'; endif; ?>><?php echo e($trip->trip_number); ?></option><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?></select></div><div class="field"><label>Vehicle</label><select class="control" name="vehicle_id"><option value="">None</option><?php $__currentLoopData = $vehicles; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $vehicle): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><option value="<?php echo e($vehicle->id); ?>" <?php if(old('vehicle_id',$bill->vehicle_id)==$vehicle->id): echo 'selected'; endif; ?>><?php echo e($vehicle->plate_number); ?></option><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?></select></div><div class="field"><label>Driver</label><select class="control" name="driver_id"><option value="">None</option><?php $__currentLoopData = $drivers; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $driver): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><option value="<?php echo e($driver->id); ?>" <?php if(old('driver_id',$bill->driver_id)==$driver->id): echo 'selected'; endif; ?>><?php echo e($driver->name); ?></option><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?></select></div>
<div class="field"><label>Accounts Payable *</label><select class="control" name="payable_account_id" required><option value="">Select</option><?php $__currentLoopData = $accounts; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $account): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><option value="<?php echo e($account->id); ?>" <?php if(old('payable_account_id',$bill->payable_account_id)==$account->id): echo 'selected'; endif; ?>><?php echo e($account->code); ?> - <?php echo e($account->name); ?></option><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?></select></div></div></div></div>
<div class="card" style="margin-top:18px"><div class="card-head"><strong>Bill Lines</strong><button class="btn btn-secondary btn-sm" type="button" id="add-line">+ Add Line</button></div><div class="line-items"><table class="table"><thead><tr><th>Description</th><th>Qty</th><th>Unit</th><th>Rate</th><th>Discount</th><th>Expense / Cost Account</th><th>VAT Code</th><th>Taxable</th><th>Recoverable VAT</th><th>Non-Recoverable</th><th>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>Taxable</span><strong id="taxable-display">0.00</strong></div><div class="total-row"><span>Recoverable VAT</span><strong id="recoverable-display">0.00</strong></div><div class="total-row"><span>Non-Recoverable VAT</span><strong id="nonrecoverable-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="field"><label>Notes</label><textarea class="control" name="notes"><?php echo e(old('notes',$bill->notes)); ?></textarea></div></div><div class="card-head" style="justify-content:flex-end"><a class="btn btn-secondary" href="<?php echo e(route('bills.index')); ?>">Cancel</a><button class="btn btn-primary">Save Draft</button></div></div></form>
<?php $__env->stopSection(); ?>
<?php $__env->startPush('scripts'); ?>
<script>
const accountOptions=`<option value="">Select</option><?php $__currentLoopData = $accounts; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $a): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><option value="<?php echo e($a->id); ?>"><?php echo e(addslashes($a->code.' - '.$a->name)); ?></option><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>`;const taxOptions=`<option value="" data-rate="0" data-recover="0">No VAT</option><?php $__currentLoopData = $taxCodes; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $t): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><option value="<?php echo e($t->id); ?>" data-rate="<?php echo e((float)$t->rate); ?>" data-recover="<?php echo e((float)$t->recoverable_percentage); ?>"><?php echo e(addslashes($t->code.' - '.$t->name.' ('.$t->rate.'%)')); ?></option><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>`;const existing=<?php echo json_encode(old('items', $bill->items->map(fn($i)=>['description'=>$i->description, 'quantity'=>$i->quantity) ?>;let idx=0;function esc(v){return String(v).replace(/[&<>'"]/g,c=>({'&':'&amp;','<':'&lt;','>':'&gt;',"'":'&#39;','"':'&quot;'}[c]))}function addLine(d={}){let i=idx++,tr=document.createElement('tr');tr.innerHTML=`<td><input class="control" name="items[${i}][description]" value="${esc(d.description||'Transport / supplier cost')}" required></td><td><input class="control calc" type="number" step=".0001" name="items[${i}][quantity]" value="${d.quantity||1}"></td><td><input class="control" name="items[${i}][unit]" value="${esc(d.unit||'Trip')}"></td><td><input class="control calc" type="number" step=".0001" name="items[${i}][unit_rate]" value="${d.unit_rate||0}"></td><td><input class="control calc" type="number" step=".0001" name="items[${i}][discount_amount]" value="${d.discount_amount||0}"></td><td><select class="control" name="items[${i}][expense_account_id]">${accountOptions}</select></td><td><select class="control tax calc" name="items[${i}][tax_code_id]">${taxOptions}</select></td><td class="taxable text-right">0.00</td><td class="recover text-right">0.00</td><td class="nonrecover text-right">0.00</td><td class="total text-right">0.00</td><td><button class="btn btn-danger btn-sm remove" type="button">×</button></td>`;document.getElementById('line-body').appendChild(tr);tr.querySelector(`[name="items[${i}][expense_account_id]"]`).value=d.expense_account_id||'';tr.querySelector('.tax').value=d.tax_code_id||'';tr.querySelectorAll('.calc').forEach(e=>e.addEventListener('input',calculate));tr.querySelector('.remove').addEventListener('click',()=>{tr.remove();calculate()});calculate()}function calculate(){let taxable=0,recover=0,non=0,grand=0;document.querySelectorAll('#line-body tr').forEach(tr=>{let q=+tr.querySelector('[name$="[quantity]"]').value||0,r=+tr.querySelector('[name$="[unit_rate]"]').value||0,d=+tr.querySelector('[name$="[discount_amount]"]').value||0,sel=tr.querySelector('.tax'),rate=+sel.selectedOptions[0]?.dataset.rate||0,pct=+sel.selectedOptions[0]?.dataset.recover||0,gross=Math.max(0,q*r-d),net=gross,vat=0,total=gross;if(document.getElementById('pricing_mode').value==='inclusive'&&rate>0){net=gross/(1+rate/100);vat=gross-net}else{vat=net*rate/100;total=net+vat}let rec=vat*pct/100,nr=vat-rec;tr.querySelector('.taxable').textContent=net.toFixed(2);tr.querySelector('.recover').textContent=rec.toFixed(2);tr.querySelector('.nonrecover').textContent=nr.toFixed(2);tr.querySelector('.total').textContent=total.toFixed(2);taxable+=net;recover+=rec;non+=nr;grand+=total});document.getElementById('taxable-display').textContent=taxable.toFixed(2);document.getElementById('recoverable-display').textContent=recover.toFixed(2);document.getElementById('nonrecoverable-display').textContent=non.toFixed(2);document.getElementById('grand-display').textContent=grand.toFixed(2)}document.getElementById('add-line').onclick=()=>addLine();document.getElementById('pricing_mode').onchange=calculate;(existing.length?existing:[{}]).forEach(addLine);
</script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH /home/cbo/public_html/resources/views/bills/form.blade.php ENDPATH**/ ?>