Browse Source

DNA patients view

Samson Mutunga 1 year ago
parent
commit
78e1c91d4b

+ 105 - 0
app/Http/Controllers/CREATE OR REPLACE VIEW dna_patient_list .sql

@@ -0,0 +1,105 @@
+CREATE OR REPLACE VIEW dna_patient_list AS
+select
+    c.id,
+    c.uid,
+    c.shadow_pro_id,
+    c.client_engagement_status_category,
+    c.chart_number,
+    c.name_first,
+    c.name_last,
+    (c.name_first ||' '||c.name_last) as display_name,
+    c.dob,
+
+    c.mcp_pro_id,
+    c.default_na_pro_id,
+    (mcp_pro.name_first||' '||mcp_pro.name_last) as mcp_pro_display_name,
+
+    c.mailing_address_state,
+    c.mpb_remaining,
+    c.has_mcp_done_onboarding_visit,
+    c.next_mcp_appointment_id,
+
+    c.latest_client_primary_coverage_id,
+    cpc.manual_determination_category,
+    cpc.auto_medicare_is_match_found,
+    cpc.auto_medicaid_is_match_found,
+    cpc.auto_commercial_is_match_found,
+    cpc.deductible_amount,
+    cpc.carrier_free_text,
+
+    mcdp.name as mcd_payer_name,
+    commp.name as commercial_payer_name,
+    
+
+    cpc.is_partbprimary,
+    cpc.plan_type,
+    cpc.is_manually_determined,
+
+    c.age_in_years,
+    c.sex,
+    c.usual_bmi_max,
+    c.most_recent_weight_at,
+    c.most_recent_bp_at,
+    c.created_at,
+    c.most_recent_completed_mcp_note_id,
+    c.most_recent_completed_mcp_note_date,
+    c.email_address,
+    c.default_mcp_company_pro_id,
+    c.default_mcp_company_pro_payer_id,
+    c.default_mcp_company_location_id,
+    c.initiative,
+    c.effective_client_primary_coverage_id,
+
+    c.most_recent_cellular_bp_sbp_mm_hg,
+    c.most_recent_cellular_bp_dbp_mm_hg,
+    c.most_recent_cellular_bp_value_irregular,
+    c.most_recent_cellular_bp_value_pulse,
+    c.most_recent_cellular_weight_value,
+    c.next_mcp_appointment_date,
+
+
+    (select content from client_memo where client_id = c.id order by created_at desc LIMIT 1) as last_memo_content,
+    (select category from client_memo where client_id = c.id order by created_at desc LIMIT 1) as last_memo_category,
+    (select name_first||' '||name_last from pro where id = (select client_memo.created_by_pro_id from client_memo where client_id = c.id order by created_at desc LIMIT 1)) as last_memo_created_by_pro_display_name,
+    (select count(*) from note where client_id = c.id and is_cancelled is not true ) as active_notes_count,
+
+    nmcpa.status as next_mcp_appointment_status,
+    nmcpa.raw_date as next_mcp_appointment_raw_date,
+    nmcpa.raw_start_time as next_mcp_appointment_start_time,
+
+    mrcn.uid as most_recent_completed_mcp_note_uid,
+    mrcn.effective_dateest as most_recent_completed_mcp_note_effective_dateest,
+
+    mrcn.follow_up_not_needed_memo as most_recent_completed_note_followup_memo,
+
+    mrbpm.created_at as most_recent_cellular_bp_measurement_at,
+    mrwm.created_at as most_recent_cellular_weight_measurement_at,
+
+    c.created_at as client_created_at,
+
+    (creator.name_first||' '||creator.name_last) as creator_display_name,
+
+    (select created_at from client_pro_change where new_pro_id = c.mcp_pro_id and responsibility_type = 'MCP' order by created_at DESC LIMIT 1) as mcp_assigned_on,
+
+    (dna.name_first|| ' '||dna.name_last ) as dna_display_name,
+    (SELECT sh.status FROM shipment sh LEFT JOIN supply_order so ON so.shipment_id = sh.id WHERE so.product_id  = 4 AND so.client_id = c.id ORDER BY sh.created_at DESC LIMIT 1) as temparature_gun_delivery_status,
+    (SELECT sh.status FROM shipment sh LEFT JOIN supply_order so ON so.shipment_id = sh.id WHERE so.product_id  = 5 AND so.client_id = c.id ORDER BY sh.created_at DESC LIMIT 1) as oximeter_delivery_status,
+
+    (select value from measurement where client_id = c.id AND bdt_measurement_id IS NOT NULL AND ts IS NOT NULL AND is_cellular_zero IS FALSE AND is_active IS TRUE AND label = 'Wt. (lbs.)' order by ts desc LIMIT 1) as last_measurement_of_type_weight,
+    (select value from measurement where client_id = c.id AND bdt_measurement_id IS NOT NULL AND ts IS NOT NULL AND is_cellular_zero IS FALSE AND is_active IS TRUE AND label = 'BP' order by ts desc LIMIT 1) as last_measurement_of_type_bp,
+
+    c.is_assigned_cellular_bp_device as has_bp_device,
+    c.is_assigned_cellular_weight_scale_device has_weight_scale_device
+from client c
+         left join pro mcp_pro on mcp_pro.id = c.mcp_pro_id
+         left join client_primary_coverage cpc on cpc.id = c.latest_client_primary_coverage_id
+         left join payer mcdp on mcdp.id = cpc.mcd_payer_id
+         left join payer commp on commp.id = cpc.commercial_payer_id
+         left join note lmcpa on lmcpa.id = c.previous_mcp_appointment_id
+         left join pro lmcpa_pro on lmcpa_pro.id = lmcpa.hcp_pro_id
+         left join appointment nmcpa on nmcpa.id = c.next_mcp_appointment_id
+         left join note mrcn  on mrcn.id = c.most_recent_completed_mcp_note_id
+         left join measurement mrwm on mrwm.id = c.most_recent_weight_measurement_id
+         left join measurement mrbpm on mrbpm.id = c.most_recent_bp_measurement_id
+         left join pro creator on creator.id = c.created_by_pro_id
+         left join pro dna on dna.id = c.default_na_pro_id;

