Point.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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 getPlanPointsOfCategory(Client $_patient, String $_category, Note $_note, $_assoc = false) {
  161. $points = Point
  162. ::where('client_id', $_patient->id)
  163. ->where('category', $_category)
  164. ->where('is_removed_due_to_entry_error', false)
  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', 'DURING_VISIT')
  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', 'DURING_VISIT')
  175. ->where('removed_in_note_id', $_note->id);
  176. })
  177. ->orWhere('last_child_plan_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 getPlanPoints(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. ->where(function ($query1) use ($_note) {
  194. $query1
  195. ->where(function ($query2) use ($_note) {
  196. $query2->where('is_removed', false)
  197. ->where('addition_reason_category', 'DURING_VISIT')
  198. ->where('added_in_note_id', $_note->id);
  199. })
  200. ->orWhere(function ($query2) use ($_note) {
  201. $query2->where('is_removed', true)
  202. ->where('removal_reason_category', 'DURING_VISIT')
  203. ->where('removed_in_note_id', $_note->id);
  204. })
  205. ->orWhere('last_child_plan_point_scoped_note_id', $_note->id);
  206. })
  207. ->orderBy('created_at')
  208. ->get();
  209. foreach ($points as $point) {
  210. if ($point->data) {
  211. $point->data = json_decode($point->data, $_assoc);
  212. }
  213. }
  214. return $points;
  215. }
  216. public static function getUnifiedPointsOfCategory(Client $_patient, String $_category, Note $_note, $_assoc = false) {
  217. $points = Point
  218. ::where('client_id', $_patient->id)
  219. ->where('category', $_category)
  220. ->where('is_removed_due_to_entry_error', false)
  221. ->where(function ($query1) use ($_note) {
  222. $query1
  223. ->where(function ($query2) use ($_note) { // added on_intake on this note
  224. $query2->where('is_removed', false)
  225. ->where('addition_reason_category', 'ON_INTAKE')
  226. ->where('added_in_note_id', $_note->id);
  227. })
  228. ->orWhere(function ($query2) use ($_note) { // removed on_intake on this note
  229. $query2->where('is_removed', true)
  230. ->where('removal_reason_category', 'ON_INTAKE')
  231. ->where('removed_in_note_id', $_note->id);
  232. })
  233. ->orWhere('last_child_review_point_scoped_note_id', $_note->id) // review added during this note
  234. ->orWhere(function ($query2) use ($_note) { // added during_visit on this note
  235. $query2->where('is_removed', false)
  236. ->where('addition_reason_category', 'DURING_VISIT')
  237. ->where('added_in_note_id', $_note->id);
  238. })
  239. ->orWhere(function ($query2) use ($_note) { // removed during_visit on this note
  240. $query2->where('is_removed', true)
  241. ->where('removal_reason_category', 'DURING_VISIT')
  242. ->where('removed_in_note_id', $_note->id);
  243. })
  244. ->orWhere('last_child_plan_point_scoped_note_id', $_note->id) // plan added during this note
  245. // marked relevant to this note
  246. ->orWhereRaw("(SELECT count(id) from note_point WHERE is_active IS TRUE AND note_id = {$_note->id} AND point_id = point.id) > 0");
  247. })
  248. ->orderBy('created_at')
  249. ->get();
  250. foreach ($points as $point) {
  251. if ($point->data) {
  252. $point->data = json_decode($point->data, $_assoc);
  253. }
  254. }
  255. return $points;
  256. }
  257. public static function getPointsOfCategory(Client $_patient, String $_category, $_assoc = false) {
  258. $points = Point
  259. ::where('client_id', $_patient->id)
  260. ->where('category', $_category)
  261. ->where('is_removed', false)
  262. ->orderBy('created_at')
  263. ->get();
  264. foreach ($points as $point) {
  265. if ($point->data) {
  266. $point->data = json_decode($point->data, $_assoc);
  267. }
  268. }
  269. return $points;
  270. }
  271. public static function getNumPointsOfCategory(Client $_patient, String $_category) {
  272. return Point
  273. ::where('client_id', $_patient->id)
  274. ->where('category', $_category)
  275. ->where('is_removed', false)
  276. ->count();
  277. }
  278. public static function getPointsOfCategoryExtended(Client $_patient, String $_category, Note $_note, $excludeEntryError = false) {
  279. $points = Point
  280. ::where('client_id', $_patient->id)
  281. ->where('category', $_category);
  282. if($excludeEntryError) {
  283. $points = $points->where('is_removed_due_to_entry_error', FALSE);
  284. }
  285. if($_category !== 'GOAL') {
  286. $points = $points->orderByRaw("((data)::json->'name')::text ASC");
  287. }
  288. else {
  289. $points = $points->orderByRaw("((data)::json->'goal')::text ASC");
  290. }
  291. $points = $points->get();
  292. $pointsByType = [
  293. "ACTIVE" => [],
  294. "HISTORIC" => [],
  295. "ENTRY_ERROR" => [],
  296. ];
  297. foreach ($points as $point) {
  298. if ($point->data) {
  299. $point->data = json_decode($point->data);
  300. }
  301. if(!$point->is_removed) {
  302. $point->state = "ACTIVE";
  303. $pointsByType["ACTIVE"][] = $point;
  304. }
  305. elseif($point->is_removed) {
  306. if(!$point->is_removed_due_to_entry_error) {
  307. $point->state = "HISTORIC";
  308. $pointsByType["HISTORIC"][] = $point;
  309. }
  310. else {
  311. $point->state = "ENTRY_ERROR";
  312. $pointsByType["ENTRY_ERROR"][] = $point;
  313. }
  314. }
  315. }
  316. $points = array_merge($pointsByType["ACTIVE"], $pointsByType["HISTORIC"], $pointsByType["ENTRY_ERROR"]);
  317. return [
  318. $points,
  319. [
  320. "ACTIVE" => count($pointsByType["ACTIVE"]),
  321. "HISTORIC" => count($pointsByType["HISTORIC"]),
  322. "ENTRY_ERROR" => count($pointsByType["ENTRY_ERROR"]),
  323. ]
  324. ];
  325. }
  326. public static function getOnlyPointOfCategory(Client $_patient, String $_category) {
  327. $point = Point
  328. ::where('client_id', $_patient->id)
  329. ->where('category', $_category)
  330. ->first();
  331. if ($point && $point->data) {
  332. $point->data = json_decode($point->data);
  333. }
  334. return $point;
  335. }
  336. public static function getOnlyTopLevelPointOfCategory(Note $_note, String $_category, $_assoc = false) {
  337. $point = Point
  338. ::where('client_id', $_note->client_id)
  339. ->where('category', $_category)
  340. ->where('intention', 'TOP_LEVEL')
  341. ->first();
  342. if ($point && $point->data) {
  343. $point->data = json_decode($point->data, $_assoc);
  344. }
  345. return $point;
  346. }
  347. public static function getOrCreateOnlyTopLevelPointOfCategory(Note $_note, String $_category, $_sessionKey, $_assoc = false) {
  348. $point = Point
  349. ::where('client_id', $_note->client_id)
  350. ->where('category', $_category)
  351. ->where('intention', 'TOP_LEVEL')
  352. ->first();
  353. if(!$point) {
  354. $response = callJava('/visitPoint/addTopLevel', [
  355. "category" => $_category,
  356. "data" => '{}',
  357. "noteUid" => $_note->uid,
  358. "additionReasonCategory" => 'ON_INTAKE',
  359. ], $_sessionKey);
  360. // TODO: dont assume success
  361. $point = Point
  362. ::where('client_id', $_note->client_id)
  363. ->where('category', $_category)
  364. ->where('intention', 'TOP_LEVEL')
  365. ->first();
  366. }
  367. if ($point && $point->data) {
  368. $point->data = json_decode($point->data, $_assoc);
  369. }
  370. return $point;
  371. }
  372. public static function fillPointStateAndBadge(Point $point, Note $note)
  373. {
  374. // state
  375. if (!$point->is_removed) {
  376. $point->state = "ACTIVE";
  377. } elseif ($point->is_removed) {
  378. if (!$point->is_removed_due_to_entry_error) {
  379. $point->state = "HISTORIC";
  380. } else {
  381. $point->state = "ENTRY_ERROR";
  382. }
  383. }
  384. // added/removed info
  385. if ($point->state === 'ACTIVE') {
  386. if ($point->added_in_note_id === $note->id && $point->addition_reason_category === 'DURING_VISIT') {
  387. $point->badge = 'Added During Visit';
  388. } elseif ($point->added_in_note_id === $note->id && $point->addition_reason_category === 'ON_INTAKE') {
  389. $point->badge = 'New Record - Pre-existing';
  390. } elseif ($point->added_in_note_id !== $note->id) {
  391. $point->badge = 'Record Present Before Visit';
  392. }
  393. } elseif ($point->state === 'HISTORIC') {
  394. if ($point->removed_in_note_id === $note->id && $point->removal_reason_category === 'DURING_VISIT') {
  395. $point->badge = 'Removed During Visit';
  396. } elseif ($point->removed_in_note_id === $note->id && $point->removal_reason_category === 'ON_INTAKE') {
  397. $point->badge = 'Marked Removed on Intake';
  398. } elseif ($point->removed_in_note_id !== $note->id) {
  399. $point->badge = 'Historic Record Removed in a Previous Visit';
  400. }
  401. } elseif ($point->state === 'ENTRY_ERROR') {
  402. if ($point->removed_in_note_id === $note->id) {
  403. $point->badge = 'Marked as Entry Error During Visit';
  404. }
  405. }
  406. return $point;
  407. }
  408. }