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.

297 lines
15 KiB

3 years ago
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Project;
use App\Models\Activity;
use App\Models\ReportActivity;
use App\Models\UserToActivity;
use DB;
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($dataPayload){
return response()->json(['status'=>'success','code'=>200,'data'=>$dataFinal, 'totalRecord'=>1], 200);
}else{
return response()->json(['status'=>'failed','message'=>'failed get list role, please try again later!','code'=>400], 400);
}
}
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);
}
// SELECT * FROM public.m_activity where proyek_id = 11 and version_gantt_id=12 and parent_id IS NULL;
$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();
// loop
// hitung persentase_progress = persentase_progress*bobot_planning/100
$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($dataPayload){
return response()->json(['status'=>'success','code'=>200,'data'=>$dataFinal, 'totalRecord'=>1], 200);
}else{
return response()->json(['status'=>'failed','message'=>'failed get list role, please try again later!','code'=>400], 400);
}
}
public function list()
{
$data = Role::all();
$countData = $data->count();
if($data){
return response()->json(['status'=>'success','code'=>200,'data'=>$data, 'totalRecord'=>$countData], 200);
}else{
return response()->json(['status'=>'failed','message'=>'failed get list role, please try again later!','code'=>400], 400);
}
}
// // cari titik last waypoint dari user di masing-masing project terpilih dari front end
// public function searchUserPresencePoints(Request $request) {
// $dataPayload = $request->all();
// $reqActivityId = [];
// foreach ($dataPayload['project_id'] as $val) {
// $reqActivityId[] = $this->getActivityByProjectId($val);
// }
// // var_dump(count($reqActivityId));
// $activity_id = [];
// $getPoints = false;
// if (count($reqActivityId) > 0) {
// for ($i=0; $i < count($reqActivityId); $i++) {
// if (count($reqActivityId[$i]) > 0) {
// foreach($reqActivityId[$i] as $reqAct) {
// array_push($activity_id, $reqAct->id);
// }
// }
// }
// }
// if (count($activity_id) > 0) {
// // $getPoints = ReportActivity::select('report_activity.*', 'm_activity.name as activity_name', 'm_proyek.nama as proyek_name')
// // ->leftJoin('m_activity', 'm_activity.id', '=', 'report_activity.activity_id')
// // ->leftJoin('m_proyek', 'm_proyek.id', '=', 'm_activity.proyek_id')
// // ->whereIn('report_activity.activity_id', $activity_id)->get();
// // $getPoints =
// }
// if($getPoints){
// return response()->json(['status'=>'success','code'=>200,'data'=>$getPoints, 'totalRecord'=>count($getPoints)], 200);
// }else{
// return response()->json(['status'=>'failed','message'=>'failed get list points, please try again later!','code'=>400], 400);
// }
// }
public function getActivityByProjectId($projectId) {
$output = [];
$data = Activity::select('id')->where('proyek_id', $projectId)->get();
if (count($data) > 0) {
foreach ($data as $actData) {
$output[] = $actData->id;
}
}
return $output;
}
/* for statusProyek, I want something output like this
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");
$progress_percentage = Activity::where('proyek_id', $reqProject[$i]['project_id'])->whereNull('parent_id')->where('version_gantt_id', $last_version_gantt)->sum("persentase_progress");
$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 = bobot_planning * 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) {
// $total_current = ($objRow->bobot_planning * $objRow->persentase_progress) / 100;
$current_progress += ($objRow->bobot_planning * $objRow->persentase_progress) / 100;
}
// yang sudah difilter dengan max_version_gantt
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);
}
// sisa hari
$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($dataFinal){
return response()->json(['status'=>'success','code'=>200,'data'=>$dataFinal, 'totalRecord'=>count($dataFinal)], 200);
}else{
return response()->json(['status'=>'failed','message'=>'failed get status proyek, please try again later!','code'=>400], 400);
}
}
public function curvaS(Request $request)
{
$dataPayload = $request->all();
$dataMaxVersionGantt = [];
foreach ($dataPayload['project_id'] as $val) {
$dataMaxVersionGantt[] = $this->getMaxVersionGantt($val);
}
// SELECT * FROM public.m_activity where proyek_id = 11 and version_gantt_id=12 and parent_id IS NULL;
$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', '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');
// loop
$dataBobotPlan =[];
for ($i=0; $i < count($data); $i++) {
// if($i)
$tmpp = 0;
$tmpa = 0;
$tmpbc = 0;
$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'];
}
// $dataBobotPlan[] = $tmp;
$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;
}
}else{
$data[$i]['cal_bobot_a'] = round($tmpa,2);
}
$data[$i]['cal_budget_cost'] = round($tmpbc,2);
// $data[$i]['cal_actual_cost']=round($tmpac,2);
if($i > 0 ){
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_actual_cost'] = round($tmpac,2);
}
}
// hitung persentase_progress = persentase_progress*bobot_planning/100
$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($dataPayload){
return response()->json(['status'=>'success','code'=>200, 'data'=>$dataFinal, 'totalRecord'=>1], 200);
}else{
return response()->json(['status'=>'failed','message'=>'failed get list role, please try again later!','code'=>400], 400);
}
}
}