Custom Backend OSPRO Surveyor Indonesia
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

390 lines
14 KiB

2 years ago
<?php
3 years ago
namespace App\Http\Controllers;
use App\Models\Activity;
2 years ago
use App\Models\CommentActivity;
use App\Models\Link;
use App\Models\Project;
use App\Models\TemplateGantt;
use App\Models\VersionGantt;
2 years ago
use App\Models\UserToActivity;
use Illuminate\Http\Request;
3 years ago
class ActivityController extends Controller
{
2 years ago
public function getByGanttId($id, $proyek_id)
{
2 years ago
if(Activity::where('proyek_id', $proyek_id)->where("version_gantt_id", $id)->count() == 0) {
$this->cloneTemplate($id, $proyek_id);
}
2 years ago
$dataGantt = $this->getDataActivity($id);
return response()->json(['status'=>'success','data'=> $dataGantt,'code'=>200], 200);
}
private function getDataActivity($id)
{
$checkHeader = Activity::where('version_gantt_id', $id)->where('type_activity', 'header')->count(); $finalData = [];
2 years ago
if($checkHeader > 0){
$dataHeader = Activity::where('version_gantt_id', $id)->where('type_activity', 'header')->first();
$startDate = date_create($dataHeader->start_date);
$endDate = date_create($dataHeader->end_date);
$dataHeader->start_date = date_format($startDate,"Y-m-d H:i:s");
$dataHeader->end_date = date_format($endDate,"Y-m-d H:i:s");
$dataHeader->progress = $dataHeader->persentase_progress / 100;
$dataHeader->planned_start = isset($dataHeader->planned_start) ? date_format(date_create($dataHeader->planned_start),"Y-m-d H:i:s") : NULL;
$dataHeader->planned_end = isset($dataHeader->planned_end) ? date_format(date_create($dataHeader->planned_end),"Y-m-d H:i:s") : NULL;
$dataHeader->type = "header";
2 years ago
$dataHeader->text = $dataHeader->name;
$finalData[] = $dataHeader;
$data = Activity::where('version_gantt_id', $id)->where('parent_id', $dataHeader->id)->orderBy('id', 'asc')->get();
}else{
$data = Activity::where('version_gantt_id', $id)->whereNull('parent_id')->orderBy('id', 'asc')->get();
}
2 years ago
foreach($data as $objRow){
$type = "project";
$dataChildren = $this->getChildren($id, $objRow->id);
$startDate = date_create($objRow->start_date);
$endDate = date_create($objRow->end_date);
2 years ago
if($objRow->type_activity=="milestone")
$type = $objRow->type_activity;
if(empty($dataChildren))
$type = "task";
2 years ago
$objRow->text = $objRow->name;
$objRow->parent = $objRow->parent_id ? $objRow->parent_id : null;
$objRow->start_date = date_format($startDate,"Y-m-d H:i:s");
$objRow->end_date = date_format($endDate,"Y-m-d H:i:s");
$objRow->planned_start = isset($objRow->planned_start) ? date_format(date_create($objRow->planned_start),"Y-m-d H:i:s") : NULL;
$objRow->planned_end = isset($objRow->planned_end) ? date_format(date_create($objRow->planned_end),"Y-m-d H:i:s") : NULL;
$objRow->progress = $objRow->persentase_progress / 100;
2 years ago
$objRow->type = $type;
$finalData[] = $objRow;
$finalData = array_merge($finalData, $dataChildren);
}
$dataLink = Link::where('version_gantt_id', $id)->get();
$finalLink = [];
foreach($dataLink as $objRow)
{
$dataRow = array(
'id'=>$objRow->id,
'source'=>$objRow->s_activity_id,
'target'=>$objRow->t_activity_id,
'type'=>$objRow->type_link,
'code'=>$objRow->code_link
);
if($objRow->lag)
$dataRow['lag'] = $objRow->lag;
$finalLink[] = $dataRow;
}
$resultData = array(
"data"=>$finalData,
"links"=>$finalLink
);
return $resultData;
}
private function getChildren($gantt_id, $parent_id)
{
$finalData = [];
$data = Activity::where('version_gantt_id', $gantt_id)->where('parent_id', $parent_id)->orderBy('id', 'asc')->get();
foreach($data as $objRow){
$objRow->parent = $parent_id;
$objRow->text = $objRow->name;
$objRow->progress = (int)$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");
$objRow->end_date = date_format($endDate,"Y-m-d H:i:s");
$objRow->planned_start = isset($objRow->planned_start) ? date_format(date_create($objRow->planned_start),"Y-m-d H:i:s") : NULL;
$objRow->planned_end = isset($objRow->planned_end) ? date_format(date_create($objRow->planned_end),"Y-m-d H:i:s") : NULL;
$dataChildren = $this->getChildren($gantt_id, $objRow->id);
if($objRow->type_activity=="milestone"){
$objRow->type = $objRow->type_activity;
}elseif(empty($dataChildren)){
$objRow->type = "task";
}else{
$objRow->type = "project";
}
$finalData[] = $objRow;
$finalData = array_merge($finalData, $dataChildren);
}
return $finalData;
}
private function cloneTemplate($id, $proyek_id)
2 years ago
{
2 years ago
$project = Project::find($proyek_id);
2 years ago
$rootActivity = Activity::create([
'version_gantt_id'=>$id,
'proyek_id'=>$proyek_id,
'name'=> $project->nama,
'kode_sortname'=>$project->kode_sortname,
'start_date'=> $project->mulai_proyek,
'end_date'=> $project->akhir_proyek,
2 years ago
'rencana_biaya'=> $project->rencana_biaya,
2 years ago
'type_activity'=> 'header',
'created_by'=>$this->currentName,
]);
$resultTypeProject = TemplateGantt::where('proyek_type_id', $project->type_proyek_id)
2 years ago
->whereNull('parent_id')
->orderByRaw('id ASC')
->get();
2 years ago
2 years ago
foreach($resultTypeProject as $objRow){
$childActivities = TemplateGantt::where("parent_id", $objRow->id)->count();
$resultNew = Activity::create([
'type_activity'=> $childActivities > 0 ? "project" : "task",
'version_gantt_id'=>$id,
'parent_id'=>$rootActivity->id,
'proyek_id'=>$proyek_id,
'name'=> $objRow->name_activity,
'start_date'=>date("Y-m-d H:i:s"),
'end_date'=>date("Y-m-d H:i:s"),
'created_by'=>$this->currentName
]);
$this->getChildrenTemplate($id, $objRow->id, $project->type_project_id, $proyek_id, $resultNew->id, $project->mulai_proyek);
2 years ago
}
}
private function getChildrenTemplate($id, $parent_id, $type_proyek_id, $proyek_id, $parent_new, $firstDay)
{
$data = TemplateGantt::where('parent_id', $parent_id)->orderByRaw('id ASC')->get();
2 years ago
foreach($data as $objRow){
$childActivities = TemplateGantt::where("parent_id", $objRow->id)->count();
$resultNew = Activity::create([
'type_activity'=> $childActivities > 0 ? "project" : "task",
'version_gantt_id'=>$id,
'parent_id'=>$parent_new,
'proyek_id'=>$proyek_id,
'name'=> $objRow->name_activity,
'start_date'=>$firstDay,
'end_date'=>$firstDay,
'created_by'=>$this->currentName
]);
$this->getChildrenTemplate($id, $objRow->id, $type_proyek_id, $proyek_id, $resultNew->id, $firstDay);
}
}
public function add(Request $request)
{
$this->validate($request, [
'version_gantt_id'=>'required'
]);
$data = $request->all();
$data['name'] = $request->text;
$data['persentase_progress'] = $request->progress;
$data['created_by'] = $this->currentName;
$data['type_activity'] = "task";
$parent = $data['parent_id'] ?? null;
if($parent){
$parentData = Activity::find($parent);
if($parentData->parent_id) {
$parentData->update(["type_activity" => "project"]);
}
2 years ago
CommentActivity::where('activity_id', $parent)->delete();
UserToActivity::where('activity_id', $parent)->delete();
}
if(!$result = Activity::create($data))
return response()->json(['status'=>'failed','action'=>'error','code'=> 500], 500);
return response()->json(['status'=>'success','action'=>'inserted', 'tid'=>$result->id,'code'=>200], 200);
}
public function edit($id){
if(empty($id) || !is_int((int)$id))
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400);
if(!$result = Activity::find($id))
return response()->json(['status'=>'failed','message'=>'Data not found!','code'=> 404], 404);
return response()->json(['status'=>'success','code'=>200,'data'=> $result], 200);
}
public function update(Request $request, $id)
{
if(empty($id) || !is_int((int)$id))
return response()->json(['status'=>'failed', 'action'=>'error','message'=>'id is required!','code'=>400], 400);
$updateBobot = true;
2 years ago
if(!$data = Activity::find($id))
return response()->json(['status'=>'failed', 'action'=>'error','message'=>'Data not found!','code'=> 404], 404);
$dataUpdate = $request->all();
$dataUpdate['name'] = $request->text;
$dataUpdate['persentase_progress'] = $request->progress*100;
$dataUpdate['updated_by'] = $this->currentName;
if($data->type_activity!='header')
$dataUpdate['type_activity'] = $request->type;
if(!$data->update($dataUpdate))
return response()->json(['status'=>'failed', 'action'=>'error','message'=>'data activity failed updated!','code'=>400], 400);
return response()->json(['status'=>'success','update_bobot'=> $updateBobot, 'data'=>$dataUpdate, 'action'=>'updated','message'=>'Activity updated!','code'=>200], 200);
}
public function updateRegular(Request $request, $id){
if(empty($id) || !is_int((int)$id))
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400);
$data = Activity::find($id);
if(!$data = Activity::find($id))
return response()->json(['status'=>'failed','message'=>'Data not found!','code'=> 404], 404);
if(!$data->update($request->all()))
return response()->json(['status'=>'failed','message'=>'Failed to update!','code'=> 500], 500);
return response()->json(['status'=>'success','message'=>'Activity Updated!','code'=> 200], 200);
}
public function delete($id)
{
if(!$data = Activity::find($id))
return response()->json(['status'=>'failed', 'action'=>'error','message'=> 'Data not found!','code'=> 404], 404);
if(!$data->delete())
return response()->json(['status'=>'failed', 'action'=>'error','message'=>'data activity failed deleted!','code'=> 500], 500);
return response()->json(['status'=>'success', "action"=>"deleted",'message'=>'data activity successfully deleted!','code'=>200], 200);
}
public function getUpdate($id)
{
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->rencana_biaya = str_replace(".", ",", $data->rencana_biaya);
return response()->json(['status'=>'success', "data"=> $data,'code'=>200], 200);
}
public function search(Request $request)
{
$dataBuilder = $this->setUpPayload($request->all(), 'm_activity');
$builder = $dataBuilder['builder'];
$countBuilder = $dataBuilder['count'];
$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);
}
2 years ago
$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,
]);
}
}
2 years ago
return response()->json(['stack' => $activityStack, 'status' => 'success', 'message' => 'Data imported!', 'projectId' => $projectId, 'code' => 200], 200);
}
}