NoteController.php 9.3 KB

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