Point.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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_review_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) {
  24. return Point
  25. ::where('client_id', $_patient->id)
  26. ->where('category', $_category)
  27. ->orderBy('created_at', 'DESC')
  28. ->first();
  29. }
  30. public static function getIntakePointsOfCategory(Client $_patient, String $_category, Note $_note) {
  31. $points = Point
  32. ::where('client_id', $_patient->id)
  33. ->where('category', $_category)
  34. ->where('addition_reason_category', 'ON_INTAKE')
  35. ->where(function ($query1) use ($_note) {
  36. $query1
  37. ->where('is_removed', false)
  38. ->orWhere(function ($query2) use ($_note) {
  39. $query2->where('is_removed', true)
  40. ->where('removed_in_note_id', $_note->id);
  41. });
  42. })
  43. ->orderBy('created_at')
  44. ->get();
  45. foreach ($points as $point) {
  46. if ($point->data) {
  47. $point->data = json_decode($point->data);
  48. }
  49. }
  50. return $points;
  51. }
  52. }