NoteController.php 11 KB

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