src/Controller/IndexController.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Config\Config;
  4. use App\Entity\Orders;
  5. use App\Pdf\AchatsPdf;
  6. use Doctrine\DBAL\Driver\Exception;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse;
  9. use Knp\Snappy\Pdf;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. /**
  15.  * @IsGranted("ROLE_USER")
  16.  */
  17. class IndexController extends AbstractController
  18. {
  19.     private $entityManager;
  20.     public function __construct(EntityManagerInterface $entityManager)
  21.     {
  22.         $this->entityManager $entityManager;
  23.     }
  24.     /**
  25.      * @Route("/", name="admin_homepage")
  26.      */
  27.     public function index(Request $requestEntityManagerInterface $entityManagerConfig $config)
  28.     {
  29.         $date_from $request->query->get('date'date('Y-01-01'));
  30.         $date_to $request->query->get('date_to'date('Y-m-d'));
  31.         $ttc $request->query->get('ttc''off');
  32.         $orders $this->getTotalSales($date_from$date_to);
  33.         $nbNewCustomers $this->getTotalNewCustomers($date_from$date_to);
  34.         $stores $config->storeAliases;
  35.         $livraisons $config->livraison;
  36.         $sales_ca 0;
  37.         $nborders 0;
  38.         $caCC 0;
  39.         $cmdCC 0;
  40.         $caL 0;
  41.         $cmdL 0;
  42.         $cmdNew 0;
  43.         $caNew 0;
  44.         $StoresVals = [];
  45.         $livVals = [];
  46.         foreach ($stores as $key => $store){
  47.             $StoresVals[$key] = array(
  48.                 "name" => $store,
  49.                 "ca" => 0,
  50.                 "cmd" => 0,
  51.             );
  52.         }
  53.         foreach ($livraisons as $key => $livraison){
  54.             $livVals[$key] = array(
  55.                 "name" => $livraison,
  56.                 "ca" => 0,
  57.                 "cmd" => 0,
  58.             );
  59.         }
  60.         foreach ($orders as $sale){
  61.             $sales_ca += $sale['sales_'.($ttc == 'on'?'ttc':'ht')];
  62.             $nborders++;
  63.             if($sale['new']){
  64.                 $cmdNew++;
  65.                 $caNew += $sale['sales_'.($ttc == 'on'?'ttc':'ht')];
  66.             }
  67.             if($sale['store']){
  68.                 $StoresVals[$sale['store']]['ca'] += $sale['sales_'.($ttc == 'on'?'ttc':'ht')];
  69.                 $StoresVals[$sale['store']]['cmd'] += 1;
  70.                 $caCC += $sale['sales_'.($ttc == 'on'?'ttc':'ht')];
  71.                 $cmdCC++;
  72.             }elseif($sale['livraison']){
  73.                 $livVals[$sale['livraison']]['ca'] += $sale['sales_'.($ttc == 'on'?'ttc':'ht')];
  74.                 $livVals[$sale['livraison']]['cmd'] += 1;
  75.                 $caL += $sale['sales_'.($ttc == 'on'?'ttc':'ht')];
  76.                 $cmdL++;
  77.             }
  78.         }
  79.         return $this->render('home.html.twig', [
  80.             'ttc' => $ttc,
  81.             'date' => $date_from,
  82.             'dateTo' => $date_to,
  83.             'salesCa' => $sales_ca,
  84.             'NbOrders' => $nborders,
  85.             'stores' => $stores,
  86.             'livraisons' => $livraisons,
  87.             'orders' => $orders,
  88.             'caCC' => $caCC,
  89.             'caL' => $caL,
  90.             'cmdCC' => $cmdCC,
  91.             'cmdL' => $cmdL,
  92.             'storesVals' => $StoresVals,
  93.             'nbNewCustomers' => $nbNewCustomers,
  94.             'cmdNew' => $cmdNew,
  95.             'caNew' => $caNew
  96.          ]);
  97.     }
  98.     /**
  99.      * @throws Exception
  100.      * @throws \Doctrine\DBAL\Exception
  101.      */
  102.     private function query($sql$params = [])
  103.     {
  104.         $conn $this->entityManager->getConnection();
  105.         $stmt $conn->prepare($sql);
  106.         $stmt->execute($params);
  107.         return $stmt;
  108.     }
  109.     public function getTotalSales($date_from$date_to)
  110.     {
  111.         $sql '
  112.         SELECT SUM((total_paid_tax_excl - total_shipping_tax_excl) / o.conversion_rate) AS sales_ht,  
  113.                SUM((total_paid_tax_incl - total_shipping_tax_incl) / o.conversion_rate) AS sales_ttc,
  114.                pdc.id_store as store,
  115.                phc.id_reference as livraison,
  116.                (SELECT IF(count(so.id_order) > 0, 0, 1) FROM ps36_orders so WHERE (so.id_customer = o.id_customer) AND (so.id_order < o.id_order) LIMIT 1) AS new
  117.         FROM `ps36_orders` o 
  118.         LEFT JOIN `ps36_order_state` os ON o.current_state = os.id_order_state 
  119.         left join ps36_prestatill_drive_creneau pdc on pdc.id_order = o.id_order 
  120.         left join ps36_prestatill_homedelivery_creneau phc on phc.id_order = o.id_order
  121.         WHERE `invoice_date` BETWEEN "' $this->escape($date_from) . ' 00:00:00" AND "' $this->escape($date_to) . ' 23:59:59" AND os.logable = 1 
  122.         GROUP BY o.id_order';
  123.         $query $this->query($sql);
  124.         return $query->fetchAll();
  125.     }
  126.     public function getTotalNewCustomers($date_from$date_to)
  127.     {
  128.         $sql 'SELECT COUNT(*) FROM ps36_customer WHERE date_add BETWEEN "' $this->escape($date_from) . ' 00:00:00" AND "' $this->escape($date_to) . ' 23:59:59"';
  129.         $query $this->query($sql);
  130.         return $query->fetchOne();
  131.     }
  132.     public function escape($string$html_ok false$bq_sql false)
  133.     {
  134.         /*if (_PS_MAGIC_QUOTES_GPC_) {
  135.             $string = stripslashes($string);
  136.         }*/
  137.         if (!is_numeric($string)) {
  138.             //$string = $this->_escape($string);
  139.             if (!$html_ok) {
  140.                 $string strip_tags(nl2br($string));
  141.             }
  142.             if ($bq_sql === true) {
  143.                 $string str_replace('`''\`'$string);
  144.             }
  145.         }
  146.         return $string;
  147.     }
  148. }