NoteController.php 9.8 KB

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