<?php
namespace App\Controller;
use App\Entity\Client;
use App\Entity\User;
use App\Entity\Doi;
use App\Entity\Financial;
use App\Entity\Budget;
use App\Entity\Department;
use App\Entity\Initiative;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use CMEN\GoogleChartsBundle\GoogleCharts\Charts\ColumnChart;
use CMEN\GoogleChartsBundle\GoogleCharts\Charts\PieChart;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class HomeController extends AbstractController {
/**
* @Route("/dashboard", name="dashboard")
*/
public function dashboard(Request $request) {
$doctrine = $this->getDoctrine();
$current_year = $request->query->get('year');
$current_user = $request->query->get('user');
$current_client = $request->query->get('client');
$current_department = $request->query->get('department');
if (empty($current_year)) {
$current_year = date('Y');
}
if (empty($current_client)) {
$client = $this->getUser()->getClient();
} else {
$client = $doctrine->getRepository(Client::class)->find($current_client);
}
if (empty($current_user)) {
$user = false;
} else {
$user = $doctrine->getRepository(User::class)->find($current_user);
}
if (!empty($current_department)) {
$department = $doctrine->getRepository(Department::class)->find($current_department);
$users_ids = [];
if (!empty($department)) {
$users = $client->getUsers();
foreach ($users as $key => $value) {
if ($value->getDepartments()->contains($department)) {
$users_ids[] = $value->getId();
}
}
}
$user = empty($users_ids) ? [-1] : $users_ids;
}
$budgetsRepo = $doctrine->getRepository(Budget::class);
$financialsRepo = $doctrine->getRepository(Financial::class);
$budget = $budgetsRepo->findOneBy(['client' => $client, 'year' => $current_year]);
if (empty($user)) {
$financials = $financialsRepo->findBy(['client' => $client]);
} else {
$financials = $financialsRepo->findBy(['user' => $user]);
}
$total_all_stages = [0,0];
$total_stage_2 = [0,0];
$total_stage_3 = [0,0];
$total_stage_5 = [0,0];
foreach ($financials as $key => $financial) {
if (empty($financial->getStart()) || ($financial->getStart()->format('Y') != $current_year) || 'Desativada' === $financial->getInitiative()->getStatus() ) {
continue;
}
$cost = $financial->getCostReduction3();
$avoided_cost = $financial->getCostReduction4();
if (!empty($cost)) {
$total_all_stages[0] += $cost;
}
if (!empty($avoided_cost)) {
$total_all_stages[1] += $avoided_cost;
}
$items = $financial->getInitiative()->getDois()[0]->getDoiItems();
if ($items[4]->getApproved()) {
$total_stage_5[0] += $cost;
$total_stage_5[1] += $avoided_cost;
} else if ($items[2]->getApproved()) {
$total_stage_3[0] += $cost;
$total_stage_3[1] += $avoided_cost;
} else if ($items[1]->getApproved()) {
$total_stage_2[0] += $cost;
$total_stage_2[1] += $avoided_cost;
} else {
$total_stage_2[0] += $cost;
$total_stage_2[1] += $avoided_cost;
}
}
if ( is_null( $budget ) ) {
$budgetTotal = 0;
} else {
switch ($current_department) {
case 1:
$budgetTotal = $budget->getTotalCompras();
break;
case 3:
$budgetTotal = $budget->getTotalEngenharia();
break;
case 4:
$budgetTotal = $budget->getTotalManutencao();
break;
case 5:
$budgetTotal = $budget->getTotalProducao();
break;
case 6:
$budgetTotal = $budget->getTotalAdministrativo();
break;
default:
$budgetTotal = $budget->getTotal();
break;
}
}
// CHART DATA COST REDUCTION
$data = [['label' => 'Redução de custo', 'color' => '#98FB98', 'data' => [['Orçamento', empty($budget) ? 0 : $budgetTotal]]]];
$data[0]['data'][] = ['DOI 1-5', $total_all_stages[0]];
$data[0]['data'][] = ['DOI 1-3', $total_stage_2[0]];
$data[0]['data'][] = ['DOI 4', $total_stage_3[0]];
$data[0]['data'][] = ['DOI 5', $total_stage_5[0]];
// CHART DATA AVOIDED COST
$data_avoided = [['label' => 'Custo evitado', 'color' => '#98ded9', 'data' => [['Orçamento', 0]]]];
$data_avoided[0]['data'][] = ['DOI 1-5', $total_all_stages[1]];
$data_avoided[0]['data'][] = ['DOI 1-3', $total_stage_2[1]];
$data_avoided[0]['data'][] = ['DOI 4', $total_stage_3[1]];
$data_avoided[0]['data'][] = ['DOI 5', $total_stage_5[1]];
// OPTIONS FOR FILTERS
if ($this->getUser()->isCoordinator()) {
$departments = empty($this->getUser()->getDepartments()) ? [] : $this->getUser()->getDepartments();
} else {
$departments = $doctrine->getRepository(Department::class)->findAll();
}
$dpt_options = [];
foreach ($departments as $key => $value) {
$dpt_options[]= ['id' => $value->getId(), 'name' => $value->getName()];
}
$users = $doctrine->getRepository(User::class)->findBy(['client' => $this->getUser()->getClient()]);
$clients = [$this->getUser()->getClient()];
$user_options = [];
foreach ($users as $key => $value) {
if ($this->getUser()->isCoordinator()) {
$add = false;
if (!empty($this->getUser()->getDepartments())) {
foreach ($this->getUser()->getDepartments() as $keyDpt => $dpt) {
if ($value->getDepartments()->contains($dpt)) {
$add = true;
}
}
}
}
if (isset($add) && !$add) {
continue;
}
$user_options[] = ['id' => $value->getId(), 'name' => $value->getFullname()];
}
foreach ($clients as $key => $value) {
$clients[$key] = ['id' => $value->getId(), 'name' => $value->getName()];
}
return $this->render('home/dashboard.html.twig', [
'data' => $data,
'data_avoided' => $data_avoided,
'users' => $user_options,
'clients' => $clients,
'departments' => $dpt_options,
'isAdmin' => $this->getUser()->isAdmin(),
'isMaster' => $this->getUser()->isMaster(),
'isManager' => $this->getUser()->isManager(),
'isCoordinator' => $this->getUser()->isCoordinator(),
]);
}
/**
* @Route("/home", name="home")
*/
public function index(Request $request) {
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
try {
$CCM_FEATURE_TOGGLE = $_ENV['CCM_FEATURE_TOGGLE'];
} catch (\Throwable $th) {
$CCM_FEATURE_TOGGLE = false;
}
return $this->render('home/index.html.twig', [
'CCM_FEATURE_TOGGLE' => $CCM_FEATURE_TOGGLE,
]);
}
/**
* @Route("/without-permission", name="without-permission")
*/
public function withoutPermission(Request $request) {
return $this->render('home/without-permission.html.twig', []);
}
}