From 3f193ec38ec78bd3d323b4aa304c5e1ab52425b6 Mon Sep 17 00:00:00 2001 From: Muhammad Sulaiman Yusuf Date: Tue, 20 Sep 2022 14:48:05 +0700 Subject: [PATCH] WIP: Dashboard BoD --- .../Controllers/DashboardBoDController.php | 170 ++++++++++++++++++ routes/web.php | 10 ++ 2 files changed, 180 insertions(+) create mode 100644 app/Http/Controllers/DashboardBoDController.php diff --git a/app/Http/Controllers/DashboardBoDController.php b/app/Http/Controllers/DashboardBoDController.php new file mode 100644 index 0000000..ec845e2 --- /dev/null +++ b/app/Http/Controllers/DashboardBoDController.php @@ -0,0 +1,170 @@ +interpolateYear($year); + + // 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) + ->pluck('sum') + ->first(); + + return response()->json([ + 'data' => [ + 'total_budget' => (int) $totalBudgets ?? rand(0,10), + 'total_expenditure' => rand(0,10), // to do integrasi + 'total_invoice' => rand(0,10), + 'total_paid_invoice' => rand(0,10), + ] + ], 200); + } + + public function getProjectPerScheduleHealth($year = '%'){ + $year = $this->interpolateYear($year); + // get data plan (vol) in % + // get data actual in % + return response()->json([ + 'data' => [ + 'behind-schedule' => rand(0,10), + 'warning' => rand(0,10), + 'on-schedule' => rand(0,10), + ] + ], 200); + } + + public function getProjectScheduleHealthPerDivision($year = '%'){ + $year = $this->interpolateYear($year); + + $divisions = Divisi::whereNull('parent')->get(); + foreach($divisions as $division){ + $scheduleData = new Collection(); + $scheduleData->prepend(rand(0, 10), 'behindSchedule'); + $scheduleData->prepend(rand(0, 10), 'warning'); + $scheduleData->prepend(rand(0, 10), 'onSchedule'); + $division->scheduleData = $scheduleData; + } + return response()->json([ + 'data' => [ + $divisions + ] + ], 200); + } + + public function getProjectPerBudgetHealth($year = '%'){ + $year = $this->interpolateYear($year); + return response()->json([ + 'data' => [ + 'overrun' => rand(0,10), + 'warning' => rand(0,10), + 'on-budget' => rand(0,10), + ] + ], 200); + } + + public function getProjectBudgetHealthPerDivision($year = '%'){ + $year = $this->interpolateYear($year); + + $divisions = Divisi::whereNull('parent')->get(); + foreach($divisions as $division){ + $budgetData = new Collection(); + $budgetData->prepend(rand(0, 10), 'overrun'); + $budgetData->prepend(rand(0, 10), 'warning'); + $budgetData->prepend(rand(0, 10), 'onBudget'); + $division->budgetData = $budgetData; + } + return response()->json([ + 'data' => [ + $divisions + ] + ], 200); + } + + public function getProjectPerPhase($year = '%'){ + $year = $this->interpolateYear($year); + $projectPhases = ProjectPhase::orderBy('order')->get(); + foreach($projectPhases as $phase){ + $phase->totalProject = rand(0,10); + } + return response()->json([ + 'data' => [ + $projectPhases + ] + ], 200); + } + + private function countTotalProjectInDivision($id, $year){ + return Project::where('divisi_id', $id) + ->where('mulai_proyek', 'like', $year) + ->count(); + } + + public function getTotalProjectPerDivision($year = '%') { + $year = $this->interpolateYear($year); + + $totalProjectPerDivision = Divisi::select('id','name') + ->with('children') + ->whereNull('parent') + ->get(); + + // to do : count in more than 1 level child + foreach($totalProjectPerDivision 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' => $totalProjectPerDivision + ], 200); + } + + private function countTotalProjectValueInDivision($id, $year){ + return Project::select(DB::raw('SUM(CAST("rencana_biaya" AS DOUBLE PRECISION))')) + ->where('mulai_proyek', 'like', $year) + ->where('divisi_id', $id) + ->pluck('sum') + ->first(); + } + + public function getTotalProjectValuePerDivision($year = '%') { + $year = $this->interpolateYear($year); + + $totalProjectValuePerDivision = Divisi::select('id','name') + ->with('children') + ->whereNull('parent') + ->get(); + + // to do : count in more than 1 level child + foreach($totalProjectValuePerDivision 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' => $totalProjectValuePerDivision + ], 200); + } + +} + diff --git a/routes/web.php b/routes/web.php index 55fee29..b636078 100644 --- a/routes/web.php +++ b/routes/web.php @@ -23,6 +23,16 @@ $router->group(['prefix'=>'api', 'middleware' => 'cors'], function () use ($rout $router->group(['middleware' => ['auth', 'cors']], function () use ($router) { + $router->get('/dashboard/get-company-cashflow[/{year}]', 'DashboardBoDController@getCompanyCashFlow'); // project expenditure + $router->get('/dashboard/get-invoice-outstanding[/{year}]', 'DashboardBoDController@getInvoiceOutstanding'); // project invoice vs cash in + $router->get('/dashboard/get-project-per-schedule-health[/{year}]', 'DashboardBoDController@getProjectPerScheduleHealth'); + $router->get('/dashboard/get-project-per-budget-health[/{year}]', 'DashboardBoDController@getProjectPerBudgetHealth'); + $router->get('/dashboard/get-project-schedule-health-per-division[/{year}]', 'DashboardBoDController@getProjectScheduleHealthPerDivision'); + $router->get('/dashboard/get-project-budget-health-per-division[/{year}]', 'DashboardBoDController@getProjectBudgetHealthPerDivision'); + $router->get('/dashboard/get-project-per-phase[/{year}]', 'DashboardBoDController@getProjectPerPhase'); // todo + $router->get('/dashboard/get-total-project-per-division[/{year}]', 'DashboardBoDController@getTotalProjectPerDivision'); // done + $router->get('/dashboard/get-total-project-value-per-division[/{year}]', 'DashboardBoDController@getTotalProjectValuePerDivision'); // done + $router->post('/role/search', 'RoleController@search'); $router->post('/role/add', 'RoleController@add'); $router->get('/role/edit/{id}', 'RoleController@edit');