NoteController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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 App\Models\SupplyOrderView;
  21. use Illuminate\Support\Facades\DB;
  22. class NoteController extends Controller
  23. {
  24. public function dashboard(Request $request, Client $patient, Note $note)
  25. {
  26. $pros = $this->pros;
  27. $noteSections = $note->sections;
  28. $allSections = SectionTemplate::where('is_active', true)->get();
  29. foreach ($allSections as $section) {
  30. $section->used = false;
  31. foreach ($noteSections as $noteSection) {
  32. if ($noteSection->sectionTemplate->id === $section->id) {
  33. $section->used = true;
  34. $section->section_uid = $noteSection->uid;
  35. break;
  36. }
  37. }
  38. }
  39. // load supplyOrders created on note->effective_date for patient
  40. $supplyOrdersOnNote = SupplyOrderView::where('client_id', $patient->id)
  41. ->where('is_cancelled', false)
  42. ->where('note_id', $note->id)
  43. ->get();
  44. // other open supplyOrders as of today
  45. $otherOpenSupplyOrders = SupplyOrderView::where('client_id', $patient->id)
  46. ->where('is_cancelled', false)
  47. ->where('note_id', '<>', $note->id)
  48. ->get();
  49. $companyProIDs = DB::select('SELECT company_pro_id FROM company_pro_document WHERE related_client_id = ?', [$patient->id]);
  50. $companyProIDInts = [];
  51. foreach($companyProIDs as $cpId){
  52. $companyProIDInts[] = $cpId->company_pro_id;
  53. }
  54. $companyPros = CompanyPro::whereIn('id', $companyProIDInts)->get();
  55. return view('app.patient.note.dashboard', compact('patient', 'note',
  56. 'allSections',
  57. 'companyPros',
  58. 'supplyOrdersOnNote', 'otherOpenSupplyOrders'));
  59. }
  60. private function filterClientDocuments($documents){
  61. $clientDocs = [];
  62. foreach($documents as $doc){
  63. if(starts_with($doc['name'], 'client_')){
  64. array_push($clientDocs, $doc);
  65. }
  66. }
  67. return $clientDocs;
  68. }
  69. public function signConfirmation(Request $request, Client $patient, Note $note) {
  70. return view('app.patient.note.sign-confirmation', compact('patient', 'note'));
  71. }
  72. public function renderNote($noteUid, Request $request)
  73. {
  74. $note = Note::where('uid', $noteUid)->first();
  75. $client = Client::where('id', $note->client_id)->first();
  76. return view('client/note', compact('note', 'client'));
  77. }
  78. public function sectionView(Request $request, Client $patient, Note $note, Section $section, $form, Page $page = null) {
  79. return view("app.patient.page-sections." . $section->sectionTemplate->internal_name . "." . $form,
  80. compact('patient', 'note', 'section', 'page'));
  81. }
  82. public function print(Request $request, Client $patient, Note $note) {
  83. if($note->visitTemplate) {
  84. return view("app.patient.note.print.print", compact('patient', 'note'));
  85. }
  86. return view("app.patient.note.print.print-legacy", compact('patient', 'note'));
  87. }
  88. public function printV2(Request $request, Client $patient, Note $note) {
  89. if($note->visitTemplate) {
  90. return view("app.patient.note.print.print-v2", compact('patient', 'note'));
  91. }
  92. return view("app.patient.note.print.print-legacy", compact('patient', 'note'));
  93. }
  94. public function printV3(Request $request, Client $patient, Note $note) {
  95. if($note->visitTemplate) {
  96. return view("app.patient.note.print.print-v3", compact('patient', 'note'));
  97. }
  98. return view("app.patient.note.print.print-legacy", compact('patient', 'note'));
  99. }
  100. public function resolve(Request $request, Client $patient, Note $note) {
  101. return view("app.patient.note.resolve", compact('patient', 'note'));
  102. }
  103. public function getHtmlForSegment($segmentUid, $sessionKey){
  104. $summaryHtml = '';
  105. $editHtml = '';
  106. try {
  107. $performer = AppSession::where('session_key', $sessionKey)->first();
  108. if (!$performer || !$performer->is_active) {
  109. return response()->json([
  110. 'success' => false,
  111. 'message' => 'Invalid session key'
  112. ]);
  113. }
  114. $pro = $performer->pro;
  115. $segment = Segment::where('uid', $segmentUid)->first();
  116. $recalculatedHtml = $segment->getRecalculatedHtml($performer, $sessionKey);
  117. } catch (\Throwable $e) {
  118. return response()->json([
  119. 'success' => false,
  120. 'message' => $e->getMessage()
  121. ]);
  122. }
  123. return response()->json([
  124. 'success'=>true,
  125. 'summaryHtml' => $recalculatedHtml['summaryHtml'],
  126. 'editHtml' => $recalculatedHtml['editHtml'],
  127. ]);
  128. }
  129. // JAVA ONLY
  130. // ... if hcpProId is passed, get from request
  131. public function getDefaultValueForSection($patientID, $sectionTemplateID)
  132. {
  133. $contentData = [];
  134. $summaryHtml = '';
  135. $patient = Client::where('id', $patientID)->first();
  136. $sectionTemplate = SectionTemplate::where('id', $sectionTemplateID)->first();
  137. if ($sectionTemplate->is_canvas) {
  138. if (file_exists(resource_path('views/app/patient/canvas-sections/' . $sectionTemplate->internal_name . '/default.php'))) {
  139. // for canvas section where we have pro mapped data, use hcpProId
  140. $hcpPro = null;
  141. if(\request()->input('hcpProUid')) {
  142. $hcpPro = Pro::where('uid', \request()->input('hcpProUid'))->first();
  143. }
  144. $note = null;
  145. if(\request()->input('noteUid')) {
  146. $note = Note::where('uid', \request()->input('noteUid'))->first();
  147. }
  148. // default should simply assign to $contentData
  149. include(resource_path('views/app/patient/canvas-sections/' . $sectionTemplate->internal_name . '/default.php'));
  150. ob_start();
  151. include(resource_path('views/app/patient/canvas-sections/' . $sectionTemplate->internal_name . '/summary.php'));
  152. $summaryHtml = ob_get_contents();
  153. ob_end_clean();
  154. }
  155. } else {
  156. if (file_exists(storage_path('sections/' . $sectionTemplate->internal_name . '/default.php'))) {
  157. // default should simply assign to $contentData and $summaryHtml as needed
  158. include(storage_path('sections/' . $sectionTemplate->internal_name . '/default.php'));
  159. }
  160. }
  161. return [
  162. 'contentData' => $contentData,
  163. 'summaryHtml' => $summaryHtml
  164. ];
  165. }
  166. // edit hpi (structured)
  167. public function editHPI(Note $note, Point $point) {
  168. return view('app.patient.note.edit-hpi', compact('note', 'point'));
  169. }
  170. public function hpiLog(Note $note, Point $point) {
  171. return view('app.patient.note.hpi-log', compact('note', 'point'));
  172. }
  173. // review log
  174. public function reviewLog(Point $point) {
  175. return view('app.patient.note.review-log', compact('point'));
  176. }
  177. // plan log
  178. public function planLog(Point $point) {
  179. return view('app.patient.note.plan-log', compact('point'));
  180. }
  181. // print/pdf
  182. public function downloadAsPdf(Request $request, Note $note) {
  183. $patient = $note->client;
  184. if($request->input('html')) {
  185. return view('app.patient.note.pdf', compact('note', 'patient'));
  186. }
  187. else {
  188. $pdf = \PDF::loadView('app.patient.note.pdf', compact('note', 'patient'));
  189. return $pdf->stream($note->created_at .'_' . 'note.pdf');
  190. }
  191. }
  192. public function generateCC(Request $request, Note $note) {
  193. $client = $note->client;
  194. return view('app.patient.segment-templates.chief_complaint.generate', compact('note', 'client'));
  195. }
  196. public function segmentSummary(Request $request, Segment $segment) {
  197. return '<div class="mrv-content border-top px-3 pt-2 mt-3">' . @$segment->summary_html . '</div>';
  198. }
  199. public function mrvSummary(Request $request, Note $note) {
  200. return view('app.patient.segment-templates.medrisk_vigilence.summary', [
  201. 'note' => $note,
  202. 'patient' => $note->client,
  203. 'segment' => $note->coreSegment
  204. ]);
  205. }
  206. public function chartSegmentView(Request $request, Client $patient, $segmentInternalName, $view) {
  207. return view("app.patient.segment-templates.{$segmentInternalName}.{$view}", [
  208. 'patient' => $patient,
  209. 'note' => $patient->coreNote,
  210. 'segmentInternalName' => $segmentInternalName,
  211. 'closeOnSave' => true
  212. ]);
  213. }
  214. public function noteSegmentView(Request $request, Client $patient, Note $note, Segment $segment, $segmentInternalName, $view) {
  215. return view("app.patient.segment-templates.{$segmentInternalName}.{$view}", [
  216. 'patient' => $patient,
  217. 'note' => $note,
  218. 'segment' => $segment,
  219. 'segmentInternalName' => $segmentInternalName
  220. ]);
  221. }
  222. public function noteSegmentViewByName(Request $request, Note $note, $segmentInternalName, $view) {
  223. return view("app.patient.segment-templates.{$segmentInternalName}.{$view}", [
  224. 'patient' => $note->client,
  225. 'note' => $note,
  226. 'segment' => $note->coreSegment,
  227. 'segmentInternalName' => $segmentInternalName
  228. ]);
  229. }
  230. public function moduleView(Request $request, Note $note, $segmentInternalName, $view) {
  231. return view("app.patient.modules.{$segmentInternalName}.{$view}", [
  232. 'patient' => $note->client,
  233. 'note' => $note,
  234. 'segment' => $note->coreSegment,
  235. 'segmentInternalName' => $segmentInternalName
  236. ]);
  237. }
  238. public function rhsSidebar(Request $request, Client $patient, Note $note) {
  239. return view('app.patient.note.rhs-sidebar', compact('patient', 'note'));
  240. }
  241. public function medicationsCenter(Request $request, Client $patient, Note $note) {
  242. $points = $this->getPointsForWizard('MEDICATION', $patient, $note);
  243. list($medications, $counts) = $this->groupByState($points);
  244. return view('app.patient.medications-center', compact('patient', 'note', 'medications', 'counts'));
  245. }
  246. public function medicationsAddMultiPreexisting(Request $request, Note $note) {
  247. return view('app.patient.medications-add-multi-preexisting', compact('note'));
  248. }
  249. public function medicationsReconcile(Request $request, Client $patient, Note $note) {
  250. return view('app.patient.medications-reconcile', compact('patient', 'note'));
  251. }
  252. public function problemsQuickAdd(Request $request, Client $patient, Note $note) {
  253. return view('app.patient.problems-quick-add', compact('patient', 'note'));
  254. }
  255. public function problemsCenter(Request $request, Client $patient, Note $note) {
  256. $points = $this->getPointsForWizard('PROBLEM', $patient, $note);
  257. list($problems, $counts) = $this->groupByState($points);
  258. return view('app.patient.problems-center', compact('patient', 'note', 'problems', 'counts'));
  259. }
  260. public function goalsCenter(Request $request, Client $patient, Note $note) {
  261. $points = $this->getPointsForWizard('GOAL', $patient, $note, 'goal');
  262. list($goals, $counts) = $this->groupByState($points);
  263. return view('app.patient.goals-center', compact('patient', 'note', 'goals', 'counts'));
  264. }
  265. public function allergiesCenter(Request $request, Client $patient, Note $note) {
  266. $points = $this->getPointsForWizard('ALLERGY', $patient, $note);
  267. list($allergies, $counts) = $this->groupByState($points);
  268. return view('app.patient.allergies-center', compact('patient', 'note', 'allergies', 'counts'));
  269. }
  270. public function careteamCenter(Request $request, Client $patient, Note $note) {
  271. $points = $this->getPointsForWizard('CARE_TEAM_MEMBER', $patient, $note);
  272. list($careTeamMembers, $counts) = $this->groupByState($points);
  273. return view('app.patient.careteam-center', compact('patient', 'note', 'careTeamMembers', 'counts'));
  274. }
  275. private function getPointsForWizard($_category, $_patient, $_note, $_sortKey = 'name') {
  276. $query = "
  277. SELECT p.id,
  278. p.uid,
  279. p.data,
  280. p.is_removed,
  281. p.is_removed_due_to_entry_error,
  282. p.added_in_note_id,
  283. p.addition_reason_category,
  284. p.removed_in_note_id,
  285. p.removal_reason_category,
  286. p.added_in_note_uid as added_note_uid,
  287. p.added_in_note_effective_date as added_on,
  288. p.removed_in_note_uid as removed_note_uid,
  289. p.removed_in_note_effective_dateest as removed_on,
  290. np.uid as note_point_uid,
  291. p.last_child_review_point_id,
  292. p.last_child_review_point_scoped_note_id,
  293. p.last_child_plan_point_id,
  294. p.last_child_plan_point_scoped_note_id,
  295. p.last_child_review_effective_date,
  296. p.last_child_plan_effective_date,
  297. p.last_child_review_data,
  298. p.last_child_plan_data,
  299. (p.last_child_review_creator_pro_first_name || ' ' || p.last_child_review_creator_pro_last_name) as last_child_review_creator,
  300. (p.last_child_plan_creator_pro_first_name || ' ' || p.last_child_plan_creator_pro_last_name) as last_child_plan_creator,
  301. p.last_child_review_point_scoped_note_uid as last_child_review_note_uid,
  302. p.last_child_plan_point_scoped_note_uid as last_child_plan_note_uid
  303. FROM point p
  304. left join note_point np on p.id = np.point_id and np.note_id = {$_note->id} and np.is_active = TRUE
  305. WHERE p.client_id = {$_patient->id} AND p.category = '{$_category}'
  306. ORDER BY ((p.data)::json->'{$_sortKey}')::text
  307. ";
  308. return DB::select($query);
  309. }
  310. private function groupByState($points) {
  311. $pointsByType = [
  312. "ACTIVE" => [],
  313. "HISTORIC" => [],
  314. "ENTRY_ERROR" => [],
  315. ];
  316. foreach ($points as $point) {
  317. if ($point->data) {
  318. $point->data = json_decode($point->data);
  319. }
  320. if(!$point->is_removed) {
  321. $point->state = "ACTIVE";
  322. $pointsByType["ACTIVE"][] = $point;
  323. }
  324. elseif($point->is_removed) {
  325. if(!$point->is_removed_due_to_entry_error) {
  326. $point->state = "HISTORIC";
  327. $pointsByType["HISTORIC"][] = $point;
  328. }
  329. else {
  330. $point->state = "ENTRY_ERROR";
  331. $pointsByType["ENTRY_ERROR"][] = $point;
  332. }
  333. }
  334. }
  335. return [
  336. array_merge($pointsByType["ACTIVE"], $pointsByType["HISTORIC"], $pointsByType["ENTRY_ERROR"]),
  337. [
  338. "ACTIVE" => count($pointsByType["ACTIVE"]),
  339. "HISTORIC" => count($pointsByType["HISTORIC"]),
  340. "ENTRY_ERROR" => count($pointsByType["ENTRY_ERROR"]),
  341. ]
  342. ];
  343. }
  344. public function supplementsCenter(Request $request, Client $patient, Note $note) {
  345. return view('app.patient.supplements-center', compact('patient', 'note'));
  346. }
  347. public function supplementsReconcile(Request $request, Client $patient, Note $note) {
  348. return view('app.patient.supplements-reconcile', compact('patient', 'note'));
  349. }
  350. public function nutritionCenter(Request $request, Client $patient, Note $note) {
  351. return view('app.patient.nutrition-center', compact('patient', 'note'));
  352. }
  353. public function exerciseCenter(Request $request, Client $patient, Note $note) {
  354. return view('app.patient.exercise-center', compact('patient', 'note'));
  355. }
  356. public function behaviorCenter(Request $request, Client $patient, Note $note) {
  357. return view('app.patient.behavior-center', compact('patient', 'note'));
  358. }
  359. public function ccmAgreement(Request $request, Note $note) {
  360. return view('app.patient.note.ccm-agreement', compact('note'));
  361. }
  362. public function rpmAgreement(Request $request, Note $note) {
  363. return view('app.patient.note.rpm-agreement', compact('note'));
  364. }
  365. // TODO move to utility
  366. private function callJava($request, $endPoint, $data, $guestAccessCode = null)
  367. {
  368. $url = config('stag.backendUrl') . $endPoint;
  369. $response = Http::asForm()
  370. ->withHeaders([
  371. 'sessionKey' => $request->cookie('sessionKey'),
  372. 'guestAccessCode' => $guestAccessCode
  373. ])
  374. ->post($url, $data)
  375. ->json();
  376. return $response;
  377. }
  378. }