src/Controller/HomeController.php line 184

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Client;
  4. use App\Entity\User;
  5. use App\Entity\Doi;
  6. use App\Entity\Financial;
  7. use App\Entity\Budget;
  8. use App\Entity\Department;
  9. use App\Entity\Initiative;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use CMEN\GoogleChartsBundle\GoogleCharts\Charts\ColumnChart;
  15. use CMEN\GoogleChartsBundle\GoogleCharts\Charts\PieChart;
  16. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  17. class HomeController extends AbstractController {
  18.     /**
  19.      * @Route("/dashboard", name="dashboard")
  20.     */
  21.     public function dashboard(Request $request) {
  22.         $doctrine $this->getDoctrine();
  23.         $current_year $request->query->get('year');
  24.         $current_user $request->query->get('user');
  25.         $current_client $request->query->get('client');
  26.         $current_department $request->query->get('department');
  27.         if (empty($current_year)) {
  28.             $current_year date('Y');
  29.         }
  30.         if (empty($current_client)) {
  31.             $client $this->getUser()->getClient();
  32.         } else {
  33.             $client $doctrine->getRepository(Client::class)->find($current_client);
  34.         }
  35.         if (empty($current_user)) {
  36.             $user false;
  37.         } else {
  38.             $user $doctrine->getRepository(User::class)->find($current_user);
  39.         }
  40.         if (!empty($current_department)) {
  41.             $department $doctrine->getRepository(Department::class)->find($current_department);
  42.             $users_ids = [];
  43.             if (!empty($department)) {
  44.                 $users $client->getUsers();
  45.                 foreach ($users as $key => $value) {
  46.                     if ($value->getDepartments()->contains($department)) {
  47.                         $users_ids[] = $value->getId();
  48.                     }
  49.                 }
  50.             }
  51.             $user = empty($users_ids) ? [-1] : $users_ids;
  52.         }
  53.         $budgetsRepo $doctrine->getRepository(Budget::class);
  54.         $financialsRepo $doctrine->getRepository(Financial::class);
  55.         $budget $budgetsRepo->findOneBy(['client' => $client'year' => $current_year]);
  56.         if (empty($user)) {
  57.             $financials $financialsRepo->findBy(['client' => $client]);
  58.         } else {
  59.             $financials $financialsRepo->findBy(['user' => $user]);
  60.         }
  61.         $total_all_stages = [0,0];
  62.         $total_stage_2 = [0,0];
  63.         $total_stage_3 = [0,0];
  64.         $total_stage_5 = [0,0];
  65.         foreach ($financials as $key => $financial) {
  66.             if (empty($financial->getStart()) || ($financial->getStart()->format('Y') != $current_year) || 'Desativada' === $financial->getInitiative()->getStatus() ) {
  67.               continue;
  68.             }
  69.             $cost $financial->getCostReduction3();
  70.             $avoided_cost $financial->getCostReduction4();
  71.             if (!empty($cost)) {
  72.                 $total_all_stages[0] += $cost;
  73.             }
  74.             if (!empty($avoided_cost)) {
  75.                 $total_all_stages[1] += $avoided_cost;
  76.             }
  77.             $items $financial->getInitiative()->getDois()[0]->getDoiItems();
  78.             if ($items[4]->getApproved()) {
  79.                 $total_stage_5[0] += $cost;
  80.                 $total_stage_5[1] += $avoided_cost;
  81.             } else if ($items[2]->getApproved()) {
  82.                 $total_stage_3[0] += $cost;
  83.                 $total_stage_3[1] += $avoided_cost;
  84.             } else if ($items[1]->getApproved()) {
  85.                 $total_stage_2[0] += $cost;
  86.                 $total_stage_2[1] += $avoided_cost;
  87.             } else {
  88.                 $total_stage_2[0] += $cost;
  89.                 $total_stage_2[1] += $avoided_cost;
  90.             }
  91.         }
  92.         if ( is_null$budget ) ) {
  93.             $budgetTotal 0;
  94.         } else {
  95.             switch ($current_department) {
  96.                 case 1:
  97.                     $budgetTotal $budget->getTotalCompras();
  98.                     break;
  99.                 case 3:
  100.                     $budgetTotal $budget->getTotalEngenharia();
  101.                     break;
  102.                 case 4:
  103.                     $budgetTotal $budget->getTotalManutencao();
  104.                     break;
  105.                 case 5:
  106.                     $budgetTotal $budget->getTotalProducao();
  107.                     break;
  108.                 case 6:
  109.                     $budgetTotal $budget->getTotalAdministrativo();
  110.                     break;
  111.                 default:
  112.                     $budgetTotal $budget->getTotal();
  113.                     break;
  114.             }
  115.         }
  116.         // CHART DATA COST REDUCTION
  117.         $data = [['label' => 'Redução de custo''color' => '#98FB98''data' => [['Orçamento', empty($budget) ? $budgetTotal]]]];
  118.         $data[0]['data'][] = ['DOI 1-5'$total_all_stages[0]];
  119.         $data[0]['data'][] = ['DOI 1-3'$total_stage_2[0]];
  120.         $data[0]['data'][] = ['DOI 4'$total_stage_3[0]];
  121.         $data[0]['data'][] = ['DOI 5'$total_stage_5[0]];
  122.         // CHART DATA AVOIDED COST
  123.         $data_avoided = [['label' => 'Custo evitado''color' => '#98ded9''data' => [['Orçamento'0]]]];
  124.         $data_avoided[0]['data'][] = ['DOI 1-5'$total_all_stages[1]];
  125.         $data_avoided[0]['data'][] = ['DOI 1-3'$total_stage_2[1]];
  126.         $data_avoided[0]['data'][] = ['DOI 4'$total_stage_3[1]];
  127.         $data_avoided[0]['data'][] = ['DOI 5'$total_stage_5[1]];
  128.         // OPTIONS FOR FILTERS
  129.         if ($this->getUser()->isCoordinator()) {
  130.             $departments = empty($this->getUser()->getDepartments()) ? [] : $this->getUser()->getDepartments();
  131.         } else {
  132.             $departments $doctrine->getRepository(Department::class)->findAll();
  133.         }
  134.         $dpt_options = [];
  135.         foreach ($departments as $key => $value) {
  136.             $dpt_options[]= ['id' => $value->getId(), 'name' => $value->getName()];
  137.         }
  138.         $users $doctrine->getRepository(User::class)->findBy(['client' => $this->getUser()->getClient()]);
  139.         $clients = [$this->getUser()->getClient()];
  140.         $user_options = [];
  141.         foreach ($users as $key => $value) {
  142.             if ($this->getUser()->isCoordinator()) {
  143.                 $add false;
  144.                 if (!empty($this->getUser()->getDepartments())) {
  145.                     foreach ($this->getUser()->getDepartments() as $keyDpt => $dpt) {
  146.                         if ($value->getDepartments()->contains($dpt)) {
  147.                             $add true;
  148.                         }
  149.                     }
  150.                 }
  151.             }
  152.             if (isset($add) && !$add) {
  153.                 continue;
  154.             }
  155.             $user_options[] = ['id' => $value->getId(), 'name' => $value->getFullname()];
  156.         }
  157.         foreach ($clients as $key => $value) {
  158.             $clients[$key] = ['id' => $value->getId(), 'name' => $value->getName()];
  159.         }
  160.         return $this->render('home/dashboard.html.twig', [
  161.             'data' => $data,
  162.             'data_avoided' => $data_avoided,
  163.             'users' => $user_options,
  164.             'clients' => $clients,
  165.             'departments' => $dpt_options,
  166.             'isAdmin' => $this->getUser()->isAdmin(),
  167.             'isMaster' => $this->getUser()->isMaster(),
  168.             'isManager' => $this->getUser()->isManager(),
  169.             'isCoordinator' => $this->getUser()->isCoordinator(),
  170.         ]);
  171.     }
  172.     /**
  173.      * @Route("/home", name="home")
  174.     */
  175.     public function index(Request $request) {
  176.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  177.         try {
  178.             $CCM_FEATURE_TOGGLE $_ENV['CCM_FEATURE_TOGGLE'];
  179.         } catch (\Throwable $th) {
  180.             $CCM_FEATURE_TOGGLE false;
  181.         }
  182.         return $this->render('home/index.html.twig', [
  183.             'CCM_FEATURE_TOGGLE' => $CCM_FEATURE_TOGGLE,
  184.         ]);
  185.     }
  186.     /**
  187.      * @Route("/without-permission", name="without-permission")
  188.     */
  189.     public function withoutPermission(Request $request) {
  190.         return $this->render('home/without-permission.html.twig', []);
  191.     }
  192. }