src/Repository/SalesRepository.php line 332

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\Sales;
  4. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  5. use Doctrine\DBAL\Exception;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Doctrine\Persistence\ManagerRegistry;
  8. /**
  9.  * @extends ServiceEntityRepository<Sales>
  10.  *
  11.  * @method Sales|null find($id, $lockMode = null, $lockVersion = null)
  12.  * @method Sales|null findOneBy(array $criteria, array $orderBy = null)
  13.  * @method Sales[]    findAll()
  14.  * @method Sales[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  15.  */
  16. class SalesRepository extends ServiceEntityRepository
  17. {
  18.     private $em;
  19.     public function __construct(ManagerRegistry $registryEntityManagerInterface $em)
  20.     {
  21.         parent::__construct($registrySales::class);
  22.         $this->em $em;
  23.     }
  24.     public function add(Sales $entitybool $flush false): void
  25.     {
  26.         $this->getEntityManager()->persist($entity);
  27.         if ($flush) {
  28.             $this->getEntityManager()->flush();
  29.         }
  30.     }
  31.     public function remove(Sales $entitybool $flush false): void
  32.     {
  33.         $this->getEntityManager()->remove($entity);
  34.         if ($flush) {
  35.             $this->getEntityManager()->flush();
  36.         }
  37.     }
  38.     public function getSalesList(array $dataReceivestring $search nullint $start 0int $length = -1,  $order = array())
  39.     {
  40.         $columns = array(
  41.             => 'DATE',
  42.             => 'TIME',
  43.             => 'AMOUNT',
  44.             => 'USERNAME',
  45.             => 'ID',
  46.             => 'SHOP'
  47.         );
  48.         $sql "select 
  49.                     s.id as ID,
  50.                     s.date as 'DATE',
  51.                     s.time as 'TIME',
  52.                     s.amount as AMOUNT,
  53.                     u.username as USERNAME,
  54.                     p.label as SHOP,
  55.                     s.bon as BON
  56.                 from sales s 
  57.                 left join user u on (u.id = s.user_id)
  58.                 left join shop p on (p.id = s.shop_id)
  59.                 WHERE s.deleted != 1
  60.                 ";
  61.         $sqlCount "select count(distinct s.id) 
  62.                 from sales s 
  63.                 left join user u on (u.id = s.user_id)
  64.                 left join shop p on (p.id = s.shop_id)
  65.                 WHERE s.deleted != 1";
  66.         //apply search if requested
  67.         if ($search != null) {
  68.             $conditions ' AND (DATE_FORMAT(s.date, "%d/%m/%Y") like "%' $search '%" OR u.username like "%' $search '%" OR s.bon like "%' $search '%") ';
  69.             $sql $sql $conditions;
  70.             $sqlCount $sqlCount $conditions;
  71.         }
  72.         //apply order on date
  73.         $sql $sql ' ORDER BY s.date desc' ;
  74.         if ($length != -1) {
  75.             $sql $sql ' LIMIT ' $length ' OFFSET ' $start;
  76.         }
  77.         $countFilter 0;
  78.         $queryBuilderCount $this->em->getConnection()->prepare($sqlCount);
  79.         $countFilter $queryBuilderCount->executeQuery()->fetchOne();
  80.         $stmt $this->em->getConnection()->prepare($sql);
  81.         $result $stmt->executeQuery()->fetchAllAssociative();
  82.         return array(
  83.             'data' => $result,
  84.             'filtre' => $countFilter
  85.         );
  86.     }
  87.     /**
  88.      * @throws Exception
  89.      */
  90.     public function getJournalList(array $dataReceivestring $search nullint $start 0int $length = -1, array $order = array())
  91.     {
  92.         $columns = array(
  93.             => 'date',
  94.             => 'nbr_ventes',
  95.             => 'montant'
  96.         );
  97.         $userFilter $dataReceive[2] != "all" " and s.user_id = "intval($dataReceive[2]) : "";
  98.         $sql "select 
  99.                         date_format(s.date, '%d %b %Y') as date,
  100.                         count(distinct s.id) as nbr_ventes,
  101.                         COALESCE(SUM(si.amount), 0) + COALESCE(SUM(sp.amount), 0) AS montant
  102.                     from sales s
  103.                     left join sale_items si on s.id = si.sale_id 
  104.                     left join sale_packs sp on s.id = sp.sale_id 
  105.                     join shop s2 on (s2.id = s.shop_id)
  106.                     where s.deleted != 1  and s.error != 1 and s2.id = " $dataReceive[0] . "
  107.                     and date_format(s.date, '%m%Y') = " $dataReceive[1] . $userFilter ;
  108.         $sqlCount "select count(distinct date_format(s.date, '%d %b %Y'))
  109.                     from sales s
  110.                     left join sale_items si on s.id = si.sale_id 
  111.                     left join sale_packs sp on s.id = sp.sale_id 
  112.                     join shop s2 on (s2.id = s.shop_id)
  113.                     where s.deleted != 1 and s.error != 1  and s2.id = " $dataReceive[0] . "
  114.                     and date_format(s.date, '%m%Y') = " $dataReceive[1] . $userFilter ;
  115.         //apply search if requested
  116.         if ($search != null) {
  117.             $conditions ' AND (s2.label like "%' $search '%")';
  118.             $sql $sql $conditions;
  119.             $sqlCount $sqlCount $conditions;
  120.         }
  121.         $sql .= ' group by date_format(s.date, \'%d %b %Y\')';
  122.         //apply order if requested
  123.         if ($length == -1) {
  124.             $sql $sql ' ORDER BY ' $columns[$order[0]['column']] . ' ' $order[0]['dir'];
  125.         } else {
  126.             $sql $sql ' ORDER BY ' $columns[$order[0]['column']] . ' ' $order[0]['dir'] . ' LIMIT ' $length ' OFFSET ' $start;
  127.         }
  128.         $countFilter 0;
  129.         $queryBuilderCount $this->em->getConnection()->prepare($sqlCount);
  130.         $countFilter $queryBuilderCount->executeQuery()->fetchOne();
  131.         $stmt $this->em->getConnection()->prepare($sql);
  132.         $result $stmt->executeQuery()->fetchAllAssociative();
  133.         return array(
  134.             'data' => $result,
  135.             'filtre' => $countFilter
  136.         );
  137.     }
  138.     /**
  139.      * @throws Exception
  140.      */
  141.     public function getSalesDetailsList($dataReceive=[], int $start 0int $length = -1, array $order = array())
  142.     {
  143.         $columns = array(
  144.             => 'mois',
  145.             => 'nb_vente',
  146.             => 'ca',
  147.             => 'couts',
  148.             => 'benef'
  149.         );
  150.         $shopFilter '';
  151.         $today = new \DateTime();
  152.         $year $today->format('Y');
  153.         if($dataReceive != []){
  154.             $year $dataReceive[0];
  155.             $shopFilter .= isset($dataReceive[1]) ? ' and s.shop_id ='$dataReceive[1] : '';
  156.         }
  157.         $sql "
  158.                 select 
  159.                 date_format(s.`date` ,'%m') as mois 
  160.                 , count(distinct s.id) as nb_vente
  161.                 , COALESCE(SUM(si.amount), 0) + COALESCE(SUM(sp.amount), 0) AS ca
  162.                 , COALESCE(SUM(si.costs), 0) + COALESCE(SUM(sp.costs), 0) AS couts
  163.                 , COALESCE(SUM(si.benef), 0) + COALESCE(SUM(sp.benef), 0) AS benef
  164.                    from sales s
  165.                    left join sale_items si on s.id = si.sale_id 
  166.                    left join sale_packs sp on s.id = sp.sale_id 
  167.                    where s.deleted != 1 
  168.                    -- and si.costs is not null 
  169.                    -- and si.count < 5000
  170.                    and date_format(s.`date` , '%Y') = " $year $shopFilter "
  171.                group by mois 
  172.                 ";
  173.         //apply search if requested
  174. //        if ($search != null) {
  175. //            $conditions = ' AND (s2.label like "%'.$search.'%")';
  176. //            $sql = $sql . $conditions;
  177. //        }
  178.         //apply order if requested
  179.         if ($length == -1) {
  180.             $sql $sql ' ORDER BY ' $columns[$order[0]['column']] . ' ' $order[0]['dir'];
  181.         } else {
  182.             $sql $sql ' ORDER BY ' $columns[$order[0]['column']] . ' ' $order[0]['dir'] . ' LIMIT ' $length ' OFFSET ' $start;
  183.         }
  184.         $stmt $this->em->getConnection()->prepare($sql);
  185.         $result $stmt->executeQuery()->fetchAllAssociative();
  186.         return array(
  187.             'data' => $result,
  188.             'filtre' => 0
  189.         );
  190.     }
  191.     /**
  192.      * @throws Exception
  193.      */
  194.     public function getTotalYear($selectedShop$year)
  195.     {
  196.         $shopFilter '';
  197.         if($selectedShop != null){
  198.             $shopFilter .= ' and s.shop_id ='$selectedShop;
  199.         }
  200.         $sql "select count(distinct s.ID)
  201.                     from sales s
  202.                     where  s.deleted != 1 and date_format(s.`date` , '%Y') = " $year $shopFilter;
  203.         $stmt $this->em->getConnection()->prepare($sql);
  204.         return $stmt->executeQuery()->fetchOne();
  205.     }
  206.     /**
  207.      * @throws Exception
  208.      */
  209.     public function getTotalWeek($dateFilter null$selectedShop null)
  210.     {
  211.         $shopFilter '';
  212.         if($selectedShop != null){
  213.             $shopFilter .= ' and s.shop_id ='$selectedShop;
  214.         }
  215.         $datetime = new \DateTime();
  216.         if ($dateFilter != null) {
  217.             $datetime $datetime->createFromFormat('Y-m-d'$dateFilter);
  218.         }
  219.         $date $datetime->format('Y-m-d');
  220.         $week strftime("%U"strtotime($date));
  221.         $year $datetime->format('Y');
  222.         $sql "select count(distinct s.ID)
  223.                     from sales s
  224.                     where  date_format(s.`date` , '%Y') =  "$year ." and date_format(s.`date` , '%U') = " $week $shopFilter;
  225.         $stmt $this->em->getConnection()->prepare($sql);
  226.         return $stmt->executeQuery()->fetchOne();
  227.     }
  228.     /**
  229.      * @throws Exception
  230.      */
  231.     public function getNumbersByMonth($selectedShop$year): array
  232.     {
  233.         $shopFilter '';
  234.         if($selectedShop != null){
  235.             $shopFilter .= ' and s.shop_id ='$selectedShop;
  236.         }
  237.         $sql "select 
  238.                date_format(s.`date` ,'%m') as mois 
  239.                ,COALESCE(SUM(si.amount), 0) + COALESCE(SUM(sp.amount), 0) AS ca
  240.                , COALESCE(SUM(si.benef), 0) + COALESCE(SUM(sp.benef), 0) AS benef
  241.                    from sales s
  242.                    left join sale_items si on s.id = si.sale_id 
  243.                    left join sale_packs sp on s.id = sp.sale_id 
  244.                    where s.deleted != 1
  245.                    and date_format(s.`date` , '%Y') = " $year $shopFilter "
  246.                group by mois 
  247.                 ORDER BY mois asc;
  248.                 ";
  249.         $stmt $this->em->getConnection()->prepare($sql);
  250.         return $stmt->executeQuery()->fetchAllAssociative();
  251.     }
  252.     /**
  253.      * @throws Exception
  254.      */
  255.     public function getNumbersByDay($dateFilter null$selectedShop null): array
  256.     {
  257.         $shopFilter '';
  258.         if($selectedShop != null){
  259.             $shopFilter .= ' and s.shop_id ='$selectedShop;
  260.         }
  261.         $datetime = new \DateTime();
  262.         if ($dateFilter != null) {
  263.             $datetime $datetime->createFromFormat('Y-m-d'$dateFilter);
  264.         }
  265.         $date $datetime->format('Y-m-d');
  266.         $week strftime("%U"strtotime($date));
  267.         $year $datetime->format('Y');
  268.         $sql "
  269.                 select 
  270.                                date_format(s.`date` ,'%w') as day 
  271.                                 , COALESCE(SUM(si.amount), 0) + COALESCE(SUM(sp.amount), 0) AS ca
  272.                                 , COALESCE(SUM(si.benef), 0) + COALESCE(SUM(sp.benef), 0) AS benef
  273.                                 from sales s
  274.                                 left join sale_items si on s.id = si.sale_id 
  275.                                 left join sale_packs sp on s.id = sp.sale_id 
  276.                                 where s.deleted != 1
  277.                                 and date_format(s.`date` , '%Y') = " $year " and  date_format(s.`date` , '%U') = " $week $shopFilter "
  278.                                group by day
  279.                 ";
  280.         $stmt $this->em->getConnection()->prepare($sql);
  281.         return $stmt->executeQuery()->fetchAllAssociative();
  282.     }
  283.     /**
  284.      * @throws Exception
  285.      */
  286.     public function getNumbersByCat($selectedShop$year): array
  287.     {
  288.         $shopFilter '';
  289.         if($selectedShop != null){
  290.             $shopFilter .= ' and s.shop_id ='$selectedShop;
  291.         }
  292.         $sql "
  293.                 select 
  294.                ifnull( c.label , 'Inconnue')  as cat 
  295.                ,COALESCE(SUM(si.amount), 0) + COALESCE(SUM(sp.amount), 0) AS nb_vente
  296.                    from sales s
  297.                    left join sale_items si on s.id = si.sale_id 
  298.                    left join sale_packs sp on s.id = sp.sale_id 
  299.                    left join product p on p.id = si.product_id 
  300.                    left join category c on c.id = p.category_id 
  301.                    where s.deleted != 1 
  302.                    -- and si.costs is not null 
  303.                    -- and si.count < 5000
  304.                    and date_format(s.`date` , '%Y') = " $year $shopFilter "
  305.                group by c.id
  306.                 ";
  307.         $stmt $this->em->getConnection()->prepare($sql);
  308.         return $stmt->executeQuery()->fetchAllAssociative();
  309.     }
  310.     /**
  311.      * @throws Exception
  312.      */
  313.     public function getTrafficByDay($selectedShop null): array
  314.     {
  315.         $today = new \DateTime();
  316.         $month $today->format('m');
  317.         $shopFilter '';
  318.         if($selectedShop != null){
  319.             $shopFilter .= ' and s.shop_id ='$selectedShop;
  320.         }
  321.         $sql "select 
  322.                     req.day,
  323.                     sum(req.cat1) as 8_12, 
  324.                     sum(req.cat2) as 12_16, 
  325.                     sum(req.cat3) as 16_20, 
  326.                     sum(req.cat4) as 20_00
  327.                     from(
  328.                         select s.id as vente, DAYOFWEEK(s.date) as day, s.shop_id as shop,
  329.                         if (date_format(s.`date`, '%H') >= 8 and date_format(s.`date`, '%H') < 12 , 1 , 0 ) as cat1,
  330.                         if (date_format(s.`date`, '%H') >= 12 and date_format(s.`date`, '%H') < 16 , 1 , 0 ) as cat2,
  331.                         if (date_format(s.`date`, '%H') >= 16 and date_format(s.`date`, '%H') < 20 , 1 , 0 ) as cat3,
  332.                         if (date_format(s.`date`, '%H') >= 20 and date_format(s.`date`, '%H') <= 23 , 1 , 0 ) as cat4
  333.                         from sales s
  334.                         where s.deleted != 1 and date_format(s.`date` , '%m') = " $month $shopFilter "
  335.                     )req
  336.                 group by day;
  337.                 ";
  338.         $stmt $this->em->getConnection()->prepare($sql);
  339.         return $stmt->executeQuery()->fetchAllAssociative();
  340.     }
  341. //    /**
  342. //     * @return Sales[] Returns an array of Sales objects
  343. //     */
  344. //    public function findByExampleField($value): array
  345. //    {
  346. //        return $this->createQueryBuilder('s')
  347. //            ->andWhere('s.exampleField = :val')
  348. //            ->setParameter('val', $value)
  349. //            ->orderBy('s.id', 'ASC')
  350. //            ->setMaxResults(10)
  351. //            ->getQuery()
  352. //            ->getResult()
  353. //        ;
  354. //    }
  355. //    public function findOneBySomeField($value): ?Sales
  356. //    {
  357. //        return $this->createQueryBuilder('s')
  358. //            ->andWhere('s.exampleField = :val')
  359. //            ->setParameter('val', $value)
  360. //            ->getQuery()
  361. //            ->getOneOrNullResult()
  362. //        ;
  363. //    }
  364. }