Point.php 3.6 KB

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