From 86588d4892e1675e7504b06e9d94d4806e45c3b1 Mon Sep 17 00:00:00 2001 From: wahyu Date: Tue, 24 Oct 2023 13:12:53 +0700 Subject: [PATCH] hierarchy & gantt cascade delete --- app/Models/Activity.php | 12 +++++++++--- app/Models/HierarchyFtth.php | 16 +++++++++++----- app/Models/VersionGantt.php | 7 +++++++ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/app/Models/Activity.php b/app/Models/Activity.php index 3a8e54c..5d9c1e7 100644 --- a/app/Models/Activity.php +++ b/app/Models/Activity.php @@ -79,8 +79,14 @@ class Activity extends Model }); static::deleted(function($data) { - if(Activity::where("parent_id", $data->parent_id)->count() == 0) - Activity::find($data->parent_id)->update(["type_activity"=>"task"]); + if (isset($data->parent_id)) { + if(Activity::where("parent_id", $data->parent_id)->count() == 0) { + $activity = Activity::find($data->parent_id); + if ($activity) { + $activity->update(["type_activity"=>"task"]); + } + } + } $data->updateBobot(true); $data->updateCostPlanning(); @@ -100,7 +106,7 @@ class Activity extends Model ->whereNull('parent_id') ->first(); - if($root->rencana_biaya > 0){ + if(isset($root) && $root->rencana_biaya > 0){ $activities = Activity::where("proyek_id", $this->proyek_id)->where("version_gantt_id", $this->version_gantt_id)->get(); foreach ($activities as $activity) { if($isDelete && $activity->id == $this->id) diff --git a/app/Models/HierarchyFtth.php b/app/Models/HierarchyFtth.php index fae503c..1a0ba8c 100644 --- a/app/Models/HierarchyFtth.php +++ b/app/Models/HierarchyFtth.php @@ -3,6 +3,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; +use App\Models\VersionGantt; class HierarchyFtth extends Model { @@ -14,15 +15,20 @@ class HierarchyFtth extends Model protected $fillable = [ 'name', 'parent_id', 'project_id', 'created_at', 'updated_at', 'progress', 'bobot_planning' ]; - + public static function boot() { parent::boot(); static::deleted(function($data) { - $ftths = HierarchyFtth::where('parent_id', $data->id)->get(); - foreach ($ftths as $ftth) { - $ftth->delete(); - } + $ftths = HierarchyFtth::where('parent_id', $data->id)->get(); + foreach ($ftths as $ftth) { + $ftth->delete(); + } + + $gantts = VersionGantt::where('hierarchy_ftth_id', $data->id)->get(); + foreach ($gantts as $gantt) { + $gantt->delete(); + } }); } } diff --git a/app/Models/VersionGantt.php b/app/Models/VersionGantt.php index 765ddf1..39b9b0f 100644 --- a/app/Models/VersionGantt.php +++ b/app/Models/VersionGantt.php @@ -38,6 +38,13 @@ class VersionGantt extends Model static::updated(function($data) { $data->updateActDuration(); + }); + + static::deleted(function ($data) { + $activities = Activity::where('version_gantt_id', $data->id)->get(); + foreach ($activities as $activity) { + $activity->delete(); + } }); }