NoteController.php 7.8 KB

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