Point.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 client()
  60. {
  61. return $this->hasOne(Client::class, 'id', 'client_id');
  62. }
  63. public function note()
  64. {
  65. return $this->hasOne(Note::class, 'id', 'added_in_note_id');
  66. }
  67. public function relevanceToNote($_note) {
  68. return NotePoint
  69. ::where('is_active', true)
  70. ->where('note_id', $_note->id)
  71. ->where('point_id', $this->id)
  72. ->first();
  73. }
  74. public static function getGlobalSingletonOfCategory(Client $_patient, String $_category, $_assoc = false) {
  75. $point = Point
  76. ::where('client_id', $_patient->id)
  77. ->where('category', $_category)
  78. ->orderBy('created_at', 'DESC')
  79. ->first();
  80. if ($point && @$point->data) {
  81. $point->data = json_decode($point->data, $_assoc);
  82. }
  83. return $point;
  84. }
  85. public static function getIntakePointsOfCategory(Client $_patient, String $_category, Note $_note, $_assoc = false) {
  86. $points = Point
  87. ::where('client_id', $_patient->id)
  88. ->where('category', $_category)
  89. ->where('is_removed_due_to_entry_error', false)
  90. ->where(function ($query1) use ($_note) {
  91. $query1
  92. ->where('addition_reason_category', 'ON_INTAKE')
  93. ->orWhere(function ($query2) use ($_note) {
  94. $query2->where('addition_reason_category', 'DURING_VISIT')
  95. ->where('added_in_note_id', '<>', $_note->id);
  96. });
  97. })
  98. ->where(function ($query1) use ($_note) {
  99. $query1
  100. ->where('is_removed', false)
  101. ->orWhere(function ($query2) use ($_note) {
  102. $query2->where('is_removed', true)
  103. ->where('removed_in_note_id', $_note->id);
  104. });
  105. })
  106. ->orderBy('created_at')
  107. ->get();
  108. foreach ($points as $point) {
  109. if ($point->data) {
  110. $point->data = json_decode($point->data, $_assoc);
  111. }
  112. }
  113. return $points;
  114. }
  115. public static function getPlanPointsOfCategory(Client $_patient, String $_category, Note $_note, $_assoc = false) {
  116. $points = Point
  117. ::where('client_id', $_patient->id)
  118. ->where('category', $_category)
  119. ->where('is_removed_due_to_entry_error', false)
  120. ->where(function ($query1) use ($_note) {
  121. $query1
  122. ->where('is_removed', false)
  123. ->orWhere(function ($query2) use ($_note) {
  124. $query2->where('is_removed', true)
  125. ->where('removed_in_note_id', $_note->id);
  126. });
  127. })
  128. ->orderBy('created_at')
  129. ->get();
  130. foreach ($points as $point) {
  131. if ($point->data) {
  132. $point->data = json_decode($point->data, $_assoc);
  133. }
  134. }
  135. return $points;
  136. }
  137. public static function getPointsOfCategory(Client $_patient, String $_category, $_assoc = false) {
  138. $points = Point
  139. ::where('client_id', $_patient->id)
  140. ->where('category', $_category)
  141. ->where('is_removed', false)
  142. ->orderBy('created_at')
  143. ->get();
  144. foreach ($points as $point) {
  145. if ($point->data) {
  146. $point->data = json_decode($point->data, $_assoc);
  147. }
  148. }
  149. return $points;
  150. }
  151. }