Browse Source

added debug 1

= 2 years ago
parent
commit
8f461c8862
1 changed files with 132 additions and 131 deletions
  1. 132 131
      app/Http/Controllers/PracticeManagementController.php

+ 132 - 131
app/Http/Controllers/PracticeManagementController.php

@@ -112,10 +112,10 @@ class PracticeManagementController extends Controller
         if($type === 'pro') {
             $fPro = Pro::where('uid', $request->input('p'))->first();
             $rows = DB::select(DB::raw("
-SELECT effective_date, hcp_pro_id, p.name_first, p.name_last, count(*), sum(number_of_units) as units 
-FROM bill JOIN pro p on p.id = bill.hcp_pro_id 
-WHERE code = 'Treatment Services' 
-GROUP BY effective_date, hcp_pro_id, p.name_first, p.name_last 
+SELECT effective_date, hcp_pro_id, p.name_first, p.name_last, count(*), sum(number_of_units) as units
+FROM bill JOIN pro p on p.id = bill.hcp_pro_id
+WHERE code = 'Treatment Services'
+GROUP BY effective_date, hcp_pro_id, p.name_first, p.name_last
 ORDER BY effective_date DESC, p.name_first ASC, p.name_last ASC
 "));
         }
@@ -177,7 +177,7 @@ SELECT effective_date, count(*), sum(number_of_units) as units FROM bill WHERE c
         }
 
         if($zero_deductible){
-            $conditions[] = "(SELECT count(*) FROM client_primary_coverage cpc WHERE 
+            $conditions[] = "(SELECT count(*) FROM client_primary_coverage cpc WHERE
             ((cpc.plan_type = 'MEDICARE' AND cpc.auto_medicare_mpb_deductible > 0) OR (cpc.plan_type = 'COMMERCIAL' AND cpc.deductible_amount > 0))
             AND c.effective_client_primary_coverage_id = cpc.id) = 0";
         }
@@ -188,12 +188,12 @@ SELECT effective_date, count(*), sum(number_of_units) as units FROM bill WHERE c
 
         if($verified){
             if($verified == 'VERIFIED'){
-                $conditions[] = "(SELECT count(*) FROM bill b WHERE 
+                $conditions[] = "(SELECT count(*) FROM bill b WHERE
             b.is_verified IS TRUE AND b.is_cancelled IS FALSE AND b.note_id = n.id) > 0";
             }
 
             if($verified == 'UNVERIFIED'){
-                $conditions[] = "(SELECT count(*) FROM bill b WHERE 
+                $conditions[] = "(SELECT count(*) FROM bill b WHERE
             b.is_verified IS FALSE AND b.is_cancelled IS FALSE AND b.note_id = n.id) > 0";
             }
         }
@@ -253,7 +253,7 @@ SELECT c.id                                                                 AS c
        n.is_bill_closed                                                     AS is_billing_closed,
        n.is_claim_closed                                                    AS is_claiming_closed,
        bill_sum.count                                                       AS bill_count,
-       clm.status                                                           AS claim_status,    
+       clm.status                                                           AS claim_status,
        cpc.plan_type                                                        AS plan_type,
        cpc.auto_medicare_is_partbprimary                                    AS auto_medicare_is_partbprimary,
        cpc.auto_medicare_mpb_deductible                                     AS medicare_deductible,
@@ -277,7 +277,7 @@ SELECT c.id                                                                 AS c
                                                     WHERE claim.note_id = n.id))
                              ORDER BY cl.position_index), '
 '::text)          AS claim_lines,
