PatientController.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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 handouts(Request $request, Client $patient )
  240. {
  241. $handouts = Handout::where('is_active', true)->get();
  242. return view('app.patient.handouts', compact('patient', 'handouts'));
  243. }
  244. public function settings(Request $request, Client $patient )
  245. {
  246. return view('app.patient.settings', compact('patient'));
  247. }
  248. public function smsReminders(Request $request, Client $patient )
  249. {
  250. return view('app.patient.sms-reminders', compact('patient'));
  251. }
  252. public function measurementConfirmationNumbers(Request $request, Client $patient )
  253. {
  254. return view('app.patient.measurement-confirmation-numbers', compact('patient'));
  255. }
  256. public function pros(Request $request, Client $patient )
  257. {
  258. return view('app.patient.pros', compact('patient'));
  259. }
  260. public function account(Request $request, Client $patient )
  261. {
  262. return view('app.patient.account', compact('patient'));
  263. }
  264. public function careChecklist(Request $request, Client $patient )
  265. {
  266. return view('app.patient.care-checklist', compact('patient'));
  267. }
  268. public function documents(Request $request, Client $patient )
  269. {
  270. return view('app.patient.documents', compact('patient'));
  271. }
  272. public function incomingReports(Request $request, Client $patient, IncomingReport $currentReport = null)
  273. {
  274. return view('app.patient.incoming-reports', compact('patient', 'currentReport'));
  275. }
  276. public function education(Request $request, Client $patient )
  277. {
  278. return view('app.patient.education', compact('patient'));
  279. }
  280. public function messaging(Request $request, Client $patient )
  281. {
  282. return view('app.patient.messaging', compact('patient'));
  283. }
  284. public function duplicate(Request $request, Client $patient )
  285. {
  286. return view('app.patient.duplicate', compact('patient'));
  287. }
  288. public function careMonths(Request $request, Client $patient )
  289. {
  290. $careMonths = CareMonth::where('client_id', $patient->id)->orderBy('start_date', 'desc')->get();
  291. $notes = Note::where('is_cancelled', false)->get();
  292. return view('app.patient.care-months', compact('patient', 'careMonths', 'notes'));
  293. }
  294. public function presence(Request $request, Client $patient )
  295. {
  296. return json_encode([
  297. "online" => $patient->is_online
  298. ]);
  299. }
  300. public function embedSection(Request $request, Client $patient, $section, $selectable) {
  301. return view('app.patient.partials.' . $section, compact('patient', 'selectable'));
  302. }
  303. public function calendar(Request $request, Client $patient, Appointment $currentAppointment) {
  304. $pros = [];
  305. if($this->pro && $this->pro->pro_type != 'ADMIN') {
  306. $accessiblePros = ProProAccess::where('owner_pro_id', $this->pro->id)->get();
  307. $accessibleProIds = [];
  308. foreach($accessiblePros as $accessiblePro){
  309. $accessibleProIds[] = $accessiblePro->accessible_pro_id;
  310. }
  311. $accessibleProIds[] = $this->pro->id;
  312. // for dna, add pros accessible via pro teams
  313. if($this->performer->pro->isDefaultNA()) {
  314. $teams = $this->performer->pro->teamsWhereAssistant;
  315. foreach ($teams as $team) {
  316. if(!in_array($team->mcp_pro_id, $accessibleProIds)) {
  317. $accessibleProIds[] = $team->mcp_pro_id;
  318. }
  319. }
  320. }
  321. $pros = Pro::whereIn('id', $accessibleProIds)->get();
  322. }
  323. return view('app.patient.appointment-calendar', compact('pros', 'patient', 'currentAppointment'));
  324. }
  325. public function programs(Request $request, Client $patient, $filter = '') {
  326. $pros = $this->pros;
  327. return view('app.patient.programs', compact('patient', 'pros', 'filter'));
  328. }
  329. public function flowsheets(Request $request, Client $patient, $filter = '') {
  330. $pros = $this->pros;
  331. return view('app.patient.flowsheets', compact('patient', 'pros', 'filter'));
  332. }
  333. public function vitalsSettings(Request $request, Client $patient) {
  334. return view('app.patient.vitals-settings', compact('patient'));
  335. }
  336. public function vitalsGraph(Request $request, Client $patient, $filter = '') {
  337. $pros = $this->pros;
  338. return view('app.patient.vitals-graph', compact('patient', 'pros', 'filter'));
  339. }
  340. public function tickets(Request $request, Client $patient, $type = '', String $currentTicket = '') {
  341. $pros = $this->pros;
  342. $allPros = Pro::all();
  343. $qlTicket = $currentTicket;
  344. if(!!$currentTicket) {
  345. $qlTicket = Ticket::where('uid', $currentTicket)->first();
  346. if($qlTicket) {
  347. $currentTicket = $qlTicket;
  348. }
  349. }
  350. return view('app.patient.tickets', compact('patient', 'pros', 'allPros', 'type', 'currentTicket'));
  351. }
  352. public function prescriptions(Request $request, Client $patient, String $type = '', String $currentErx = '') {
  353. if(!!$currentErx) {
  354. $currentErx = Erx::where('uid', $currentErx)->first();
  355. }
  356. $note = $patient->coreNote;
  357. return view('app.patient.prescriptions.index', compact('patient', 'type', 'currentErx', 'note'));
  358. }
  359. public function prescriptionsPopup(Request $request, Client $patient, String $type = '', String $currentErx = '') {
  360. if(!!$currentErx) {
  361. $currentErx = Erx::where('uid', $currentErx)->first();
  362. }
  363. $note = null;
  364. if($request->input('noteUid')) {
  365. $note = Note::where('uid', $request->input('noteUid'))->first();
  366. }
  367. return view('app.patient.prescriptions-popup.list-popup', compact('patient', 'type', 'currentErx', 'note'));
  368. }
  369. public function prescriptionsList(Request $request, Client $patient, String $type = '', String $currentErx = '') {
  370. if(!!$currentErx) {
  371. $currentErx = Erx::where('uid', $currentErx)->first();
  372. }
  373. $note = null;
  374. if($request->input('noteUid')) {
  375. $note = Note::where('uid', $request->input('noteUid'))->first();
  376. }
  377. return view('app.patient.prescriptions.list', compact('patient', 'type', 'currentErx', 'note'));
  378. }
  379. public function downloadPrescriptionAsPdf(Request $request, Erx $prescription){
  380. if($request->input('html')) {
  381. return view('app.patient.prescriptions.pdf.pdf-preview', compact('prescription'));
  382. }
  383. else {
  384. $pdf = PDF::loadView('app.patient.prescriptions.pdf.pdf-preview', compact('prescription'));
  385. return $pdf->download($prescription->created_at .'_' . 'erx.pdf');
  386. }
  387. }
  388. public function transmitPrescription(Request $request, Erx $prescription){
  389. // re-generate pdf with cover sheet as first page and save it to FS
  390. $filePath = config('app.temp_dir') . "/{$prescription->uid}.pdf";
  391. $pdf = PDF::loadView('app.patient.prescriptions.pdf.pdf-preview-with-cover-sheet', compact('prescription'));
  392. $pdf->save($filePath);
  393. // send it along with the rest of the params to /api/erx/transmit [multi-part POST]
  394. $url = config('stag.backendUrl') . '/erx/transmit';
  395. $params = [
  396. "uid" => $request->input('uid'),
  397. "toWho" => $request->input('toWho'),
  398. "toEmail" => $request->input('toEmail'),
  399. "toFaxNumber" => $request->input('toFaxNumber'),
  400. "toFaxNumberAttentionLine" => $request->input('toFaxNumberAttentionLine'),
  401. "toFaxNumberCoverSheetMemo" => $request->input('toFaxNumberCoverSheetMemo'),
  402. ];
  403. if($request->input('copyToPatient')) {
  404. $params["copyToPatientFaxNumber"] = $request->input('copyToPatientFaxNumber');
  405. $params["copyToPatientEmail"] = $request->input('copyToPatientEmail');
  406. }
  407. $pdf = fopen($filePath, 'r');
  408. $response = Http
  409. ::attach('pdfSystemFile', $filePath, "{$prescription->uid}.pdf")
  410. ->withHeaders(['sessionKey' => $request->cookie('sessionKey')])
  411. ->post($url, $params)
  412. ->json();
  413. return $response;
  414. }
  415. public function supplyOrders(Request $request, Client $patient, SupplyOrder $supplyOrder = null)
  416. {
  417. $products = Product::where('is_active', true)->orderBy('created_at', 'desc')->get();
  418. return view('app.patient.supply-orders', compact('patient', 'supplyOrder', 'products'));
  419. }
  420. public function shipments(Request $request, Client $patient, Shipment $shipment = null)
  421. {
  422. return view('app.patient.shipments', compact('patient', 'shipment'));
  423. }
  424. public function appointments(Request $request, Client $patient, $forPro = 'all', $status = 'all') {
  425. $pros = $this->pros;
  426. $appointments = $patient->appointmentsForProByStatus('all', 'ALL');
  427. $appointmentProIDs = $appointments->map(function($_item) {
  428. return $_item->pro_id;
  429. });
  430. $appointmentPros = Pro::whereIn('id', $appointmentProIDs)->get();
  431. $appointments = $patient->appointmentsForProByStatus($forPro, strtoupper($status));
  432. return view('app.patient.appointments',
  433. compact('patient', 'pros', 'appointments', 'appointmentPros', 'forPro', 'status'));
  434. }
  435. public function mcpRequests(Request $request, Client $patient) {
  436. return view('app.patient.mcp-requests', compact('patient'));
  437. }
  438. public function eligibleRefreshes(Request $request, Client $patient) {
  439. return view('app.patient.eligible-refreshes', compact('patient'));
  440. }
  441. public function insuranceCoverage(Request $request, Client $patient) {
  442. $mbPayers = MBPayer::all();
  443. return view('app.patient.insurance-coverage', compact('patient', 'mbPayers'));
  444. }
  445. public function clientPrimaryCoverages(Request $request, Client $patient) {
  446. $mbPayers = MBPayer::all();
  447. return view('app.patient.client-primary-coverages', compact('patient', 'mbPayers'));
  448. }
  449. public function primaryCoverage(Request $request, Client $patient) {
  450. $mbPayers = MBPayer::all();
  451. return view('app.patient.primary-coverage', compact('patient', 'mbPayers'));
  452. }
  453. public function primaryCoverageForm(Request $request, Client $patient) {
  454. $mbPayers = MBPayer::all();
  455. return view('app.patient.primary-coverage-form', compact('patient', 'mbPayers'));
  456. }
  457. public function primaryCoverageManualDeterminationModal(Request $request, Client $patient) {
  458. if($patient->latestClientPrimaryCoverage->plan_type === 'MEDICARE'){
  459. return view('app.patient.primary-coverage-manual-determination-medicare-modal', compact('patient'));
  460. }
  461. if($patient->latestClientPrimaryCoverage->plan_type === 'MEDICAID'){
  462. return view('app.patient.primary-coverage-manual-determination-medicaid-modal', compact('patient'));
  463. }
  464. if($patient->latestClientPrimaryCoverage->plan_type === 'COMMERCIAL'){
  465. return view('app.patient.primary-coverage-manual-determination-commercial-modal', compact('patient'));
  466. }
  467. return "Plan Type is missing!";
  468. }
  469. public function mbClaim(Request $request, MBClaim $mbClaim) {
  470. return view('app.patient.mb-claim-single', compact('mbClaim'));
  471. }
  472. public function accounts(Request $request, Client $patient) {
  473. return view('app.patient.accounts', compact('patient'));
  474. }
  475. public function careMonthMatrix(Request $request, CareMonth $careMonth) {
  476. return view('app.patient.care-month.matrix', [
  477. 'patient' => $careMonth->patient,
  478. 'careMonth' => $careMonth,
  479. ]);
  480. }
  481. }