RdController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\AccountInvite;
  4. use App\Models\Appointment;
  5. use App\Models\AppointmentView;
  6. use App\Models\Bill;
  7. use App\Models\Client;
  8. use App\Models\ClientBDTDevice;
  9. use App\Models\ClientMemo;
  10. use App\Models\ClientProAccess;
  11. use App\Models\ClientReviewRequest;
  12. use App\Models\ClientSMS;
  13. use App\Models\Erx;
  14. use App\Models\IncomingReport;
  15. use App\Models\Note;
  16. use App\Models\SupplyOrder;
  17. use Illuminate\Http\Request;
  18. class RdController extends Controller
  19. {
  20. public function dashboard(Request $request){
  21. $performer = $this->performer();
  22. $pro = $performer->pro;
  23. $performerProID = $performer->pro->id;
  24. $milliseconds = strtotime(date('Y-m-d')) . '000'; //required by the calendar
  25. return view('app.rd.dashboard', compact( 'milliseconds'));
  26. }
  27. public function patients(Request $request)
  28. {
  29. $filters = $request->all();
  30. $pro = $this->performer->pro;
  31. $patients = Client::where('rd_pro_id', $pro->id);
  32. $proAccessClientIDs = ClientProAccess::where('pro_id', $pro->id)->pluck('client_id')->toArray();
  33. $patients = $patients->orWhereIn('id', $proAccessClientIDs);
  34. if ($request->input('name')) {
  35. $name = trim($request->input('name'));
  36. if ($name) {
  37. $patients = $patients->where(function ($q) use ($name) {
  38. $q->where('name_first', 'ILIKE', '%' . $name . '%')
  39. ->orWhere('name_last', 'ILIKE', '%' . $name . '%');
  40. });
  41. }
  42. }
  43. if ($request->input('home_address_state')) {
  44. if($request->input('home_address_state') == 'NONE'){
  45. $patients = $patients->whereNull('mailing_address_state');
  46. }else if($request->input('home_address_state') == 'NOT_MD'){
  47. $patients = $patients->where('mailing_address_state', '<>' , 'MD');
  48. }else{
  49. $patients = $patients->where('mailing_address_state', '=' , $request->input('home_address_state'));
  50. }
  51. }
  52. $this->filterMultiQuery($request, $patients, 'age_in_years', 'age_category', 'age_value_1', 'age_value_2', false);
  53. $this->filterSimpleQuery($request, $patients, 'sex', 'sex');
  54. $this->filterMultiQuery($request, $patients, 'usual_bmi_max', 'bmi_category', 'bmi_value_1', 'bmi_value_2', false);
  55. $this->filterMultiQuery($request, $patients, 'most_recent_completed_mcp_note_date', 'last_visit_category', 'last_visit_value_1', 'last_visit_value_2');
  56. $this->filterMultiQuery($request, $patients, 'next_mcp_appointment_date', 'next_appointment_category', 'next_appointment_value_1', 'next_appointment_value_2');
  57. switch($request->input('status')) {
  58. case 'ACTIVE':
  59. $patients->where('is_active', true)->where('has_mcp_done_onboarding_visit', true);
  60. break;
  61. case 'AWAITING_VISIT':
  62. $patients->where('is_active', true)->where('has_mcp_done_onboarding_visit', false);
  63. break;
  64. case 'INACTIVE':
  65. $patients->where('is_active', '<>', true);
  66. break;
  67. }
  68. $sortBy = $request->input('sort_by') ?: 'name_first';
  69. $sortDir = $request->input('sort_dir') ?: 'ASC';
  70. $patients = $patients->orderByRaw("$sortBy $sortDir NULLS LAST");
  71. $patients = $patients->orderBy('created_at', 'DESC')->paginate(20);
  72. return view('app.rd.patients', compact('patients', 'filters'));
  73. }
  74. public function dashboardPatients(Request $request){
  75. $pro = $this->performer->pro;
  76. $patients = Client::where('rd_pro_id', $pro->id)->paginate(25);
  77. return view('app.rd.dashboard.patients', compact('patients'));
  78. }
  79. public function clientReviewRequests(Request $request){
  80. $hideTitle = $request->get('hideTitle');
  81. $performer = $this->performer();
  82. $pro = $performer->pro;
  83. $reviewRequests = ClientReviewRequest::where('pro_id', $pro->id)->where('is_active', true)->orderBy('created_at', 'DESC')->paginate(50);
  84. return view('app.rd.review-requests.list', compact('reviewRequests', 'hideTitle'));
  85. }
  86. public function patientsAwaitingMcpVisit(Request $request){
  87. $pro = $this->performer->pro;
  88. $records = $pro->patientsAwaitingMcpVisitRecordsAsRd();
  89. return view('app.rd.dashboard.patients_awaiting_mcp_visit', compact('records'));
  90. }
  91. public function patientsWithoutAppointment(Request $request){
  92. $pro = $this->performer->pro;
  93. $records = $pro->patientsWithoutAppointmentRecordsAsRd();
  94. return view('app.rd.dashboard.patients_without_appointment', compact('records'));
  95. }
  96. public function encountersPendingMyReview(Request $request){
  97. $pro = $this->performer->pro;
  98. $records = $pro->encountersPendingMyReviewRecordsAsRd();
  99. return view('app.rd.dashboard.encounters_pending_my_review', compact('records'));
  100. }
  101. public function encountersInProgress(Request $request){
  102. $pro = $this->performer->pro;
  103. $records = $pro->encountersInProgressRecordsAsRd();
  104. return view('app.rd.dashboard.encounters_in_progress', compact('records'));
  105. }
  106. public function appointmentsPendingConfirmation(Request $request){
  107. $pro = $this->performer->pro;
  108. $records = $pro->appointmentsPendingConfirmationRecordsAsRd();
  109. return view('app.rd.dashboard.appointments_pending_confirmation', compact('records'));
  110. }
  111. public function cancelledAppointmentsPendingAck(Request $request){
  112. $pro = $this->performer->pro;
  113. $records = $pro->cancelledAppointmentsPendingAckRecordsAsRd();
  114. return view('app.rd.dashboard.cancelled_appointments_pending_ack', compact('records'));
  115. }
  116. public function reportsPendingAck(Request $request){
  117. $pro = $this->performer->pro;
  118. $records = $pro->reportsPendingAckRecordsAsRd();
  119. return view('app.rd.dashboard.reports_pending_ack', compact('records'));
  120. }
  121. public function supplyOrdersPendingMyAck(Request $request){
  122. $pro = $this->performer->pro;
  123. $records = $pro->supplyOrdersPendingMyAckRecordsAsRd();
  124. return view('app.rd.dashboard.supply_orders_pending_my_ack', compact('records'));
  125. }
  126. public function supplyOrdersPendingHcpApproval(Request $request){
  127. $pro = $this->performer->pro;
  128. $records = $pro->supplyOrdersPendingHcpApprovalRecordsAsRd();
  129. return view('app.rd.dashboard.supply_orders_pending_hcp_approval', compact('records'));
  130. }
  131. public function appointments(Request $request)
  132. {
  133. $pro = $this->performer->pro;
  134. $filters = $request->all();
  135. $appointments = AppointmentView::where('client_rd_pro_id', $pro->id);
  136. $this->filterMultiQuery($request, $appointments, 'raw_date', 'date_category', 'date_value_1', 'date_value_2');
  137. $this->filterSimpleQuery($request, $appointments, 'status', 'status');
  138. $appointments = $appointments->orderBy('end_time', 'DESC')->paginate(20);
  139. return view('app.rd.appointments', compact('appointments', 'filters'));
  140. }
  141. public function clientCcmRmStatus(Request $request){
  142. $pro = $this->performer()->pro;
  143. $filters = $request->all();
  144. $patients = Client::whereNull('shadow_pro_id')->where('rd_pro_id', $pro->id);
  145. if($pro->pro_type !== 'ADMIN') {
  146. if($pro->is_hcp){
  147. $patients = $patients->where('mcp_pro_id', $this->performer()->pro->id);
  148. }
  149. if($pro->is_considered_for_dna){
  150. $patients = $patients->where('default_na_pro_id', $this->performer()->pro->id);
  151. }
  152. }
  153. $this->filterMultiQuery($request, $patients, 'age_in_years', 'age_category', 'age_value_1', 'age_value_2', false);
  154. $this->filterSimpleQuery($request, $patients, 'sex', 'sex');
  155. $this->filterMultiQuery($request, $patients, 'usual_bmi', 'bmi_category', 'bmi_value_1', 'bmi_value_2');
  156. $this->filterMultiQuery($request, $patients, 'most_recent_weight_at', 'last_weighed_in_category', 'last_weighed_in_value_1', 'last_weighed_in_value_2');
  157. $this->filterMultiQuery($request, $patients, 'most_recent_bp_at', 'last_bp_category', 'last_bp_value_1', 'last_bp_value_2');
  158. switch($request->input('status')) {
  159. case 'ACTIVE':
  160. $patients->where('is_active', true)->where('has_mcp_done_onboarding_visit', true);
  161. break;
  162. case 'AWAITING_VISIT':
  163. $patients->where('is_active', true)->where('has_mcp_done_onboarding_visit', false);
  164. break;
  165. case 'INACTIVE':
  166. $patients->where('is_active', '<>', true);
  167. break;
  168. }
  169. if($request->input('is_eligible_for_cm')){
  170. $patients->where('is_eligible_for_cm', '=', $request->input('is_eligible_for_cm'));
  171. }
  172. if($request->input('is_enrolled_in_cm')){
  173. $patients->where('is_enrolled_in_cm', '=', $request->input('is_enrolled_in_cm'));
  174. }
  175. if($request->input('has_cm_setup_been_performed')){
  176. $patients->where('has_cm_setup_been_performed', '=', $request->input('has_cm_setup_been_performed')=='YES'? true : false);
  177. }
  178. if($request->input('is_eligible_for_rm')){
  179. $patients->where('is_eligible_for_rm', '=', $request->input('is_eligible_for_rm'));
  180. }
  181. if($request->input('is_enrolled_in_rm')){ /*-- correct --*/
  182. $patients->where('is_enrolled_in_rm', '=', $request->input('is_enrolled_in_rm')); /*-- correct --*/
  183. }
  184. if($request->input('has_rm_setup_been_performed')){
  185. $patients->where('has_rm_setup_been_performed', '=', $request->input('has_rm_setup_been_performed') =='YES'? true : false);
  186. }
  187. $patients = $patients->orderBy('created_at', 'DESC')->paginate(20);
  188. return view('app.rd.client-ccm-rm-status', compact('patients', 'filters'));
  189. }
  190. public function rpmMatrix(Request $request)
  191. {
  192. $proID = $this->performer()->pro->id;
  193. $isAdmin = $this->performer()->pro->pro_type == 'ADMIN';
  194. $query = Client::whereNull('shadow_pro_id');
  195. $query->where('rd_pro_id', '=', $proID);
  196. $clients = $query->orderByRaw('most_recent_cellular_measurement_at desc nulls last')
  197. ->paginate(50);
  198. return view ('app.rd.rpm-matrix', compact('clients'));
  199. }
  200. public function notes(Request $request)
  201. {
  202. $pro = $this->performer->pro;
  203. $filters = $request->all();
  204. $notes = Note::query();
  205. $notes = $notes->whereHas('client', function($qry)use($pro){
  206. return $qry->where('rd_pro_id', $pro->id);
  207. });
  208. $this->filterMultiQuery($request, $notes, 'effective_time', 'date_category', 'date_value_1', 'date_value_2');
  209. $this->filterSimpleQuery($request, $notes, 'new_or_fu_or_na', 'new_or_fu_or_na');
  210. $notes = $notes->orderBy('created_at', 'DESC')->paginate(20);
  211. return view('app.rd.notes', compact('notes','filters'));
  212. }
  213. public function memos(Request $request){
  214. $filters = $request->all();
  215. $memos = ClientMemo::select('client_memo.*')
  216. ->join('client', 'client.id', '=', 'client_memo.client_id')
  217. ->where('client.rd_pro_id', $this->performer->pro->id);
  218. $this->filterMultiQuery($request, $memos, 'client_memo.created_at', 'date_category', 'date_value_1', 'date_value_2');
  219. $this->filterSimpleQuery($request, $memos, 'category', 'category');
  220. $memos = $memos->orderBy('created_at', 'DESC')->paginate(20);
  221. return view('app.rd.memos', compact('memos', 'filters'));
  222. }
  223. public function bills(Request $request)
  224. {
  225. $pro = $this->performer->pro;
  226. $filters = $request->all();
  227. $bills = Bill::whereHas('client', function($qry)use($pro){
  228. return $qry->where('rd_pro_id', $pro->id);
  229. });
  230. $this->filterMultiQuery($request, $bills, 'created_at', 'date_category', 'date_value_1', 'date_value_2');
  231. $status = $request->get('status');
  232. if($status){
  233. if($status == 'CANCELLED') $bills = $bills->where('is_cancelled', true);
  234. if($status == 'NOT_CANCELLED') $bills = $bills->where('is_cancelled', false);
  235. }
  236. $bills = $bills->orderBy('created_at', 'DESC')->paginate(20);
  237. return view('app.rd.bills', compact('bills', 'filters'));
  238. }
  239. public function erxAndOrders(Request $request)
  240. {
  241. $pro = $this->performer->pro;
  242. $filters = $request->all();
  243. $erxAndOrders = Erx::query();
  244. $erxAndOrders = $erxAndOrders->whereHas('client', function($qry)use($pro){
  245. return $qry->where('rd_pro_id', $pro->id);
  246. });
  247. $this->filterMultiQuery($request, $erxAndOrders, 'created_at', 'date_category', 'date_value_1', 'date_value_2');
  248. $this->filterSimpleQuery($request, $erxAndOrders, 'pro_declared_status', 'status');
  249. $erxAndOrders = $erxAndOrders->orderBy('created_at', 'DESC')->paginate(20);
  250. return view('app.rd.erx_and_orders', compact('erxAndOrders', 'filters'));
  251. }
  252. public function reports(Request $request)
  253. {
  254. $pro = $this->performer->pro;
  255. $filters = $request->all();
  256. $reports = IncomingReport::whereHas('client',function($qry)use($pro){
  257. return $qry->where('rd_pro_id', $pro->id);
  258. });
  259. $this->filterMultiQuery($request, $reports, 'report_date', 'date_category', 'date_value_1', 'date_value_2');
  260. $status = $request->get('status');
  261. if($status){
  262. if($status == 'SIGNED') $reports = $reports->where('has_hcp_pro_signed', true);
  263. if($status == 'NOT_SIGNED') $reports = $reports->where('has_hcp_pro_signed', false);
  264. }
  265. $reports = $reports->orderBy('created_at', 'DESC')->paginate(20);
  266. return view('app.rd.reports', compact('reports', 'filters'));
  267. }
  268. public function supplyOrders(Request $request)
  269. {
  270. $pro = $this->performer->pro;
  271. $filters = $request->all();
  272. $supplyOrders = SupplyOrder::select('supply_order.*')->whereHas('client',function($qry)use($pro){
  273. return $qry->where('rd_pro_id', $pro->id);
  274. });
  275. $this->filterMultiQuery($request, $supplyOrders, 'created_at', 'date_category', 'date_value_1', 'date_value_2');
  276. $status = $request->get('status');
  277. if($status){
  278. if($status == 'CLEARED_FOR_SHIPMENT'){
  279. $supplyOrders = $supplyOrders->where('is_cleared_for_shipment', true);
  280. }elseif($status == 'NOT_CLEARED_FOR_SHIPMENT'){
  281. $supplyOrders = $supplyOrders->where('is_cleared_for_shipment', false);
  282. }elseif($status == 'CANCELLED'){
  283. $supplyOrders = $supplyOrders->where('is_cancelled', true);
  284. }else{
  285. $supplyOrders = $supplyOrders->join('shipment', 'shipment.id', '=', 'supply_order.shipment_id')->where('shipment.status', $status);
  286. }
  287. }
  288. $supplyOrders = $supplyOrders->orderBy('created_at', 'DESC')->paginate(20);
  289. return view('app.rd.supply_orders', compact('supplyOrders', 'filters'));
  290. }
  291. public function clientMessages(Request $request)
  292. {
  293. $filters = $request->all();
  294. $clientMessages = ClientSMS::select('client_sms.*')
  295. ->join('client', 'client.id', '=', 'client_sms.client_id')
  296. ->where('client.rd_pro_id', $this->performer->pro->id);
  297. $this->filterMultiQuery($request, $clientMessages, 'client_sms.created_at', 'date_category', 'date_value_1', 'date_value_2');
  298. $this->filterSimpleQuery($request, $clientMessages, 'sms_status', 'sms_status');
  299. $clientMessages = $clientMessages->orderBy('client_sms.created_at', 'DESC');
  300. $clientMessages = $clientMessages->paginate(20);
  301. return view('app.rd.client_messages', compact('clientMessages', 'filters'));
  302. }
  303. public function patientsAccountsInvites(Request $request){
  304. $filters = $request->all();
  305. $accountInvites = AccountInvite::select('account_invite.*')
  306. ->join('client', 'client.id', '=', 'account_invite.for_client_id');
  307. $accountInvites = $accountInvites->where('client.rd_pro_id', $this->performer->pro->id);
  308. $this->filterMultiQuery($request, $accountInvites, 'account_invite.created_at', 'date_category', 'date_value_1', 'date_value_2');
  309. $this->filterSimpleQuery($request, $accountInvites, 'account_invite.status', 'status');
  310. $accountInvites = $accountInvites->orderBy('created_at', 'DESC')->paginate(20);
  311. return view('app.rd.patients-accounts-invites', compact('accountInvites', 'filters'));
  312. }
  313. public function clientsBdtDevices(Request $request){
  314. $filters = $request->all();
  315. $devices = ClientBDTDevice::select('client_bdt_device.*')
  316. ->join('client', 'client.id', '=', 'client_bdt_device.client_id')
  317. ->where('client.rd_pro_id', $this->performer->pro->id);
  318. $this->filterMultiQuery($request, $devices, 'client_bdt_device.created_at', 'date_category', 'date_value_1', 'date_value_2');
  319. $status = $request->get('status');
  320. if($status){
  321. if($status === 'ACTIVE') $devices = $devices->where('client_bdt_device.is_active', true);
  322. if($status === 'DEACTIVATED') $devices = $devices->where('client_bdt_device.is_active', false);
  323. }
  324. $devices = $devices->orderBy('created_at', 'DESC')->paginate(20);
  325. return view('app.rd.clients_bdt_devices', compact('devices', 'filters'));
  326. }
  327. }