-       array_to_string(ARRAY(SELECT cl2.status FROM claim cl2 WHERE cl2.note_id = n.id 
+       array_to_string(ARRAY(SELECT cl2.status FROM claim cl2 WHERE cl2.note_id = n.id
                                                               ORDER BY cl2.created_at DESC), '
 '::text)          AS claim_statuses
 $from
@@ -434,11 +434,11 @@ OFFSET {$offset} LIMIT {$perPage}
         $filters = $request->all();
         $query = Note::query();
         $query = $query->where('is_core_note', false);
-        
-        
+
+
         $this->filterMultiQuery($request, $query, 'created_at', 'created_at_category', 'created_at_value_1', 'created_at_value_2');
         $this->filterMultiQuery($request, $query, 'effective_dateest', 'effective_date_category', 'effective_date_value_1', 'effective_date_value_2');
-       
+
         $filterByProsIDs = $request->get('pros');
         if($filterByProsIDs && count($filterByProsIDs)){
             $query = $query->whereIn('hcp_pro_id', $filterByProsIDs);
@@ -452,8 +452,8 @@ OFFSET {$offset} LIMIT {$perPage}
         $filterByPatientsIDs = $request->get('patients');
         if($filterByPatientsIDs && count($filterByPatientsIDs)){
             $query = $query->whereIn('client_id', $filterByPatientsIDs);
-        } 
-        
+        }
+
         $status = $request->get('status');
         if($status){
             if($status == 'CANCELLED') $query = $query->where('is_cancelled', true);
@@ -462,10 +462,10 @@ OFFSET {$offset} LIMIT {$perPage}
             if($status == 'NOT_YET_SIGNED_BUT_ALLY_SIGNED') $query = $query->where('is_signed_by_hcp', false)->where('is_signed_by_ally', true);
             if($status == 'WITH_BILLS') $query = $query->where('is_signed_by_hcp', true)->where('is_cancelled', false)->whereHas('bills');
             if($status == 'WITHOUT_BILLS') $query = $query->where('is_signed_by_hcp', true)->where('is_cancelled', false)->whereDoesntHave('bills');
-            
+
         }
 
-        
+
         $allProsWithNotesIDs = DB::table('note')->pluck('hcp_pro_id')->toArray();
         $allProsWithNotes = Pro::whereIn('id', $allProsWithNotesIDs)->get();
 
@@ -1033,7 +1033,7 @@ OFFSET {$offset} LIMIT {$perPage}
             $bills = $bills->where('code', '=', $filter);
         }
 
-       
+
 
         $bills = $bills->orderBy('effective_date', 'desc')->paginate();
 
@@ -1109,9 +1109,9 @@ OFFSET {$offset} LIMIT {$perPage}
         // 99454
         $filter = $request->input('c4');
         if ($filter) {
-            $q = "(SELECT COUNT(claim_line.id) 
-FROM claim_line 
-WHERE claim_line.cpt = '99454' AND 
+            $q = "(SELECT COUNT(claim_line.id)
+FROM claim_line
+WHERE claim_line.cpt = '99454' AND
     claim_line.claim_id IN (SELECT claim.id FROM claim WHERE status <> 'CANCELLED' AND care_month_id = bill.care_month_id))";
             if ($filter == 'yes') {
                 $bills = $bills->whereRaw("$q > 0");
@@ -1124,9 +1124,9 @@ WHERE claim_line.cpt = '99454' AND
         // 99457
         $filter = $request->input('c7');
         if ($filter) {
-            $q = "(SELECT COUNT(claim_line.id) 
-FROM claim_line 
-WHERE claim_line.cpt = '99457' AND 
+            $q = "(SELECT COUNT(claim_line.id)
+FROM claim_line
+WHERE claim_line.cpt = '99457' AND
     claim_line.claim_id IN (SELECT claim.id FROM claim WHERE status <> 'CANCELLED' AND care_month_id = bill.care_month_id))";
             if ($filter == 'yes') {
                 $bills = $bills->whereRaw("$q > 0");
@@ -1139,9 +1139,9 @@ WHERE claim_line.cpt = '99457' AND
         // 99458
         $filter = $request->input('c8');
         if ($filter) {
-            $q = "(SELECT COUNT(claim_line.id) 
-FROM claim_line 
-WHERE claim_line.cpt = '99458' AND 
+            $q = "(SELECT COUNT(claim_line.id)
+FROM claim_line
+WHERE claim_line.cpt = '99458' AND
     claim_line.claim_id IN (SELECT claim.id FROM claim WHERE status <> 'CANCELLED' AND care_month_id = bill.care_month_id))";
             if ($filter == 'yes') {
                 $bills = $bills->whereRaw("$q > 0");
@@ -1188,7 +1188,7 @@ WHERE claim_line.cpt = '99458' AND
 //        if(!$to_date){
 //            $to_date = '2900-12-31';
 //        }
-        
+
 
         //TODO sanitize inputs
 
@@ -1202,7 +1202,7 @@ WHERE claim_line.cpt = '99458' AND
 
         $rows = DB::table('pro')
         ->selectRaw(" *,
-            (SELECT id FROM note WHERE note.hcp_pro_id = pro.id AND is_cancelled = false ORDER BY created_at DESC LIMIT 1) as last_note_id, 
+            (SELECT id FROM note WHERE note.hcp_pro_id = pro.id AND is_cancelled = false ORDER BY created_at DESC LIMIT 1) as last_note_id,
             (SELECT note.created_at FROM note WHERE note.hcp_pro_id = pro.id AND note.is_cancelled = false ORDER BY note.created_at DESC LIMIT 1) as last_note_created_at,
             (SELECT COUNT(*) FROM note WHERE note.hcp_pro_id = pro.id AND note.is_cancelled = false AND created_at::DATE >= '${startOfCurrentMonth}'::DATE AND created_at::DATE <= '${endOfCurrentMonth}'::DATE ) AS notes_this_month,
             (SELECT COUNT(*) FROM client WHERE client.mcp_pro_id = pro.id AND client.client_engagement_status_category <> 'DUMMY' AND client.client_engagement_status_category <> 'DUPLICATE') as total_client_count,
@@ -1211,7 +1211,7 @@ WHERE claim_line.cpt = '99458' AND
 
         $prosIDs = $request->get('pros_ids');
         if($prosIDs){
-            $pros = 
+            $pros =
             $rows = $rows->whereIn('pro.id', $prosIDs);
         }
 
@@ -1393,7 +1393,7 @@ WHERE claim_line.cpt = '99458' AND
         $query = "
 SELECT client.name_first, client.name_last,
        (client.name_first || ' ' || client.name_last) as client_name,
-       client.uid as client_uid, 
+       client.uid as client_uid,
        client.dob,
        care_month.is_client_enrolled_in_rm,
        client.most_recent_completed_mcp_note_date,
@@ -1424,17 +1424,17 @@ SELECT client.name_first, client.name_last,
        client.cell_number,
        care_month.most_recent_cellular_bp_dbp_mm_hg,
        care_month.most_recent_cellular_bp_sbp_mm_hg,
-       care_month.most_recent_cellular_bp_measurement_at,      
+       care_month.most_recent_cellular_bp_measurement_at,
        care_month.most_recent_cellular_weight_value,
        care_month.most_recent_cellular_weight_measurement_at
 FROM care_month join client on care_month.client_id = client.id join note mrnote on client.most_recent_completed_mcp_note_id = mrnote.id
      left join note mrmnote on mrmnote.id = (
-        select max(n.id) from note n 
-        where 
-              n.client_id = client.id AND 
+        select max(n.id) from note n
+        where
+              n.client_id = client.id AND
               n.is_cancelled = FALSE AND
               (n.is_signed_by_hcp IS NOT NULL AND n.is_signed_by_hcp = TRUE) AND
-              n.effective_dateest::date >= care_month.start_date::date AND 
+              n.effective_dateest::date >= care_month.start_date::date AND
               n.effective_dateest::date < (care_month.start_date::date + INTERVAL '1 month')
      )
 WHERE
@@ -1461,7 +1461,7 @@ WHERE
         $query = "
 SELECT client.name_first, client.name_last,
        (client.name_first || ' ' || client.name_last) as client_name,
-       client.uid as client_uid, 
+       client.uid as client_uid,
        client.dob,
        care_month.is_client_enrolled_in_rm,
        client.most_recent_completed_mcp_note_date,
@@ -1492,17 +1492,17 @@ SELECT client.name_first, client.name_last,
        client.cell_number,
        care_month.most_recent_cellular_bp_dbp_mm_hg,
        care_month.most_recent_cellular_bp_sbp_mm_hg,
-       care_month.most_recent_cellular_bp_measurement_at,      
+       care_month.most_recent_cellular_bp_measurement_at,
        care_month.most_recent_cellular_weight_value,
        care_month.most_recent_cellular_weight_measurement_at
 FROM care_month join client on care_month.client_id = client.id join note mrnote on client.most_recent_completed_mcp_note_id = mrnote.id
      left join note mrmnote on mrmnote.id = (
-        select max(n.id) from note n 
-        where 
-              n.client_id = client.id AND 
+        select max(n.id) from note n
+        where
+              n.client_id = client.id AND
               n.is_cancelled = FALSE AND
               (n.is_signed_by_hcp IS NOT NULL AND n.is_signed_by_hcp = TRUE) AND
-              n.effective_dateest::date >= care_month.start_date::date AND 
+              n.effective_dateest::date >= care_month.start_date::date AND
               n.effective_dateest::date < (care_month.start_date::date + INTERVAL '1 month')
      )
 WHERE care_month.uid = '{$careMonthUid}' AND client.uid = '{$clientUid}'
@@ -1521,7 +1521,7 @@ WHERE care_month.uid = '{$careMonthUid}' AND client.uid = '{$clientUid}'
         $query = "
 SELECT client.name_first, client.name_last,
        (client.name_first || ' ' || client.name_last) as client_name,
-       client.uid as client_uid, 
+       client.uid as client_uid,
        client.dob,
        care_month.is_client_enrolled_in_rm,
        client.most_recent_completed_mcp_note_date,
@@ -1552,17 +1552,17 @@ SELECT client.name_first, client.name_last,
        client.cell_number,
        care_month.most_recent_cellular_bp_dbp_mm_hg,
        care_month.most_recent_cellular_bp_sbp_mm_hg,
-       care_month.most_recent_cellular_bp_measurement_at,      
+       care_month.most_recent_cellular_bp_measurement_at,
        care_month.most_recent_cellular_weight_value,
        care_month.most_recent_cellular_weight_measurement_at
 FROM care_month join client on care_month.client_id = client.id join note mrnote on client.most_recent_completed_mcp_note_id = mrnote.id
      left join note mrmnote on mrmnote.id = (
-        select max(n.id) from note n 
-        where 
-              n.client_id = client.id AND 
+        select max(n.id) from note n
+        where
+              n.client_id = client.id AND
               n.is_cancelled = FALSE AND
               (n.is_signed_by_hcp IS NOT NULL AND n.is_signed_by_hcp = TRUE) AND
-              n.effective_dateest::date >= care_month.start_date::date AND 
+              n.effective_dateest::date >= care_month.start_date::date AND
               n.effective_dateest::date < (care_month.start_date::date + INTERVAL '1 month')
      )
 WHERE care_month.uid = '{$careMonthUid}' AND client.uid = '{$clientUid}'
@@ -1581,7 +1581,7 @@ WHERE care_month.uid = '{$careMonthUid}' AND client.uid = '{$clientUid}'
         $query = "
 SELECT client.name_first, client.name_last,
        (client.name_first || ' ' || client.name_last) as client_name,
-       client.uid as client_uid, 
+       client.uid as client_uid,
        client.dob,
        care_month.is_client_enrolled_in_rm,
        client.most_recent_completed_mcp_note_date,
@@ -1612,17 +1612,17 @@ SELECT client.name_first, client.name_last,
        client.cell_number,
        care_month.most_recent_cellular_bp_dbp_mm_hg,
        care_month.most_recent_cellular_bp_sbp_mm_hg,
-       care_month.most_recent_cellular_bp_measurement_at,      
+       care_month.most_recent_cellular_bp_measurement_at,
        care_month.most_recent_cellular_weight_value,
        care_month.most_recent_cellular_weight_measurement_at
 FROM care_month join client on care_month.client_id = client.id join note mrnote on client.most_recent_completed_mcp_note_id = mrnote.id
      left join note mrmnote on mrmnote.id = (
-        select max(n.id) from note n 
-        where 
-              n.client_id = client.id AND 
+        select max(n.id) from note n
+        where
+              n.client_id = client.id AND
               n.is_cancelled = FALSE AND
               (n.is_signed_by_hcp IS NOT NULL AND n.is_signed_by_hcp = TRUE) AND
-              n.effective_dateest::date >= care_month.start_date::date AND 
+              n.effective_dateest::date >= care_month.start_date::date AND
               n.effective_dateest::date < (care_month.start_date::date + INTERVAL '1 month')
      )
 WHERE care_month.uid = '{$careMonthUid}' AND client.uid = '{$clientUid}'
@@ -1822,7 +1822,7 @@ WHERE care_month.uid = '{$careMonthUid}' AND client.uid = '{$clientUid}'
         $query = "
 SELECT client.name_first, client.name_last,
        (client.name_first || ' ' || client.name_last) as client_name,
-       client.uid as client_uid, 
+       client.uid as client_uid,
        client.dob,
        care_month.is_client_enrolled_in_rm,
        client.most_recent_completed_mcp_note_date,
@@ -1854,22 +1854,22 @@ SELECT client.name_first, client.name_last,
        client.cell_number,
        care_month.most_recent_cellular_bp_dbp_mm_hg,
        care_month.most_recent_cellular_bp_sbp_mm_hg,
-       care_month.most_recent_cellular_bp_measurement_at,      
+       care_month.most_recent_cellular_bp_measurement_at,
        care_month.most_recent_cellular_weight_value,
        care_month.most_recent_cellular_weight_measurement_at,
 
 	mcp.name_last || ', ' || mcp.name_last AS mcp_name,
-	
+
        {$genericBillIdColumns}
 FROM care_month join client on care_month.client_id = client.id left join pro AS mcp on care_month.mcp_pro_id = mcp.id join note mrnote on client.most_recent_completed_mcp_note_id = mrnote.id
     {$genericBillJoinClause}
     left join note mrmnote on mrmnote.id = (
-        select max(n.id) from note n 
-        where 
-              n.client_id = client.id AND 
+        select max(n.id) from note n
+        where
+              n.client_id = client.id AND
               n.is_cancelled = FALSE AND
               (n.is_signed_by_hcp IS NOT NULL AND n.is_signed_by_hcp = TRUE) AND
-              n.effective_dateest::date >= care_month.start_date::date AND 
+              n.effective_dateest::date >= care_month.start_date::date AND
               n.effective_dateest::date < (care_month.start_date::date + INTERVAL '1 month')
      )
 WHERE
@@ -2252,7 +2252,7 @@ WHERE
 
         $countQuery = "
 SELECT COUNT(*)
-FROM care_month join client on care_month.client_id = client.id 
+FROM care_month join client on care_month.client_id = client.id
     join note mrnote on client.most_recent_completed_mcp_note_id = mrnote.id
     left join pro mcpPro on care_month.mcp_pro_id = mcpPro.id
     left join pro rmmPro on care_month.rmm_pro_id = rmmPro.id
@@ -2260,7 +2260,7 @@ FROM care_month join client on care_month.client_id = client.id
     left join payer on cpc.commercial_payer_id = payer.id
     {$genericBillJoinClause}
 WHERE
-      " . (count($conditions) > 0 ? implode(" AND ", $conditions) : '') . "      
+      " . (count($conditions) > 0 ? implode(" AND ", $conditions) : '') . "
 ";
 
         $countResult = DB::select($countQuery);
@@ -2269,7 +2269,7 @@ WHERE
         $query = "
 SELECT client.name_first, client.name_last,
        (client.name_first || ' ' || client.name_last) as client_name,
-       client.uid as client_uid, 
+       client.uid as client_uid,
        client.dob,
        client.age_in_years,
        care_month.is_client_enrolled_in_rm,
@@ -2320,7 +2320,7 @@ SELECT client.name_first, client.name_last,
        client.cell_number,
        care_month.most_recent_cellular_bp_dbp_mm_hg,
        care_month.most_recent_cellular_bp_sbp_mm_hg,
-       care_month.most_recent_cellular_bp_measurement_at,      
+       care_month.most_recent_cellular_bp_measurement_at,
        care_month.most_recent_cellular_weight_value,
        care_month.most_recent_cellular_weight_measurement_at,
        mcpPro.mcp_rpm_payment_strategy,
@@ -2333,7 +2333,7 @@ SELECT client.name_first, client.name_last,
        care_month.days_between_most_recent_mcp_note_date_and_end_of_care_month,
        cpc.plan_type,
        cpc.auto_medicare_is_partbprimary,
-       (CASE 
+       (CASE
            WHEN cpc.plan_type LIKE 'COMMERCIAL' THEN payer.name
            ELSE cpc.plan_type
        END) as payer_name,
@@ -2341,7 +2341,7 @@ SELECT client.name_first, client.name_last,
        (SELECT COUNT(*) FROM claim_line where claim_line.cpt = '99457' AND claim_line.claim_id IN (SELECT claim.id FROM claim WHERE claim.care_month_id = care_month.id AND claim.status != 'CANCELLED')) as claim_457,
        (SELECT COUNT(*) FROM claim_line where claim_line.cpt = '99458' AND claim_line.claim_id IN (SELECT claim.id FROM claim WHERE claim.care_month_id = care_month.id AND claim.status != 'CANCELLED')) as claim_458,
        {$genericBillIdColumns}
-FROM care_month join client on care_month.client_id = client.id 
+FROM care_month join client on care_month.client_id = client.id
     left join note mrnote on client.most_recent_completed_mcp_note_id = mrnote.id
     left join pro mcpPro on care_month.mcp_pro_id = mcpPro.id
     left join pro rmmPro on care_month.rmm_pro_id = rmmPro.id
@@ -2352,7 +2352,7 @@ FROM care_month join client on care_month.client_id = client.id
 WHERE
       " . (count($conditions) > 0 ? implode(" AND ", $conditions) : '') . "
       {$orderBy}  OFFSET {$offset} LIMIT {$perPage}
-      
+
 ";
 
         // dd($query);
@@ -2371,7 +2371,7 @@ WHERE
         $stats['totalPatients'] = $paginator->total();
         $commonStatSQL = "
 SELECT COUNT(*)
-FROM care_month join client on care_month.client_id = client.id 
+FROM care_month join client on care_month.client_id = client.id
     join note mrnote on client.most_recent_completed_mcp_note_id = mrnote.id
     left join pro mcpPro on care_month.mcp_pro_id = mcpPro.id
     left join pro rmmPro on care_month.rmm_pro_id = rmmPro.id
@@ -2385,8 +2385,9 @@ WHERE
         $v = $request->input('wmo') ?: date('Y-m-d');
         $statQuery = "$commonStatSQL
 (SELECT COUNT(mstat.id) FROM measurement mstat WHERE mstat.care_month_id = care_month.id AND mstat.created_at::date = '$v' AND mstat.is_cellular_zero IS FALSE) > 0 AND
-  " . (count($conditions) > 0 ? implode(" AND ", $conditions) : '1') . "      
+  " . (count($conditions) > 0 ? implode(" AND ", $conditions) : '1') . "
 ";
+        dd($statQuery);
         $statResult = DB::select($statQuery);
         $stats['withMeasOn'] = $statResult[0]->count;
 
@@ -2394,28 +2395,28 @@ WHERE
         $v = $request->input('nmo') ?: date('Y-m-d');
         $statQuery = "$commonStatSQL
 (SELECT COUNT(mstat.id) FROM measurement mstat WHERE mstat.care_month_id = care_month.id AND mstat.created_at::date = '$v' AND mstat.is_cellular_zero IS FALSE) = 0 AND
-  " . (count($conditions) > 0 ? implode(" AND ", $conditions) : '1') . "      
+  " . (count($conditions) > 0 ? implode(" AND ", $conditions) : '1') . "
 ";
         $statResult = DB::select($statQuery);
         $stats['noMeasOn'] = $statResult[0]->count;
 
         $statQuery = "$commonStatSQL
     care_month.number_of_days_with_remote_measurements >= 16 AND
-      " . (count($conditions) > 0 ? implode(" AND ", $conditions) : '1') . "      
+      " . (count($conditions) > 0 ? implode(" AND ", $conditions) : '1') . "
 ";
         $statResult = DB::select($statQuery);
         $stats['gt16MD'] = $statResult[0]->count;
 
         $statQuery = "$commonStatSQL
     care_month.rm_total_time_in_seconds >= 1200 AND care_month.rm_total_time_in_seconds < 2400 AND
-      " . (count($conditions) > 0 ? implode(" AND ", $conditions) : '1') . "      
+      " . (count($conditions) > 0 ? implode(" AND ", $conditions) : '1') . "
 ";
         $statResult = DB::select($statQuery);
         $stats['gt20M'] = $statResult[0]->count;
 
         $statQuery = "$commonStatSQL
     care_month.rm_total_time_in_seconds >= 2400 AND
-      " . (count($conditions) > 0 ? implode(" AND ", $conditions) : '1') . "      
+      " . (count($conditions) > 0 ? implode(" AND ", $conditions) : '1') . "
 ";
         $statResult = DB::select($statQuery);
         $stats['gt40M'] = $statResult[0]->count;
@@ -2513,15 +2514,15 @@ WHERE
 
         // count query
         $countQuery = "
-SELECT count(*) 
-FROM care_month join client on care_month.client_id = client.id 
+SELECT count(*)
+FROM care_month join client on care_month.client_id = client.id
     left join pro mcpPro on care_month.mcp_pro_id = mcpPro.id
     left join pro rmmPro on care_month.rmm_pro_id = rmmPro.id
     left join pro rmePro on care_month.rme_pro_id = rmePro.id
     left join note mrnote on client.most_recent_completed_mcp_note_id = mrnote.id
     left join appointment nv on nv.id = mrnote.follow_up_appointment_id
 WHERE
-      " . (count($conditions) > 0 ? implode(" AND ", $conditions) : '') . "      
+      " . (count($conditions) > 0 ? implode(" AND ", $conditions) : '') . "
 ";
 
         $countResult = DB::select($countQuery);
@@ -2545,7 +2546,7 @@ WHERE
 
         // main query
         $query = "
-SELECT 
+SELECT
        -- display columns
        (client.name_first || ' ' || client.name_last) as client_name,
        client.age_in_years,
@@ -2603,7 +2604,7 @@ SELECT
        -- nv.raw_date as next_visit_date,
        (SELECT MIN(appt.raw_date) FROM appointment appt WHERE appt.client_id = client.id AND appt.raw_date > NOW()::DATE) as next_visit_date,
        -- TODO vital settings
-       
+
        -- functionality support columns
        client.uid as client_uid,
        care_month.uid as care_month_uid,
@@ -2626,7 +2627,7 @@ SELECT
        care_month.rm_reason_icd2,
        care_month.rm_reason_icd3,
        care_month.rm_reason_icd4
-FROM care_month join client on care_month.client_id = client.id 
+FROM care_month join client on care_month.client_id = client.id
     left join pro mcpPro on care_month.mcp_pro_id = mcpPro.id
     left join pro rmmPro on care_month.rmm_pro_id = rmmPro.id
     left join pro rmePro on care_month.rme_pro_id = rmePro.id
@@ -2638,7 +2639,7 @@ FROM care_month join client on care_month.client_id = client.id
 WHERE
       " . (count($conditions) > 0 ? implode(" AND ", $conditions) : '') . "
       ORDER BY {$orderBy} OFFSET {$offset} LIMIT {$perPage}
-      
+
 ";
 
         $patients = DB::select($query);
@@ -2652,7 +2653,7 @@ WHERE
         $stats['totalPatients'] = $paginator->total();
         $commonStatSQL = "
 SELECT COUNT(*)
-FROM care_month join client on care_month.client_id = client.id 
+FROM care_month join client on care_month.client_id = client.id
     left join pro mcpPro on care_month.mcp_pro_id = mcpPro.id
     left join pro rmmPro on care_month.rmm_pro_id = rmmPro.id
     left join pro rmePro on care_month.rme_pro_id = rmePro.id
@@ -2668,7 +2669,7 @@ WHERE
         $v = $request->input('wmo') ?: date('Y-m-d');
         $statQuery = "$commonStatSQL
 (SELECT COUNT(mstat.id) FROM measurement mstat WHERE mstat.care_month_id = care_month.id AND mstat.created_at::date = '$v' AND mstat.is_cellular_zero IS FALSE) > 0 AND
-  " . (count($conditions) > 0 ? implode(" AND ", $conditions) : '1') . "      
+  " . (count($conditions) > 0 ? implode(" AND ", $conditions) : '1') . "
 ";
         $statResult = DB::select($statQuery);
         $stats['withMeasOn'] = $statResult[0]->count;
@@ -2677,28 +2678,28 @@ WHERE
         $v = $request->input('nmo') ?: date('Y-m-d');
         $statQuery = "$commonStatSQL
 (SELECT COUNT(mstat.id) FROM measurement mstat WHERE mstat.care_month_id = care_month.id AND mstat.created_at::date = '$v' AND mstat.is_cellular_zero IS FALSE) = 0 AND
-  " . (count($conditions) > 0 ? implode(" AND ", $conditions) : '1') . "      
+  " . (count($conditions) > 0 ? implode(" AND ", $conditions) : '1') . "
 ";
         $statResult = DB::select($statQuery);
         $stats['noMeasOn'] = $statResult[0]->count;
 
         $statQuery = "$commonStatSQL
     care_month.number_of_days_with_remote_measurements >= 16 AND
-      " . (count($conditions) > 0 ? implode(" AND ", $conditions) : '1') . "      
+      " . (count($conditions) > 0 ? implode(" AND ", $conditions) : '1') . "
 ";
         $statResult = DB::select($statQuery);
         $stats['gt16MD'] = $statResult[0]->count;
 
         $statQuery = "$commonStatSQL
     care_month.rm_total_time_in_seconds >= 1200 AND care_month.rm_total_time_in_seconds < 2400 AND
-      " . (count($conditions) > 0 ? implode(" AND ", $conditions) : '1') . "      
+      " . (count($conditions) > 0 ? implode(" AND ", $conditions) : '1') . "
 ";
         $statResult = DB::select($statQuery);
         $stats['gt20M'] = $statResult[0]->count;
 
         $statQuery = "$commonStatSQL
     care_month.rm_total_time_in_seconds >= 2400 AND
-      " . (count($conditions) > 0 ? implode(" AND ", $conditions) : '1') . "      
+      " . (count($conditions) > 0 ? implode(" AND ", $conditions) : '1') . "
 ";
         $statResult = DB::select($statQuery);
         $stats['gt40M'] = $statResult[0]->count;
@@ -2708,7 +2709,7 @@ WHERE
 
     public function rpmManagerRow(Request $request, $uid) {
         $query = "
-SELECT 
+SELECT
        -- display columns
        (client.name_first || ' ' || client.name_last) as client_name,
        client.age_in_years,
@@ -2766,7 +2767,7 @@ SELECT
        -- nv.raw_date as next_visit_date,
        (SELECT MIN(appt.raw_date) FROM appointment appt WHERE appt.client_id = client.id AND appt.raw_date > NOW()::DATE) as next_visit_date,
        -- TODO vital settings
-       
+
        -- functionality support columns
        client.uid as client_uid,
        care_month.uid as care_month_uid,
@@ -2789,7 +2790,7 @@ SELECT
        care_month.rm_reason_icd2,
        care_month.rm_reason_icd3,
        care_month.rm_reason_icd4
-FROM care_month join client on care_month.client_id = client.id 
+FROM care_month join client on care_month.client_id = client.id
     left join pro mcpPro on care_month.mcp_pro_id = mcpPro.id
     left join pro rmmPro on care_month.rmm_pro_id = rmmPro.id
     left join pro rmePro on care_month.rme_pro_id = rmePro.id
@@ -2862,12 +2863,12 @@ WHERE
 SELECT COUNT(*)
 FROM claim_line cline
     join claim on cline.claim_id = claim.id
-    join client on claim.client_id = client.id 
+    join client on claim.client_id = client.id
     left join pro on claim.pro_id = pro.id
     left join client_primary_coverage cpc on client.effective_client_primary_coverage_id = cpc.id
     left join payer on cpc.commercial_payer_id = payer.id
 WHERE
-      " . implode(' AND ', $conditions) . "      
+      " . implode(' AND ', $conditions) . "
 ";
 
         $countResult = DB::select($countQuery);
@@ -2875,21 +2876,21 @@ WHERE
 
         $query = "
 SELECT (client.name_first || ' ' || client.name_last) as client_name,
-       client.uid as client_uid, 
+       client.uid as client_uid,
        client.dob,
        client.age_in_years,
        client.is_enrolled_in_rm,
        client.mailing_address_state,
        (pro.name_first || ' ' || pro.name_last) as pro_name,
-       (CASE 
+       (CASE
            WHEN cpc.plan_type LIKE 'COMMERCIAL' THEN payer.name
            ELSE cpc.plan_type
        END) as payer_name,
        cline.date_of_service,
        cline.cpt
-FROM claim_line cline 
+FROM claim_line cline
     join claim on cline.claim_id = claim.id
-    join client on claim.client_id = client.id 
+    join client on claim.client_id = client.id
     left join pro on claim.pro_id = pro.id
     left join client_primary_coverage cpc on client.effective_client_primary_coverage_id = cpc.id
     left join payer on cpc.commercial_payer_id = payer.id
@@ -3002,13 +3003,13 @@ WHERE p.category = 'PROBLEM'
         $query = "
 SELECT p.uid as point_uid,
        (c.name_first || ' ' || c.name_last) as client_name,
-       c.uid as client_uid, 
+       c.uid as client_uid,
        c.dob,
        c.age_in_years,
        c.is_enrolled_in_rm,
        c.mailing_address_state,
        (mcp.name_first || ' ' || mcp.name_last) as mcp_name,
-       (CASE 
+       (CASE
            WHEN cpc.plan_type LIKE 'COMMERCIAL' THEN payer.name
            ELSE cpc.plan_type
        END) as payer_name,
@@ -3026,7 +3027,7 @@ FROM point p
     join pro mcp on c.mcp_pro_id = mcp.id
     left join client_primary_coverage cpc on c.effective_client_primary_coverage_id = cpc.id
     left join payer on cpc.commercial_payer_id = payer.id
-    left join point last_review on p.last_child_review_point_id = last_review.id 
+    left join point last_review on p.last_child_review_point_id = last_review.id
     left join point last_plan on p.last_child_plan_point_id = last_plan.id
 WHERE p.category = 'PROBLEM'
     AND p.is_removed_due_to_entry_error IS NOT TRUE " .
@@ -3093,7 +3094,7 @@ WHERE p.category = 'PROBLEM'
             $query = "
     SELECT client.name_first, client.name_last,
            (client.name_first || ' ' || client.name_last) as client_name,
-           client.uid as client_uid, 
+           client.uid as client_uid,
            client.dob,
            care_month.is_client_enrolled_in_rm,
            client.most_recent_completed_mcp_note_date,
@@ -3112,14 +3113,14 @@ WHERE p.category = 'PROBLEM'
            client.cell_number,
            client.most_recent_cellular_bp_dbp_mm_hg,
            client.most_recent_cellular_bp_sbp_mm_hg,
-           client.most_recent_cellular_bp_measurement_at,      
+           client.most_recent_cellular_bp_measurement_at,
            client.most_recent_cellular_weight_value,
            client.most_recent_cellular_weight_measurement_at,
            GREATEST(client.most_recent_cellular_bp_measurement_at, client.most_recent_cellular_weight_measurement_at) as latest_measurement_at
     FROM care_month join client on care_month.client_id = client.id
     WHERE
           (
-              (care_month.mcp_pro_id = {$performer->pro->id} AND care_month.rm_num_measurements_not_stamped_by_mcp > 0) OR 
+              (care_month.mcp_pro_id = {$performer->pro->id} AND care_month.rm_num_measurements_not_stamped_by_mcp > 0) OR
               (care_month.rmm_pro_id = {$performer->pro->id} AND care_month.rm_num_measurements_not_stamped_by_rmm > 0)
           )
           AND EXTRACT(MONTH from care_month.start_date) = " . ($request->input('m') ?: 'EXTRACT(MONTH from now())') . "
@@ -3192,7 +3193,7 @@ SELECT client.name_first, client.name_last,
        (mcp_pro.name_first || ' ' || mcp_pro.name_last) as mcp_pro_name,
        (rmm_pro.name_first || ' ' || rmm_pro.name_last) as rmm_pro_name,
        client.uid as client_uid,
-       client.chart_number, 
+       client.chart_number,
        client.dob,
        care_month.is_client_enrolled_in_rm,
        client.most_recent_completed_mcp_note_date,
@@ -3224,17 +3225,17 @@ SELECT client.name_first, client.name_last,
        client.cell_number,
        care_month.most_recent_cellular_bp_dbp_mm_hg,
        care_month.most_recent_cellular_bp_sbp_mm_hg,
-       care_month.most_recent_cellular_bp_measurement_at,      
+       care_month.most_recent_cellular_bp_measurement_at,
        care_month.most_recent_cellular_weight_value,
        care_month.most_recent_cellular_weight_measurement_at
 FROM care_month join client on care_month.client_id = client.id join note mrnote on client.most_recent_completed_mcp_note_id = mrnote.id
      left join note mrmnote on mrmnote.id = (
-        select max(n.id) from note n 
-        where 
-              n.client_id = client.id AND 
+        select max(n.id) from note n
+        where
+              n.client_id = client.id AND
               n.is_cancelled = FALSE AND
               (n.is_signed_by_hcp IS NOT NULL AND n.is_signed_by_hcp = TRUE) AND
-              n.effective_dateest::date >= care_month.start_date::date AND 
+              n.effective_dateest::date >= care_month.start_date::date AND
               n.effective_dateest::date < (care_month.start_date::date + INTERVAL '1 month')
      )
     left join pro mcp_pro on care_month.mcp_pro_id = mcp_pro.id
@@ -3871,7 +3872,7 @@ SELECT claim.uid AS uid,
        note.method,
        note.new_or_fu_or_na,
        care_month.uid AS care_month_uid,
-       care_month.start_date as care_month_start__date, 
+       care_month.start_date as care_month_start__date,
        -- claim.status_updated_at,
        (DATE(claim.status_updated_at) || ' ' ||
             LPAD(EXTRACT(hour FROM claim.status_updated_at)::text, 2, '0') || ':' ||
@@ -4482,7 +4483,7 @@ ORDER BY c.name_last, c.name_first
         }
 
         $this->filterMultiQuery($request, $devices, 'client_bdt_device.most_recent_measurement_at', 'date_category', 'date_value_1', 'date_value_2');
-        
+
         // $dateKeyName = $request->get('date_category');
         // $date1 = $request->get('date_value_1');
         // $date2 = $request->get('date_value_2');
@@ -4558,7 +4559,7 @@ ORDER BY c.name_last, c.name_first
                 }
                 break;
         }
-        
+
         $status = $request->get('status');
         if($status){
             if($status === 'ACTIVE') $devices = $devices->where('client_bdt_device.is_active', true);
@@ -4589,7 +4590,7 @@ ORDER BY c.name_last, c.name_first
     public function segmentTemplates(Request $request){
         $segmentTemplates = SegmentTemplate::query();
         $segmentTemplates = $segmentTemplates->orderBy('created_at', 'DESC');
-        
+
         $responseType = $request->get('response_type');
         if($responseType && $responseType == 'json'){
             $segmentTemplates = $segmentTemplates->where('is_active', true)->get();
@@ -4717,10 +4718,10 @@ ORDER BY c.name_last, c.name_first
     public function rmActionReport(Request $request){
 
         $filters = $request->all();
-    
+
         $careMonthStartDate = $request->get('care_month_start_date');
         $careMonthYear = $request->get('care_month_year');
-        
+
         if(!$careMonthStartDate){
             $careMonthStartDate = '2022-01-01';
         }
@@ -4861,11 +4862,11 @@ ORDER BY c.name_last, c.name_first
             cm.has_mcp_interacted_with_client_about_rm,
             cm.claim_suggestion_json,
             cm.is_claim_closed,
-            (CASE 
+            (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 
+            (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,
@@ -4882,7 +4883,7 @@ ORDER BY c.name_last, c.name_first
         ";
 
         $from = "
-            care_month cm 
+            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
@@ -4897,7 +4898,7 @@ ORDER BY c.name_last, c.name_first
         $offset = ($page - 1) * $perPage;
 
         $countSql = "
-        SELECT 
+        SELECT
             COUNT(*)
         FROM
             $from
@@ -4909,7 +4910,7 @@ ORDER BY c.name_last, c.name_first
         $total = $countResult[0]->count;
 
         $sql = "
-        SELECT 
+        SELECT
             $columns
         FROM
             $from
@@ -4938,7 +4939,7 @@ ORDER BY c.name_last, c.name_first
                 "verify_peer"=>false,
                 "verify_peer_name"=>false,
             ),
-        );        
+        );
         $response = @file_get_contents($url, false, stream_context_create($arrContextOptions));
         $response = @json_decode($response);
         $flyerTemplates = [];
@@ -5055,10 +5056,10 @@ ORDER BY c.name_last, c.name_first
             -- n.visit_number IN (1,2) AND
 	    n.is_signed_by_hcp IS TRUE AND
             -- n.is_claim_closed IS NOT TRUE AND
-            n.is_cancelled IS NOT TRUE AND 
+            n.is_cancelled IS NOT TRUE AND
             n.created_at::DATE >= '2022-01-01'::DATE AND
             c.client_engagement_status_category <> 'DUMMY' AND
-            c.name_first NOT ILIKE '%test%' AND 
+            c.name_first NOT ILIKE '%test%' AND
             c.name_last NOT ILIKE '%test%' AND
             n.id IN (SELECT note_id FROM bill WHERE code ILIKE '%treatment%' AND bill.is_cancelled IS NOT TRUE AND note_id IS NOT NULL) AND
             -- n.id NOT IN (SELECT note_id FROM claim WHERE note_id IS NOT NULL) AND
@@ -5135,19 +5136,19 @@ ORDER BY c.name_last, c.name_first
 	    JOIN bill AS b ON (b.note_id = n.id AND b.is_cancelled IS NOT TRUE AND b.code ILIKE '%treatment%')
             ";
         $where = "WHERE
-            
+
 		-- n.visit_number = 2 AND
 		n.is_signed_by_hcp IS TRUE AND
             -- n.effective_dateest < '2022-03-01' AND
-	    
+
 		n.is_claim_closed IS NOT TRUE AND
-            
-	    n.is_cancelled IS NOT TRUE AND 
+
+	    n.is_cancelled IS NOT TRUE AND
             c.client_engagement_status_category <> 'DUMMY' AND
-            c.name_first NOT ILIKE '%test%' AND 
+            c.name_first NOT ILIKE '%test%' AND
             c.name_last NOT ILIKE '%test%' AND
             n.id IN (SELECT note_id FROM bill WHERE code ILIKE '%treatment%' AND bill.is_cancelled IS NOT TRUE AND note_id IS NOT NULL)
-		    -- AND	
+		    -- AND
             -- n.id NOT IN (SELECT note_id FROM claim WHERE note_id IS NOT NULL) AND
             ";
         $filters = [];
@@ -5191,8 +5192,8 @@ ORDER BY c.name_last, c.name_first
             $filters = '';
         }
         // $filters = '';
-        $orderBy = "ORDER BY 
-			-- c.id ASC, 
+        $orderBy = "ORDER BY
+			-- c.id ASC,
 		n.id ASC, n.effective_dateest ASC";
 
         $countQuery = "SELECT count(*) {$from} {$where} {$filters}";
@@ -5237,7 +5238,7 @@ ORDER BY c.name_last, c.name_first
             ";
         $where = "WHERE
             (c.client_engagement_status_category IS NULL OR client_engagement_status_category <> 'DUMMY') AND
-            c.name_first NOT ILIKE '%test%' AND 
+            c.name_first NOT ILIKE '%test%' AND
             c.name_last NOT ILIKE '%test%' AND
             c.created_at::DATE >= '2022-01-01'::DATE
              -- lmcpc.id IS NOT NULL ANd lmcpc.plan_type != 'MEDICARE'
@@ -5407,5 +5408,5 @@ ORDER BY c.name_last, c.name_first
         return view('app.ps.review-requests.list', compact('reviewRequests'));
     }
 
-   
+
 }