NoteController.php 11 KB

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