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.
474 lines
16 KiB
474 lines
16 KiB
<?php |
|
|
|
namespace App\Http\Controllers; |
|
|
|
use App\Models\Activity; |
|
use App\Models\AssignMaterial; |
|
use App\Models\Divisi; |
|
use App\Models\Project; |
|
use App\Models\ProjectPhase; |
|
use App\Models\ReportActivityMaterial; |
|
use App\Models\User; |
|
use App\Models\UserToVersionGantt; |
|
use App\Models\VersionGantt; |
|
use Illuminate\Support\Collection; |
|
use Illuminate\Support\Facades\DB; |
|
|
|
class DashboardBoDController extends Controller |
|
{ |
|
private function interpolateYear($year){ |
|
if($year) |
|
$year = '%'.$year.'%'; |
|
return $year; |
|
} |
|
|
|
private function curlReq($url, $token){ |
|
$ch = curl_init(); |
|
$headers = [ |
|
'Authorization: '.$token |
|
]; |
|
curl_setopt($ch, CURLOPT_URL, $url); |
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
|
|
$response = curl_exec($ch); |
|
if ($response === false) |
|
$response = curl_error($ch); |
|
curl_close($ch); |
|
|
|
return json_decode($response); |
|
} |
|
|
|
private function getInvoiceIntegration($search) { |
|
if(empty($search)) |
|
return response()->json(['status'=>'error', 'message'=>'Empty query string!'], 400); |
|
|
|
$url = str_replace("SEARCH", $search, config('api.adw').'/project_cost?project_no=SEARCH'); |
|
$token = config('api.adw_token'); |
|
$response = $this->curlReq($url, $token); |
|
|
|
if(@$response->data->project_no == "") |
|
return null; |
|
|
|
return $response; |
|
} |
|
|
|
// to do |
|
public function getCompanyCashFlow($year = '%') { |
|
$year = $this->interpolateYear($year); |
|
$totalExpenditure = $totalInvoice = $totalPaidInvoice = 0; |
|
|
|
// we can't use eloquent's sum() method because someone decided to use varchar as datatype in rencana_biaya field |
|
$totalBudgets = Project::select(DB::raw('SUM(CAST("rencana_biaya" AS DOUBLE PRECISION))')) |
|
->where('mulai_proyek', 'like', $year) |
|
/* ->orWhere('akhir_proyek', 'like', $year) */ |
|
->pluck('sum') |
|
->first(); |
|
|
|
$projects = Project::where('mulai_proyek', 'like', $year) |
|
/* ->orWhere('akhir_proyek', 'like', $year) */ |
|
->get(); |
|
foreach($projects as $project){ |
|
$project->expenses = 0; |
|
|
|
// expenditure |
|
$assignedMaterials = AssignMaterial::where('proyek_id', $project->id)->get(); |
|
foreach($assignedMaterials as $assignedMaterial) { |
|
$reportedMaterials = ReportActivityMaterial::where('assign_material_id', $assignedMaterial->id)->get(); |
|
foreach($reportedMaterials as $reportedMaterial) { |
|
$project->expenses += $reportedMaterial->qty * $assignedMaterial->budget; |
|
} |
|
} |
|
$totalExpenditure += $project->expenses; |
|
|
|
$resp = null; |
|
if($project->kode_sortname != ""){ |
|
$resp = $this->getInvoiceIntegration($project->kode_sortname); |
|
/* $resp = $project->kode_sortname; */ |
|
$totalInvoice += $resp->data->total_invoice_amount ?? 0; |
|
$totalPaidInvoice += $resp->data->total_invoice_paid_amount ?? 0; |
|
} |
|
|
|
} |
|
|
|
return response()->json([ |
|
'data' => [ |
|
'total_budget' => (int) $totalBudgets ?? 0, |
|
'total_expenditure' => $totalExpenditure, |
|
'total_invoice' => $totalInvoice, |
|
'total_paid_invoice' => $totalPaidInvoice , |
|
] |
|
], 200); |
|
} |
|
|
|
public function getInvoiceOutstanding($year = '%'){ |
|
$year = $this->interpolateYear($year); |
|
$projects = Project::where('mulai_proyek', 'like', $year) |
|
/* ->orWhere('akhir_proyek', 'like', $year) */ |
|
->get(); |
|
$return = []; |
|
foreach($projects as $project){ |
|
$resp = null; |
|
if($project->kode_sortname != ""){ |
|
$resp = $this->getInvoiceIntegration($project->kode_sortname); |
|
array_push($return, [ |
|
'project' => $project->nama, |
|
'invoiced' => $resp->data->total_invoice_amount ?? 0, |
|
'paid' => $resp->data->total_invoice_paid_amount ?? 0, |
|
'response' => $resp, |
|
]); |
|
} |
|
} |
|
|
|
return response()->json([ |
|
'data' => $return |
|
], 200); |
|
} |
|
|
|
public function getTotalProjectPerScheduleHealth($year = '%'){ |
|
$year = $this->interpolateYear($year); |
|
|
|
$return = [ |
|
'behind-schedule' => 0, |
|
'warning' => 0, |
|
'on-schedule' => 0, |
|
]; |
|
|
|
$projects = Project::where('mulai_proyek', 'like', $year)->get(); |
|
foreach($projects as $project) { |
|
$project->scurve = $this->getSCurve($project->id); |
|
if(@$project->scurve['difference'] > 0 && @$project->scurve['difference'] <= 5) |
|
$return['warning'] += 1; |
|
elseif(@$project->scurve['difference'] > 5 && @$project->scurve['difference'] <= 100) |
|
$return['behind-schedule'] += 1; |
|
elseif(@$project->scurve['difference'] == 0) |
|
$return['on-schedule'] += 1; |
|
} |
|
|
|
return response()->json(['data' => $return], 200); |
|
} |
|
|
|
public function getTotalProjectScheduleHealthPerDivision($year = '%'){ |
|
$year = $this->interpolateYear($year); |
|
|
|
$divisions = Divisi::whereNull('parent')->get(); |
|
foreach($divisions as $division){ |
|
|
|
$scheduleData = new Collection(); |
|
$behindSchedule = $warning = $onSchedule = 0; |
|
|
|
$projects = Project::where('mulai_proyek', 'like', $year)->where('divisi_id', $division->id)->get(); |
|
foreach($projects as $project) { |
|
$project->scurve = $this->getSCurve($project->id); |
|
if(@$project->scurve['difference'] > 0 && @$project->scurve['difference'] <= 5) |
|
$warning++; |
|
elseif(@$project->scurve['difference'] > 5 && @$project->scurve['difference'] <= 100) |
|
$behindSchedule++; |
|
elseif(@$project->scurve['difference'] == 0) |
|
$onSchedule++; |
|
} |
|
|
|
$scheduleData->prepend($behindSchedule, 'behindSchedule'); |
|
$scheduleData->prepend($warning, 'warning'); |
|
$scheduleData->prepend($onSchedule, 'onSchedule'); |
|
$division->scheduleData = $scheduleData; |
|
} |
|
return response()->json([ |
|
'data' => [ |
|
$divisions |
|
] |
|
], 200); |
|
} |
|
|
|
public function getTotalProjectPerBudgetHealth($year = '%'){ |
|
$year = $this->interpolateYear($year); |
|
return response()->json([ |
|
'data' => [ |
|
'overrun' => Project::where('mulai_proyek', 'like', $year)->where('budget_health', 'overrun')->count(), |
|
'warning' => Project::where('mulai_proyek', 'like', $year)->where('budget_health', 'warning')->count(), |
|
'on-budget' => Project::where('mulai_proyek', 'like', $year)->where('budget_health', 'on-budget')->count(), |
|
] |
|
], 200); |
|
} |
|
|
|
private function countTotalProjectByBudgetHealthInDivision($divisi, $year, $health){ |
|
return Project::where('divisi_id', $divisi) |
|
->where('mulai_proyek', 'like', $year) |
|
/* ->orWhere('akhir_proyek', 'like', $year) */ |
|
->where('budget_health', $health) |
|
->count(); |
|
} |
|
|
|
|
|
public function getTotalProjectBudgetHealthPerDivision($year = '%'){ |
|
$year = $this->interpolateYear($year); |
|
$divisions = Divisi::select('id','name') |
|
->with('children') |
|
->whereNull('parent') |
|
->get(); |
|
// to do : count in more than 1 level child |
|
foreach($divisions as $division){ |
|
$budgetData = new Collection(); |
|
$budgetData->prepend($this->countTotalProjectByBudgetHealthInDivision($division->id, $year, 'overrun'), 'overrun'); |
|
$budgetData->prepend($this->countTotalProjectByBudgetHealthInDivision($division->id, $year, 'warning'), 'warning'); |
|
$budgetData->prepend($this->countTotalProjectByBudgetHealthInDivision($division->id, $year, 'on-budget'), 'on-budget'); |
|
foreach($division->children as $d){ |
|
$budgetData['overrun'] += $this->countTotalProjectByBudgetHealthInDivision($d->id, $year, 'overrun'); |
|
$budgetData['warning'] += $this->countTotalProjectByBudgetHealthInDivision($d->id, $year, 'warning'); |
|
$budgetData['on-budget'] += $this->countTotalProjectByBudgetHealthInDivision($d->id, $year, 'on-budget'); |
|
} |
|
unset($division->children); |
|
$division->budgetData = $budgetData; |
|
} |
|
foreach($divisions as $division){ |
|
} |
|
return response()->json([ |
|
'data' => [ |
|
$divisions |
|
] |
|
], 200); |
|
} |
|
|
|
public function getTotalProjectPerPhase($year = '%'){ |
|
$year = $this->interpolateYear($year); |
|
$projectPhases = ProjectPhase::orderBy('order')->get(); |
|
foreach($projectPhases as $phase){ |
|
$phase->totalProject = Project::where('phase_id', $phase->id) |
|
->where('mulai_proyek', 'like', $year) |
|
/* ->orWhere('akhir_proyek', 'like', $year) */ |
|
->count(); |
|
} |
|
return response()->json([ |
|
'data' => [ |
|
$projectPhases |
|
] |
|
], 200); |
|
} |
|
|
|
private function countTotalProjectInDivision($id, $year){ |
|
return Project::where('divisi_id', $id) |
|
->where('mulai_proyek', 'like', $year) |
|
/* ->orWhere('akhir_proyek', 'like', $year) */ |
|
->count(); |
|
} |
|
|
|
public function getTotalProjectPerDivision($year = '%') { |
|
$year = $this->interpolateYear($year); |
|
|
|
$divisions = Divisi::select('id','name') |
|
->with('children') |
|
->whereNull('parent') |
|
->get(); |
|
|
|
// to do : count in more than 1 level child |
|
foreach($divisions as $v){ |
|
$v->total = $this->countTotalProjectInDivision($v->id, $year); |
|
foreach($v->children as $d){ |
|
$v->total += $this->countTotalProjectInDivision($d->id, $year); |
|
} |
|
unset($v->children); |
|
} |
|
|
|
return response()->json([ |
|
'data' => $divisions |
|
], 200); |
|
} |
|
|
|
private function countTotalProjectValueInDivision($id, $year){ |
|
return Project::select(DB::raw('SUM(CAST("rencana_biaya" AS DOUBLE PRECISION))')) |
|
->where('mulai_proyek', 'like', $year) |
|
/* ->orWhere('akhir_proyek', 'like', $year) */ |
|
->where('divisi_id', $id) |
|
->pluck('sum') |
|
->first(); |
|
} |
|
|
|
public function getTotalProjectValuePerDivision($year = '%') { |
|
$year = $this->interpolateYear($year); |
|
|
|
$divisions = Divisi::select('id','name') |
|
->with('children') |
|
->whereNull('parent') |
|
->get(); |
|
|
|
// to do : count in more than 1 level child |
|
foreach($divisions as $v){ |
|
$v->total = $this->countTotalProjectValueInDivision($v->id, $year); |
|
foreach($v->children as $d){ |
|
$v->total += $this->countTotalProjectValueInDivision($d->id, $year); |
|
} |
|
unset($v->children); |
|
} |
|
|
|
return response()->json([ |
|
'data' => $divisions |
|
], 200); |
|
} |
|
|
|
private function getSCurve($project_id) |
|
{ |
|
DB::enableQueryLog(); |
|
|
|
$dataPayload = [ |
|
'project_id' => $project_id, |
|
'period' => 'week', |
|
]; |
|
$allGantt[] = $this->getLatestGantt($dataPayload['project_id']); |
|
$dataResponse=[]; |
|
|
|
foreach ($allGantt as $keyGantt) { |
|
$dataProject = Project::find($keyGantt['proyek_id']); |
|
|
|
$totalRencanaBudget = Activity::whereNull('parent_id')->where("proyek_id", $keyGantt['proyek_id'])->where("version_gantt_id", $keyGantt['last_gantt_id'])->sum("rencana_biaya"); |
|
|
|
if(!Activity::where("version_gantt_id", $keyGantt['last_gantt_id'])->first()) |
|
continue; |
|
|
|
$alreadyHasReport = DB::table('report_activity_material as a') |
|
->select('a.id') |
|
->join('m_activity as b', 'b.id', '=', 'a.activity_id') |
|
->where('b.version_gantt_id', '=', $keyGantt['last_gantt_id']) |
|
->exists(); |
|
|
|
if(!$alreadyHasReport) |
|
continue; |
|
|
|
$minDate = Activity::where('version_gantt_id', $keyGantt['last_gantt_id'])->min("planned_start"); |
|
|
|
$begin = new \DateTime($minDate.' Monday'); |
|
$maxDate = DB::table('assign_material_to_activity as ama') |
|
->where("ama.proyek_id", $keyGantt['proyek_id']) |
|
->join('m_activity as a', 'a.id', '=', 'ama.activity_id') |
|
->where('a.version_gantt_id', '=', $keyGantt['last_gantt_id']) |
|
->max("plan_date"); // plan date overlapped with assign_material_to_activity's, it should be m_activity's |
|
$end = new \DateTime($maxDate. ' Friday'); |
|
$interval = new \DateInterval('P7D'); |
|
$period = new \DatePeriod($begin, $interval, $end); |
|
|
|
$tempTtlPercentPlan=0; |
|
$tempTtlPercentActual=0; |
|
$tempPercentagePlan = []; |
|
$tempPercentageReal = []; |
|
|
|
foreach ($period as $dt) { |
|
$minSevenDays = new \Datetime($dt->format("Y-m-d")); |
|
$minSevenDays = $minSevenDays->modify('-7 day')->format("Y-m-d"); |
|
$dataPlanM = DB::table('assign_material_to_activity as ama') |
|
->select('ama.activity_id', 'ama.qty_planning', 'ama.plan_date', 'ama.start_activity', 'a.bobot_planning', 'a.biaya_actual', 'a.duration', 'a.persentase_progress') |
|
->join('m_activity as a', 'a.id', '=', 'ama.activity_id') |
|
->where('ama.proyek_id', '=', $keyGantt['proyek_id']) |
|
->where('a.version_gantt_id', '=', $keyGantt['last_gantt_id']) |
|
->whereDate('ama.plan_date', '<=',$dt->format("Y-m-d")) |
|
->whereDate('ama.plan_date', '>', $minSevenDays) |
|
->get(); |
|
$dataActualM = DB::table('report_activity_material as ram') |
|
->select('ram.activity_id', 'ram.qty', 'ram.report_date', 'a.bobot_planning', 'a.biaya_actual', 'a.duration', 'a.persentase_progress') |
|
->join('m_activity as a', 'a.id', '=', 'ram.activity_id') |
|
->where('a.version_gantt_id', '=', $keyGantt['last_gantt_id']) |
|
->where('a.proyek_id', '=', $keyGantt['proyek_id']) |
|
->whereDate('ram.report_date', '<=',$dt->format("Y-m-d")) |
|
->whereDate('ram.report_date', '>',$minSevenDays) |
|
->get(); |
|
$sumPercentagePlan=0; |
|
|
|
foreach ($dataPlanM as $keyPlanM) { |
|
$sumVolPlan = DB::table('assign_material_to_activity') |
|
->select('activity_id', DB::raw('SUM(qty_planning) as ttl_qty_plan')) |
|
->where('activity_id', '=', $keyPlanM->activity_id) |
|
->groupBy('activity_id') |
|
->first(); |
|
try { |
|
$sumPercentagePlan+=($keyPlanM->qty_planning/$sumVolPlan->ttl_qty_plan)*$keyPlanM->bobot_planning; |
|
} catch (\DivisionByZeroError $e) { |
|
return response()->json(['message' => $e->getMessage()]); |
|
} |
|
} |
|
|
|
$sumPercentageActual=0; |
|
foreach ($dataActualM as $keyActualM) { |
|
$sumVolActual = DB::table('assign_material_to_activity') |
|
->select('activity_id', DB::raw('SUM(qty_planning) as ttl_qty_plan')) |
|
->where('activity_id', '=', $keyActualM->activity_id) |
|
->groupBy('activity_id') |
|
->first(); |
|
try { |
|
$sumPercentageActual+=($keyActualM->qty/$sumVolActual->ttl_qty_plan)*$keyActualM->bobot_planning; |
|
} catch (\DivisionByZeroError $e) { |
|
return response()->json(['message' => $e->getMessage()]); |
|
} |
|
} |
|
|
|
$tempTtlPercentPlan+= $sumPercentagePlan; |
|
$tempTtlPercentActual+= $sumPercentageActual; |
|
|
|
if($tempTtlPercentPlan >= 100 || $tempTtlPercentActual >= 100){ |
|
if($tempTtlPercentActual >= 100) |
|
$tempTtlPercentActual = 100; |
|
if($tempTtlPercentPlan >= 100) |
|
$tempTtlPercentPlan = 100; |
|
} |
|
|
|
|
|
$tempPercentage[] = array(round($tempTtlPercentPlan,2), round($tempTtlPercentActual,2)); |
|
$tempPercentagePlan[] = round($tempTtlPercentPlan, 2); |
|
$tempPercentagePlanWhr[] = ["weekly period", $tempPercentagePlan]; |
|
$tempPercentageReal[] = round($tempTtlPercentActual, 2); |
|
if($tempTtlPercentPlan >= 100 && $tempTtlPercentActual >= 100){ |
|
break; |
|
} |
|
} |
|
|
|
$plannedPercentage = end($tempPercentagePlan); |
|
$progressPercentage = end($tempPercentageReal); |
|
$difference = $plannedPercentage - $progressPercentage; |
|
|
|
$dataResponse = array( |
|
'planned' => $plannedPercentage == false ? 0 : $plannedPercentage, |
|
'progress' => $progressPercentage == false ? 0 : $progressPercentage, |
|
'difference' => $difference, |
|
); |
|
|
|
|
|
} |
|
|
|
return $dataResponse; |
|
} |
|
|
|
private function getLatestGantt($id){ |
|
$maxGanttId = VersionGantt::where("proyek_id", $id)->max("id"); |
|
$data = array( |
|
"last_gantt_id" => $maxGanttId, |
|
"proyek_id" => $id |
|
); |
|
return $data; |
|
} |
|
|
|
public function getDetailExpenditure($year = '%'){ |
|
$year = $this->interpolateYear($year); |
|
$projects = Project::where('mulai_proyek', 'like', $year) |
|
/* ->orWhere('akhir_proyek', 'like', $year) */ |
|
->get(); |
|
foreach($projects as $project){ |
|
$lastGantt = $this->getLatestGantt($project->id); |
|
|
|
if($project->kode_sortname != ""){ |
|
$resp = $this->getInvoiceIntegration($project->kode_sortname); |
|
$project->invoice = [ |
|
'invoiced' => $resp->data->total_invoice_amount ?? 0, |
|
'paid' => $resp->data->total_invoice_paid_amount ?? 0, |
|
]; |
|
} |
|
|
|
$project->pm = User::find($project->pm_id); |
|
/* $project->header = Activity::where('proyek_id', $project->id)->where('version_gantt_id', $lastGantt['last_gantt_id'])->whereNull('parent_id')->first(); */ |
|
$project->scurve = $this->getSCurve($project->id); |
|
$project->manPowers = UserToVersionGantt::where('version_gantt_id', $lastGantt['last_gantt_id'])->count(); |
|
} |
|
|
|
return response()->json([ |
|
'data' => $projects, |
|
'total_manpowers' => User::count() |
|
], 200); |
|
} |
|
} |
|
|
|
|