PatientController.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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\Note;
  16. use App\Models\NoteTemplate;
  17. use App\Models\Pro;
  18. use App\Models\Product;
  19. use App\Models\ProProAccess;
  20. use App\Models\SectionTemplate;
  21. use App\Models\Shipment;
  22. use App\Models\SupplyOrder;
  23. use App\Models\Ticket;
  24. use Illuminate\Http\Request;
  25. use Illuminate\Support\Facades\DB;
  26. use Illuminate\Support\Facades\File;
  27. use Illuminate\Support\Facades\Http;
  28. use PDF;
  29. class PatientController extends Controller
  30. {
  31. public function claimsResolver(Request $request, Client $patient)
  32. {
  33. $notes = $patient->notesAscending;
  34. $hcpSignedNotesCount = 0;
  35. foreach($notes as $note){
  36. if($note->is_signed_by_hcp){
  37. $hcpSignedNotesCount += 1;
  38. }
  39. }
  40. $data = [
  41. 'dog' => 'bark',
  42. 'patient' => $patient,
  43. 'hcpSignedNotesCount' => $hcpSignedNotesCount
  44. ];
  45. return view('app.patient.claims-resolver', $data);
  46. }
  47. public function dashboard(Request $request, Client $patient )
  48. {
  49. $mcpPros = Pro::where('is_enrolled_as_mcp', true)->get();
  50. $facilities = []; // Facility::where('is_active', true)->get();
  51. // get assigned devices
  52. $assignedDeviceIDs = DB::select(DB::raw("SELECT device_id from client_bdt_device where is_active = true"));
  53. $assignedDeviceIDs = array_map(function($_x) {
  54. return $_x->device_id;
  55. }, $assignedDeviceIDs);
  56. // get all except assigned ones
  57. $devices = BDTDevice::where('is_active', true)
  58. ->whereNotIn('id', $assignedDeviceIDs)
  59. ->orderBy('imei', 'asc')
  60. ->get();
  61. $assignedDeviceIDs = null;
  62. unset($assignedDeviceIDs);
  63. $dxInfoLines = ClientInfoLine::where('client_id', $patient->id)
  64. ->where('category', 'dx')
  65. ->where('is_removed', false)
  66. ->orderBy('content_text', 'asc')
  67. ->get();
  68. return view('app.patient.dashboard',
  69. compact('patient', 'facilities', 'devices', 'dxInfoLines'));
  70. }
  71. public function actionItems(Request $request, Client $patient )
  72. {
  73. $facilities = []; // Facility::where('is_active', true)->get();
  74. return view('app.patient.action-items', compact('patient', 'facilities'));
  75. }
  76. public function actionItemsErx(Request $request, Client $patient, $filter = 'open')
  77. {
  78. $allPros = Pro::all();
  79. $facilities = []; // Facility::where('is_active', true)->get();
  80. return view('app.patient.action-items-erx', compact('patient', 'facilities', 'filter', 'allPros'));
  81. }
  82. public function actionItemsLab(Request $request, Client $patient, $filter = 'open')
  83. {
  84. $allPros = Pro::all();
  85. $facilities = []; // Facility::where('is_active', true)->get();
  86. return view('app.patient.action-items-lab', compact('patient', 'facilities', 'filter', 'allPros'));
  87. }
  88. public function actionItemsImaging(Request $request, Client $patient, $filter = 'open')
  89. {
  90. $allPros = Pro::all();
  91. $facilities = []; // Facility::where('is_active', true)->get();
  92. return view('app.patient.action-items-imaging', compact('patient', 'facilities', 'filter', 'allPros'));
  93. }
  94. public function actionItemsEquipment(Request $request, Client $patient, $filter = 'open')
  95. {
  96. $allPros = Pro::all();
  97. $facilities = []; // Facility::where('is_active', true)->get();
  98. return view('app.patient.action-items-equipment', compact('patient', 'facilities', 'filter', 'allPros'));
  99. }
  100. public function actionItemsOther(Request $request, Client $patient, $filter = 'open')
  101. {
  102. $allPros = Pro::all();
  103. $facilities = []; // Facility::where('is_active', true)->get();
  104. return view('app.patient.action-items-other', compact('patient', 'facilities', 'filter', 'allPros'));
  105. }
  106. public function actionItemsErxSingle(Request $request, Client $patient, Ticket $ticket) {
  107. $allPros = Pro::all();
  108. $facilities = []; // Facility::where('is_active', true)->get();
  109. return view('app.patient.action-items-erx-single', compact('patient', 'facilities', 'allPros', 'ticket'));
  110. }
  111. public function actionItemsLabSingle(Request $request, Client $patient, Ticket $ticket) {
  112. $allPros = Pro::all();
  113. $facilities = []; // Facility::where('is_active', true)->get();
  114. return view('app.patient.action-items-lab-single', compact('patient', 'facilities', 'allPros', 'ticket'));
  115. }
  116. public function actionItemsImagingSingle(Request $request, Client $patient, Ticket $ticket) {
  117. $allPros = Pro::all();
  118. $facilities = []; // Facility::where('is_active', true)->get();
  119. return view('app.patient.action-items-imaging-single', compact('patient', 'facilities', 'allPros', 'ticket'));
  120. }
  121. public function actionItemsEquipmentSingle(Request $request, Client $patient, Ticket $ticket) {
  122. $allPros = Pro::all();
  123. $facilities = []; // Facility::where('is_active', true)->get();
  124. return view('app.patient.action-items-equipment-single', compact('patient', 'facilities', 'allPros', 'ticket'));
  125. }
  126. public function actionItemsOtherSingle(Request $request, Client $patient, Ticket $ticket) {
  127. $allPros = Pro::all();
  128. $facilities = []; // Facility::where('is_active', true)->get();
  129. return view('app.patient.action-items-other-single', compact('patient', 'facilities', 'allPros', 'ticket'));
  130. }
  131. public function intake(Request $request, Client $patient )
  132. {
  133. $files = File::allFiles(resource_path('views/app/intake-templates'));
  134. $templates = [];
  135. foreach ($files as $file) {
  136. $templates[] = str_replace(".blade.php", "", $file->getFilename());
  137. }
  138. return view('app.patient.intake', compact('patient', 'templates'));
  139. }
  140. public function carePlan(Request $request, Client $patient )
  141. {
  142. return view('app.patient.care-plan', compact('patient'));
  143. }
  144. public function medications(Request $request, Client $patient )
  145. {
  146. $infoLines = ClientInfoLine::where('client_id', $patient->id)
  147. ->where('category', 'rx')
  148. ->where('is_removed', false)
  149. ->orderBy('content_text', 'asc')
  150. ->get();
  151. return view('app.patient.medications', compact('patient', 'infoLines'));
  152. }
  153. public function dxAndFocusAreas(Request $request, Client $patient )
  154. {
  155. $dxInfoLines = ClientInfoLine::where('client_id', $patient->id)
  156. ->where('category', 'dx')
  157. ->where('is_removed', false)
  158. ->orderBy('content_text', 'asc')
  159. ->get();
  160. return view('app.patient.dx-and-focus-areas', compact('patient', 'dxInfoLines'));
  161. }
  162. public function careTeam(Request $request, Client $patient )
  163. {
  164. $infoLines = ClientInfoLine::where('client_id', $patient->id)
  165. ->where('category', 'care_team')
  166. ->where('is_removed', false)
  167. ->get();
  168. return view('app.patient.care-team', compact('patient', 'infoLines'));
  169. }
  170. public function devices(Request $request, Client $patient )
  171. {
  172. // get assigned devices
  173. $assignedDeviceIDs = DB::select(DB::raw("SELECT device_id from client_bdt_device where is_active = true"));
  174. $assignedDeviceIDs = array_map(function($_x) {
  175. return $_x->device_id;
  176. }, $assignedDeviceIDs);
  177. // get all except assigned ones
  178. $devices = BDTDevice::where('is_active', true)
  179. ->whereNotIn('id', $assignedDeviceIDs)
  180. ->orderBy('imei', 'asc')
  181. ->get();
  182. $assignedDeviceIDs = null;
  183. unset($assignedDeviceIDs);
  184. return view('app.patient.devices', compact('patient', 'devices'));
  185. }
  186. public function measurements(Request $request, Client $patient )
  187. {
  188. return view('app.patient.measurements', compact('patient'));
  189. }
  190. public function labsAndStudies(Request $request, Client $patient )
  191. {
  192. return view('app.patient.labs-and-studies', compact('patient'));
  193. }
  194. public function history(Request $request, Client $patient )
  195. {
  196. $infoLines = ClientInfoLine::where('client_id', $patient->id)
  197. ->where('category', 'LIKE', 'history_%')
  198. ->where('is_removed', false)
  199. ->get();
  200. return view('app.patient.history', compact('patient', 'infoLines'));
  201. }
  202. public function memos(Request $request, Client $patient )
  203. {
  204. return view('app.patient.memos', compact('patient'));
  205. }
  206. public function sms(Request $request, Client $patient )
  207. {
  208. return view('app.patient.sms', compact('patient'));
  209. }
  210. public function smsNumbers(Request $request, Client $patient )
  211. {
  212. return view('app.patient.sms-numbers', compact('patient'));
  213. }
  214. public function immunizations(Request $request, Client $patient )
  215. {
  216. return view('app.patient.immunizations', compact('patient'));
  217. }
  218. public function allergies(Request $request, Client $patient )
  219. {
  220. $infoLines = ClientInfoLine::where('client_id', $patient->id)
  221. ->where('category', 'allergy')
  222. ->where('is_removed', false)
  223. ->get();
  224. return view('app.patient.allergies', compact('patient', 'infoLines'));
  225. }
  226. public function notes(Request $request, Client $patient, $filter = 'active')
  227. {
  228. $pros = $this->pros;
  229. return view('app.patient.notes', compact('patient','pros', 'filter'));
  230. }
  231. public function genericBills(Request $request, Client $patient)
  232. {
  233. return view('app.patient.generic-bills', compact('patient'));
  234. }
  235. public function rmSetup(Request $request, Client $patient)
  236. {
  237. return view('app.patient.rm-setup', compact('patient'));
  238. }
  239. public function sections(Request $request, Client $patient )
  240. {
  241. $pros = $this->pros;
  242. $sections = $patient->sections;
  243. $allSections = SectionTemplate::where('is_active', true)->get();
  244. foreach ($allSections as $section) {
  245. $section->used = false;
  246. foreach ($sections as $section) {
  247. if ($section->sectionTemplate->id === $section->id) {
  248. $section->used = true;
  249. $section->section_uid = $section->uid;
  250. break;
  251. }
  252. }
  253. }
  254. return view('app.patient.sections', compact('patient', 'pros', 'allSections'));
  255. }
  256. public function handouts(Request $request, Client $patient )
  257. {
  258. $handouts = Handout::where('is_active', true)->get();
  259. return view('app.patient.handouts', compact('patient', 'handouts'));
  260. }
  261. public function settings(Request $request, Client $patient )
  262. {
  263. return view('app.patient.settings', compact('patient'));
  264. }
  265. public function smsReminders(Request $request, Client $patient )
  266. {
  267. return view('app.patient.sms-reminders', compact('patient'));
  268. }
  269. public function measurementConfirmationNumbers(Request $request, Client $patient )
  270. {
  271. return view('app.patient.measurement-confirmation-numbers', compact('patient'));
  272. }
  273. public function pros(Request $request, Client $patient )
  274. {
  275. return view('app.patient.pros', compact('patient'));
  276. }
  277. public function account(Request $request, Client $patient )
  278. {
  279. return view('app.patient.account', compact('patient'));
  280. }
  281. public function careChecklist(Request $request, Client $patient )
  282. {
  283. return view('app.patient.care-checklist', compact('patient'));
  284. }
  285. public function documents(Request $request, Client $patient )
  286. {
  287. return view('app.patient.documents', compact('patient'));
  288. }
  289. public function incomingReports(Request $request, Client $patient, IncomingReport $currentReport = null)
  290. {
  291. return view('app.patient.incoming-reports', compact('patient', 'currentReport'));
  292. }
  293. public function education(Request $request, Client $patient )
  294. {
  295. return view('app.patient.education', compact('patient'));
  296. }
  297. public function messaging(Request $request, Client $patient )
  298. {
  299. return view('app.patient.messaging', compact('patient'));
  300. }
  301. public function duplicate(Request $request, Client $patient )
  302. {
  303. return view('app.patient.duplicate', compact('patient'));
  304. }
  305. public function careMonths(Request $request, Client $patient )
  306. {
  307. $careMonths = CareMonth::where('client_id', $patient->id)->orderBy('start_date', 'desc')->get();
  308. $notes = Note::where('is_cancelled', false)->get();
  309. return view('app.patient.care-months', compact('patient', 'careMonths', 'notes'));
  310. }
  311. public function presence(Request $request, Client $patient )
  312. {
  313. return json_encode([
  314. "online" => $patient->is_online
  315. ]);
  316. }
  317. public function embedSection(Request $request, Client $patient, $section, $selectable) {
  318. return view('app.patient.partials.' . $section, compact('patient', 'selectable'));
  319. }
  320. public function calendar(Request $request, Client $patient, Appointment $currentAppointment) {
  321. $pros = [];
  322. if($this->pro && $this->pro->pro_type != 'ADMIN') {
  323. $accessiblePros = ProProAccess::where('owner_pro_id', $this->pro->id)->get();
  324. $accessibleProIds = [];
  325. foreach($accessiblePros as $accessiblePro){
  326. $accessibleProIds[] = $accessiblePro->id;
  327. }
  328. $accessibleProIds[] = $this->pro->id;
  329. // for dna, add pros accessible via pro teams
  330. if($this->performer->pro->isDefaultNA()) {
  331. $teams = $this->performer->pro->teamsWhereAssistant;
  332. foreach ($teams as $team) {
  333. if(!in_array($team->mcp_pro_id, $accessibleProIds)) {
  334. $accessibleProIds[] = $team->mcp_pro_id;
  335. }
  336. }
  337. }
  338. $pros = Pro::whereIn('id', $accessibleProIds)->get();
  339. }
  340. return view('app.patient.appointment-calendar', compact('pros', 'patient', 'currentAppointment'));
  341. }
  342. public function programs(Request $request, Client $patient, $filter = '') {
  343. $pros = $this->pros;
  344. return view('app.patient.programs', compact('patient', 'pros', 'filter'));
  345. }
  346. public function flowsheets(Request $request, Client $patient, $filter = '') {
  347. $pros = $this->pros;
  348. return view('app.patient.flowsheets', compact('patient', 'pros', 'filter'));
  349. }
  350. public function vitalsSettings(Request $request, Client $patient) {
  351. return view('app.patient.vitals-settings', compact('patient'));
  352. }
  353. public function vitalsGraph(Request $request, Client $patient, $filter = '') {
  354. $pros = $this->pros;
  355. return view('app.patient.vitals-graph', compact('patient', 'pros', 'filter'));
  356. }
  357. public function tickets(Request $request, Client $patient, $type = '', String $currentTicket = '') {
  358. $pros = $this->pros;
  359. $allPros = Pro::all();
  360. $qlTicket = $currentTicket;
  361. if(!!$currentTicket) {
  362. $qlTicket = Ticket::where('uid', $currentTicket)->first();
  363. if($qlTicket) {
  364. $currentTicket = $qlTicket;
  365. }
  366. }
  367. return view('app.patient.tickets', compact('patient', 'pros', 'allPros', 'type', 'currentTicket'));
  368. }
  369. public function prescriptions(Request $request, Client $patient, String $type = '', String $currentErx = '') {
  370. if(!!$currentErx) {
  371. $currentErx = Erx::where('uid', $currentErx)->first();
  372. }
  373. return view('app.patient.prescriptions.index', compact('patient', 'type', 'currentErx'));
  374. }
  375. public function prescriptionsPopup(Request $request, Client $patient, String $type = '', String $currentErx = '') {
  376. if(!!$currentErx) {
  377. $currentErx = Erx::where('uid', $currentErx)->first();
  378. }
  379. $note = null;
  380. if($request->input('noteUid')) {
  381. $note = Note::where('uid', $request->input('noteUid'))->first();
  382. }
  383. return view('app.patient.prescriptions-popup.list-popup', compact('patient', 'type', 'currentErx', 'note'));
  384. }
  385. public function prescriptionsList(Request $request, Client $patient, String $type = '', String $currentErx = '') {
  386. if(!!$currentErx) {
  387. $currentErx = Erx::where('uid', $currentErx)->first();
  388. }
  389. $note = null;
  390. if($request->input('noteUid')) {
  391. $note = Note::where('uid', $request->input('noteUid'))->first();
  392. }
  393. return view('app.patient.prescriptions.list', compact('patient', 'type', 'currentErx', 'note'));
  394. }
  395. public function downloadPrescriptionAsPdf(Request $request, Erx $prescription){
  396. if($request->input('html')) {
  397. return view('app.patient.prescriptions.pdf.pdf-preview', compact('prescription'));
  398. }
  399. else {
  400. $pdf = PDF::loadView('app.patient.prescriptions.pdf.pdf-preview', compact('prescription'));
  401. return $pdf->download($prescription->created_at .'_' . 'erx.pdf');
  402. }
  403. }
  404. public function transmitPrescription(Request $request, Erx $prescription){
  405. // re-generate pdf with cover sheet as first page and save it to FS
  406. $filePath = config('app.temp_dir') . "/{$prescription->uid}.pdf";
  407. $pdf = PDF::loadView('app.patient.prescriptions.pdf.pdf-preview-with-cover-sheet', compact('prescription'));
  408. $pdf->save($filePath);
  409. // send it along with the rest of the params to /api/erx/transmit [multi-part POST]
  410. $url = config('stag.backendUrl') . '/erx/transmit';
  411. $params = [
  412. "uid" => $request->input('uid'),
  413. "toWho" => $request->input('toWho'),
  414. "toEmail" => $request->input('toEmail'),
  415. "toFaxNumber" => $request->input('toFaxNumber'),
  416. "toFaxNumberAttentionLine" => $request->input('toFaxNumberAttentionLine'),
  417. "toFaxNumberCoverSheetMemo" => $request->input('toFaxNumberCoverSheetMemo'),
  418. ];
  419. if($request->input('copyToPatient')) {
  420. $params["copyToPatientFaxNumber"] = $request->input('copyToPatientFaxNumber');
  421. $params["copyToPatientEmail"] = $request->input('copyToPatientEmail');
  422. }
  423. $pdf = fopen($filePath, 'r');
  424. $response = Http
  425. ::attach('pdfSystemFile', $filePath, "{$prescription->uid}.pdf")
  426. ->withHeaders(['sessionKey' => $request->cookie('sessionKey')])
  427. ->post($url, $params)
  428. ->json();
  429. return $response;
  430. }
  431. public function supplyOrders(Request $request, Client $patient, SupplyOrder $supplyOrder = null)
  432. {
  433. $products = Product::where('is_active', true)->orderBy('created_at', 'desc')->get();
  434. return view('app.patient.supply-orders', compact('patient', 'supplyOrder', 'products'));
  435. }
  436. public function shipments(Request $request, Client $patient, Shipment $shipment = null)
  437. {
  438. return view('app.patient.shipments', compact('patient', 'shipment'));
  439. }
  440. public function appointments(Request $request, Client $patient, $forPro = 'all', $status = 'all') {
  441. $pros = $this->pros;
  442. $appointments = $patient->appointmentsForProByStatus('all', 'ALL');
  443. $appointmentProIDs = $appointments->map(function($_item) {
  444. return $_item->pro_id;
  445. });
  446. $appointmentPros = Pro::whereIn('id', $appointmentProIDs)->get();
  447. $appointments = $patient->appointmentsForProByStatus($forPro, strtoupper($status));
  448. return view('app.patient.appointments',
  449. compact('patient', 'pros', 'appointments', 'appointmentPros', 'forPro', 'status'));
  450. }
  451. public function mcpRequests(Request $request, Client $patient) {
  452. return view('app.patient.mcp-requests', compact('patient'));
  453. }
  454. public function eligibleRefreshes(Request $request, Client $patient) {
  455. return view('app.patient.eligible-refreshes', compact('patient'));
  456. }
  457. public function insuranceCoverage(Request $request, Client $patient) {
  458. $mbPayers = MBPayer::all();
  459. return view('app.patient.insurance-coverage', compact('patient', 'mbPayers'));
  460. }
  461. public function clientPrimaryCoverages(Request $request, Client $patient) {
  462. $mbPayers = MBPayer::all();
  463. return view('app.patient.client-primary-coverages', compact('patient', 'mbPayers'));
  464. }
  465. public function primaryCoverage(Request $request, Client $patient) {
  466. $mbPayers = MBPayer::all();
  467. return view('app.patient.primary-coverage', compact('patient', 'mbPayers'));
  468. }
  469. public function primaryCoverageForm(Request $request, Client $patient) {
  470. $mbPayers = MBPayer::all();
  471. return view('app.patient.primary-coverage-form', compact('patient', 'mbPayers'));
  472. }
  473. public function primaryCoverageManualDeterminationModal(Request $request, Client $patient) {
  474. if($patient->latestClientPrimaryCoverage->plan_type === 'MEDICARE'){
  475. return view('app.patient.primary-coverage-manual-determination-medicare-modal', compact('patient'));
  476. }
  477. if($patient->latestClientPrimaryCoverage->plan_type === 'MEDICAID'){
  478. return view('app.patient.primary-coverage-manual-determination-medicaid-modal', compact('patient'));
  479. }
  480. if($patient->latestClientPrimaryCoverage->plan_type === 'COMMERCIAL'){
  481. return view('app.patient.primary-coverage-manual-determination-commercial-modal', compact('patient'));
  482. }
  483. }
  484. public function mbClaim(Request $request, MBClaim $mbClaim) {
  485. return view('app.patient.mb-claim-single', compact('mbClaim'));
  486. }
  487. public function accounts(Request $request, Client $patient) {
  488. return view('app.patient.accounts', compact('patient'));
  489. }
  490. public function careMonthMatrix(Request $request, CareMonth $careMonth) {
  491. return view('app.patient.care-month.matrix', [
  492. 'patient' => $careMonth->patient,
  493. 'careMonth' => $careMonth,
  494. ]);
  495. }
  496. }