Point.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 lastChildReview()
  14. {
  15. return $this->hasOne(Point::class, 'id', 'last_child_review_point_id');
  16. }
  17. public function lastChildReviewNote()
  18. {
  19. return $this->hasOne(Note::class, 'id', 'last_child_review_point_scoped_note_id');
  20. }
  21. public function childPlans()
  22. {
  23. return $this->hasMany(Point::class, 'parent_point_id', 'id')
  24. ->where('category', 'PLAN')
  25. ->orderBy('created_at', 'DESC');
  26. }
  27. public function lastChildPlan()
  28. {
  29. return $this->hasOne(Point::class, 'id', 'last_child_plan_point_id');
  30. }
  31. public function lastChildPlanNote()
  32. {
  33. return $this->hasOne(Note::class, 'id', 'last_child_plan_point_scoped_note_id');
  34. }
  35. public function client()
  36. {
  37. return $this->hasOne(Client::class, 'id', 'client_id');
  38. }
  39. public function note()
  40. {
  41. return $this->hasOne(Note::class, 'id', 'added_in_note_id');
  42. }
  43. public static function getGlobalSingletonOfCategory(Client $_patient, String $_category, $_assoc = false) {
  44. $point = Point
  45. ::where('client_id', $_patient->id)
  46. ->where('category', $_category)
  47. ->orderBy('created_at', 'DESC')
  48. ->first();
  49. if ($point && @$point->data) {
  50. $point->data = json_decode($point->data, $_assoc);
  51. }
  52. return $point;
  53. }
  54. public static function getIntakePointsOfCategory(Client $_patient, String $_category, Note $_note, $_assoc = false) {
  55. $points = Point
  56. ::where('client_id', $_patient->id)
  57. ->where('category', $_category)
  58. ->where(function ($query1) use ($_note) {
  59. $query1
  60. ->where('addition_reason_category', 'ON_INTAKE')
  61. ->orWhere(function ($query2) use ($_note) {
  62. $query2->where('addition_reason_category', 'DURING_VISIT')
  63. ->where('added_in_note_id', '<>', $_note->id);
  64. });
  65. })
  66. ->where(function ($query1) use ($_note) {
  67. $query1
  68. ->where('is_removed', false)
  69. ->orWhere(function ($query2) use ($_note) {
  70. $query2->where('is_removed', true)
  71. ->where('removed_in_note_id', $_note->id);
  72. });
  73. })
  74. ->orderBy('created_at')
  75. ->get();
  76. foreach ($points as $point) {
  77. if ($point->data) {
  78. $point->data = json_decode($point->data, $_assoc);
  79. }
  80. }
  81. return $points;
  82. }
  83. public static function getPlanPointsOfCategory(Client $_patient, String $_category, Note $_note, $_assoc = false) {
  84. $points = Point
  85. ::where('client_id', $_patient->id)
  86. ->where('category', $_category)
  87. ->where(function ($query1) use ($_note) {
  88. $query1
  89. ->where('is_removed', false)
  90. ->orWhere(function ($query2) use ($_note) {
  91. $query2->where('is_removed', true)
  92. ->where('removed_in_note_id', $_note->id);
  93. });
  94. })
  95. ->orderBy('created_at')
  96. ->get();
  97. foreach ($points as $point) {
  98. if ($point->data) {
  99. $point->data = json_decode($point->data, $_assoc);
  100. }
  101. }
  102. return $points;
  103. }
  104. public static function getPointsOfCategory(Client $_patient, String $_category, $_assoc = false) {
  105. $points = Point
  106. ::where('client_id', $_patient->id)
  107. ->where('category', $_category)
  108. ->where('is_removed', false)
  109. ->orderBy('created_at')
  110. ->get();
  111. foreach ($points as $point) {
  112. if ($point->data) {
  113. $point->data = json_decode($point->data, $_assoc);
  114. }
  115. }
  116. return $points;
  117. }
  118. }