NoteController.php 11 KB

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