|
@@ -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'));
|
|
|
|
|
|
}
|
|
|
|