Point.php 17 KB

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