NoteController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\AppSession;
  4. use App\Models\Page;
  5. use App\Models\Point;
  6. use App\Models\Pro;
  7. use App\Models\SupplyOrder;
  8. use App\Models\Ticket;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Collection;
  11. use Illuminate\Support\Facades\Blade;
  12. use Illuminate\Support\Facades\Http;
  13. use App\Models\Note;
  14. use App\Models\Client;
  15. use App\Models\CompanyPro;
  16. use App\Models\Section;
  17. use App\Models\SectionTemplate;
  18. use App\Models\Segment;
  19. use App\Models\SegmentTemplate;
  20. use Illuminate\Support\Facades\DB;
  21. class NoteController extends Controller
  22. {
  23. public function dashboard(Request $request, Client $patient, Note $note)
  24. {
  25. $pros = $this->pros;
  26. $noteSections = $note->sections;
  27. $allSections = SectionTemplate::where('is_active', true)->get();
  28. foreach ($allSections as $section) {
  29. $section->used = false;
  30. foreach ($noteSections as $noteSection) {
  31. if ($noteSection->sectionTemplate->id === $section->id) {
  32. $section->used = true;
  33. $section->section_uid = $noteSection->uid;
  34. break;
  35. }
  36. }
  37. }
  38. // load tickets created on note->effective_date for patient
  39. $ticketsOnNote = Ticket::where('client_id', $patient->id)
  40. ->where('is_entry_error', false)
  41. ->where('note_id', $note->id)
  42. ->get();
  43. // other open tickets as of today
  44. $otherOpenTickets = Ticket::where('client_id', $patient->id)
  45. ->where('is_entry_error', false)
  46. ->where('is_open', true)
  47. ->where(function ($query) use ($note) {
  48. $query->where('note_id', '<>', $note->id)->orWhereNull('note_id'); // weird, but just the <> isn't working!
  49. })
  50. ->get();
  51. // load supplyOrders created on note->effective_date for patient
  52. $supplyOrdersOnNote = SupplyOrder::where('client_id', $patient->id)
  53. ->where('is_cancelled', false)
  54. ->where('note_id', $note->id)
  55. ->get();
  56. // other open supplyOrders as of today
  57. $otherOpenSupplyOrders = SupplyOrder::where('client_id', $patient->id)
  58. ->where('is_cancelled', false)
  59. ->where('note_id', '<>', $note->id)
  60. ->get();
  61. $templates = get_doc_templates();
  62. $companyProIDs = DB::select('SELECT company_pro_id FROM company_pro_document WHERE related_client_id = ?', [$patient->id]);
  63. $companyProIDInts = [];
  64. foreach($companyProIDs as $cpId){
  65. $companyProIDInts[] = $cpId->company_pro_id;
  66. }
  67. $companyPros = CompanyPro::whereIn('id', $companyProIDInts)->get();
  68. return view('app.patient.note.dashboard', compact('patient', 'note',
  69. 'allSections',
  70. 'ticketsOnNote', 'otherOpenTickets',
  71. 'companyPros',
  72. 'supplyOrdersOnNote', 'otherOpenSupplyOrders', 'templates'));
  73. }
  74. public function signConfirmation(Request $request, Client $patient, Note $note) {
  75. return view('app.patient.note.sign-confirmation', compact('patient', 'note'));
  76. }
  77. public function renderNote($noteUid, Request $request)
  78. {
  79. $note = Note::where('uid', $noteUid)->first();
  80. $client = Client::where('id', $note->client_id)->first();
  81. return view('client/note', compact('note', 'client'));
  82. }
  83. public function sectionView(Request $request, Client $patient, Note $note, Section $section, $form, Page $page = null) {
  84. return view("app.patient.page-sections." . $section->sectionTemplate->internal_name . "." . $form,
  85. compact('patient', 'note', 'section', 'page'));
  86. }
  87. public function print(Request $request, Client $patient, Note $note) {
  88. return view("app.patient.note.print", compact('patient', 'note'));
  89. }
  90. public function getHtmlForSegment($segmentUid, $sessionKey){
  91. $summaryHtml = '';
  92. $editHtml = '';
  93. try {
  94. $performer = AppSession::where('session_key', $sessionKey)->first();
  95. if (!$performer || !$performer->is_active) {
  96. return response()->json([
  97. 'success' => false,
  98. 'message' => 'Invalid session key'
  99. ]);
  100. }
  101. $pro = $performer->pro;
  102. $segment = Segment::where('uid', $segmentUid)->first();
  103. $recalculatedHtml = $segment->getRecalculatedHtml($performer, $sessionKey);
  104. } catch (\Throwable $e) {
  105. return response()->json([
  106. 'success' => false,
  107. 'message' => $e->getMessage()
  108. ]);
  109. }
  110. return response()->json([
  111. 'success'=>true,
  112. 'summaryHtml' => $recalculatedHtml['summaryHtml'],
  113. 'editHtml' => $recalculatedHtml['editHtml'],
  114. ]);
  115. }
  116. // JAVA ONLY
  117. // ... if hcpProId is passed, get from request
  118. public function getDefaultValueForSection($patientID, $sectionTemplateID)
  119. {
  120. $contentData = [];
  121. $summaryHtml = '';
  122. $patient = Client::where('id', $patientID)->first();
  123. $sectionTemplate = SectionTemplate::where('id', $sectionTemplateID)->first();
  124. if ($sectionTemplate->is_canvas) {
  125. if (file_exists(resource_path('views/app/patient/canvas-sections/' . $sectionTemplate->internal_name . '/default.php'))) {
  126. // for canvas section where we have pro mapped data, use hcpProId
  127. $hcpPro = null;
  128. if(\request()->input('hcpProUid')) {
  129. $hcpPro = Pro::where('uid', \request()->input('hcpProUid'))->first();
  130. }
  131. $note = null;
  132. if(\request()->input('noteUid')) {
  133. $note = Note::where('uid', \request()->input('noteUid'))->first();
  134. }
  135. // default should simply assign to $contentData
  136. include(resource_path('views/app/patient/canvas-sections/' . $sectionTemplate->internal_name . '/default.php'));
  137. ob_start();
  138. include(resource_path('views/app/patient/canvas-sections/' . $sectionTemplate->internal_name . '/summary.php'));
  139. $summaryHtml = ob_get_contents();
  140. ob_end_clean();
  141. }
  142. } else {
  143. if (file_exists(storage_path('sections/' . $sectionTemplate->internal_name . '/default.php'))) {
  144. // default should simply assign to $contentData and $summaryHtml as needed
  145. include(storage_path('sections/' . $sectionTemplate->internal_name . '/default.php'));
  146. }
  147. }
  148. return [
  149. 'contentData' => $contentData,
  150. 'summaryHtml' => $summaryHtml
  151. ];
  152. }
  153. public function processFormSubmit(Request $request)
  154. {
  155. // guest_access_code, section_uid, data
  156. // REMEMBER, if this is an hcp scoped canvas section, data will not be the ENTIRE node...
  157. // ... it will only be the hcp scope within that node
  158. $guestAccessCode = $request->get('guest_access_code');
  159. if($guestAccessCode){
  160. //its from guest
  161. $sectionForToken = Section::where('guest_access_code', $guestAccessCode)->first();
  162. abort_if(!$sectionForToken, 401, 'Unauthorized');
  163. }else{
  164. //its not from guest so require performer
  165. abort_if(!$this->performer, 401, 'Unauthorized');
  166. abort_if(!$this->performer->is_active, 401, 'Unauthorized');
  167. }
  168. // TODO require
  169. $section_uid = $request->get('section_uid');
  170. $section = Section::where('uid', $section_uid)->first();
  171. $note = Note::where('id', $section->note_id)->first();
  172. $client = null;
  173. if($note){
  174. $client = Client::where('id', $note->client_id)->first();
  175. }else{
  176. $client = Client::where('id', $section->client_id)->first();
  177. }
  178. $patient = $client;
  179. $sectionTemplate = SectionTemplate::where('id', $section->section_template_id)->first();
  180. $newContentData = [];
  181. $newSummaryHtml = "";
  182. $sectionInternalName = $sectionTemplate->internal_name;
  183. if ($sectionTemplate->is_canvas) {
  184. $key = $sectionTemplate->internal_name;
  185. // Because sectionTemplate is_canvas, any update to the section will require updating the canvas.
  186. // ... there are TWO possibilities.
  187. // ...... 1) if !is_hcp_scoped, then what comes in from the section simply swaps out the entire node
  188. // ...... 2) if is_hcp_scoped, then what comes in from the section is incoprorated into that scope in the node
  189. $newCanvasNodeData = null;
  190. if($sectionTemplate->is_hcp_scoped){
  191. $currentCanvasData = json_decode($client->canvas_data, true);
  192. $currentCanvasDataNode = isset($currentCanvasData[$key]) ? $currentCanvasData[$key] : [];
  193. $currentCanvasDataNode[$note->hcpPro->id] = json_decode($request->get('data'), true);
  194. $newCanvasNodeData = json_encode($currentCanvasDataNode);
  195. }else{
  196. $newCanvasNodeData = $request->get('data');
  197. }
  198. $response = null;
  199. $data = [
  200. 'uid' => $client->uid,
  201. 'noteUid'=> $note?$note->uid:null,
  202. 'key' => $key,
  203. 'data' => $newCanvasNodeData
  204. ];
  205. $response = $this->calljava($request, '/client/updateCanvasData', $data, $guestAccessCode);
  206. //TODO: handle $response->success == false
  207. if($note){
  208. $client = Client::where('id', $note->client_id)->first();
  209. }else{
  210. $client = Client::where('id', $section->client_id)->first();
  211. }
  212. $patient = $client;
  213. if (file_exists(resource_path("views/app/patient/canvas-sections/{$sectionInternalName}/processor.php"))) {
  214. include(resource_path("views/app/patient/canvas-sections/{$sectionInternalName}/processor.php"));
  215. } else {
  216. $newContentData = json_decode($request->get('data'), true);
  217. }
  218. ob_start();
  219. include(resource_path("views/app/patient/canvas-sections/{$sectionInternalName}/summary.php"));
  220. $newSummaryHtml = ob_get_contents();
  221. ob_end_clean();
  222. // TODO call Java to update the canvas
  223. } elseif (file_exists(storage_path('sections/' . $sectionTemplate->internal_name . '/form.blade.php'))) {
  224. include(storage_path('sections/' . $sectionTemplate->internal_name . '/processor.php'));
  225. ob_start();
  226. include(storage_path('sections/' . $sectionTemplate->internal_name . '/summary.php'));
  227. $newSummaryHtml = ob_get_contents();
  228. ob_end_clean();
  229. } else {
  230. $newContentData = json_decode($request->get('data'), true);
  231. if (isset($newContentData['value'])) {
  232. $newSummaryHtml = $newContentData['value'];
  233. }
  234. }
  235. $response = null;
  236. $data = [
  237. 'uid' => $section->uid,
  238. 'contentData' => json_encode($newContentData),
  239. 'summaryHtml' => $newSummaryHtml
  240. ];
  241. $response = $this->calljava($request, '/section/update', $data, $guestAccessCode);
  242. return [
  243. 'success' => $response['success'],
  244. 'newSummaryHtml' => $newSummaryHtml
  245. ];
  246. }
  247. // edit hpi (structured)
  248. public function editHPI(Note $note, Point $point) {
  249. return view('app.patient.note.edit-hpi', compact('note', 'point'));
  250. }
  251. public function hpiLog(Note $note, Point $point) {
  252. return view('app.patient.note.hpi-log', compact('note', 'point'));
  253. }
  254. // review log
  255. public function reviewLog(Point $point) {
  256. return view('app.patient.note.review-log', compact('point'));
  257. }
  258. // plan log
  259. public function planLog(Point $point) {
  260. return view('app.patient.note.plan-log', compact('point'));
  261. }
  262. // print/pdf
  263. public function downloadAsPdf(Request $request, Note $note) {
  264. $patient = $note->client;
  265. if($request->input('html')) {
  266. return view('app.patient.note.pdf', compact('note', 'patient'));
  267. }
  268. else {
  269. $pdf = \PDF::loadView('app.patient.note.pdf', compact('note', 'patient'));
  270. return $pdf->stream($note->created_at .'_' . 'note.pdf');
  271. }
  272. }
  273. public function generateCC(Request $request, Note $note) {
  274. $client = $note->client;
  275. return view('app.patient.segment-templates.chief_complaint.generate', compact('note', 'client'));
  276. }
  277. public function segmentSummary(Request $request, Segment $segment) {
  278. return '<div class="mrv-content border-top px-3 pt-2 mt-3">' . @$segment->summary_html . '</div>';
  279. }
  280. public function mrvSummary(Request $request, Note $note) {
  281. return view('app.patient.segment-templates.medrisk_vigilence.summary', [
  282. 'note' => $note,
  283. 'patient' => $note->client,
  284. 'segment' => $note->coreSegment
  285. ]);
  286. }
  287. public function chartSegmentView(Request $request, Client $patient, $segmentInternalName, $view) {
  288. return view("app.patient.segment-templates.{$segmentInternalName}.{$view}", [
  289. 'patient' => $patient,
  290. 'note' => $patient->coreNote,
  291. 'segmentInternalName' => $segmentInternalName,
  292. 'closeOnSave' => true
  293. ]);
  294. }
  295. public function noteSegmentView(Request $request, Client $patient, Note $note, Segment $segment, $segmentInternalName, $view) {
  296. return view("app.patient.segment-templates.{$segmentInternalName}.{$view}", [
  297. 'patient' => $patient,
  298. 'note' => $note,
  299. 'segment' => $segment,
  300. 'segmentInternalName' => $segmentInternalName
  301. ]);
  302. }
  303. public function noteSegmentViewByName(Request $request, Note $note, $segmentInternalName, $view) {
  304. return view("app.patient.segment-templates.{$segmentInternalName}.{$view}", [
  305. 'patient' => $note->client,
  306. 'note' => $note,
  307. 'segment' => $note->coreSegment,
  308. 'segmentInternalName' => $segmentInternalName
  309. ]);
  310. }
  311. public function moduleView(Request $request, Note $note, $segmentInternalName, $view) {
  312. return view("app.patient.modules.{$segmentInternalName}.{$view}", [
  313. 'patient' => $note->client,
  314. 'note' => $note,
  315. 'segment' => $note->coreSegment,
  316. 'segmentInternalName' => $segmentInternalName
  317. ]);
  318. }
  319. public function rhsSidebar(Request $request, Client $patient, Note $note) {
  320. return view('app.patient.note.rhs-sidebar', compact('patient', 'note'));
  321. }
  322. public function medicationsCenter(Request $request, Client $patient, Note $note) {
  323. return view('app.patient.medications-center', compact('patient', 'note'));
  324. }
  325. public function medicationsReconcile(Request $request, Client $patient, Note $note) {
  326. return view('app.patient.medications-reconcile', compact('patient', 'note'));
  327. }
  328. public function problemsQuickAdd(Request $request, Client $patient, Note $note) {
  329. return view('app.patient.problems-quick-add', compact('patient', 'note'));
  330. }
  331. public function problemsCenter(Request $request, Client $patient, Note $note) {
  332. return view('app.patient.problems-center', compact('patient', 'note'));
  333. }
  334. public function goalsCenter(Request $request, Client $patient, Note $note) {
  335. return view('app.patient.goals-center', compact('patient', 'note'));
  336. }
  337. public function allergiesCenter(Request $request, Client $patient, Note $note) {
  338. return view('app.patient.allergies-center', compact('patient', 'note'));
  339. }
  340. public function careteamCenter(Request $request, Client $patient, Note $note) {
  341. return view('app.patient.careteam-center', compact('patient', 'note'));
  342. }
  343. public function supplementsCenter(Request $request, Client $patient, Note $note) {
  344. return view('app.patient.supplements-center', compact('patient', 'note'));
  345. }
  346. public function supplementsReconcile(Request $request, Client $patient, Note $note) {
  347. return view('app.patient.supplements-reconcile', compact('patient', 'note'));
  348. }
  349. public function nutritionCenter(Request $request, Client $patient, Note $note) {
  350. return view('app.patient.nutrition-center', compact('patient', 'note'));
  351. }
  352. public function exerciseCenter(Request $request, Client $patient, Note $note) {
  353. return view('app.patient.exercise-center', compact('patient', 'note'));
  354. }
  355. public function behaviorCenter(Request $request, Client $patient, Note $note) {
  356. return view('app.patient.behavior-center', compact('patient', 'note'));
  357. }
  358. public function ccmAgreement(Request $request, Note $note) {
  359. return view('app.patient.note.ccm-agreement', compact('note'));
  360. }
  361. public function rpmAgreement(Request $request, Note $note) {
  362. return view('app.patient.note.rpm-agreement', compact('note'));
  363. }
  364. // TODO move to utility
  365. private function callJava($request, $endPoint, $data, $guestAccessCode = null)
  366. {
  367. $url = config('stag.backendUrl') . $endPoint;
  368. $response = Http::asForm()
  369. ->withHeaders([
  370. 'sessionKey' => $request->cookie('sessionKey'),
  371. 'guestAccessCode' => $guestAccessCode
  372. ])
  373. ->post($url, $data)
  374. ->json();
  375. return $response;
  376. }
  377. }