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.

614 lines
17 KiB

<?php
namespace App\Http\Controllers;
use App\Models\User;
use App\Models\Divisi;
use App\Models\Project;
use App\Models\ProjectPhase;
use App\Models\ProjectExpenditure;
use App\Models\UserToVersionGantt;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
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
{
private function interpolateYear($year)
{
if ($year)
$year = '%' . $year . '%';
return $year;
}
public function getCompanyCashFlow($company_id, $all_project, $hierarchy, $role_name)
{
$totalExpenditure = $totalInvoice = $totalPaidInvoice = 0;
$totalBudgets = null;
$role = urldecode($role_name);
if ($role === "Super Admin") {
$totalBudgets = Project::sum(DB::raw('CAST("rencana_biaya" AS DOUBLE PRECISION)'));
} elseif (!empty($all_project)) {
$totalBudgets = Project::where('company_id', $company_id)
->sum(DB::raw('CAST("rencana_biaya" AS DOUBLE PRECISION)'));
} else {
$totalBudgets = Project::where('created_by_id', $hierarchy)
->sum(DB::raw('CAST("rencana_biaya" AS DOUBLE PRECISION)'));
}
$projects = null;
if ($role === "Super Admin") {
$projects = Project::get();
} elseif (!empty($all_project)) {
$projects = Project::where('company_id', $company_id)
->get();
} else {
$projects = Project::where('created_by_id', $hierarchy)
->get();
}
foreach ($projects as $project) {
$project->expenses = 0;
$resp = null;
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,
'total_paid_invoice' => $totalPaidInvoice,
]
], 200);
}
public function getDetailExpenditureColor($role_name, $company_id)
{
$role = urldecode($role_name);
$query = null;
if ($role === 'Super Admin') {
$query = ProjectExpenditure::get();
} else {
$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($role_name, $company_id)
{
$role = urldecode($role_name);
$query = null;
if ($role === 'Super Admin') {
$query = ProjectFinancialHealth::get();
} else {
$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($role_name, $company_id)
{
$role = urldecode($role_name);
$query = null;
if ($role === 'Super Admin') {
$query = ProjectScheduleHealth::get();
} else {
$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($role_name, $company_id)
{
$role = urldecode($role_name);
$query = null;
if ($role === 'Super Admin') {
$query = ProjectInvoice::get();
} else {
$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($role_name, $company_id, $all_project, $hierarchy)
{
$role = urldecode($role_name);
$projects = null;
if ($role === 'Super Admin') {
$projects = Project::get();
} elseif (!empty($all_project)) {
$projects = Project::where('company_id', $company_id)
->get();
} else {
$projects = Project::where('created_by_id', $hierarchy)
->get();
}
$return = [];
foreach ($projects as $project) {
$resp = null;
if ($project->kode_sortname != "") {
array_push($return, [
'project' => $project->nama,
'project_code' => $project->kode_sortname,
'invoiced' => 0,
'paid' => 0,
'response' => $resp,
]);
}
}
return response()->json([
'data' => $return
], 200);
}
public function getTotalProjectPerScheduleHealth($role_name, $company_id, $all_project, $hierarchy)
{
$role = urldecode($role_name);
$replaceHierarchy = preg_replace('/[\[\]]/', '', $hierarchy);
$return = [
'behind-schedule' => 0,
'warning' => 0,
'on-schedule' => 0,
];
$divisi = Divisi::query()
->where('company_id', $company_id)
->pluck('id');
$projects = null;
if ($role === 'Super Admin') {
$projects = Project::get();
} elseif ($all_project == 'true') {
$projects = Project::whereIn('divisi_id', $divisi)
->where('company_id', $company_id)
->get();
} else {
$projects = Project::whereIn('divisi_id', $divisi)
->where('created_by_id', $replaceHierarchy)
->get();
}
foreach ($projects as $index => $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;
try {
if ($selisihProgress > 0 && $selisihProgress <= 20) {
$return['warning'] += 1;
$projects[$index]->status = 'warning';
} elseif ($selisihProgress == 0) {
$return['on-schedule'] += 1;
$projects[$index]->status = 'on-schedule';
} else {
$return['behind-schedule'] += 1;
$projects[$index]->status = 'behind-schedule';
}
} catch (\Error $e) {
return response()->json(['msg' => $e->getMessage(), 'data' => $project], 200);
}
}
return response()->json(['data' => $return, 'q' => $projects], 200);
}
public function getTotalProjectScheduleHealthPerDivision($role_name, $company_id)
{
$role = urldecode($role_name);
$divisions = null;
if ($role === 'Super Admin') {
$divisions = Divisi::whereNull('parent')
->get();
} else {
$divisions = Divisi::whereNull('parent')
->where('company_id', $company_id)
->get();
}
foreach ($divisions as $index => $division) {
$scheduleData = new Collection();
$behindSchedule = $warning = $onSchedule = 0;
$projects = Project::where('divisi_id', $division->id)->get();
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($role_name, $company_id, $all_project, $hierarchy)
{
$role = urldecode($role_name);
$replaceHierarchy = preg_replace('/[\[\]]/', '', $hierarchy);
$response = [
'data' => [
'overrun' => 0,
'warning' => 0,
'on-budget' => 0,
]
];
$divisi = Divisi::query()
->where('company_id', $company_id)
->pluck('id');
// arr overrun
if ($role === 'Super Admin') {
$response['data']['overrun'] = Project::where('budget_health', 'overrun')
->count();
} elseif ($all_project == 'true') {
$response['data']['overrun'] = Project::whereIn('divisi_id', $divisi)
->where([['budget_health', 'overrun'], ['company_id', $company_id]])
->count();
} else {
$response['data']['overrun'] = Project::whereIn('divisi_id', $divisi)
->where([['budget_health', 'overrun'], ['created_by_id', $replaceHierarchy]])
->count();
}
// arr warning
if ($role === 'Super Admin') {
$response['data']['warning'] = Project::where('budget_health', 'warning')
->count();
} elseif ($all_project == 'true') {
$response['data']['warning'] = Project::whereIn('divisi_id', $divisi)
->where([['budget_health', 'warning'], ['company_id', $company_id]])
->count();
} else {
$response['data']['warning'] = Project::whereIn('divisi_id', $divisi)
->where([['budget_health', 'warning'], ['created_by_id', $replaceHierarchy]])
->count();
}
// arr on-budget
if ($role === 'Super Admin') {
$response['data']['on-budget'] = Project::where('budget_health', 'on-budget')
->count();
} elseif ($all_project == 'true') {
$response['data']['on-budget'] = Project::whereIn('divisi_id', $divisi)
->where([['company_id', $company_id], ['budget_health', 'on-budget']])
->count();
} else {
$response['data']['on-budget'] = Project::whereIn('divisi_id', $divisi)
->where([['budget_health', 'on-budget'], ['created_by_id', $replaceHierarchy]])
->count();
}
return response()->json($response, 200);
}
private function countTotalProjectByBudgetHealthInDivision($divisi, $health)
{
$project = Project::where([['divisi_id', $divisi], ['budget_health', $health]])->count();
return $project;
}
public function getTotalProjectBudgetHealthPerDivision($role_name, $company_id)
{
$role = urldecode($role_name);
$divisions = null;
if ($role === 'Super Admin') {
$divisions = Divisi::select('id', 'name')
->with('children')
->whereNull('parent')
->get();
} else {
$divisions = Divisi::select('id', 'name')
->with('children')
->whereNull('parent')
->where('company_id', $company_id)
->get();
}
// to do : count in more than 1 level child
foreach ($divisions as $division) {
$budgetData = new Collection();
$budgetData->prepend($this->countTotalProjectByBudgetHealthInDivision($division->id, 'overrun'), 'overrun');
$budgetData->prepend($this->countTotalProjectByBudgetHealthInDivision($division->id, 'warning'), 'warning');
$budgetData->prepend($this->countTotalProjectByBudgetHealthInDivision($division->id, 'on-budget'), 'on-budget');
foreach ($division->children as $d) {
$budgetData['overrun'] += $this->countTotalProjectByBudgetHealthInDivision($d->id, 'overrun');
$budgetData['warning'] += $this->countTotalProjectByBudgetHealthInDivision($d->id, 'warning');
$budgetData['on-budget'] += $this->countTotalProjectByBudgetHealthInDivision($d->id, 'on-budget');
}
unset($division->children);
$division->budgetData = $budgetData;
}
return response()->json([
'data' => [
$divisions
]
], 200);
}
public function getTotalProjectPerPhase($role_name, $company_id, $all_project, $hierarchy)
{
$role = urldecode($role_name);
$projectPhases = null;
if ($role === 'Super Admin') {
$projectPhases = ProjectPhase::orderBy('order')->get();
} else {
$projectPhases = ProjectPhase::where('company_id', $company_id)->orderBy('order')->get();
}
foreach ($projectPhases as $phase) {
if ($role === 'Super Admin') {
$phase->totalProject = Project::where('phase_id', $phase->id)
->count();
} elseif ($all_project) {
$phase->totalProject = Project::where('phase_id', $phase->id)
->where('company_id', $company_id)
->count();
} else {
$phase->totalProject = Project::where('phase_id', $phase->id)
->where('created_by_id', $hierarchy)
->count();
}
}
return response()->json([
'data' => [
$projectPhases
]
], 200);
}
private function countTotalProjectInDivision($id)
{
return Project::where('divisi_id', $id)
->count();
}
public function getTotalProjectPerDivision($role_name, $company_id)
{
$role = urldecode($role_name);
$divisions = null;
if ($role === 'Super Admin') {
$divisions = Divisi::select('id', 'name', 'parent', 'color')
->with('children')
->whereNull('parent')
->get();
} else {
$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
foreach ($divisions as $v) {
$v->total = $this->countTotalProjectInDivision($v->id);
foreach ($v->children as $d) {
$v->total += $this->countTotalProjectInDivision($d->id);
}
unset($v->children);
}
return response()->json([
'data' => $divisions
], 200);
}
private function countTotalProjectValueInDivision($id)
{
return Project::select(DB::raw('SUM(CAST("rencana_biaya" AS DOUBLE PRECISION))'))
->where('divisi_id', $id)
->pluck('sum')
->first();
}
public function getTotalProjectValuePerDivision($role_name, $company_id)
{
$role = urldecode($role_name);
if ($role === 'Super Admin') {
$divisions = Divisi::select('id', 'name', 'color')
->with('children')
->whereNull('parent')
->get();
} else {
$divisions = Divisi::select('id', 'name', 'color')
->with('children')
->whereNull('parent')
->where('company_id', $company_id)
->get();
}
foreach ($divisions as $v) {
$v->total = $this->countTotalProjectValueInDivision($v->id);
foreach ($v->children as $d) {
$v->total += $this->countTotalProjectValueInDivision($d->id);
}
unset($v->children);
}
return response()->json([
'data' => $divisions
], 200);
}
public function getDetailExpenditure($year = '%', $company_id, $all_project, $hierarchy)
{
$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();
}
foreach ($projects as $project) {
$lastGantt = MasterFunctionsHelper::getLatestGantt($project->id);
if ($project->kode_sortname != "") {
$project->invoice = [
'invoiced' => 0,
'paid' => 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(); */
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);
}
return response()->json([
'data' => $projects,
'total_manpowers' => User::count()
], 200);
}
}