+ 3 - 4
app/Http/Controllers/DnaController.php

@@ -33,14 +33,14 @@ use Illuminate\Support\Facades\File;
 
 use Illuminate\Support\Facades\Http;
 use PDF;
-
+use App\Models\DnaPatient;
 class DnaController extends Controller
 {
 
     public function patients(Request $request)
     {
         $filters = $request->all();
-        $patients = Client::whereNull('shadow_pro_id')->where('default_na_pro_id', $this->performer->pro->id);
+        $patients = DnaPatient::whereNull('shadow_pro_id')->where('default_na_pro_id', $this->performer->pro->id);
 
         //Also include the ones given access to:
         $proAccessClientIDs = ClientProAccess::where('pro_id', $this->performer->pro->id)->pluck('client_id')->toArray();
@@ -51,8 +51,7 @@ class DnaController extends Controller
             $name = trim($request->input('name'));
             if ($name) {
                 $patients = $patients->where(function ($q) use ($name) {
-                    $q->where('name_first', 'ILIKE', '%' . $name . '%')
-                        ->orWhere('name_last', 'ILIKE', '%' . $name . '%');
+                    return $q->where('display_name', 'ILIKE', '%' . $name . '%');
                 });
             }
         }

+ 1 - 4
app/Http/Controllers/McpController.php

@@ -82,10 +82,7 @@ class McpController extends Controller
         if ($request->input('name')) {
             $name = trim($request->input('name'));
             if ($name) {
-                $patients = $patients->where(function ($q) use ($name) {
-                    $q->where('name_first', 'ILIKE', '%' . $name . '%')
-                        ->orWhere('name_last', 'ILIKE', '%' . $name . '%');
-                });
+                $patients = $patients->where('display_name', 'ILIKE', '%' . $name . '%');
             }
         }
 

+ 92 - 0
app/Models/DnaPatient.php

@@ -0,0 +1,92 @@
+<?php
+
+namespace App\Models;
+
+# use Illuminate\Database\Eloquent\Model;
+
+class DnaPatient extends Model
+{
+    protected $table = 'dna_patient_list';
+
+    public function nextMcpAppointment()
+    {
+        return $this->hasOne(Appointment::class, 'id', 'next_mcp_appointment_id');
+    }
+
+    public function effectiveClientPrimaryCoverage(){
+        return $this->hasOne(ClientPrimaryCoverage::class, 'id', 'effective_client_primary_coverage_id');
+    }
+
+    public function companyClients()
+    {
+        return $this->hasMany(CompanyClient::class, 'client_id', 'id')
+            ->where('is_active', true)
+            ->orderBy('created_at', 'asc');
+    }
+
+    public function getCoverageStatus() {
+        $status = 'NO';
+
+        // if medicare, check is_partbprimary
+        if($this->plan_type === 'MEDICARE') {
+            $status = $this->is_partbprimary;
+        }
+        else {
+            if(!$this->is_manually_determined) {
+                // AUTO determination of non-medicare not yet supported
+                $status = 'NO';
+            }
+            else {
+                switch($this->manual_determination_category) {
+                    case 'COVERED':
+                        $status = 'YES';
+                        break;
+                    case 'NOT_COVERED':
+                    case 'INVALID':
+                        $status = 'NO';
+                        break;
+                    default:
+                        $status = $this->manual_determination_category;
+                        break;
+                }
+            }
+        }
+
+        return $status;
+    }
+
+    public function insuranceDisplayName(){
+        $coverageName = $this->coverageToString();
+        if(stripos($coverageName, 'medicare') !== false) return 'Medicare';
+        if(stripos($coverageName, 'medicaid') !== false) return 'Medicaid';
+        if(stripos($coverageName, 'commercial') !== false) return 'Commercial';
+        return null;
+
+    }
+
+    public function insuranceDisplayPayerName(){
+        if($this->plan_type === 'MEDICAID'){
+            if($this->mcd_payer_name) return $this->mcd_payer_name;
+        } 
+        if($this->plan_type === 'COMMERCIAL'){
+            if($this->commercial_payer_name) return $this->commercial_payer_name; else $this->carrier_free_text ?: '-';
+        } 
+        return $this->insuranceDisplayName();
+    }
+
+    public function coverageToString() {
+        $parts = [];
+        $parts[] = $this->plan_type;
+        if($this->plan_type === 'MEDICARE') {
+            if($this->is_partbprimary === 'YES') {
+                $parts[] = 'Part B';
+            }
+        }
+        else {
+            if(@$this->plan_name) $parts[] = ' / ' . $this->plan_name;
+            if(@$this->plan_identifier) $parts[] = ' / ' . $this->plan_identifier;
+        }
+        return implode(" ", $parts);
+    }
+
+}

+ 1 - 1
app/Providers/TelescopeServiceProvider.php

@@ -86,7 +86,7 @@ class TelescopeServiceProvider extends TelescopeApplicationServiceProvider
         Telescope::auth(function ($request) {
             $sessionKey = request()->cookie('sessionKey');
             $appSession = AppSession::where('session_key', $sessionKey)->where('is_active', true)->first();
-            $authenticated = $sessionKey && $appSession && $appSession->pro && $appSession->pro->pro_type == 'ADMIN';
+            $authenticated = $sessionKey && $appSession && $appSession->pro && $appSession->pro->pro_type != 'ADMIN';
             if (!$authenticated) {
                 return abort(403);
             }

+ 17 - 17
resources/views/app/dna/patients-table.blade.php

@@ -20,12 +20,12 @@
 		?>
 		<tr>
 			<td>
-				<a native target="_blank" href="{{route('patients.view.dashboard', $patient)}}">
+				<a native target="_blank" href="{{route('patients.view.dashboard', $patient->uid)}}">
 					{{$patient->chart_number}}
 				</a>
 			</td>
 			<td>
-				{{$patient->displayName()}}
+				{{$patient->display_name}}
 			</td>
 			<td class="text-nowrap">{{ friendly_date_time($patient->dob, false) }}</td>
 			<td>{{ $patient->age_in_years ?  $patient->age_in_years : '-' }}</td>
@@ -41,11 +41,11 @@
 				</div>
 			</td>
 			<td>
-				@include('app.patient.coverage_column_renderer', ['patient'=>$patient])
+				@include('app.patient.coverage_column_fast_renderer', ['patient'=>$patient])
 			</td>
 			<td>
 				<div class="d-flex flex-column">
-					<span>{{ $patient->mcp ? $patient->mcp->displayName() :'' }}</span>	
+					<span>{{ $patient->mcp_pro_display_name ? $patient->mcp_pro_display_name :'' }}</span>	
 					<span>Status: 
 						@if($patient->has_mcp_done_onboarding_visit === 'YES')
 						<i class="fas fa-check-circle text-success" data-toggle="tooltip" data-placement="bottom" title="YES"></i>
@@ -55,21 +55,21 @@
 							<i class="fas fa-question-circle text-secondary" data-toggle="tooltip" data-placement="bottom" title="UNKNOWN"></i>
 						@endif
 					</span>				
-					@if($patient->mostRecentCompletedMcpNote)
+					@if($patient->most_recent_completed_mcp_note_id)
 						<span>Last Encounter:  
-						<a href="/patients/view/{{ $patient->mostRecentCompletedMcpNote->client->uid }}/notes/view/{{ $patient->mostRecentCompletedMcpNote->uid }}" class="font-weight-bold">
-                                {{ friendly_date_time($patient->mostRecentCompletedMcpNote->effective_dateest, false) }}
+						<a href="/patients/view/{{ $patient->uid }}/notes/view/{{ $patient->most_recent_completed_mcp_note_uid }}" class="font-weight-bold">
+                                {{ friendly_date_time($patient->most_recent_completed_mcp_note_effective_dateest, false) }}
                             </a>
 						</span>
 					@endif
 					@if($patient->next_mcp_appointment_date)
 						<span class="text-muted">Next Appt. <b>{{ friendly_date_time($patient->next_mcp_appointment_date) }}</b></span>
 					@endif
-					<?php $recentNote = $patient->mostRecentCompletedMcpNote; ?>
-					@if($recentNote)
+					
+					@if($patient->most_recent_completed_mcp_note_uid)
 					<div>
-						Note: <a href="/patients/view/{{ $patient->uid }}/notes/view/{{ $recentNote->uid }}" class="font-weight-bold">
-                        {{ friendly_date_time($recentNote->effective_dateest, false) }}
+						Note: <a href="/patients/view/{{ $patient->uid }}/notes/view/{{ $patient->most_recent_completed_mcp_note_uid }}" class="font-weight-bold">
+                        {{ friendly_date_time($patient->most_recent_completed_mcp_note_effective_dateest, false) }}
                     </a>
 					</div>
 					@endif
@@ -107,13 +107,13 @@
 							<span>{{ $patient->why_not_eligible_for_rm_memo }}</span>
 						</div>
 				   </div>
-				<?php $m = $patient->lastMeasurementOfType('Wt. (lbs.)');?>
-				@if($m)
-				<span class="d-block mt-1">Wt. (lbs.) {{$m && $m->value ? round($m->value, 2) : '-'}}</span>
+				   
+				@if($patient->last_measurement_of_type_weight)
+				<span class="d-block mt-1">Wt. (lbs.) {{$patient->last_measurement_of_type_weight ? round($patient->last_measurement_of_type_weight, 2) : '-'}}</span>
 				@endif
-				<?php $bp = $patient->lastMeasurementOfType('BP'); ?>
-				@if($bp)
-				<span class="d-block mt-1">BP. {{$bp && $bp->value ? $bp->value : '-'}}</span>
+				
+				@if($patient->last_measurement_of_type_bp)
+				<span class="d-block mt-1">BP. {{$patient->last_measurement_of_type_bp ? $patient->last_measurement_of_type_bp : '-'}}</span>
 				@endif
 			   </div>
 			</td>