src/Flexy/ShopBundle/Service/FlexyShopStatisticProvider.php line 830

  1. <?php
  2. namespace App\Flexy\ShopBundle\Service;
  3. use App\Entity\Settings;
  4. use App\Flexy\ShopBundle\Entity\Order\CashBoxOrder;
  5. use App\Flexy\ShopBundle\Entity\Order\Order;
  6. use App\Flexy\ShopBundle\Entity\Order\OrderItem;
  7. use App\Flexy\ShopBundle\Entity\Product\CategoryProduct;
  8. use App\Flexy\ShopBundle\Entity\Product\Product;
  9. use App\Flexy\ShopBundle\Entity\Promotion\Coupon;
  10. use App\Flexy\ShopBundle\Entity\Store\Store;
  11. use App\Service\FlexySettingsProvider;
  12. use DateTime;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. use Symfony\UX\Chartjs\Builder\ChartBuilderInterface;
  16. use Symfony\UX\Chartjs\Model\Chart;
  17. class FlexyShopStatisticProvider{
  18.     public function __construct(
  19.          private readonly EntityManagerInterface $em,
  20.          private readonly RequestStack $requestStack,
  21.          private readonly FlexySettingsProvider $flexySettingsProvider,
  22.         private readonly ChartBuilderInterface $chartBuilderInterface
  23.          )
  24.     {
  25.     }
  26.     public $chartColors = ['rgba(138,97,255,1)''rgb(209, 67, 188)''#d68d2e','#499bce','#f4d33e','#52ae1d'];
  27.     public function quickStats($store=null,$year=null,$month=null){
  28.         // ByStore,ByDateInterval,
  29.         $lastWeek = new DateTime('last week this month');
  30.         $lastMonth = new DateTime('first day of this month');
  31.     
  32.         if($lastWeek $lastMonth){
  33.             
  34.             $lastMonth = clone $lastWeek;
  35.             $lastMonth->modify('first day of this month');
  36.             
  37.         }
  38.         
  39.         
  40.         if($year and $month){
  41.             $lastWeek = new DateTime($year."-".$month);
  42.             $lastMonth = new DateTime($year."-".$month);
  43.             
  44.             $lastWeek->modify("last week this month");
  45.             $lastMonth->modify("first day of this month");
  46.             
  47.             
  48.         }
  49.         $queryCustomers $this->em->createQuery(
  50.             'SELECT COUNT(c) as all,
  51.                    SUM(CASE WHEN c.createdAt >= :last_week THEN 1 ELSE 0 END) as last_week,
  52.                    SUM(CASE WHEN c.createdAt >= :last_month THEN 1 ELSE 0 END) as last_month
  53.             FROM App\Flexy\ShopBundle\Entity\Customer\Customer c
  54.             WHERE (c.store = :store OR :store IS NULL)
  55.             AND (YEAR(c.createdAt) = :year OR :year IS NULL)
  56.             AND c.gender IS NOT NULL
  57.             AND c.dateOfBirth IS NOT NULL
  58.             AND (MONTH(c.createdAt) = :month OR :month IS NULL)
  59.         ');
  60.         $queryCustomers
  61.         ->setParameter('store'$store)
  62.         ->setParameter('year'$year)
  63.         ->setParameter('month'$month)
  64.         ->setParameter('last_week'$lastWeek)
  65.         ->setParameter('last_month'$lastMonth); 
  66.         $statsCustomers  $queryCustomers->getSingleResult();
  67.         //
  68.         
  69.         
  70.         //
  71.         $queryCustomersMale $this->em->createQuery(
  72.             'SELECT COUNT(c) as all,
  73.                    SUM(CASE WHEN c.createdAt >= :last_week THEN 1 ELSE 0 END) as last_week,
  74.                    SUM(CASE WHEN c.createdAt >= :last_month THEN 1 ELSE 0 END) as last_month
  75.             FROM App\Flexy\ShopBundle\Entity\Customer\Customer c
  76.             WHERE (YEAR(c.createdAt) = :year OR :year IS NULL)
  77.             AND (MONTH(c.createdAt) = :month OR :month IS NULL)
  78.             AND c.dateOfBirth <= :min_date_of_birth
  79.             AND c.gender = :gender'
  80.             
  81.               
  82.         );
  83.         
  84.         /* (c.store = :store OR :store IS NULL)
  85.             AND*/
  86.         
  87.         $queryCustomersMale
  88.             //->setParameter('store', $store)
  89.             ->setParameter('year'$year)
  90.             ->setParameter('month'$month)
  91.             ->setParameter('last_week'$lastWeek)
  92.             ->setParameter('last_month'$lastMonth)
  93.            ->setParameter('min_date_of_birth', new \DateTime('2008-01-01'))
  94.             ->setParameter('gender''male');  // Valeur du genre à "male"
  95.         
  96.         $statsCustomersMale  $queryCustomersMale->getSingleResult();
  97.         
  98.         //
  99.          //
  100.          $queryCustomersFemale $this->em->createQuery(
  101.             'SELECT COUNT(c) as all,
  102.                    SUM(CASE WHEN c.createdAt >= :last_week THEN 1 ELSE 0 END) as last_week,
  103.                    SUM(CASE WHEN c.createdAt >= :last_month THEN 1 ELSE 0 END) as last_month
  104.             FROM App\Flexy\ShopBundle\Entity\Customer\Customer c
  105.             WHERE (YEAR(c.createdAt) = :year OR :year IS NULL)
  106.             AND (MONTH(c.createdAt) = :month OR :month IS NULL)
  107.             AND c.dateOfBirth <= :min_date_of_birth
  108.             AND c.gender = :gender'
  109.             
  110.         );
  111.         
  112.         /* (c.store = :store OR :store IS NULL)
  113.             AND*/
  114.         $queryCustomersFemale
  115.             //->setParameter('store', $store)
  116.             ->setParameter('year'$year)
  117.             ->setParameter('month'$month)
  118.             ->setParameter('last_week'$lastWeek)
  119.             ->setParameter('last_month'$lastMonth)
  120.             ->setParameter('min_date_of_birth', new \DateTime('2008-01-01'))
  121.             ->setParameter('gender''female');  // Valeur du genre à "male"
  122.         
  123.         $statsCustomersFemale  $queryCustomersFemale->getSingleResult();
  124.         
  125.         //
  126.           
  127.       
  128.        /*$queryCustomersNoProductSubscription = $this->em->createQuery(
  129.            
  130.            'SELECT COUNT(c) as all,
  131.                    SUM(CASE WHEN c.createdAt >= :last_week THEN 1 ELSE 0 END) as last_week,
  132.                    SUM(CASE WHEN c.createdAt >= :last_month THEN 1 ELSE 0 END) as last_month
  133.             FROM App\Flexy\ShopBundle\Entity\Customer\Customer c
  134.             LEFT JOIN App\Flexy\ShopBundle\Entity\Product\ProductSubscription ps WITH ps.customer = c
  135.             WHERE (c.store = :store OR :store IS NULL)
  136.             AND (YEAR(c.createdAt) = :year OR :year IS NULL)
  137.             AND c.gender IS NOT NULL
  138.             AND c.dateOfBirth IS NOT NULL
  139.             AND (MONTH(c.createdAt) = :month OR :month IS NULL)
  140.             AND ps.customer IS NULL'  // Filtre pour les clients sans souscription
  141.             
  142.  
  143.         );*/
  144.         
  145.          $queryCustomersWithTwoOrders $this->em->createQuery(
  146.             'SELECT COUNT(c.id) AS all
  147.      FROM App\Flexy\ShopBundle\Entity\Customer\Customer c
  148.      WHERE
  149.       (YEAR(c.createdAt) = :year OR :year IS NULL)
  150.                AND (MONTH( c.createdAt) = :month OR :month IS NULL)
  151.                AND (
  152.          SELECT COUNT(o.id)
  153.          FROM App\Flexy\ShopBundle\Entity\Order\Order o
  154.          WHERE  o.customer = c
  155.            AND NOT EXISTS (
  156.                SELECT o.id
  157.                FROM App\Flexy\ShopBundle\Entity\Order\OrderItem oi
  158.                JOIN oi.product p
  159.                WHERE oi.parentOrder = o AND p.name IN (:excludedNames)
  160.            )
  161.      ) > 1'
  162.         );
  163.         
  164.         $queryCustomersWithTwoOrders
  165.         ->setParameter('excludedNames', ['Réedition Carte''Entre journalier'])
  166.         ->setParameter('year'$year)
  167.         ->setParameter('month'$month);
  168.         $statsCustomersWithTwoOrders $queryCustomersWithTwoOrders->getSingleResult();
  169.         //
  170.         $queryCustomersNoProductSubscription $this->em->createQuery(
  171.             'SELECT COUNT(c) as all,
  172.                    SUM(CASE WHEN c.createdAt >= :last_week THEN 1 ELSE 0 END) as last_week,
  173.                    SUM(CASE WHEN c.createdAt >= :last_month THEN 1 ELSE 0 END) as last_month
  174.             FROM App\Flexy\ShopBundle\Entity\Customer\Customer c
  175.             LEFT JOIN App\Flexy\ShopBundle\Entity\Order\Order o WITH o.customer = c
  176.             LEFT JOIN App\Flexy\ShopBundle\Entity\Order\OrderItem item WITH item.parentOrder = o
  177.    
  178.         
  179.             WHERE (c.store = :store OR :store IS NULL)
  180.             AND (YEAR(c.createdAt) = :year OR :year IS NULL)
  181.              
  182.             
  183.             AND (MONTH(c.createdAt) = :month OR :month IS NULL)
  184.             AND item.parentOrder  IS NULL'  // Filtre pour les clients sans souscription
  185.         );
  186.                 
  187.         $queryCustomersNoProductSubscription
  188.             ->setParameter('store'$store)
  189.             ->setParameter('year'$year)
  190.             ->setParameter('month'$month)
  191.             ->setParameter('last_week'$lastWeek)
  192.             ->setParameter('last_month'$lastMonth);
  193.         
  194.         $statsCustomersNoProductSubscription $queryCustomersNoProductSubscription->getSingleResult();
  195.         
  196.         
  197.         
  198.          //
  199.            $queryCustomerskids $this->em->createQuery(
  200.             'SELECT COUNT(c) as all,
  201.                    SUM(CASE WHEN c.createdAt >= :last_week THEN 1 ELSE 0 END) as last_week,
  202.                    SUM(CASE WHEN c.createdAt >= :last_month THEN 1 ELSE 0 END) as last_month
  203.             FROM App\Flexy\ShopBundle\Entity\Customer\Customer c
  204.             WHERE (YEAR(c.createdAt) = :year OR :year IS NULL)
  205.             AND (MONTH(c.createdAt) = :month OR :month IS NULL)
  206.             AND c.dateOfBirth >= :min_date_of_birth
  207.             AND c.gender IN (:genders)  '  // Condition pour inclure les genres male et female
  208.         );
  209.         /* (c.store = :store OR :store IS NULL)
  210.             AND*/
  211.         
  212.         $queryCustomerskids
  213.             //->setParameter('store', $store)
  214.             ->setParameter('year'$year)
  215.             ->setParameter('month'$month)
  216.             ->setParameter('last_week'$lastWeek)
  217.             ->setParameter('last_month'$lastMonth)
  218.             ->setParameter('min_date_of_birth', new \DateTime('2008-01-01'))  // Date minimale : 1er janvier 2008
  219.             ->setParameter('genders', ['male''female']);  // Filtrer par genres "male" ou "female"
  220.         
  221.         $statsCustomerskids  $queryCustomerskids->getSingleResult();
  222.         //
  223.         
  224.         $querySubscriptions $this->em->createQuery(
  225.             'SELECT COUNT(ps) as all,
  226.                    SUM(CASE WHEN ps.createdAt >= :last_week THEN 1 ELSE 0 END) as last_week,
  227.                    SUM(CASE WHEN ps.createdAt >= :last_month THEN 1 ELSE 0 END) as last_month
  228.             FROM App\Flexy\ShopBundle\Entity\Product\ProductSubscription ps
  229.             WHERE (YEAR(ps.createdAt) = :year OR :year IS NULL)
  230.             AND (MONTH(ps.createdAt) = :month OR :month IS NULL)
  231.         ');
  232.         $querySubscriptions
  233.         ->setParameter('year'$year)
  234.         ->setParameter('month'$month)
  235.         ->setParameter('last_week'$lastWeek)
  236.         ->setParameter('last_month'$lastMonth); 
  237.         
  238.         $statsSubscription  $querySubscriptions->getSingleResult();
  239.         
  240.         
  241.         $quickStats = [
  242.             "revenue"=>[
  243.                 "all"=>(float)$this->getRevenue($store,$year,$month)["all"],
  244.                 "new"=>'',/*(float)$this->getRevenue($store,$year,$month)["last_week"]*/
  245.                 "old"=>'',
  246.                 "unit"=>$this->flexySettingsProvider->get()->getCurrency(),
  247.                 "icon"=>'<i class="fa-solid fa-comment-dollar"></i>',
  248.                 "color"=>"purple"
  249.             ],
  250.             "customers"=>[
  251.                 "all"=>$statsCustomers['all'],
  252.                 "new"=>$statsCustomers['last_week'],
  253.                 "old"=> $statsCustomers['last_month'],
  254.                 "unit"=>null,
  255.                 "icon"=>'<i class="fa-solid fa-people-group"></i>',
  256.                 "color"=>"pink-dark"
  257.             ],
  258.              "Adhérents hommes"=>[
  259.                 "all"=>$statsCustomersMale['all'],
  260.                 "new"=>$statsCustomersMale['last_week'],
  261.                 "old"=> $statsCustomersMale['last_month'],
  262.                 "unit"=>null,
  263.                 "icon"=>'<i class="fa-solid fa-person"></i>',
  264.                 "color"=>"pink-dark"
  265.             ],
  266.             "Adhérentes femmes"=>[
  267.                 "all"=>$statsCustomersFemale['all'],
  268.                 "new"=>$statsCustomersFemale['last_week'],
  269.                 "old"=> $statsCustomersFemale['last_month'],
  270.                 "unit"=>null,
  271.                 "icon"=>'<i class="fa-solid fa-person-dress"></i>',
  272.                 "color"=>"pink-dark"
  273.             ],
  274.             "Adhérents Enfants"=>[
  275.                 "all"=>$statsCustomerskids['all'],
  276.                 "new"=>$statsCustomerskids['last_week'],
  277.                 "old"=> $statsCustomerskids['last_month'],
  278.                 "unit"=>null,
  279.                 "icon"=>'<i class="fa-solid fa-child"></i>',
  280.                 "color"=>"pink-dark"
  281.             ],
  282.              "Visiteurs non inscrits"=>[
  283.                 "all"=>$statsCustomersNoProductSubscription['all'],
  284.                 "new"=>$statsCustomersNoProductSubscription['last_week'],
  285.                 "old"=> $statsCustomersNoProductSubscription['last_month'],
  286.                 "unit"=>null,
  287.                 "icon"=>'<i class="fa-solid fa-people-group"></i>',
  288.                 "color"=>"pink-dark"
  289.             ],
  290.             /*"waitingOrders"=>[
  291.                 "all"=>$this->statsOrdersByStatus("waiting",$store,$year,$month)["all"],
  292.                 "new"=>$this->statsOrdersByStatus("waiting",$store,$year,$month)["last_week"],
  293.                 "old"=>$this->statsOrdersByStatus("waiting",$store,$year,$month)["last_month"],
  294.                 "unit"=>null,
  295.                 "icon"=>'<i class="fa-solid fa-file-excel"></i>',
  296.                 "color"=>"orange-dark"
  297.             ],*/
  298.             "contrats renouvelables "=>[
  299.                 "all"=>$statsCustomersWithTwoOrders['all'],
  300.                 "new"=>'',
  301.                 "old"=> '',
  302.                 "unit"=>null,
  303.                 "icon"=>'<i class="fa-solid fa-file-circle-plus"></i>',
  304.                 "color"=>"pink-dark"
  305.             ],
  306.             "Contrats annulés"=>[
  307.                 "all"=>$this->statsOrdersByStatus("canceled",$store,$year,$month)["all"],
  308.                 "new"=>$this->statsOrdersByStatus("canceled",$store,$year,$month)["last_week"],
  309.                 "old"=>$this->statsOrdersByStatus("canceled",$store,$year,$month)["last_month"],
  310.                 "unit"=>null,
  311.                 "icon"=>'<i class="fa-regular fa-file-lines"></i>',
  312.                 "color"=>"blue-dark"
  313.             ],
  314.             
  315.             /*"completedOrders"=>[
  316.                 "all"=>$this->statsOrdersByStatus("payed",$store,$year,$month)["all"],
  317.                 "new"=>$this->statsOrdersByStatus("payed",$store,$year,$month)["last_week"],
  318.                 "old"=>$this->statsOrdersByStatus("payed",$store,$year,$month)["last_month"],
  319.                 "unit"=>null,
  320.                 "icon"=>'<i class="fa-solid fa-clipboard-check"></i>',
  321.                 "color"=>"green-dark"
  322.             ],*/
  323.            
  324.             
  325.             "subscriptions"=>[
  326.                 "all"=>$statsSubscription["all"],
  327.                 "new"=>$statsSubscription["last_week"],
  328.                 "old"=>$statsSubscription["last_month"],
  329.                 "unit"=>null,
  330.                 "icon"=>'<i class="fa-solid fa-link"></i>',
  331.                 "color"=>"purple-dark"
  332.             ],
  333.             
  334.             
  335.             ];
  336.         return $quickStats;
  337.     }
  338.     public function getRevenue($store=null,$year=null,$month=null)
  339. {
  340.     $lastWeek = new DateTime('last week this month');
  341.     $lastMonth = new DateTime('first day of this month');
  342.     if($lastWeek $lastMonth){
  343.         
  344.         $lastMonth = clone $lastWeek;
  345.         $lastMonth->modify('first day of this month');
  346.         
  347.     }
  348.     
  349.     
  350.     if($year and $month){
  351.         $lastWeek = new DateTime($year."-".$month);
  352.         $lastMonth = new DateTime($year."-".$month);
  353.         
  354.         $lastWeek->modify("last week this month");
  355.         $lastMonth->modify("first day of this month");
  356.         
  357.         
  358.     }
  359.     //SELECT SUM(oi.quantity * oi.price) as all,
  360.     /*$query = $this->em->createQuery(
  361.         'SELECT 
  362.           SUM(payments.amount) as all,
  363.         SUM(CASE WHEN cashBoxOrder.startAt BETWEEN :lastWeek AND :now THEN payments.amount ELSE 0 END) as last_week,
  364.         SUM(CASE WHEN cashBoxOrder.startAt BETWEEN :lastMonth AND :now THEN payments.amount ELSE 0 END) as last_month,
  365.         SUM(CASE WHEN cashBoxOrder.startAt BETWEEN :today and :tomorrow  THEN payments.amount ELSE 0 END) as today,
  366.         SUM(CASE WHEN cashBoxOrder.startAt BETWEEN :yesterday and :today  THEN payments.amount ELSE 0 END) as yesterday
  367.          FROM App\Flexy\ShopBundle\Entity\Order\Order o
  368.          JOIN o.payments payments
  369.          JOIN o.cashBoxOrder cashBoxOrder
  370.             WHERE (o.store = :store OR :store IS NULL)
  371.             AND cashBoxOrder IS NOT NULL
  372.             
  373.             AND (YEAR(cashBoxOrder.startAt) = :year OR :year IS NULL)
  374.             AND (MONTH(cashBoxOrder.startAt) = :month OR :month IS NULL)
  375.          '
  376.     )->setParameters([
  377.         
  378.         'today' => new \DateTime('today'),
  379.         'yesterday' => new \DateTime('yesterday'),
  380.         'tomorrow' => new \DateTime('tomorrow'),
  381.         'lastWeek' => $lastWeek,
  382.         'lastMonth' => $lastMonth,
  383.         'now' => new \DateTime(),
  384.         'store'=>$store,
  385.         'year'=>$year,
  386.         'month'=>$month
  387.     ]);
  388.     $revenue = $query->getSingleResult();*/
  389.     
  390.     //SELECT SUM(oi.quantity * oi.price) as all,
  391.     $query $this->em->createQuery(
  392.         'SELECT 
  393.             SUM(payments.amount) as all,
  394.             SUM(CASE WHEN cashBoxOrder.startAt BETWEEN :lastWeek AND :now THEN payments.amount ELSE 0 END) as last_week,
  395.             SUM(CASE WHEN cashBoxOrder.startAt BETWEEN :lastMonth AND :now THEN payments.amount ELSE 0 END) as last_month,
  396.             SUM(CASE WHEN cashBoxOrder.startAt BETWEEN :today AND :tomorrow THEN payments.amount ELSE 0 END) as today,
  397.             SUM(CASE WHEN cashBoxOrder.startAt BETWEEN :yesterday AND :yesterdayEnd THEN payments.amount ELSE 0 END) as yesterday,
  398.             SUM(CASE WHEN cashBoxOrder.startAt BETWEEN :firstDayOfMonth AND :now THEN payments.amount ELSE 0 END) as current_month
  399.          FROM App\Flexy\ShopBundle\Entity\Order\Order o
  400.          JOIN o.payments payments
  401.          JOIN o.cashBoxOrder cashBoxOrder
  402.             WHERE (o.store = :store OR :store IS NULL)
  403.             AND cashBoxOrder IS NOT NULL
  404.             AND (YEAR(cashBoxOrder.startAt) = :year OR :year IS NULL)
  405.             AND (MONTH(cashBoxOrder.startAt) = :month OR :month IS NULL)'
  406.     )->setParameters([
  407.         'today' => new \DateTime('today'),
  408.         'tomorrow' => new \DateTime('tomorrow'),
  409.         'yesterday' => new \DateTime('yesterday'),
  410.         'yesterdayEnd' => (new \DateTime('today'))->modify('-1 second'),
  411.         'firstDayOfMonth' => (new \DateTime('first day of this month'))->setTime(00),  // Le début du mois courant à 00:00:00
  412.         'lastWeek' => $lastWeek,
  413.         'lastMonth' => $lastMonth,
  414.         'now' => new \DateTime(),
  415.         'store' => $store,
  416.         'year' => $year,
  417.         'month' => $month
  418.     ]);
  419.     
  420.     $revenue $query->getSingleResult();
  421.     return $revenue;
  422.      
  423. }
  424. public function getAverageRevenuePerDay($period="day",$details false)
  425.     {
  426.         $query $this->em->createQuery(
  427.             'SELECT DATE(o.createdAt) as period,
  428.             AVG(i.price * i.quantity) as averageRevenue
  429.      FROM App\Flexy\ShopBundle\Entity\Order\OrderItem i
  430.      JOIN i.parentOrder o
  431.      GROUP BY period
  432.         ');
  433. if($period == "week"){
  434.     $query $this->em->createQuery(
  435.         'SELECT YEARWEEK(o.createdAt) as period,
  436.         AVG(i.price * i.quantity) as averageRevenue
  437.     FROM App\Flexy\ShopBundle\Entity\Order\OrderItem i
  438.     JOIN i.parentOrder o
  439.     GROUP BY period
  440.     ');
  441. }elseif($period == "month"){
  442.     $query $this->em->createQuery(
  443.         "SELECT DATE_FORMAT(o.createdAt, '%Y-%m') as period,
  444.         AVG(i.price * i.quantity) as averageRevenue
  445.     FROM App\Flexy\ShopBundle\Entity\Order\OrderItem i
  446.     JOIN i.parentOrder o
  447.     GROUP BY period
  448.     ");
  449. }
  450.         $results $query->getResult();
  451.         if($details==true){
  452.             return $results;
  453.         }
  454.         
  455.         $totalRevenue 0;
  456.         foreach($results as $singleResult){
  457.             
  458.             $totalRevenue $totalRevenue $singleResult["averageRevenue"];
  459.         }
  460.         $revenue 0;
  461.         if(count($results) > 0){
  462.             $revenue $totalRevenue count($results);
  463.         }
  464.         return  $revenue;
  465.     }
  466. public function getRevenueByYear($store=null,$year=null)
  467. {
  468.     
  469.   $entityManager $this->em;
  470.     $subquery $entityManager->createQuery(
  471.         'SELECT o.id
  472.          FROM App\Flexy\ShopBundle\Entity\Order\Order o
  473.          WHERE o.status != :status
  474.          AND (YEAR(o.createdAt) = :year OR :year IS NULL )'
  475.     )->setParameters([
  476.         'status' => 'canceled',
  477.         'year' => $year,
  478.     ]);
  479.     $query $entityManager->createQuery(
  480.         'SELECT MONTH(orderEntity.createdAt) as month, YEAR(orderEntity.createdAt) as year,
  481.         SUM(oi.quantity * oi.price) as revenue
  482.          FROM App\Flexy\ShopBundle\Entity\Order\OrderItem oi
  483.          JOIN oi.parentOrder orderEntity
  484.          WHERE orderEntity.id IN (' $subquery->getDQL() . ')
  485.                   AND orderEntity.status != :status
  486.                   AND (YEAR(orderEntity.createdAt) = :year OR :year IS NULL ) 
  487.                   AND (orderEntity.store = :store OR :store IS NULL)
  488.          GROUP BY month, year'
  489.     )->setParameters([
  490.         'status' => 'canceled',
  491.         'year' => $year,
  492.         'store' => $store,
  493.     ]);
  494.     $revenue $query->getResult();
  495.     // Format month and year as strings
  496.     foreach ($revenue as $key => $value) {
  497.         // Vérifiez que les clés 'month' et 'year' existent avant de les utiliser
  498.         if (isset($value['month']) && isset($value['year'])) {
  499.             $month DateTime::createFromFormat('!m'$value['month'])->format('F');
  500.             $year $value['year'];
  501.             $revenue[$key]['monthYear'] = $month " " $year;
  502.             unset($revenue[$key]['month']);
  503.             unset($revenue[$key]['year']);
  504.         } else {
  505.             // Gérer les cas où les clés 'month' ou 'year' sont manquantes
  506.             throw new \Exception("Les clés 'month' ou 'year' sont manquantes dans les résultats de la requête.");
  507.         }
  508.     }
  509.     return $revenue;
  510. }
  511. public function getRevenueByMonth($store=null,$year=null,$month=null)
  512. {
  513.     $entityManager $this->em;
  514.     $subquery $entityManager->createQuery(
  515.         'SELECT o.id
  516.          FROM App\Flexy\ShopBundle\Entity\Order\Order o
  517.          WHERE o.status != :status
  518.          AND YEAR(o.createdAt) = :year
  519.          AND MONTH(o.createdAt) = :month'
  520.     )->setParameters([
  521.         'status' => 'canceled',
  522.         'year' => $year ?? date('Y'),
  523.         'month' => $month ?? date('m'),
  524.     ]);
  525.     $query $entityManager->createQuery(
  526.         'SELECT orderEntity.createdAt as date, SUM(oi.quantity * oi.price) as revenue
  527.          FROM App\Flexy\ShopBundle\Entity\Order\OrderItem oi
  528.          JOIN oi.parentOrder orderEntity
  529.          WHERE orderEntity.id IN (' $subquery->getDQL() . ')
  530.                   AND orderEntity.status != :status
  531.                   AND YEAR(orderEntity.createdAt) = :year
  532.                   AND MONTH(orderEntity.createdAt) = :month
  533.                   AND (orderEntity.store = :store OR :store IS NULL)
  534.          GROUP BY date'
  535.     )->setParameters([
  536.         'status' => 'canceled',
  537.         'year' => $year ?? date('Y'),
  538.         'month' => $month ?? date('m'),
  539.         'store' => $store,
  540.     ]);
  541.     $revenue $query->getResult();
  542.     // Format date as string
  543.     foreach ($revenue as $key => $value) {
  544.         $date $value['date'];
  545.         $revenue[$key]['date'] = $date->format('Y-m-d');
  546.     }
  547.     return $revenue;
  548. }
  549.     public function statsOrderStatus($store=null,$year=null,$month=null){
  550.         $query "SELECT 
  551.         COUNT(DISTINCT o.id) as all,
  552.         
  553.         SUM(CASE WHEN EXISTS(SELECT stypeCollect FROM App\Flexy\ShopBundle\Entity\Shipping\StepType stypeCollect WHERE CONCAT(',', stypeCollect.events, ',') LIKE '%collected%' AND ts.stepType = stypeCollect) THEN 1 ELSE 0 END) as collecting,
  554.         
  555.         SUM(CASE WHEN EXISTS(SELECT stypeProcessing FROM App\Flexy\ShopBundle\Entity\Shipping\StepType stypeProcessing WHERE CONCAT(',', stypeProcessing.events, ',') LIKE '%processed%' AND ts.stepType = stypeProcessing) THEN 1 ELSE 0 END) as processing,
  556.         SUM(CASE WHEN EXISTS(SELECT stypeShipping FROM App\Flexy\ShopBundle\Entity\Shipping\StepType stypeShipping WHERE CONCAT(',', stypeShipping.events, ',') LIKE '%shipped%' AND ts.stepType = stypeShipping) THEN 1 ELSE 0 END) as shipping,
  557.         SUM(CASE WHEN o.status = 'paid' THEN 1 ELSE 0 END) as paid,
  558.         ROUND( ( SUM(CASE WHEN EXISTS(SELECT stypeCollectPercent FROM App\Flexy\ShopBundle\Entity\Shipping\StepType stypeCollectPercent WHERE CONCAT(',', stypeCollectPercent.events, ',') LIKE '%collected%' AND ts.stepType = stypeCollectPercent) THEN 1 ELSE 0 END) / COUNT(DISTINCT o.id)) * 100) as percentage_collecting,
  559.         ROUND( ( SUM(CASE WHEN EXISTS(SELECT stypeProcessingPercent FROM App\Flexy\ShopBundle\Entity\Shipping\StepType stypeProcessingPercent WHERE CONCAT(',', stypeProcessingPercent.events, ',') LIKE '%processed%' AND ts.stepType = stypeProcessingPercent) THEN 1 ELSE 0 END) / COUNT(DISTINCT o.id)) * 100) as percentage_processing,
  560.         ROUND( ( SUM(CASE WHEN EXISTS(SELECT stypeShippingPercent FROM App\Flexy\ShopBundle\Entity\Shipping\StepType stypeShippingPercent WHERE CONCAT(',', stypeShippingPercent.events, ',') LIKE '%shipped%' AND ts.stepType = stypeShippingPercent) THEN 1 ELSE 0 END) / COUNT(DISTINCT o.id)) * 100) as percentage_shipping,
  561.         ROUND( ( SUM(CASE WHEN o.status = 'paid' THEN 1 ELSE 0 END) / COUNT(DISTINCT o.id)) * 100) as percentage_paid
  562.   
  563.         
  564.         
  565.       FROM App\Flexy\ShopBundle\Entity\Shipping\Shipment s
  566.       JOIN s.relatedOrder o
  567.       LEFT JOIN s.trackingSteps ts
  568.       WHERE s.id IS NOT NULL
  569.       AND o.status != :status 
  570.       AND (o.store = :store OR :store IS NULL)
  571.       AND (YEAR(o.createdAt) = :year OR :year IS NULL)
  572.       AND (MONTH(o.createdAt) = :month OR :month IS NULL)
  573.       ";
  574.         $queryBuilder $this->em->createQuery($query)->setParameters([
  575.             "status"=>"canceled",
  576.             "store"=>$store,
  577.             "year"=>$year,
  578.             "month"=>$month
  579.         ]);
  580.         $statsOrdersStatus $queryBuilder->getResult();
  581.         return $statsOrdersStatus;
  582.     }
  583.     public function statsOrdersByStatus($status="canceled",$store=null,$year=null,$month=null){
  584.         $queryOrders $this->em->createQuery(
  585.             'SELECT COUNT(o) as all,
  586.                SUM(CASE WHEN o.createdAt >= :last_week THEN 1 ELSE 0 END) as last_week,
  587.                SUM(CASE WHEN o.createdAt >= :last_month THEN 1 ELSE 0 END) as last_month
  588.         FROM App\Flexy\ShopBundle\Entity\Order\Order o
  589.         WHERE o.status = :status 
  590.         AND (o.store = :store OR :store IS NULL)
  591.         AND (YEAR(o.createdAt) = :year OR :year IS NULL)
  592.         AND (MONTH(o.createdAt) = :month OR :month IS NULL)
  593.     ');
  594.         $lastWeek = new DateTime('last week this month');
  595.         $lastMonth = new DateTime('first day of this month');
  596.         if($lastWeek $lastMonth){
  597.         
  598.             $lastMonth = clone $lastWeek;
  599.             $lastMonth->modify('first day of this month');
  600.             
  601.         }
  602.         
  603.         if($year and $month){
  604.             $lastWeek = new DateTime($year."-".$month);
  605.             $lastMonth = new DateTime($year."-".$month);
  606.             $lastWeek->modify("last week this month");
  607.             $lastMonth->modify("first day of this month");
  608.             
  609.             
  610.         }
  611.         $queryOrders
  612.             ->setParameter('last_week'$lastWeek)
  613.             ->setParameter('last_month'$lastMonth)
  614.             ->setParameter('status'$status)
  615.             ->setParameter('store'$store)
  616.             ->setParameter('year'$year)
  617.             ->setParameter('month'$month)
  618.             ; 
  619.         $statsOrders  $queryOrders->getSingleResult();
  620.     
  621.         return $statsOrders;
  622.     }
  623.     public function productRevenue(Product $product): float
  624.     {
  625.         $qb $this->em->createQueryBuilder();
  626.         $qb->select('SUM(oi.price * oi.quantity) as revenue')
  627.            ->from(OrderItem::class, 'oi')
  628.            ->join('oi.product''p')
  629.            ->where('p.id = :productId')
  630.            ->setParameter('productId'$product->getId());
  631.         $result $qb->getQuery()->getSingleResult();
  632.         return $result['revenue'];
  633.     }
  634.     public function categoryRevenue(CategoryProduct $category): float
  635.     {
  636.         $qb $this->em->createQueryBuilder();
  637.         $qb->select('SUM(oi.price * oi.quantity) as revenue')
  638.            ->from(OrderItem::class, 'oi')
  639.            ->join('oi.product''p')
  640.            ->join('p.categoriesProduct''c')
  641.            ->where('c.id = :categoryId')
  642.            ->setParameter('categoryId'$category->getId());
  643.         $result $qb->getQuery()->getSingleResult();
  644.         return $result['revenue'];
  645.     }
  646.     /*public function revenueByProducts($store=null,$year=null,$month=null){
  647.         
  648.         $dql = 'SELECT 
  649.         p, 
  650.         (
  651.             SELECT SUM(oi.quantity * oi.price)
  652.             FROM App\Flexy\ShopBundle\Entity\Order\OrderItem oi
  653.             JOIN oi.parentOrder o
  654.             WHERE o.status != :status AND oi.product = p 
  655.             AND (o.store = :store OR :store IS NULL)
  656.             AND (YEAR(o.createdAt) = :year OR :year IS NULL)
  657.             AND (MONTH(o.createdAt) = :month OR :month IS NULL)
  658.                 ) as revenue
  659.             FROM 
  660.                 App\Flexy\ShopBundle\Entity\Product\Product p
  661.                 LEFT JOIN
  662.                     App\Flexy\ShopBundle\Entity\Order\OrderItem oItem WITH oItem.product = p
  663.                 GROUP BY
  664.                     p.id, p.name
  665.                 HAVING
  666.                     revenue > 0
  667.                 ORDER BY
  668.                     revenue DESC
  669.                 ';
  670.                 $query = $this->em->createQuery($dql)->setParameters(
  671.                     [
  672.                         "status"=>"canceled",
  673.                         "year"=>$year,
  674.                         "month"=>$month,
  675.                         "store"=>$store
  676.                     ]
  677.                 )->setMaxResults(12);
  678.         
  679.         
  680.         $productsRevenue = $query->getResult();
  681.         return $productsRevenue;
  682.     }*/
  683.     
  684.       public function revenueByProducts($store=null,$year=null,$month=null){
  685.         
  686.         $dql '
  687.         SELECT 
  688.             p.id AS productId,
  689.             p.name AS productName,
  690.             SUM(oi.quantity * oi.price) AS revenue,
  691.             COUNT(oi.id) AS totalSold
  692.         FROM 
  693.              App\Flexy\ShopBundle\Entity\Order\OrderItem oi
  694.         LEFT JOIN 
  695.              App\Flexy\ShopBundle\Entity\Product\Product p WITH  p.id = oi.product
  696.              LEFT JOIN 
  697.         oi.parentOrder o     
  698.          WHERE    
  699.     
  700.        (YEAR(oi.createdAt) = :year OR :year IS NULL)
  701.         AND (o.store = :store OR :store IS NULL)
  702.         AND (MONTH(oi.createdAt) = :month OR :month IS NULL)
  703.         AND  p.name IS NOT NULL
  704.         GROUP BY 
  705.              p.id, p.name
  706.         HAVING 
  707.             revenue > 0
  708.         ORDER BY 
  709.             revenue DESC
  710.     ';
  711.     
  712.     
  713.             $query $this->em->createQuery($dql)->setParameters([
  714.             
  715.             "store"=>$store,
  716.             'year' => $year,
  717.             'month' => $month,
  718.            
  719.         ])->setMaxResults(12);
  720.     
  721.       $productsRevenue $query->getResult();
  722.   
  723.       return $productsRevenue;
  724.     
  725.     }
  726.     
  727.     public function visitAll($store=null,$year=null,$month=null){
  728.        
  729.        $dql "SELECT 
  730.         SUM(CASE WHEN v.manual = 0 OR v.manual IS NULL THEN 1 ELSE 0 END) as automatique,
  731.         SUM(CASE WHEN v.manual = 1 THEN 1 ELSE 0 END) as  manuel,
  732.         DATE_FORMAT(v.createdAt, '%M') as Mois,
  733.         YEAR(CURRENT_DATE()) as annee
  734.         FROM 
  735.         App\Flexy\ShopBundle\Entity\Customer\Visit v
  736.         WHERE 
  737.                 YEAR(v.createdAt) = YEAR(CURRENT_DATE())
  738.          GROUP BY 
  739.          Mois
  740.          ";
  741.         $query $this->em->createQuery($dql)->setMaxResults(12);
  742.         
  743.         $visit $query->getResult();
  744.         return $visit;
  745.     
  746.     }
  747.     public function getVisitYesterday($store=null,$year=null,$month=null)
  748.     {
  749.         $today = new \DateTime('today');
  750.         $yesterday =new \DateTime('yesterday');
  751.         $yesterdayFormatted $yesterday->format('Y-m-d H:i');
  752.         $todayFormatted $today->format('Y-m-d H:i');  
  753.         $dql "SELECT 
  754.         SUM(CASE WHEN v.manual = 0 OR v.manual IS NULL THEN 1 ELSE 0 END) as automatique,      
  755.         SUM(CASE WHEN v.manual = 1 THEN 1 ELSE 0 END) as  manuel,
  756.         SUM(CASE WHEN v.manual = 0 OR v.manual IS NULL AND c.gender = 'female' THEN 1 ELSE 0 END) as female_automatique,
  757.         SUM(CASE WHEN v.manual = 1 AND c.gender = 'female' THEN 1 ELSE 0 END) as female_manuel,
  758.         SUM(CASE WHEN v.manual = 0 OR v.manual IS NULL AND c.gender = 'male' THEN 1 ELSE 0 END) as male_automatique,
  759.         SUM(CASE WHEN v.manual = 1 AND c.gender = 'male' THEN 1 ELSE 0 END) as male_manuel     
  760.         FROM 
  761.         App\Flexy\ShopBundle\Entity\Customer\Visit v
  762.         LEFT JOIN
  763.         App\Flexy\ShopBundle\Entity\Customer\Customer c WITH v.customer = c
  764.         WHERE        v.createdAt >= :yesterday
  765.                 AND  v.createdAt <  :today
  766.          
  767.          ";
  768.          $query $this->em->createQuery($dql)->setParameters(
  769.             
  770.             [
  771.                 "yesterday"=>$yesterdayFormatted,
  772.                 "today"=>$todayFormatted,
  773.             ]
  774.         
  775.     )->setMaxResults(12);
  776.     
  777.     $visit $query->getResult();
  778.          //dd($visit);
  779.         return $visit;
  780.     }
  781.     public function getVisitToday($store=null,$year=null,$month=null)
  782.     {
  783.         $today = new \DateTime('today');
  784.         $tomorrow =new \DateTime('tomorrow');
  785.         $tomorrowFormatted $tomorrow->format('Y-m-d H:i');
  786.         $todayFormatted $today->format('Y-m-d H:i');  
  787.         $dql "SELECT 
  788.         SUM(CASE WHEN v.manual = 0 OR v.manual IS NULL THEN 1 ELSE 0 END) as automatique,
  789.         SUM(CASE WHEN v.manual = 1 THEN 1 ELSE 0 END) as  manuel,
  790.         SUM(CASE WHEN v.manual = 0 OR v.manual IS NULL AND c.gender = 'female' THEN 1 ELSE 0 END) as female_automatique,
  791.         SUM(CASE WHEN v.manual = 1 AND c.gender = 'female' THEN 1 ELSE 0 END) as female_manuel,
  792.         SUM(CASE WHEN v.manual = 0 OR v.manual IS NULL AND c.gender = 'male' THEN 1 ELSE 0 END) as male_automatique,
  793.         SUM(CASE WHEN v.manual = 1 AND c.gender = 'male' THEN 1 ELSE 0 END) as male_manuel   
  794.         FROM 
  795.         App\Flexy\ShopBundle\Entity\Customer\Visit v
  796.         LEFT JOIN
  797.         App\Flexy\ShopBundle\Entity\Customer\Customer c WITH v.customer = c
  798.         WHERE        v.createdAt >= :today
  799.                 AND  v.createdAt <  :tomorrow
  800.          
  801.          ";
  802.          $query $this->em->createQuery($dql)->setParameters(
  803.             
  804.             [
  805.                 "tomorrow"=>$tomorrowFormatted,
  806.                 "today"=>$todayFormatted,
  807.             ]
  808.         
  809.     )->setMaxResults(12);
  810.     
  811.     $visit $query->getResult();
  812.          //dd($visit);
  813.         return $visit;
  814.     }
  815.     public function revenueByCategories($store=null,$year=null,$month=null){
  816.         
  817.         
  818.         $dql 'SELECT 
  819.         c.name as category, 
  820.         (
  821.             SELECT SUM(oi.quantity * oi.price)
  822.             FROM App\Flexy\ShopBundle\Entity\Order\OrderItem oi
  823.             JOIN oi.parentOrder o
  824.             WHERE c.parentCategory IS NOT NULL AND o.status != :status AND oi.product MEMBER OF c.products 
  825.             AND (YEAR(o.createdAt) = :year OR :year IS NULL)
  826.             AND (MONTH(o.createdAt) = :month OR :month IS NULL)
  827.             AND (o.store = :store OR :store IS NULL)
  828.         ) as revenue
  829.     FROM 
  830.     App\Flexy\ShopBundle\Entity\Product\CategoryProduct c
  831.     GROUP BY
  832.         c.id, c.name
  833.     HAVING
  834.         revenue > 0
  835.     ORDER BY
  836.         revenue DESC';
  837.         $query $this->em->createQuery($dql)->setParameters(
  838.             
  839.                 [
  840.                     "status"=>"canceled",
  841.                     "year"=>$year,
  842.                     "month"=>$month,
  843.                     "store"=>$store
  844.                 ]
  845.             
  846.         )->setMaxResults(12);
  847.         
  848.         $categoriesRevenue $query->getResult();
  849.         return $categoriesRevenue;
  850.     }
  851.     public function revenueByAgents($store=null,$year=null,$month=null){
  852.         
  853.         
  854.         $dql 'SELECT 
  855.         a.firstName as agentFirstName, 
  856.         a.lastName as agentLastName, 
  857.         (
  858.             SELECT SUM(payments.amount)
  859.             FROM App\Flexy\ShopBundle\Entity\Payment\Payment payments
  860.             JOIN payments.relatedOrder o
  861.             WHERE o.agent = a 
  862.             
  863.             AND (YEAR(o.createdAt) = :year OR :year IS NULL)
  864.             AND (MONTH(o.createdAt) = :month OR :month IS NULL)
  865.             AND (o.store = :store OR :store IS NULL)
  866.         ) as revenue
  867.     FROM 
  868.     App\Flexy\ShopBundle\Entity\Resource\Agent a
  869.     GROUP BY
  870.         a.id, a.firstName
  871.     HAVING
  872.         revenue > 0
  873.     ORDER BY
  874.         revenue DESC';
  875.         $query $this->em->createQuery($dql)->setParameters(
  876.             
  877.                 [
  878.                     
  879.                     "year"=>$year,
  880.                     "month"=>$month,
  881.                     "store"=>$store
  882.                 ]
  883.             
  884.         )->setMaxResults(12);
  885.         
  886.         $agentsRevenue $query->getResult();
  887.         return $agentsRevenue;
  888.     }
  889.     public function revenueByParentCategories($store=null,$year=null,$month=null){
  890.         
  891.         
  892.         $dql 'SELECT 
  893.         pc.name as category, 
  894.         (
  895.             SELECT SUM(oi.quantity * oi.price)
  896.             FROM App\Flexy\ShopBundle\Entity\Order\OrderItem oi
  897.             JOIN oi.parentOrder o
  898.             JOIN oi.product p
  899.             JOIN p.categoriesProduct categ
  900.             WHERE categ.parentCategory = pc AND o.status != :status
  901.             AND (YEAR(o.createdAt) = :year OR :year IS NULL)
  902.             AND (MONTH(o.createdAt) = :month OR :month IS NULL)
  903.             AND (o.store = :store OR :store IS NULL)
  904.         ) as revenue
  905.     FROM 
  906.         App\Flexy\ShopBundle\Entity\Product\CategoryProduct c
  907.     JOIN
  908.         c.parentCategory pc
  909.     
  910.     WHERE
  911.         c.parentCategory IS NOT NULL
  912.     GROUP BY
  913.         pc.id, pc.name
  914.     HAVING
  915.         revenue > 0
  916.     ORDER BY
  917.         revenue DESC';
  918.         $query $this->em->createQuery($dql)->setParameters(
  919.             
  920.                 [
  921.                     "status"=>"canceled",
  922.                     "year"=>$year,
  923.                     "month"=>$month,
  924.                     "store"=>$store
  925.                 ]
  926.             
  927.         );
  928.         
  929.         $categoriesRevenue $query->getResult();
  930.         return $categoriesRevenue;
  931.     }
  932.     public function revenueByCashBoxOrder(CashBoxOrder $cashBoxOrder){
  933.         $queryBuilder $this->em->createQueryBuilder();
  934.         $queryBuilder->select('SUM(oi.price * oi.quantity) AS revenue');
  935.         $queryBuilder->from('App\Flexy\ShopBundle\Entity\Order\Order''o');
  936.         $queryBuilder->join('o.orderItems''oi');
  937.         $queryBuilder->join('App\Flexy\ShopBundle\Entity\Order\CashBoxOrder''cashBox');
  938.         
  939.         $queryBuilder->where('cashBox = :cashBox');
  940.         
  941.         $queryBuilder->groupBy('cashBox');
  942.         $queryBuilder->addGroupBy('cashBox.startAt');
  943.         $queryBuilder->setParameter("cashBox",$cashBoxOrder);
  944.         
  945.         $results $queryBuilder->getQuery()->getResult();
  946.         return $results;
  947.     }
  948.     public function revenueByCityRegion(){
  949.         $queryBuilder $this->em->createQueryBuilder();
  950.         $queryBuilder->select('cr.name AS cityRegionName, SUM(oi.price * oi.quantity) AS revenue')
  951.              ->from('App\Flexy\ShopBundle\Entity\Order\Order''o')
  952.              ->join('o.orderItems''oi')
  953.              ->leftJoin('o.cityRegionCollect''crc')
  954.              ->leftJoin('o.cityRegionShipping''crs')
  955.              ->join('App\Flexy\ShopBundle\Entity\Shipping\CityRegion''cr''WITH''cr = crc OR cr = crs')
  956.              ->groupBy('cr')
  957.              ->addGroupBy('cr.name');
  958.         $results $queryBuilder->getQuery()->getResult();
  959.         return $results;
  960.     }
  961.     
  962.     // CHARTS FUNCTIONS :
  963.     public function chartRevenue($store=null,$year=null,$month=null){
  964.         
  965.         //$chart = $this->chartBuilderInterface->createChart(Chart::TYPE_LINE);
  966.           $chart $this->chartBuilderInterface->createChart(Chart::TYPE_BAR);
  967.         $labels = [];
  968.         $data = [];
  969.         if($year and $month){
  970.             foreach($this->getRevenueByMonth($store,$year,$month) as $singleMonth){
  971.                 $labels[] = $singleMonth["date"];
  972.                 $data[] = $singleMonth["revenue"];
  973.             }
  974.         }
  975.         else{
  976.             foreach($this->getRevenueByYear($store,$year) as $singleMonth){
  977.          
  978.                      
  979.                 $labels[] = $singleMonth["monthYear"];
  980.                 $data[] = $singleMonth["revenue"];
  981.                 
  982.             }
  983.         }
  984.         
  985.         $chart->setData([
  986.             'labels' => $labels,
  987.             'datasets' => [
  988.                 [
  989.                     'label' => 'Chiffre d\'affaire ',
  990.                     'backgroundColor' => $this->chartColors,
  991.                     //'borderColor' => 'rgb(255, 99, 132)',
  992.                     "borderRadius"=>10,
  993.                     'data' => $data,
  994.                     "tension"=> 0.5,
  995.                     "fill"   => true,
  996.                 ],
  997.             ],
  998.         ]);
  999.         $chart->setOptions([
  1000.             "plugins"=>[
  1001.                 "legend"=>[
  1002.                     "labels"=>[
  1003.                         "font"=>[
  1004.                             "size"=>12,
  1005.                             "family"=> "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif"
  1006.                         ]
  1007.                     ]
  1008.                 ]
  1009.             ],
  1010.             
  1011.             'scales' => [
  1012.                 
  1013.                 'y' => [
  1014.                     'ticks' => [
  1015.                         'font' => [
  1016.                             'size' => 11,
  1017.                             'family' => "'Montserrat', 'Helvetica', 'Arial', sans-serif",
  1018.                             'style' => 'normal',
  1019.                             'weight' => 'normal',
  1020.                         ],
  1021.                         'color' => 'rgba(0,0,0,0.3)',
  1022.                     ],
  1023.                     'suggestedMin' => 0,
  1024.                     //'suggestedMax' => 3000,
  1025.                     "display"=>true ,
  1026.                     "grid"=>[
  1027.                         "lineWidth"=>0.2,
  1028.                         "borderColor"=>"rgba(0,0,0,0)"
  1029.                     ],
  1030.                     
  1031.                      
  1032.                     
  1033.                 ],
  1034.                 'x' => [
  1035.                     'ticks' => [
  1036.                         'font' => [
  1037.                             'size' => 11,
  1038.                             'family' => "'Montserrat', 'Helvetica', 'Arial', sans-serif",
  1039.                             'style' => 'normal',
  1040.                             'weight' => 'normal',
  1041.                         ],
  1042.                         'color' => 'rgba(0,0,0,0.3)',
  1043.                     ],
  1044.                     "display"=>true,
  1045.                     "grid"=>[
  1046.                         "lineWidth"=>0,
  1047.                         "borderColor"=>"rgba(0,0,0,0)"
  1048.                     ],
  1049.                 ],
  1050.             ],
  1051.         ]);
  1052.         return $chart;
  1053.     }
  1054.     
  1055.      public function chartVisteBymonths($store=null,$year=null,$month=null){
  1056.         $chart $this->chartBuilderInterface->createChart(Chart::TYPE_LINE);
  1057.         $labels = [];
  1058.         $data = [];
  1059.         foreach($this->visitAll($store,$year,$month) as $singleData){
  1060.             
  1061.             $labels[] = $singleData["Mois"];
  1062.             $data[] = $singleData["automatique"];
  1063.             $data1[] = $singleData["manuel"];
  1064.         }
  1065.         $chart->setData([
  1066.             'labels' => $labels,
  1067.             'datasets' => [
  1068.                 [
  1069.                     'label' => '🏃🏻‍♂️ Pointage Manuel',
  1070.                     'backgroundColor' => 'rgb(255, 99, 132, .4)',
  1071.                     'borderColor' => 'rgb(255, 99, 132)',
  1072.                     'data' => $data1,
  1073.                     'tension' => 0.4,
  1074.                 ],
  1075.                 [
  1076.                     'label' => '🏃🏻‍♂️ Pointage 💳 Carte',
  1077.                     'backgroundColor' => 'rgba(45, 220, 126, .4)',
  1078.                     'borderColor' => 'rgba(45, 220, 126)',
  1079.                     'data' => $data,
  1080.                     'tension' => 0.4,
  1081.                 ],
  1082.             ],
  1083.         ]);
  1084.         $chart->setOptions([
  1085.             'maintainAspectRatio' => false,
  1086.         ]);
  1087.         return $chart;
  1088.     }
  1089.     public function chartRevenueByParentCategories($store=null,$year=null,$month=null){
  1090.         $chart $this->chartBuilderInterface->createChart(Chart::TYPE_DOUGHNUT);
  1091.         $labels = [];
  1092.         $data = [];
  1093.         foreach($this->revenueByParentCategories($store,$year,$month) as $singleData){
  1094.             $labels[] = $singleData["category"];
  1095.             $data[] = $singleData["revenue"];
  1096.         }
  1097.         $chart->setData([
  1098.             'labels' => $labels,
  1099.             'datasets' => [
  1100.                 [
  1101.                     'label' => 'Chiffre d\'affaire ',
  1102.                     'backgroundColor' => $this->chartColors,
  1103.                     'data' => $data,
  1104.                 ],
  1105.             ],
  1106.         ]);
  1107.         $chart->setOptions([
  1108.             "plugins"=>[
  1109.                 "legend"=>[
  1110.                     "labels"=>[
  1111.                         "font"=>[
  1112.                             "size"=>12,
  1113.                             "family"=> "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif"
  1114.                         ]
  1115.                     ]
  1116.                 ]
  1117.             ],
  1118.             
  1119.             'scales' => [
  1120.                 
  1121.                 'y' => [
  1122.                     'ticks' => [
  1123.                         'font' => [
  1124.                             'size' => 11,
  1125.                             'family' => "'Montserrat', 'Helvetica', 'Arial', sans-serif",
  1126.                             'style' => 'normal',
  1127.                             'weight' => 'normal',
  1128.                         ],
  1129.                         'color' => 'rgba(0,0,0,0.3)',
  1130.                     ],
  1131.                     'suggestedMin' => 0,
  1132.                     "display"=>false ,
  1133.                     "grid"=>[
  1134.                         "lineWidth"=>0.2,
  1135.                         "borderColor"=>"rgba(0,0,0,0)"
  1136.                     ],
  1137.                     
  1138.                      
  1139.                     
  1140.                 ],
  1141.                 'x' => [
  1142.                     'ticks' => [
  1143.                         'font' => [
  1144.                             'size' => 11,
  1145.                             'family' => "'Montserrat', 'Helvetica', 'Arial', sans-serif",
  1146.                             'style' => 'normal',
  1147.                             'weight' => 'normal',
  1148.                         ],
  1149.                         'color' => 'rgba(0,0,0,0.3)',
  1150.                     ],
  1151.                     "display"=>true,
  1152.                     "grid"=>[
  1153.                         "lineWidth"=>0,
  1154.                         "borderColor"=>"rgba(0,0,0,0)"
  1155.                     ],
  1156.                 ],
  1157.             ],
  1158.         ]);
  1159.         return $chart;
  1160.     }
  1161.     public function chartRevenueByProduct(){
  1162.         $chart $this->chartBuilderInterface->createChart(Chart::TYPE_BAR);
  1163.         $chart->setData([
  1164.             'labels' => ['January''February''March''April''May''June''July','January''February''March''April''May''June''July'],
  1165.             'datasets' => [
  1166.                 [
  1167.                     'label' => 'Chiffre d\'affaire ',
  1168.                     'backgroundColor' => $this->chartColors,
  1169.                     //'borderColor' => 'rgb(255, 99, 132)',
  1170.                     "borderRadius"=>10,
  1171.                     'data' => [121052203045,121052203045],
  1172.                     "tension"=> 0.4,
  1173.                     //"fill"   => true,
  1174.                 ],
  1175.             ],
  1176.         ]);
  1177.         $chart->setOptions([
  1178.             "plugins"=>[
  1179.                 "legend"=>[
  1180.                     "labels"=>[
  1181.                         "font"=>[
  1182.                             "size"=>12,
  1183.                             "family"=> "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif"
  1184.                         ]
  1185.                     ]
  1186.                 ]
  1187.             ],
  1188.             
  1189.             'scales' => [
  1190.                 
  1191.                 'y' => [
  1192.                     'ticks' => [
  1193.                         'font' => [
  1194.                             'size' => 11,
  1195.                             'family' => "'Montserrat', 'Helvetica', 'Arial', sans-serif",
  1196.                             'style' => 'normal',
  1197.                             'weight' => 'normal',
  1198.                         ],
  1199.                         'color' => 'rgba(0,0,0,0.3)',
  1200.                     ],
  1201.                     'suggestedMin' => 0,
  1202.                     'suggestedMax' => 100,
  1203.                     "display"=>true ,
  1204.                     "grid"=>[
  1205.                         "lineWidth"=>0.2,
  1206.                         "borderColor"=>"rgba(0,0,0,0)"
  1207.                     ],
  1208.                     
  1209.                      
  1210.                     
  1211.                 ],
  1212.                 'x' => [
  1213.                     'ticks' => [
  1214.                         'font' => [
  1215.                             'size' => 11,
  1216.                             'family' => "'Montserrat', 'Helvetica', 'Arial', sans-serif",
  1217.                             'style' => 'normal',
  1218.                             'weight' => 'normal',
  1219.                         ],
  1220.                         'color' => 'rgba(0,0,0,0.3)',
  1221.                     ],
  1222.                     "display"=>true,
  1223.                     "grid"=>[
  1224.                         "lineWidth"=>0,
  1225.                         "borderColor"=>"rgba(0,0,0,0)"
  1226.                     ],
  1227.                 ],
  1228.             ],
  1229.         ]);
  1230.         return $chart;
  1231.     }
  1232.     
  1233.     public function chartRevenueByCategory(){
  1234.         $chart $this->chartBuilderInterface->createChart(Chart::TYPE_BAR);
  1235.         $chart->setData([
  1236.             'labels' => ['January''February''March''April''May''June''July','January''February''March''April''May''June''July'],
  1237.             'datasets' => [
  1238.                 [
  1239.                     'label' => 'Chiffre d\'affaire ',
  1240.                     'backgroundColor' => $this->chartColors,
  1241.                     //'borderColor' => 'rgb(255, 99, 132)',
  1242.                     "borderRadius"=>10,
  1243.                     'data' => [121052203045,121052203045],
  1244.                     "tension"=> 0.4,
  1245.                     //"fill"   => true,
  1246.                 ],
  1247.             ],
  1248.         ]);
  1249.         $chart->setOptions([
  1250.             "plugins"=>[
  1251.                 "legend"=>[
  1252.                     "labels"=>[
  1253.                         "font"=>[
  1254.                             "size"=>12,
  1255.                             "family"=> "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif"
  1256.                         ]
  1257.                     ]
  1258.                 ]
  1259.             ],
  1260.             
  1261.             'scales' => [
  1262.                 
  1263.                 'y' => [
  1264.                     'ticks' => [
  1265.                         'font' => [
  1266.                             'size' => 11,
  1267.                             'family' => "'Montserrat', 'Helvetica', 'Arial', sans-serif",
  1268.                             'style' => 'normal',
  1269.                             'weight' => 'normal',
  1270.                         ],
  1271.                         'color' => 'rgba(0,0,0,0.3)',
  1272.                     ],
  1273.                     'suggestedMin' => 0,
  1274.                     'suggestedMax' => 100,
  1275.                     "display"=>true ,
  1276.                     "grid"=>[
  1277.                         "lineWidth"=>0.2,
  1278.                         "borderColor"=>"rgba(0,0,0,0)"
  1279.                     ],
  1280.                     
  1281.                      
  1282.                     
  1283.                 ],
  1284.                 'x' => [
  1285.                     'ticks' => [
  1286.                         'font' => [
  1287.                             'size' => 11,
  1288.                             'family' => "'Montserrat', 'Helvetica', 'Arial', sans-serif",
  1289.                             'style' => 'normal',
  1290.                             'weight' => 'normal',
  1291.                         ],
  1292.                         'color' => 'rgba(0,0,0,0.3)',
  1293.                     ],
  1294.                     "display"=>true,
  1295.                     "grid"=>[
  1296.                         "lineWidth"=>0,
  1297.                         "borderColor"=>"rgba(0,0,0,0)"
  1298.                     ],
  1299.                 ],
  1300.             ],
  1301.         ]);
  1302.         return $chart;
  1303.     }
  1304.     
  1305. public function periodsOrdersOptions($store=null){
  1306.     
  1307.         $qb $this->em->createQueryBuilder();
  1308.         $qb->select('YEAR(o.createdAt) AS year, MONTH(o.createdAt) AS month')
  1309.         ->from('App\Flexy\ShopBundle\Entity\Order\Order''o')
  1310.         ->where(
  1311.             '(o.store = :store OR :store IS NULL)'
  1312.         )
  1313.         ->orderBy('year''DESC')
  1314.         ->addOrderBy('month''ASC')
  1315.         ->setParameters([
  1316.             "store"=>$store,
  1317.         ])
  1318.         ;
  1319.         $results $qb->getQuery()->getResult();
  1320.         $years = [];
  1321.         foreach ($results as $result) {
  1322.             $year $result['year'];
  1323.             $month $result['month'];
  1324.             $monthName date("F"mktime(000$month1));
  1325.         
  1326.             if (!isset($years[$year])) {
  1327.                 $years[$year] = [];
  1328.             }
  1329.         
  1330.             if (!in_array(['number' => $month'name' => $monthName], $years[$year])) {
  1331.                 $years[$year][] = ['number' => $month'name' => $monthName];
  1332.             }
  1333.         }
  1334.         
  1335.         $finalArray = [];
  1336.         foreach ($years as $year => $months) {
  1337.             $finalArray[] = [
  1338.                 'year' => $year,
  1339.                 'months' => $months
  1340.             ];
  1341.         }
  1342.         
  1343.         // sort the final array by the latest year and month
  1344.         usort($finalArray, function($a$b) {
  1345.             $aYear $a['year'];
  1346.             $aMonth end($a['months'])['number'];
  1347.             $bYear $b['year'];
  1348.             $bMonth end($b['months'])['number'];
  1349.         
  1350.             if ($aYear == $bYear) {
  1351.                 return ($aMonth $bMonth) ? : -1;
  1352.             }
  1353.         
  1354.             return ($aYear $bYear) ? : -1;
  1355.         });
  1356.         return $finalArray;
  1357.         
  1358.         // $finalArray now contains an array of years where orders exist, each year
  1359.         // with the months where orders exist, including the numeric value and name of the month,
  1360.         // sorted by the latest year and month.
  1361. }
  1362. }