McpController.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Appointment;
  4. use App\Models\BDTDevice;
  5. use App\Models\CareMonth;
  6. use App\Models\Client;
  7. use App\Models\ClientBDTDevice;
  8. use App\Models\ClientInfoLine;
  9. use App\Models\Erx;
  10. use App\Models\Facility;
  11. use App\Models\Handout;
  12. use App\Models\IncomingReport;
  13. use App\Models\MBClaim;
  14. use App\Models\MBPayer;
  15. use App\Models\Measurement;
  16. use App\Models\Note;
  17. use App\Models\NoteTemplate;
  18. use App\Models\Pro;
  19. use App\Models\Product;
  20. use App\Models\ProProAccess;
  21. use App\Models\SectionTemplate;
  22. use App\Models\Shipment;
  23. use App\Models\SupplyOrder;
  24. use App\Models\Ticket;
  25. use Illuminate\Http\Request;
  26. use Illuminate\Support\Facades\DB;
  27. use Illuminate\Support\Facades\File;
  28. use App\Models\Bill;
  29. use App\Models\ClientSMS;
  30. use App\Models\AccountInvite;
  31. use App\Models\ClientMemo;
  32. use Illuminate\Support\Facades\Http;
  33. use PDF;
  34. class McpController extends Controller
  35. {
  36. public function patients(Request $request)
  37. {
  38. $filters = $request->all();
  39. $patients = Client::whereNull('shadow_pro_id');
  40. //TODO: implement in admin controller
  41. if($this->performer->pro->pro_type != 'ADMIN'){
  42. $patients->where('mcp_pro_id', $this->performer->pro->id);
  43. }
  44. // filters
  45. /*
  46. array:18 [▼
  47. "age_category" => "LESS_THAN"
  48. "age_value_1" => "34"
  49. "age_value_2" => null
  50. "sex" => "M"
  51. "bmi_category" => "BETWEEN"
  52. "bmi_value_1" => "20"
  53. "bmi_value_2" => "25"
  54. "last_visit_category" => "LESS_THAN"
  55. "last_visit_value_1" => "2021-10-14"
  56. "last_visit_value_2" => null
  57. "next_appointment_category" => "LESS_THAN"
  58. "next_appointment_value_1" => "2021-10-15"
  59. "status" => "ACTIVE"
  60. "last_weighed_in_category" => "EXACTLY"
  61. "last_weighed_in_value_1" => "2021-10-07"
  62. "last_bp_category" => "BETWEEN"
  63. "last_bp_value_1" => "2021-10-01"
  64. "last_bp_value_2" => "2021-10-31"
  65. ]
  66. */
  67. if ($request->input('name')) {
  68. $name = trim($request->input('name'));
  69. if ($name) {
  70. $patients = $patients->where(function ($q) use ($name) {
  71. $q->where('name_first', 'ILIKE', '%' . $name . '%')
  72. ->orWhere('name_last', 'ILIKE', '%' . $name . '%');
  73. });
  74. }
  75. }
  76. if ($request->input('home_address_state')) {
  77. if($request->input('home_address_state') == 'NONE'){
  78. $patients = $patients->whereNull('mailing_address_state');
  79. }else if($request->input('home_address_state') == 'NOT_MD'){
  80. $patients = $patients->where('mailing_address_state', '<>' , 'MD');
  81. }else{
  82. $patients = $patients->where('mailing_address_state', '=' , $request->input('home_address_state'));
  83. }
  84. }
  85. $this->filterMultiQuery($request, $patients, 'age_in_years', 'age_category', 'age_value_1', 'age_value_2');
  86. $this->filterSimpleQuery($request, $patients, 'sex', 'sex');
  87. $this->filterMultiQuery($request, $patients, 'usual_bmi_max', 'bmi_category', 'bmi_value_1', 'bmi_value_2');
  88. $this->filterMultiQuery($request, $patients, 'most_recent_weight_at', 'last_weighed_in_category', 'last_weighed_in_value_1', 'last_weighed_in_value_2');
  89. $this->filterMultiQuery($request, $patients, 'most_recent_bp_at', 'last_bp_category', 'last_bp_value_1', 'last_bp_value_2');
  90. switch($request->input('status')) {
  91. case 'ACTIVE':
  92. $patients->where('is_active', true)->where('has_mcp_done_onboarding_visit', true);
  93. break;
  94. case 'AWAITING_VISIT':
  95. $patients->where('is_active', true)->where('has_mcp_done_onboarding_visit', false);
  96. break;
  97. case 'INACTIVE':
  98. $patients->where('is_active', '<>', true);
  99. break;
  100. }
  101. $initiative = $request->input('initiative');
  102. if($initiative){
  103. $wildCardedInitiative = '%'.$initiative.'%';
  104. $patients->where('initiative', 'ilike', $wildCardedInitiative);
  105. }
  106. $include_test_records = $request->input('include_test_records');
  107. if(!$include_test_records){
  108. $patients = $patients->where(function ($q) {
  109. $q->whereNull('client_engagement_status_category')
  110. ->orWhere('client_engagement_status_category', '<>', 'DUMMY');
  111. });
  112. }
  113. if ($request->input('chart_number')) {
  114. $patients = $patients->where('chart_number', 'ILIKE' , '%'.$request->input('chart_number').'%');
  115. }
  116. $sortBy = $request->input('sort_by') ?: 'name_first';
  117. $sortDir = $request->input('sort_dir') ?: 'ASC';
  118. $patients = $patients->orderByRaw("$sortBy $sortDir NULLS LAST");
  119. $patients = $patients->orderBy('created_at', 'DESC')->paginate(50);
  120. return view('app.mcp.patients', compact('patients', 'filters'));
  121. }
  122. public function notes(Request $request)
  123. {
  124. $filters = $request->all();
  125. $notes = Note::query();
  126. $notes = $notes->where('hcp_pro_id', $this->performer->pro->id);
  127. $this->filterMultiQuery($request, $notes, 'effective_time', 'date_category', 'date_value_1', 'date_value_2');
  128. $this->filterSimpleQuery($request, $notes, 'new_or_fu_or_na', 'new_or_fu_or_na');
  129. $notes = $notes->orderBy('created_at', 'DESC')->paginate(20);
  130. return view('app.mcp.notes', compact('notes','filters'));
  131. }
  132. public function appointments(Request $request)
  133. {
  134. $filters = $request->all();
  135. $appointments = Appointment::where('pro_id', $this->performer->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.mcp.appointments', compact('appointments', 'filters'));
  140. }
  141. public function bills(Request $request)
  142. {
  143. $filters = $request->all();
  144. $bills = Bill::where('hcp_pro_id', $this->performer->pro->id);
  145. $this->filterMultiQuery($request, $bills, 'created_at', 'date_category', 'date_value_1', 'date_value_2');
  146. $status = $request->get('status');
  147. if($status){
  148. if($status == 'CANCELLED') $bills = $bills->where('is_cancelled', true);
  149. if($status == 'NOT_CANCELLED') $bills = $bills->where('is_cancelled', false);
  150. }
  151. $bills = $bills->orderBy('created_at', 'DESC')->paginate(20);
  152. return view('app.mcp.bills', compact('bills', 'filters'));
  153. }
  154. public function clients_bdt_devices(Request $request){
  155. $filters = $request->all();
  156. $devices = ClientBDTDevice::select('client_bdt_device.*')
  157. ->join('client', 'client.id', '=', 'client_bdt_device.client_id')
  158. ->where('client.mcp_pro_id', $this->performer->pro->id);
  159. $this->filterMultiQuery($request, $devices, 'client_bdt_device.created_at', 'date_category', 'date_value_1', 'date_value_2');
  160. $status = $request->get('status');
  161. if($status){
  162. if($status === 'ACTIVE') $devices = $devices->where('client_bdt_device.is_active', true);
  163. if($status === 'DEACTIVATED') $devices = $devices->where('client_bdt_device.is_active', false);
  164. }
  165. $devices = $devices->orderBy('created_at', 'DESC')->paginate(20);
  166. return view('app.mcp.clients_bdt_devices', compact('devices', 'filters'));
  167. }
  168. public function memos(Request $request){
  169. $filters = $request->all();
  170. $memos = ClientMemo::select('client_memo.*')
  171. ->join('client', 'client.id', '=', 'client_memo.client_id')
  172. ->where('client.mcp_pro_id', $this->performer->pro->id);
  173. $this->filterMultiQuery($request, $memos, 'client_memo.created_at', 'date_category', 'date_value_1', 'date_value_2');
  174. $this->filterSimpleQuery($request, $memos, 'category', 'category');
  175. $memos = $memos->orderBy('created_at', 'DESC')->paginate(20);
  176. return view('app.mcp.memos', compact('memos', 'filters'));
  177. }
  178. public function erx_and_orders(Request $request)
  179. {
  180. $filters = $request->all();
  181. $erxAndOrders = Erx::query();
  182. $erxAndOrders = $erxAndOrders->where('hcp_pro_id', $this->performer->pro->id);
  183. $this->filterMultiQuery($request, $erxAndOrders, 'created_at', 'date_category', 'date_value_1', 'date_value_2');
  184. $this->filterSimpleQuery($request, $erxAndOrders, 'pro_declared_status', 'status');
  185. $erxAndOrders = $erxAndOrders->orderBy('created_at', 'DESC')->paginate(20);
  186. return view('app.mcp.erx_and_orders', compact('erxAndOrders', 'filters'));
  187. }
  188. public function reports(Request $request)
  189. {
  190. $filters = $request->all();
  191. $reports = IncomingReport::where('hcp_pro_id', $this->performer->pro->id);
  192. $this->filterMultiQuery($request, $reports, 'report_date', 'date_category', 'date_value_1', 'date_value_2');
  193. $status = $request->get('status');
  194. if($status){
  195. if($status == 'SIGNED') $reports = $reports->where('has_hcp_pro_signed', true);
  196. if($status == 'NOT_SIGNED') $reports = $reports->where('has_hcp_pro_signed', false);
  197. }
  198. $reports = $reports->orderBy('created_at', 'DESC')->paginate(20);
  199. return view('app.mcp.reports', compact('reports', 'filters'));
  200. }
  201. public function supply_orders(Request $request)
  202. {
  203. $filters = $request->all();
  204. $supplyOrders = SupplyOrder::select('supply_order.*')->where('supply_order.signed_by_pro_id', $this->performer->pro->id);
  205. $this->filterMultiQuery($request, $supplyOrders, 'created_at', 'date_category', 'date_value_1', 'date_value_2');
  206. $status = $request->get('status');
  207. if($status){
  208. if($status == 'CLEARED_FOR_SHIPMENT'){
  209. $supplyOrders = $supplyOrders->where('is_cleared_for_shipment', true);
  210. }elseif($status == 'NOT_CLEARED_FOR_SHIPMENT'){
  211. $supplyOrders = $supplyOrders->where('is_cleared_for_shipment', false);
  212. }elseif($status == 'CANCELLED'){
  213. $supplyOrders = $supplyOrders->where('is_cancelled', true);
  214. }else{
  215. $supplyOrders = $supplyOrders->join('shipment', 'shipment.id', '=', 'supply_order.shipment_id')->where('shipment.status', $status);
  216. }
  217. }
  218. $supplyOrders = $supplyOrders->orderBy('created_at', 'DESC')->paginate(20);
  219. return view('app.mcp.supply_orders', compact('supplyOrders', 'filters'));
  220. }
  221. public function client_messages(Request $request)
  222. {
  223. $filters = $request->all();
  224. $clientMessages = ClientSMS::select('client_sms.*')
  225. ->join('client', 'client.id', '=', 'client_sms.client_id')
  226. ->where('client.mcp_pro_id', $this->performer->pro->id);
  227. $this->filterMultiQuery($request, $clientMessages, 'client_sms.created_at', 'date_category', 'date_value_1', 'date_value_2');
  228. $this->filterSimpleQuery($request, $clientMessages, 'sms_status', 'sms_status');
  229. $clientMessages = $clientMessages->orderBy('client_sms.created_at', 'DESC');
  230. $clientMessages = $clientMessages->paginate(20);
  231. return view('app.mcp.client_messages', compact('clientMessages', 'filters'));
  232. }
  233. public function patients_accounts_invites(Request $request){
  234. $filters = $request->all();
  235. $accountInvites = AccountInvite::select('account_invite.*')
  236. ->join('client', 'client.id', '=', 'account_invite.for_client_id');
  237. $accountInvites = $accountInvites->where('client.mcp_pro_id', $this->performer->pro->id);
  238. $this->filterMultiQuery($request, $accountInvites, 'account_invite.created_at', 'date_category', 'date_value_1', 'date_value_2');
  239. $this->filterSimpleQuery($request, $accountInvites, 'account_invite.status', 'status');
  240. $accountInvites = $accountInvites->orderBy('created_at', 'DESC')->paginate(20);
  241. return view('app.mcp.patients-accounts-invites', compact('accountInvites', 'filters'));
  242. }
  243. public function new_patients_awaiting_visit(Request $request){
  244. $data = [
  245. 'records' => Client::where('mcp_pro_id', $this->performer->pro->id)
  246. ->where('has_mcp_done_onboarding_visit', '!=', 'YES')
  247. ->orderBy('created_at')
  248. ->get()
  249. ];
  250. return view('app.mcp.new_patients_awaiting_visit', $data);
  251. }
  252. public function notes_pending_signature(Request $request){
  253. $data = [
  254. 'records' => Note::where('hcp_pro_id', $this->performer->pro->id)
  255. ->where('is_cancelled', '<>', true)
  256. ->where('is_signed_by_hcp', '<>', true)
  257. ->where('is_core_note', false)
  258. ->orderBy('created_at')
  259. ->get()
  260. ];
  261. return view('app.mcp.notes_pending_signature', $data);
  262. }
  263. public function notes_pending_summary_suggestion(Request $request){
  264. $pro = $this->performer->pro;
  265. $data = [
  266. 'records' => $pro->get_notes_pending_summary_suggestion_as_mcp()
  267. ];
  268. return view('app.mcp.notes_pending_summary_suggestion', $data);
  269. }
  270. public function notes_rejected_summary_suggestion(Request $request){
  271. $pro = $this->performer->pro;
  272. $data = [
  273. 'records' => $pro->get_notes_rejected_summary_suggestion_as_mcp()
  274. ];
  275. return view('app.mcp.notes_rejected_summary_suggestion', $data);
  276. }
  277. public function notes_pending_billing(Request $request){
  278. $data = [
  279. 'records' => Note::where('hcp_pro_id', $this->performer->pro->id)
  280. ->where('is_cancelled', '<>', true)
  281. ->where('is_signed_by_hcp', true)
  282. ->where('is_billing_marked_done', '<>', true)
  283. ->orderBy('created_at')
  284. ->get()
  285. ];
  286. return view('app.mcp.notes_pending_billing', $data);
  287. }
  288. public function bills_pending_signature(Request $request){
  289. $data = [
  290. 'records' => Bill::where('bill_service_type', '<>', 'CARE_MONTH')->where(function ($query) {
  291. $query->where('hcp_pro_id', $this->performer->pro->id)->where('is_signed_by_hcp', false)->where('is_cancelled', false);
  292. })
  293. ->orWhere(function ($query) {
  294. $query->where('cm_pro_id', $this->performer->pro->id)->where('is_signed_by_cm', false)->where('is_cancelled', false);
  295. })->orWhere(function ($query) {
  296. $query->where('rme_pro_id', $this->performer->pro->id)->where('is_signed_by_rme', false)->where('is_cancelled', false);
  297. })->orWhere(function ($query) {
  298. $query->where('rmm_pro_id', $this->performer->pro->id)->where('is_signed_by_rmm', false)->where('is_cancelled', false);
  299. })->orWhere(function ($query) {
  300. $query->where('generic_pro_id', $this->performer->pro->id)->where('is_signed_by_generic_pro', false)->where('is_cancelled', false);
  301. })
  302. ->orderBy('created_at', 'DESC')
  303. ->get()
  304. ];
  305. return view('app.mcp.bills_pending_signature', $data);
  306. }
  307. public function reports_pending_signature(Request $request){
  308. $data = [
  309. 'records' => IncomingReport::where('hcp_pro_id', $this->performer->pro->id)
  310. ->where('has_hcp_pro_signed', '<>', true)
  311. ->where('is_entry_error', '<>', true)
  312. ->orderBy('created_at')
  313. ->get()
  314. ];
  315. return view('app.mcp.reports_pending_signature', $data);
  316. }
  317. public function patients_without_appointments(Request $request){
  318. $patients = $this->performer->pro->get_patients_without_appointment_query()->paginate(20);
  319. return view('app.mcp.patients_without_appointments', compact('patients'));
  320. }
  321. public function patients_overdue_for_visit(Request $request){
  322. $patients = $this->performer->pro->get_patients_overdue_for_visit_query()->paginate(20);
  323. return view('app.mcp.patients_overdue_for_visit', compact('patients'));
  324. }
  325. public function cancelled_appointments_pending_review(Request $request){
  326. $data = [];
  327. return view('app.mcp.cancelled_appointments_pending_review', $data);
  328. }
  329. public function cancelled_bills_pending_review(Request $request){
  330. $data = [
  331. 'records' => Bill::where('hcp_pro_id', $this->performer->pro->id)
  332. ->where('bill_service_type', 'NOTE')
  333. ->where('is_cancelled', true)
  334. ->where('is_cancellation_acknowledged', '<>', true)
  335. ->orderBy('created_at')
  336. ->get()
  337. ];
  338. return view('app.mcp.cancelled_bills_pending_review', $data);
  339. }
  340. public function cancelled_supply_orders_pending_review(Request $request){
  341. $data = [
  342. 'records' => SupplyOrder::where('signed_by_pro_id', $this->performer->pro->id)
  343. ->where('is_cancelled', true)
  344. ->where('is_cancellation_acknowledged', '<>', true)
  345. ->orderBy('created_at')
  346. ->get()
  347. ];
  348. return view('app.mcp.cancelled_supply_orders_pending_review', $data);
  349. }
  350. public function erx_and_orders_pending_signature(Request $request){
  351. $data = [
  352. 'records' => Erx::where('hcp_pro_id', $this->performer->pro->id)
  353. ->where('pro_declared_status', '<>', 'CANCELLED')
  354. ->where('has_hcp_pro_signed', '<>', true)
  355. ->orderBy('created_at')
  356. ->get()
  357. ];
  358. return view('app.mcp.erx_and_orders_pending_signature', $data);
  359. }
  360. public function supply_orders_pending_signature(Request $request){
  361. $data = [
  362. 'records' => SupplyOrder::where('created_by_pro_id', $this->performer->pro->id)
  363. ->whereNull('signed_by_pro_id')
  364. ->where('is_cancelled', '<>', true)
  365. ->orderBy('created_at')
  366. ->get()
  367. ];
  368. return view('app.mcp.supply_orders_pending_signature', $data);
  369. }
  370. public function supply_orders_awaiting_shipment(Request $request){
  371. $data = [
  372. 'records' => SupplyOrder::where('created_by_pro_id', $this->performer->pro->id)
  373. ->where('is_signed_by_pro', true)
  374. ->where('is_cleared_for_shipment', true)
  375. ->whereNull('shipment_id')
  376. ->orderBy('created_at')
  377. ->get()
  378. ];
  379. return view('app.mcp.supply_orders_awaiting_shipment', $data);
  380. }
  381. public function unsigned_incoming_reports(Request $request){
  382. $data = [
  383. 'records' => IncomingReport::where('hcp_pro_id', $this->performer->pro->id)
  384. ->whereRaw('(has_hcp_pro_signed IS NULL OR has_hcp_pro_signed = FALSE)')
  385. ->orderBy('created_at', 'desc')
  386. ->get()
  387. ];
  388. return view('app.mcp.unsigned_incoming_reports', $data);
  389. }
  390. public function patients_awaiting_rpm_interaction(Request $request) {
  391. $cmStartDate = date('Y-m-01');
  392. $query = "
  393. SELECT
  394. client.uid as client_uid,
  395. care_month.uid as care_month_uid,
  396. (client.name_first || ' ' || client.name_last) as client_name,
  397. client.age_in_years,
  398. client.sex,
  399. care_month.start_date,
  400. care_month.number_of_days_with_remote_measurements
  401. FROM
  402. client join care_month on client.id = care_month.client_id
  403. WHERE
  404. care_month.start_date = '{$cmStartDate}'
  405. AND client.is_enrolled_in_rm = 'YES'
  406. AND care_month.has_mcp_interacted_with_client_about_rm IS NOT TRUE
  407. AND care_month.mcp_pro_id = {$this->performer->pro->id}
  408. AND (client.client_engagement_status_category IS NULL OR client.client_engagement_status_category != 'DUMMY')
  409. AND client.name_first NOT ILIKE '%test%'
  410. AND client.name_last NOT ILIKE '%test%'
  411. ORDER BY care_month.number_of_days_with_remote_measurements DESC NULLS LAST
  412. ";
  413. $data = [
  414. 'records' => DB::select($query)
  415. ];
  416. return view('app.mcp.patients_awaiting_rpm_interaction', $data);
  417. }
  418. public function measurements_pending_stamping(Request $request){
  419. $data = [
  420. 'records' => CareMonth::where('mcp_pro_id', $this->performer->pro->id)
  421. ->where('rm_num_measurements_not_stamped_by_mcp', '>', 0)
  422. ->whereNotNull('rm_num_measurements_not_stamped_by_mcp')
  423. ->orderBy('created_at', 'DESC')
  424. ->paginate(15)
  425. ];
  426. return view('app.mcp.measurements_pending_stamping', $data);
  427. }
  428. public function measurements_pending_stamping_in_care_month(Request $request) {
  429. $patient = Client::where('uid', $request->input('patientUid'))->first();
  430. $careMonth = CareMonth::where('uid', $request->input('careMonthUid'))->first();
  431. return view('app.mcp.measurements_pending_stamping_in_care_month', compact('patient', 'careMonth'));
  432. }
  433. public function measurements_mass_stamping(Request $request){
  434. $careMonthsWithMeasurementsPendingStamping = CareMonth::select('id')
  435. ->where('mcp_pro_id', $this->performer->pro->id)
  436. ->where('rm_num_measurements_not_stamped_by_mcp', '>', 0)
  437. ->whereNotNull('rm_num_measurements_not_stamped_by_mcp')
  438. ->orderBy('created_at', 'DESC')
  439. ->get()
  440. ->map(function($_x) {
  441. return $_x->id;
  442. })
  443. ->toArray();
  444. $measurementsPendingStamping = Measurement::whereIn('care_month_id', $careMonthsWithMeasurementsPendingStamping)
  445. ->orderBy('created_at', 'DESC')
  446. ->whereNotNull('ts')
  447. ->whereNotIn('label', ['SBP', 'DBP'])
  448. ->where('is_cellular_zero', '<>', true)
  449. ->where('is_active', true)
  450. ->where('has_been_stamped_by_mcp', false)
  451. ->whereNotNull('client_bdt_measurement_id')
  452. ->paginate(500);
  453. return view('app.mcp.measurements_mass_stamping', compact('measurementsPendingStamping'));
  454. }
  455. }