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.

284 lines
8.3 KiB

<?php
namespace App\Http\Controllers;
use App\Models\Project;
use App\Models\Divisi;
use App\Models\ProjectPhase;
use App\Models\AssignMaterial;
use App\Models\ReportActivityMaterial;
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;
}
2 years ago
// 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);
}
2 years ago
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,
]);
}
}
2 years ago
return response()->json([
'data' => $return
2 years ago
], 200);
}
// to do
public function getTotalProjectPerScheduleHealth($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);
}
2 years ago
// todo
public function getTotalProjectScheduleHealthPerDivision($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);
}
2 years ago
public function getTotalProjectPerBudgetHealth($year = '%'){
$year = $this->interpolateYear($year);
return response()->json([
'data' => [
2 years ago
'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);
}
2 years ago
private function countTotalProjectByBudgetHealthInDivision($divisi, $year, $health){
return Project::where('divisi_id', $divisi)
->where('mulai_proyek', 'like', $year)
/* ->orWhere('akhir_proyek', 'like', $year) */
2 years ago
->where('budget_health', $health)
->count();
}
2 years ago
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();
2 years ago
$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;
}
2 years ago
foreach($divisions as $division){
}
return response()->json([
'data' => [
$divisions
]
], 200);
}
2 years ago
public function getTotalProjectPerPhase($year = '%'){
$year = $this->interpolateYear($year);
$projectPhases = ProjectPhase::orderBy('order')->get();
foreach($projectPhases as $phase){
2 years ago
$phase->totalProject = Project::where('phase_id', $phase->id)
->where('mulai_proyek', 'like', $year)
/* ->orWhere('akhir_proyek', 'like', $year) */
2 years ago
->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);
2 years ago
$divisions = Divisi::select('id','name')
->with('children')
->whereNull('parent')
->get();
// to do : count in more than 1 level child
2 years ago
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([
2 years ago
'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);
2 years ago
$divisions = Divisi::select('id','name')
->with('children')
->whereNull('parent')
->get();
// to do : count in more than 1 level child
2 years ago
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([
2 years ago
'data' => $divisions
], 200);
}
}