Ver código fonte

RM action report - overhaul w/ multi-sort

Vijayakrishnan 3 anos atrás
pai
commit
6256d5d1a9

+ 128 - 41
app/Http/Controllers/PracticeManagementController.php

@@ -3767,85 +3767,172 @@ ORDER BY c.name_last, c.name_first
 
         $numOfMeasurements = $request->get('num_of_measurements'); //16_or_more, 12_or_more
         $hasRecentVisit = $request->get('has_recent_visit'); //yes no
-        $hasBeenSpokenToThisMonth = $request->get('has_been_spoken_to'); //yes no 
+        $hasBeenSpokenToThisMonth = $request->get('has_been_spoken_to'); //yes no
 
-        $cmQuery = CareMonth::where('start_date', $careMonthStartDate);
+        // default sort
+        if(!$request->input('sort_by')) {
+            $orderBy = "cm.start_date ASC NULLS LAST, cm.client_id ASC NULLS LAST";
+        }
+        else {
+            $sortBy = json_decode($request->input('sort_by'));
+            $orderByClause = [];
+            $includeDefaultKeys = true;
+            foreach ($sortBy as $sortCriteria) {
+                $orderByClause[] = "{$sortCriteria->key} {$sortCriteria->order} NULLS LAST";
+            }
+            $orderBy = implode(', ', $orderByClause);
+        }
 
-        //remove dummies
-        $cmQuery = $cmQuery->whereHas('client', function($clientQuery) {
-            return $clientQuery->whereRaw("(client_engagement_status_category != 'DUMMY' OR client_engagement_status_category IS NULL)")
-            ->where('is_enrolled_in_rm', 'YES');
-        });
+        $conditions = [];
+
+        // start date
+        $conditions[] = "(cm.start_date >= '$careMonthStartDate')";
 
