McpController.php 19 KB

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