From bb8542c1aafe46d51cc836031701d882eb59db76 Mon Sep 17 00:00:00 2001 From: Muhammad Sulaiman Yusuf Date: Tue, 13 Dec 2022 14:34:53 +0700 Subject: [PATCH] update header if start / end date changed / an activity deleted --- app/Models/Activity.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/app/Models/Activity.php b/app/Models/Activity.php index 49bcc1e..7e06d88 100644 --- a/app/Models/Activity.php +++ b/app/Models/Activity.php @@ -63,6 +63,9 @@ class Activity extends Model $data->updatePersentaseProgress(); $data->updateCostActual(); } + if($this->start_date != request()->start_date || $this->end_date != request()->end_date) { + $data->updateStartEndDateHeader(); + } }); static::deleted(function($data) { @@ -75,6 +78,9 @@ class Activity extends Model $data->updatePersentaseProgress(); $data->updateCostActual(); } + if($this->start_date != request()->start_date || $this->end_date != request()->end_date) { + $data->updateStartEndDateHeader(); + } }); } @@ -160,6 +166,18 @@ class Activity extends Model ]); } + private function updateStartEndDateHeader() + { + $earliestStartDate = Activity::where('version_gantt_id', $this->version_gantt_id)->oldest('start_date')->pluck('start_date')->first(); + $latestEndDate = Activity::where('version_gantt_id', $this->version_gantt_id)->latest('end_date')->pluck('end_date')->first(); + if($header = Activity::where('version_gantt_id', $this->version_gantt_id)->whereNull('parent_id')->first()) { + $header->update([ + "start_date" => $earliestStartDate, + "end_date" => $latestEndDate + ]); + } + } + public function getJobsDoneAttribute() { if(!ReportActivityMaterial::where('activity_id', $this->id)->first())