From b3b178d8ec053e27ec2f644971271d89ffa80edf Mon Sep 17 00:00:00 2001 From: Wahyu Ramadhan Date: Thu, 22 Jun 2023 16:26:47 +0700 Subject: [PATCH] Fix progress data type and daysoff duration --- app/Http/Controllers/ActivityController.php | 4 +-- app/Models/VersionGantt.php | 36 +++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/ActivityController.php b/app/Http/Controllers/ActivityController.php index 144b92c..c31e50b 100644 --- a/app/Http/Controllers/ActivityController.php +++ b/app/Http/Controllers/ActivityController.php @@ -107,7 +107,7 @@ class ActivityController extends Controller foreach($data as $objRow){ $objRow->parent = $parent_id; $objRow->text = $objRow->name; - $objRow->progress = (int)$objRow->persentase_progress/100; + $objRow->progress = (float)$objRow->persentase_progress/100; $startDate = date_create($objRow->start_date); $endDate = date_create($objRow->end_date); $objRow->start_date = date_format($startDate,"Y-m-d H:i:s"); @@ -323,7 +323,7 @@ class ActivityController extends Controller if(!$data = Activity::find($id)) return response()->json(['status'=>'failed', 'action'=>'error','message'=> 'Data not found!','code'=>400], 400); - $data->progress = (int) $data->persentase_progress / 100; + $data->progress = (float) $data->persentase_progress / 100; $data->rencana_biaya = str_replace(".", ",", $data->rencana_biaya); return response()->json(['status'=>'success', "data"=> $data,'code'=>200], 200); } diff --git a/app/Models/VersionGantt.php b/app/Models/VersionGantt.php index 72c6185..236bff2 100644 --- a/app/Models/VersionGantt.php +++ b/app/Models/VersionGantt.php @@ -3,6 +3,8 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; +use App\Models\Activity; +use DateTime; class VersionGantt extends Model { @@ -28,4 +30,38 @@ class VersionGantt extends Model 'updated_at', 'updated_by' ]; + + public static function boot() { + parent::boot(); + + static::updated(function($data) { + $data->updateActDuration(); + }); + } + + public function updateActDuration(){ + $daysOff = explode(',', $this->config_dayoff); + $activities = Activity::where('version_gantt_id', $this->id)->get(); + foreach ($activities as $value) { + $startDate = new DateTime($value->start_date); + $endDate = new DateTime($value->end_date); + $duration = $endDate->diff($startDate)->days + 1; + + // Iterate through each day and subtract the days off + for ($i = 0; $i < $duration; $i++) { + $currentDate = clone $startDate; + $currentDate->modify("+$i day"); + + $currentDayOfWeek = $currentDate->format('N'); // Get the day of the week (1 - Monday, 7 - Sunday) + + if (in_array($currentDayOfWeek, $daysOff)) { + $duration--; // Subtract one day from the duration for each day off + } + } + + // Update the activity duration + $value->duration = $duration; + $value->save(); + } + } }