@extends('backend.template.layouts.template-base') @section('title', isset($bill) ? 'Modify bill' : 'New bill') @section('styles') @endsection @section('content') @php $isEdit = isset($bill); $p = $prefillPatient ?? ($bill->patient ?? null); $showPatientCard = $isEdit || $p; $ageYears = $p?->age ?? ($p?->dob ? \Carbon\Carbon::parse($p->dob)->age : null); $honorific = $p?->honorificPrefix() ?: ''; $resolvedSex = $p?->resolvedSex(); $sexLetter = $resolvedSex ? strtoupper(substr($resolvedSex, 0, 1)) : ''; $ageSex = ($ageYears !== null && $sexLetter) ? $ageYears . ' YRS/' . $sexLetter : ($ageYears !== null ? $ageYears . ' YRS' : '—'); $patientDisplay = $p ? trim(($honorific !== '' ? $honorific . ' ' : '') . ($p->name ?: trim(($p->first_name ?? '') . ' ' . ($p->last_name ?? '')))) : '—'; $lastReg = $lastRegNo ?? ($isEdit ? $bill->id : null); $defReferred = old('referred_by', session('selected_referrer', $isEdit ? $bill->referred_by : '')); $defCentre = old('collection_centre', $isEdit ? $bill->collection_centre : 'Main'); $defAgent = old('sample_collection_agent', $isEdit ? $bill->sample_collection_agent : ''); $defSampleCollectedAt = old('sample_collected_at', $isEdit ? ($bill->sample_collected_at ?? '') : ''); $showSampleCollectedBlock = filled($defSampleCollectedAt); $defLab = old('lab_investigations', $isEdit ? $bill->lab_investigations : ($prefillLabInvestigations ?? '')); $prefillTotals = $prefillPaymentTotals ?? null; $defPaid = old('lab_paid', $isEdit ? $bill->paid_amount : (float) ($prefillTotals['total_amount'] ?? 0)); $defDiscount = old('lab_discount', $isEdit ? $bill->discount : 0); $defPayPaid = old('payment_paid', $isEdit ? 0 : (float) ($prefillTotals['total_amount'] ?? 0)); $defPayDiscount = old('payment_discount', $isEdit ? $bill->discount : 0); $defAmountReceived = old('amount_received', 0); $advancePaid = $isEdit ? (float) $bill->paid_amount : 0; $defMode = old('payment_mode', $isEdit ? $bill->payment_mode : 'cash'); $defRemarks = old('remarks', $isEdit ? $bill->remarks : ''); $defTotal = $isEdit ? (float) $bill->total_amount : (float) ($prefillTotals['total_amount'] ?? 0); $defBalance = $isEdit ? (float) $bill->balance : (float) ($prefillTotals['balance'] ?? ($prefillTotals['total_amount'] ?? 0)); $labOpen = $isEdit || filled($defLab) || request()->filled('patient_id') || request()->filled('booking_id'); $labLines = array_values(array_filter(array_map('trim', preg_split('/\r\n|\n|\r/', (string) $defLab)))); @endphp