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.

280 lines
10 KiB

<?php
namespace App\Http\Controllers;
use App\Models\Activity;
use App\Models\CommentActivity;
use App\Models\Link;
use App\Models\Project;
use App\Models\TemplateGantt;
use App\Models\UserToActivity;
use Illuminate\Http\Request;
class ActivityController extends Controller
{
public function getByGanttId($id, $proyek_id)
{
if(Activity::where('proyek_id', $proyek_id)->where("version_gantt_id", $id)->count() == 0) {
$this->cloneTemplate($id, $proyek_id);
}
$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 = [];
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->type = "project";
$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();
}
foreach($data as $objRow){
$type = "project";
$dataChildren = $this->getChildren($id, $objRow->id);
$startDate = date_create($objRow->start_date);
$endDate = date_create($objRow->end_date);
if($objRow->type_activity=="milestone")
$type = $objRow->type_activity;
if(empty($dataChildren))
$type = "task";
$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;
$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)
{
$project = Project::find($proyek_id);
$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,
'rencana_biaya'=> $project->rencana_biaya,
'type_activity'=> 'header',
'created_by'=>$this->currentName,
]);
$resultTypeProject = TemplateGantt::where('proyek_type_id', $project->type_proyek_id)
->whereNull('parent_id')
->orderByRaw('id ASC')
->get();
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);
}
}
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();
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"]);
}
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;
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);
}
}