NoteController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\AppSession;
  4. use App\Models\Page;
  5. use App\Models\Pro;
  6. use App\Models\SupplyOrder;
  7. use App\Models\Ticket;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Collection;
  10. use Illuminate\Support\Facades\Blade;
  11. use Illuminate\Support\Facades\Http;
  12. use App\Models\Note;
  13. use App\Models\Client;
  14. use App\Models\Section;
  15. use App\Models\SectionTemplate;
  16. use App\Models\Segment;
  17. use App\Models\SegmentTemplate;
  18. class NoteController extends Controller
  19. {
  20. public function dashboard(Request $request, Client $patient, Note $note)
  21. {
  22. $pros = $this->pros;
  23. $noteSections = $note->sections;
  24. $allSections = SectionTemplate::where('is_active', true)->get();
  25. foreach ($allSections as $section) {
  26. $section->used = false;
  27. foreach ($noteSections as $noteSection) {
  28. if ($noteSection->sectionTemplate->id === $section->id) {
  29. $section->used = true;
  30. $section->section_uid = $noteSection->uid;
  31. break;
  32. }
  33. }
  34. }
  35. // load tickets created on note->effective_date for patient
  36. $ticketsOnNote = Ticket::where('client_id', $patient->id)
  37. ->where('is_entry_error', false)
  38. ->where('note_id', $note->id)
  39. ->get();
  40. // other open tickets as of today
  41. $otherOpenTickets = Ticket::where('client_id', $patient->id)
  42. ->where('is_entry_error', false)
  43. ->where('is_open', true)
  44. ->where(function ($query) use ($note) {
  45. $query->where('note_id', '<>', $note->id)->orWhereNull('note_id'); // weird, but just the <> isn't working!
  46. })
  47. ->get();
  48. // load supplyOrders created on note->effective_date for patient
  49. $supplyOrdersOnNote = SupplyOrder::where('client_id', $patient->id)
  50. ->where('is_cancelled', false)
  51. ->where('note_id', $note->id)
  52. ->get();
  53. // other open supplyOrders as of today
  54. $otherOpenSupplyOrders = SupplyOrder::where('client_id', $patient->id)
  55. ->where('is_cancelled', false)
  56. ->get();
  57. return view('app.patient.note.dashboard', compact('patient', 'note',
  58. 'allSections',
  59. 'ticketsOnNote', 'otherOpenTickets',
  60. 'supplyOrdersOnNote', 'otherOpenSupplyOrders'));
  61. }
  62. public function renderNote($noteUid, Request $request)
  63. {
  64. $note = Note::where('uid', $noteUid)->first();
  65. $client = Client::where('id', $note->client_id)->first();
  66. return view('client/note', compact('note', 'client'));
  67. }
  68. public function sectionView(Request $request, Client $patient, Note $note, Section $section, $form, Page $page = null) {
  69. return view("app.patient.page-sections." . $section->sectionTemplate->internal_name . "." . $form,
  70. compact('patient', 'note', 'section', 'page'));
  71. }
  72. public function getHtmlForSegment($segmentUid, $sessionKey){
  73. $performer = AppSession::where('session_key', $sessionKey)->first();
  74. if(!$performer || !$performer->is_active){
  75. return response()->json([
  76. 'success'=>false,
  77. 'message'=>'Invalid session key'
  78. ]);
  79. }
  80. $pro = $performer->pro;
  81. $segment = Segment::where('uid', $segmentUid)->first();
  82. $segmentTemplate = $segment->segmentTemplate;
  83. $note = $segment->note;
  84. $patient = $note->client;
  85. $data = compact('performer', 'pro', 'segment', 'segmentTemplate', 'note', 'patient');
  86. //try {
  87. $summaryHtml = view('app.patient.segment-templates.' . $segmentTemplate->internal_name . '/summary', $data)->render();
  88. $editHtml = view('app.patient.segment-templates.' . $segmentTemplate->internal_name . '/edit', $data)->render();
  89. // } catch (\Throwable $e) {
  90. // return response()->json([
  91. // 'success'=>false,
  92. // 'message'=>$e->getMessage()
  93. // ]);
  94. // }
  95. return response()->json([
  96. 'success'=>true,
  97. 'summaryHtml' => $summaryHtml,
  98. 'editHtml' => $editHtml
  99. ]);
  100. }
  101. // JAVA ONLY
  102. // ... if hcpProId is passed, get from request
  103. public function getDefaultValueForSection($patientID, $sectionTemplateID)
  104. {
  105. $contentData = [];
  106. $summaryHtml = '';
  107. $patient = Client::where('id', $patientID)->first();
  108. $sectionTemplate = SectionTemplate::where('id', $sectionTemplateID)->first();
  109. if ($sectionTemplate->is_canvas) {
  110. if (file_exists(resource_path('views/app/patient/canvas-sections/' . $sectionTemplate->internal_name . '/default.php'))) {
  111. // for canvas section where we have pro mapped data, use hcpProId
  112. $hcpPro = null;
  113. if(\request()->input('hcpProUid')) {
  114. $hcpPro = Pro::where('uid', \request()->input('hcpProUid'))->first();
  115. }
  116. $note = null;
  117. if(\request()->input('noteUid')) {
  118. $note = Note::where('uid', \request()->input('noteUid'))->first();
  119. }
  120. // default should simply assign to $contentData
  121. include(resource_path('views/app/patient/canvas-sections/' . $sectionTemplate->internal_name . '/default.php'));
  122. ob_start();
  123. include(resource_path('views/app/patient/canvas-sections/' . $sectionTemplate->internal_name . '/summary.php'));
  124. $summaryHtml = ob_get_contents();
  125. ob_end_clean();
  126. }
  127. } else {
  128. if (file_exists(storage_path('sections/' . $sectionTemplate->internal_name . '/default.php'))) {
  129. // default should simply assign to $contentData and $summaryHtml as needed
  130. include(storage_path('sections/' . $sectionTemplate->internal_name . '/default.php'));
  131. }
  132. }
  133. return [
  134. 'contentData' => $contentData,
  135. 'summaryHtml' => $summaryHtml
  136. ];
  137. }
  138. public function processFormSubmit(Request $request)
  139. {
  140. // guest_access_code, section_uid, data
  141. // REMEMBER, if this is an hcp scoped canvas section, data will not be the ENTIRE node...
  142. // ... it will only be the hcp scope within that node
  143. $guestAccessCode = $request->get('guest_access_code');
  144. if($guestAccessCode){
  145. //its from guest
  146. $sectionForToken = Section::where('guest_access_code', $guestAccessCode)->first();
  147. abort_if(!$sectionForToken, 401, 'Unauthorized');
  148. }else{
  149. //its not from guest so require performer
  150. abort_if(!$this->performer, 401, 'Unauthorized');
  151. abort_if(!$this->performer->is_active, 401, 'Unauthorized');
  152. }
  153. // TODO require
  154. $section_uid = $request->get('section_uid');
  155. $section = Section::where('uid', $section_uid)->first();
  156. $note = Note::where('id', $section->note_id)->first();
  157. $client = null;
  158. if($note){
  159. $client = Client::where('id', $note->client_id)->first();
  160. }else{
  161. $client = Client::where('id', $section->client_id)->first();
  162. }
  163. $patient = $client;
  164. $sectionTemplate = SectionTemplate::where('id', $section->section_template_id)->first();
  165. $newContentData = [];
  166. $newSummaryHtml = "";
  167. $sectionInternalName = $sectionTemplate->internal_name;
  168. if ($sectionTemplate->is_canvas) {
  169. $key = $sectionTemplate->internal_name;
  170. // Because sectionTemplate is_canvas, any update to the section will require updating the canvas.
  171. // ... there are TWO possibilities.
  172. // ...... 1) if !is_hcp_scoped, then what comes in from the section simply swaps out the entire node
  173. // ...... 2) if is_hcp_scoped, then what comes in from the section is incoprorated into that scope in the node
  174. $newCanvasNodeData = null;
  175. if($sectionTemplate->is_hcp_scoped){
  176. $currentCanvasData = json_decode($client->canvas_data, true);
  177. $currentCanvasDataNode = isset($currentCanvasData[$key]) ? $currentCanvasData[$key] : [];
  178. $currentCanvasDataNode[$note->hcpPro->id] = json_decode($request->get('data'), true);
  179. $newCanvasNodeData = json_encode($currentCanvasDataNode);
  180. }else{
  181. $newCanvasNodeData = $request->get('data');
  182. }
  183. $response = null;
  184. $data = [
  185. 'uid' => $client->uid,
  186. 'noteUid'=> $note?$note->uid:null,
  187. 'key' => $key,
  188. 'data' => $newCanvasNodeData
  189. ];
  190. $response = $this->calljava($request, '/client/updateCanvasData', $data, $guestAccessCode);
  191. //TODO: handle $response->success == false
  192. if($note){
  193. $client = Client::where('id', $note->client_id)->first();
  194. }else{
  195. $client = Client::where('id', $section->client_id)->first();
  196. }
  197. $patient = $client;
  198. if (file_exists(resource_path("views/app/patient/canvas-sections/{$sectionInternalName}/processor.php"))) {
  199. include(resource_path("views/app/patient/canvas-sections/{$sectionInternalName}/processor.php"));
  200. } else {
  201. $newContentData = json_decode($request->get('data'), true);
  202. }
  203. ob_start();
  204. include(resource_path("views/app/patient/canvas-sections/{$sectionInternalName}/summary.php"));
  205. $newSummaryHtml = ob_get_contents();
  206. ob_end_clean();
  207. // TODO call Java to update the canvas
  208. } elseif (file_exists(storage_path('sections/' . $sectionTemplate->internal_name . '/form.blade.php'))) {
  209. include(storage_path('sections/' . $sectionTemplate->internal_name . '/processor.php'));
  210. ob_start();
  211. include(storage_path('sections/' . $sectionTemplate->internal_name . '/summary.php'));
  212. $newSummaryHtml = ob_get_contents();
  213. ob_end_clean();
  214. } else {
  215. $newContentData = json_decode($request->get('data'), true);
  216. if (isset($newContentData['value'])) {
  217. $newSummaryHtml = $newContentData['value'];
  218. }
  219. }
  220. $response = null;
  221. $data = [
  222. 'uid' => $section->uid,
  223. 'contentData' => json_encode($newContentData),
  224. 'summaryHtml' => $newSummaryHtml
  225. ];
  226. $response = $this->calljava($request, '/section/update', $data, $guestAccessCode);
  227. return [
  228. 'success' => $response['success'],
  229. 'newSummaryHtml' => $newSummaryHtml
  230. ];
  231. }
  232. // TODO move to utility
  233. private function callJava($request, $endPoint, $data, $guestAccessCode = null)
  234. {
  235. $url = config('stag.backendUrl') . $endPoint;
  236. $response = Http::asForm()
  237. ->withHeaders([
  238. 'sessionKey' => $request->cookie('sessionKey'),
  239. 'guestAccessCode' => $guestAccessCode
  240. ])
  241. ->post($url, $data)
  242. ->json();
  243. return $response;
  244. }
  245. }