Point.php 5.0 KB

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