@ -11,13 +11,19 @@ use App\Models\VersionGantt;
use App\Models\UserToActivity;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
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);
$gantt = VersionGantt::find($id);
if(Activity::where('proyek_id', $proyek_id)->where("version_gantt_id", $id)->count() == 0) {
if(!$gantt->hierarchy_ftth_id) {
$this->cloneTemplate($id, $proyek_id);
} else {
$this->cloneTemplate($id, $proyek_id, $gantt->hierarchy_ftth_id);
}
}
$dataGantt = $this->getDataActivity($id);
@ -40,9 +46,9 @@ class ActivityController extends Controller
$dataHeader->type = "header";
$dataHeader->text = $dataHeader->name;
$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('sortorder ', 'asc')->get();
}else{
$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('sortorder ', 'asc')->get();
}
foreach($data as $objRow){
@ -95,7 +101,7 @@ class ActivityController extends Controller
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();
$data = Activity::where('version_gantt_id', $gantt_id)->where('parent_id', $parent_id)->orderBy('sortorder ', 'asc')->get();
foreach($data as $objRow){
$objRow->parent = $parent_id;
$objRow->text = $objRow->name;
@ -120,21 +126,35 @@ class ActivityController extends Controller
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'=> 'project',
'created_by'=>$this->currentName,
]);
private function cloneTemplate($id, $proyek_id, $hierarchy_ftth_id = null) {
$project = Project::find($proyek_id);
if($hierarchy_ftth_id){
$gantt = VersionGantt::find($id);
$rootActivity = Activity::create([
'version_gantt_id'=>$id,
'proyek_id'=>$proyek_id,
'name'=> $gantt->name_version,
'start_date'=> $project->mulai_proyek,
'end_date'=> $project->akhir_proyek,
'rencana_biaya'=> $project->rencana_biaya,
'type_activity'=> 'project',
'created_by'=>$this->currentName,
'sortorder'=>1
]);
} else {
$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'=> 'project',
'created_by'=>$this->currentName,
'sortorder'=>1
]);
}
$resultTypeProject = TemplateGantt::where('proyek_type_id', $project->type_proyek_id)
->whereNull('parent_id')
@ -143,6 +163,7 @@ class ActivityController extends Controller
foreach($resultTypeProject as $objRow){
$childActivities = TemplateGantt::where("parent_id", $objRow->id)->count();
$max = Activity::where('version_gantt_id', $id)->max('sortorder');
$resultNew = Activity::create([
'type_activity'=> $childActivities > 0 ? "project" : "task",
'version_gantt_id'=>$id,
@ -151,7 +172,8 @@ class ActivityController extends Controller
'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
'created_by'=>$this->currentName,
'sortorder'=>$max+1
]);
$this->getChildrenTemplate($id, $objRow->id, $project->type_project_id, $proyek_id, $resultNew->id, $project->mulai_proyek);
}
@ -162,6 +184,7 @@ class ActivityController extends Controller
$data = TemplateGantt::where('parent_id', $parent_id)->orderByRaw('id ASC')->get();
foreach($data as $objRow){
$childActivities = TemplateGantt::where("parent_id", $objRow->id)->count();
$max = Activity::where('version_gantt_id', $id)->max('sortorder');
$resultNew = Activity::create([
'type_activity'=> $childActivities > 0 ? "project" : "task",
'version_gantt_id'=>$id,
@ -170,7 +193,8 @@ class ActivityController extends Controller
'name'=> $objRow->name_activity,
'start_date'=>$firstDay,
'end_date'=>$firstDay,
'created_by'=>$this->currentName
'created_by'=>$this->currentName,
'sortorder'=>$max+1
]);
$this->getChildrenTemplate($id, $objRow->id, $type_proyek_id, $proyek_id, $resultNew->id, $firstDay);
}
@ -186,6 +210,8 @@ class ActivityController extends Controller
$data['name'] = $request->text;
$data['persentase_progress'] = $request->progress;
$data['created_by'] = $this->currentName;
$max = Activity::where('version_gantt_id', $request->version_gantt_id)->max('sortorder');
$data['sortorder'] = $max + 1;
$data['type_activity'] = "task";
$parent = $data['parent_id'] ?? null;
@ -227,8 +253,13 @@ class ActivityController extends Controller
$dataUpdate['name'] = $request->text;
$dataUpdate['persentase_progress'] = $request->progress*100;
$dataUpdate['updated_by'] = $this->currentName;
unset($dataUpdate['sortorder']);
if($data->type_activity!='header')
$dataUpdate['type_activity'] = $request->type;
if($request->has("target")){
$this->updateOrder($id, $request->target);
}
if(!$data->update($dataUpdate))
return response()->json(['status'=>'failed', 'action'=>'error','message'=>'data activity failed updated!','code'=>400], 400);
@ -236,6 +267,29 @@ class ActivityController extends Controller
return response()->json(['status'=>'success','update_bobot'=> $updateBobot, 'data'=>$dataUpdate, 'action'=>'updated','message'=>'Activity updated!','code'=>200], 200);
}
private function updateOrder($taskId, $target){
$nextTask = false;
$targetId = $target;
if(strpos($target, "next:") === 0){
$targetId = substr($target, strlen("next:"));
$nextTask = true;
}
if($targetId == "null")
return;
$targetOrder = Activity::find($targetId)->sortorder;
if($nextTask)
$targetOrder++;
Activity::where("sortorder", ">=", $targetOrder)->increment("sortorder");
$updatedTask = Activity::find($taskId);
$updatedTask->sortorder = $targetOrder;
$updatedTask->save();
}
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);
@ -295,6 +349,51 @@ class ActivityController extends Controller
$activityStack = [];
$hasWeight = false;
foreach ($data['activities'] as $key => $value) {
if(isset($value['weight']) & & $value['weight'] != null & & $value['weight'] != 0){
$hasWeight = true;
break;
}
}
if(!$hasWeight){
foreach ($data['activities'] as $key => $value) {
if($key == 0){
$data['activities'][$key]['weight'] = 100;
} else {
$parentWeight = 0;
$siblingsCount = 1;
$i = $key;
while($i > 0){
if ($data['activities'][$i - 1]['level'] == $data['activities'][$key]['level']-1){
$parentWeight = $data['activities'][$i - 1]['weight'];
break;
}
if ($data['activities'][$key]['level'] == $data['activities'][$i]['level']){
$siblingsCount++;
}
$i--;
}
$i = $key+1;
while($i < count ( $ data [ ' activities ' ] ) ) {
if ($data['activities'][$i]['level'] == $data['activities'][$key]['level']-1){
break;
}
// Log::info('level '.$data['activities'][$key]['level'].' i level '.$data['activities'][$i]['level']);
if ($data['activities'][$key]['level'] == $data['activities'][$i]['level']){
$siblingsCount++;
}
$i++;
}
$data['activities'][$key]['weight'] = $parentWeight / $siblingsCount;
}
};
}
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']);
@ -309,7 +408,8 @@ class ActivityController extends Controller
$input['bobot_planning'] = $activity_row['weight'];
$input['persentase_progress'] = 0;
$input['type_activity'] = $i == 0 ? "header" : "task";
$input['created_by'] = $this->currentName;
$input['created_by'] = $this->currentName;
$input['sortorder'] = $activity_row['no'];
if (!$activity = Activity::create($input)) {
Activity::where('version_gantt_id', $data['ganttId'])->delete();