NoteController.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. public function getDefaultValueForSection($patientID, $sectionTemplateID)
  66. {
  67. $contentData = [];
  68. $summaryHtml = '';
  69. $patient = Client::where('id', $patientID)->first();
  70. $sectionTemplate = SectionTemplate::where('id', $sectionTemplateID)->first();
  71. if ($sectionTemplate->is_canvas) {
  72. if (file_exists(resource_path('views/app/patient/canvas-sections/' . $sectionTemplate->internal_name . '/default.php'))) {
  73. // for canvas section where we have pro mapped data, use sectionProUid
  74. $sectionPro = null;
  75. if(\request()->input('sectionProUid')) {
  76. $sectionPro = Pro::where('uid', \request()->input('sectionProUid'))->first();
  77. }
  78. // default should simply assign to $contentData
  79. include(resource_path('views/app/patient/canvas-sections/' . $sectionTemplate->internal_name . '/default.php'));
  80. ob_start();
  81. include(resource_path('views/app/patient/canvas-sections/' . $sectionTemplate->internal_name . '/summary.php'));
  82. $summaryHtml = ob_get_contents();
  83. ob_end_clean();
  84. }
  85. } else {
  86. if (file_exists(storage_path('sections/' . $sectionTemplate->internal_name . '/default.php'))) {
  87. // default should simply assign to $contentData and $summaryHtml as needed
  88. include(storage_path('sections/' . $sectionTemplate->internal_name . '/default.php'));
  89. }
  90. }
  91. return [
  92. 'contentData' => $contentData,
  93. 'summaryHtml' => $summaryHtml
  94. ];
  95. }
  96. public function processFormSubmit(Request $request)
  97. {
  98. $guestAccessCode = $request->get('guest_access_code');
  99. if($guestAccessCode){
  100. //its from guest
  101. $sectionForToken = Section::where('guest_access_code', $guestAccessCode)->first();
  102. abort_if(!$sectionForToken, 401, 'Unauthorized');
  103. }else{
  104. //its not from guest so require performer
  105. abort_if(!$this->performer, 401, 'Unauthorized');
  106. abort_if(!$this->performer->is_active, 401, 'Unauthorized');
  107. }
  108. // TODO require
  109. $section_uid = $request->get('section_uid');
  110. $section = Section::where('uid', $section_uid)->first();
  111. $note = Note::where('id', $section->note_id)->first();
  112. $client = null;
  113. if($note){
  114. $client = Client::where('id', $note->client_id)->first();
  115. }else{
  116. $client = Client::where('id', $section->client_id)->first();
  117. }
  118. $patient = $client;
  119. $sectionTemplate = SectionTemplate::where('id', $section->section_template_id)->first();
  120. $newContentData = [];
  121. $newSummaryHtml = "";
  122. $sectionInternalName = $sectionTemplate->internal_name;
  123. if ($sectionTemplate->is_canvas) {
  124. $response = null;
  125. $data = [
  126. 'uid' => $client->uid,
  127. 'key' => $sectionTemplate->internal_name,
  128. 'data' => $request->get('data')
  129. ];
  130. $response = $this->calljava($request, '/client/updateCanvasData', $data, $guestAccessCode);
  131. //TODO: handle $response->success == false
  132. if($note){
  133. $client = Client::where('id', $note->client_id)->first();
  134. }else{
  135. $client = Client::where('id', $section->client_id)->first();
  136. }
  137. $patient = $client;
  138. if (file_exists(resource_path("views/app/patient/canvas-sections/{$sectionInternalName}/processor.php"))) {
  139. include(resource_path("views/app/patient/canvas-sections/{$sectionInternalName}/processor.php"));
  140. } else {
  141. $newContentData = json_decode($request->get('data'), true);
  142. }
  143. ob_start();
  144. include(resource_path("views/app/patient/canvas-sections/{$sectionInternalName}/summary.php"));
  145. $newSummaryHtml = ob_get_contents();
  146. ob_end_clean();
  147. // TODO call Java to update the canvas
  148. } elseif (file_exists(storage_path('sections/' . $sectionTemplate->internal_name . '/form.blade.php'))) {
  149. include(storage_path('sections/' . $sectionTemplate->internal_name . '/processor.php'));
  150. ob_start();
  151. include(storage_path('sections/' . $sectionTemplate->internal_name . '/summary.php'));
  152. $newSummaryHtml = ob_get_contents();
  153. ob_end_clean();
  154. } else {
  155. $newContentData = json_decode($request->get('data'), true);
  156. if (isset($newContentData['value'])) {
  157. $newSummaryHtml = $newContentData['value'];
  158. }
  159. }
  160. $response = null;
  161. $data = [
  162. 'uid' => $section->uid,
  163. 'contentData' => json_encode($newContentData),
  164. 'summaryHtml' => $newSummaryHtml
  165. ];
  166. $response = $this->calljava($request, '/section/update', $data, $guestAccessCode);
  167. return [
  168. 'success' => $response['success'],
  169. 'newSummaryHtml' => $newSummaryHtml
  170. ];
  171. }
  172. // TODO move to utility
  173. private function callJava($request, $endPoint, $data, $guestAccessCode = null)
  174. {
  175. $url = config('stag.backendUrl') . $endPoint;
  176. $response = Http::asForm()
  177. ->withHeaders([
  178. 'sessionKey' => $request->cookie('sessionKey'),
  179. 'guestAccessCode' => $guestAccessCode
  180. ])
  181. ->post($url, $data)
  182. ->json();
  183. return $response;
  184. }
  185. }