NoteController.php 9.8 KB

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