Point.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 client()
  68. {
  69. return $this->hasOne(Client::class, 'id', 'client_id');
  70. }
  71. public function note()
  72. {
  73. return $this->hasOne(Note::class, 'id', 'added_in_note_id');
  74. }
  75. public function relevanceToNote($_note) {
  76. return NotePoint
  77. ::where('is_active', true)
  78. ->where('note_id', $_note->id)
  79. ->where('point_id', $this->id)
  80. ->first();
  81. }
  82. public static function getGlobalSingletonOfCategory(Client $_patient, String $_category, $_assoc = false) {
  83. $point = Point
  84. ::where('client_id', $_patient->id)
  85. ->where('category', $_category)
  86. ->orderBy('created_at', 'DESC')
  87. ->first();
  88. if ($point && @$point->data) {
  89. $point->data = json_decode($point->data, $_assoc);
  90. }
  91. return $point;
  92. }
  93. public static function getIntakePointsOfCategory(Client $_patient, String $_category, Note $_note, $_assoc = false) {
  94. $points = Point
  95. ::where('client_id', $_patient->id)
  96. ->where('category', $_category)
  97. ->where('is_removed_due_to_entry_error', false)
  98. ->where(function ($query1) use ($_note) {
  99. $query1
  100. ->where('addition_reason_category', 'ON_INTAKE')
  101. ->orWhere(function ($query2) use ($_note) {
  102. $query2->where('addition_reason_category', 'DURING_VISIT')
  103. ->where('added_in_note_id', '<>', $_note->id);
  104. });
  105. })
  106. ->where(function ($query1) use ($_note) {
  107. $query1
  108. ->where('is_removed', false)
  109. ->orWhere(function ($query2) use ($_note) {
  110. $query2->where('is_removed', true)
  111. ->where('removed_in_note_id', $_note->id);
  112. });
  113. })
  114. ->orderBy('created_at')
  115. ->get();
  116. foreach ($points as $point) {
  117. if ($point->data) {
  118. $point->data = json_decode($point->data, $_assoc);
  119. }
  120. }
  121. return $points;
  122. }
  123. public static function getPlanPointsOfCategory(Client $_patient, String $_category, Note $_note, $_assoc = false) {
  124. $points = Point
  125. ::where('client_id', $_patient->id)
  126. ->where('category', $_category)
  127. ->where('is_removed_due_to_entry_error', false)
  128. ->where(function ($query1) use ($_note) {
  129. $query1
  130. ->where('is_removed', false)
  131. ->orWhere(function ($query2) use ($_note) {
  132. $query2->where('is_removed', true)
  133. ->where('removed_in_note_id', $_note->id);
  134. });
  135. })
  136. ->orderBy('created_at')
  137. ->get();
  138. foreach ($points as $point) {
  139. if ($point->data) {
  140. $point->data = json_decode($point->data, $_assoc);
  141. }
  142. }
  143. return $points;
  144. }
  145. public static function getPointsOfCategory(Client $_patient, String $_category, $_assoc = false) {
  146. $points = Point
  147. ::where('client_id', $_patient->id)
  148. ->where('category', $_category)
  149. ->where('is_removed', false)
  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 getPointsOfCategoryExtended(Client $_patient, String $_category, Note $_note) {
  160. $points = Point
  161. ::where('client_id', $_patient->id)
  162. ->where('category', $_category);
  163. if($_category !== 'GOAL') {
  164. $points = $points->orderByRaw("((data)::json->'name')::text ASC");
  165. }
  166. else {
  167. $points = $points->orderByRaw("((data)::json->'goal')::text ASC");
  168. }
  169. $points = $points->get();
  170. $pointsByType = [
  171. "ACTIVE" => [],
  172. "HISTORIC" => [],
  173. "ENTRY_ERROR" => [],
  174. ];
  175. foreach ($points as $point) {
  176. if ($point->data) {
  177. $point->data = json_decode($point->data);
  178. }
  179. if(!$point->is_removed) {
  180. $point->state = "ACTIVE";
  181. $pointsByType["ACTIVE"][] = $point;
  182. }
  183. elseif($point->is_removed) {
  184. if(!$point->is_removed_due_to_entry_error) {
  185. $point->state = "HISTORIC";
  186. $pointsByType["HISTORIC"][] = $point;
  187. }
  188. else {
  189. $point->state = "ENTRY_ERROR";
  190. $pointsByType["ENTRY_ERROR"][] = $point;
  191. }
  192. }
  193. }
  194. $points = array_merge($pointsByType["ACTIVE"], $pointsByType["HISTORIC"], $pointsByType["ENTRY_ERROR"]);
  195. return [
  196. $points,
  197. [
  198. "ACTIVE" => count($pointsByType["ACTIVE"]),
  199. "HISTORIC" => count($pointsByType["HISTORIC"]),
  200. "ENTRY_ERROR" => count($pointsByType["ENTRY_ERROR"]),
  201. ]
  202. ];
  203. }
  204. public static function fillPointStateAndBadge(Point $point, Note $note)
  205. {
  206. // state
  207. if (!$point->is_removed) {
  208. $point->state = "ACTIVE";
  209. } elseif ($point->is_removed) {
  210. if (!$point->is_removed_due_to_entry_error) {
  211. $point->state = "HISTORIC";
  212. } else {
  213. $point->state = "ENTRY_ERROR";
  214. }
  215. }
  216. // added/removed info
  217. if ($point->state === 'ACTIVE') {
  218. if ($point->added_in_note_id === $note->id && $point->addition_reason_category === 'DURING_VISIT') {
  219. $point->badge = 'Added During Visit';
  220. } elseif ($point->added_in_note_id === $note->id && $point->addition_reason_category === 'ON_INTAKE') {
  221. $point->badge = 'New Record - Pre-existing';
  222. } elseif ($point->added_in_note_id !== $note->id) {
  223. $point->badge = 'Record Present Before Visit';
  224. }
  225. } elseif ($point->state === 'HISTORIC') {
  226. if ($point->removed_in_note_id === $note->id && $point->removal_reason_category === 'DURING_VISIT') {
  227. $point->badge = 'Removed During Visit';
  228. } elseif ($point->removed_in_note_id === $note->id && $point->removal_reason_category === 'ON_INTAKE') {
  229. $point->badge = 'Marked Removed on Intake';
  230. } elseif ($point->removed_in_note_id !== $note->id) {
  231. $point->badge = 'Historic Record Removed In Previous Visit';
  232. }
  233. } elseif ($point->state === 'ENTRY_ERROR') {
  234. if ($point->removed_in_note_id === $note->id) {
  235. $point->badge = 'Marked as Entry Error During Visit';
  236. }
  237. }
  238. return $point;
  239. }
  240. }