Point.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. <?php
  2. namespace App\Models;
  3. # use Illuminate\Database\Eloquent\Model;
  4. class Point extends Model
  5. {
  6. protected $table = 'point';
  7. public function client()
  8. {
  9. return $this->hasOne(Client::class, 'id', 'client_id');
  10. }
  11. public function creatorPro()
  12. {
  13. return $this->hasOne(Pro::class, 'id', 'created_by_pro_id');
  14. }
  15. public function childReviews()
  16. {
  17. return $this->hasMany(Point::class, 'parent_point_id', 'id')
  18. ->where('category', 'REVIEW')
  19. ->orderBy('created_at', 'DESC');
  20. }
  21. public function childReviewAddedInNote($_note)
  22. {
  23. $review = Point::where('added_in_note_id', $_note->id)
  24. ->where('category', 'REVIEW')
  25. ->where('parent_point_id', $this->id)
  26. ->orderBy('created_at', 'DESC')
  27. ->first();
  28. if(!!$review) {
  29. $review->data = json_decode($review->data);
  30. }
  31. return $review;
  32. }
  33. public function childPlanAddedInNote($_note)
  34. {
  35. $review = Point::where('added_in_note_id', $_note->id)
  36. ->where('category', 'PLAN')
  37. ->where('parent_point_id', $this->id)
  38. ->orderBy('created_at', 'DESC')
  39. ->first();
  40. if(!!$review) {
  41. $review->data = json_decode($review->data);
  42. }
  43. return $review;
  44. }
  45. public function lastChildReview()
  46. {
  47. return $this->hasOne(Point::class, 'id', 'last_child_review_point_id');
  48. }
  49. public function lastChildReviewNote()
  50. {
  51. return $this->hasOne(Note::class, 'id', 'last_child_review_point_scoped_note_id');
  52. }
  53. public function childPlans()
  54. {
  55. return $this->hasMany(Point::class, 'parent_point_id', 'id')
  56. ->where('category', 'PLAN')
  57. ->orderBy('created_at', 'DESC');
  58. }
  59. public function lastChildPlan()
  60. {
  61. return $this->hasOne(Point::class, 'id', 'last_child_plan_point_id');
  62. }
  63. public function lastChildPlanNote()
  64. {
  65. return $this->hasOne(Note::class, 'id', 'last_child_plan_point_scoped_note_id');
  66. }
  67. public function coreChildReview()
  68. {
  69. return $this->hasOne(Point::class, 'id', 'core_child_review_point_id');
  70. }
  71. public function coreChildPlan()
  72. {
  73. return $this->hasOne(Point::class, 'id', 'core_child_plan_point_id');
  74. }
  75. public function parentPoint()
  76. {
  77. return $this->hasOne(Point::class, 'id', 'parent_point_id');
  78. }
  79. public function client()
  80. {
  81. return $this->hasOne(Client::class, 'id', 'client_id');
  82. }
  83. public function note()
  84. {
  85. return $this->hasOne(Note::class, 'id', 'added_in_note_id');
  86. }
  87. public function relevanceToNote($_note) {
  88. return NotePoint
  89. ::where('is_active', true)
  90. ->where('note_id', $_note->id)
  91. ->where('point_id', $this->id)
  92. ->first();
  93. }
  94. // NOTE: $point is not a point instance - but a raw object containing point data
  95. public static function getCurrentAndPreviousChildReviews($point, $note) {
  96. $current = null;
  97. $previous = null;
  98. $point = Point::where('id', $point->id)->first();
  99. if ($point->lastChildReview) {
  100. if ($point->last_child_review_point_scoped_note_id === $note->id) {
  101. if($point->lastChildReview->data) {
  102. $current = $point->lastChildReview;
  103. $current->data = json_decode($current->data);
  104. }
  105. $previous = \App\Models\Point::where('id', '<', $point->lastChildReview->id)
  106. ->where('category', 'REVIEW')
  107. ->where('parent_point_id', $point->id)
  108. ->orderBy('id', 'DESC')
  109. ->first();
  110. if($previous && $previous->data) {
  111. $previous->data = json_decode($previous->data);
  112. }
  113. }
  114. else {
  115. if($point->lastChildReview->data) {
  116. $previous = $point->lastChildReview;
  117. $previous->data = json_decode($previous->data);
  118. }
  119. }
  120. }
  121. return [$current, $previous];
  122. }
  123. // NOTE: $point is not a point instance - but a raw object containing point data
  124. public static function getCurrentAndPreviousChildPlans($point, $note) {
  125. $current = null;
  126. $previous = null;
  127. $point = Point::where('id', $point->id)->first();
  128. if ($point->lastChildPlan) {
  129. if ($point->last_child_plan_point_scoped_note_id === $note->id) {
  130. if($point->lastChildPlan->data) {
  131. $current = $point->lastChildPlan;
  132. $current->data = json_decode($current->data);
  133. }
  134. $previous = \App\Models\Point::where('id', '<', $point->lastChildPlan->id)
  135. ->where('category', 'PLAN')
  136. ->where('parent_point_id', $point->id)
  137. ->orderBy('id', 'DESC')
  138. ->first();
  139. if($previous && $previous->data) {
  140. $previous->data = json_decode($previous->data);
  141. }
  142. }
  143. else {
  144. if($point->lastChildPlan->data) {
  145. $previous = $point->lastChildPlan;
  146. $previous->data = json_decode($previous->data);
  147. }
  148. }
  149. }
  150. return [$current, $previous];
  151. }
  152. public static function getGlobalSingletonOfCategory(Client $_patient, String $_category, $_assoc = false) {
  153. $point = Point
  154. ::where('client_id', $_patient->id)
  155. ->where('category', $_category)
  156. ->orderBy('created_at', 'DESC')
  157. ->first();
  158. if ($point && @$point->data) {
  159. $point->data = json_decode($point->data, $_assoc);
  160. }
  161. return $point;
  162. }
  163. public static function getIntakePointsOfCategory(Client $_patient, String $_category, Note $_note, $_assoc = false) {
  164. $points = Point
  165. ::where('client_id', $_patient->id)
  166. ->where('category', $_category)
  167. ->where('is_removed_due_to_entry_error', false)
  168. ->whereRaw("(is_removed = TRUE OR addition_reason_category != 'DURING_VISIT' OR added_in_note_id != {$_note->id})")
  169. ->where(function ($query1) use ($_note) {
  170. $query1
  171. ->where(function ($query2) use ($_note) {
  172. $query2->where('is_removed', false)
  173. ->where('addition_reason_category', 'ON_INTAKE')
  174. ->where('added_in_note_id', $_note->id);
  175. })
  176. ->orWhere(function ($query2) use ($_note) {
  177. $query2->where('is_removed', true)
  178. ->where('removal_reason_category', 'ON_INTAKE')
  179. ->where('removed_in_note_id', $_note->id);
  180. })
  181. ->orWhere('last_child_review_point_scoped_note_id', $_note->id)
  182. ->orWhereRaw("(SELECT count(id) from note_point WHERE is_active Is TRUE AND note_id = {$_note->id} AND point_id = point.id) > 0");
  183. })
  184. ->orderBy('created_at')
  185. ->get();
  186. foreach ($points as $point) {
  187. if ($point->data) {
  188. $point->data = json_decode($point->data, $_assoc);
  189. }
  190. }
  191. return $points;
  192. }
  193. public static function getIntakePoints(Client $_patient, Note $_note, $_assoc = false) {
  194. $points = Point
  195. ::where('client_id', $_patient->id)
  196. ->where('is_removed_due_to_entry_error', false)
  197. ->whereRaw("(is_removed = TRUE OR addition_reason_category != 'DURING_VISIT' OR added_in_note_id != {$_note->id})")
  198. ->where(function ($query1) use ($_note) {
  199. $query1
  200. ->where(function ($query2) use ($_note) {
  201. $query2->where('is_removed', false)
  202. ->where('addition_reason_category', 'ON_INTAKE')
  203. ->where('added_in_note_id', $_note->id);
  204. })
  205. ->orWhere(function ($query2) use ($_note) {
  206. $query2->where('is_removed', true)
  207. ->where('removal_reason_category', 'ON_INTAKE')
  208. ->where('removed_in_note_id', $_note->id);
  209. })
  210. ->orWhere('last_child_review_point_scoped_note_id', $_note->id)
  211. ->orWhereRaw("(SELECT count(id) from note_point WHERE is_active Is TRUE AND note_id = {$_note->id} AND point_id = point.id) > 0");
  212. })
  213. ->orderBy('created_at')
  214. ->get();
  215. foreach ($points as $point) {
  216. if ($point->data) {
  217. $point->data = json_decode($point->data, $_assoc);
  218. }
  219. }
  220. return $points;
  221. }
  222. public static function getIntakePointsWithChildReview(Client $_patient, Note $_note, $_assoc = false) {
  223. $points = Point
  224. ::where('client_id', $_patient->id)
  225. ->where('is_removed_due_to_entry_error', false)
  226. ->whereRaw("(is_removed = TRUE OR addition_reason_category != 'DURING_VISIT' OR added_in_note_id != {$_note->id})")
  227. ->where(function ($query1) use ($_note) {
  228. $query1
  229. ->where('last_child_review_point_scoped_note_id', $_note->id)
  230. ->orWhereRaw("(SELECT count(id) from note_point WHERE is_active IS TRUE AND note_id = {$_note->id} AND point_id = point.id) > 0");
  231. })
  232. ->orderBy('created_at')
  233. ->get();
  234. foreach ($points as $point) {
  235. if ($point->data) {
  236. $point->data = json_decode($point->data, $_assoc);
  237. }
  238. }
  239. return $points;
  240. }
  241. public static function getPlanPointsOfCategory(Client $_patient, String $_category, Note $_note, $_assoc = false) {
  242. $points = Point
  243. ::where('client_id', $_patient->id)
  244. ->where('category', $_category)
  245. ->where('is_removed_due_to_entry_error', false)
  246. ->where(function ($query1) use ($_note) {
  247. $query1
  248. ->where(function ($query2) use ($_note) {
  249. $query2->where('is_removed', false)
  250. ->where('addition_reason_category', 'DURING_VISIT')
  251. ->where('added_in_note_id', $_note->id);
  252. })
  253. ->orWhere(function ($query2) use ($_note) {
  254. $query2->where('is_removed', true)
  255. ->where('removal_reason_category', 'DURING_VISIT')
  256. ->where('removed_in_note_id', $_note->id);
  257. })
  258. ->orWhere('last_child_plan_point_scoped_note_id', $_note->id)
  259. ->orWhereRaw("(SELECT count(id) from note_point WHERE is_active IS TRUE AND note_id = {$_note->id} AND point_id = point.id) > 0");
  260. })
  261. ->orderBy('created_at')
  262. ->get();
  263. foreach ($points as $point) {
  264. if ($point->data) {
  265. $point->data = json_decode($point->data, $_assoc);
  266. }
  267. }
  268. return $points;
  269. }
  270. public static function getPlanPoints(Client $_patient, Note $_note, $_assoc = false) {
  271. $points = Point
  272. ::where('client_id', $_patient->id)
  273. ->where('is_removed_due_to_entry_error', false)
  274. ->where(function ($query1) use ($_note) {
  275. $query1
  276. ->where(function ($query2) use ($_note) {
  277. $query2->where('is_removed', false)
  278. ->where('addition_reason_category', 'DURING_VISIT')
  279. ->where('added_in_note_id', $_note->id);
  280. })
  281. ->orWhere(function ($query2) use ($_note) {
  282. $query2->where('is_removed', true)
  283. ->where('removal_reason_category', 'DURING_VISIT')
  284. ->where('removed_in_note_id', $_note->id);
  285. })
  286. ->orWhere('last_child_plan_point_scoped_note_id', $_note->id)
  287. ->orWhereIn('category', ['WEIGHT_LOSS_INTAKE']);
  288. })
  289. ->orderBy('created_at')
  290. ->get();
  291. foreach ($points as $point) {
  292. if ($point->data) {
  293. $point->data = json_decode($point->data, $_assoc);
  294. }
  295. }
  296. return $points;
  297. }
  298. public static function getUnifiedPointsOfCategory(Client $_patient, String $_category, Note $_note, $_assoc = false) {
  299. $points = Point
  300. ::where('client_id', $_patient->id)
  301. ->where('category', $_category)
  302. ->where('is_removed_due_to_entry_error', false)
  303. ->where(function ($query1) use ($_note) {
  304. $query1
  305. ->where(function ($query2) use ($_note) { // added on_intake on this note
  306. $query2->where('is_removed', false)
  307. ->where('addition_reason_category', 'ON_INTAKE')
  308. ->where('added_in_note_id', $_note->id);
  309. })
  310. ->orWhere(function ($query2) use ($_note) { // removed on_intake on this note
  311. $query2->where('is_removed', true)
  312. ->where('removal_reason_category', 'ON_INTAKE')
  313. ->where('removed_in_note_id', $_note->id);
  314. })
  315. ->orWhere('last_child_review_point_scoped_note_id', $_note->id) // review added during this note
  316. ->orWhere(function ($query2) use ($_note) { // added during_visit on this note
  317. $query2->where('is_removed', false)
  318. ->where('addition_reason_category', 'DURING_VISIT')
  319. ->where('added_in_note_id', $_note->id);
  320. })
  321. ->orWhere(function ($query2) use ($_note) { // removed during_visit on this note
  322. $query2->where('is_removed', true)
  323. ->where('removal_reason_category', 'DURING_VISIT')
  324. ->where('removed_in_note_id', $_note->id);
  325. })
  326. ->orWhere('last_child_plan_point_scoped_note_id', $_note->id) // plan added during this note
  327. // marked relevant to this note
  328. ->orWhereRaw("(SELECT count(id) from note_point WHERE is_active IS TRUE AND note_id = {$_note->id} AND point_id = point.id) > 0");
  329. })
  330. ->orderBy('created_at')
  331. ->get();
  332. foreach ($points as $point) {
  333. if ($point->data) {
  334. $point->data = json_decode($point->data, $_assoc);
  335. }
  336. }
  337. return $points;
  338. }
  339. public static function getPointsOfCategory(Client $_patient, String $_category, $_assoc = false) {
  340. $points = Point
  341. ::where('client_id', $_patient->id)
  342. ->where('category', $_category)
  343. ->where('is_removed', false)
  344. ->orderBy('created_at')
  345. ->get();
  346. foreach ($points as $point) {
  347. if ($point->data) {
  348. $point->data = json_decode($point->data, $_assoc);
  349. }
  350. }
  351. return $points;
  352. }
  353. public static function getPointsOfCategoryExceptEntryError(Client $_patient, Note $_note, String $_category, $_assoc = false) {
  354. $points = Point
  355. ::where('client_id', $_patient->id)
  356. ->where('category', $_category)
  357. ->where(function ($query2) use ($_note) {
  358. $query2->where('is_removed', false)
  359. ->orWhereRaw("(is_removed_due_to_entry_error IS NOT TRUE AND ((SELECT count(id) from note_point WHERE is_active IS TRUE AND note_id = {$_note->id} AND point_id = point.id) > 0))");
  360. })
  361. ->orderBy('created_at')
  362. ->get();
  363. foreach ($points as $point) {
  364. if ($point->data) {
  365. $point->data = json_decode($point->data, $_assoc);
  366. }
  367. }
  368. return $points;
  369. }
  370. public static function getNumPointsOfCategory(Client $_patient, String $_category) {
  371. return Point
  372. ::where('client_id', $_patient->id)
  373. ->where('category', $_category)
  374. ->where('is_removed', false)
  375. ->count();
  376. }
  377. public static function getPointsOfCategoryExtended(Client $_patient, String $_category, Note $_note, $excludeEntryError = false) {
  378. $points = Point
  379. ::where('client_id', $_patient->id)
  380. ->where('category', $_category);
  381. if($excludeEntryError) {
  382. $points = $points->where('is_removed_due_to_entry_error', FALSE);
  383. }
  384. if($_category !== 'GOAL') {
  385. $points = $points->orderByRaw("((data)::json->'name')::text ASC");
  386. }
  387. else {
  388. $points = $points->orderByRaw("((data)::json->'goal')::text ASC");
  389. }
  390. $points = $points->get();
  391. $pointsByType = [
  392. "ACTIVE" => [],
  393. "HISTORIC" => [],
  394. "ENTRY_ERROR" => [],
  395. ];
  396. foreach ($points as $point) {
  397. if ($point->data) {
  398. $point->data = json_decode($point->data);
  399. }
  400. if(!$point->is_removed) {
  401. $point->state = "ACTIVE";
  402. $pointsByType["ACTIVE"][] = $point;
  403. }
  404. elseif($point->is_removed) {
  405. if(!$point->is_removed_due_to_entry_error) {
  406. $point->state = "HISTORIC";
  407. $pointsByType["HISTORIC"][] = $point;
  408. }
  409. else {
  410. $point->state = "ENTRY_ERROR";
  411. $pointsByType["ENTRY_ERROR"][] = $point;
  412. }
  413. }
  414. }
  415. $points = array_merge($pointsByType["ACTIVE"], $pointsByType["HISTORIC"], $pointsByType["ENTRY_ERROR"]);
  416. return [
  417. $points,
  418. [
  419. "ACTIVE" => count($pointsByType["ACTIVE"]),
  420. "HISTORIC" => count($pointsByType["HISTORIC"]),
  421. "ENTRY_ERROR" => count($pointsByType["ENTRY_ERROR"]),
  422. ]
  423. ];
  424. }
  425. public static function getOnlyPointOfCategory(Client $_patient, String $_category) {
  426. $point = Point
  427. ::where('client_id', $_patient->id)
  428. ->where('category', $_category)
  429. ->first();
  430. if ($point && $point->data) {
  431. $point->data = json_decode($point->data);
  432. }
  433. return $point;
  434. }
  435. public static function getOnlyTopLevelPointOfCategory(Note $_note, String $_category, $_assoc = false) {
  436. $point = Point
  437. ::where('client_id', $_note->client_id)
  438. ->where('category', $_category)
  439. ->where('intention', 'TOP_LEVEL')
  440. ->first();
  441. if ($point && $point->data) {
  442. $point->data = json_decode($point->data, $_assoc);
  443. }
  444. return $point;
  445. }
  446. public static function getOrCreateOnlyTopLevelPointOfCategory(Note $_note, String $_category, $_sessionKey, $_assoc = false) {
  447. $point = Point
  448. ::where('client_id', $_note->client_id)
  449. ->where('category', $_category)
  450. ->where('intention', 'TOP_LEVEL')
  451. ->first();
  452. if(!$point) {
  453. $response = callJava('/visitPoint/addTopLevel', [
  454. "category" => $_category,
  455. "data" => '{}',
  456. "noteUid" => $_note->uid,
  457. "additionReasonCategory" => 'ON_INTAKE',
  458. ], $_sessionKey);
  459. // TODO: dont assume success
  460. $point = Point
  461. ::where('client_id', $_note->client_id)
  462. ->where('category', $_category)
  463. ->where('intention', 'TOP_LEVEL')
  464. ->first();
  465. }
  466. if ($point && $point->data) {
  467. $point->data = json_decode($point->data, $_assoc);
  468. }
  469. return $point;
  470. }
  471. public static function fillPointStateAndBadge(Point $point, Note $note)
  472. {
  473. // state
  474. if (!$point->is_removed) {
  475. $point->state = "ACTIVE";
  476. } elseif ($point->is_removed) {
  477. if (!$point->is_removed_due_to_entry_error) {
  478. $point->state = "HISTORIC";
  479. } else {
  480. $point->state = "ENTRY_ERROR";
  481. }
  482. }
  483. // added/removed info
  484. if ($point->state === 'ACTIVE') {
  485. if ($point->added_in_note_id === $note->id && $point->addition_reason_category === 'DURING_VISIT') {
  486. $point->badge = 'Added During Visit';
  487. } elseif ($point->added_in_note_id === $note->id && $point->addition_reason_category === 'ON_INTAKE') {
  488. $point->badge = 'New Record - Pre-existing';
  489. } elseif ($point->added_in_note_id !== $note->id) {
  490. $point->badge = 'Record Present Before Visit';
  491. }
  492. } elseif ($point->state === 'HISTORIC') {
  493. if ($point->removed_in_note_id === $note->id && $point->removal_reason_category === 'DURING_VISIT') {
  494. $point->badge = 'Removed During Visit';
  495. } elseif ($point->removed_in_note_id === $note->id && $point->removal_reason_category === 'ON_INTAKE') {
  496. $point->badge = 'Marked Historic';
  497. } elseif ($point->removed_in_note_id !== $note->id) {
  498. $point->badge = 'Historic Record Removed in a Previous Visit';
  499. }
  500. } elseif ($point->state === 'ENTRY_ERROR') {
  501. if ($point->removed_in_note_id === $note->id) {
  502. $point->badge = 'Marked as Entry Error During Visit';
  503. }
  504. }
  505. return $point;
  506. }
  507. }