+        // only those enrolled in rm
+        $conditions[] = "(c.is_enrolled_in_rm = 'YES')";
+
+        // measurement days
         if($numOfMeasurements){
-            if($numOfMeasurements  == '16_or_more'){
-                $cmQuery = $cmQuery->where('number_of_days_with_remote_measurements', '>=', 16);
+            if($numOfMeasurements  == '16_or_more') {
+                $conditions[] = "(cm.number_of_days_with_remote_measurements >= 16)";
             }
-            if($numOfMeasurements  == 'min_or_more'){
-                $cmQuery = $cmQuery->where('number_of_days_with_remote_measurements', '>=', $minRequiredMeasurements)
-                ->where('number_of_days_with_remote_measurements', '<', 16);
+            elseif($numOfMeasurements  == 'min_or_more') {
+                $conditions[] = "(cm.number_of_days_with_remote_measurements >= $minRequiredMeasurements AND cm.number_of_days_with_remote_measurements < 16)";
             }
-
-            if($numOfMeasurements  == 'less_than_min'){
-                $cmQuery = $cmQuery->where('number_of_days_with_remote_measurements', '<', $minRequiredMeasurements);
+            elseif($numOfMeasurements  == 'less_than_min') {
+                $conditions[] = "(cm.number_of_days_with_remote_measurements < $minRequiredMeasurements)";
             }
         }
 
+        // days since last visit
         if($hasRecentVisit){
             if($hasRecentVisit == 'YES'){
-                $cmQuery = $cmQuery->whereHas('client', function($clientQuery) {
-                    return $clientQuery->whereRaw("most_recent_completed_mcp_note_date::DATE >= ((NOW() - interval '90 days')::DATE)");
-                });
+                $conditions[] = "(c.most_recent_completed_mcp_note_date >= ((NOW() - interval '90 days')::DATE))";
             }else{
-                $cmQuery = $cmQuery->whereHas('client', function($clientQuery) {
-                    return $clientQuery->whereRaw("most_recent_completed_mcp_note_date::DATE < ((NOW() - interval '90 days')::DATE)");
-                });
+                $conditions[] = "(c.most_recent_completed_mcp_note_date::DATE < ((NOW() - interval '90 days')::DATE))";
             }
         }
 
+        // communicated
         if($hasBeenSpokenToThisMonth){
-            if($hasBeenSpokenToThisMonth  == 'YES'){
-                $cmQuery = $cmQuery->where('has_anyone_interacted_with_client_about_rm_outside_note', '=', true);
-            }else{
-                $cmQuery = $cmQuery->where('has_anyone_interacted_with_client_about_rm_outside_note', '=', false);
+            if($hasBeenSpokenToThisMonth  == 'YES') {
+                $conditions[] = "(cm.has_anyone_interacted_with_client_about_rm_outside_note IS TRUE)";
+            }
+            else {
+                $conditions[] = "(cm.has_anyone_interacted_with_client_about_rm_outside_note IS NOT TRUE)";
             }
-           
         }
 
+        // claiming closed
         $claimingClosed = $request->get('claiming_closed');
         if($claimingClosed){
-            if($claimingClosed === 'YES') $cmQuery = $cmQuery->where('is_claim_closed', true);
-            if($claimingClosed === 'NO') $cmQuery = $cmQuery->where('is_claim_closed', false);
+            if($claimingClosed === 'YES') {
+                $conditions[] = "(cm.is_claim_closed IS TRUE)";
+            }
+            elseif($claimingClosed === 'NO') {
+                $conditions[] = "(cm.is_claim_closed IS TRUE)";
+            }
         }
 
+        // mcp
         if($request->input('mcp_uid')) {
             $mcp = Pro::where('uid', $request->input('mcp_uid'))->first();
             if($mcp) {
-                $cmQuery = $cmQuery->where('mcp_pro_id', $mcp->id);
+                $conditions[] = "(cm.mcp_pro_id = $mcp->id)";
             }
         }
 
+        // client status
         if($request->input('status')) {
             $v = trim($request->input('status'));
-            $q = '';
             if($v === 'ACTIVE') {
-                $q = "(client.client_engagement_status_category IS NULL OR client.client_engagement_status_category = '{$v}')";
+                $conditions[] = "(c.client_engagement_status_category IS NULL OR c.client_engagement_status_category = '{$v}')";
             }
             else {
-                $q = "(client.client_engagement_status_category = '{$v}')";
-            }
-
-            if($q) {
-                $cmQuery = $cmQuery->whereHas('client', function($clientQuery) use ($q) {
-                    return $clientQuery->whereRaw($q);
-                });
+                $conditions[] = "(c.client_engagement_status_category = '{$v}')";
             }
         }
+        else {
+            $conditions[] = "(c.client_engagement_status_category IS NULL OR c.client_engagement_status_category = 'ACTIVE')";
+        }
+
+        $columns = "
+            cm.id as care_month_id,
+            cm.uid as care_month_uid,
+            cm.start_date,
+            c.uid as client_uid,
+            (c.name_first || ' ' || c.name_last) as client_name,
+            (mcp.name_first || ' ' || mcp.name_last) as mcp_name,
+            (rmm.name_first || ' ' || rmm.name_last) as rmm_name,
+            cm.number_of_days_with_remote_measurements,
+            DATE_PART('day', NOW() - c.most_recent_cellular_measurement_at) as dslm,
+            c.most_recent_completed_mcp_note_date as mr_note_date,
+            cm.days_between_most_recent_mcp_note_date_and_end_of_care_month,
+            mrnote.uid as mr_note_uid,
+            cm.rm_total_time_in_seconds,
+            cm.rm_total_time_in_seconds_by_mcp,
+            cm.rm_total_time_in_seconds_by_rmm_pro,
+            cm.has_admin_interacted_with_client_about_rm,
+            cm.has_mcp_interacted_with_client_about_rm,
+            cm.claim_suggestion_json,
+            cm.is_claim_closed,
+            (CASE 
+               WHEN cm.mcp_rm_generic_bill_id IS NOT NULL AND mcp_rm_bill.is_cancelled IS NOT TRUE THEN TRUE
+               ELSE FALSE
+            END) as mcp_payable,
+            (CASE 
+               WHEN cm.rmm_rm_generic_bill_id IS NOT NULL AND rmm_rm_bill.is_cancelled IS NOT TRUE THEN TRUE
+               ELSE FALSE
+            END) as rmm_payable,
+            mcp_rm_bill.uid as mcp_rm_bill_uid,
+            rmm_rm_bill.uid as rmm_rm_bill_uid,
+            mcp_rm_bill.code as mcp_rm_bill_code,
+            rmm_rm_bill.code as rmm_rm_bill_code,
+            (SELECT cl.status FROM claim cl WHERE cl.care_month_id = cm.id AND cl.is_cancelled IS NOT TRUE LIMIT 1) as claim_status
+        ";
+
+        $from = "
+            care_month cm 
+            join client c on cm.client_id = c.id
+            left join pro mcp on c.mcp_pro_id = mcp.id
+            left join pro rmm on c.rmm_pro_id = rmm.id
+            left join bill mcp_rm_bill on cm.mcp_rm_generic_bill_id = mcp_rm_bill.id
+            left join bill rmm_rm_bill on cm.rmm_rm_generic_bill_id = rmm_rm_bill.id
+            left join note mrnote on c.most_recent_completed_mcp_note_id = mrnote.id
+        ";
+
+        $page = $request->input('page') ?: 1;
+        $perPage = $request->input('per_page') ?: 15;
+        $offset = ($page - 1) * $perPage;
+
+        $countSql = "
+        SELECT 
+            COUNT(*)
+        FROM
+            $from
+        where " . implode(" AND ", $conditions) . "
+        ";
+
+        $countResult = DB::select($countSql);
+        $total = $countResult[0]->count;
+
+        $sql = "
+        SELECT 
+            $columns
+        FROM
+            $from
+        WHERE " . implode(" AND ", $conditions) . "
+        ORDER BY $orderBy
+        OFFSET {$offset} LIMIT {$perPage}
+        ";
+
+        $rows = DB::select($sql);
 
-        $cmQuery = $cmQuery->orderBy('start_date', 'DESC')->orderBy('client_id', 'ASC');
-        $rows =$cmQuery->paginate(15);
+        $paginator = new LengthAwarePaginator($rows, $total, $request->input('per_page') ?: 15, $request->input('page') ?: 1);
+        $perPage = $request->input('per_page') ?: 15;
+        $paginator->setPath(route('practice-management.rmActionReport'));
 
-        return view('app.practice-management.rm-action-report', compact('rows', 'filters', 'minRequiredMeasurements'));
+        return view('app.practice-management.rm-action-report', compact('rows', 'filters', 'minRequiredMeasurements', 'paginator'));
 
     }
 

+ 64 - 90
resources/views/app/practice-management/rm-action-report.blade.php

@@ -128,64 +128,58 @@
             <table class="table table-sm table-striped table-bordered border-0 p-0 m-0 text-nowrap">
                 <thead class="bg-light border-bottom-0">
                     <tr>
-                        <th>Month</th>
-                        <th>Patient</th>                        
-                        <th>MCP</th>
-                        <th>RMM</th>
-                        <th>Meas.<br>Days</th>
-                        <th>DSLM</th>
-                        <th>Last<br>Visit</th>
-                        <th>DSLV</th>
-                        <th>MCP<br>Mins.</th>
-                        <th>RMM<br>Mins.</th>
-                        <th>Total<br>Mins.</th>
-                        <th>Admin<br>Comm.</th>
-                        <th>MCP<br>Comm.</th>
-                        <th>MCP<br>Payable</th>
-                        <th>MCP Bill</th>
-                        <th>RMM<br>Payable</th>
-                        <th>RMM Bill</th>
-                        <th>Guidance</th>
-                        <th>Claiming<br>Closed?</th>
-                        <th>Claim</th>
-                        <th>Claim<br>Status</th>
+                        <th>@include('app.practice-management._sort_header_multi', ['route' => route("practice-management.rmActionReport"), 'label' => 'Month', 'key' => 'start_date'])</th>
+                        <th>@include('app.practice-management._sort_header_multi', ['route' => route("practice-management.rmActionReport"), 'label' => 'Patient', 'key' => 'client_name'])</th>
+                        <th>@include('app.practice-management._sort_header_multi', ['route' => route("practice-management.rmActionReport"), 'label' => 'MCP', 'key' => 'mcp_name'])</th>
+                        <th>@include('app.practice-management._sort_header_multi', ['route' => route("practice-management.rmActionReport"), 'label' => 'RMM', 'key' => 'rmm_name'])</th>
+                        <th>@include('app.practice-management._sort_header_multi', ['route' => route("practice-management.rmActionReport"), 'label' => 'Meas.<br>Days', 'key' => 'number_of_days_with_remote_measurements'])</th>
+                        <th>@include('app.practice-management._sort_header_multi', ['route' => route("practice-management.rmActionReport"), 'label' => 'DSLM', 'key' => 'dslm'])</th>
+                        <th>@include('app.practice-management._sort_header_multi', ['route' => route("practice-management.rmActionReport"), 'label' => 'Last<br>Visit', 'key' => 'mr_note_date'])</th>
+                        <th>@include('app.practice-management._sort_header_multi', ['route' => route("practice-management.rmActionReport"), 'label' => 'DSLV', 'key' => 'days_between_most_recent_mcp_note_date_and_end_of_care_month'])</th>
+                        <th>@include('app.practice-management._sort_header_multi', ['route' => route("practice-management.rmActionReport"), 'label' => 'MCP<br>Mins.', 'key' => 'rm_total_time_in_seconds_by_mcp'])</th>
+                        <th>@include('app.practice-management._sort_header_multi', ['route' => route("practice-management.rmActionReport"), 'label' => 'RMM<br>Mins.', 'key' => 'rm_total_time_in_seconds_by_rmm_pro'])</th>
+                        <th>@include('app.practice-management._sort_header_multi', ['route' => route("practice-management.rmActionReport"), 'label' => 'Total<br>Mins.', 'key' => 'rm_total_time_in_seconds'])</th>
+                        <th>@include('app.practice-management._sort_header_multi', ['route' => route("practice-management.rmActionReport"), 'label' => 'Admin<br>Comm.', 'key' => 'has_admin_interacted_with_client_about_rm'])</th>
+                        <th>@include('app.practice-management._sort_header_multi', ['route' => route("practice-management.rmActionReport"), 'label' => 'MCP<br>Comm.', 'key' => 'has_mcp_interacted_with_client_about_rm'])</th>
+                        <th>@include('app.practice-management._sort_header_multi', ['route' => route("practice-management.rmActionReport"), 'label' => 'MCP<br>Payable', 'key' => 'mcp_payable'])</th>
+                        <th class="text-secondary">MCP Bill</th>
+                        <th>@include('app.practice-management._sort_header_multi', ['route' => route("practice-management.rmActionReport"), 'label' => 'RMM<br>Payable', 'key' => 'rmm_payable'])</th>
+                        <th class="text-secondary">RMM Bill</th>
+                        <th class="text-secondary">Guidance</th>
+                        <th>@include('app.practice-management._sort_header_multi', ['route' => route("practice-management.rmActionReport"), 'label' => 'Claiming<br>Closed?', 'key' => 'is_claim_closed'])</th>
+                        <th class="text-secondary">Claim</th>
+                        <th>@include('app.practice-management._sort_header_multi', ['route' => route("practice-management.rmActionReport"), 'label' => 'Claim<br>Status', 'key' => 'claim_status'])</th>
                     </tr>
                 </thead>
                 <tbody>
                     @foreach ($rows as $row)
                     <tr class="{{false ? 'bg-light' : ''}}">
                         <td class="text-nowrap border-left-0">
-                            <a href="/patients/view/{{$row->client->uid}}/care-months/view/{{$row->uid}}">
+                            <a href="/patients/view/{{$row->client_uid}}/care-months/view/{{$row->care_month_uid}}">
                                 {{friendly_date_month_year($row->start_date)}}
                             </a>
                         </td>
                         <td class="">
-                            <a href="/patients/view/{{$row->client->uid}}">
-                                {{$row->client->name_first}} {{$row->client->name_last}}
+                            <a href="/patients/view/{{$row->client_uid}}">
+                                {{$row->client_name}}
                             </a>
                         </td>                        
                         <td>
-                            @if($row->mcp)
-                                {{$row->mcp->name_first}} {{$row->mcp->name_last}}
-                            @else 
-                                --
-                            @endif
+                            {{$row->mcp_name}}
                         </td>
                         <td>
-                            @if($row->rmmPro)
-                                {{$row->rmmPro->name_first}} {{$row->rmmPro->name_last}}
-                            @else 
-                                --
-                            @endif
+                            {{$row->rmm_name}}
                         </td>
                         <td>
                             {{$row->number_of_days_with_remote_measurements}}
                         </td>
-                        <td></td>
+                        <td>
+                            {{$row->dslm}}
+                        </td>
                         <td>
                             <!-- Most Recent Visit -->
-                            @if($row->client->mostRecentCompletedMcpNote)
-                            <a href="/patients/view/{{ $row->patient->uid }}/notes/view/{{ $row->client->mostRecentCompletedMcpNote->uid }}">{{ friendly_date($row->client->most_recent_completed_mcp_note_date) }}</a>
+                            @if($row->mr_note_uid)
+                                <a href="/patients/view/{{ $row->client_uid }}/notes/view/{{ $row->mr_note_uid }}">{{ friendly_date($row->mr_note_date) }}</a>
                             @else
                                 -
                             @endif
@@ -196,47 +190,39 @@
                         </td>
                         <td>
                             <!-- # Minutes Billed by MCP -->
-                            {{ $row->rm_total_time_in_seconds_by_mcp / 60 }}
+                            {{ round($row->rm_total_time_in_seconds_by_mcp / 60, 2) }}
                         </td>
                         <td>
                             <!-- # Minutes Billed by RMM -->
-                            {{ $row->rm_total_time_in_seconds_by_rmm / 60 }}
+                            {{ round($row->rm_total_time_in_seconds_by_rmm_pro / 60, 2) }}
                         </td>
                         <td>
                             <!-- # Minutes Billed Total -->
-                            {{ $row->rmTotalTimeInSeconds / 60 }}
+                            {{ round($row->rm_total_time_in_seconds / 60, 2) }}
                         </td>
                         <td>
                             <!-- Has Admin interacted? -->
-                            {{ $row->has_admin_interacted_with_client_about_rm }}
+                            {{ $row->has_admin_interacted_with_client_about_rm ? 'Yes' : 'No' }}
                         </td>
                         <td>
                             <!-- Has MCP interacted? -->
-                            {{ $row->has_mcp_interacted_with_client_about_rm }}
+                            {{ $row->has_mcp_interacted_with_client_about_rm ? 'Yes' : 'No' }}
                         </td>
                         <td>
                             <!-- Is Payable to MCP? -->
-                            {{ $row->mcpRmGenericBill ? 'YES':'NO' }}
+                            {{ $row->mcp_payable ? 'Yes' : 'No' }}
                         </td>
                         <td>
                             <!-- MCP Bill -->
-                            @if($row->mcpRmGenericBill)
-                                {{ $row->rmmRmGenericBill->code }}
-                            @else
-                                -
-                            @endif
+                            {{ $row->mcp_rm_bill_code }}
                         </td>
                         <td>
                             <!-- Is Payable to RMM? -->
-                            {{ $row->rmmRmGenericBill ? 'YES':'NO' }}
+                            {{ $row->rmm_payable ? 'Yes' : 'No' }}
                         </td>
                         <td>
                             <!-- RMM Bill -->
-                            @if($row->rmmRmGenericBill)
-                                {{ $row->rmmRmGenericBill->code }}
-                            @else
-                                -
-                            @endif
+                            {{ $row->rmm_rm_bill_code }}
                         </td>
                         <td>
                             <?php
@@ -254,26 +240,27 @@
                             {{ $row->is_claim_closed ? 'YES':'NO' }}
                         </td>
                         <td>
-                            <div class="d-flex justify-content-between">
-                                <div>
-                                    @foreach($row->claims as $claim)
-                                        <div class="d-flex flex-column">
-                                            @foreach($claim->lines as $claimLine)
-                                            <span><i class="fas fa-check-circle" style="color: #8bc34a;"></i> {{$claimLine->cpt}}</span>
-                                            @endforeach
-                                        </div>                                
-                                    @endforeach
-                                </div>
-                                
-                            </div>
-                            @include('app.patient.care-month._create-claim-table-action', ['careMonth' => $row])
+{{--                            <div class="d-flex justify-content-between">--}}
+{{--                                <div>--}}
+{{--                                    @foreach($row->claims as $claim)--}}
+{{--                                        <div class="d-flex flex-column">--}}
+{{--                                            @foreach($claim->lines as $claimLine)--}}
+{{--                                            <span><i class="fas fa-check-circle" style="color: #8bc34a;"></i> {{$claimLine->cpt}}</span>--}}
+{{--                                            @endforeach--}}
+{{--                                        </div>                                --}}
+{{--                                    @endforeach--}}
+{{--                                </div>--}}
+{{--                                --}}
+{{--                            </div>--}}
+{{--                            @include('app.patient.care-month._create-claim-table-action', ['careMonth' => $row])--}}
                         </td>
                         <td>
-                            <?php $rowFirstClaim = $row->claims->first(); ?>
-                            @if($rowFirstClaim)
-                                <span class="mr-2"><b>{{$rowFirstClaim->status}}</b></span>
-                                @include('app.patient.care-month.update-claim-status', ['claim' => $rowFirstClaim])
-                            @endif
+{{--                            <?php $rowFirstClaim = $row->claims->first(); ?>--}}
+{{--                            @if($rowFirstClaim)--}}
+{{--                                <span class="mr-2"><b>{{$rowFirstClaim->status}}</b></span>--}}
+{{--                                @include('app.patient.care-month.update-claim-status', ['claim' => $rowFirstClaim])--}}
+{{--                            @endif--}}
+                            {{$row->claim_status}}
                         </td>
                     </tr>
                     @endforeach
@@ -282,26 +269,13 @@
 
         </div>
     </div>
-    <div class="mt-3">
-        {{$rows->appends(request()->input())->links()}}
+    <div class="d-flex align-items-baseline mt-3">
+        {!! $paginator->withQueryString()->links() !!}
+        <div class="mb-3">
+            <div class="ml-4 mb-3">Showing <b>{{$paginator->firstItem()}}</b> to <b>{{$paginator->lastItem()}}</b> (page {{$paginator->currentPage()}}) of <b>{{$paginator->total()}}</b> care months</div>
+        </div>
     </div>
 
 </div>
-<?php
-$loadedFilters = $filters;
-$allFilterKeys = [
-	'care_month_start_date',
-    'care_month_year',
-    'claiming_closed',
-    'num_of_measurements',
-    'has_recent_visit',
-    'has_been_spoken_to',
-];
-for ($i=0; $i < count($allFilterKeys); $i++) {
-	if (!isset($loadedFilters[$allFilterKeys[$i]]) || !$loadedFilters[$allFilterKeys[$i]]) {
-		$loadedFilters[$allFilterKeys[$i]] = '';
-	}
-}
-?>
 
 @endsection