McpController.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. $patients = $patients->orderBy('created_at', 'DESC')->paginate(50);
  117. return view('app.mcp.patients', compact('patients', 'filters'));
  118. }
  119. public function notes(Request $request)
  120. {
  121. $filters = $request->all();
  122. $notes = Note::query();
  123. $notes = $notes->where('hcp_pro_id', $this->performer->pro->id);
  124. $this->filterMultiQuery($request, $notes, 'effective_time', 'date_category', 'date_value_1', 'date_value_2');
  125. $this->filterSimpleQuery($request, $notes, 'new_or_fu_or_na', 'new_or_fu_or_na');
  126. $notes = $notes->orderBy('created_at', 'DESC')->paginate(20);
  127. return view('app.mcp.notes', compact('notes','filters'));
  128. }
  129. public function appointments(Request $request)
  130. {
  131. $filters = $request->all();
  132. $appointments = Appointment::where('pro_id', $this->performer->pro->id);
  133. $this->filterMultiQuery($request, $appointments, 'raw_date', 'date_category', 'date_value_1', 'date_value_2');
  134. $this->filterSimpleQuery($request, $appointments, 'status', 'status');
  135. $appointments = $appointments->orderBy('end_time', 'DESC')->paginate(20);
  136. return view('app.mcp.appointments', compact('appointments', 'filters'));
  137. }
  138. public function bills(Request $request)
  139. {
  140. $filters = $request->all();
  141. $bills = Bill::where('hcp_pro_id', $this->performer->pro->id);
  142. $this->filterMultiQuery($request, $bills, 'created_at', 'date_category', 'date_value_1', 'date_value_2');
  143. $status = $request->get('status');
  144. if($status){
  145. if($status == 'CANCELLED') $bills = $bills->where('is_cancelled', true);
  146. if($status == 'NOT_CANCELLED') $bills = $bills->where('is_cancelled', false);
  147. }
  148. $bills = $bills->orderBy('created_at', 'DESC')->paginate(20);
  149. return view('app.mcp.bills', compact('bills', 'filters'));
  150. }
  151. public function clients_bdt_devices(Request $request){
  152. $filters = $request->all();
  153. $devices = ClientBDTDevice::select('client_bdt_device.*')
  154. ->join('client', 'client.id', '=', 'client_bdt_device.client_id')
  155. ->where('client.mcp_pro_id', $this->performer->pro->id);
  156. $this->filterMultiQuery($request, $devices, 'client_bdt_device.created_at', 'date_category', 'date_value_1', 'date_value_2');
  157. $status = $request->get('status');
  158. if($status){
  159. if($status === 'ACTIVE') $devices = $devices->where('client_bdt_device.is_active', true);
  160. if($status === 'DEACTIVATED') $devices = $devices->where('client_bdt_device.is_active', false);
  161. }
  162. $devices = $devices->orderBy('created_at', 'DESC')->paginate(20);
  163. return view('app.mcp.clients_bdt_devices', compact('devices', 'filters'));
  164. }
  165. public function memos(Request $request){
  166. $filters = $request->all();
  167. $memos = ClientMemo::select('client_memo.*')
  168. ->join('client', 'client.id', '=', 'client_memo.client_id')
  169. ->where('client.mcp_pro_id', $this->performer->pro->id);
  170. $this->filterMultiQuery($request, $memos, 'client_memo.created_at', 'date_category', 'date_value_1', 'date_value_2');
  171. $this->filterSimpleQuery($request, $memos, 'category', 'category');
  172. $memos = $memos->orderBy('created_at', 'DESC')->paginate(20);
  173. return view('app.mcp.memos', compact('memos', 'filters'));
  174. }
  175. public function erx_and_orders(Request $request)
  176. {
  177. $filters = $request->all();
  178. $erxAndOrders = Erx::query();
  179. $erxAndOrders = $erxAndOrders->where('hcp_pro_id', $this->performer->pro->id);
  180. $this->filterMultiQuery($request, $erxAndOrders, 'created_at', 'date_category', 'date_value_1', 'date_value_2');
  181. $this->filterSimpleQuery($request, $erxAndOrders, 'pro_declared_status', 'status');
  182. $erxAndOrders = $erxAndOrders->orderBy('created_at', 'DESC')->paginate(20);
  183. return view('app.mcp.erx_and_orders', compact('erxAndOrders', 'filters'));
  184. }
  185. public function reports(Request $request)
  186. {
  187. $filters = $request->all();
  188. $reports = IncomingReport::where('hcp_pro_id', $this->performer->pro->id);
  189. $this->filterMultiQuery($request, $reports, 'report_date', 'date_category', 'date_value_1', 'date_value_2');
  190. $status = $request->get('status');
  191. if($status){
  192. if($status == 'SIGNED') $reports = $reports->where('has_hcp_pro_signed', true);
  193. if($status == 'NOT_SIGNED') $reports = $reports->where('has_hcp_pro_signed', false);
  194. }
  195. $reports = $reports->orderBy('created_at', 'DESC')->paginate(20);
  196. return view('app.mcp.reports', compact('reports', 'filters'));
  197. }
  198. public function supply_orders(Request $request)
  199. {
  200. $filters = $request->all();
  201. $supplyOrders = SupplyOrder::select('supply_order.*')->where('supply_order.signed_by_pro_id', $this->performer->pro->id);
  202. $this->filterMultiQuery($request, $supplyOrders, 'created_at', 'date_category', 'date_value_1', 'date_value_2');
  203. $status = $request->get('status');
  204. if($status){
  205. if($status == 'CLEARED_FOR_SHIPMENT'){
  206. $supplyOrders = $supplyOrders->where('is_cleared_for_shipment', true);
  207. }elseif($status == 'NOT_CLEARED_FOR_SHIPMENT'){
  208. $supplyOrders = $supplyOrders->where('is_cleared_for_shipment', false);
  209. }elseif($status == 'CANCELLED'){
  210. $supplyOrders = $supplyOrders->where('is_cancelled', true);
  211. }else{
  212. $supplyOrders = $supplyOrders->join('shipment', 'shipment.id', '=', 'supply_order.shipment_id')->where('shipment.status', $status);
  213. }
  214. }
  215. $supplyOrders = $supplyOrders->orderBy('created_at', 'DESC')->paginate(20);
  216. return view('app.mcp.supply_orders', compact('supplyOrders', 'filters'));
  217. }
  218. public function client_messages(Request $request)
  219. {
  220. $filters = $request->all();
  221. $clientMessages = ClientSMS::select('client_sms.*')
  222. ->join('client', 'client.id', '=', 'client_sms.client_id')
  223. ->where('client.mcp_pro_id', $this->performer->pro->id);
  224. $this->filterMultiQuery($request, $clientMessages, 'client_sms.created_at', 'date_category', 'date_value_1', 'date_value_2');
  225. $this->filterSimpleQuery($request, $clientMessages, 'sms_status', 'sms_status');
  226. $clientMessages = $clientMessages->orderBy('client_sms.created_at', 'DESC');
  227. $clientMessages = $clientMessages->paginate(20);
  228. return view('app.mcp.client_messages', compact('clientMessages', 'filters'));
  229. }
  230. public function patients_accounts_invites(Request $request){
  231. $filters = $request->all();
  232. $accountInvites = AccountInvite::select('account_invite.*')
  233. ->join('client', 'client.id', '=', 'account_invite.for_client_id');
  234. $accountInvites = $accountInvites->where('client.mcp_pro_id', $this->performer->pro->id);
  235. $this->filterMultiQuery($request, $accountInvites, 'account_invite.created_at', 'date_category', 'date_value_1', 'date_value_2');
  236. $this->filterSimpleQuery($request, $accountInvites, 'account_invite.status', 'status');
  237. $accountInvites = $accountInvites->orderBy('created_at', 'DESC')->paginate(20);
  238. return view('app.mcp.patients-accounts-invites', compact('accountInvites', 'filters'));
  239. }
  240. public function new_patients_awaiting_visit(Request $request){
  241. $data = [
  242. 'records' => Client::where('mcp_pro_id', $this->performer->pro->id)
  243. ->where('has_mcp_done_onboarding_visit', '!=', 'YES')
  244. ->orderBy('created_at')
  245. ->get()
  246. ];
  247. return view('app.mcp.new_patients_awaiting_visit', $data);
  248. }
  249. public function notes_pending_signature(Request $request){
  250. $data = [
  251. 'records' => Note::where('hcp_pro_id', $this->performer->pro->id)
  252. ->where('is_cancelled', '<>', true)
  253. ->where('is_signed_by_hcp', '<>', true)
  254. ->where('is_core_note', false)
  255. ->orderBy('created_at')
  256. ->get()
  257. ];
  258. return view('app.mcp.notes_pending_signature', $data);
  259. }
  260. public function notes_pending_summary_suggestion(Request $request){
  261. $pro = $this->performer->pro;
  262. $data = [
  263. 'records' => $pro->get_notes_pending_summary_suggestion_as_mcp()
  264. ];
  265. return view('app.mcp.notes_pending_summary_suggestion', $data);
  266. }
  267. public function notes_rejected_summary_suggestion(Request $request){
  268. $pro = $this->performer->pro;
  269. $data = [
  270. 'records' => $pro->get_notes_rejected_summary_suggestion_as_mcp()
  271. ];
  272. return view('app.mcp.notes_rejected_summary_suggestion', $data);
  273. }
  274. public function notes_pending_billing(Request $request){
  275. $data = [
  276. 'records' => Note::where('hcp_pro_id', $this->performer->pro->id)
  277. ->where('is_cancelled', '<>', true)
  278. ->where('is_signed_by_hcp', true)
  279. ->where('is_billing_marked_done', '<>', true)
  280. ->orderBy('created_at')
  281. ->get()
  282. ];
  283. return view('app.mcp.notes_pending_billing', $data);
  284. }
  285. public function bills_pending_signature(Request $request){
  286. $data = [
  287. 'records' => Bill::where('bill_service_type', '<>', 'CARE_MONTH')->where(function ($query) {
  288. $query->where('hcp_pro_id', $this->performer->pro->id)->where('is_signed_by_hcp', false)->where('is_cancelled', false);
  289. })
  290. ->orWhere(function ($query) {
  291. $query->where('cm_pro_id', $this->performer->pro->id)->where('is_signed_by_cm', false)->where('is_cancelled', false);
  292. })->orWhere(function ($query) {
  293. $query->where('rme_pro_id', $this->performer->pro->id)->where('is_signed_by_rme', false)->where('is_cancelled', false);
  294. })->orWhere(function ($query) {
  295. $query->where('rmm_pro_id', $this->performer->pro->id)->where('is_signed_by_rmm', false)->where('is_cancelled', false);
  296. })->orWhere(function ($query) {
  297. $query->where('generic_pro_id', $this->performer->pro->id)->where('is_signed_by_generic_pro', false)->where('is_cancelled', false);
  298. })
  299. ->orderBy('created_at', 'DESC')
  300. ->get()
  301. ];
  302. return view('app.mcp.bills_pending_signature', $data);
  303. }
  304. public function reports_pending_signature(Request $request){
  305. $data = [
  306. 'records' => IncomingReport::where('hcp_pro_id', $this->performer->pro->id)
  307. ->where('has_hcp_pro_signed', '<>', true)
  308. ->where('is_entry_error', '<>', true)
  309. ->orderBy('created_at')
  310. ->get()
  311. ];
  312. return view('app.mcp.reports_pending_signature', $data);
  313. }
  314. public function patients_without_appointments(Request $request){
  315. $patients = $this->performer->pro->get_patients_without_appointment_query()->paginate(20);
  316. return view('app.mcp.patients_without_appointments', compact('patients'));
  317. }
  318. public function patients_overdue_for_visit(Request $request){
  319. $patients = $this->performer->pro->get_patients_overdue_for_visit_query()->paginate(20);
  320. return view('app.mcp.patients_overdue_for_visit', compact('patients'));
  321. }
  322. public function cancelled_appointments_pending_review(Request $request){
  323. $data = [];
  324. return view('app.mcp.cancelled_appointments_pending_review', $data);
  325. }
  326. public function cancelled_bills_pending_review(Request $request){
  327. $data = [
  328. 'records' => Bill::where('hcp_pro_id', $this->performer->pro->id)
  329. ->where('bill_service_type', 'NOTE')
  330. ->where('is_cancelled', true)
  331. ->where('is_cancellation_acknowledged', '<>', true)
  332. ->orderBy('created_at')
  333. ->get()
  334. ];
  335. return view('app.mcp.cancelled_bills_pending_review', $data);
  336. }
  337. public function cancelled_supply_orders_pending_review(Request $request){
  338. $data = [
  339. 'records' => SupplyOrder::where('signed_by_pro_id', $this->performer->pro->id)
  340. ->where('is_cancelled', true)
  341. ->where('is_cancellation_acknowledged', '<>', true)
  342. ->orderBy('created_at')
  343. ->get()
  344. ];
  345. return view('app.mcp.cancelled_supply_orders_pending_review', $data);
  346. }
  347. public function erx_and_orders_pending_signature(Request $request){
  348. $data = [
  349. 'records' => Erx::where('hcp_pro_id', $this->performer->pro->id)
  350. ->where('pro_declared_status', '<>', 'CANCELLED')
  351. ->where('has_hcp_pro_signed', '<>', true)
  352. ->orderBy('created_at')
  353. ->get()
  354. ];
  355. return view('app.mcp.erx_and_orders_pending_signature', $data);
  356. }
  357. public function supply_orders_pending_signature(Request $request){
  358. $data = [
  359. 'records' => SupplyOrder::where('created_by_pro_id', $this->performer->pro->id)
  360. ->whereNull('signed_by_pro_id')
  361. ->where('is_cancelled', '<>', true)
  362. ->orderBy('created_at')
  363. ->get()
  364. ];
  365. return view('app.mcp.supply_orders_pending_signature', $data);
  366. }
  367. public function supply_orders_awaiting_shipment(Request $request){
  368. $data = [
  369. 'records' => SupplyOrder::where('created_by_pro_id', $this->performer->pro->id)
  370. ->where('is_signed_by_pro', true)
  371. ->where('is_cleared_for_shipment', true)
  372. ->whereNull('shipment_id')
  373. ->orderBy('created_at')
  374. ->get()
  375. ];
  376. return view('app.mcp.supply_orders_awaiting_shipment', $data);
  377. }
  378. public function unsigned_incoming_reports(Request $request){
  379. $data = [
  380. 'records' => IncomingReport::where('hcp_pro_id', $this->performer->pro->id)
  381. ->whereRaw('(has_hcp_pro_signed IS NULL OR has_hcp_pro_signed = FALSE)')
  382. ->orderBy('created_at', 'desc')
  383. ->get()
  384. ];
  385. return view('app.mcp.unsigned_incoming_reports', $data);
  386. }
  387. public function measurements_pending_stamping(Request $request){
  388. $data = [
  389. 'records' => CareMonth::where('mcp_pro_id', $this->performer->pro->id)
  390. ->where('rm_num_measurements_not_stamped_by_mcp', '>', 0)
  391. ->whereNotNull('rm_num_measurements_not_stamped_by_mcp')
  392. ->orderBy('created_at', 'DESC')
  393. ->paginate(15)
  394. ];
  395. return view('app.mcp.measurements_pending_stamping', $data);
  396. }
  397. public function measurements_pending_stamping_in_care_month(Request $request) {
  398. $patient = Client::where('uid', $request->input('patientUid'))->first();
  399. $careMonth = CareMonth::where('uid', $request->input('careMonthUid'))->first();
  400. return view('app.mcp.measurements_pending_stamping_in_care_month', compact('patient', 'careMonth'));
  401. }
  402. public function measurements_mass_stamping(Request $request){
  403. $careMonthsWithMeasurementsPendingStamping = CareMonth::select('id')
  404. ->where('mcp_pro_id', $this->performer->pro->id)
  405. ->where('rm_num_measurements_not_stamped_by_mcp', '>', 0)
  406. ->whereNotNull('rm_num_measurements_not_stamped_by_mcp')
  407. ->orderBy('created_at', 'DESC')
  408. ->get()
  409. ->map(function($_x) {
  410. return $_x->id;
  411. })
  412. ->toArray();
  413. $measurementsPendingStamping = Measurement::whereIn('care_month_id', $careMonthsWithMeasurementsPendingStamping)
  414. ->orderBy('created_at', 'DESC')
  415. ->whereNotNull('ts')
  416. ->whereNotIn('label', ['SBP', 'DBP'])
  417. ->where('is_cellular_zero', '<>', true)
  418. ->where('is_active', true)
  419. ->where('has_been_stamped_by_mcp', false)
  420. ->whereNotNull('client_bdt_measurement_id')
  421. ->paginate(500);
  422. return view('app.mcp.measurements_mass_stamping', compact('measurementsPendingStamping'));
  423. }
  424. }