Point.php 21 KB

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