Point.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. public static function getGlobalSingletonOfCategory(Client $_patient, String $_category, $_assoc = false) {
  91. $point = Point
  92. ::where('client_id', $_patient->id)
  93. ->where('category', $_category)
  94. ->orderBy('created_at', 'DESC')
  95. ->first();
  96. if ($point && @$point->data) {
  97. $point->data = json_decode($point->data, $_assoc);
  98. }
  99. return $point;
  100. }
  101. public static function getIntakePointsOfCategory(Client $_patient, String $_category, Note $_note, $_assoc = false) {
  102. $points = Point
  103. ::where('client_id', $_patient->id)
  104. ->where('category', $_category)
  105. ->where('is_removed_due_to_entry_error', false)
  106. ->whereRaw("(is_removed = TRUE OR addition_reason_category != 'DURING_VISIT' OR added_in_note_id != {$_note->id})")
  107. ->where(function ($query1) use ($_note) {
  108. $query1
  109. ->where(function ($query2) use ($_note) {
  110. $query2->where('is_removed', false)
  111. ->where('addition_reason_category', 'ON_INTAKE')
  112. ->where('added_in_note_id', $_note->id);
  113. })
  114. ->orWhere(function ($query2) use ($_note) {
  115. $query2->where('is_removed', true)
  116. ->where('removal_reason_category', 'ON_INTAKE')
  117. ->where('removed_in_note_id', $_note->id);
  118. })
  119. ->orWhere('last_child_review_point_scoped_note_id', $_note->id)
  120. ->orWhereRaw("(SELECT count(id) from note_point WHERE is_active Is TRUE AND note_id = {$_note->id} AND point_id = point.id) > 0");
  121. })
  122. ->orderBy('created_at')
  123. ->get();
  124. foreach ($points as $point) {
  125. if ($point->data) {
  126. $point->data = json_decode($point->data, $_assoc);
  127. }
  128. }
  129. return $points;
  130. }
  131. public static function getIntakePoints(Client $_patient, Note $_note, $_assoc = false) {
  132. $points = Point
  133. ::where('client_id', $_patient->id)
  134. ->where('is_removed_due_to_entry_error', false)
  135. ->whereRaw("(is_removed = TRUE OR addition_reason_category != 'DURING_VISIT' OR added_in_note_id != {$_note->id})")
  136. ->where(function ($query1) use ($_note) {
  137. $query1
  138. ->where(function ($query2) use ($_note) {
  139. $query2->where('is_removed', false)
  140. ->where('addition_reason_category', 'ON_INTAKE')
  141. ->where('added_in_note_id', $_note->id);
  142. })
  143. ->orWhere(function ($query2) use ($_note) {
  144. $query2->where('is_removed', true)
  145. ->where('removal_reason_category', 'ON_INTAKE')
  146. ->where('removed_in_note_id', $_note->id);
  147. })
  148. ->orWhere('last_child_review_point_scoped_note_id', $_note->id)
  149. ->orWhereRaw("(SELECT count(id) from note_point WHERE is_active Is TRUE AND note_id = {$_note->id} AND point_id = point.id) > 0");
  150. })
  151. ->orderBy('created_at')
  152. ->get();
  153. foreach ($points as $point) {
  154. if ($point->data) {
  155. $point->data = json_decode($point->data, $_assoc);
  156. }
  157. }
  158. return $points;
  159. }
  160. public static function getIntakePointsWithChildReview(Client $_patient, Note $_note, $_assoc = false) {
  161. $points = Point
  162. ::where('client_id', $_patient->id)
  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('last_child_review_point_scoped_note_id', $_note->id)
  168. ->orWhereRaw("(SELECT count(id) from note_point WHERE is_active IS TRUE AND note_id = {$_note->id} AND point_id = point.id) > 0");
  169. })
  170. ->orderBy('created_at')
  171. ->get();
  172. foreach ($points as $point) {
  173. if ($point->data) {
  174. $point->data = json_decode($point->data, $_assoc);
  175. }
  176. }
  177. return $points;
  178. }
  179. public static function getPlanPointsOfCategory(Client $_patient, String $_category, Note $_note, $_assoc = false) {
  180. $points = Point
  181. ::where('client_id', $_patient->id)
  182. ->where('category', $_category)
  183. ->where('is_removed_due_to_entry_error', false)
  184. ->where(function ($query1) use ($_note) {
  185. $query1
  186. ->where(function ($query2) use ($_note) {
  187. $query2->where('is_removed', false)
  188. ->where('addition_reason_category', 'DURING_VISIT')
  189. ->where('added_in_note_id', $_note->id);
  190. })
  191. ->orWhere(function ($query2) use ($_note) {
  192. $query2->where('is_removed', true)
  193. ->where('removal_reason_category', 'DURING_VISIT')
  194. ->where('removed_in_note_id', $_note->id);
  195. })
  196. ->orWhere('last_child_plan_point_scoped_note_id', $_note->id)
  197. ->orWhereRaw("(SELECT count(id) from note_point WHERE is_active IS TRUE AND note_id = {$_note->id} AND point_id = point.id) > 0");
  198. })
  199. ->orderBy('created_at')
  200. ->get();
  201. foreach ($points as $point) {
  202. if ($point->data) {
  203. $point->data = json_decode($point->data, $_assoc);
  204. }
  205. }
  206. return $points;
  207. }
  208. public static function getPlanPoints(Client $_patient, Note $_note, $_assoc = false) {
  209. $points = Point
  210. ::where('client_id', $_patient->id)
  211. ->where('is_removed_due_to_entry_error', false)
  212. ->where(function ($query1) use ($_note) {
  213. $query1
  214. ->where(function ($query2) use ($_note) {
  215. $query2->where('is_removed', false)
  216. ->where('addition_reason_category', 'DURING_VISIT')
  217. ->where('added_in_note_id', $_note->id);
  218. })
  219. ->orWhere(function ($query2) use ($_note) {
  220. $query2->where('is_removed', true)
  221. ->where('removal_reason_category', 'DURING_VISIT')
  222. ->where('removed_in_note_id', $_note->id);
  223. })
  224. ->orWhere('last_child_plan_point_scoped_note_id', $_note->id)
  225. ->orWhereIn('category', ['WEIGHT_LOSS_INTAKE']);
  226. })
  227. ->orderBy('created_at')
  228. ->get();
  229. foreach ($points as $point) {
  230. if ($point->data) {
  231. $point->data = json_decode($point->data, $_assoc);
  232. }
  233. }
  234. return $points;
  235. }
  236. public static function getUnifiedPointsOfCategory(Client $_patient, String $_category, Note $_note, $_assoc = false) {
  237. $points = Point
  238. ::where('client_id', $_patient->id)
  239. ->where('category', $_category)
  240. ->where('is_removed_due_to_entry_error', false)
  241. ->where(function ($query1) use ($_note) {
  242. $query1
  243. ->where(function ($query2) use ($_note) { // added on_intake on this note
  244. $query2->where('is_removed', false)
  245. ->where('addition_reason_category', 'ON_INTAKE')
  246. ->where('added_in_note_id', $_note->id);
  247. })
  248. ->orWhere(function ($query2) use ($_note) { // removed on_intake on this note
  249. $query2->where('is_removed', true)
  250. ->where('removal_reason_category', 'ON_INTAKE')
  251. ->where('removed_in_note_id', $_note->id);
  252. })
  253. ->orWhere('last_child_review_point_scoped_note_id', $_note->id) // review added during this note
  254. ->orWhere(function ($query2) use ($_note) { // added during_visit on this note
  255. $query2->where('is_removed', false)
  256. ->where('addition_reason_category', 'DURING_VISIT')
  257. ->where('added_in_note_id', $_note->id);
  258. })
  259. ->orWhere(function ($query2) use ($_note) { // removed during_visit on this note
  260. $query2->where('is_removed', true)
  261. ->where('removal_reason_category', 'DURING_VISIT')
  262. ->where('removed_in_note_id', $_note->id);
  263. })
  264. ->orWhere('last_child_plan_point_scoped_note_id', $_note->id) // plan added during this note
  265. // marked relevant to this note
  266. ->orWhereRaw("(SELECT count(id) from note_point WHERE is_active IS TRUE AND note_id = {$_note->id} AND point_id = point.id) > 0");
  267. })
  268. ->orderBy('created_at')
  269. ->get();
  270. foreach ($points as $point) {
  271. if ($point->data) {
  272. $point->data = json_decode($point->data, $_assoc);
  273. }
  274. }
  275. return $points;
  276. }
  277. public static function getPointsOfCategory(Client $_patient, String $_category, $_assoc = false) {
  278. $points = Point
  279. ::where('client_id', $_patient->id)
  280. ->where('category', $_category)
  281. ->where('is_removed', false)
  282. ->orderBy('created_at')
  283. ->get();
  284. foreach ($points as $point) {
  285. if ($point->data) {
  286. $point->data = json_decode($point->data, $_assoc);
  287. }
  288. }
  289. return $points;
  290. }
  291. public static function getNumPointsOfCategory(Client $_patient, String $_category) {
  292. return Point
  293. ::where('client_id', $_patient->id)
  294. ->where('category', $_category)
  295. ->where('is_removed', false)
  296. ->count();
  297. }
  298. public static function getPointsOfCategoryExtended(Client $_patient, String $_category, Note $_note, $excludeEntryError = false) {
  299. $points = Point
  300. ::where('client_id', $_patient->id)
  301. ->where('category', $_category);
  302. if($excludeEntryError) {
  303. $points = $points->where('is_removed_due_to_entry_error', FALSE);
  304. }
  305. if($_category !== 'GOAL') {
  306. $points = $points->orderByRaw("((data)::json->'name')::text ASC");
  307. }
  308. else {
  309. $points = $points->orderByRaw("((data)::json->'goal')::text ASC");
  310. }
  311. $points = $points->get();
  312. $pointsByType = [
  313. "ACTIVE" => [],
  314. "HISTORIC" => [],
  315. "ENTRY_ERROR" => [],
  316. ];
  317. foreach ($points as $point) {
  318. if ($point->data) {
  319. $point->data = json_decode($point->data);
  320. }
  321. if(!$point->is_removed) {
  322. $point->state = "ACTIVE";
  323. $pointsByType["ACTIVE"][] = $point;
  324. }
  325. elseif($point->is_removed) {
  326. if(!$point->is_removed_due_to_entry_error) {
  327. $point->state = "HISTORIC";
  328. $pointsByType["HISTORIC"][] = $point;
  329. }
  330. else {
  331. $point->state = "ENTRY_ERROR";
  332. $pointsByType["ENTRY_ERROR"][] = $point;
  333. }
  334. }
  335. }
  336. $points = array_merge($pointsByType["ACTIVE"], $pointsByType["HISTORIC"], $pointsByType["ENTRY_ERROR"]);
  337. return [
  338. $points,
  339. [
  340. "ACTIVE" => count($pointsByType["ACTIVE"]),
  341. "HISTORIC" => count($pointsByType["HISTORIC"]),
  342. "ENTRY_ERROR" => count($pointsByType["ENTRY_ERROR"]),
  343. ]
  344. ];
  345. }
  346. public static function getOnlyPointOfCategory(Client $_patient, String $_category) {
  347. $point = Point
  348. ::where('client_id', $_patient->id)
  349. ->where('category', $_category)
  350. ->first();
  351. if ($point && $point->data) {
  352. $point->data = json_decode($point->data);
  353. }
  354. return $point;
  355. }
  356. public static function getOnlyTopLevelPointOfCategory(Note $_note, String $_category, $_assoc = false) {
  357. $point = Point
  358. ::where('client_id', $_note->client_id)
  359. ->where('category', $_category)
  360. ->where('intention', 'TOP_LEVEL')
  361. ->first();
  362. if ($point && $point->data) {
  363. $point->data = json_decode($point->data, $_assoc);
  364. }
  365. return $point;
  366. }
  367. public static function getOrCreateOnlyTopLevelPointOfCategory(Note $_note, String $_category, $_sessionKey, $_assoc = false) {
  368. $point = Point
  369. ::where('client_id', $_note->client_id)
  370. ->where('category', $_category)
  371. ->where('intention', 'TOP_LEVEL')
  372. ->first();
  373. if(!$point) {
  374. $response = callJava('/visitPoint/addTopLevel', [
  375. "category" => $_category,
  376. "data" => '{}',
  377. "noteUid" => $_note->uid,
  378. "additionReasonCategory" => 'ON_INTAKE',
  379. ], $_sessionKey);
  380. // TODO: dont assume success
  381. $point = Point
  382. ::where('client_id', $_note->client_id)
  383. ->where('category', $_category)
  384. ->where('intention', 'TOP_LEVEL')
  385. ->first();
  386. }
  387. if ($point && $point->data) {
  388. $point->data = json_decode($point->data, $_assoc);
  389. }
  390. return $point;
  391. }
  392. public static function fillPointStateAndBadge(Point $point, Note $note)
  393. {
  394. // state
  395. if (!$point->is_removed) {
  396. $point->state = "ACTIVE";
  397. } elseif ($point->is_removed) {
  398. if (!$point->is_removed_due_to_entry_error) {
  399. $point->state = "HISTORIC";
  400. } else {
  401. $point->state = "ENTRY_ERROR";
  402. }
  403. }
  404. // added/removed info
  405. if ($point->state === 'ACTIVE') {
  406. if ($point->added_in_note_id === $note->id && $point->addition_reason_category === 'DURING_VISIT') {
  407. $point->badge = 'Added During Visit';
  408. } elseif ($point->added_in_note_id === $note->id && $point->addition_reason_category === 'ON_INTAKE') {
  409. $point->badge = 'New Record - Pre-existing';
  410. } elseif ($point->added_in_note_id !== $note->id) {
  411. $point->badge = 'Record Present Before Visit';
  412. }
  413. } elseif ($point->state === 'HISTORIC') {
  414. if ($point->removed_in_note_id === $note->id && $point->removal_reason_category === 'DURING_VISIT') {
  415. $point->badge = 'Removed During Visit';
  416. } elseif ($point->removed_in_note_id === $note->id && $point->removal_reason_category === 'ON_INTAKE') {
  417. $point->badge = 'Marked Removed on Intake';
  418. } elseif ($point->removed_in_note_id !== $note->id) {
  419. $point->badge = 'Historic Record Removed in a Previous Visit';
  420. }
  421. } elseif ($point->state === 'ENTRY_ERROR') {
  422. if ($point->removed_in_note_id === $note->id) {
  423. $point->badge = 'Marked as Entry Error During Visit';
  424. }
  425. }
  426. return $point;
  427. }
  428. }