NoteController.php 18 KB

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