@extends('layouts.app') @push('styles') @php $auditCss = public_path('css/Monitor/ncd-medication-audit.css'); if (!is_file($auditCss)) $auditCss = dirname(base_path()).'/public/css/Monitor/ncd-medication-audit.css'; @endphp @php $flowCss=public_path('css/Monitor/ncd-clinic-time.css'); if(!is_file($flowCss)) $flowCss=dirname(base_path()).'/public/css/Monitor/ncd-clinic-time.css'; @endphp @endpush @section('content') @php $labels = ['matched'=>'Matched','rounded'=>'Whole-tablet rounding','missing'=>'Not dispensed','unexpected'=>'No matching prescription','short'=>'Quantity short','excess'=>'Quantity excess','review'=>'Needs review','self_funded'=>'Self-funded']; $number = static function($n) { if($n===null) return 'Unavailable'; $s=number_format((float)$n,4,'.',',');return rtrim(rtrim($s,'0'),'.'); }; @endphp

NCD Medication Audit

Compare fixed NCD prescriptions with recorded dispensing by clinic, General ID and visit date.

Clinic databases
@foreach($clinics as $connection=>$label) @endforeach

Enabled clinics: {{ implode(', ', array_map(static fn($connection) => $clinics[$connection], $enabledClinics)) }}.@if(count($enabledClinics)

Use up to 93 days. Quantities use the saved days from Visit Date to Next Appointment, including extensions. Dose and stock strength are compared in mg; unclear quantities require review.

@if($errors->any())@endif
@if(!$ran)
Choose clinics and dates, then run the audit. The audit reads data only; it does not change prescriptions, consumption or stock.
@else @if(collect($sources)->contains(static fn($source)=>!$source['ok'])) @endif
@foreach($counts as $status=>$count)
{{ number_format($count) }}{{ $labels[$status] }}
@endforeach
contains(static fn($source)=>!$source['ok'])) open @endif> Source coverage · {{ $filters['from'] }} to {{ $filters['to'] }}
@foreach($sources as $source)@endforeach
ClinicNCD follow-up rowsConsumption rows scannedComparisonsStatus
{{ $clinics[$source['clinic']] }}{{ $source['followups'] ?? '—' }}{{ $source['consumptions'] ?? '—' }}{{ $source['comparisons'] ?? '—' }}{{ $source['message'] }}

Rows compare medicines, not patient totals. “No matching prescription” may need review against another prescribing service. Self-funded prescriptions without clinic consumption are listed separately. Whole-tablet rounding is informational. Duplicate visits, duplicate consumption rows and malformed records are never labelled matched. Stock items use generic-name matching or a configured stock mapping; brand-only names need mapping.

{{ $filters['show']==='issues' ? 'Comparisons needing attention' : 'All medicine comparisons' }}

{{ number_format($rows->total()) }} rows · page {{ $rows->currentPage() }} of {{ max(1,$rows->lastPage()) }}
@forelse($rows as $row) @empty@endforelse
Clinic / visit dateGeneral ID / medicineNCD prescriptionDispensing consumptionComparison
{{ $clinics[$row['clinic']] }}
{{ $row['date'] ? \Carbon\Carbon::parse($row['date'])->format('d-m-Y') : 'Date unavailable' }}
{{ $row['pid'] ?: 'ID unavailable' }}
{{ $row['medicine_name'] }}
@forelse($row['prescriptions'] as $prescription)
{{ $prescription['text'] }}Follow-up row #{{ $prescription['followup_id'] }}
@emptyNo dose recorded for this medicine.@endforelse @if($row['expected_mg']!==null)
Required: {{ $number($row['expected_mg']) }} mg
@endif @if(count($row['prescriptions'])===1 && $row['prescriptions'][0]['days']!==null) {{ $row['prescriptions'][0]['days'] }} saved days · next appointment {{ $row['prescriptions'][0]['next_date'] ?? 'unavailable' }} @endif ncd_followups: {{ $row['followup_ids'] ? '#'.implode(', #',$row['followup_ids']) : 'no matching visit' }}
@forelse($row['dispensed'] as $stock)
{{ $number($stock['quantity']) }} {{ ($stock['unit'] ?? null)==='tablet' ? 'tablets' : 'stock units' }} · {{ $stock['name'] }}Stock #{{ $stock['id'] }} · consumption #{{ $stock['consumption_id'] }}@if($stock['strength_mg']!==null) · {{ $number($stock['strength_mg']) }} mg/tablet @endif
@emptyNo fixed-medicine consumption recorded.@endforelse @if($row['dispensed'] && $row['actual_mg']!==null)
Recorded: {{ $number($row['actual_mg']) }} mg
@endif consumptions: {{ $row['consumption_ids'] ? '#'.implode(', #',$row['consumption_ids']) : 'no matching record' }}
{{ $labels[$row['status']] }}

{{ $row['note'] }}

@if($row['difference_mg']!==null && $row['expected_mg']!==null)Recorded − required: {{ $row['difference_mg']>0 ? '+' : '' }}{{ $number($row['difference_mg']) }} mg@endif @foreach($row['issues'] as $issue){{ $issue }}@endforeach
@if(collect($sources)->every(static fn($s)=>!$s['ok'])) No clinic data could be audited. @elseif($filters['show']==='issues' && array_sum($counts)>0) No discrepancies found in {{ array_sum($counts) }} medicine comparisons. Choose Show → All comparisons to see the matching records. @else No fixed-medicine comparisons found for these filters. Check source coverage and the selected visit dates above. @endif
@if($rows->hasPages())@endif

Printed output includes this page of results only. No stock balance reconciliation is performed.

@endif @include('Monitor.partials.ncdConsumptionHistory') @if(isset($flowRan, $flowDate)) @include('Monitor.partials.ncdClinicTime') @endif
@endsection