Browse Source

refactor

pull/3/head
Muhammad Sulaiman Yusuf 2 years ago
parent
commit
ffb296832e
  1. 450
      app/Http/Controllers/ActivityController.php

450
app/Http/Controllers/ActivityController.php

@ -2,43 +2,38 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Activity; use App\Models\Activity;
use App\Models\Link;
use App\Models\Project;
use App\Models\TemplateGantt;
use App\Models\UserToActivity;
use App\Models\CommentActivity;
use App\Models\AssignMaterial; use App\Models\AssignMaterial;
use App\Models\AssignTools; use App\Models\AssignTools;
use App\Models\VersionGantt; use App\Models\CommentActivity;
use App\Models\Holiday; use App\Models\Holiday;
use App\Models\Link;
use App\Models\Project;
use App\Models\ReportActivity; use App\Models\ReportActivity;
use App\Models\ReportActivityMaterial; use App\Models\ReportActivityMaterial;
use DB; use App\Models\TemplateGantt;
use App\Models\UserToActivity;
use App\Models\VersionGantt;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
class ActivityController extends Controller class ActivityController extends Controller
{ {
public function getByGanttId($id, $proyek_id) public function getByGanttId($id, $proyek_id)
{ {
$cekData = Activity::where("version_gantt_id", $id)->count(); if(Activity::where("version_gantt_id", $id)->count() == 0)
$dataGantt = [];
if($cekData > 0){
$dataGantt = $this->getDataActivity($id); $dataGantt = $this->getDataActivity($id);
}else{
$this->templateToActivity($id, $proyek_id); $this->templateToActivity($id, $proyek_id);
$dataGantt = $this->getDataActivity($id); $dataGantt = $this->getDataActivity($id);
}
return response()->json(['status'=>'success','data'=> $dataGantt,'code'=>200], 200); return response()->json(['status'=>'success','data'=> $dataGantt,'code'=>200], 200);
} }
private function getDataActivity($id) 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(); $dataHeader = Activity::where('version_gantt_id', $id)->where('type_activity', 'header')->first();
$startDate = date_create($dataHeader->start_date); $startDate = date_create($dataHeader->start_date);
$endDate = date_create($dataHeader->end_date); $endDate = date_create($dataHeader->end_date);
@ -48,15 +43,19 @@ class ActivityController extends Controller
$dataHeader->text = $dataHeader->name; $dataHeader->text = $dataHeader->name;
$finalData[] = $dataHeader; $finalData[] = $dataHeader;
$data = Activity::where('version_gantt_id', $id)->where('parent_id', $dataHeader->id)->orderBy('id', 'asc')->get(); $data = Activity::where('version_gantt_id', $id)->where('parent_id', $dataHeader->id)->orderBy('id', 'asc')->get();
}else{
if(Activity::where('version_gantt_id', $id)->where('type_activity', 'header')->count() == 0)
$data = Activity::where('version_gantt_id', $id)->whereNull('parent_id')->orderBy('id', 'asc')->get(); $data = Activity::where('version_gantt_id', $id)->whereNull('parent_id')->orderBy('id', 'asc')->get();
}
foreach($data as $objRow){ foreach($data as $objRow){
$type = "project";
if($objRow->type_activity=="milestone")
$type = $objRow->type_activity;
if(empty($dataChildren))
$type = "task";
$objRow->text = $objRow->name; $objRow->text = $objRow->name;
if($objRow->parent_id){ $objRow->parent = $objRow->parent_id ? $objRow->parent_id : null;
$objRow->parent = $objRow->parent_id;
}
$startDate = date_create($objRow->start_date); $startDate = date_create($objRow->start_date);
$endDate = date_create($objRow->end_date); $endDate = date_create($objRow->end_date);
$objRow->jobs_done = $this->sumVolumeActualMaterial($objRow->id); $objRow->jobs_done = $this->sumVolumeActualMaterial($objRow->id);
@ -69,13 +68,7 @@ class ActivityController extends Controller
$objRow->planned_end = isset($objRow->planned_end) ? date_format(date_create($objRow->planned_end),"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 = (int)$objRow->persentase_progress/100; $objRow->progress = (int)$objRow->persentase_progress/100;
$dataChildren = $this->getChildren($id, $objRow->id); $dataChildren = $this->getChildren($id, $objRow->id);
if($objRow->type_activity=="milestone"){ $objRow->type = $type;
$objRow->type = $objRow->type_activity;
}else if(empty($dataChildren)){
$objRow->type = "task";
}else{
$objRow->type = "project";
}
$finalData[] = $objRow; $finalData[] = $objRow;
$finalData = array_merge($finalData, $dataChildren); $finalData = array_merge($finalData, $dataChildren);
} }
@ -91,14 +84,9 @@ class ActivityController extends Controller
'type'=>$objRow->type_link, 'type'=>$objRow->type_link,
'code'=>$objRow->code_link 'code'=>$objRow->code_link
); );
if($objRow->lag) if($objRow->lag)
{
$dataRow['lag'] = $objRow->lag; $dataRow['lag'] = $objRow->lag;
}
$finalLink[] = $dataRow; $finalLink[] = $dataRow;
} }
$resultData = array( $resultData = array(
@ -141,108 +129,88 @@ class ActivityController extends Controller
return $finalData; return $finalData;
} }
private function sumVolumeActuall($id)
{
$sumVa = ReportActivity::where("activity_id", $id)->sum("job_count_report");
return $sumVa;
}
private function sumVolumeActualMaterial($id) private function sumVolumeActualMaterial($id)
{ {
$tmpPercentage = [];
$dataPlan = AssignMaterial::where('activity_id', $id)->get(); $dataPlan = AssignMaterial::where('activity_id', $id)->get();
$tmpPercentage1 = []; if($dataPlan->isEmpty())
if(!$dataPlan->isEmpty()){ return 0;
foreach ($dataPlan as $value) { foreach ($dataPlan as $value) {
$qty_plan = $value->qty_planning; $tmpPercentage[] = 100;
$status_activity = $value->status_activity;
$getDataVolActual = ReportActivityMaterial::where('assign_material_id', '=', $value->id)->sum("qty"); $getDataVolActual = ReportActivityMaterial::where('assign_material_id', '=', $value->id)->sum("qty");
$percentage1 = ($getDataVolActual * 100) / $qty_plan; $percentage = ($getDataVolActual * 100) / $value->qty_planning;
if($status_activity == 'done'){ if($value->status_activity != 'done'){
$tmpPercentage1[] = 100; $tmpPercentage[] = $percentage >= 100 ? 90 : $percentage;
} }
else{
$tmpPercentage1[] = $percentage1 >= 100 ? 90 : $percentage1;
} }
}
$totalPercentage = array_sum($tmpPercentage1) > 0 ? array_sum($tmpPercentage1) / count($tmpPercentage1) : 0;
}else{
return array_sum($tmpPercentage) > 0 ? array_sum($tmpPercentage) / count($tmpPercentage) : 0;
$totalPercentage = 0;
}
return $totalPercentage;
} }
private function getUserActivity($id) private function getUserActivity($id)
{ {
$data = UserToActivity::select("u.name as name")->join("m_users as u", "u.id", "=", "assign_hr_to_activity.user_id")->where('assign_hr_to_activity.activity_id', $id)->get(); return Arr::flatten(UserToActivity::select("u.name as name")
$nameCollection = []; ->join("m_users as u", "u.id", "=", "assign_hr_to_activity.user_id")
foreach($data as $value){ ->where('assign_hr_to_activity.activity_id', $id)
$nameCollection[] = $value->name; ->get()
} ->toArray());
return $nameCollection;
} }
private function getMaterialActivity($id) private function getMaterialActivity($id)
{ {
$data = AssignMaterial::select("m.description as name")->join("m_req_material as m", "m.id", "=", "assign_material_to_activity.material_id")->where('assign_material_to_activity.activity_id', $id)->get(); return Arr::flatten(AssignMaterial::select("m.description as name")
$nameCollection = []; ->join("m_req_material as m", "m.id", "=", "assign_material_to_activity.material_id")
foreach($data as $value){ ->where('assign_material_to_activity.activity_id', $id)
$nameCollection[] = $value->name; ->get()
} ->toArray());
return $nameCollection;
} }
private function getToolsActivity($id) private function getToolsActivity($id)
{ {
$data = AssignTools::select("m.name as name")->join("m_tools_resource as m", "m.id", "=", "assign_tools_to_activity.tools_id")->where('assign_tools_to_activity.activity_id', $id)->get(); return Arr::flatten(AssignTools::select("m.name as name")
$nameCollection = []; ->join("m_tools_resource as m", "m.id", "=", "assign_tools_to_activity.tools_id")
foreach($data as $value){ ->where('assign_tools_to_activity.activity_id', $id)
$nameCollection[] = $value->name; ->get()
} ->toArray());
return $nameCollection;
} }
private function templateToActivity($id, $proyek_id) private function templateToActivity($id, $proyek_id)
{ {
$resultProject = Project::find($proyek_id); $project = Project::findOrFail($proyek_id);
$newRencanaBiaya = str_replace(".","", $resultProject->rencana_biaya);
$dataHeader = array( $rootActivity = Activity::create([
'version_gantt_id'=>$id, 'version_gantt_id'=>$id,
'proyek_id'=>$proyek_id, 'proyek_id'=>$proyek_id,
'name'=> $resultProject->nama, 'name'=> $project->nama,
'kode_sortname'=>$resultProject->kode_sortname, 'kode_sortname'=>$project->kode_sortname,
'start_date'=> $resultProject->mulai_proyek, 'start_date'=> $project->mulai_proyek,
'end_date'=> $resultProject->akhir_proyek, 'end_date'=> $project->akhir_proyek,
'rencana_biaya'=> $newRencanaBiaya, 'rencana_biaya'=> str_replace(".","", $project->rencana_biaya),
'type_activity'=> 'header', 'type_activity'=> 'header',
'created_by'=>$this->currentName 'created_by'=>$this->currentName,
); ]);
$newHeader = Activity::create($dataHeader);
$resultTypeProject = TemplateGantt::where('proyek_type_id',$resultProject->type_proyek_id)->whereNull('parent_id')->orderByRaw('id ASC')->get();
$typeProjectId = $resultProject->type_proyek_id; $resultTypeProject = TemplateGantt::where('proyek_type_id',$project
->type_proyek_id)
->whereNull('parent_id')
->orderByRaw('id ASC')
->get();
foreach($resultTypeProject as $objRow){ foreach($resultTypeProject as $objRow){
$newActivity = array( $childActivities = TemplateGantt::where("parent_id", $objRow->id)->count();
$resultNew = Activity::create([
'type_activity'=> $childActivities > 0 ? "project" : "task",
'version_gantt_id'=>$id, 'version_gantt_id'=>$id,
'parent_id'=>$newHeader->id, 'parent_id'=>$rootActivity->id,
'proyek_id'=>$proyek_id, 'proyek_id'=>$proyek_id,
'name'=> $objRow->name_activity, 'name'=> $objRow->name_activity,
'start_date'=>date("Y-m-d H:i:s"), 'start_date'=>date("Y-m-d H:i:s"),
'end_date'=>date("Y-m-d H:i:s"), 'end_date'=>date("Y-m-d H:i:s"),
'created_by'=>$this->currentName 'created_by'=>$this->currentName
); ]);
$checkChild = TemplateGantt::where("parent_id", $objRow->id)->count(); $this->getChildrenTemplate($id, $objRow->id, $project->type_project_id ,$proyek_id, $resultNew->id, $project->mulai_proyek);
if($checkChild > 0){
$newActivity['type_activity'] = "project";
}else{
$newActivity['type_activity'] = "task";
}
$resultNew = Activity::create($newActivity);
$this->getChildrenTemplate($id, $objRow->id, $typeProjectId, $proyek_id, $resultNew->id, $resultProject->mulai_proyek);
} }
} }
@ -250,7 +218,9 @@ class ActivityController extends Controller
{ {
$data = TemplateGantt::where('proyek_type_id', $type_proyek_id)->where('parent_id', $parent_id)->orderByRaw('id ASC')->get(); $data = TemplateGantt::where('proyek_type_id', $type_proyek_id)->where('parent_id', $parent_id)->orderByRaw('id ASC')->get();
foreach($data as $objRow){ foreach($data as $objRow){
$newActivity = array( $childActivities = TemplateGantt::where("parent_id", $objRow->id)->count();
$resultNew = Activity::create([
'type_activity'=> $childActivities > 0 ? "project" : "task",
'version_gantt_id'=>$id, 'version_gantt_id'=>$id,
'parent_id'=>$parent_new, 'parent_id'=>$parent_new,
'proyek_id'=>$proyek_id, 'proyek_id'=>$proyek_id,
@ -258,14 +228,7 @@ class ActivityController extends Controller
'start_date'=>$firstDay, 'start_date'=>$firstDay,
'end_date'=>$firstDay, 'end_date'=>$firstDay,
'created_by'=>$this->currentName 'created_by'=>$this->currentName
); ]);
$checkChild = TemplateGantt::where("parent_id", $objRow->id)->count();
if($checkChild > 0){
$newActivity['type_activity'] = "project";
}else{
$newActivity['type_activity'] = "task";
}
$resultNew = Activity::create($newActivity);
$this->getChildrenTemplate($id, $objRow->id, $type_proyek_id, $proyek_id, $resultNew->id, $firstDay); $this->getChildrenTemplate($id, $objRow->id, $type_proyek_id, $proyek_id, $resultNew->id, $firstDay);
} }
} }
@ -282,20 +245,17 @@ class ActivityController extends Controller
$data['created_by'] = $this->currentName; $data['created_by'] = $this->currentName;
$data['type_activity'] = "task"; $data['type_activity'] = "task";
$parent = isset($data['parent_id']) ? $data['parent_id'] : null; $parent = $data['parent_id'] ?? null;
if($parent && $parent > 0){ if($parent){
$this->updateTypeProject($parent); $this->updateTypeProject($parent);
$comment = CommentActivity::where('activity_id', $parent)->delete(); CommentActivity::where('activity_id', $parent)->delete();
$hr = UserToActivity::where('activity_id', $parent)->delete(); UserToActivity::where('activity_id', $parent)->delete();
} }
$result = Activity::create($data); if(!$result = Activity::create($data))
return response()->json(['status'=>'failed','action'=>'error','code'=> 500], 500);
if($result){
return response()->json(['status'=>'success','action'=>'inserted', 'tid'=>$result->id,'code'=>200], 200); return response()->json(['status'=>'success','action'=>'inserted', 'tid'=>$result->id,'code'=>200], 200);
}else{
return response()->json(['status'=>'failed','action'=>'error','code'=>400], 400);
}
} }
private function updateTypeProject($id) private function updateTypeProject($id)
@ -304,46 +264,42 @@ class ActivityController extends Controller
} }
public function edit($id){ public function edit($id){
if(!$id || (int) $id < 0 || $id==""){ if(empty($id) || !is_int((int)$id))
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400);
die();
}
$result = Activity::find($id); if(!$result = Activity::find($id))
return response()->json(['status'=>'failed','message'=>'Data not found!','code'=> 404], 404);
if($result){
return response()->json(['status'=>'success','code'=>200,'data'=> $result], 200); return response()->json(['status'=>'success','code'=>200,'data'=> $result], 200);
}else{
return response()->json(['status'=>'failed','message'=>'failed get data activity, please try again later!','code'=>400], 400);
}
} }
public function update(Request $request, $id) public function update(Request $request, $id)
{ {
if(!$id || (int) $id < 0 || $id==""){ if(empty($id) || !is_int((int)$id))
return response()->json(['status'=>'failed', 'action'=>'error','message'=>'id is required!','code'=>400], 400); return response()->json(['status'=>'failed', 'action'=>'error','message'=>'id is required!','code'=>400], 400);
}
$data = Activity::find($id);
$oldRencanaBiaya = $data->rencana_biaya;
$newRencanaBiaya = str_replace(",",".",$request->rencana_biaya);
if($oldRencanaBiaya==$newRencanaBiaya){
$updateBobot = false; $updateBobot = false;
}else{ $data = Activity::findOrFail($id);
$updateBobot = true; if(!$data = Activity::find($id))
} return response()->json(['status'=>'failed', 'action'=>'error','message'=>'Data not found!','code'=> 404], 404);
$parent = $data->parent_id; $parent = $data->parent_id;
$dataUpdate = $request->all(); $dataUpdate = $request->all();
$oldRencanaBiaya = $data->rencana_biaya;
$newRencanaBiaya = str_replace(",",".",$request->rencana_biaya);
if($oldRencanaBiaya != $newRencanaBiaya)
$updateBobot = true;
$dataUpdate['name'] = $request->text; $dataUpdate['name'] = $request->text;
$dataUpdate['persentase_progress'] = $request->progress*100; $dataUpdate['persentase_progress'] = $request->progress*100;
$dataUpdate['updated_by'] = $this->currentName; $dataUpdate['updated_by'] = $this->currentName;
if($data->type_activity!='header'){ if($data->type_activity!='header')
$dataUpdate['type_activity'] = $request->type; $dataUpdate['type_activity'] = $request->type;
}
if($data){ if(!$data->update($dataUpdate))
$result = $data->update($dataUpdate); return response()->json(['status'=>'failed', 'action'=>'error','message'=>'data activity failed updated!','code'=>400], 400);
if($result){
if($parent && (int)$parent > 0){ if($parent){
$this->updateCostPlanning($parent); $this->updateCostPlanning($parent);
$this->updatePersentaseProgress($parent); $this->updatePersentaseProgress($parent);
$this->updateCostActual($parent); $this->updateCostActual($parent);
@ -351,58 +307,53 @@ class ActivityController extends Controller
$this->calculateAllBobot($data->proyek_id, $data->version_gantt_id); $this->calculateAllBobot($data->proyek_id, $data->version_gantt_id);
return response()->json(['status'=>'success','update_bobot'=>$updateBobot,'data'=>$dataUpdate, 'action'=>'updated','message'=>'data activity successfully updated!','code'=>200], 200); return response()->json(['status'=>'success','update_bobot'=> $updateBobot, 'data'=>$dataUpdate, 'action'=>'updated','message'=>'Activity updated!','code'=>200], 200);
}else{
return response()->json(['status'=>'failed', 'action'=>'error','message'=>'data activity failed updated!','code'=>400], 400);
}
}else{
return response()->json(['status'=>'failed', 'action'=>'error','message'=>'data activity not found!','code'=>400], 400);
die();
}
} }
public function updateRegular(Request $request, $id){ public function updateRegular(Request $request, $id){
if(!$id || (int) $id < 0 || $id==""){ if(empty($id) || !is_int((int)$id))
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400);
}
$data = Activity::find($id); $data = Activity::find($id);
if($data){ if(!$data = Activity::find($id))
$result = $data->update($request->all()); return response()->json(['status'=>'failed','message'=>'Data not found!','code'=> 404], 404);
}else{
return response()->json(['status'=>'failed','message'=>'data activity not found!','code'=>400], 400);
die();
}
if(!$data->update($request->all()))
return response()->json(['status'=>'failed','message'=>'Failed to update!','code'=> 500], 500);
if($result){ return response()->json(['status'=>'success','message'=>'Activity Updated!','code'=> 200], 200);
return response()->json(['status'=>'success','message'=>'data activity successfully updated!','code'=>200], 200);
}else{
return response()->json(['status'=>'failed','message'=>'data activity failed updated!','code'=>400], 400);
}
} }
private function calculateAllBobot($proyek, $gantt) private function calculateAllBobot($proyek, $gantt)
{ {
$checkHeader = Activity::where('version_gantt_id', $gantt)->where("proyek_id", $proyek)->where('type_activity', 'header')->count(); if(Activity::where('version_gantt_id', $gantt)->where("proyek_id", $proyek)->where('type_activity', 'header')->count() == 0)
if($checkHeader > 0){ $totalCost = Activity::select(
$dataHeader = Activity::where('version_gantt_id', $gantt)->where("proyek_id", $proyek)->where('type_activity', 'header')->first(); DB::raw('sum(cast(rencana_biaya as double precision))')
$totalCost = Activity::select(DB::raw('sum(cast(rencana_biaya as double precision))'))->where("proyek_id", $proyek)->where("version_gantt_id", $gantt)->where("parent_id", $dataHeader->id)->first(); )
}else{ ->where("proyek_id", $proyek)
$totalCost = Activity::select(DB::raw('sum(cast(rencana_biaya as double precision))'))->where("proyek_id", $proyek)->where("version_gantt_id", $gantt)->whereNull("parent_id")->first(); ->where("version_gantt_id", $gantt)
} ->whereNull("parent_id")
$totalCost = $totalCost->sum; ->first();
if($totalCost > 0){
$activitys = Activity::where("proyek_id", $proyek)->where("version_gantt_id", $gantt)->get(); $rootActivity = Activity::where('version_gantt_id', $gantt)
foreach ($activitys as $activity) { ->where("proyek_id", $proyek)
$bobot = ((int)$activity->rencana_biaya/$totalCost)*100; ->where('type_activity', 'header')
$willUpdated = Activity::find($activity->id); ->first();
$dataUpdate = array(
"bobot_planning"=>$bobot, $totalCost = Activity::select(DB::raw('sum(cast(rencana_biaya as double precision))'))
->where("proyek_id", $proyek)
->where("version_gantt_id", $gantt)
->where("parent_id", $rootActivity->id)
->first();
if($totalCost->sum > 0){
$activities = Activity::where("proyek_id", $proyek)->where("version_gantt_id", $gantt)->get();
foreach ($activities as $activity) {
$activity->update([
"bobot_planning" => ( (int)$activity->rencana_biaya / $totalCost->sum ) * 100,
"updated_by" => $this->currentName "updated_by" => $this->currentName
); ]);
$willUpdated->update($dataUpdate);
} }
} }
return true; return true;
@ -410,42 +361,35 @@ class ActivityController extends Controller
public function delete($id) public function delete($id)
{ {
$data = Activity::find($id); $data = Activity::findOrFail($id);
if($data){ if(!$data = Activity::find($id))
$parent = $data->parent_id; return response()->json(['status'=>'failed', 'action'=>'error','message'=> 'Data not found!','code'=> 404], 404);
if($parent && (int)$parent > 0){
$this->checkForUpdateParent($parent); if($data->parent_id){
$this->updateCostPlanning($parent); $this->checkForUpdateParent($data->parent_id);
$this->updatePersentaseProgress($parent); $this->updateCostPlanning($data->parent_id);
$this->updateCostActual($parent); $this->updatePersentaseProgress($data->parent_id);
} $this->updateCostActual($data->parent_id);
$delete = $data->delete();
}else{
return response()->json(['status'=>'failed', 'action'=>'error','message'=>'data activity not found!','code'=>400], 400);
die();
} }
if(!$data->delete())
return response()->json(['status'=>'failed', 'action'=>'error','message'=>'data activity failed deleted!','code'=> 500], 500);
if($delete){
return response()->json(['status'=>'success', "action"=>"deleted",'message'=>'data activity successfully deleted!','code'=>200], 200); return response()->json(['status'=>'success', "action"=>"deleted",'message'=>'data activity successfully deleted!','code'=>200], 200);
}else{
return response()->json(['status'=>'failed', 'action'=>'error','message'=>'data activity failed deleted!','code'=>400], 400);
}
} }
private function checkForUpdateParent($id) private function checkForUpdateParent($id)
{ {
$count = Activity::where("parent_id", $id)->count(); if(Activity::where("parent_id", $id)->count() == 0)
if(!$count || $count==0){
Activity::find($id)->update(["type_activity"=>"task"]); Activity::find($id)->update(["type_activity"=>"task"]);
} }
}
public function getUpdate($id) public function getUpdate($id)
{ {
$data = Activity::find($id); if(!$data = Activity::find($id))
if($data){ return response()->json(['status'=>'failed', 'action'=>'error','message'=> 'Data not found!','code'=>400], 400);
$data->progress = (int) $data->persentase_progress / 100; $data->progress = (int) $data->persentase_progress / 100;
$data->rencana_biaya = str_replace(".", ",", $data->rencana_biaya); $data->rencana_biaya = str_replace(".", ",", $data->rencana_biaya);
$data->jobs_done = $this->sumVolumeActualMaterial($data->id); $data->jobs_done = $this->sumVolumeActualMaterial($data->id);
@ -453,69 +397,54 @@ class ActivityController extends Controller
$data->assign_tools = $this->getToolsActivity($data->id); $data->assign_tools = $this->getToolsActivity($data->id);
$data->assign_material = $this->getMaterialActivity($data->id); $data->assign_material = $this->getMaterialActivity($data->id);
return response()->json(['status'=>'success', "data"=> $data,'code'=>200], 200); return response()->json(['status'=>'success', "data"=> $data,'code'=>200], 200);
}else{
return response()->json(['status'=>'failed', 'action'=>'error','message'=>'data activity not found!','code'=>400], 400);
}
} }
private function updateCostPlanning($id) private function updateCostPlanning($id)
{ {
$sumBiaya = Activity::select(DB::raw('sum(cast(rencana_biaya as double precision))'))->where("parent_id", $id)->first(); $sumBiaya = Activity::select(DB::raw('sum(cast(rencana_biaya as double precision))'))
$dataActivity = Activity::find($id); ->where("parent_id", $id)
$dataUpdate = array( ->first();
$activity = Activity::find($id);
$data= array(
"rencana_biaya" => $sumBiaya->sum, "rencana_biaya" => $sumBiaya->sum,
"updated_by" => $this->currentName "updated_by" => $this->currentName
); );
$dataActivity->update($dataUpdate);
if($dataActivity){ if($activity->update($data) && $activity->parent_id)
$parent = $dataActivity->parent_id; $this->updateCostPlanning($activity->parent_id);
if($parent && (int)$parent > 0){
$this->updateCostPlanning($parent);
}
}
} }
private function updatePersentaseProgress($id) private function updatePersentaseProgress($id)
{ {
$sumProgress = Activity::where("parent_id", $id)->sum("persentase_progress"); $siblings = Activity::where("parent_id", $id);
$totalChild = Activity::where("parent_id", $id)->count(); $sumProgress = $siblings->sum("persentase_progress");
$dataActivity = Activity::find($id); $totalChild = $siblings->count();
$calculateProgressActual = $sumProgress/$totalChild; $activity = Activity::find($id);
$dataUpdate = array(
"persentase_progress" => $calculateProgressActual, $activity->update([
"persentase_progress" => $sumProgress / $totalChild,
"updated_by" => $this->currentName "updated_by" => $this->currentName
); ]);
$dataActivity->update($dataUpdate); if($activity->parent_id)
if($dataActivity){ $this->updatePersentaseProgress($activity->parent_id);
$parent = $dataActivity->parent_id;
if($parent && (int)$parent > 0){
$this->updatePersentaseProgress($parent);
}
}
} }
private function updateCostActual($id) private function updateCostActual($id)
{ {
$biayaActual = Activity::where("parent_id", $id)->sum("biaya_actual"); $activity = Activity::find($id);
$dataActivity = Activity::find($id);
$dataUpdate = array( $activity->update([
"biaya_actual" => $biayaActual, "biaya_actual" => Activity::where("parent_id", $id)->sum("biaya_actual"),
"updated_by" => $this->currentName "updated_by" => $this->currentName
); ]);
$dataActivity->update($dataUpdate); if($activity->parent_id)
if($dataActivity){ $this->updateCostActual($activity->parent_id);
$parent = $dataActivity->parent_id;
if($parent && (int)$parent > 0){
$this->updateCostActual($parent);
}
}
} }
public function search(Request $request) public function search(Request $request)
{ {
$payload = $request->all(); $dataBuilder = $this->setUpPayload($request->all(), 'm_activity');
$dataBuilder = $this->setUpPayload($payload, 'm_activity');
$builder = $dataBuilder['builder']; $builder = $dataBuilder['builder'];
$countBuilder = $dataBuilder['count']; $countBuilder = $dataBuilder['count'];
$dataGet = $builder->get(); $dataGet = $builder->get();
@ -523,21 +452,12 @@ class ActivityController extends Controller
return response()->json(['status'=>'success','code'=>200,'data'=>$dataGet, 'totalRecord'=>$totalRecord], 200); return response()->json(['status'=>'success','code'=>200,'data'=>$dataGet, 'totalRecord'=>$totalRecord], 200);
} }
public function calculateAllCost($activity_id, $proyek_id)
{
$materialCostPlanning = 0;
$toolsCostPlanning = 0;
$allCost = $materialCostPlanning + $toolsCostPlanning;
return $allCost;
}
public function getPercentagePerDay(Request $request) public function getPercentagePerDay(Request $request)
{ {
$dataPayload = $request->all(); $dataPayload = $request->all();
$allGantt = []; $allGantt = [];
foreach ($dataPayload['project_id'] as $val) { foreach ($dataPayload['project_id'] as $val) {
$allGantt[] = $this->getMaxVersionGantt($val); $allGantt[] = $this->getLatestGantt($val);
} }
$dataFinal=[]; $dataFinal=[];
@ -647,7 +567,7 @@ class ActivityController extends Controller
$allGantt = $dataPayload['gannt_id']; $allGantt = $dataPayload['gannt_id'];
}else{ }else{
foreach ($dataPayload['project_id'] as $val) { foreach ($dataPayload['project_id'] as $val) {
$allGantt[] = $this->getMaxVersionGantt($val); $allGantt[] = $this->getLatestGantt($val);
} }
} }
$dataFinal=[]; $dataFinal=[];
@ -841,7 +761,7 @@ class ActivityController extends Controller
$dataPayload = $request->all(); $dataPayload = $request->all();
$allGantt = []; $allGantt = [];
foreach ($dataPayload['project_id'] as $val) { foreach ($dataPayload['project_id'] as $val) {
$allGantt[] = $this->getMaxVersionGantt($val); $allGantt[] = $this->getLatestGantt($val);
} }
$dataFinal=[]; $dataFinal=[];
@ -954,13 +874,11 @@ class ActivityController extends Controller
} }
} }
private function getMaxVersionGantt($id){ private function getLatestGantt($id){
$maxGanttId = VersionGantt::where("proyek_id", $id)->max("id"); return array(
$data = array( "last_gantt_id" => VersionGantt::where("proyek_id", $id)->latest(),
"last_gantt_id" => $maxGanttId,
"proyek_id" => $id "proyek_id" => $id
); );
return $data;
} }
public function setBaseline($gantt_id) public function setBaseline($gantt_id)
@ -968,14 +886,10 @@ class ActivityController extends Controller
$activities = Activity::where("version_gantt_id", $gantt_id)->get(); $activities = Activity::where("version_gantt_id", $gantt_id)->get();
foreach ($activities as $activity) { foreach ($activities as $activity) {
$planned_start = $activity->start_date; $activity->update([
$planned_end = $activity->end_date; "planned_start"=>$activity->start_date,
$willUpdated = Activity::find($activity->id); "planned_end"=>$activity->end_date,
$dataUpdate = array( ]);
"planned_start"=>$planned_start,
"planned_end"=>$planned_end
);
$willUpdated->update($dataUpdate);
} }
return response()->json(['status'=>'success','message'=>'Set baseline success!','code'=> 200], 200); return response()->json(['status'=>'success','message'=>'Set baseline success!','code'=> 200], 200);
@ -999,7 +913,7 @@ class ActivityController extends Controller
); );
} }
} }
elseif ($countReports > 1) { if ($countReports > 1) {
$firstReport = ReportActivity::where('activity_id', $activity_id)->orderBy('report_date')->first(); $firstReport = ReportActivity::where('activity_id', $activity_id)->orderBy('report_date')->first();
$lastReport = ReportActivity::where('activity_id', $activity_id)->orderByDesc('report_date')->first(); $lastReport = ReportActivity::where('activity_id', $activity_id)->orderByDesc('report_date')->first();
$reports[] = array( $reports[] = array(

Loading…
Cancel
Save