Point.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 getPlanPointsOfCategory(Client $_patient, String $_category, Note $_note, $_assoc = false) {
  131. $points = Point
  132. ::where('client_id', $_patient->id)
  133. ->where('category', $_category)
  134. ->where('is_removed_due_to_entry_error', false)
  135. ->where(function ($query1) use ($_note) {
  136. $query1
  137. ->where(function ($query2) use ($_note) {
  138. $query2->where('is_removed', false)
  139. ->where('addition_reason_category', 'DURING_VISIT')
  140. ->where('added_in_note_id', $_note->id);
  141. })
  142. ->orWhere(function ($query2) use ($_note) {
  143. $query2->where('is_removed', true)
  144. ->where('removal_reason_category', 'DURING_VISIT')
  145. ->where('removed_in_note_id', $_note->id);
  146. })
  147. ->orWhere('last_child_plan_point_scoped_note_id', $_note->id)
  148. ->orWhereRaw("(SELECT count(id) from note_point WHERE is_active IS TRUE AND note_id = {$_note->id} AND point_id = point.id) > 0");
  149. })
  150. ->orderBy('created_at')
  151. ->get();
  152. foreach ($points as $point) {
  153. if ($point->data) {
  154. $point->data = json_decode($point->data, $_assoc);
  155. }
  156. }
  157. return $points;
  158. }
  159. public static function getPointsOfCategory(Client $_patient, String $_category, $_assoc = false) {
  160. $points = Point
  161. ::where('client_id', $_patient->id)
  162. ->where('category', $_category)
  163. ->where('is_removed', false)
  164. ->orderBy('created_at')
  165. ->get();
  166. foreach ($points as $point) {
  167. if ($point->data) {
  168. $point->data = json_decode($point->data, $_assoc);
  169. }
  170. }
  171. return $points;
  172. }
  173. public static function getNumPointsOfCategory(Client $_patient, String $_category) {
  174. return Point
  175. ::where('client_id', $_patient->id)
  176. ->where('category', $_category)
  177. ->where('is_removed', false)
  178. ->count();
  179. }
  180. public static function getPointsOfCategoryExtended(Client $_patient, String $_category, Note $_note) {
  181. $points = Point
  182. ::where('client_id', $_patient->id)
  183. ->where('category', $_category);
  184. if($_category !== 'GOAL') {
  185. $points = $points->orderByRaw("((data)::json->'name')::text ASC");
  186. }
  187. else {
  188. $points = $points->orderByRaw("((data)::json->'goal')::text ASC");
  189. }
  190. $points = $points->get();
  191. $pointsByType = [
  192. "ACTIVE" => [],
  193. "HISTORIC" => [],
  194. "ENTRY_ERROR" => [],
  195. ];
  196. foreach ($points as $point) {
  197. if ($point->data) {
  198. $point->data = json_decode($point->data);
  199. }
  200. if(!$point->is_removed) {
  201. $point->state = "ACTIVE";
  202. $pointsByType["ACTIVE"][] = $point;
  203. }
  204. elseif($point->is_removed) {
  205. if(!$point->is_removed_due_to_entry_error) {
  206. $point->state = "HISTORIC";
  207. $pointsByType["HISTORIC"][] = $point;
  208. }
  209. else {
  210. $point->state = "ENTRY_ERROR";
  211. $pointsByType["ENTRY_ERROR"][] = $point;
  212. }
  213. }
  214. }
  215. $points = array_merge($pointsByType["ACTIVE"], $pointsByType["HISTORIC"], $pointsByType["ENTRY_ERROR"]);
  216. return [
  217. $points,
  218. [
  219. "ACTIVE" => count($pointsByType["ACTIVE"]),
  220. "HISTORIC" => count($pointsByType["HISTORIC"]),
  221. "ENTRY_ERROR" => count($pointsByType["ENTRY_ERROR"]),
  222. ]
  223. ];
  224. }
  225. public static function fillPointStateAndBadge(Point $point, Note $note)
  226. {
  227. // state
  228. if (!$point->is_removed) {
  229. $point->state = "ACTIVE";
  230. } elseif ($point->is_removed) {
  231. if (!$point->is_removed_due_to_entry_error) {
  232. $point->state = "HISTORIC";
  233. } else {
  234. $point->state = "ENTRY_ERROR";
  235. }
  236. }
  237. // added/removed info
  238. if ($point->state === 'ACTIVE') {
  239. if ($point->added_in_note_id === $note->id && $point->addition_reason_category === 'DURING_VISIT') {
  240. $point->badge = 'Added During Visit';
  241. } elseif ($point->added_in_note_id === $note->id && $point->addition_reason_category === 'ON_INTAKE') {
  242. $point->badge = 'New Record - Pre-existing';
  243. } elseif ($point->added_in_note_id !== $note->id) {
  244. $point->badge = 'Record Present Before Visit';
  245. }
  246. } elseif ($point->state === 'HISTORIC') {
  247. if ($point->removed_in_note_id === $note->id && $point->removal_reason_category === 'DURING_VISIT') {
  248. $point->badge = 'Removed During Visit';
  249. } elseif ($point->removed_in_note_id === $note->id && $point->removal_reason_category === 'ON_INTAKE') {
  250. $point->badge = 'Marked Removed on Intake';
  251. } elseif ($point->removed_in_note_id !== $note->id) {
  252. $point->badge = 'Historic Record Removed In Previous Visit';
  253. }
  254. } elseif ($point->state === 'ENTRY_ERROR') {
  255. if ($point->removed_in_note_id === $note->id) {
  256. $point->badge = 'Marked as Entry Error During Visit';
  257. }
  258. }
  259. return $point;
  260. }
  261. }