NoteController.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\AppSession;
  4. use App\Models\Page;
  5. use App\Models\Point;
  6. use App\Models\Pro;
  7. use App\Models\SupplyOrder;
  8. use App\Models\Ticket;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Collection;
  11. use Illuminate\Support\Facades\Blade;
  12. use Illuminate\Support\Facades\Http;
  13. use App\Models\Note;
  14. use App\Models\Client;
  15. use App\Models\CompanyPro;
  16. use App\Models\Section;
  17. use App\Models\SectionTemplate;
  18. use App\Models\Segment;
  19. use App\Models\SegmentTemplate;
  20. use Illuminate\Support\Facades\DB;
  21. class NoteController extends Controller
  22. {
  23. public function dashboard(Request $request, Client $patient, Note $note)
  24. {
  25. $pros = $this->pros;
  26. $noteSections = $note->sections;
  27. $allSections = SectionTemplate::where('is_active', true)->get();
  28. foreach ($allSections as $section) {
  29. $section->used = false;
  30. foreach ($noteSections as $noteSection) {
  31. if ($noteSection->sectionTemplate->id === $section->id) {
  32. $section->used = true;
  33. $section->section_uid = $noteSection->uid;
  34. break;
  35. }
  36. }
  37. }
  38. // load tickets created on note->effective_date for patient
  39. $ticketsOnNote = Ticket::where('client_id', $patient->id)
  40. ->where('is_entry_error', false)
  41. ->where('note_id', $note->id)
  42. ->get();
  43. // other open tickets as of today
  44. $otherOpenTickets = Ticket::where('client_id', $patient->id)
  45. ->where('is_entry_error', false)
  46. ->where('is_open', true)
  47. ->where(function ($query) use ($note) {
  48. $query->where('note_id', '<>', $note->id)->orWhereNull('note_id'); // weird, but just the <> isn't working!
  49. })
  50. ->get();
  51. // load supplyOrders created on note->effective_date for patient
  52. $supplyOrdersOnNote = SupplyOrder::where('client_id', $patient->id)
  53. ->where('is_cancelled', false)
  54. ->where('note_id', $note->id)
  55. ->get();
  56. // other open supplyOrders as of today
  57. $otherOpenSupplyOrders = SupplyOrder::where('client_id', $patient->id)
  58. ->where('is_cancelled', false)
  59. ->where('note_id', '<>', $note->id)
  60. ->get();
  61. $templates = $this->filterClientDocuments(get_doc_templates());
  62. $companyProIDs = DB::select('SELECT company_pro_id FROM company_pro_document WHERE related_client_id = ?', [$patient->id]);
  63. $companyProIDInts = [];
  64. foreach($companyProIDs as $cpId){
  65. $companyProIDInts[] = $cpId->company_pro_id;
  66. }
  67. $companyPros = CompanyPro::whereIn('id', $companyProIDInts)->get();
  68. return view('app.patient.note.dashboard', compact('patient', 'note',
  69. 'allSections',
  70. 'ticketsOnNote', 'otherOpenTickets',
  71. 'companyPros',
  72. 'supplyOrdersOnNote', 'otherOpenSupplyOrders', 'templates'));
  73. }
  74. private function filterClientDocuments($documents){
  75. $clientDocs = [];
  76. foreach($documents as $doc){
  77. if(starts_with($doc['name'], 'client_')){
  78. array_push($clientDocs, $doc);
  79. }
  80. }
  81. return $clientDocs;
  82. }
  83. public function signConfirmation(Request $request, Client $patient, Note $note) {
  84. return view('app.patient.note.sign-confirmation', compact('patient', 'note'));
  85. }
  86. public function renderNote($noteUid, Request $request)
  87. {
  88. $note = Note::where('uid', $noteUid)->first();
  89. $client = Client::where('id', $note->client_id)->first();
  90. return view('client/note', compact('note', 'client'));
  91. }
  92. public function sectionView(Request $request, Client $patient, Note $note, Section $section, $form, Page $page = null) {
  93. return view("app.patient.page-sections." . $section->sectionTemplate->internal_name . "." . $form,
  94. compact('patient', 'note', 'section', 'page'));
  95. }
  96. public function print(Request $request, Client $patient, Note $note) {
  97. if($note->visitTemplate) {
  98. return view("app.patient.note.print.print", compact('patient', 'note'));
  99. }
  100. return view("app.patient.note.print.print-legacy", compact('patient', 'note'));
  101. }
  102. public function resolve(Request $request, Client $patient, Note $note) {
  103. return view("app.patient.note.resolve", compact('patient', 'note'));
  104. }
  105. public function getHtmlForSegment($segmentUid, $sessionKey){
  106. $summaryHtml = '';
  107. $editHtml = '';
  108. try {
  109. $performer = AppSession::where('session_key', $sessionKey)->first();
  110. if (!$performer || !$performer->is_active) {
  111. return response()->json([
  112. 'success' => false,
  113. 'message' => 'Invalid session key'
  114. ]);
  115. }
  116. $pro = $performer->pro;
  117. $segment = Segment::where('uid', $segmentUid)->first();
  118. $recalculatedHtml = $segment->getRecalculatedHtml($performer, $sessionKey);
  119. } catch (\Throwable $e) {
  120. return response()->json([
  121. 'success' => false,
  122. 'message' => $e->getMessage()
  123. ]);
  124. }
  125. return response()->json([
  126. 'success'=>true,
  127. 'summaryHtml' => $recalculatedHtml['summaryHtml'],
  128. 'editHtml' => $recalculatedHtml['editHtml'],
  129. ]);
  130. }
  131. // JAVA ONLY
  132. // ... if hcpProId is passed, get from request
  133. public function getDefaultValueForSection($patientID, $sectionTemplateID)
  134. {
  135. $contentData = [];
  136. $summaryHtml = '';
  137. $patient = Client::where('id', $patientID)->first();
  138. $sectionTemplate = SectionTemplate::where('id', $sectionTemplateID)->first();
  139. if ($sectionTemplate->is_canvas) {
  140. if (file_exists(resource_path('views/app/patient/canvas-sections/' . $sectionTemplate->internal_name . '/default.php'))) {
  141. // for canvas section where we have pro mapped data, use hcpProId
  142. $hcpPro = null;
  143. if(\request()->input('hcpProUid')) {
  144. $hcpPro = Pro::where('uid', \request()->input('hcpProUid'))->first();
  145. }
  146. $note = null;
  147. if(\request()->input('noteUid')) {
  148. $note = Note::where('uid', \request()->input('noteUid'))->first();
  149. }
  150. // default should simply assign to $contentData
  151. include(resource_path('views/app/patient/canvas-sections/' . $sectionTemplate->internal_name . '/default.php'));
  152. ob_start();
  153. include(resource_path('views/app/patient/canvas-sections/' . $sectionTemplate->internal_name . '/summary.php'));
  154. $summaryHtml = ob_get_contents();
  155. ob_end_clean();
  156. }
  157. } else {
  158. if (file_exists(storage_path('sections/' . $sectionTemplate->internal_name . '/default.php'))) {
  159. // default should simply assign to $contentData and $summaryHtml as needed
  160. include(storage_path('sections/' . $sectionTemplate->internal_name . '/default.php'));
  161. }
  162. }
  163. return [
  164. 'contentData' => $contentData,
  165. 'summaryHtml' => $summaryHtml
  166. ];
  167. }
  168. public function processFormSubmit(Request $request)
  169. {
  170. // guest_access_code, section_uid, data
  171. // REMEMBER, if this is an hcp scoped canvas section, data will not be the ENTIRE node...
  172. // ... it will only be the hcp scope within that node
  173. $guestAccessCode = $request->get('guest_access_code');
  174. if($guestAccessCode){
  175. //its from guest
  176. $sectionForToken = Section::where('guest_access_code', $guestAccessCode)->first();
  177. abort_if(!$sectionForToken, 401, 'Unauthorized');
  178. }else{
  179. //its not from guest so require performer
  180. abort_if(!$this->performer, 401, 'Unauthorized');
  181. abort_if(!$this->performer->is_active, 401, 'Unauthorized');
  182. }
  183. // TODO require
  184. $section_uid = $request->get('section_uid');
  185. $section = Section::where('uid', $section_uid)->first();
  186. $note = Note::where('id', $section->note_id)->first();
  187. $client = null;
  188. if($note){
  189. $client = Client::where('id', $note->client_id)->first();
  190. }else{
  191. $client = Client::where('id', $section->client_id)->first();
  192. }
  193. $patient = $client;
  194. $sectionTemplate = SectionTemplate::where('id', $section->section_template_id)->first();
  195. $newContentData = [];
  196. $newSummaryHtml = "";
  197. $sectionInternalName = $sectionTemplate->internal_name;
  198. if ($sectionTemplate->is_canvas) {
  199. $key = $sectionTemplate->internal_name;
  200. // Because sectionTemplate is_canvas, any update to the section will require updating the canvas.
  201. // ... there are TWO possibilities.
  202. // ...... 1) if !is_hcp_scoped, then what comes in from the section simply swaps out the entire node
  203. // ...... 2) if is_hcp_scoped, then what comes in from the section is incoprorated into that scope in the node
  204. $newCanvasNodeData = null;
  205. if($sectionTemplate->is_hcp_scoped){
  206. $currentCanvasData = json_decode($client->canvas_data, true);
  207. $currentCanvasDataNode = isset($currentCanvasData[$key]) ? $currentCanvasData[$key] : [];
  208. $currentCanvasDataNode[$note->hcpPro->id] = json_decode($request->get('data'), true);
  209. $newCanvasNodeData = json_encode($currentCanvasDataNode);
  210. }else{
  211. $newCanvasNodeData = $request->get('data');
  212. }
  213. $response = null;
  214. $data = [
  215. 'uid' => $client->uid,
  216. 'noteUid'=> $note?$note->uid:null,
  217. 'key' => $key,
  218. 'data' => $newCanvasNodeData
  219. ];
  220. $response = $this->calljava($request, '/client/updateCanvasData', $data, $guestAccessCode);
  221. //TODO: handle $response->success == false
  222. if($note){
  223. $client = Client::where('id', $note->client_id)->first();
  224. }else{
  225. $client = Client::where('id', $section->client_id)->first();
  226. }
  227. $patient = $client;
  228. if (file_exists(resource_path("views/app/patient/canvas-sections/{$sectionInternalName}/processor.php"))) {
  229. include(resource_path("views/app/patient/canvas-sections/{$sectionInternalName}/processor.php"));
  230. } else {
  231. $newContentData = json_decode($request->get('data'), true);
  232. }
  233. ob_start();
  234. include(resource_path("views/app/patient/canvas-sections/{$sectionInternalName}/summary.php"));
  235. $newSummaryHtml = ob_get_contents();
  236. ob_end_clean();
  237. // TODO call Java to update the canvas
  238. } elseif (file_exists(storage_path('sections/' . $sectionTemplate->internal_name . '/form.blade.php'))) {
  239. include(storage_path('sections/' . $sectionTemplate->internal_name . '/processor.php'));
  240. ob_start();
  241. include(storage_path('sections/' . $sectionTemplate->internal_name . '/summary.php'));
  242. $newSummaryHtml = ob_get_contents();
  243. ob_end_clean();
  244. } else {
  245. $newContentData = json_decode($request->get('data'), true);
  246. if (isset($newContentData['value'])) {
  247. $newSummaryHtml = $newContentData['value'];
  248. }
  249. }
  250. $response = null;
  251. $data = [
  252. 'uid' => $section->uid,
  253. 'contentData' => json_encode($newContentData),
  254. 'summaryHtml' => $newSummaryHtml
  255. ];
  256. $response = $this->calljava($request, '/section/update', $data, $guestAccessCode);
  257. return [
  258. 'success' => $response['success'],
  259. 'newSummaryHtml' => $newSummaryHtml
  260. ];
  261. }
  262. // edit hpi (structured)
  263. public function editHPI(Note $note, Point $point) {
  264. return view('app.patient.note.edit-hpi', compact('note', 'point'));
  265. }
  266. public function hpiLog(Note $note, Point $point) {
  267. return view('app.patient.note.hpi-log', compact('note', 'point'));
  268. }
  269. // review log
  270. public function reviewLog(Point $point) {
  271. return view('app.patient.note.review-log', compact('point'));
  272. }
  273. // plan log
  274. public function planLog(Point $point) {
  275. return view('app.patient.note.plan-log', compact('point'));
  276. }
  277. // print/pdf
  278. public function downloadAsPdf(Request $request, Note $note) {
  279. $patient = $note->client;
  280. if($request->input('html')) {
  281. return view('app.patient.note.pdf', compact('note', 'patient'));
  282. }
  283. else {
  284. $pdf = \PDF::loadView('app.patient.note.pdf', compact('note', 'patient'));
  285. return $pdf->stream($note->created_at .'_' . 'note.pdf');
  286. }
  287. }
  288. public function generateCC(Request $request, Note $note) {
  289. $client = $note->client;
  290. return view('app.patient.segment-templates.chief_complaint.generate', compact('note', 'client'));
  291. }
  292. public function segmentSummary(Request $request, Segment $segment) {
  293. return '<div class="mrv-content border-top px-3 pt-2 mt-3">' . @$segment->summary_html . '</div>';
  294. }
  295. public function mrvSummary(Request $request, Note $note) {
  296. return view('app.patient.segment-templates.medrisk_vigilence.summary', [
  297. 'note' => $note,
  298. 'patient' => $note->client,
  299. 'segment' => $note->coreSegment
  300. ]);
  301. }
  302. public function chartSegmentView(Request $request, Client $patient, $segmentInternalName, $view) {
  303. return view("app.patient.segment-templates.{$segmentInternalName}.{$view}", [
  304. 'patient' => $patient,
  305. 'note' => $patient->coreNote,
  306. 'segmentInternalName' => $segmentInternalName,
  307. 'closeOnSave' => true
  308. ]);
  309. }
  310. public function noteSegmentView(Request $request, Client $patient, Note $note, Segment $segment, $segmentInternalName, $view) {
  311. return view("app.patient.segment-templates.{$segmentInternalName}.{$view}", [
  312. 'patient' => $patient,
  313. 'note' => $note,
  314. 'segment' => $segment,
  315. 'segmentInternalName' => $segmentInternalName
  316. ]);
  317. }
  318. public function noteSegmentViewByName(Request $request, Note $note, $segmentInternalName, $view) {
  319. return view("app.patient.segment-templates.{$segmentInternalName}.{$view}", [
  320. 'patient' => $note->client,
  321. 'note' => $note,
  322. 'segment' => $note->coreSegment,
  323. 'segmentInternalName' => $segmentInternalName
  324. ]);
  325. }
  326. public function moduleView(Request $request, Note $note, $segmentInternalName, $view) {
  327. return view("app.patient.modules.{$segmentInternalName}.{$view}", [
  328. 'patient' => $note->client,
  329. 'note' => $note,
  330. 'segment' => $note->coreSegment,
  331. 'segmentInternalName' => $segmentInternalName
  332. ]);
  333. }
  334. public function rhsSidebar(Request $request, Client $patient, Note $note) {
  335. return view('app.patient.note.rhs-sidebar', compact('patient', 'note'));
  336. }
  337. public function medicationsCenter(Request $request, Client $patient, Note $note) {
  338. $points = $this->getPointsForWizard('MEDICATION', $patient, $note);
  339. list($medications, $counts) = $this->groupByState($points);
  340. return view('app.patient.medications-center', compact('patient', 'note', 'medications', 'counts'));
  341. }
  342. public function medicationsAddMultiPreexisting(Request $request, Note $note) {
  343. return view('app.patient.medications-add-multi-preexisting', compact('note'));
  344. }
  345. public function medicationsReconcile(Request $request, Client $patient, Note $note) {
  346. return view('app.patient.medications-reconcile', compact('patient', 'note'));
  347. }
  348. public function problemsQuickAdd(Request $request, Client $patient, Note $note) {
  349. return view('app.patient.problems-quick-add', compact('patient', 'note'));
  350. }
  351. public function problemsCenter(Request $request, Client $patient, Note $note) {
  352. $points = $this->getPointsForWizard('PROBLEM', $patient, $note);
  353. list($problems, $counts) = $this->groupByState($points);
  354. return view('app.patient.problems-center', compact('patient', 'note', 'problems', 'counts'));
  355. }
  356. public function goalsCenter(Request $request, Client $patient, Note $note) {
  357. $points = $this->getPointsForWizard('GOAL', $patient, $note, 'goal');
  358. list($goals, $counts) = $this->groupByState($points);
  359. return view('app.patient.goals-center', compact('patient', 'note', 'goals', 'counts'));
  360. }
  361. public function allergiesCenter(Request $request, Client $patient, Note $note) {
  362. $points = $this->getPointsForWizard('ALLERGY', $patient, $note);
  363. list($allergies, $counts) = $this->groupByState($points);
  364. return view('app.patient.allergies-center', compact('patient', 'note', 'allergies', 'counts'));
  365. }
  366. public function careteamCenter(Request $request, Client $patient, Note $note) {
  367. $points = $this->getPointsForWizard('CARE_TEAM_MEMBER', $patient, $note);
  368. list($careTeamMembers, $counts) = $this->groupByState($points);
  369. return view('app.patient.careteam-center', compact('patient', 'note', 'careTeamMembers', 'counts'));
  370. }
  371. private function getPointsForWizard($_category, $_patient, $_note, $_sortKey = 'name') {
  372. $query = "
  373. SELECT p.id,
  374. p.uid,
  375. p.data,
  376. p.is_removed,
  377. p.is_removed_due_to_entry_error,
  378. p.added_in_note_id,
  379. p.addition_reason_category,
  380. p.removed_in_note_id,
  381. p.removal_reason_category,
  382. p.added_in_note_uid as added_note_uid,
  383. p.added_in_note_effective_date as added_on,
  384. p.removed_in_note_uid as removed_note_uid,
  385. p.removed_in_note_effective_dateest as removed_on,
  386. np.uid as note_point_uid,
  387. p.last_child_review_point_id,
  388. p.last_child_review_point_scoped_note_id,
  389. p.last_child_plan_point_id,
  390. p.last_child_plan_point_scoped_note_id,
  391. p.last_child_review_effective_date,
  392. p.last_child_plan_effective_date,
  393. p.last_child_review_data,
  394. p.last_child_plan_data,
  395. (p.last_child_review_creator_pro_first_name || ' ' || p.last_child_review_creator_pro_last_name) as last_child_review_creator,
  396. (p.last_child_plan_creator_pro_first_name || ' ' || p.last_child_plan_creator_pro_last_name) as last_child_plan_creator,
  397. p.last_child_review_point_scoped_note_uid as last_child_review_note_uid,
  398. p.last_child_plan_point_scoped_note_uid as last_child_plan_note_uid
  399. FROM point p
  400. left join note_point np on p.id = np.point_id and np.note_id = {$_note->id} and np.is_active = TRUE
  401. WHERE p.client_id = {$_patient->id} AND p.category = '{$_category}'
  402. ORDER BY ((p.data)::json->'{$_sortKey}')::text
  403. ";
  404. return DB::select($query);
  405. }
  406. private function groupByState($points) {
  407. $pointsByType = [
  408. "ACTIVE" => [],
  409. "HISTORIC" => [],
  410. "ENTRY_ERROR" => [],
  411. ];
  412. foreach ($points as $point) {
  413. if ($point->data) {
  414. $point->data = json_decode($point->data);
  415. }
  416. if(!$point->is_removed) {
  417. $point->state = "ACTIVE";
  418. $pointsByType["ACTIVE"][] = $point;
  419. }
  420. elseif($point->is_removed) {
  421. if(!$point->is_removed_due_to_entry_error) {
  422. $point->state = "HISTORIC";
  423. $pointsByType["HISTORIC"][] = $point;
  424. }
  425. else {
  426. $point->state = "ENTRY_ERROR";
  427. $pointsByType["ENTRY_ERROR"][] = $point;
  428. }
  429. }
  430. }
  431. return [
  432. array_merge($pointsByType["ACTIVE"], $pointsByType["HISTORIC"], $pointsByType["ENTRY_ERROR"]),
  433. [
  434. "ACTIVE" => count($pointsByType["ACTIVE"]),
  435. "HISTORIC" => count($pointsByType["HISTORIC"]),
  436. "ENTRY_ERROR" => count($pointsByType["ENTRY_ERROR"]),
  437. ]
  438. ];
  439. }
  440. public function supplementsCenter(Request $request, Client $patient, Note $note) {
  441. return view('app.patient.supplements-center', compact('patient', 'note'));
  442. }
  443. public function supplementsReconcile(Request $request, Client $patient, Note $note) {
  444. return view('app.patient.supplements-reconcile', compact('patient', 'note'));
  445. }
  446. public function nutritionCenter(Request $request, Client $patient, Note $note) {
  447. return view('app.patient.nutrition-center', compact('patient', 'note'));
  448. }
  449. public function exerciseCenter(Request $request, Client $patient, Note $note) {
  450. return view('app.patient.exercise-center', compact('patient', 'note'));
  451. }
  452. public function behaviorCenter(Request $request, Client $patient, Note $note) {
  453. return view('app.patient.behavior-center', compact('patient', 'note'));
  454. }
  455. public function ccmAgreement(Request $request, Note $note) {
  456. return view('app.patient.note.ccm-agreement', compact('note'));
  457. }
  458. public function rpmAgreement(Request $request, Note $note) {
  459. return view('app.patient.note.rpm-agreement', compact('note'));
  460. }
  461. // TODO move to utility
  462. private function callJava($request, $endPoint, $data, $guestAccessCode = null)
  463. {
  464. $url = config('stag.backendUrl') . $endPoint;
  465. $response = Http::asForm()
  466. ->withHeaders([
  467. 'sessionKey' => $request->cookie('sessionKey'),
  468. 'guestAccessCode' => $guestAccessCode
  469. ])
  470. ->post($url, $data)
  471. ->json();
  472. return $response;
  473. }
  474. }