NoteController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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\Section;
  16. use App\Models\SectionTemplate;
  17. use App\Models\Segment;
  18. use App\Models\SegmentTemplate;
  19. class NoteController extends Controller
  20. {
  21. public function dashboard(Request $request, Client $patient, Note $note)
  22. {
  23. $pros = $this->pros;
  24. $noteSections = $note->sections;
  25. $allSections = SectionTemplate::where('is_active', true)->get();
  26. foreach ($allSections as $section) {
  27. $section->used = false;
  28. foreach ($noteSections as $noteSection) {
  29. if ($noteSection->sectionTemplate->id === $section->id) {
  30. $section->used = true;
  31. $section->section_uid = $noteSection->uid;
  32. break;
  33. }
  34. }
  35. }
  36. // load tickets created on note->effective_date for patient
  37. $ticketsOnNote = Ticket::where('client_id', $patient->id)
  38. ->where('is_entry_error', false)
  39. ->where('note_id', $note->id)
  40. ->get();
  41. // other open tickets as of today
  42. $otherOpenTickets = Ticket::where('client_id', $patient->id)
  43. ->where('is_entry_error', false)
  44. ->where('is_open', true)
  45. ->where(function ($query) use ($note) {
  46. $query->where('note_id', '<>', $note->id)->orWhereNull('note_id'); // weird, but just the <> isn't working!
  47. })
  48. ->get();
  49. // load supplyOrders created on note->effective_date for patient
  50. $supplyOrdersOnNote = SupplyOrder::where('client_id', $patient->id)
  51. ->where('is_cancelled', false)
  52. ->where('note_id', $note->id)
  53. ->get();
  54. // other open supplyOrders as of today
  55. $otherOpenSupplyOrders = SupplyOrder::where('client_id', $patient->id)
  56. ->where('is_cancelled', false)
  57. ->where('note_id', '<>', $note->id)
  58. ->get();
  59. $templates = get_doc_templates();
  60. return view('app.patient.note.dashboard', compact('patient', 'note',
  61. 'allSections',
  62. 'ticketsOnNote', 'otherOpenTickets',
  63. 'supplyOrdersOnNote', 'otherOpenSupplyOrders', 'templates'));
  64. }
  65. public function signConfirmation(Request $request, Client $patient, Note $note) {
  66. return view('app.patient.note.sign-confirmation', compact('patient', 'note'));
  67. }
  68. public function renderNote($noteUid, Request $request)
  69. {
  70. $note = Note::where('uid', $noteUid)->first();
  71. $client = Client::where('id', $note->client_id)->first();
  72. return view('client/note', compact('note', 'client'));
  73. }
  74. public function sectionView(Request $request, Client $patient, Note $note, Section $section, $form, Page $page = null) {
  75. return view("app.patient.page-sections." . $section->sectionTemplate->internal_name . "." . $form,
  76. compact('patient', 'note', 'section', 'page'));
  77. }
  78. public function print(Request $request, Client $patient, Note $note) {
  79. return view("app.patient.note.print", compact('patient', 'note'));
  80. }
  81. public function getHtmlForSegment($segmentUid, $sessionKey){
  82. $summaryHtml = '';
  83. $editHtml = '';
  84. try {
  85. $performer = AppSession::where('session_key', $sessionKey)->first();
  86. if (!$performer || !$performer->is_active) {
  87. return response()->json([
  88. 'success' => false,
  89. 'message' => 'Invalid session key'
  90. ]);
  91. }
  92. $pro = $performer->pro;
  93. $segment = Segment::where('uid', $segmentUid)->first();
  94. $recalculatedHtml = $segment->getRecalculatedHtml($performer, $sessionKey);
  95. } catch (\Throwable $e) {
  96. return response()->json([
  97. 'success' => false,
  98. 'message' => $e->getMessage()
  99. ]);
  100. }
  101. return response()->json([
  102. 'success'=>true,
  103. 'summaryHtml' => $recalculatedHtml['summaryHtml'],
  104. 'editHtml' => $recalculatedHtml['editHtml'],
  105. ]);
  106. }
  107. // JAVA ONLY
  108. // ... if hcpProId is passed, get from request
  109. public function getDefaultValueForSection($patientID, $sectionTemplateID)
  110. {
  111. $contentData = [];
  112. $summaryHtml = '';
  113. $patient = Client::where('id', $patientID)->first();
  114. $sectionTemplate = SectionTemplate::where('id', $sectionTemplateID)->first();
  115. if ($sectionTemplate->is_canvas) {
  116. if (file_exists(resource_path('views/app/patient/canvas-sections/' . $sectionTemplate->internal_name . '/default.php'))) {
  117. // for canvas section where we have pro mapped data, use hcpProId
  118. $hcpPro = null;
  119. if(\request()->input('hcpProUid')) {
  120. $hcpPro = Pro::where('uid', \request()->input('hcpProUid'))->first();
  121. }
  122. $note = null;
  123. if(\request()->input('noteUid')) {
  124. $note = Note::where('uid', \request()->input('noteUid'))->first();
  125. }
  126. // default should simply assign to $contentData
  127. include(resource_path('views/app/patient/canvas-sections/' . $sectionTemplate->internal_name . '/default.php'));
  128. ob_start();
  129. include(resource_path('views/app/patient/canvas-sections/' . $sectionTemplate->internal_name . '/summary.php'));
  130. $summaryHtml = ob_get_contents();
  131. ob_end_clean();
  132. }
  133. } else {
  134. if (file_exists(storage_path('sections/' . $sectionTemplate->internal_name . '/default.php'))) {
  135. // default should simply assign to $contentData and $summaryHtml as needed
  136. include(storage_path('sections/' . $sectionTemplate->internal_name . '/default.php'));
  137. }
  138. }
  139. return [
  140. 'contentData' => $contentData,
  141. 'summaryHtml' => $summaryHtml
  142. ];
  143. }
  144. public function processFormSubmit(Request $request)
  145. {
  146. // guest_access_code, section_uid, data
  147. // REMEMBER, if this is an hcp scoped canvas section, data will not be the ENTIRE node...
  148. // ... it will only be the hcp scope within that node
  149. $guestAccessCode = $request->get('guest_access_code');
  150. if($guestAccessCode){
  151. //its from guest
  152. $sectionForToken = Section::where('guest_access_code', $guestAccessCode)->first();
  153. abort_if(!$sectionForToken, 401, 'Unauthorized');
  154. }else{
  155. //its not from guest so require performer
  156. abort_if(!$this->performer, 401, 'Unauthorized');
  157. abort_if(!$this->performer->is_active, 401, 'Unauthorized');
  158. }
  159. // TODO require
  160. $section_uid = $request->get('section_uid');
  161. $section = Section::where('uid', $section_uid)->first();
  162. $note = Note::where('id', $section->note_id)->first();
  163. $client = null;
  164. if($note){
  165. $client = Client::where('id', $note->client_id)->first();
  166. }else{
  167. $client = Client::where('id', $section->client_id)->first();
  168. }
  169. $patient = $client;
  170. $sectionTemplate = SectionTemplate::where('id', $section->section_template_id)->first();
  171. $newContentData = [];
  172. $newSummaryHtml = "";
  173. $sectionInternalName = $sectionTemplate->internal_name;
  174. if ($sectionTemplate->is_canvas) {
  175. $key = $sectionTemplate->internal_name;
  176. // Because sectionTemplate is_canvas, any update to the section will require updating the canvas.
  177. // ... there are TWO possibilities.
  178. // ...... 1) if !is_hcp_scoped, then what comes in from the section simply swaps out the entire node
  179. // ...... 2) if is_hcp_scoped, then what comes in from the section is incoprorated into that scope in the node
  180. $newCanvasNodeData = null;
  181. if($sectionTemplate->is_hcp_scoped){
  182. $currentCanvasData = json_decode($client->canvas_data, true);
  183. $currentCanvasDataNode = isset($currentCanvasData[$key]) ? $currentCanvasData[$key] : [];
  184. $currentCanvasDataNode[$note->hcpPro->id] = json_decode($request->get('data'), true);
  185. $newCanvasNodeData = json_encode($currentCanvasDataNode);
  186. }else{
  187. $newCanvasNodeData = $request->get('data');
  188. }
  189. $response = null;
  190. $data = [
  191. 'uid' => $client->uid,
  192. 'noteUid'=> $note?$note->uid:null,
  193. 'key' => $key,
  194. 'data' => $newCanvasNodeData
  195. ];
  196. $response = $this->calljava($request, '/client/updateCanvasData', $data, $guestAccessCode);
  197. //TODO: handle $response->success == false
  198. if($note){
  199. $client = Client::where('id', $note->client_id)->first();
  200. }else{
  201. $client = Client::where('id', $section->client_id)->first();
  202. }
  203. $patient = $client;
  204. if (file_exists(resource_path("views/app/patient/canvas-sections/{$sectionInternalName}/processor.php"))) {
  205. include(resource_path("views/app/patient/canvas-sections/{$sectionInternalName}/processor.php"));
  206. } else {
  207. $newContentData = json_decode($request->get('data'), true);
  208. }
  209. ob_start();
  210. include(resource_path("views/app/patient/canvas-sections/{$sectionInternalName}/summary.php"));
  211. $newSummaryHtml = ob_get_contents();
  212. ob_end_clean();
  213. // TODO call Java to update the canvas
  214. } elseif (file_exists(storage_path('sections/' . $sectionTemplate->internal_name . '/form.blade.php'))) {
  215. include(storage_path('sections/' . $sectionTemplate->internal_name . '/processor.php'));
  216. ob_start();
  217. include(storage_path('sections/' . $sectionTemplate->internal_name . '/summary.php'));
  218. $newSummaryHtml = ob_get_contents();
  219. ob_end_clean();
  220. } else {
  221. $newContentData = json_decode($request->get('data'), true);
  222. if (isset($newContentData['value'])) {
  223. $newSummaryHtml = $newContentData['value'];
  224. }
  225. }
  226. $response = null;
  227. $data = [
  228. 'uid' => $section->uid,
  229. 'contentData' => json_encode($newContentData),
  230. 'summaryHtml' => $newSummaryHtml
  231. ];
  232. $response = $this->calljava($request, '/section/update', $data, $guestAccessCode);
  233. return [
  234. 'success' => $response['success'],
  235. 'newSummaryHtml' => $newSummaryHtml
  236. ];
  237. }
  238. // edit hpi (structured)
  239. public function editHPI(Note $note, Point $point) {
  240. return view('app.patient.note.edit-hpi', compact('note', 'point'));
  241. }
  242. public function hpiLog(Note $note, Point $point) {
  243. return view('app.patient.note.hpi-log', compact('note', 'point'));
  244. }
  245. // review log
  246. public function reviewLog(Point $point) {
  247. return view('app.patient.note.review-log', compact('point'));
  248. }
  249. // plan log
  250. public function planLog(Point $point) {
  251. return view('app.patient.note.plan-log', compact('point'));
  252. }
  253. // print/pdf
  254. public function downloadAsPdf(Request $request, Note $note) {
  255. $patient = $note->client;
  256. if($request->input('html')) {
  257. return view('app.patient.note.pdf', compact('note', 'patient'));
  258. }
  259. else {
  260. $pdf = \PDF::loadView('app.patient.note.pdf', compact('note', 'patient'));
  261. return $pdf->stream($note->created_at .'_' . 'note.pdf');
  262. }
  263. }
  264. public function generateCC(Request $request, Note $note) {
  265. $client = $note->client;
  266. return view('app.patient.segment-templates.chief_complaint.generate', compact('note', 'client'));
  267. }
  268. public function segmentSummary(Request $request, Segment $segment) {
  269. return '<div class="mrv-content border-top px-3 pt-2 mt-3">' . @$segment->summary_html . '</div>';
  270. }
  271. public function mrvSummary(Request $request, Note $note) {
  272. return view('app.patient.segment-templates.medrisk_vigilence.summary', [
  273. 'note' => $note,
  274. 'patient' => $note->client,
  275. 'segment' => $note->coreSegment
  276. ]);
  277. }
  278. public function chartSegmentView(Request $request, Client $patient, $segmentInternalName, $view) {
  279. return view("app.patient.segment-templates.{$segmentInternalName}.{$view}", [
  280. 'patient' => $patient,
  281. 'note' => $patient->coreNote,
  282. 'segmentInternalName' => $segmentInternalName,
  283. 'closeOnSave' => true
  284. ]);
  285. }
  286. public function noteSegmentView(Request $request, Client $patient, Note $note, Segment $segment, $segmentInternalName, $view) {
  287. return view("app.patient.segment-templates.{$segmentInternalName}.{$view}", [
  288. 'patient' => $patient,
  289. 'note' => $note,
  290. 'segment' => $segment,
  291. 'segmentInternalName' => $segmentInternalName
  292. ]);
  293. }
  294. public function noteSegmentViewByName(Request $request, Note $note, $segmentInternalName, $view) {
  295. return view("app.patient.segment-templates.{$segmentInternalName}.{$view}", [
  296. 'patient' => $note->client,
  297. 'note' => $note,
  298. 'segment' => $note->coreSegment,
  299. 'segmentInternalName' => $segmentInternalName
  300. ]);
  301. }
  302. public function rhsSidebar(Request $request, Client $patient, Note $note) {
  303. return view('app.patient.note.rhs-sidebar', compact('patient', 'note'));
  304. }
  305. public function medicationsCenter(Request $request, Client $patient, Note $note) {
  306. return view('app.patient.medications-center', compact('patient', 'note'));
  307. }
  308. public function medicationsReconcile(Request $request, Client $patient, Note $note) {
  309. return view('app.patient.medications-reconcile', compact('patient', 'note'));
  310. }
  311. public function problemsQuickAdd(Request $request, Client $patient, Note $note) {
  312. return view('app.patient.problems-quick-add', compact('patient', 'note'));
  313. }
  314. public function problemsCenter(Request $request, Client $patient, Note $note) {
  315. return view('app.patient.problems-center', compact('patient', 'note'));
  316. }
  317. public function goalsCenter(Request $request, Client $patient, Note $note) {
  318. return view('app.patient.goals-center', compact('patient', 'note'));
  319. }
  320. public function allergiesCenter(Request $request, Client $patient, Note $note) {
  321. return view('app.patient.allergies-center', compact('patient', 'note'));
  322. }
  323. public function careteamCenter(Request $request, Client $patient, Note $note) {
  324. return view('app.patient.careteam-center', compact('patient', 'note'));
  325. }
  326. public function supplementsCenter(Request $request, Client $patient, Note $note) {
  327. return view('app.patient.supplements-center', compact('patient', 'note'));
  328. }
  329. public function supplementsReconcile(Request $request, Client $patient, Note $note) {
  330. return view('app.patient.supplements-reconcile', compact('patient', 'note'));
  331. }
  332. public function nutritionCenter(Request $request, Client $patient, Note $note) {
  333. return view('app.patient.nutrition-center', compact('patient', 'note'));
  334. }
  335. public function exerciseCenter(Request $request, Client $patient, Note $note) {
  336. return view('app.patient.exercise-center', compact('patient', 'note'));
  337. }
  338. public function behaviorCenter(Request $request, Client $patient, Note $note) {
  339. return view('app.patient.behavior-center', compact('patient', 'note'));
  340. }
  341. public function ccmAgreement(Request $request, Note $note) {
  342. return view('app.patient.note.ccm-agreement', compact('note'));
  343. }
  344. public function rpmAgreement(Request $request, Note $note) {
  345. return view('app.patient.note.rpm-agreement', compact('note'));
  346. }
  347. // TODO move to utility
  348. private function callJava($request, $endPoint, $data, $guestAccessCode = null)
  349. {
  350. $url = config('stag.backendUrl') . $endPoint;
  351. $response = Http::asForm()
  352. ->withHeaders([
  353. 'sessionKey' => $request->cookie('sessionKey'),
  354. 'guestAccessCode' => $guestAccessCode
  355. ])
  356. ->post($url, $data)
  357. ->json();
  358. return $response;
  359. }
  360. }