NoteController.php 18 KB

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