diff --git a/app/Http/Controllers/ActivityController.php b/app/Http/Controllers/ActivityController.php index 561c767..4fedaf9 100644 --- a/app/Http/Controllers/ActivityController.php +++ b/app/Http/Controllers/ActivityController.php @@ -6,6 +6,7 @@ use App\Models\CommentActivity; use App\Models\Link; use App\Models\Project; use App\Models\TemplateGantt; +use App\Models\VersionGantt; use App\Models\UserToActivity; use Illuminate\Http\Request; @@ -277,7 +278,112 @@ class ActivityController extends Controller $dataGet = $builder->get(); $totalRecord = $countBuilder->count(); return response()->json(['status'=>'success','code'=>200,'data'=>$dataGet, 'totalRecord'=>$totalRecord], 200); - } + } + + public function import(Request $request) + { + $data = $request->all(); + + $data['created_by'] = $this->currentName; + + Activity::where('version_gantt_id', $data['ganttId'])->delete(); + + $projectId = VersionGantt::where('id', $data['ganttId'])->first()->proyek_id; + + $activityStack = []; + + foreach ($data['activities'] as $i => $activity_row) { + $startDate = \DateTime::createFromFormat('d-m-y', $activity_row['start_date']); + $endDate = \DateTime::createFromFormat('d-m-y', $activity_row['end_date']); + + $input['name'] = $activity_row['name']; + $input['proyek_id'] = $projectId; + $input['version_gantt_id'] = $data['ganttId']; + $input['parent_id'] = null; + $input['start_date'] = $startDate->format('Y-m-d'); + $input['end_date'] = $endDate->format('Y-m-d'); + $input['duration'] = $activity_row['duration']; + $input['bobot_planning'] = $activity_row['weight']; + $input['persentase_progress'] = 0; + $input['type_activity'] = $i == 0 ? "header" : "task"; + $input['created_by'] = $this->currentName; + + if (!$activity = Activity::create($input)) { + Activity::where('version_gantt_id', $data['ganttId'])->delete(); + return response()->json(['status' => 'error', 'message' => 'Input failed on ' . $activity['name'], 'code' => 500], 500); + } + $data['activities'][$i]['activity_id'] = $activity->id; + + if ($i == 0) { + $activity->type_activity = "project"; + $activity->save(); + $activity->level = $activity_row['level']; + array_push($activityStack, $activity); + continue; + } + + $activity->level = $activity_row['level']; + + if ($lastStack = end($activityStack)) { + $levelLowerThanLastStack = $activity->level < $lastStack->level; + $levelEqualWithLastStack = $activity->level == $lastStack->level; + + if ($levelLowerThanLastStack) { + $lastStackIsNotRight = $levelLowerThanLastStack; + do { + array_pop($activityStack); + $lastStack = end($activityStack); + if ($activity->level > $lastStack->level) + $lastStackIsNotRight = false; + } while ($lastStackIsNotRight); + } + + if ($levelEqualWithLastStack) { + array_pop($activityStack); + } + } + + $activity->parent_id = $activityStack[count($activityStack) - 1]->id ?? null; + // there should be better way to except / filter attribute level before save because it's cause error + // cant use except() / filter() on $activity collection somehow + unset($activity->level); + $activity->save(); + $activity->level = $activity_row['level']; + + if (@$activityStack[count($activityStack) - 1]->level != $activity->level && $activity->level != $data['activities'][$i - 1]['level']) { + array_push($activityStack, $activity); + } + + if ($activity->level < @$data['activities'][$i + 1]['level']) { + unset($activity->level); + $activity->type_activity = "project"; + $activity->save(); + $activity->level = $activity_row['level']; + } + + if (!empty($activity_row['predecessor'])) { + $key = array_search($activity_row['predecessor'], array_column($data['activities'], 'no')); + + if (!$predecessorActivity = Activity::find($data['activities'][$key]['activity_id'])) + continue; + + $predecessorFinishDate = new \DateTime($predecessorActivity->end_date); + $interval = $predecessorFinishDate->diff(new \DateTime($activity->start_date)); + $diff = $interval->days; + + Link::create([ + 'created_by' => $this->currentName, + 's_activity_id' => $predecessorActivity->id, + 't_activity_id' => $activity->id, + 'type_link' => 0, + 'code_link' => 'FS', + 'version_gantt_id' => $data['ganttId'], + 'lag' => $diff > 1 ? $diff : null, + ]); + } + } + return response()->json(['stack' => $activityStack, 'status' => 'success', 'message' => 'Data imported!', 'projectId' => $projectId, 'code' => 200], 200); + } } diff --git a/app/Models/Activity.php b/app/Models/Activity.php index 7ee841a..4c436d8 100644 --- a/app/Models/Activity.php +++ b/app/Models/Activity.php @@ -63,9 +63,9 @@ class Activity extends Model $data->updatePersentaseProgress(); $data->updateCostActual(); } - if($data->start_date != request()->start_date || $data->end_date != request()->end_date) { - $data->updateStartEndDateHeader(); - } + // if($data->start_date != request()->start_date || $data->end_date != request()->end_date) { + // $data->updateStartEndDateHeader(); + // } }); static::deleted(function($data) { diff --git a/routes/web.php b/routes/web.php index b102e41..8d6f568 100644 --- a/routes/web.php +++ b/routes/web.php @@ -200,6 +200,7 @@ $router->group(['prefix'=>'api', 'middleware' => 'cors'], function () use ($rout $router->get('/sumVolActualM/{id}', 'ActivityController@sumVolumeActualMaterial'); $router->get('/activity/{id}/{proyek_id}/get', 'ActivityController@getByGanttId'); $router->get('/activity/search', 'ActivityController@search'); + $router->post('/activity/import', 'ActivityController@import'); $router->post('/task', 'ActivityController@add'); $router->get('/task/edit/{id}', 'ActivityController@edit'); $router->put('/task/{id}', 'ActivityController@update');