McpController.php 20 KB

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