Point.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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 getPointsOfCategoryExceptEntryError(Client $_patient, Note $_note, String $_category, $_assoc = false) {
  350. $points = Point
  351. ::where('client_id', $_patient->id)
  352. ->where('category', $_category)
  353. ->where(function ($query2) use ($_note) {
  354. $query2->where('is_removed', false)
  355. ->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))");
  356. })
  357. ->orderBy('created_at')
  358. ->get();
  359. foreach ($points as $point) {
  360. if ($point->data) {
  361. $point->data = json_decode($point->data, $_assoc);
  362. }
  363. }
  364. return $points;
  365. }
  366. public static function getNumPointsOfCategory(Client $_patient, String $_category) {
  367. return Point
  368. ::where('client_id', $_patient->id)
  369. ->where('category', $_category)
  370. ->where('is_removed', false)
  371. ->count();
  372. }
  373. public static function getPointsOfCategoryExtended(Client $_patient, String $_category, Note $_note, $excludeEntryError = false) {
  374. $points = Point
  375. ::where('client_id', $_patient->id)
  376. ->where('category', $_category);
  377. if($excludeEntryError) {
  378. $points = $points->where('is_removed_due_to_entry_error', FALSE);
  379. }
  380. if($_category !== 'GOAL') {
  381. $points = $points->orderByRaw("((data)::json->'name')::text ASC");
  382. }
  383. else {
  384. $points = $points->orderByRaw("((data)::json->'goal')::text ASC");
  385. }
  386. $points = $points->get();
  387. $pointsByType = [
  388. "ACTIVE" => [],
  389. "HISTORIC" => [],
  390. "ENTRY_ERROR" => [],
  391. ];
  392. foreach ($points as $point) {
  393. if ($point->data) {
  394. $point->data = json_decode($point->data);
  395. }
  396. if(!$point->is_removed) {
  397. $point->state = "ACTIVE";
  398. $pointsByType["ACTIVE"][] = $point;
  399. }
  400. elseif($point->is_removed) {
  401. if(!$point->is_removed_due_to_entry_error) {
  402. $point->state = "HISTORIC";
  403. $pointsByType["HISTORIC"][] = $point;
  404. }
  405. else {
  406. $point->state = "ENTRY_ERROR";
  407. $pointsByType["ENTRY_ERROR"][] = $point;
  408. }
  409. }
  410. }
  411. $points = array_merge($pointsByType["ACTIVE"], $pointsByType["HISTORIC"], $pointsByType["ENTRY_ERROR"]);
  412. return [
  413. $points,
  414. [
  415. "ACTIVE" => count($pointsByType["ACTIVE"]),
  416. "HISTORIC" => count($pointsByType["HISTORIC"]),
  417. "ENTRY_ERROR" => count($pointsByType["ENTRY_ERROR"]),
  418. ]
  419. ];
  420. }
  421. public static function getOnlyPointOfCategory(Client $_patient, String $_category) {
  422. $point = Point
  423. ::where('client_id', $_patient->id)
  424. ->where('category', $_category)
  425. ->first();
  426. if ($point && $point->data) {
  427. $point->data = json_decode($point->data);
  428. }
  429. return $point;
  430. }
  431. public static function getOnlyTopLevelPointOfCategory(Note $_note, String $_category, $_assoc = false) {
  432. $point = Point
  433. ::where('client_id', $_note->client_id)
  434. ->where('category', $_category)
  435. ->where('intention', 'TOP_LEVEL')
  436. ->first();
  437. if ($point && $point->data) {
  438. $point->data = json_decode($point->data, $_assoc);
  439. }
  440. return $point;
  441. }
  442. public static function getOrCreateOnlyTopLevelPointOfCategory(Note $_note, String $_category, $_sessionKey, $_assoc = false) {
  443. $point = Point
  444. ::where('client_id', $_note->client_id)
  445. ->where('category', $_category)
  446. ->where('intention', 'TOP_LEVEL')
  447. ->first();
  448. if(!$point) {
  449. $response = callJava('/visitPoint/addTopLevel', [
  450. "category" => $_category,
  451. "data" => '{}',
  452. "noteUid" => $_note->uid,
  453. "additionReasonCategory" => 'ON_INTAKE',
  454. ], $_sessionKey);
  455. // TODO: dont assume success
  456. $point = Point
  457. ::where('client_id', $_note->client_id)
  458. ->where('category', $_category)
  459. ->where('intention', 'TOP_LEVEL')
  460. ->first();
  461. }
  462. if ($point && $point->data) {
  463. $point->data = json_decode($point->data, $_assoc);
  464. }
  465. return $point;
  466. }
  467. public static function fillPointStateAndBadge(Point $point, Note $note)
  468. {
  469. // state
  470. if (!$point->is_removed) {
  471. $point->state = "ACTIVE";
  472. } elseif ($point->is_removed) {
  473. if (!$point->is_removed_due_to_entry_error) {
  474. $point->state = "HISTORIC";
  475. } else {
  476. $point->state = "ENTRY_ERROR";
  477. }
  478. }
  479. // added/removed info
  480. if ($point->state === 'ACTIVE') {
  481. if ($point->added_in_note_id === $note->id && $point->addition_reason_category === 'DURING_VISIT') {
  482. $point->badge = 'Added During Visit';
  483. } elseif ($point->added_in_note_id === $note->id && $point->addition_reason_category === 'ON_INTAKE') {
  484. $point->badge = 'New Record - Pre-existing';
  485. } elseif ($point->added_in_note_id !== $note->id) {
  486. $point->badge = 'Record Present Before Visit';
  487. }
  488. } elseif ($point->state === 'HISTORIC') {
  489. if ($point->removed_in_note_id === $note->id && $point->removal_reason_category === 'DURING_VISIT') {
  490. $point->badge = 'Removed During Visit';
  491. } elseif ($point->removed_in_note_id === $note->id && $point->removal_reason_category === 'ON_INTAKE') {
  492. $point->badge = 'Marked Historic';
  493. } elseif ($point->removed_in_note_id !== $note->id) {
  494. $point->badge = 'Historic Record Removed in a Previous Visit';
  495. }
  496. } elseif ($point->state === 'ENTRY_ERROR') {
  497. if ($point->removed_in_note_id === $note->id) {
  498. $point->badge = 'Marked as Entry Error During Visit';
  499. }
  500. }
  501. return $point;
  502. }
  503. }