NoteController.php 11 KB

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