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.

578 lines
17 KiB

<?php
namespace App\Http\Controllers;
use App\Models\User;
use App\Models\Divisi;
2 years ago
use App\Models\Project;
use App\Models\ProjectPhase;
use App\Models\ProjectExpenditure;
2 years ago
use App\Models\UserToVersionGantt;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
1 year ago
use Illuminate\Support\Facades\Log;
use App\Models\ProjectScheduleHealth;
use App\Helpers\MasterFunctionsHelper;
use App\Models\ProjectFinancialHealth;
use App\Models\ProjectInvoice;
class DashboardBoDController extends Controller
{
1 year ago
private function interpolateYear($year)
{
if ($year)
$year = '%' . $year . '%';
return $year;
}
1 year ago
private function curlReq($url, $token)
{
$ch = curl_init();
$headers = [
1 year ago
'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);
}
1 year ago
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;
}
public function getCompanyCashFlow($year = '%', $company_id, $all_project, $hierarchy)
1 year ago
{
$year = $this->interpolateYear($year);
$totalExpenditure = $totalInvoice = $totalPaidInvoice = 0;
$totalBudgets = null;
if ($all_project) {
$totalBudgets = Project::where('mulai_proyek', 'like', $year)
->where('company_id', $company_id)
->sum(DB::raw('CAST("rencana_biaya" AS DOUBLE PRECISION)'));
} else {
$totalBudgets = Project::where('mulai_proyek', 'like', $year)
->where('created_by_id', $hierarchy)
->sum(DB::raw('CAST("rencana_biaya" AS DOUBLE PRECISION)'));
}
$projects = null;
if ($all_project) {
$projects = Project::where('mulai_proyek', 'like', $year)
->where('company_id', $company_id)
->get();
} else {
$projects = Project::where('mulai_proyek', 'like', $year)
->where('created_by_id', $hierarchy)
->get();
}
1 year ago
foreach ($projects as $project) {
$project->expenses = 0;
$resp = null;
1 year ago
if ($project->kode_sortname != "") {
// $resp = $this->getInvoiceIntegration($project->kode_sortname);
// $cost = $resp->data->total_cost ?? 0;
// $cost = substr($cost, 0, strpos($cost, "."));
$cost = 0;
$totalExpenditure = 0;
$totalInvoice = 0;
$totalPaidInvoice = 0;
// $totalExpenditure += (int) $cost;
// $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,
1 year ago
'total_paid_invoice' => $totalPaidInvoice,
]
], 200);
}
public function getDetailExpenditureColor($company_id) {
$query = ProjectExpenditure::where('company_id',$company_id)->get();
$data = [];
foreach($query as $value) {
if($value['name'] === 'Total Budget') {
$data['total_budget'] = $value['color'];
}
if($value['name'] === 'Expenditure') {
$data['total_expenditure'] = $value['color'];
}
if($value['name'] === 'Invoice') {
$data['total_invoice'] = $value['color'];
}
if($value['name'] === 'Cash In') {
$data['total_paid_invoice'] = $value['color'];
}
}
return response()->json([
'data' => [
'total_budget' => $data['total_budget'] ?? '',
'total_expenditure' => $data['total_expenditure'] ?? '',
'total_invoice' => $data['total_invoice'] ?? '',
'total_paid_invoice' => $data['total_paid_invoice'] ?? '',
]
], 200);
}
public function getDetailFinancialHealthColor($company_id) {
$query = ProjectFinancialHealth::where('company_id',$company_id)->get();
$data = [];
foreach($query as $value) {
if($value['name'] === 'Cost Overrun') {
$data['overrun'] = $value['color'];
}
if($value['name'] === 'Early Warning') {
$data['warning'] = $value['color'];
}
if($value['name'] === 'On Budget') {
$data['on-budget'] = $value['color'];
}
}
return response()->json([
'data' => [
'overrun' => $data['overrun'] ?? '',
'warning' => $data['warning'] ?? '',
'on-budget' => $data['on-budget'] ?? '',
]
], 200);
}
public function getDetailScheduleHealthColor($company_id) {
$query = ProjectScheduleHealth::where('company_id',$company_id)->get();
$data = [];
foreach($query as $value) {
if($value['name'] === 'Behind Schedule') {
$data['behind-schedule'] = $value['color'];
}
if($value['name'] === 'Early Warning') {
$data['warning'] = $value['color'];
}
if($value['name'] === 'On Schedule') {
$data['on-schedule'] = $value['color'];
}
}
return response()->json([
'data' => [
'behind-schedule' => $data['behind-schedule'] ?? '',
'warning' => $data['warning'] ?? '',
'on-schedule' => $data['on-schedule'] ?? '',
]
], 200);
}
public function getDetailInvoiceColor($company_id) {
$query = ProjectInvoice::where('company_id',$company_id)->get();
$data = [];
foreach($query as $value) {
if($value['name'] === 'Invoiced') {
$data['invoiced'] = $value['color'];
}
if($value['name'] === 'Cash In') {
$data['paid'] = $value['color'];
}
}
return response()->json([
'data' => [
'invoiced' => $data['invoiced'] ?? '',
'paid' => $data['paid'] ?? ''
]
], 200);
}
// integrasi
public function getInvoiceOutstanding($year = '%', $company_id, $all_project, $hierarchy)
1 year ago
{
$year = $this->interpolateYear($year);
$projects = null;
if ($all_project) {
$projects = Project::where('mulai_proyek', 'like', $year)
->where('company_id', $company_id)
->get();
} else {
$projects = Project::where('mulai_proyek', 'like', $year)
->where('created_by_id', $hierarchy)
->get();
}
$return = [];
1 year ago
foreach ($projects as $project) {
$resp = null;
1 year ago
if ($project->kode_sortname != "") {
// $resp = $this->getInvoiceIntegration($project->kode_sortname);
array_push($return, [
'project' => $project->nama,
'project_code' => $project->kode_sortname,
'invoiced' => 0,
'paid' => 0,
'response' => $resp,
]);
}
}
2 years ago
return response()->json([
'data' => $return
2 years ago
], 200);
}
public function getTotalProjectPerScheduleHealth($year = '%', $company_id, $all_project, $hierarchy)
1 year ago
{
$year = $this->interpolateYear($year);
$return = [
'behind-schedule' => 0,
'warning' => 0,
'on-schedule' => 0,
];
$projects = null;
if ($all_project) {
$projects = Project::where('mulai_proyek', 'like', $year)
->where('company_id', $company_id)
->get();
} else {
$projects = Project::where('mulai_proyek', 'like', $year)
->where('created_by_id', $hierarchy)
->get();
}
foreach ($projects as $index => $project) {
1 year ago
$project->scurve = MasterFunctionsHelper::getSCurve($project->id);
$selisihProgress = 0;
$planningProgress = 0;
$actualProgress = 0;
1 year ago
if ($project->scurve && $project->scurve[0]) {
$planningArray = $project->scurve[0]['data']['percentagePlan'];
$actualArray = $project->scurve[0]['data']['percentageReal'];
$planningProgress = !empty($planningArray) ? $planningArray[count($planningArray) - 1] : 0;
$actualProgress = !empty($actualArray) ? $actualArray[count($actualArray) - 1] : 0;
}
$selisihProgress = $planningProgress - $actualProgress;
2 years ago
try {
if ($selisihProgress > 0 && $selisihProgress <= 5) {
1 year ago
$return['warning'] += 1;
$projects[$index]->status = 'warning';
} elseif ($selisihProgress == 0) {
1 year ago
$return['on-schedule'] += 1;
$projects[$index]->status = 'on-schedule';
} else {
1 year ago
$return['behind-schedule'] += 1;
$projects[$index]->status = 'behind-schedule';
}
2 years ago
} catch (\Error $e) {
1 year ago
return response()->json(['msg' => $e->getMessage(), 'data' => $project], 200);
2 years ago
}
}
2 years ago
return response()->json(['data' => $return, 'q' => $projects], 200);
}
public function getTotalProjectScheduleHealthPerDivision($year = '%', $company_id)
1 year ago
{
$year = $this->interpolateYear($year);
$divisions = Divisi::whereNull('parent')
->where('company_id', $company_id)
11 months ago
->whereNull('parent')
->get();
foreach ($divisions as $index => $division) {
$scheduleData = new Collection();
$behindSchedule = $warning = $onSchedule = 0;
$projects = Project::where('mulai_proyek', 'like', $year)->where('divisi_id', $division->id)->get();
1 year ago
foreach ($projects as $project) {
$project->scurve = MasterFunctionsHelper::getSCurve($project->id);
$selisihProgress = 0;
$planningProgress = 0;
$actualProgress = 0;
if ($project->scurve && $project->scurve[0]) {
$planningArray = $project->scurve[0]['data']['percentagePlan'];
$actualArray = $project->scurve[0]['data']['percentageReal'];
$planningProgress = !empty($planningArray) ? $planningArray[count($planningArray) - 1] : 0;
$actualProgress = !empty($actualArray) ? $actualArray[count($actualArray) - 1] : 0;
}
$selisihProgress = $planningProgress - $actualProgress;
if ($selisihProgress > 0 && $selisihProgress <= 5) {
$warning++;
} elseif ($selisihProgress == 0) {
$onSchedule++;
} else {
$behindSchedule++;
}
}
$scheduleData->prepend($behindSchedule, 'behindSchedule');
$scheduleData->prepend($warning, 'warning');
$scheduleData->prepend($onSchedule, 'onSchedule');
$divisions[$index]->scheduleData = $scheduleData;
}
return response()->json([
'data' => [
$divisions
]
], 200);
}
public function getTotalProjectPerBudgetHealth($year = '%', $company_id, $all_project, $hierarchy)
1 year ago
{
$year = $this->interpolateYear($year);
$response = [
'data' => [
'overrun' => 0,
'warning' => 0,
'on-budget' => 0,
]
];
if ($all_project) {
$response['data']['overrun'] = Project::where('mulai_proyek', 'like', $year)
->where('budget_health', 'overrun')
->where('company_id', $company_id)
->count();
} else {
$response['data']['overrun'] = Project::where('mulai_proyek', 'like', $year)
->where('budget_health', 'overrun')
->where('created_by_id', $hierarchy)
->count();
}
if ($all_project) {
$response['data']['overrun'] = Project::where('mulai_proyek', 'like', $year)
->where('budget_health', 'overrun')
->where('company_id', $company_id)
->count();
} else {
$response['data']['overrun'] = Project::where('mulai_proyek', 'like', $year)
->where('budget_health', 'overrun')
->where('created_by_id', $hierarchy)
->count();
}
if ($all_project) {
$response['data']['warning'] = Project::where('mulai_proyek', 'like', $year)
->where('budget_health', 'warning')
->where('company_id', $company_id)
->count();
} else {
$response['data']['warning'] = Project::where('mulai_proyek', 'like', $year)
->where('budget_health', 'warning')
->where('created_by_id', $hierarchy)
->count();
}
if ($all_project) {
$response['data']['on-budget'] = Project::where('mulai_proyek', 'like', $year)
->where('budget_health', 'on-budget')
->where('company_id', $company_id)
->count();
} else {
$response['data']['on-budget'] = Project::where('mulai_proyek', 'like', $year)
->where('budget_health', 'on-budget')
->where('created_by_id', $hierarchy)
->count();
}
return response()->json($response, 200);
}
1 year ago
private function countTotalProjectByBudgetHealthInDivision($divisi, $year, $health)
{
2 years ago
return Project::where('divisi_id', $divisi)
->where('mulai_proyek', 'like', $year)
1 year ago
/* ->orWhere('akhir_proyek', 'like', $year) */
2 years ago
->where('budget_health', $health)
->count();
}
2 years ago
public function getTotalProjectBudgetHealthPerDivision($year = '%', $company_id)
1 year ago
{
2 years ago
$year = $this->interpolateYear($year);
1 year ago
$divisions = Divisi::select('id', 'name')
2 years ago
->with('children')
->whereNull('parent')
->where('company_id', $company_id)
2 years ago
->get();
// to do : count in more than 1 level child
1 year ago
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');
1 year ago
foreach ($division->children as $d) {
2 years ago
$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;
}
1 year ago
foreach ($divisions as $division) {
2 years ago
}
return response()->json([
'data' => [
$divisions
]
], 200);
}
public function getTotalProjectPerPhase($year = '%', $company_id, $all_project, $hierarchy)
1 year ago
{
$year = $this->interpolateYear($year);
$projectPhases = ProjectPhase::where('company_id', $company_id)->orderBy('order')->get();
1 year ago
foreach ($projectPhases as $phase) {
if ($all_project) {
$phase->totalProject = Project::where('phase_id', $phase->id)
->where('mulai_proyek', 'like', $year)
->where('company_id', $company_id)
->count();
} else {
$phase->totalProject = Project::where('phase_id', $phase->id)
->where('mulai_proyek', 'like', $year)
->where('created_by_id', $hierarchy)
->count();
}
}
return response()->json([
'data' => [
$projectPhases
]
], 200);
}
1 year ago
private function countTotalProjectInDivision($id, $year)
{
return Project::where('divisi_id', $id)
->where('mulai_proyek', 'like', $year)
->count();
}
public function getTotalProjectPerDivision($year = '%', $company_id)
1 year ago
{
$year = $this->interpolateYear($year);
$divisions = Divisi::select('id', 'name', 'parent', 'color')
->with('children')
->whereNull('parent')
->where('company_id', $company_id)
->get();
// to do : count in more than 1 level child
1 year ago
foreach ($divisions as $v) {
$v->total = $this->countTotalProjectInDivision($v->id, $year);
1 year ago
foreach ($v->children as $d) {
$v->total += $this->countTotalProjectInDivision($d->id, $year);
}
unset($v->children);
}
return response()->json([
2 years ago
'data' => $divisions
], 200);
}
1 year ago
private function countTotalProjectValueInDivision($id, $year)
{
return Project::select(DB::raw('SUM(CAST("rencana_biaya" AS DOUBLE PRECISION))'))
->where('mulai_proyek', 'like', $year)
1 year ago
/* ->orWhere('akhir_proyek', 'like', $year) */
->where('divisi_id', $id)
->pluck('sum')
->first();
}
public function getTotalProjectValuePerDivision($year = '%', $company_id)
1 year ago
{
$year = $this->interpolateYear($year);
$divisions = Divisi::select('id', 'name', 'color')
->with('children')
->whereNull('parent')
->where('company_id', $company_id)
->get();
1 year ago
foreach ($divisions as $v) {
$v->total = $this->countTotalProjectValueInDivision($v->id, $year);
1 year ago
foreach ($v->children as $d) {
$v->total += $this->countTotalProjectValueInDivision($d->id, $year);
}
unset($v->children);
}
return response()->json([
2 years ago
'data' => $divisions
], 200);
}
public function getDetailExpenditure($year = '%', $company_id, $all_project, $hierarchy)
1 year ago
{
2 years ago
$year = $this->interpolateYear($year);
$projects = null;
if ($all_project) {
$projects = Project::where('mulai_proyek', 'like', $year)
->where('company_id', $company_id)
->orderBy('id', 'desc')
->get();
} else {
$projects = Project::where('mulai_proyek', 'like', $year)
->where('created_by_id', $hierarchy)
->orderBy('id', 'desc')
->get();
}
1 year ago
foreach ($projects as $project) {
$lastGantt = MasterFunctionsHelper::getLatestGantt($project->id);
2 years ago
1 year ago
if ($project->kode_sortname != "") {
2 years ago
$project->invoice = [
'invoiced' => 0,
'paid' => 0,
2 years ago
];
}
$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(); */
1 year ago
if (!isset($lastGantt['last_gantt_id'])) {
$project->manPowers = 0;
} else {
$project->manPowers = UserToVersionGantt::where('version_gantt_id', $lastGantt['last_gantt_id'])->count();
$project->scurve = MasterFunctionsHelper::getSCurve($project->id);
}
$project->lastGanttId = MasterFunctionsHelper::getLatestGantt($project->id);
2 years ago
}
return response()->json([
'data' => $projects,
'total_manpowers' => User::count()
], 200);
}
}