NoteController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. return view('app.patient.note.dashboard', compact('patient', 'note',
  60. 'allSections',
  61. 'ticketsOnNote', 'otherOpenTickets',
  62. 'supplyOrdersOnNote', 'otherOpenSupplyOrders'));
  63. }
  64. public function signConfirmation(Request $request, Client $patient, Note $note) {
  65. return view('app.patient.note.sign-confirmation', compact('patient', 'note'));
  66. }
  67. public function renderNote($noteUid, Request $request)
  68. {
  69. $note = Note::where('uid', $noteUid)->first();
  70. $client = Client::where('id', $note->client_id)->first();
  71. return view('client/note', compact('note', 'client'));
  72. }
  73. public function sectionView(Request $request, Client $patient, Note $note, Section $section, $form, Page $page = null) {
  74. return view("app.patient.page-sections." . $section->sectionTemplate->internal_name . "." . $form,
  75. compact('patient', 'note', 'section', 'page'));
  76. }
  77. public function getHtmlForSegment($segmentUid, $sessionKey){
  78. $performer = AppSession::where('session_key', $sessionKey)->first();
  79. if(!$performer || !$performer->is_active){
  80. return response()->json([
  81. 'success'=>false,
  82. 'message'=>'Invalid session key'
  83. ]);
  84. }
  85. $pro = $performer->pro;
  86. $segment = Segment::where('uid', $segmentUid)->first();
  87. $segmentTemplate = $segment->segmentTemplate;
  88. $note = $segment->note;
  89. $patient = $note->client;
  90. $data = compact('performer', 'pro', 'segment', 'segmentTemplate', 'note', 'patient');
  91. //try {
  92. $summaryHtml = view('app.patient.segment-templates.' . $segmentTemplate->internal_name . '/summary', $data)->render();
  93. $editHtml = view('app.patient.segment-templates.' . $segmentTemplate->internal_name . '/edit', $data)->render();
  94. // } catch (\Throwable $e) {
  95. // return response()->json([
  96. // 'success'=>false,
  97. // 'message'=>$e->getMessage()
  98. // ]);
  99. // }
  100. return response()->json([
  101. 'success'=>true,
  102. 'summaryHtml' => $summaryHtml,
  103. 'editHtml' => $editHtml
  104. ]);
  105. }
  106. // JAVA ONLY
  107. // ... if hcpProId is passed, get from request
  108. public function getDefaultValueForSection($patientID, $sectionTemplateID)
  109. {
  110. $contentData = [];
  111. $summaryHtml = '';
  112. $patient = Client::where('id', $patientID)->first();
  113. $sectionTemplate = SectionTemplate::where('id', $sectionTemplateID)->first();
  114. if ($sectionTemplate->is_canvas) {
  115. if (file_exists(resource_path('views/app/patient/canvas-sections/' . $sectionTemplate->internal_name . '/default.php'))) {
  116. // for canvas section where we have pro mapped data, use hcpProId
  117. $hcpPro = null;
  118. if(\request()->input('hcpProUid')) {
  119. $hcpPro = Pro::where('uid', \request()->input('hcpProUid'))->first();
  120. }
  121. $note = null;
  122. if(\request()->input('noteUid')) {
  123. $note = Note::where('uid', \request()->input('noteUid'))->first();
  124. }
  125. // default should simply assign to $contentData
  126. include(resource_path('views/app/patient/canvas-sections/' . $sectionTemplate->internal_name . '/default.php'));
  127. ob_start();
  128. include(resource_path('views/app/patient/canvas-sections/' . $sectionTemplate->internal_name . '/summary.php'));
  129. $summaryHtml = ob_get_contents();
  130. ob_end_clean();
  131. }
  132. } else {
  133. if (file_exists(storage_path('sections/' . $sectionTemplate->internal_name . '/default.php'))) {
  134. // default should simply assign to $contentData and $summaryHtml as needed
  135. include(storage_path('sections/' . $sectionTemplate->internal_name . '/default.php'));
  136. }
  137. }
  138. return [
  139. 'contentData' => $contentData,
  140. 'summaryHtml' => $summaryHtml
  141. ];
  142. }
  143. public function processFormSubmit(Request $request)
  144. {
  145. // guest_access_code, section_uid, data
  146. // REMEMBER, if this is an hcp scoped canvas section, data will not be the ENTIRE node...
  147. // ... it will only be the hcp scope within that node
  148. $guestAccessCode = $request->get('guest_access_code');
  149. if($guestAccessCode){
  150. //its from guest
  151. $sectionForToken = Section::where('guest_access_code', $guestAccessCode)->first();
  152. abort_if(!$sectionForToken, 401, 'Unauthorized');
  153. }else{
  154. //its not from guest so require performer
  155. abort_if(!$this->performer, 401, 'Unauthorized');
  156. abort_if(!$this->performer->is_active, 401, 'Unauthorized');
  157. }
  158. // TODO require
  159. $section_uid = $request->get('section_uid');
  160. $section = Section::where('uid', $section_uid)->first();
  161. $note = Note::where('id', $section->note_id)->first();
  162. $client = null;
  163. if($note){
  164. $client = Client::where('id', $note->client_id)->first();
  165. }else{
  166. $client = Client::where('id', $section->client_id)->first();
  167. }
  168. $patient = $client;
  169. $sectionTemplate = SectionTemplate::where('id', $section->section_template_id)->first();
  170. $newContentData = [];
  171. $newSummaryHtml = "";
  172. $sectionInternalName = $sectionTemplate->internal_name;
  173. if ($sectionTemplate->is_canvas) {
  174. $key = $sectionTemplate->internal_name;
  175. // Because sectionTemplate is_canvas, any update to the section will require updating the canvas.
  176. // ... there are TWO possibilities.
  177. // ...... 1) if !is_hcp_scoped, then what comes in from the section simply swaps out the entire node
  178. // ...... 2) if is_hcp_scoped, then what comes in from the section is incoprorated into that scope in the node
  179. $newCanvasNodeData = null;
  180. if($sectionTemplate->is_hcp_scoped){
  181. $currentCanvasData = json_decode($client->canvas_data, true);
  182. $currentCanvasDataNode = isset($currentCanvasData[$key]) ? $currentCanvasData[$key] : [];
  183. $currentCanvasDataNode[$note->hcpPro->id] = json_decode($request->get('data'), true);
  184. $newCanvasNodeData = json_encode($currentCanvasDataNode);
  185. }else{
  186. $newCanvasNodeData = $request->get('data');
  187. }
  188. $response = null;
  189. $data = [
  190. 'uid' => $client->uid,
  191. 'noteUid'=> $note?$note->uid:null,
  192. 'key' => $key,
  193. 'data' => $newCanvasNodeData
  194. ];
  195. $response = $this->calljava($request, '/client/updateCanvasData', $data, $guestAccessCode);
  196. //TODO: handle $response->success == false
  197. if($note){
  198. $client = Client::where('id', $note->client_id)->first();
  199. }else{
  200. $client = Client::where('id', $section->client_id)->first();
  201. }
  202. $patient = $client;
  203. if (file_exists(resource_path("views/app/patient/canvas-sections/{$sectionInternalName}/processor.php"))) {
  204. include(resource_path("views/app/patient/canvas-sections/{$sectionInternalName}/processor.php"));
  205. } else {
  206. $newContentData = json_decode($request->get('data'), true);
  207. }
  208. ob_start();
  209. include(resource_path("views/app/patient/canvas-sections/{$sectionInternalName}/summary.php"));
  210. $newSummaryHtml = ob_get_contents();
  211. ob_end_clean();
  212. // TODO call Java to update the canvas
  213. } elseif (file_exists(storage_path('sections/' . $sectionTemplate->internal_name . '/form.blade.php'))) {
  214. include(storage_path('sections/' . $sectionTemplate->internal_name . '/processor.php'));
  215. ob_start();
  216. include(storage_path('sections/' . $sectionTemplate->internal_name . '/summary.php'));
  217. $newSummaryHtml = ob_get_contents();
  218. ob_end_clean();
  219. } else {
  220. $newContentData = json_decode($request->get('data'), true);
  221. if (isset($newContentData['value'])) {
  222. $newSummaryHtml = $newContentData['value'];
  223. }
  224. }
  225. $response = null;
  226. $data = [
  227. 'uid' => $section->uid,
  228. 'contentData' => json_encode($newContentData),
  229. 'summaryHtml' => $newSummaryHtml
  230. ];
  231. $response = $this->calljava($request, '/section/update', $data, $guestAccessCode);
  232. return [
  233. 'success' => $response['success'],
  234. 'newSummaryHtml' => $newSummaryHtml
  235. ];
  236. }
  237. // review log
  238. public function reviewLog(Point $point) {
  239. return view('app.patient.note.review-log', compact('point'));
  240. }
  241. // plan log
  242. public function planLog(Point $point) {
  243. return view('app.patient.note.plan-log', compact('point'));
  244. }
  245. // print/pdf
  246. public function downloadAsPdf(Request $request, Note $note) {
  247. $patient = $note->client;
  248. if($request->input('html')) {
  249. return view('app.patient.note.pdf', compact('note', 'patient'));
  250. }
  251. else {
  252. $pdf = \PDF::loadView('app.patient.note.pdf', compact('note', 'patient'));
  253. return $pdf->stream($note->created_at .'_' . 'note.pdf');
  254. }
  255. }
  256. public function segmentSummary(Request $request, Segment $segment) {
  257. return '<div class="mrv-content border-top px-3 pt-2 mt-3">' . @$segment->summary_html . '</div>';
  258. }
  259. public function medicationsCenter(Request $request, Client $patient, Note $note) {
  260. return view('app.patient.medications-center', compact('patient', 'note'));
  261. }
  262. public function medicationsReconcile(Request $request, Client $patient, Note $note) {
  263. return view('app.patient.medications-reconcile', compact('patient', 'note'));
  264. }
  265. public function problemsCenter(Request $request, Client $patient, Note $note) {
  266. return view('app.patient.problems-center', compact('patient', 'note'));
  267. }
  268. public function goalsCenter(Request $request, Client $patient, Note $note) {
  269. return view('app.patient.goals-center', compact('patient', 'note'));
  270. }
  271. // TODO move to utility
  272. private function callJava($request, $endPoint, $data, $guestAccessCode = null)
  273. {
  274. $url = config('stag.backendUrl') . $endPoint;
  275. $response = Http::asForm()
  276. ->withHeaders([
  277. 'sessionKey' => $request->cookie('sessionKey'),
  278. 'guestAccessCode' => $guestAccessCode
  279. ])
  280. ->post($url, $data)
  281. ->json();
  282. return $response;
  283. }
  284. }