Point.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 childReviews()
  8. {
  9. return $this->hasMany(Point::class, 'parent_point_id', 'id')
  10. ->where('category', 'REVIEW')
  11. ->orderBy('created_at', 'DESC');
  12. }
  13. public function childReviewAddedInNote($_note)
  14. {
  15. $review = Point::where('added_in_note_id', $_note->id)
  16. ->where('category', 'REVIEW')
  17. ->where('parent_point_id', $this->id)
  18. ->orderBy('created_at', 'DESC')
  19. ->first();
  20. if(!!$review) {
  21. $review->data = json_decode($review->data);
  22. }
  23. return $review;
  24. }
  25. public function childPlanAddedInNote($_note)
  26. {
  27. $review = Point::where('added_in_note_id', $_note->id)
  28. ->where('category', 'PLAN')
  29. ->where('parent_point_id', $this->id)
  30. ->orderBy('created_at', 'DESC')
  31. ->first();
  32. if(!!$review) {
  33. $review->data = json_decode($review->data);
  34. }
  35. return $review;
  36. }
  37. public function lastChildReview()
  38. {
  39. return $this->hasOne(Point::class, 'id', 'last_child_review_point_id');
  40. }
  41. public function lastChildReviewNote()
  42. {
  43. return $this->hasOne(Note::class, 'id', 'last_child_review_point_scoped_note_id');
  44. }
  45. public function childPlans()
  46. {
  47. return $this->hasMany(Point::class, 'parent_point_id', 'id')
  48. ->where('category', 'PLAN')
  49. ->orderBy('created_at', 'DESC');
  50. }
  51. public function lastChildPlan()
  52. {
  53. return $this->hasOne(Point::class, 'id', 'last_child_plan_point_id');
  54. }
  55. public function lastChildPlanNote()
  56. {
  57. return $this->hasOne(Note::class, 'id', 'last_child_plan_point_scoped_note_id');
  58. }
  59. public function coreChildReview()
  60. {
  61. return $this->hasOne(Point::class, 'id', 'core_child_review_point_id');
  62. }
  63. public function coreChildPlan()
  64. {
  65. return $this->hasOne(Point::class, 'id', 'core_child_plan_point_id');
  66. }
  67. public function parentPoint()
  68. {
  69. return $this->hasOne(Point::class, 'id', 'parent_point_id');
  70. }
  71. public function client()
  72. {
  73. return $this->hasOne(Client::class, 'id', 'client_id');
  74. }
  75. public function note()
  76. {
  77. return $this->hasOne(Note::class, 'id', 'added_in_note_id');
  78. }
  79. public function relevanceToNote($_note) {
  80. return NotePoint
  81. ::where('is_active', true)
  82. ->where('note_id', $_note->id)
  83. ->where('point_id', $this->id)
  84. ->first();
  85. }
  86. public static function getGlobalSingletonOfCategory(Client $_patient, String $_category, $_assoc = false) {
  87. $point = Point
  88. ::where('client_id', $_patient->id)
  89. ->where('category', $_category)
  90. ->orderBy('created_at', 'DESC')
  91. ->first();
  92. if ($point && @$point->data) {
  93. $point->data = json_decode($point->data, $_assoc);
  94. }
  95. return $point;
  96. }
  97. public static function getIntakePointsOfCategory(Client $_patient, String $_category, Note $_note, $_assoc = false) {
  98. $points = Point
  99. ::where('client_id', $_patient->id)
  100. ->where('category', $_category)
  101. ->where('is_removed_due_to_entry_error', false)
  102. ->where(function ($query1) use ($_note) {
  103. $query1
  104. ->where('addition_reason_category', 'ON_INTAKE')
  105. ->orWhere(function ($query2) use ($_note) {
  106. $query2->where('addition_reason_category', 'DURING_VISIT')
  107. ->where('added_in_note_id', '<>', $_note->id);
  108. });
  109. })
  110. ->where(function ($query1) use ($_note) {
  111. $query1
  112. ->where('is_removed', false)
  113. ->orWhere(function ($query2) use ($_note) {
  114. $query2->where('is_removed', true)
  115. ->where('removed_in_note_id', $_note->id);
  116. });
  117. })
  118. ->orderBy('created_at')
  119. ->get();
  120. foreach ($points as $point) {
  121. if ($point->data) {
  122. $point->data = json_decode($point->data, $_assoc);
  123. }
  124. }
  125. return $points;
  126. }
  127. public static function getPlanPointsOfCategory(Client $_patient, String $_category, Note $_note, $_assoc = false) {
  128. $points = Point
  129. ::where('client_id', $_patient->id)
  130. ->where('category', $_category)
  131. ->where('is_removed_due_to_entry_error', false)
  132. ->where(function ($query1) use ($_note) {
  133. $query1
  134. ->where('is_removed', false)
  135. ->orWhere(function ($query2) use ($_note) {
  136. $query2->where('is_removed', true)
  137. ->where('removed_in_note_id', $_note->id);
  138. });
  139. })
  140. ->orderBy('created_at')
  141. ->get();
  142. foreach ($points as $point) {
  143. if ($point->data) {
  144. $point->data = json_decode($point->data, $_assoc);
  145. }
  146. }
  147. return $points;
  148. }
  149. public static function getPointsOfCategory(Client $_patient, String $_category, $_assoc = false) {
  150. $points = Point
  151. ::where('client_id', $_patient->id)
  152. ->where('category', $_category)
  153. ->where('is_removed', false)
  154. ->orderBy('created_at')
  155. ->get();
  156. foreach ($points as $point) {
  157. if ($point->data) {
  158. $point->data = json_decode($point->data, $_assoc);
  159. }
  160. }
  161. return $points;
  162. }
  163. public static function getNumPointsOfCategory(Client $_patient, String $_category) {
  164. return Point
  165. ::where('client_id', $_patient->id)
  166. ->where('category', $_category)
  167. ->where('is_removed', false)
  168. ->count();
  169. }
  170. public static function getPointsOfCategoryExtended(Client $_patient, String $_category, Note $_note) {
  171. $points = Point
  172. ::where('client_id', $_patient->id)
  173. ->where('category', $_category);
  174. if($_category !== 'GOAL') {
  175. $points = $points->orderByRaw("((data)::json->'name')::text ASC");
  176. }
  177. else {
  178. $points = $points->orderByRaw("((data)::json->'goal')::text ASC");
  179. }
  180. $points = $points->get();
  181. $pointsByType = [
  182. "ACTIVE" => [],
  183. "HISTORIC" => [],
  184. "ENTRY_ERROR" => [],
  185. ];
  186. foreach ($points as $point) {
  187. if ($point->data) {
  188. $point->data = json_decode($point->data);
  189. }
  190. if(!$point->is_removed) {
  191. $point->state = "ACTIVE";
  192. $pointsByType["ACTIVE"][] = $point;
  193. }
  194. elseif($point->is_removed) {
  195. if(!$point->is_removed_due_to_entry_error) {
  196. $point->state = "HISTORIC";
  197. $pointsByType["HISTORIC"][] = $point;
  198. }
  199. else {
  200. $point->state = "ENTRY_ERROR";
  201. $pointsByType["ENTRY_ERROR"][] = $point;
  202. }
  203. }
  204. }
  205. $points = array_merge($pointsByType["ACTIVE"], $pointsByType["HISTORIC"], $pointsByType["ENTRY_ERROR"]);
  206. return [
  207. $points,
  208. [
  209. "ACTIVE" => count($pointsByType["ACTIVE"]),
  210. "HISTORIC" => count($pointsByType["HISTORIC"]),
  211. "ENTRY_ERROR" => count($pointsByType["ENTRY_ERROR"]),
  212. ]
  213. ];
  214. }
  215. public static function fillPointStateAndBadge(Point $point, Note $note)
  216. {
  217. // state
  218. if (!$point->is_removed) {
  219. $point->state = "ACTIVE";
  220. } elseif ($point->is_removed) {
  221. if (!$point->is_removed_due_to_entry_error) {
  222. $point->state = "HISTORIC";
  223. } else {
  224. $point->state = "ENTRY_ERROR";
  225. }
  226. }
  227. // added/removed info
  228. if ($point->state === 'ACTIVE') {
  229. if ($point->added_in_note_id === $note->id && $point->addition_reason_category === 'DURING_VISIT') {
  230. $point->badge = 'Added During Visit';
  231. } elseif ($point->added_in_note_id === $note->id && $point->addition_reason_category === 'ON_INTAKE') {
  232. $point->badge = 'New Record - Pre-existing';
  233. } elseif ($point->added_in_note_id !== $note->id) {
  234. $point->badge = 'Record Present Before Visit';
  235. }
  236. } elseif ($point->state === 'HISTORIC') {
  237. if ($point->removed_in_note_id === $note->id && $point->removal_reason_category === 'DURING_VISIT') {
  238. $point->badge = 'Removed During Visit';
  239. } elseif ($point->removed_in_note_id === $note->id && $point->removal_reason_category === 'ON_INTAKE') {
  240. $point->badge = 'Marked Removed on Intake';
  241. } elseif ($point->removed_in_note_id !== $note->id) {
  242. $point->badge = 'Historic Record Removed In Previous Visit';
  243. }
  244. } elseif ($point->state === 'ENTRY_ERROR') {
  245. if ($point->removed_in_note_id === $note->id) {
  246. $point->badge = 'Marked as Entry Error During Visit';
  247. }
  248. }
  249. return $point;
  250. }
  251. }