PatientController.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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\ClientMemoView;
  10. use App\Models\ClientPrimaryCoverage;
  11. use App\Models\ClientProAccess;
  12. use App\Models\Company;
  13. use App\Models\CompanyPro;
  14. use App\Models\CompanyClient;
  15. use App\Models\CompanyProDocument;
  16. use App\Models\Customer;
  17. use App\Models\Erx;
  18. use App\Models\Facility;
  19. use App\Models\Handout;
  20. use App\Models\HandoutClient;
  21. use App\Models\IncomingReport;
  22. use App\Models\Invoice;
  23. use App\Models\MBClaim;
  24. use App\Models\MBPayer;
  25. use App\Models\Note;
  26. use App\Models\NoteTemplate;
  27. use App\Models\Pro;
  28. use App\Models\Product;
  29. use App\Models\ProProAccess;
  30. use App\Models\SectionTemplate;
  31. use App\Models\Shipment;
  32. use App\Models\SupplyOrder;
  33. use App\Models\Ticket;
  34. use Illuminate\Http\Request;
  35. use Illuminate\Support\Facades\DB;
  36. use Illuminate\Support\Facades\File;
  37. use App\Models\Measurement;
  38. use App\Models\ClientReviewRequest;
  39. use App\Models\Point;
  40. use App\Models\Survey;
  41. use Illuminate\Support\Facades\Http;
  42. use PDF;
  43. class PatientController extends Controller
  44. {
  45. public function invoicingCompanies(Request $request, Client $patient) {
  46. return view('app.patient.invoicing.companies', compact('patient'));
  47. }
  48. public function invoicingInvoices(Request $request, Client $patient, Customer $customer) {
  49. return view('app.patient.invoicing.invoices', compact('patient', 'customer'));
  50. }
  51. public function invoicingCustomerTransactions(Request $request, Client $patient, Customer $customer) {
  52. return view('app.patient.invoicing.customer-transactions', compact('patient', 'customer'));
  53. }
  54. public function invoicingInvoiceTransactions(Request $request, Client $patient, Invoice $invoice) {
  55. $customer = $invoice->customer;
  56. return view('app.patient.invoicing.invoice-transactions', compact('patient', 'invoice', 'customer'));
  57. }
  58. public function invoicingInvoiceTransactionsInPopup(Request $request, Client $patient, Invoice $invoice) {
  59. $customer = $invoice->customer;
  60. return view('app.patient.invoicing.invoice-transactions-in-popup', compact('patient', 'invoice', 'customer'));
  61. }
  62. public function claimsResolver(Request $request, Client $patient)
  63. {
  64. $notes = $patient->notesAscending;
  65. $hcpSignedNotesCount = 0;
  66. foreach($notes as $note){
  67. if($note->is_signed_by_hcp){
  68. $hcpSignedNotesCount += 1;
  69. }
  70. }
  71. $data = [
  72. 'dog' => 'bark',
  73. 'patient' => $patient,
  74. 'hcpSignedNotesCount' => $hcpSignedNotesCount
  75. ];
  76. return view('app.patient.claims-resolver', $data);
  77. }
  78. public function dashboard(Request $request, Client $patient )
  79. {
  80. $mcpPros = Pro::where('is_enrolled_as_mcp', true)->get();
  81. $facilities = []; // Facility::where('is_active', true)->get();
  82. // get assigned devices
  83. $assignedDeviceIDs = DB::select(DB::raw("SELECT device_id from client_bdt_device where is_active = true"));
  84. $assignedDeviceIDs = array_map(function($_x) {
  85. return $_x->device_id;
  86. }, $assignedDeviceIDs);
  87. // get all except assigned ones
  88. $devices = BDTDevice::where('is_active', true)
  89. ->whereNotIn('id', $assignedDeviceIDs)
  90. ->orderBy('imei', 'asc')
  91. ->get();
  92. $assignedDeviceIDs = null;
  93. unset($assignedDeviceIDs);
  94. $availableDevices = count($devices);
  95. $patientDeviceIDs = ClientBDTDevice::select('id')->where('client_id', $patient->id)->where('is_active', true)->get()->toArray();
  96. $patientDeviceIDs = array_map(function ($_x) {
  97. return $_x["id"];
  98. }, $patientDeviceIDs);
  99. $dxInfoLines = ClientInfoLine::where('client_id', $patient->id)
  100. ->where('category', 'dx')
  101. ->where('is_removed', false)
  102. ->orderBy('content_text', 'asc')
  103. ->get();
  104. $clientMemos = ClientMemoView::where('client_id', $patient->id)->where('is_cancelled', false)->orderBy('created_at', 'DESC')->get();
  105. $shortCutsObject = [];
  106. foreach ($this->pro->allShortcuts() as $shortcut) {
  107. // %replaceables%
  108. $shortcut->text = str_replace("%AGE%", $patient->age_in_years, $shortcut->text);
  109. $shortcut->text = str_replace("%GENDER%", $patient->sex, $shortcut->text);
  110. $shortcut->text = str_replace("%NAME%", $patient->displayName(), $shortcut->text);
  111. $shortCutsObject[] = [
  112. "name" => $shortcut->shortcut,
  113. "value" => $shortcut->text
  114. ];
  115. }
  116. $recentMeasurements = $patient->recentMeasurements;
  117. $latestVitals = Point::where('client_id', $patient->id)->where('category', 'VITALS')->orderBy('id', 'DESC')->first();
  118. $nonCoreVisitNotes = $patient->nonCoreVisitNotes;
  119. $disallowPointEdits = $nonCoreVisitNotes && count($nonCoreVisitNotes);
  120. return view('app.patient.dashboard',
  121. compact('patient', 'facilities', 'devices', 'dxInfoLines', 'clientMemos', 'shortCutsObject', 'availableDevices', 'patientDeviceIDs', 'recentMeasurements', 'latestVitals', 'disallowPointEdits'));
  122. }
  123. public function canvasMigrate(Request $request, Client $patient )
  124. {
  125. $mcpPros = Pro::where('is_enrolled_as_mcp', true)->get();
  126. $facilities = []; // Facility::where('is_active', true)->get();
  127. // get assigned devices
  128. $assignedDeviceIDs = DB::select(DB::raw("SELECT device_id from client_bdt_device where is_active = true"));
  129. $assignedDeviceIDs = array_map(function($_x) {
  130. return $_x->device_id;
  131. }, $assignedDeviceIDs);
  132. // get all except assigned ones
  133. $devices = BDTDevice::where('is_active', true)
  134. ->whereNotIn('id', $assignedDeviceIDs)
  135. ->orderBy('imei', 'asc')
  136. ->get();
  137. $assignedDeviceIDs = null;
  138. unset($assignedDeviceIDs);
  139. $dxInfoLines = ClientInfoLine::where('client_id', $patient->id)
  140. ->where('category', 'dx')
  141. ->where('is_removed', false)
  142. ->orderBy('content_text', 'asc')
  143. ->get();
  144. return view('app.patient.canvas-migrate',
  145. compact('patient', 'facilities', 'devices', 'dxInfoLines'));
  146. }
  147. public function canvas(Request $request, Client $patient){
  148. return view('app.patient.canvas_dump', compact('patient'));
  149. }
  150. public function intake(Request $request, Client $patient )
  151. {
  152. $files = File::allFiles(resource_path('views/app/intake-templates'));
  153. $templates = [];
  154. foreach ($files as $file) {
  155. $templates[] = str_replace(".blade.php", "", $file->getFilename());
  156. }
  157. return view('app.patient.intake', compact('patient', 'templates'));
  158. }
  159. public function carePlan(Request $request, Client $patient )
  160. {
  161. return view('app.patient.care-plan', compact('patient'));
  162. }
  163. public function medications(Request $request, Client $patient )
  164. {
  165. $infoLines = ClientInfoLine::where('client_id', $patient->id)
  166. ->where('category', 'rx')
  167. ->where('is_removed', false)
  168. ->orderBy('content_text', 'asc')
  169. ->get();
  170. return view('app.patient.medications', compact('patient', 'infoLines'));
  171. }
  172. public function dxAndFocusAreas(Request $request, Client $patient )
  173. {
  174. $dxInfoLines = ClientInfoLine::where('client_id', $patient->id)
  175. ->where('category', 'dx')
  176. ->where('is_removed', false)
  177. ->orderBy('content_text', 'asc')
  178. ->get();
  179. return view('app.patient.dx-and-focus-areas', compact('patient', 'dxInfoLines'));
  180. }
  181. public function careTeam(Request $request, Client $patient )
  182. {
  183. $infoLines = ClientInfoLine::where('client_id', $patient->id)
  184. ->where('category', 'care_team')
  185. ->where('is_removed', false)
  186. ->get();
  187. return view('app.patient.care-team', compact('patient', 'infoLines'));
  188. }
  189. public function devices(Request $request, Client $patient )
  190. {
  191. // get assigned devices
  192. $assignedDeviceIDs = DB::select(DB::raw("SELECT device_id from client_bdt_device where is_active = true"));
  193. $assignedDeviceIDs = array_map(function($_x) {
  194. return $_x->device_id;
  195. }, $assignedDeviceIDs);
  196. // get all except assigned ones
  197. $devices = BDTDevice::where('is_active', true)
  198. ->whereNotIn('id', $assignedDeviceIDs)
  199. ->orderBy('imei', 'asc')
  200. ->get();
  201. $assignedDeviceIDs = null;
  202. unset($assignedDeviceIDs);
  203. return view('app.patient.devices', compact('patient', 'devices'));
  204. }
  205. public function measurements(Request $request, Client $patient )
  206. {
  207. $measurements = Measurement::where('client_id', $patient->id)->where('is_active', true)->orderByRaw('ts DESC NULLS LAST')->paginate(30);
  208. return view('app.patient.measurements', compact('patient', 'measurements'));
  209. }
  210. public function labsAndStudies(Request $request, Client $patient )
  211. {
  212. return view('app.patient.labs-and-studies', compact('patient'));
  213. }
  214. public function history(Request $request, Client $patient )
  215. {
  216. $infoLines = ClientInfoLine::where('client_id', $patient->id)
  217. ->where('category', 'LIKE', 'history_%')
  218. ->where('is_removed', false)
  219. ->get();
  220. return view('app.patient.history', compact('patient', 'infoLines'));
  221. }
  222. public function memos(Request $request, Client $patient )
  223. {
  224. return view('app.patient.memos', compact('patient'));
  225. }
  226. public function memosThread(Request $request, Client $patient )
  227. {
  228. return view('app.patient.memos-thread', compact('patient'));
  229. }
  230. public function messagesThread(Request $request, Client $patient )
  231. {
  232. return view('app.patient.messages-thread', compact('patient'));
  233. }
  234. public function sms(Request $request, Client $patient )
  235. {
  236. return view('app.patient.sms', compact('patient'));
  237. }
  238. public function outgoingSmsLog(Request $request, Client $patient )
  239. {
  240. return view('app.patient.outgoing-sms-log', compact('patient'));
  241. }
  242. public function reviewRequests(Request $request, Client $patient){
  243. $pro = $this->performer->pro;
  244. $reviewRequests = ClientReviewRequest::where('client_id', $patient->id);
  245. if($pro->pro_type !== 'ADMIN'){
  246. $reviewRequests = $reviewRequests->where('pro_id', $pro->id)->where('is_active', true);
  247. }
  248. $reviewRequests = $reviewRequests->orderBy('created_at', 'DESC')->paginate(50);
  249. return view('app.patient.review-requests.list', compact('patient', 'reviewRequests'));
  250. }
  251. public function smsNumbers(Request $request, Client $patient )
  252. {
  253. return view('app.patient.sms-numbers', compact('patient'));
  254. }
  255. public function immunizations(Request $request, Client $patient )
  256. {
  257. return view('app.patient.immunizations', compact('patient'));
  258. }
  259. public function allergies(Request $request, Client $patient )
  260. {
  261. $infoLines = ClientInfoLine::where('client_id', $patient->id)
  262. ->where('category', 'allergy')
  263. ->where('is_removed', false)
  264. ->get();
  265. return view('app.patient.allergies', compact('patient', 'infoLines'));
  266. }
  267. public function notes(Request $request, Client $patient, $filter = 'active')
  268. {
  269. $pros = $this->pros;
  270. return view('app.patient.notes', compact('patient','pros', 'filter'));
  271. }
  272. public function genericBills(Request $request, Client $patient)
  273. {
  274. return view('app.patient.generic-bills', compact('patient'));
  275. }
  276. public function rmSetup(Request $request, Client $patient)
  277. {
  278. // get assigned devices
  279. $assignedDeviceIDs = DB::select(DB::raw("SELECT device_id from client_bdt_device where is_active = true"));
  280. $assignedDeviceIDs = array_map(function($_x) {
  281. return $_x->device_id;
  282. }, $assignedDeviceIDs);
  283. // get all except assigned ones
  284. $devices = BDTDevice::where('is_active', true)
  285. ->whereNotIn('id', $assignedDeviceIDs)
  286. ->orderBy('imei', 'asc')
  287. ->get();
  288. $assignedDeviceIDs = null;
  289. unset($assignedDeviceIDs);
  290. return view('app.patient.rm-setup', compact('patient', 'devices'));
  291. }
  292. public function handouts(Request $request, Client $patient )
  293. {
  294. $clientHandouts = HandoutClient::where('client_id', $patient->id)->get();
  295. $handouts = Handout::where('is_active', true)->orderBy('display_name', 'ASC')->get();
  296. return view('app.patient.handouts', compact('patient', 'clientHandouts', 'handouts'));
  297. }
  298. public function settings(Request $request, Client $patient )
  299. {
  300. $companies = Company::all();
  301. return view('app.patient.settings', compact('patient', 'companies'));
  302. }
  303. public function smsReminders(Request $request, Client $patient )
  304. {
  305. return view('app.patient.sms-reminders', compact('patient'));
  306. }
  307. public function measurementConfirmationNumbers(Request $request, Client $patient )
  308. {
  309. return view('app.patient.measurement-confirmation-numbers', compact('patient'));
  310. }
  311. public function pros(Request $request, Client $patient )
  312. {
  313. return view('app.patient.pros', compact('patient'));
  314. }
  315. public function proChanges(Request $request, Client $patient )
  316. {
  317. return view('app.patient.client-pro-changes', compact('patient'));
  318. }
  319. public function account(Request $request, Client $patient )
  320. {
  321. return view('app.patient.account', compact('patient'));
  322. }
  323. public function careChecklist(Request $request, Client $patient )
  324. {
  325. return view('app.patient.care-checklist', compact('patient'));
  326. }
  327. public function documents(Request $request, Client $patient )
  328. {
  329. return view('app.patient.documents', compact('patient'));
  330. }
  331. public function incomingReports(Request $request, Client $patient, IncomingReport $currentReport = null)
  332. {
  333. return view('app.patient.incoming-reports', compact('patient', 'currentReport'));
  334. }
  335. public function education(Request $request, Client $patient )
  336. {
  337. return view('app.patient.education', compact('patient'));
  338. }
  339. public function messaging(Request $request, Client $patient )
  340. {
  341. return view('app.patient.messaging', compact('patient'));
  342. }
  343. public function duplicate(Request $request, Client $patient )
  344. {
  345. return view('app.patient.duplicate', compact('patient'));
  346. }
  347. public function careMonths(Request $request, Client $patient )
  348. {
  349. $careMonths = CareMonth::where('client_id', $patient->id)->orderBy('start_date', 'desc')->get();
  350. $notes = Note::where('is_cancelled', false)->get();
  351. return view('app.patient.care-months', compact('patient', 'careMonths', 'notes'));
  352. }
  353. public function presence(Request $request, Client $patient )
  354. {
  355. return json_encode([
  356. "online" => $patient->is_online
  357. ]);
  358. }
  359. public function embedSection(Request $request, Client $patient, $section, $selectable) {
  360. return view('app.patient.partials.' . $section, compact('patient', 'selectable'));
  361. }
  362. public function calendar(Request $request, Client $patient, Appointment $currentAppointment) {
  363. $pros = [];
  364. if($this->pro && $this->pro->pro_type != 'ADMIN') {
  365. $accessiblePros = ProProAccess::where('owner_pro_id', $this->pro->id)->get();
  366. $accessibleProIds = [];
  367. foreach($accessiblePros as $accessiblePro){
  368. $accessibleProIds[] = $accessiblePro->accessible_pro_id;
  369. }
  370. $accessibleProIds[] = $this->pro->id;
  371. // for dna, add pros accessible via pro teams
  372. if($this->performer->pro->isDefaultNA()) {
  373. $teams = $this->performer->pro->teamsWhereAssistant;
  374. foreach ($teams as $team) {
  375. if(!in_array($team->mcp_pro_id, $accessibleProIds)) {
  376. $accessibleProIds[] = $team->mcp_pro_id;
  377. }
  378. }
  379. }
  380. $pros = Pro::whereIn('id', $accessibleProIds)->get();
  381. }
  382. $dateLastWeek = date_sub(date_create(), date_interval_create_from_date_string("14 days"));
  383. $dateLastWeek = date_format($dateLastWeek, "Y-m-d");
  384. $appointments = Appointment::where('client_id', $patient->id)
  385. ->orderBy('raw_date', 'desc')->orderBy('raw_start_time', 'desc')
  386. ->where('raw_date', '>=', $dateLastWeek)
  387. ->get();
  388. $appointmentProIDs = $appointments->map(function($_item) {
  389. return $_item->pro_id;
  390. });
  391. $appointmentPros = Pro::whereIn('id', $appointmentProIDs)->get();
  392. return view('app.patient.appointment-calendar',
  393. compact('pros', 'patient', 'currentAppointment', 'appointments', 'appointmentPros'));
  394. }
  395. public function flowsheets(Request $request, Client $patient, $filter = '') {
  396. $pros = $this->pros;
  397. return view('app.patient.flowsheets', compact('patient', 'pros', 'filter'));
  398. }
  399. public function vitalsSettings(Request $request, Client $patient) {
  400. return view('app.patient.vitals-settings', compact('patient'));
  401. }
  402. public function vitalsGraph(Request $request, Client $patient, $filter = '') {
  403. $pros = $this->pros;
  404. return view('app.patient.vitals-graph', compact('patient', 'pros', 'filter'));
  405. }
  406. public function sleepStudy(Request $request, Client $patient) {
  407. return view('app.patient.sleep-study', compact('patient'));
  408. }
  409. public function sleepStudyStep(Request $request, Client $patient) {
  410. return view('app.patient.sleep-study.' . $request->input('step'), compact('patient'));
  411. }
  412. public function tickets(Request $request, Client $patient, $type = '', String $currentTicket = '') {
  413. $pros = $this->pros;
  414. $allPros = Pro::all();
  415. $qlTicket = $currentTicket;
  416. if(!!$currentTicket) {
  417. $qlTicket = Ticket::where('uid', $currentTicket)->first();
  418. if($qlTicket) {
  419. $currentTicket = $qlTicket;
  420. }
  421. }
  422. return view('app.patient.tickets', compact('patient', 'pros', 'allPros', 'type', 'currentTicket'));
  423. }
  424. protected function getDefaultFacility(){
  425. $defaultFacility = Facility::where('name', 'Ultra Care Pharmacy')->where('address_city', 'Baltimore')->first();
  426. return $defaultFacility;
  427. }
  428. public function prescriptions(Request $request, Client $patient, String $type = '', String $currentErx = '') {
  429. $this->updateDefaultPatientPharmacy($patient);
  430. if(!!$currentErx) {
  431. $currentErx = Erx::where('uid', $currentErx)->first();
  432. }
  433. $note = $patient->coreNote;
  434. $defaultFacility = $this->getDefaultFacility();
  435. $patient->refresh();
  436. return view('app.patient.prescriptions.index', compact('patient', 'type', 'currentErx', 'note', 'defaultFacility'));
  437. }
  438. protected function updateDefaultPatientPharmacy(Client $patient){
  439. $prescriptions = $patient->prescriptions;
  440. if(!count($prescriptions)) return;
  441. $defaultFacility = $this->getDefaultFacility();
  442. if(!$defaultFacility) return;
  443. foreach($prescriptions as $prescription){
  444. if($prescription->logistics_detail_json) continue;
  445. $this->setPrescriptionDefaultPharmacy($prescription, $defaultFacility);
  446. }
  447. }
  448. private function setPrescriptionDefaultPharmacy(Erx $prescription, Facility $facility){
  449. $data = [
  450. 'uid' => $prescription->uid,
  451. 'logisticsDetailJson' => json_encode([
  452. 'facilityName' => $facility->name,
  453. 'facilityCity' => $facility->address_city,
  454. 'facilityState' => $facility->address_state,
  455. 'facilityAddressMemo' => '',
  456. 'facilityPhone' => $facility->phone,
  457. 'facilityFax' => $facility->fax,
  458. 'facilityZip' => $facility->address_zip,
  459. ])
  460. ];
  461. $response = $this->callJavaApi('/erx/updateLogisticsDetail', $data);
  462. }
  463. public function prescriptionsPopup(Request $request, Client $patient, String $type = '', String $currentErx = '') {
  464. if(!!$currentErx) {
  465. $currentErx = Erx::where('uid', $currentErx)->first();
  466. }
  467. $note = null;
  468. if($request->input('noteUid')) {
  469. $note = Note::where('uid', $request->input('noteUid'))->first();
  470. }
  471. return view('app.patient.prescriptions-popup.list-popup', compact('patient', 'type', 'currentErx', 'note'));
  472. }
  473. public function prescriptionsList(Request $request, Client $patient, String $type = '', String $currentErx = '') {
  474. if(!!$currentErx) {
  475. $currentErx = Erx::where('uid', $currentErx)->first();
  476. }
  477. $note = null;
  478. if($request->input('noteUid')) {
  479. $note = Note::where('uid', $request->input('noteUid'))->first();
  480. }
  481. return view('app.patient.prescriptions.list', compact('patient', 'type', 'currentErx', 'note'));
  482. }
  483. public function downloadPrescriptionAsPdf(Request $request, Erx $prescription){
  484. if($request->input('html')) {
  485. return view('app.patient.prescriptions.pdf.pdf-preview', compact('prescription'));
  486. }
  487. else {
  488. $pdf = PDF::loadView('app.patient.prescriptions.pdf.pdf-preview', compact('prescription'));
  489. return $pdf->download($prescription->created_at .'_' . 'erx.pdf');
  490. }
  491. }
  492. public function transmitPrescription(Request $request, Erx $prescription){
  493. // re-generate pdf with cover sheet as first page and save it to FS
  494. $filePath = config('app.temp_dir') . "/{$prescription->uid}.pdf";
  495. $pdf = PDF::loadView('app.patient.prescriptions.pdf.pdf-preview-with-cover-sheet', compact('prescription'));
  496. $pdf->save($filePath);
  497. // send it along with the rest of the params to /api/erx/transmit [multi-part POST]
  498. $url = config('stag.backendUrl') . '/erx/transmit';
  499. $params = [
  500. "uid" => $request->input('uid'),
  501. "toWho" => $request->input('toWho'),
  502. "toEmail" => $request->input('toEmail'),
  503. "toFaxNumber" => $request->input('toFaxNumber'),
  504. "toFaxNumberAttentionLine" => $request->input('toFaxNumberAttentionLine'),
  505. "toFaxNumberCoverSheetMemo" => $request->input('toFaxNumberCoverSheetMemo'),
  506. ];
  507. if($request->input('copyToPatient')) {
  508. $params["copyToPatientFaxNumber"] = $request->input('copyToPatientFaxNumber');
  509. $params["copyToPatientEmail"] = $request->input('copyToPatientEmail');
  510. }
  511. $pdf = fopen($filePath, 'r');
  512. $response = Http
  513. ::attach('pdfSystemFile', $filePath, "{$prescription->uid}.pdf")
  514. ->withHeaders(['sessionKey' => $request->cookie('sessionKey')])
  515. ->post($url, $params)
  516. ->json();
  517. return $response;
  518. }
  519. public function supplyOrders(Request $request, Client $patient, SupplyOrder $supplyOrder = null)
  520. {
  521. $products = Product::where('is_active', true)->orderBy('created_at', 'desc')->get();
  522. return view('app.patient.supply-orders', compact('patient', 'supplyOrder', 'products'));
  523. }
  524. public function shipments(Request $request, Client $patient, Shipment $shipment = null)
  525. {
  526. return view('app.patient.shipments', compact('patient', 'shipment'));
  527. }
  528. public function appointments(Request $request, Client $patient, $forPro = 'all', $status = 'all') {
  529. $pros = $this->pros;
  530. $appointments = $patient->appointmentsForProByStatus($forPro, strtoupper($status));
  531. $appointmentProIDs = $appointments->map(function($_item) {
  532. return $_item->pro_id;
  533. });
  534. $appointmentPros = Pro::whereIn('id', $appointmentProIDs)->get();
  535. return view('app.patient.appointments',
  536. compact('patient', 'pros', 'appointments', 'appointmentPros', 'forPro', 'status'));
  537. }
  538. public function mcpRequests(Request $request, Client $patient) {
  539. return view('app.patient.mcp-requests', compact('patient'));
  540. }
  541. public function eligibleRefreshes(Request $request, Client $patient) {
  542. return view('app.patient.eligible-refreshes', compact('patient'));
  543. }
  544. public function insuranceCoverage(Request $request, Client $patient) {
  545. $mbPayers = MBPayer::all();
  546. return view('app.patient.insurance-coverage', compact('patient', 'mbPayers'));
  547. }
  548. public function clientPrimaryCoverages(Request $request, Client $patient) {
  549. $mbPayers = MBPayer::all();
  550. return view('app.patient.client-primary-coverages', compact('patient', 'mbPayers'));
  551. }
  552. public function primaryCoverage(Request $request, Client $patient) {
  553. $mbPayers = MBPayer::all();
  554. return view('app.patient.primary-coverage', compact('patient', 'mbPayers'));
  555. }
  556. public function primaryCoverageForm(Request $request, Client $patient) {
  557. $mbPayers = MBPayer::all();
  558. return view('app.patient.primary-coverage-form', compact('patient', 'mbPayers'));
  559. }
  560. public function primaryCoverageManualDeterminationModal(Request $request, Client $patient) {
  561. $coverageUid = $request->get('coverageUid');
  562. $coverage = ClientPrimaryCoverage::where('uid', $coverageUid)->first();
  563. if($patient->latestClientPrimaryCoverage->plan_type === 'MEDICARE'){
  564. return view('app.patient.primary-coverage-manual-determination-medicare-modal', compact('patient', 'coverage'));
  565. }
  566. if($patient->latestClientPrimaryCoverage->plan_type === 'MEDICAID'){
  567. return view('app.patient.primary-coverage-manual-determination-medicaid-modal', compact('patient', 'coverage'));
  568. }
  569. if($patient->latestClientPrimaryCoverage->plan_type === 'COMMERCIAL'){
  570. return view('app.patient.primary-coverage-manual-determination-commercial-modal', compact('patient', 'coverage'));
  571. }
  572. return "Plan Type is missing!";
  573. }
  574. public function mbClaim(Request $request, MBClaim $mbClaim) {
  575. return view('app.patient.mb-claim-single', compact('mbClaim'));
  576. }
  577. public function accounts(Request $request, Client $patient) {
  578. return view('app.patient.accounts', compact('patient'));
  579. }
  580. public function companies(Request $request, Client $patient) {
  581. $companies = Company::where('is_active', true)->get();
  582. $companyClientStatusMap = [];
  583. $companyClientStatusMap['NEW'] = 'New';
  584. $companyClientStatusMap['ELIGIBILITY_VERIFIED'] = 'Eligibility Verified';
  585. $companyClientStatusMap['ELIGIBILITY_PENDING'] = 'Eligibility Pending';
  586. $companyClientStatusMap['NOT_ELIGIBLE'] = 'Not Eligible';
  587. $companyClientStatusMap['INITIAL_CONSULT'] = 'Initial Consult';
  588. $companyClientStatusMap['HST_DELIVERED'] = 'HST Delivered';
  589. $companyClientStatusMap['STUDY_PENDING'] = 'Study Pending';
  590. $companyClientStatusMap['STUDY_COMPLETED'] = 'Study Completed';
  591. $companyClientStatusMap['STUDY_INTERPRETED'] = 'Study Interpreted';
  592. $companyClientStatusMap['POST_HST_VISIT'] = 'Post HST Visit';
  593. $companyClientStatusMap['CPAP_RX'] = 'CPAP Rx';
  594. $companyClientStatusMap['ORAL_APPLIANCE_RX'] = 'Oral Appliance Rx';
  595. $companyClientStatusMap['NOT_INTERESTED'] = 'Not Interested';
  596. $companyClientStatusMap['IN_LAB_STUDY'] = 'In Lab Study';
  597. $companyClientStatusMap['UNRESPONSIVE'] = 'Unresponsive';
  598. return view('app.patient.companies', compact('patient', 'companies', 'companyClientStatusMap'));
  599. }
  600. public function rtm(Request $request, Client $patient) {
  601. return view('app.patient.rtm', compact('patient'));
  602. }
  603. public function careMonthMatrix(Request $request, CareMonth $careMonth) {
  604. return view('app.patient.care-month.matrix', [
  605. 'patient' => $careMonth->patient,
  606. 'careMonth' => $careMonth,
  607. ]);
  608. }
  609. public function clientProAccess(Request $request, Client $patient) {
  610. $rows = ClientProAccess::where('client_id', $patient->id)->get();
  611. return view('app.patient.client-pro-access', compact('patient', 'rows'));
  612. }
  613. public function clientDocuments(Request $request, Client $patient){
  614. $templates = get_doc_templates();
  615. $companyProIDs = DB::select('SELECT company_pro_id FROM company_pro_document WHERE related_client_id = ?', [$patient->id]);
  616. $companyProIDInts = [];
  617. foreach($companyProIDs as $cpId){
  618. $companyProIDInts[] = $cpId->company_pro_id;
  619. }
  620. $companyPros = CompanyPro::whereIn('id', $companyProIDInts)->get();
  621. return view('app.patient.client-documents', compact('templates', 'companyPros', 'patient'));
  622. }
  623. public function clientDocumentsRequests(Request $request, Client $patient){
  624. $templates = $this->getTemplates();
  625. $companyClients = CompanyClient::where('client_id', $patient->id)->get();
  626. $companyClientsIds = CompanyClient::where('client_id', $patient->id)->pluck('id')->toArray();
  627. $documents = CompanyProDocument::whereIn('company_client_id', $companyClientsIds)->orderBy('created_at', 'DESC')->paginate(50);
  628. return view('app.patient.client-documents-requests', compact('templates', 'patient', 'companyClients', 'documents'));
  629. }
  630. private function getTemplates(){
  631. return get_doc_templates();
  632. }
  633. public function insuranceCoverageHistory(Request $request, Client $patient){
  634. $insuranceCoverageHistory = ClientPrimaryCoverage::where('client_id', $patient->id)->orderBy('created_at', 'DESC')->get();
  635. return view('app.patient.insurance-coverage-history', compact('patient', 'insuranceCoverageHistory'));
  636. }
  637. public function protocolBuilder(Request $request, Client $patient) {
  638. return view('app.patient.rtm.protocol-builder', compact('patient'));
  639. }
  640. public function checkIfCptCodeIsSubmitted(Request $request){
  641. $clientUid = $request->get('clientUid');
  642. $cptCode = $request->get('code');
  643. if(!$clientUid || !$cptCode) return $this->fail('Error');
  644. $client = Client::where('uid', $clientUid)->first();
  645. $notes = $client->notes;
  646. $isCptSubmitted = false;
  647. foreach($notes as $note){
  648. $noteClaims = $note->claims;
  649. foreach($noteClaims as $noteClaim){
  650. foreach($noteClaim->lines as $claimLine){
  651. if($claimLine->cpt == $cptCode){
  652. if($noteClaim->status == 'SUBMITTED'){
  653. $isCptSubmitted = true;
  654. }
  655. }
  656. }
  657. }
  658. }
  659. if($isCptSubmitted) return $this->pass('SUBMITTED');
  660. return $this->pass('NOT_SUBMITTED');
  661. }
  662. public function surveys(Request $request, Client $patient)
  663. {
  664. $filters = $request->all();
  665. $entityTypes = Survey::ALLOWED_ENTITIES;
  666. $surveyFormsPath = resource_path(Survey::FORM_PATH);
  667. $filesInFolder = File::allFiles($surveyFormsPath);
  668. $forms = [];
  669. foreach ($filesInFolder as $path) {
  670. $file = pathinfo($path);
  671. $fileName = $file['filename'];
  672. $internalName = explode('.', $fileName)[0];
  673. $forms[] = $internalName;
  674. }
  675. $records = Survey::where('entity_uid', $patient->uid);
  676. $searchString = $request->get('string');
  677. if($searchString){
  678. $searchString = strtolower($searchString);
  679. $records = $records->where(function ($q) use ($searchString) {
  680. return $q->orWhereRaw('LOWER(title::text) ILIKE ?', ['%' . $searchString . '%'])
  681. ->orWhereRaw('LOWER(internal_name::text) ILIKE ?', ['%' . $searchString . '%'])
  682. ->orWhereRaw('survey_data ILIKE ?', ['%' . $searchString . '%']);
  683. });
  684. }
  685. $entityType = $request->get('entity_type');
  686. if($entityType){
  687. $records = $records->where('entity_type', $entityType);
  688. }
  689. $records = $records->orderBy('created_at', 'DESC')->paginate(5);
  690. return view('app.patient.surveys.list', compact('forms', 'records', 'entityTypes', 'filters', 'patient'));
  691. }
  692. public function getEntityRecords(Request $request, Client $patient)
  693. {
  694. $term = $request->get('term');
  695. $type = $request->get('type');
  696. if(!in_array($type, Survey::ALLOWED_ENTITIES)){
  697. return $this->fail('Invalid entity type');
  698. }
  699. $records = [];
  700. if($type === 'Client'){
  701. $clients = Client::query();
  702. $clients = $clients->where('is_active', true);
  703. $clients = $clients->where(function ($q) use ($term) {
  704. return $q->orWhereRaw('LOWER(name_first::text) ILIKE ?', ['%' . $term . '%'])
  705. ->orWhereRaw('LOWER(name_last::text) ILIKE ?', ['%' . $term . '%'])
  706. ->orWhereRaw('LOWER(email_address::text) ILIKE ?', ['%' . $term . '%'])
  707. ->orWhereRaw('cell_number ILIKE ?', ['%' . $term . '%']);
  708. });
  709. $clients = $clients->orderBy('name_first', 'ASC')->limit(10)->get();
  710. $clientsData = $clients->map(function($client) {
  711. return [
  712. "uid" => $client->uid,
  713. "id" => $client->id,
  714. "text" => $client->displayName(),
  715. ];
  716. });
  717. return json_encode([
  718. "results" => $clientsData
  719. ]);
  720. }
  721. return $this->pass($records);
  722. }
  723. }