NoteController.php 17 KB

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