src/Controller/DashboardController.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Action;
  4. use App\Entity\Appointments;
  5. use App\Entity\User;
  6. use App\Entity\Order;
  7. use App\Entity\Settings;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. class DashboardController extends AbstractController
  12. {
  13.     /**
  14.      * @Route("/", name="dashboard")
  15.      */
  16.     public function dashboard(): Response
  17.     {
  18.         # get the entity manager $em
  19.         $em $this->getDoctrine()->getManager();
  20.         $connection $em->getConnection();
  21.         
  22.         # create variables with dates
  23.         $today = new \DateTime("today midnight");
  24.         $thisyear $today->format('Y');
  25.         
  26.         $startofthisyear date_create_from_format('j-M-Y''1-Jan-' $thisyear);
  27.         $endofthisyear date_create_from_format('j-M-Y''31-Dec-' $thisyear);
  28.         
  29.         #----------------------------------------------------------------------------#
  30.         # PIE CHART
  31.         # 1. Get all users
  32.         # 2. Check for each user the actions that are not finished
  33.         
  34.         $userlist = [];
  35.         $useractionlist = [];
  36.         $colorlist = [];
  37.         
  38.         # get repository for all users
  39.         $repoUsers $em->getRepository(User::class);
  40.         
  41.         # build and execute query to get all users with status = 1
  42.         $queryUsers $repoUsers->createQueryBuilder('a')
  43.             ->select('a.id, a.username, a.color')
  44.             ->where('a.status = 1');
  45.         $users $queryUsers->getQuery()->execute();
  46.         
  47.         # loop through all the users
  48.         foreach ($users as $user) {
  49.             
  50.             # get user id
  51.             $userid $user['id'];
  52.             
  53.             # build and execute custom query to select all open actions related to the user
  54.             $countuserquery "SELECT id FROM action WHERE actionholder_id = $userid and status = 1 and isfinished = 0";
  55.             $stmt $em->getConnection()->prepare($countuserquery);
  56.             $stmt->execute();
  57.             $result $stmt->fetchAll();
  58.             
  59.             # if there are open actions
  60.             if (count($result)) {
  61.                 
  62.                 # create array with all users (usernames)
  63.                 $userlist[] = $user['username'];
  64.                 
  65.                 # create array with number of open actions
  66.                 $useractionlist[] = count($result);
  67.                 
  68.                 # create array with colors (user setting)
  69.                 $colorlist[] = $user['color'];      
  70.             }
  71.         }
  72.         
  73.         #----------------------------------------------------------------------------#
  74.         # APPOINTMENTS
  75.         
  76.         # get repository via the entity manager
  77.         $repoAppointments $em->getRepository(Appointments::class);
  78.         
  79.         # build query and execute, get list with appointment for logged in user
  80.         $queryAppointments$repoAppointments->createQueryBuilder('a')
  81.             ->select('a.id, a.title, a.startdate, a.enddate')
  82.             ->where('a.status = 1')
  83.             ->andWhere('a.enddate >= :now')
  84.             ->andWhere('a.host = ' $this->getUser()->getId())
  85.             ->setParameter('now',$today->format('Y-m-d'))
  86.             ->orderBy('a.startdate','ASC');
  87.         $appointments $queryAppointments->getQuery()->execute();
  88.         
  89.         # build query and execute to count all open appointments
  90.         $queryAppointmentsCount$repoAppointments->createQueryBuilder('a')
  91.             ->select('count(DISTINCT a.id)')
  92.             ->leftJoin('a.actionholder','u')
  93.             ->andWhere('a.host = ' $this->getUser()->getId())
  94.             ->orWhere('u.id = ' $this->getUser()->getId())
  95.             ->andWhere('a.enddate >= :today')
  96.             ->andWhere('a.status = 1')
  97.             #->groupBy('a.id')
  98.             ->setParameter('today',$today);
  99.         $appointmentscount $queryAppointmentsCount->getQuery()->getSingleScalarResult();
  100.         
  101.         #----------------------------------------------------------------------------#
  102.         # ORDERS    
  103.         
  104.         # default array for number of orders per month this year
  105.         $orderspermontharray = [ 00000000000];
  106.         
  107.         # build query to get orders per month for this year
  108.         $orderspermonthquery "SELECT MONTH(o.createdate) Maand, COUNT(*) `Aantal` FROM `order` o WHERE YEAR(o.createdate) = YEAR(CURRENT_DATE) GROUP BY YEAR(o.createdate), MONTH(o.createdate)";
  109.         #$orderspermonthquery = "SELECT MONTH(o.createdate) Maand, COUNT(*) `Aantal` FROM `order` o GROUP BY YEAR(o.createdate), MONTH(o.createdate)";
  110.         
  111.         $statement $connection->prepare($orderspermonthquery);
  112.         $statement->execute();
  113.         $orderspermonth $statement->fetchAll();
  114.         
  115.         foreach ($orderspermonth as $key=>$value) {
  116.             $currentmonth $value["Maand"];    
  117.             $orderspermontharray[$currentmonth-1] = $value["Aantal"];
  118.         }
  119.         
  120.         #----------------------------------------------------------------------------#
  121.         # FINANCIALS toezeggingen last year
  122.         
  123.         # financials per month array ly = last year
  124.         $lyfinancialspermontharray = [ 00000000000];
  125.         
  126.         $lyfinancialspermonthquery "SELECT MONTH(f.financialdate) Maand, COUNT(*) `Aantal`, SUM(invatprice) 'EUR' FROM `financials` f JOIN `order` o ON f.orderid_id = o.id WHERE f.category_id = 1 AND o.STATUS = 1 AND f.STATUS = 1 AND YEAR(f.financialdate) = YEAR(CURRENT_DATE)-1 GROUP BY YEAR(f.financialdate), MONTH(f.financialdate)";
  127.         $statement $connection->prepare($lyfinancialspermonthquery);
  128.         $statement->execute();
  129.         $lyfinancialspermonth $statement->fetchAll();
  130.         
  131.         foreach ($lyfinancialspermonth as $key=>$value) {
  132.             $currentmonth $value["Maand"];    
  133.             $lyfinancialspermontharray[$currentmonth-1] = $value["EUR"];
  134.         }        
  135.         #----------------------------------------------------------------------------#
  136.         # FINANCIALS toezeggingen this year
  137.         
  138.         # financials per month array ty = this year
  139.         $tyfinancialspermontharray = [ 00000000000];
  140.         
  141.         $tyfinancialspermonthquery "SELECT MONTH(f.financialdate) Maand, COUNT(*) `Aantal`, SUM(invatprice) 'EUR' FROM `financials` f JOIN `order` o ON f.orderid_id = o.id WHERE f.category_id = 1 AND o.STATUS = 1 AND f.STATUS = 1 AND YEAR(f.financialdate) = YEAR(CURRENT_DATE) GROUP BY YEAR(f.financialdate), MONTH(f.financialdate)";
  142.         $statement $connection->prepare($tyfinancialspermonthquery);
  143.         $statement->execute();
  144.         $tyfinancialspermonth $statement->fetchAll();
  145.         
  146.         foreach ($tyfinancialspermonth as $key=>$value) {
  147.             $currentmonth $value["Maand"];    
  148.             $tyfinancialspermontharray[$currentmonth-1] = $value["EUR"];
  149.         }    
  150.         
  151.         #dump($tyfinancialspermontharray);
  152.         #exit();
  153.         
  154.         #----------------------------------------------------------------------------#
  155.         # ACTIONS    
  156.         
  157.         # get repository via entity manager of actions
  158.         $repoActions $em->getRepository(Action::class);
  159.         # build and execute query to get all actions for the logged in user that are open
  160.         $queryActions $repoActions->createQueryBuilder('a')
  161.             ->select('a.title, a.id, a.duedate, a.status, c.name')
  162.             ->leftjoin('a.orderid','o')
  163.             ->leftjoin('o.customer','c')
  164.             ->where('a.status = 1')
  165.             ->andWhere('a.actionholder = ' $this->getUser()->getId())
  166.             ->andWhere('a.isfinished = false')
  167.             ->orderBy('a.duedate''asc');
  168.         $actions $queryActions->getQuery()->execute();
  169.         
  170.         # build and execute query to get all intime finished actions for the logged in user of this year
  171.         $queryActionsIntime $repoActions->createQueryBuilder('a')
  172.             ->select('count(a.id)')
  173.             ->where('a.status = 1')
  174.             ->andWhere('a.actionholder = ' $this->getUser()->getId())
  175.             ->andWhere('a.duedate >= :startofthisyear')
  176.             ->andWhere('a.duedate < :endofthisyear')
  177.             ->andWhere('a.isfinished = true')
  178.             ->andWhere('a.completedate <= a.duedate')
  179.             ->orderBy('a.duedate','ASC')
  180.             ->setParameter('startofthisyear',$startofthisyear->format('Y-m-d'))
  181.             ->setParameter('endofthisyear',$endofthisyear->format('Y-m-d'));
  182.         $actionsIntimeCount $queryActionsIntime->getQuery()->getSingleScalarResult();
  183.         
  184.         # build and execute query to get all outtime actions for the logged in user of this year
  185.         $queryActionsOuttime $repoActions->createQueryBuilder('a')
  186.             ->select('count(a.id)')
  187.             ->where('a.status = 1')
  188.             ->andWhere('a.actionholder = ' $this->getUser()->getId())
  189.             ->andWhere('a.duedate >= :startofthisyear')
  190.             ->andWhere('a.duedate < :endofthisyear')
  191.             ->andWhere('a.isfinished = true')
  192.             ->andWhere('a.completedate > a.duedate')
  193.             ->setParameter('startofthisyear',$startofthisyear->format('Y-m-d'))
  194.             ->setParameter('endofthisyear',$endofthisyear->format('Y-m-d'));
  195.         $actionsOuttimeCount $queryActionsOuttime->getQuery()->getSingleScalarResult();        
  196.         
  197.         # calculate the ratio between the in time and not in time finished actions by the user of this year
  198.         if ($actionsOuttimeCount OR $actionsIntimeCount) {
  199.             $intimeratio round(100*(($actionsIntimeCount / ($actionsIntimeCount $actionsOuttimeCount))));
  200.         } else {
  201.             $intimeratio 100;
  202.         }
  203.         
  204.         # build and execute query to get all current open actions that are too late
  205.         $queryActionsOpenLateCount $repoActions->createQueryBuilder('a')
  206.             ->select('count(a.id)')
  207.             ->where('a.status = 1')
  208.             ->andWhere('a.actionholder = ' $this->getUser()->getId())
  209.             ->andWhere('a.duedate < :today')
  210.             ->andWhere('a.isfinished = false')
  211.             ->orderBy('a.duedate','ASC')
  212.             ->setParameter('today',$today->format('Y-m-d'));
  213.         $actionsOpenLateCount $queryActionsOpenLateCount->getQuery()->getSingleScalarResult();    
  214.         #----------------------------------------------------------------------------#
  215.         # ACTIONS in category 42 Vergunningaanvraag (ingediend bij gemeente)
  216.         # build and execute query to get all actions for the logged in user that are open
  217.         $queryActionsVergunning $repoActions->createQueryBuilder('a')
  218.             ->select('a.title, a.id, a.duedate, a.status, c.name, c.city')
  219.             ->leftjoin('a.orderid','o')
  220.             ->leftjoin('o.customer','c')
  221.             ->where('a.status = 1')
  222.             ->andWhere('a.category = 42')
  223.             ->andWhere('a.isfinished = false')
  224.             ->orderBy('a.duedate''asc');
  225.         $actionsvergunningsaanvraag $queryActionsVergunning->getQuery()->execute();
  226.         
  227.         #----------------------------------------------------------------------------#
  228.         # ORDERS WITHOUT ACTIONS    
  229.         
  230.         #$queryOrdersnoActions ="SELECT o.id as `id`, COUNT(a.id) - SUM(a.isfinished) as `som` FROM `order` o LEFT JOIN action a ON o.id=a.orderid_id WHERE a.id IS NULL and o.status = 1 OR a.status = 1 and o.status = 1 and o.orderstatus_id = 9 OR o.orderstatus_id = 11 GROUP BY o.id";
  231.         $queryOrdersNoOpenActions ="SELECT o.id as `id`, COUNT(a.id) - SUM(a.isfinished) as `som` FROM `order` o LEFT JOIN action a ON o.id=a.orderid_id WHERE o.status = 1 and a.status = 1 and (o.orderstatus_id = 9 OR o.orderstatus_id = 11) GROUP BY o.id ORDER BY `id` ASC";
  232.         
  233.         $statement $connection->prepare($queryOrdersNoOpenActions);
  234.         $statement->execute();
  235.         $ordersNoOpenActions $statement->fetchAll();
  236.         
  237.         $ordernoactionarray = [];
  238.         $count 0;
  239.     
  240.         foreach ($ordersNoOpenActions as $orderNoOpenAction) {
  241.             
  242.             $sum $orderNoOpenAction["som"];
  243.             
  244.             if ($sum == 0) {
  245.                 
  246.                 $count++;
  247.                 
  248.                 $order $this->getDoctrine()->getRepository(Order::class)->find($orderNoOpenAction["id"]);
  249.                 
  250.                 $ordernoactionarray[$count]["id"] = $order->getId();
  251.                 $ordernoactionarray[$count]["customer"] = $order->getCustomer()->getName();
  252.                 $ordernoactionarray[$count]["city"] = $order->getCustomer()->getCity();
  253.                 $ordernoactionarray[$count]["ordernumber"] = $order->getOrdernumber();
  254.             }
  255.         }
  256.         
  257.         # find orders without actions
  258.         $queryOrdersNoActions ="SELECT o.id as `id`, COUNT(a.id) as `som` FROM `order` o LEFT JOIN action a ON o.id=a.orderid_id WHERE o.status = 1 and (o.orderstatus_id = 9 OR o.orderstatus_id = 11) GROUP BY o.id ORDER BY `id` ASC";
  259.         
  260.         $statement $connection->prepare($queryOrdersNoActions);
  261.         $statement->execute();
  262.         $ordersNoActions $statement->fetchAll();
  263.         
  264.         foreach ($ordersNoActions as $ordersNoAction) {
  265.             
  266.             $sum $ordersNoAction["som"];
  267.             
  268.             if ($sum == 0) {
  269.                 
  270.                 $count++;
  271.                 
  272.                 $order $this->getDoctrine()->getRepository(Order::class)->find($ordersNoAction["id"]);
  273.                 
  274.                 $ordernoactionarray[$count]["id"] = $order->getId();
  275.                 $ordernoactionarray[$count]["customer"] = $order->getCustomer()->getName();
  276.                 $ordernoactionarray[$count]["city"] = $order->getCustomer()->getCity();
  277.                 $ordernoactionarray[$count]["ordernumber"] = $order->getOrdernumber();
  278.             }
  279.         }
  280.         
  281.         #----------------------------------------------------------------------------#
  282.         # ORDERS IN NAZORG [LEGACY]
  283.         
  284.         # get repository via the entity manager
  285.         #$repoOrders = $em->getRepository(Order::class);
  286.         
  287.         # build and execute query to get all orders in nazorg
  288.         #$queryordersinnazorg = $repoOrders->createQueryBuilder('o')
  289.         #    ->select('o.id','o.ordernumber','c.name','c.city')
  290.         #    ->leftjoin('o.customer','c')
  291.         #    ->where('o.status = 1')
  292.         #    ->andWhere('o.orderstatus = 11')
  293.         #    ->orderBy('o.ordernumber', 'asc');        
  294.             
  295.         #$ordersinnazorg = $queryordersinnazorg->getQuery()->execute();
  296.         #----------------------------------------------------------------------------#
  297.         # ACTIONS in category XX Nazorg
  298.         
  299.         $actionCategoryNazorgIds $em->getRepository(Settings::class)->findOneBy(['name' => 'defaultnazorgcategory'])->getValue();
  300.         
  301.         // Zorg ervoor dat de waarde een array wordt, bijvoorbeeld als er meerdere IDs in een string staan
  302.         $actionCategoryNazorgIds explode(','$actionCategoryNazorgIds);
  303.         $actionCategoryNazorgIds array_map('trim'$actionCategoryNazorgIds); // Spaties verwijderen
  304.         $queryActionsNazorg $repoActions->createQueryBuilder('a')
  305.         ->select('a.title, a.id, a.duedate, a.status, c.name, c.city')
  306.         ->leftJoin('a.orderid''o')
  307.         ->leftJoin('o.customer''c')
  308.         ->where('a.status = 1')
  309.         ->andWhere('a.category IN (:actionCategoryNazorgIds)')
  310.         ->andWhere('a.isfinished = false')
  311.         ->setParameter('actionCategoryNazorgIds'$actionCategoryNazorgIds)
  312.         ->orderBy('a.duedate''asc');
  313.     
  314.         $actionsnazorg $queryActionsNazorg->getQuery()->execute();
  315.         #----------------------------------------------------------------------------#
  316.         # RETURN    
  317.         
  318.         #echo "<pre>";
  319.         #print_r($colorlist);
  320.         #echo "<br />";
  321.         #print_r($orderspermontharray);
  322.         #exit();
  323.         
  324.         return $this->render('dashboard/index.html.twig', [
  325.             'user' => $this->getUser(),                                    # current logged in user
  326.             'actions' => $actions,                                        # object actions
  327.             'vergunningsaanvragen' => $actionsvergunningsaanvraag,         # object actions in specific category
  328.             'appointments' => $appointments,                            # object appointment
  329.             'appointmentscount' => $appointmentscount,                    # integer with number of appointments
  330.             'orderspermontharray' => $orderspermontharray,                # array with the number of orders for each month
  331.             'lyfinancialspermontharray' => $lyfinancialspermontharray,    # array with the sum of toezeggingen for each month for last year
  332.             'tyfinancialspermontharray' => $tyfinancialspermontharray,    # array with the sum of toezeggingen for each month for this year
  333.             'actionsopenlatecount' => $actionsOpenLateCount,            # integer with number of open actions that are late
  334.             'intimeratio' => $intimeratio,                                # double with intimeratio
  335.             'userlist' => $userlist,                                    # array with users for pie chart
  336.             'useractionlist' => $useractionlist,                        # array with number of actions for pie chart
  337.             'colorlist' => $colorlist,                                    # array with color HEX for pie chart
  338.             'ordernoaction' => $ordernoactionarray,                        # array with orders with no open actions
  339.             'nazorgacties' => $actionsnazorg,                            # object actions in specific category
  340.             #'ordersinnazorg' => $ordersinnazorg,                        # objects orders in nazorg [LEGACY]
  341.         ]);
  342.     }
  343.     
  344.     /**
  345.      * @Route("/changelog", name="changelog")
  346.      */
  347.     public function changelog(): Response
  348.     {
  349.         return $this->render('dashboard/changelog.html.twig', [
  350.         ]);
  351.     }
  352.     
  353.     /**
  354.      * @Route("/phpinfo", name="phpinfo")
  355.      */
  356.     public function phpinfo(): Response
  357.     {
  358.         phpinfo();
  359.         exit();
  360.     }
  361. }