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.

225 lines
10 KiB

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Project;
use App\Models\Activity;
use App\Models\ReportActivity;
use App\Models\Role;
class DashboardController extends Controller
{
public function costPlanningActual(Request $request)
{
$dataPayload = $request->all();
$dataMaxVersionGantt = [];
foreach ($dataPayload['project_id'] as $val) {
$dataMaxVersionGantt[] = $this->getMaxVersionGantt($val);
}
$dataFinal=[];
foreach ($dataMaxVersionGantt as $val) {
$data = Activity::select('m_activity.id', 'm_activity.proyek_id', 'm_activity.name', 'm_activity.kode_sortname', 'm_activity.rencana_biaya', 'm_activity.biaya_actual', 'mp.nama as proyek_name')->join("m_proyek as mp", "m_activity.proyek_id", "=", "mp.id")->where('proyek_id', $val['proyek_id'])->where('version_gantt_id', $val['last_version_gantt'])->whereNull('parent_id')->orderBy('id', 'asc')->get();
$dataFinal[] = array("proyek_id" => $val['proyek_id'], "proyek_name" => isset($data) && count($data) > 0 ? $data[0]['proyek_name'] : "-", "version_gantt_id" => $val['last_version_gantt'], "data" => $data);
}
if(!$dataFinal)
return response()->json(['status'=>'failed','message'=>'Data not found','code'=> 404], 404);
return response()->json(['status'=>'success','code'=>200,'data'=>$dataFinal, 'totalRecord'=>1], 200);
}
public function getMaxVersionGantt($id){
$data = Activity::select(Activity::raw('MAX(version_gantt_id) as last_version_gantt'))->where("proyek_id", $id)->first();
return array("last_version_gantt"=>$data->last_version_gantt, "proyek_id" => $id, "proyek_name" => $data->proyek_name);
}
public function percentagePlanningActual(Request $request)
{
$dataPayload = $request->all();
$dataMaxVersionGantt = [];
foreach ($dataPayload['project_id'] as $val) {
$dataMaxVersionGantt[] = $this->getMaxVersionGantt($val);
}
$dataFinal=[];
foreach ($dataMaxVersionGantt as $val) {
$data = Activity::select('m_activity.id', 'm_activity.proyek_id', 'm_activity.name', 'm_activity.kode_sortname', 'm_activity.persentase_progress', 'm_activity.persentase_bobot', 'm_activity.bobot_planning', 'mp.nama as proyek_name')->join("m_proyek as mp", "m_activity.proyek_id", "=", "mp.id")->where('proyek_id', $val['proyek_id'])->where('version_gantt_id', $val['last_version_gantt'])->whereNull('parent_id')->orderBy('id', 'asc')->get();
$dataFinal[] = array("proyek_id" => $val['proyek_id'], "proyek_name" => isset($data) && count($data) > 0 ? $data[0]['proyek_name'] : "-", "version_gantt_id" => $val['last_version_gantt'], "data" => $data);
}
if(!$dataFinal)
return response()->json(['status'=>'failed','message'=>'failed get list role, please try again later!','code'=>400], 400);
return response()->json(['status'=>'success','code'=>200,'data'=>$dataFinal, 'totalRecord'=>1], 200);
}
public function list()
{
$data = Role::all();
$countData = $data->count();
if(!$data)
return response()->json(['status'=>'failed','message'=>'failed get list role, please try again later!','code'=>400], 400);
return response()->json(['status'=>'success','code'=>200,'data'=>$data, 'totalRecord'=>$countData], 200);
}
public function getActivityByProjectId($projectId) {
$output = [];
$data = Activity::select('id')->where('proyek_id', $projectId)->get();
foreach ($data as $actData) {
$output[] = $actData->id;
}
return $output;
}
/*
const statusProyek = [
{
"id": "1",
"proyek_name": "Pembuatan Aplikasi Survey Covid Varian Baru", // ambil dari m_proyek
"total_task": 80, // ambil dari m_activity -> SUM(jumlah_pekerjaan) where proyek_id berdasarkan proyek_id terpilih
"task_on_progress": 60, // ambil dari report_activity -> SUM(job_count_report) where activity_id berdasarkan activity_id terpilih
"day_left": 7, // ambil dari m_proyek, cari sisa hari (end_date - hari ini)
"percentage": 75 // (task_on_progress / total_task * 100)
},
{
"id": "2",
"proyek_name": "Pembangunan Jembatan Layang",
"total_task": 70, // SELECT SUM(jumlah_pekerjaan) FROM public.m_activity WHERE proyek_id IN (11);
"task_on_progress": 70, // SELECT SUM(job_count_report) FROM public.report_activity WHERE activity_id IN (349, 355);
"day_left": 0,
"percentage": 100
},
...
]
*/
public function statusProyek(Request $request) {
$dataPayload = $request->all();
$reqProject = $dataFinal = [];
if (!isset($dataPayload['project_id']))
return response()->json(['status'=>'failed','message'=>'Undefined project_id','code'=>400], 400);
foreach ($dataPayload['project_id'] as $val) {
$prj["project_id"] = $val;
$prj["activity_id"] = $this->getActivityByProjectId($val);
$reqProject[] = $prj;
}
if (count($reqProject) > 0) {
$row = [];
for ($i=0; $i < count($reqProject); $i++) {
$dataMaxVersionGantt = Activity::select(Activity::raw('MAX(version_gantt_id) as last_version_gantt'))->where("proyek_id", $reqProject[$i]['project_id'])->first();
$last_version_gantt = $dataMaxVersionGantt->last_version_gantt;
$queryProject = Project::select('id', 'nama', 'mulai_proyek', 'akhir_proyek')->where('id',$reqProject[$i]['project_id'])->first();
$sum_jumlah_pekerjaan = Activity::where('proyek_id', $reqProject[$i]['project_id'])->where('version_gantt_id', $last_version_gantt)->sum("jumlah_pekerjaan");
$count_activity = Activity::where('proyek_id', $reqProject[$i]['project_id'])->where('version_gantt_id', $last_version_gantt)->count("id");
$count_activity_done = Activity::where('proyek_id', $reqProject[$i]['project_id'])->where('persentase_progress', '100')->where('version_gantt_id', $last_version_gantt)->count("id");
$count_progress_percentage = Activity::where('proyek_id', $reqProject[$i]['project_id'])->whereNull('parent_id')->where('version_gantt_id', $last_version_gantt)->count("persentase_progress");
$progress_actual = Activity::select('id', 'bobot_planning', 'persentase_progress')->where('proyek_id', $reqProject[$i]['project_id'])->whereNull('parent_id')->where('version_gantt_id', $last_version_gantt)->get();
$current_progress = 0;
foreach($progress_actual as $objRow) {
$current_progress += ($objRow->bobot_planning * $objRow->persentase_progress) / 100;
}
if (count($reqProject[$i]['activity_id']) > 0) {
for ($j=0; $j < count($reqProject[$i]['activity_id']); $j++) {
$sum_job_count_report = ReportActivity::whereIn('activity_id', $reqProject[$i]['activity_id'])->sum("job_count_report");
}
}
if (!isset($queryProject->id))
return response()->json(['status'=>'failed','message'=>'Project with id='.$reqProject[$i]['project_id'].' is not found!','code'=>400], 400);
$now = time();
$akhir_proyek = strtotime($queryProject->akhir_proyek);
$datediff = $akhir_proyek - $now;
$mulai_proyek = strtotime($queryProject->mulai_proyek);
$total_kontrak = $akhir_proyek - $mulai_proyek;
$row["id"] = $queryProject->id;
$row["proyek_name"] = $queryProject->nama;
$row["mulai_proyek"] = $queryProject->mulai_proyek;
$row["akhir_proyek"] = $queryProject->akhir_proyek;
$row["total_kontrak"] = round($total_kontrak / (60 * 60 * 24));
$row["total_task"] = $sum_jumlah_pekerjaan;
$row["task_on_progress"] = $sum_job_count_report;
$row["count_activity"] = $count_activity;
$row["count_activity_done"] = $count_activity_done;
$row["day_left"] = round($datediff / (60 * 60 * 24));
$row["percentage"] = round($current_progress,3);
$row["count_progress_percentage"] = $count_progress_percentage;
$dataFinal[] = $row;
}
}
if(empty($dataFinal))
return response()->json(['status'=>'failed','message'=>'failed get status proyek, please try again later!','code'=>400], 400);
return response()->json(['status'=>'success','code'=>200,'data'=>$dataFinal, 'totalRecord'=>count($dataFinal)], 200);
}
public function curvaS(Request $request)
{
$dataPayload = $request->all();
$dataMaxVersionGantt = [];
$dataFinal=[];
foreach ($dataPayload['project_id'] as $val) {
$dataMaxVersionGantt[] = $this->getMaxVersionGantt($val);
}
foreach ($dataMaxVersionGantt as $val) {
$data = Activity::select('m_activity.id', 'm_activity.proyek_id', 'm_activity.name', 'm_activity.kode_sortname', 'm_activity.persentase_progress', 'm_activity.persentase_bobot', 'm_activity.bobot_planning', 'm_activity.biaya_actual', 'mp.nama as proyek_name')->join("m_proyek as mp", "m_activity.proyek_id", "=", "mp.id")->where('proyek_id', $val['proyek_id'])->where('version_gantt_id', $val['last_version_gantt'])->whereNull('parent_id')->orderBy('id', 'asc')->get();
$totalCostPlanning = Activity::where('proyek_id', $val['proyek_id'])->where('version_gantt_id', $val['last_version_gantt'])->whereNull('parent_id')->sum('rencana_biaya');
for ($i=0; $i < count($data); $i++) {
$tmpp = $tmpa = $tmpbc = $tmpac = 0;
for ($x=$i; $x >= 0 ; $x--) {
$tmpp += $data[$x]['bobot_planning'];
$tmpa += ($data[$x]['persentase_progress'] * $data[$x]['bobot_planning'])/100;
$tmpbc += ((($data[$x]['persentase_progress'] * $data[$x]['bobot_planning'])/100)*$totalCostPlanning)/100;
$tmpac += $data[$x]['biaya_actual'];
}
$data[$i]['cal_bobot_p'] = round($tmpp,2);
if($i > 0 ){
if($data[$i-1]['cal_bobot_a'] != null){
if($data[$i-1]['cal_bobot_a'] == round($tmpa,2)){
$data[$i]['cal_bobot_a'] = null;
}else{
$data[$i]['cal_bobot_a'] = round($tmpa,2);
}
}else{
$data[$i]['cal_bobot_a'] = null;
}
if($data[$i-1]['cal_actual_cost'] != null){
if($data[$i-1]['cal_actual_cost'] == round($tmpac,2)){
$data[$i]['cal_actual_cost'] = null;
}else{
$data[$i]['cal_actual_cost'] = round($tmpac,2);
}
}else{
$data[$i]['cal_actual_cost'] = null;
}
}else{
$data[$i]['cal_bobot_a'] = round($tmpa,2);
$data[$i]['cal_actual_cost'] = round($tmpac,2);
}
$data[$i]['cal_budget_cost'] = round($tmpbc,2);
}
$dataFinal[] = array("proyek_id" => $val['proyek_id'], "proyek_name" => isset($data) && count($data) > 0 ? $data[0]['proyek_name'] : "-", "version_gantt_id" => $val['last_version_gantt'], "data" => $data);
}
if(!$dataFinal)
return response()->json(['status'=>'failed','message'=>'failed get list role, please try again later!','code'=>400], 400);
return response()->json(['status'=>'success','code'=>200, 'data'=>$dataFinal, 'totalRecord'=>1], 200);
}
}