123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- <?php
- namespace App\Models;
- # use Illuminate\Database\Eloquent\Model;
- class Point extends Model
- {
- protected $table = 'point';
- public function childReviews()
- {
- return $this->hasMany(Point::class, 'parent_point_id', 'id')
- ->where('category', 'REVIEW')
- ->orderBy('created_at', 'DESC');
- }
- public function childReviewAddedInNote($_note)
- {
- $review = Point::where('added_in_note_id', $_note->id)
- ->where('category', 'REVIEW')
- ->where('parent_point_id', $this->id)
- ->orderBy('created_at', 'DESC')
- ->first();
- if(!!$review) {
- $review->data = json_decode($review->data);
- }
- return $review;
- }
- public function childPlanAddedInNote($_note)
- {
- $review = Point::where('added_in_note_id', $_note->id)
- ->where('category', 'PLAN')
- ->where('parent_point_id', $this->id)
- ->orderBy('created_at', 'DESC')
- ->first();
- if(!!$review) {
- $review->data = json_decode($review->data);
- }
- return $review;
- }
- public function lastChildReview()
- {
- return $this->hasOne(Point::class, 'id', 'last_child_review_point_id');
- }
- public function lastChildReviewNote()
- {
- return $this->hasOne(Note::class, 'id', 'last_child_review_point_scoped_note_id');
- }
- public function childPlans()
- {
- return $this->hasMany(Point::class, 'parent_point_id', 'id')
- ->where('category', 'PLAN')
- ->orderBy('created_at', 'DESC');
- }
- public function lastChildPlan()
- {
- return $this->hasOne(Point::class, 'id', 'last_child_plan_point_id');
- }
- public function lastChildPlanNote()
- {
- return $this->hasOne(Note::class, 'id', 'last_child_plan_point_scoped_note_id');
- }
- public function client()
- {
- return $this->hasOne(Client::class, 'id', 'client_id');
- }
- public function note()
- {
- return $this->hasOne(Note::class, 'id', 'added_in_note_id');
- }
- public function relevanceToNote($_note) {
- return NotePoint
- ::where('is_active', true)
- ->where('note_id', $_note->id)
- ->where('point_id', $this->id)
- ->first();
- }
- public static function getGlobalSingletonOfCategory(Client $_patient, String $_category, $_assoc = false) {
- $point = Point
- ::where('client_id', $_patient->id)
- ->where('category', $_category)
- ->orderBy('created_at', 'DESC')
- ->first();
- if ($point && @$point->data) {
- $point->data = json_decode($point->data, $_assoc);
- }
- return $point;
- }
- public static function getIntakePointsOfCategory(Client $_patient, String $_category, Note $_note, $_assoc = false) {
- $points = Point
- ::where('client_id', $_patient->id)
- ->where('category', $_category)
- ->where('is_removed_due_to_entry_error', false)
- ->where(function ($query1) use ($_note) {
- $query1
- ->where('addition_reason_category', 'ON_INTAKE')
- ->orWhere(function ($query2) use ($_note) {
- $query2->where('addition_reason_category', 'DURING_VISIT')
- ->where('added_in_note_id', '<>', $_note->id);
- });
- })
- ->where(function ($query1) use ($_note) {
- $query1
- ->where('is_removed', false)
- ->orWhere(function ($query2) use ($_note) {
- $query2->where('is_removed', true)
- ->where('removed_in_note_id', $_note->id);
- });
- })
- ->orderBy('created_at')
- ->get();
- foreach ($points as $point) {
- if ($point->data) {
- $point->data = json_decode($point->data, $_assoc);
- }
- }
- return $points;
- }
- public static function getPlanPointsOfCategory(Client $_patient, String $_category, Note $_note, $_assoc = false) {
- $points = Point
- ::where('client_id', $_patient->id)
- ->where('category', $_category)
- ->where('is_removed_due_to_entry_error', false)
- ->where(function ($query1) use ($_note) {
- $query1
- ->where('is_removed', false)
- ->orWhere(function ($query2) use ($_note) {
- $query2->where('is_removed', true)
- ->where('removed_in_note_id', $_note->id);
- });
- })
- ->orderBy('created_at')
- ->get();
- foreach ($points as $point) {
- if ($point->data) {
- $point->data = json_decode($point->data, $_assoc);
- }
- }
- return $points;
- }
- public static function getPointsOfCategory(Client $_patient, String $_category, $_assoc = false) {
- $points = Point
- ::where('client_id', $_patient->id)
- ->where('category', $_category)
- ->where('is_removed', false)
- ->orderBy('created_at')
- ->get();
- foreach ($points as $point) {
- if ($point->data) {
- $point->data = json_decode($point->data, $_assoc);
- }
- }
- return $points;
- }
- public static function getPointsOfCategoryExtended(Client $_patient, String $_category, Note $_note) {
- $points = Point
- ::where('client_id', $_patient->id)
- ->where('category', $_category)
- ->where(function ($q) use($_note) {
- $q
- ->where('is_removed_due_to_entry_error', false)
- ->orWhere('removed_in_note_id', '<>', $_note->id);
- })
- ->orderBy('is_removed')
- ->orderBy('added_in_note_id', 'DESC')
- ->orderBy('removal_effective_date', 'DESC')
- ->orderByRaw('removed_in_note_id DESC NULLS FIRST')
- ->orderBy('created_at')
- ->get();
- $pointsByType = [
- "new" => [],
- "preExisting" => [],
- "historic" => [],
- "eeFromOtherNote" => []
- ];
- foreach ($points as $point) {
- if ($point->data) {
- $point->data = json_decode($point->data);
- }
- if(!$point->is_removed && $point->added_in_note_id === $_note->id) {
- $point->state = 1;
- $pointsByType["new"][] = $point;
- }
- elseif(!$point->is_removed && $point->added_in_note_id !== $_note->id) {
- $point->state = 2;
- $pointsByType["preExisting"][] = $point;
- }
- elseif($point->is_removed && !$point->is_removed_due_to_entry_error) {
- $point->state = 3;
- $pointsByType["historic"][] = $point;
- }
- elseif($point->is_removed_due_to_entry_error) {
- $point->state = 4;
- $pointsByType["eeFromOtherNote"][] = $point;
- }
- }
- $points = array_merge($pointsByType["new"], $pointsByType["preExisting"], $pointsByType["historic"], $pointsByType["eeFromOtherNote"]);
- return $points;
- }
- }
|