NoteController.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Pro;
  4. use App\Models\SupplyOrder;
  5. use App\Models\Ticket;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Collection;
  8. use Illuminate\Support\Facades\Http;
  9. use App\Models\Note;
  10. use App\Models\Client;
  11. use App\Models\Section;
  12. use App\Models\SectionTemplate;
  13. class NoteController extends Controller
  14. {
  15. public function dashboard(Request $request, Client $patient, Note $note)
  16. {
  17. $pros = $this->pros;
  18. $noteSections = $note->sections;
  19. $allSections = SectionTemplate::where('is_active', true)->get();
  20. foreach ($allSections as $section) {
  21. $section->used = false;
  22. foreach ($noteSections as $noteSection) {
  23. if ($noteSection->sectionTemplate->id === $section->id) {
  24. $section->used = true;
  25. $section->section_uid = $noteSection->uid;
  26. break;
  27. }
  28. }
  29. }
  30. $allyPros = Pro::all(); //TODO: paginate, use select2
  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. 'allyPros', '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. // JAVA ONLY
  65. // ... if hcpProId is passed, get from request
  66. public function getDefaultValueForSection($patientID, $sectionTemplateID)
  67. {
  68. $contentData = [];
  69. $summaryHtml = '';
  70. $patient = Client::where('id', $patientID)->first();
  71. $sectionTemplate = SectionTemplate::where('id', $sectionTemplateID)->first();
  72. if ($sectionTemplate->is_canvas) {
  73. if (file_exists(resource_path('views/app/patient/canvas-sections/' . $sectionTemplate->internal_name . '/default.php'))) {
  74. // for canvas section where we have pro mapped data, use hcpProId
  75. $hcpPro = null;
  76. if(\request()->input('hcpProUId')) {
  77. $hcpPro = Pro::where('uid', \request()->input('hcpProUid'))->first();
  78. }
  79. $note = null;
  80. if(\request()->input('noteUid')) {
  81. $note = Note::where('uid', \request()->input('noteUid'))->first();
  82. }
  83. // default should simply assign to $contentData
  84. include(resource_path('views/app/patient/canvas-sections/' . $sectionTemplate->internal_name . '/default.php'));
  85. ob_start();
  86. include(resource_path('views/app/patient/canvas-sections/' . $sectionTemplate->internal_name . '/summary.php'));
  87. $summaryHtml = ob_get_contents();
  88. ob_end_clean();
  89. }
  90. } else {
  91. if (file_exists(storage_path('sections/' . $sectionTemplate->internal_name . '/default.php'))) {
  92. // default should simply assign to $contentData and $summaryHtml as needed
  93. include(storage_path('sections/' . $sectionTemplate->internal_name . '/default.php'));
  94. }
  95. }
  96. return [
  97. 'contentData' => $contentData,
  98. 'summaryHtml' => $summaryHtml
  99. ];
  100. }
  101. public function processFormSubmit(Request $request)
  102. {
  103. // guest_access_code, section_uid, data
  104. // REMEMBER, if this is an hcp scoped canvas section, data will not be the ENTIRE node...
  105. // ... it will only be the hcp scope within that node
  106. $guestAccessCode = $request->get('guest_access_code');
  107. if($guestAccessCode){
  108. //its from guest
  109. $sectionForToken = Section::where('guest_access_code', $guestAccessCode)->first();
  110. abort_if(!$sectionForToken, 401, 'Unauthorized');
  111. }else{
  112. //its not from guest so require performer
  113. abort_if(!$this->performer, 401, 'Unauthorized');
  114. abort_if(!$this->performer->is_active, 401, 'Unauthorized');
  115. }
  116. // TODO require
  117. $section_uid = $request->get('section_uid');
  118. $section = Section::where('uid', $section_uid)->first();
  119. $note = Note::where('id', $section->note_id)->first();
  120. $client = null;
  121. if($note){
  122. $client = Client::where('id', $note->client_id)->first();
  123. }else{
  124. $client = Client::where('id', $section->client_id)->first();
  125. }
  126. $patient = $client;
  127. $sectionTemplate = SectionTemplate::where('id', $section->section_template_id)->first();
  128. $newContentData = [];
  129. $newSummaryHtml = "";
  130. $sectionInternalName = $sectionTemplate->internal_name;
  131. if ($sectionTemplate->is_canvas) {
  132. $key = $sectionTemplate->internal_name;
  133. // Because sectionTemplate is_canvas, any update to the section will require updating the canvas.
  134. // ... there are TWO possibilities.
  135. // ...... 1) if !is_hcp_scoped, then what comes in from the section simply swaps out the entire node
  136. // ...... 2) if is_hcp_scoped, then what comes in from the section is incoprorated into that scope in the node
  137. $newCanvasNodeData = null;
  138. if($sectionTemplate->is_hcp_scoped){
  139. $currentCanvasData = json_decode($client->canvas_data, true);
  140. $currentCanvasDataNode = isset($currentCanvasData[$key]) ? $currentCanvasData[$key] : [];
  141. $currentCanvasDataNode[$note->hcpPro->id] = json_decode($request->get('data'), true);
  142. $newCanvasNodeData = json_encode($currentCanvasDataNode);
  143. }else{
  144. $newCanvasNodeData = $request->get('data');
  145. }
  146. $response = null;
  147. $data = [
  148. 'uid' => $client->uid,
  149. 'key' => $key,
  150. 'data' => $newCanvasNodeData
  151. ];
  152. $response = $this->calljava($request, '/client/updateCanvasData', $data, $guestAccessCode);
  153. //TODO: handle $response->success == false
  154. if($note){
  155. $client = Client::where('id', $note->client_id)->first();
  156. }else{
  157. $client = Client::where('id', $section->client_id)->first();
  158. }
  159. $patient = $client;
  160. if (file_exists(resource_path("views/app/patient/canvas-sections/{$sectionInternalName}/processor.php"))) {
  161. include(resource_path("views/app/patient/canvas-sections/{$sectionInternalName}/processor.php"));
  162. } else {
  163. $newContentData = json_decode($request->get('data'), true);
  164. }
  165. ob_start();
  166. include(resource_path("views/app/patient/canvas-sections/{$sectionInternalName}/summary.php"));
  167. $newSummaryHtml = ob_get_contents();
  168. ob_end_clean();
  169. // TODO call Java to update the canvas
  170. } elseif (file_exists(storage_path('sections/' . $sectionTemplate->internal_name . '/form.blade.php'))) {
  171. include(storage_path('sections/' . $sectionTemplate->internal_name . '/processor.php'));
  172. ob_start();
  173. include(storage_path('sections/' . $sectionTemplate->internal_name . '/summary.php'));
  174. $newSummaryHtml = ob_get_contents();
  175. ob_end_clean();
  176. } else {
  177. $newContentData = json_decode($request->get('data'), true);
  178. if (isset($newContentData['value'])) {
  179. $newSummaryHtml = $newContentData['value'];
  180. }
  181. }
  182. $response = null;
  183. $data = [
  184. 'uid' => $section->uid,
  185. 'contentData' => json_encode($newContentData),
  186. 'summaryHtml' => $newSummaryHtml
  187. ];
  188. $response = $this->calljava($request, '/section/update', $data, $guestAccessCode);
  189. return [
  190. 'success' => $response['success'],
  191. 'newSummaryHtml' => $newSummaryHtml
  192. ];
  193. }
  194. // TODO move to utility
  195. private function callJava($request, $endPoint, $data, $guestAccessCode = null)
  196. {
  197. $url = config('stag.backendUrl') . $endPoint;
  198. $response = Http::asForm()
  199. ->withHeaders([
  200. 'sessionKey' => $request->cookie('sessionKey'),
  201. 'guestAccessCode' => $guestAccessCode
  202. ])
  203. ->post($url, $data)
  204. ->json();
  205. return $response;
  206. }
  207. }