From 1bb91826ae8976eafb790058690ff5fa418850e3 Mon Sep 17 00:00:00 2001 From: wahyu Date: Mon, 9 Oct 2023 14:30:41 +0700 Subject: [PATCH] countDays, schedule, hr date --- app/Helpers/MasterFunctionsHelper.php | 20 ++++++++++ app/Http/Controllers/ActivityController.php | 40 +++++++++++++++++-- app/Http/Controllers/ProjectController.php | 16 +------- .../Controllers/UserToActivityController.php | 10 +++-- app/Models/UserToActivity.php | 2 + routes/web.php | 1 + 6 files changed, 67 insertions(+), 22 deletions(-) diff --git a/app/Helpers/MasterFunctionsHelper.php b/app/Helpers/MasterFunctionsHelper.php index 706d734..d681c40 100644 --- a/app/Helpers/MasterFunctionsHelper.php +++ b/app/Helpers/MasterFunctionsHelper.php @@ -36,6 +36,26 @@ class MasterFunctionsHelper return json_decode($output); } + public static function countDays($ganttId, $start, $end){ + $dayOffs = VersionGantt::where('id', $ganttId)->first()->config_dayoff; + $diff = date_diff($start, $end); + $endCount = clone $end; + $duration = $diff->days + 1; + $daysRemaining = $duration; + // Loop until the remaining days become zero + while ($daysRemaining > 0) { + $endCount->modify('-1 day'); + // Check if the current day is a day off (Sunday or Saturday) + $currentDayOfWeek = (int) $endCount->format('w'); + if (strpos($dayOffs, (string) $currentDayOfWeek) !== false) { + // continue; // Skip the day off and continue to the next day + $duration--; + } + $daysRemaining--; // Decrease the remaining days by one + } + return $duration; + } + public function getLatestGantt($id) { $maxGanttId = VersionGantt::where("proyek_id", $id)->max("id"); diff --git a/app/Http/Controllers/ActivityController.php b/app/Http/Controllers/ActivityController.php index 1b43a3e..a516e9e 100644 --- a/app/Http/Controllers/ActivityController.php +++ b/app/Http/Controllers/ActivityController.php @@ -1,6 +1,7 @@ json(['status' => 'success', 'message' => 'Activity Updated!', 'code' => 200], 200); } + public function updateSchedule($ganttId){ + if (empty($ganttId) || !is_int((int) $ganttId)) + return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400); + + $activities = Activity::where('version_gantt_id', $ganttId)->get(); + if (!$activities) + return response()->json(['status' => 'failed', 'message' => 'Activities not found!', 'code' => 404], 404); + $duration = 0; + foreach ($activities as $key => $activity) { + if (isset($activity->planned_start) && isset($activity->planned_end)) { + $today = new DateTime(); + $endDate = new DateTime($activity->end_date); + $startDate = new DateTime($activity->start_date); + $plannedStart = new DateTime($activity->planned_start); + $plannedEnd = new DateTime($activity->planned_end); + $duration = MasterFunctionsHelper::countDays($ganttId, $plannedStart, $plannedEnd); + if ($today >= $endDate) { + $endDate = $today; + $rerataBobot = $activity->bobot_planning / $duration; + $bobotActual = $activity->progress * $activity->bobot_planning; + $sisaBobot = $activity->bobot_planning - $bobotActual; + $sisaHari = $sisaBobot / $rerataBobot; + $endDate->modify("+".$sisaHari." Days"); + $actualDuration = MasterFunctionsHelper::countDays($ganttId, $startDate, $endDate); + $activity->duration = $actualDuration; + $activity->end_date = $endDate; + $activity->save(); + } + } + } + return response()->json(['status' => 'success', 'message' => 'Activities Updated!', 'code' => 200], 200); + } + public function batchUpdate(Request $request, $ganttId) { $entities = $request->all(); @@ -434,12 +468,12 @@ class ActivityController extends Controller if (isset($entity['data']['target'])) { $this->updateOrder($entity['data']['id'], $entity['data']['target']); } - if(!$activityToUpdate->update($entity['data'])) + if (!$activityToUpdate->update($entity['data'])) return response()->json(['status' => 'failed', 'message' => 'Failed to update activity !', 'code' => 500], 500); $updatedJobsDone = $activityToUpdate->jobs_done; - } else if ($entity['entity'] == "link"){ + } else if ($entity['entity'] == "link") { $linkToUpdate = $link->firstWhere('id', $entity['data']['id']); - if(!$linkToUpdate->update($entity['data'])) + if (!$linkToUpdate->update($entity['data'])) return response()->json(['status' => 'failed', 'message' => 'Failed to update link !', 'code' => 500], 500); } } diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index 7f5c8f9..75ecec4 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -438,23 +438,9 @@ class ProjectController extends Controller } $activity->start_date = $reports[$i]['min_date']; //same early $activity->end_date = $reports[$i]['max_date']->setTime(23,59,59); // same early - $dayOffs = VersionGantt::where('id', $activity->version_gantt_id)->first()->config_dayoff; $startDate = new DateTime($activity->start_date); $endDate = new DateTime($activity->end_date); - $diff = date_diff($startDate, $endDate); - $duration = $diff->days + 1; - $daysRemaining = $duration; - // Loop until the remaining days become zero - while ($daysRemaining > 0) { - $endDate->modify('-1 day'); - // Check if the current day is a day off (Sunday or Saturday) - $currentDayOfWeek = (int) $endDate->format('w'); - if (strpos($dayOffs, (string) $currentDayOfWeek) !== false) { - // continue; // Skip the day off and continue to the next day - $duration--; - } - $daysRemaining--; // Decrease the remaining days by one - } + $duration = MasterFunctionsHelper::countDays($activity->version_gantt_id, $startDate, $endDate); $activity->duration = $duration; $activity->actual_start = $reports[$i]['min_date']; $activity->save(); diff --git a/app/Http/Controllers/UserToActivityController.php b/app/Http/Controllers/UserToActivityController.php index c422cf6..2c3a658 100644 --- a/app/Http/Controllers/UserToActivityController.php +++ b/app/Http/Controllers/UserToActivityController.php @@ -27,7 +27,7 @@ class UserToActivityController extends Controller public function addMultiple(Request $request){ $users_id = $request->user_id; - $users_role = $request->role_proyek_id; + $users_role = $request->role_proyek_id; // $data = UserToActivity::where("activity_id", $request->activity_id); // if($data){ @@ -42,8 +42,10 @@ class UserToActivityController extends Controller "activity_id" => $request->activity_id, "role_proyek_id" => $users_role[$index], "proyek_id" => $request->proyek_id, - "created_by" => $this->currentName, - "version_gantt_id" => $request->version_gantt_id + "start_date" => $request->start_date, + "end_date" => $request->end_date, + "created_by" => $this->currentName, + "version_gantt_id" => $request->version_gantt_id ); $result = UserToActivity::create($dataInsert); if($result){ @@ -216,7 +218,7 @@ class UserToActivityController extends Controller public function datatables(Request $request){ $id_activity = $request->query('idact'); - $data = UserToActivity::select("assign_hr_to_activity.id as id", "u.name as user_name", "mrp.name as role_name") + $data = UserToActivity::select("assign_hr_to_activity.id as id", "assign_hr_to_activity.start_date", "assign_hr_to_activity.end_date", "u.name as user_name", "mrp.name as role_name") ->join("m_role_proyek as mrp", "mrp.id", "=", "assign_hr_to_activity.role_proyek_id") ->join("m_users as u", "u.id", "=", "assign_hr_to_activity.user_id") ->where('assign_hr_to_activity.activity_id', $id_activity) diff --git a/app/Models/UserToActivity.php b/app/Models/UserToActivity.php index 76f948a..1b795b1 100644 --- a/app/Models/UserToActivity.php +++ b/app/Models/UserToActivity.php @@ -18,6 +18,8 @@ class UserToActivity extends Model 'version_gantt_id', 'role_proyek_id', 'activity_id', + 'start_date', + 'end_date', 'created_at', 'created_by', 'updated_at', diff --git a/routes/web.php b/routes/web.php index bcc0e2b..7f62207 100644 --- a/routes/web.php +++ b/routes/web.php @@ -209,6 +209,7 @@ $router->group(['prefix'=>'api', 'middleware' => 'cors'], function () use ($rout $router->post('/activity/import-update', 'ActivityController@importUpdate'); $router->post('/activity/import-old', 'ActivityController@importOld'); $router->post('/activity/batch-update/{ganttId}', 'ActivityController@batchUpdate'); + $router->get('/activity/update-schedule/{ganttId}', 'ActivityController@updateSchedule'); $router->post('/task', 'ActivityController@add'); $router->get('/task/edit/{id}', 'ActivityController@edit'); $router->put('/task/{id}', 'ActivityController@update');