src/Repository/ProductRepository.php line 1308

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\Product;
  4. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  5. use Doctrine\Common\Persistence\ManagerRegistry;
  6. use Doctrine\ORM\QueryBuilder;
  7. use Doctrine\ORM\Query\ResultSetMapping;
  8. use App\Entity\User;
  9. use App\Service\UserService;
  10. class ProductRepository extends ServiceEntityRepository
  11. {
  12.     private $userServ;
  13.     public function __construct(ManagerRegistry $registryUserService $userServ)
  14.     {
  15.         parent::__construct($registryProduct::class);
  16.         $this->userServ $userServ;
  17.     }
  18.     public function searchForProducts($search_query$countOnly false$sorting "popularity"$order "desc") {
  19.       $search_query_htmlentities htmlentities($search_query);
  20.       $search_query_htmlentities str_replace(" ""%"$search_query_htmlentities);
  21.       $search_query_htmlentities "%$search_query_htmlentities%";
  22.       $search_query str_replace("’""%"$search_query);
  23.       $search_query str_replace("'""%"$search_query);
  24.       $search_query str_replace("-""%"$search_query);
  25.       $search_query str_replace(" ""%"$search_query);
  26.       $search_query str_replace("º""%"$search_query);
  27.       $tag_search_query $search_query;
  28.       if (strlen($tag_search_query) > 3) {
  29.         $tag_search_query "%$tag_search_query%";
  30.       }
  31.       $search_query "%$search_query%";
  32.       $em $this->getEntityManager();
  33.       $conn $em->getConnection();
  34.       $sql "
  35.       SELECT DISTINCT product.id, product.company_id, product.orig_name, product.creation_date,
  36.       IF (product.orig_name LIKE ?, 20, IF (product.orig_brand_name LIKE ?, 10, 0))
  37.           + IF (product.orig_brand_name LIKE ?, 10, 0)
  38.           + IF (company.name LIKE ?, 5, 0)
  39.           + IF (tag.name LIKE ?, 5, 0)
  40.           AS weight,
  41.       ( ( (SELECT COUNT(cart_product.id) FROM cart_product WHERE cart_product.product_id = product.id) + 1 ) / 100 ) as total_cart
  42.       FROM product
  43.       LEFT JOIN company ON company.id = product.company_id
  44.       LEFT JOIN tag_product ON tag_product.product_id = product.id
  45.       LEFT JOIN tag ON tag.id = tag_product.tag_id
  46.       LEFT JOIN pricing_product ON pricing_product.product_id = product.id
  47.       LEFT JOIN pricing ON pricing.id = pricing_product.pricing_id
  48.       WHERE (
  49.         product.orig_name LIKE ?
  50.         OR product.orig_brand_name LIKE ?
  51.         OR product.orig_description LIKE ?
  52.         OR product.list_of_ingredients LIKE ?
  53.         OR company.name LIKE ?
  54.         OR tag.name LIKE ?
  55.       ) AND (
  56.         product.draft != 1
  57.         AND product.available > 0
  58.         AND product.is_hri = 0
  59.         AND product.is_displayed_in_association_only = 0
  60.         AND product.main_image_id IS NOT NULL
  61.         AND company.show_publicly = 1
  62.       ) AND
  63.       (
  64.         (
  65.           product.qty_ready_to_ship > 0
  66.           AND product.delivery_type = 0
  67.         ) OR (
  68.           product.delivery_type != 0
  69.         )
  70.       )
  71.       ";
  72.       if ($sorting == "popularity") {
  73.         if (strtolower($order) == "asc") {
  74.           $order "ASC";
  75.         } else {
  76.           $order "DESC";
  77.         }
  78.         $sql .= "
  79.         ORDER BY ( `weight` + `total_cart` ) $order
  80.         ";
  81.       }
  82.       if ($sorting == "price") {
  83.         if (strtolower($order) == "asc") {
  84.           $order "ASC";
  85.         } else {
  86.           $order "DESC";
  87.         }
  88.         $sql .= "
  89.         ORDER BY pricing.price $order
  90.         ";
  91.       }
  92.       if ($sorting == "date") {
  93.         if (strtolower($order) == "asc") {
  94.           $order "ASC";
  95.         } else {
  96.           $order "DESC";
  97.         }
  98.         $sql .= "
  99.         ORDER BY product.creation_date $order
  100.         ";
  101.       }
  102.       $stmt $conn->prepare($sql);
  103.       $stmt->bindValue(1$search_query);
  104.       $stmt->bindValue(2$search_query);
  105.       $stmt->bindValue(3$search_query);
  106.       $stmt->bindValue(4$search_query);
  107.       $stmt->bindValue(5$tag_search_query);
  108.       $stmt->bindValue(6$search_query);
  109.       $stmt->bindValue(7$search_query);
  110.       $stmt->bindValue(8$search_query_htmlentities);
  111.       $stmt->bindValue(9$search_query_htmlentities);
  112.       $stmt->bindValue(10$search_query);
  113.       $stmt->bindValue(11$tag_search_query);
  114.       $stmt->execute();
  115.       $results $stmt->fetchAll();
  116.       $products = [];
  117.       foreach($results as $result) {
  118.         $product $this->createQueryBuilder('p')
  119.         ->leftJoin('p.company''company')
  120.         ->andWhere('p.id = :selected_product')
  121.         ->setParameter('selected_product'$result["id"])
  122.         ->getQuery()->getResult();
  123.         ;
  124.         if ($product) {
  125.           $products[current($product)->getId()] = current($product);
  126.         }
  127.       }
  128.       if ($countOnly) {
  129.         return count($products);
  130.       }
  131.       return $products;
  132.     }
  133.     public function searchForProductsSuggestions($search_query) {
  134.       $search_query_htmlentities htmlentities($search_query);
  135.       $search_query_htmlentities str_replace(" ""%"$search_query_htmlentities);
  136.       $search_query_htmlentities "%$search_query_htmlentities%";
  137.       $search_query str_replace("’""%"$search_query);
  138.       $search_query str_replace("'""%"$search_query);
  139.       $search_query str_replace("-""%"$search_query);
  140.       $search_query str_replace(" ""%"$search_query);
  141.       $search_query str_replace("º""%"$search_query);
  142.       $tag_search_query $search_query;
  143.       if (strlen($tag_search_query) > 3) {
  144.         $tag_search_query "%$tag_search_query%";
  145.       }
  146.       $search_query "%$search_query%";
  147.       $em $this->getEntityManager();
  148.       $conn $em->getConnection();
  149.       $sql "
  150.       SELECT DISTINCT product.id, product.company_id, product.orig_name,
  151.       IF (product.orig_name LIKE ?, 20, IF (product.orig_brand_name LIKE ?, 10, 0))
  152.           + IF (product.orig_brand_name LIKE ?, 10, 0)
  153.           + IF (company.name LIKE ?, 5, 0)
  154.           + IF (tag.name LIKE ?, 5, 0)
  155.           AS weight,
  156.       ( ( (SELECT COUNT(cart_product.id) FROM cart_product WHERE cart_product.product_id = product.id) + 1 ) / 100 ) as total_cart
  157.       FROM product
  158.       LEFT JOIN company ON company.id = product.company_id
  159.       LEFT JOIN tag_product ON tag_product.product_id = product.id
  160.       LEFT JOIN tag ON tag.id = tag_product.tag_id
  161.       WHERE (
  162.         product.orig_name LIKE ?
  163.         OR product.orig_brand_name LIKE ?
  164.         OR product.orig_description LIKE ?
  165.         OR product.list_of_ingredients LIKE ?
  166.         OR company.name LIKE ?
  167.         OR tag.name LIKE ?
  168.       ) AND (
  169.         product.draft != 1
  170.         AND product.available > 0
  171.         AND product.is_hri = 0
  172.         AND product.is_displayed_in_association_only = 0
  173.         AND product.main_image_id IS NOT NULL
  174.         AND company.show_publicly = 1
  175.       ) AND
  176.       (
  177.         (
  178.           product.qty_ready_to_ship > 0
  179.           AND product.delivery_type = 0
  180.         ) OR (
  181.           product.delivery_type != 0
  182.         )
  183.       )
  184.       ORDER BY ( `weight` + `total_cart` ) DESC
  185.       ";
  186.       $stmt $conn->prepare($sql);
  187.       $stmt->bindValue(1$search_query);
  188.       $stmt->bindValue(2$search_query);
  189.       $stmt->bindValue(3$search_query);
  190.       $stmt->bindValue(4$search_query);
  191.       $stmt->bindValue(5$tag_search_query);
  192.       $stmt->bindValue(6$search_query);
  193.       $stmt->bindValue(7$search_query);
  194.       $stmt->bindValue(8$search_query_htmlentities);
  195.       $stmt->bindValue(9$search_query_htmlentities);
  196.       $stmt->bindValue(10$search_query);
  197.       $stmt->bindValue(11$tag_search_query);
  198.       $stmt->execute();
  199.       $results $stmt->fetchAll();
  200.       $products = [];
  201.       foreach($results as $result) {
  202.         $product $this->createQueryBuilder('p')
  203.         ->leftJoin('p.company''company')
  204.         ->andWhere('p.id = :selected_product')
  205.         ->setParameter('selected_product'$result["id"])
  206.         ->getQuery()->getResult();
  207.         ;
  208.         if ($product) {
  209.           $products[current($product)->getId()] = current($product);
  210.         }
  211.       }
  212.       return $products;
  213.       //
  214.       // $products = $this->createQueryBuilder('p');
  215.       //
  216.       // $products
  217.       // ->leftJoin('p.tags', 't')
  218.       // ->leftJoin('p.company', 'c')
  219.       // ->andWhere('p.draft != 1')
  220.       // ->andWhere('p.available = 1')
  221.       // ->andWhere('c.showPublicly = 1')
  222.       // ;
  223.       //
  224.       // if(!$this->userServ->getAssociationUserIsBrowsing()) {
  225.       //   $products->andWhere('p.isDisplayedInAssociationOnly != true');
  226.       // }
  227.       //
  228.       // $products
  229.       // ->andWhere('p.origName LIKE :search_query OR p.origBrandName LIKE :search_query OR t.name LIKE :search_query OR c.name LIKE :search_query')
  230.       // ->setParameter('search_query', $search_query)
  231.       // ->orderBy('size(p.inCarts)', 'desc')
  232.       // // ->groupBy('p.company')
  233.       // ;
  234.       //
  235.       // if (count($products->getQuery()->getResult()) < 5) {
  236.       //   $products->groupBy('p.company');
  237.       // }
  238.       //
  239.       // return $products->getQuery();
  240.     }
  241.     /*
  242.      * Return all the products that are low in quantity with no reminder sent
  243.      * Only fetch those that sold something to assure to get only those needed
  244.      */
  245.     public function findWithLowQuantity($limit=30)
  246.     {
  247.         $qb $this->createQueryBuilder('p')
  248.             ->join('p.inCarts''c')
  249.             ->leftJoin('c.cart''cart')
  250.             ->andWhere('cart.isPaid = true')
  251.             ->andWhere('p.draft != 1')
  252.             ->andWhere('p.reminderLowQuantitySent = false')
  253.             ->andWhere('p.DeliveryType = 0')
  254.             ->andWhere('p.available = 1')
  255.             ->andWhere('p.isJustInTime = false')
  256.             ->andWhere('p.qtyReadyToShip < p.minimumInStorage')
  257.             ->andWhere('p.minimumInStorage != 0');
  258.         // todo is it revelant here ?
  259.         if(!$this->userServ->getAssociationUserIsBrowsing())
  260.             $qb->andWhere('p.isDisplayedInAssociationOnly != true');
  261.         $qb->groupBy('p.id')
  262.             ->orderBy('p.company''DESC')
  263.             ->setMaxResults($limit);
  264.         return $qb->getQuery()->execute();
  265.     }
  266.     /**
  267.      * @param $price
  268.      * @return Product[]
  269.      */
  270.     public function findAllGreaterThanPrice($price): array
  271.     {
  272.         // automatically knows to select Products
  273.         // the "p" is an alias you'll use in the rest of the query
  274.         $qb $this->createQueryBuilder('p')
  275.             ->andWhere('p.draft != 1')
  276.             ->andWhere('p.available = 1')
  277.             ->andWhere('p.price > :price');
  278.         if(!$this->userServ->getAssociationUserIsBrowsing())
  279.             $qb->andWhere('p.isDisplayedInAssociationOnly = false');
  280.         if(true// later check if user is browsing HRI
  281.             $qb->andWhere('p.isConsumer = true');
  282.         $qb->setParameter('price'$price)
  283.             ->orderBy('p.price''ASC')
  284.             ->getQuery();
  285.         return $qb->execute();
  286.         // to get just one result:
  287.         // $product = $qb->setMaxResults(1)->getOneOrNullResult();
  288.     }
  289.     /*
  290.      * Return the total of Product in a category
  291.      */
  292.     public function getTotalProducts($companyId$categoryId=false){
  293.         $product $this->createQueryBuilder('m')
  294.             ->select('count(m.id)')
  295.             ->where('m.company = :companyId')
  296.             ->andWhere('m.draft != 1')
  297.             ->andWhere('m.available = 1')
  298.             ->andWhere('m.mainImage IS NOT NULL');
  299.         if(!$this->userServ->getAssociationUserIsBrowsing())
  300.             $product->andWhere('m.isDisplayedInAssociationOnly = false');
  301.         if(true// later check if user is browsing HRI
  302.             $product->andWhere('m.isConsumer = true');
  303.         $product->setParameter('companyId'$companyId);
  304.         if($categoryId){
  305.             $product->andWhere('m.categories = :categoryId')
  306.             ->setParameter('categoryId'$categoryId);
  307.         }
  308.         $amount $product->getQuery()
  309.             ->getSingleScalarResult();
  310.         return $amount;
  311.     }
  312.     /*
  313.      * Return all the categories used
  314.      */
  315.     public function getAllUsedCategories($companyId){
  316.         $product $this->createQueryBuilder('c')
  317.             ->where('c.company = :companyId')
  318.             ->andWhere('c.draft != 1')
  319.             ->andWhere('c.available = 1')
  320.             ->andWhere('c.mainImage IS NOT NULL')
  321.             ->setParameter('companyId'$companyId)
  322.             ->groupBy('c.categories')
  323.             ->orderBy('c.categories''DESC')
  324.             ->getQuery();
  325.         //@TODO Need a better way
  326.         $results $product->getResult();
  327.         $cats = array();
  328.         foreach($results as $res){
  329.             $key $res->getCategories()->getId();
  330.             $cats[$key]=$res->getCategories()->getName();
  331.         }
  332.         return $cats;
  333.     }
  334.     public function validateBrowsingAssociationInQuery(QueryBuilder $products): QueryBuilder{
  335.         if($this->userServ->getAssociationUserIsBrowsing()){
  336.             $products
  337.                 ->leftJoin('company.associations''associations')
  338.                 ->andWhere('associations = :association')
  339.                 ->setParameter('association'$this->userServ->getAssociationUserIsBrowsing());
  340.             //$products->andWhere('p.isDisplayedInAssociationOnly != true');
  341.         } else {
  342.             $products->andWhere('p.isDisplayedInAssociationOnly = false');
  343.         }
  344.         return $products;
  345.     }
  346.     /*
  347.       Return limited numbers of products with a specific category
  348.       and that are ready for being shown on the front page
  349.       - Results must be limited to $maxresults
  350.       - Must be in specified category
  351.       - Must be Salable()
  352.       - Must be ShippedByMaturin()
  353.       - (TODO) Maximum of 2 products per company
  354.       - (TODO) Must respect:  if (!$associationUserIsBrowsing && $product->getIsDisplayedInAssociationOnly())
  355.     */
  356.     public function getProductsFromCategoryForFrontpage($category$maxResults 10) {
  357.       $em $this->getEntityManager();
  358.       $conn $em->getConnection();
  359.       $subCategories $category->getAllSubCategories();
  360.       $subCategoriesIds = [];
  361.       foreach($subCategories as $subCategory) {
  362.         $subCategoriesIds[] = $subCategory->getId();
  363.       }
  364.       $subCategoriesIds implode(",",$subCategoriesIds);
  365.       $sql "
  366.       SELECT DISTINCT product.id, product.company_id
  367.       FROM product
  368.       LEFT JOIN company ON company.id = product.company_id
  369.       WHERE (
  370.         product.categories_id = ?
  371.         OR product.sub_category_id = ?
  372.         OR product.categories_id IN (?)
  373.         OR product.sub_category_id IN (?)
  374.       ) AND (
  375.         product.draft != 1
  376.         AND product.qty_ready_to_ship > 0
  377.         AND product.delivery_type = 0
  378.         AND product.delivery_mail = 1
  379.         AND product.available > 0
  380.         AND product.is_hri = 0
  381.         AND product.is_displayed_in_association_only = 0
  382.         AND product.main_image_id IS NOT NULL
  383.         AND company.show_publicly = 1
  384.       )
  385.       ORDER BY RAND()
  386.       LIMIT 4
  387.       ";
  388.       $stmt $conn->prepare($sql);
  389.       $stmt->bindValue(1$category->getId());
  390.       $stmt->bindValue(2$category->getId());
  391.       $stmt->bindValue(3$subCategoriesIds);
  392.       $stmt->bindValue(4$subCategoriesIds);
  393.       $stmt->execute();
  394.       $selected_products $stmt->fetchAll();
  395.       $selected_products array_slice($selected_products04);
  396.       shuffle($selected_products);
  397.       $products $this->createQueryBuilder('p')
  398.           ->leftJoin('p.company''company')
  399.           ->addSelect('company')
  400.           ->leftJoin('p.storages''storages')
  401.           ->addSelect('storages')
  402.           ->leftJoin('p.pricings''pricings')
  403.           ->addSelect('pricings')
  404.           ->leftJoin('pricings.products''pproduct')
  405.           ->addSelect('pproduct')
  406.           ->andWhere('p.id IN (:selected_products)')
  407.           ->setParameter('selected_products'$selected_products)
  408.           ;
  409.       return $products->getQuery()->getResult();
  410.     }
  411.     public function getProductsForCategory($category$sorting "popularity"$order "desc") {
  412.       $em $this->getEntityManager();
  413.       $conn $em->getConnection();
  414.       $subCategories $category->getAllSubCategories();
  415.       $subCategoriesIds = [];
  416.       foreach($subCategories as $subCategory) {
  417.         $subCategoriesIds[] = $subCategory->getId();
  418.       }
  419.       $subCategoriesIds implode(",",$subCategoriesIds);
  420.       $sql "
  421.       SELECT DISTINCT product.id, product.company_id,
  422.       ( ( (SELECT COUNT(cart_product.id) FROM cart_product WHERE cart_product.product_id = product.id) + 1 ) / 100 ) as total_cart
  423.       FROM product
  424.       LEFT JOIN company ON company.id = product.company_id
  425.       LEFT JOIN pricing_product ON pricing_product.product_id = product.id
  426.       LEFT JOIN pricing ON pricing.id = pricing_product.pricing_id
  427.       WHERE (
  428.         product.categories_id = ?
  429.         OR product.sub_category_id = ?
  430.         OR product.categories_id IN (?)
  431.         OR product.sub_category_id IN (?)
  432.       ) AND (
  433.         product.draft != 1
  434.         AND product.available > 0
  435.         AND product.is_hri = 0
  436.         AND product.is_displayed_in_association_only = 0
  437.         AND product.main_image_id IS NOT NULL
  438.         AND company.show_publicly = 1
  439.         AND (
  440.           (
  441.             product.qty_ready_to_ship > 0
  442.             AND product.delivery_type = 0
  443.           ) OR
  444.           (
  445.             product.delivery_type != 0
  446.           )
  447.         )
  448.       )
  449.       ";
  450.       if ($sorting == "popularity") {
  451.         if (strtolower($order) == "asc") {
  452.           $order "ASC";
  453.         } else {
  454.           $order "DESC";
  455.         }
  456.         $sql .= "
  457.         ORDER BY ( `total_cart` ) $order
  458.         ";
  459.       }
  460.       if ($sorting == "price") {
  461.         if (strtolower($order) == "asc") {
  462.           $order "ASC";
  463.         } else {
  464.           $order "DESC";
  465.         }
  466.         $sql .= "
  467.         ORDER BY pricing.price $order
  468.         ";
  469.       }
  470.       if ($sorting == "date") {
  471.         if (strtolower($order) == "asc") {
  472.           $order "ASC";
  473.         } else {
  474.           $order "DESC";
  475.         }
  476.         $sql .= "
  477.         ORDER BY product.creation_date $order
  478.         ";
  479.       }
  480.       $stmt $conn->prepare($sql);
  481.       $stmt->bindValue(1$category->getId());
  482.       $stmt->bindValue(2$category->getId());
  483.       $stmt->bindValue(3$subCategoriesIds);
  484.       $stmt->bindValue(4$subCategoriesIds);
  485.       $stmt->execute();
  486.       $results $stmt->fetchAll();
  487.       $products = [];
  488.       foreach($results as $result) {
  489.         $product $this->createQueryBuilder('p')
  490.         ->leftJoin('p.company''company')
  491.         ->andWhere('p.id = :selected_product')
  492.         ->setParameter('selected_product'$result["id"])
  493.         ->getQuery()->getResult();
  494.         ;
  495.         if ($product) {
  496.           $products[current($product)->getId()] = current($product);
  497.         }
  498.       }
  499.       return $products;
  500.     }
  501.     public function getProductsSponsored($category$sorting "popularity"$order "desc") {
  502.       $em $this->getEntityManager();
  503.       $conn $em->getConnection();
  504.       $sql "
  505.       SELECT DISTINCT product.id, product.company_id,
  506.       ( ( (SELECT COUNT(cart_product.id) FROM cart_product WHERE cart_product.product_id = product.id) + 1 ) / 100 ) as total_cart
  507.       FROM product
  508.       LEFT JOIN company ON company.id = product.company_id
  509.       LEFT JOIN pricing_product ON pricing_product.product_id = product.id
  510.       LEFT JOIN pricing ON pricing.id = pricing_product.pricing_id
  511.       LEFT JOIN product_sponsored ON product_sponsored.product_id = product.id
  512.       LEFT JOIN product_sponsored_category_product_sponsored ON product_sponsored_category_product_sponsored.product_sponsored_id = product_sponsored.id
  513.       LEFT JOIN product_sponsored_category ON product_sponsored_category.id = product_sponsored_category_product_sponsored.product_sponsored_category_id
  514.       WHERE (
  515.         product_sponsored_category.url = ?
  516.         AND product_sponsored.from_date <= ?
  517.         AND product_sponsored.to_date >= ?
  518.         AND product_sponsored.active = 1
  519.       ) AND (
  520.         product.draft != 1
  521.         AND product.available > 0
  522.         AND company.show_publicly = 1
  523.         AND (
  524.           (
  525.             product.qty_ready_to_ship > 0
  526.             AND product.delivery_type = 0
  527.           ) OR
  528.           (
  529.             product.delivery_type != 0
  530.           )
  531.         )
  532.       )
  533.       ";
  534.       if ($sorting == "popularity") {
  535.         if (strtolower($order) == "asc") {
  536.           $order "ASC";
  537.         } else {
  538.           $order "DESC";
  539.         }
  540.         $sql .= "
  541.         ORDER BY ( `total_cart` ) $order
  542.         ";
  543.       }
  544.       if ($sorting == "price") {
  545.         if (strtolower($order) == "asc") {
  546.           $order "ASC";
  547.         } else {
  548.           $order "DESC";
  549.         }
  550.         $sql .= "
  551.         ORDER BY pricing.price $order
  552.         ";
  553.       }
  554.       if ($sorting == "date") {
  555.         if (strtolower($order) == "asc") {
  556.           $order "ASC";
  557.         } else {
  558.           $order "DESC";
  559.         }
  560.         $sql .= "
  561.         ORDER BY product.creation_date $order
  562.         ";
  563.       }
  564.       $stmt $conn->prepare($sql);
  565.       $now = new \DateTime('now');
  566.       $stmt->bindValue(1$category->getUrl());
  567.       $stmt->bindValue(2$now->format("Y-m-d H:i:s"));
  568.       $stmt->bindValue(3$now->format("Y-m-d H:i:s"));
  569.       $stmt->execute();
  570.       $results $stmt->fetchAll();
  571.       $products = [];
  572.       foreach($results as $result) {
  573.         $product $this->createQueryBuilder('p')
  574.         ->leftJoin('p.company''company')
  575.         ->andWhere('p.id = :selected_product')
  576.         ->setParameter('selected_product'$result["id"])
  577.         ->getQuery()->getResult();
  578.         ;
  579.         if ($product) {
  580.           $products[current($product)->getId()] = current($product);
  581.         }
  582.       }
  583.       return $products;
  584.     }
  585.     public function getProdCategory($category$sorting "popularity"$order "desc"){
  586.       $products $this->createQueryBuilder('p')
  587.           ->Select('p.id AS id','company.id AS selected_company')
  588.           ->leftJoin('p.company''company')
  589.           ->leftJoin('p.pricings''pricing')
  590.           ->andWhere('p.categories = :category')
  591.           ->OrWhere('p.subCategory = :category')
  592.           ->OrWhere('p.subCategory IN (:subCategory)')
  593.           ->setParameter('category'$category)
  594.           ->setParameter('subCategory'$category->getAllSubCategories())
  595.           ->andWhere('p.draft != 1')
  596.           ->andWhere('p.mainImage IS NOT NULL')
  597.           ->andWhere('p.available = 1')
  598.           ->andWhere('p.isDisplayedInAssociationOnly = 0')
  599.           ->andWhere('p.isHri = 0')
  600.           ->andWhere('p.isConsumer = true')
  601.           ->andWhere('company.showPublicly = true')
  602.           ->andWhere('p.qtyReadyToShip > 0');
  603.           if ($sorting == "popularity") {
  604.             if (strtolower($order) == "asc") {
  605.               $order "ASC";
  606.             } else {
  607.               $order "DESC";
  608.             }
  609.              $sorting 'size(p.inCarts)';
  610.           }
  611.           if ($sorting == "price") {
  612.             if (strtolower($order) == "asc") {
  613.               $order "ASC";
  614.             } else {
  615.               $order "DESC";
  616.             }
  617.             $sorting "pricing.price";
  618.           }
  619.           if ($sorting == "date") {
  620.             if (strtolower($order) == "asc") {
  621.               $order "ASC";
  622.             } else {
  623.               $order "DESC";
  624.             }
  625.             $sorting ="p.creationDate";
  626.           }
  627.           $products->orderBy$sorting$order );
  628.           $results =  $products->getQuery()->getResult();
  629.       $prod= [];
  630.       foreach($results as $result) {
  631.         $product $this->createQueryBuilder('p')
  632.         ->leftJoin('p.company''company')
  633.         ->andWhere('p.id = :selected_product')
  634.         ->setParameter('selected_product'$result['id'])
  635.         ->getQuery()->getResult();
  636.         ;
  637.         if ($product) {
  638.           $prod[current($product)->getId()] = current($product);
  639.         }
  640.       }
  641.       return $prod;
  642.   }
  643. // récuperer le placement pour ensuite l'organiser par popularité
  644.   public function getcategoriesByPlacement($category$sorting$order){
  645.     $products $this->createQueryBuilder('p')
  646.     ->Select('p.id AS id','company.id AS selected_company','size(p.inCarts) AS panier')
  647.     ->leftJoin('p.company''company')
  648.     ->leftJoin('p.subCategory''categ')
  649.     ->leftJoin('categ.subCategory''categSub')
  650.     ->addSelect('categ.id AS cateId,categ.placement AS firstnameplacement, categ.name As firstname ,categSub.placement AS secondplacement, categSub.name As secondCateg')
  651.     ->leftJoin('p.pricings''pricing')
  652.     ->andWhere('p.categories = :category')
  653.     ->OrWhere('p.subCategory = :category')
  654.     ->OrWhere('p.subCategory IN (:subCategory)')
  655.     ->setParameter('category'$category)
  656.     ->setParameter('subCategory'$category->getAllSubCategories())
  657.     ->andWhere('p.draft != 1')
  658.     ->andWhere('p.mainImage IS NOT NULL')
  659.     ->andWhere('p.available = 1')
  660.     ->andWhere('p.isDisplayedInAssociationOnly = 0')
  661.     ->andWhere('categ.placement > 0')
  662.     ->andWhere('categSub.placement  > 0')
  663.     ->andWhere('p.isHri = 0'
  664.     ->andWhere('p.isConsumer = true')
  665.     ->andWhere('company.showPublicly = true')
  666.     ->andWhere('p.qtyReadyToShip > 0');
  667.     
  668.     if($this->userServ->getAssociationUserIsBrowsing()){
  669.       $urlnameAssociation $this->userServ->getAssociationUserIsBrowsing()->getUrlName();
  670.       $products->leftJoin('company.associations''association')
  671.               ->andWhere('association.urlName = :urlnameAssociation')
  672.               ->setParameter('urlnameAssociation'$urlnameAssociation);
  673.     }
  674.     if ($sorting != null) {
  675.      
  676.       if ($sorting == "popularity") {
  677.         if (strtolower($order) == "asc") {
  678.           $order "ASC";
  679.         } else {
  680.           $order "DESC";
  681.         }
  682.   
  683.          $sorting 'size(p.inCarts)';
  684.   
  685.       }
  686.       if ($sorting == "price") {
  687.         if (strtolower($order) == "asc") {
  688.           $order "ASC";
  689.         } else {
  690.           $order "DESC";
  691.         }
  692.        
  693.         $sorting "pricing.price";
  694.         
  695.       }
  696.       if ($sorting == "date") {
  697.         if (strtolower($order) == "asc") {
  698.           $order "ASC";
  699.         } else {
  700.           $order "DESC";
  701.         }
  702.   
  703.         $sorting ="p.creationDate";
  704.       }
  705.       $products->orderBy$sorting $order);
  706.     }else{
  707.       $products->orderBy'categ.placement');
  708.     }
  709.     
  710.     
  711.     $results =  $products->getQuery()->getResult();
  712.    
  713.    $prod= [];
  714.       foreach($results as $result) {
  715.         $product $this->createQueryBuilder('p')
  716.         ->leftJoin('p.company''company')
  717.         ->andWhere('p.id = :selected_product')
  718.         ->setParameter('selected_product'$result['id'])
  719.         ->getQuery()->getResult();
  720.         ;
  721.         if ($product) {
  722.           $prod[current($product)->getId()] = current($product);
  723.         }
  724.       }
  725.     
  726.   //  dd($categFinal);
  727.     return $prod;
  728. }
  729.     public function getProductsForAllCategory($sorting "popularity"$order "desc") {
  730.       $em $this->getEntityManager();
  731.       $conn $em->getConnection();
  732.       $sql "
  733.       SELECT DISTINCT product.id, product.company_id,
  734.       ( ( (SELECT COUNT(cart_product.id) FROM cart_product WHERE cart_product.product_id = product.id) + 1 ) / 100 ) as total_cart
  735.       FROM product
  736.       LEFT JOIN company ON company.id = product.company_id
  737.       LEFT JOIN pricing_product ON pricing_product.product_id = product.id
  738.       LEFT JOIN pricing ON pricing.id = pricing_product.pricing_id
  739.       WHERE (
  740.         product.draft != 1
  741.         AND product.available > 0
  742.         AND product.is_hri = 0
  743.         AND product.is_displayed_in_association_only = 0
  744.         AND product.main_image_id IS NOT NULL
  745.         AND company.show_publicly = 1
  746.         AND (
  747.           (
  748.             product.qty_ready_to_ship > 0
  749.             AND product.delivery_type = 0
  750.           ) OR
  751.           (
  752.             product.delivery_type != 0
  753.           )
  754.         )
  755.       )
  756.       ";
  757.       if ($sorting == "popularity") {
  758.         if (strtolower($order) == "asc") {
  759.           $order "ASC";
  760.         } else {
  761.           $order "DESC";
  762.         }
  763.         $sql .= "
  764.         ORDER BY ( `total_cart` ) $order
  765.         ";
  766.       }
  767.       if ($sorting == "price") {
  768.         if (strtolower($order) == "asc") {
  769.           $order "ASC";
  770.         } else {
  771.           $order "DESC";
  772.         }
  773.         $sql .= "
  774.         ORDER BY pricing.price $order
  775.         ";
  776.       }
  777.       if ($sorting == "date") {
  778.         if (strtolower($order) == "asc") {
  779.           $order "ASC";
  780.         } else {
  781.           $order "DESC";
  782.         }
  783.         $sql .= "
  784.         ORDER BY product.creation_date $order
  785.         ";
  786.       }
  787.       $stmt $conn->prepare($sql);
  788.       $stmt->execute();
  789.       $results $stmt->fetchAll();
  790.       $products = [];
  791.       foreach($results as $result) {
  792.         $product $this->createQueryBuilder('p')
  793.         ->leftJoin('p.company''company')
  794.         ->andWhere('p.id = :selected_product')
  795.         ->setParameter('selected_product'$result["id"])
  796.         ->getQuery()->getResult();
  797.         ;
  798.         if ($product) {
  799.           $products[current($product)->getId()] = current($product);
  800.         }
  801.       }
  802.       return $products;
  803.     }
  804.     public function getProductsFromCategory($category$results true){
  805.         $products $this->createQueryBuilder('p')
  806.             ->leftJoin('p.company''company')
  807.             ->addSelect('company')
  808.             ->leftJoin('p.storages''storages')
  809.             ->addSelect('storages')
  810.             ->leftJoin('p.pricings''pricing')
  811.             ->andWhere('p.categories = :category')
  812.             ->OrWhere('p.subCategory = :category')
  813.             ->OrWhere('p.subCategory IN (:subCategory)')
  814.             ->setParameter('category'$category)
  815.             ->setParameter('subCategory'$category->getAllSubCategories())
  816.             ->andWhere('p.draft != 1')
  817.             ->andWhere('p.mainImage IS NOT NULL')
  818.             ->andWhere('p.available = 1')
  819.             ->andWhere('p.isHri = 0')
  820.             ->andWhere('company.showPublicly = true')
  821.             ->andWhere('p.qtyReadyToShip > 0');
  822.         $products $this->validateBrowsingAssociationInQuery($products);
  823.         if(true// later check if user is browsing HRI
  824.             $products->andWhere('p.isConsumer = true');
  825.         $products->orderBy('size(p.inCarts)'"desc");
  826.         $products->groupBy('p.id');
  827.         if($results)
  828.             return $products->getQuery()->getResult();
  829.         else
  830.             return $products;
  831.     }
  832.     /*
  833.      * Return all products
  834.      */
  835.     public function getSalableProductsWithNoDeal($returnResults true){
  836.         $products $this->createQueryBuilder('p')
  837.             ->addSelect('p')
  838.             ->leftJoin('p.pricings''pricings')
  839.             ->addSelect('pricings')
  840.             ->leftJoin('pricings.products''pproduct')
  841.             ->addSelect('pproduct')
  842.             ->where('p.draft != 1')
  843.             ->leftJoin('p.company''company')
  844.             ->andWhere('company.showPublicly = true')
  845.             ->andWhere('p.mainImage IS NOT NULL')
  846.             ->andWhere('p.available = 1')
  847.             ->leftJoin('p.deal''deal')
  848.             ->addSelect('deal')
  849.             ->leftJoin('p.discountPricings''discount')
  850.             ->addSelect('discount')
  851.             ->andWhere('deal IS NULL');
  852.         if(!$this->userServ->getAssociationUserIsBrowsing())
  853.             $products->andWhere('p.isDisplayedInAssociationOnly != true');
  854.         if(true// later check if user is browsing HRI
  855.             $products->andWhere('p.isConsumer = true');
  856.         $products->orderBy('p.qtyReadyToShip''DESC');
  857.         if($returnResults)
  858.             return $products->getQuery()->getResult();
  859.         else
  860.             return $products;
  861.     }
  862.     /*
  863.      * Return all products
  864.      * Called by the route "allCategory",
  865.      */
  866.     public function getAdminProducts($returnResults true$available=true ){
  867.         $products $this->createQueryBuilder('p')
  868.             ->leftJoin('p.pricings''pricings')
  869.             ->addSelect('pricings')
  870.             ->leftJoin('pricings.products''pproduct')
  871.             ->addSelect('pproduct')
  872.             ->where('p.draft != 1')
  873.             ->leftJoin('p.company''company')
  874.             ->andWhere('p.mainImage IS NOT NULL');
  875.         if($available)
  876.                 $products->andWhere('p.available = 1');
  877.         if(!$this->userServ->getAssociationUserIsBrowsing())
  878.             $products->andWhere('p.isDisplayedInAssociationOnly != true');
  879.         if(true// later check if user is browsing HRI
  880.             $products->andWhere('p.isConsumer = true');
  881.         $products->orderBy('size(p.inCarts)''desc')
  882.                 ->groupBy('p.id')
  883.                 ;
  884.         if($returnResults)
  885.             return $products->getQuery()->getResult();
  886.         else
  887.             return $products;
  888.     }
  889.     /*
  890.      * Return all products
  891.      * Called by the route "allCategory",
  892.      */
  893.     public function getSalableProducts($returnResults true$available=true ){
  894.         $products $this->createQueryBuilder('p')
  895.             ->leftJoin('p.pricings''pricings')
  896.             ->addSelect('pricings')
  897.             ->leftJoin('pricings.products''pproduct')
  898.             ->addSelect('pproduct')
  899.             ->where('p.draft != 1')
  900.             ->leftJoin('p.company''company')
  901.             ->andWhere('company.showPublicly = true')
  902.             ->andWhere('p.mainImage IS NOT NULL');
  903.         if($available){
  904.             $products->andWhere('p.available = 1');
  905.         }
  906.         $products $this->validateBrowsingAssociationInQuery($products);
  907.         if(true// later check if user is browsing HRI
  908.             $products->andWhere('p.isConsumer = true');
  909.         $products->orderBy('size(p.inCarts)''desc')
  910.             ->groupBy('p.id')
  911.             ;
  912.         if($returnResults)
  913.             return $products->getQuery()->getResult();
  914.         else
  915.             return $products;
  916.     }
  917.     /*
  918.      * Return amount of salable products
  919.      */
  920.     public function countSalableProducts(): int {
  921.         $amount $this->createQueryBuilder('p')
  922.             ->select('count(p.id)')
  923.             ->andWhere('p.draft != 1')
  924.             ->andWhere('p.available = 1')
  925.             ->andWhere('p.mainImage IS NOT NULL');
  926.         if(!$this->userServ->getAssociationUserIsBrowsing())
  927.             $amount->andWhere('p.isDisplayedInAssociationOnly != true');
  928.         if(true// later check if user is browsing HRI
  929.             $amount->andWhere('p.isConsumer = true');
  930.         return (int)$amount->getQuery()->getSingleScalarResult();
  931.     }
  932.     /*
  933.      * Return the total of Product in a category
  934.      */
  935.     public function getTotalProductsFromCategory($category){
  936.         $amount $this->createQueryBuilder('p')
  937.             ->select('count(p.id)')
  938.             ->where('p.categories = :category')
  939.             ->setParameter('category'$category)
  940.             ->OrWhere('p.subCategory IN (:subCategory)')
  941.             ->setParameter('subCategory'$category->getAllSubCategories())
  942.             ->andWhere('p.draft != 1')
  943.             ->andWhere('p.available = 1')
  944.             ->andWhere('p.mainImage IS NOT NULL');
  945.         if(!$this->userServ->getAssociationUserIsBrowsing())
  946.             $amount->andWhere('p.isDisplayedInAssociationOnly != true');
  947.         if(true// later check if user is browsing HRI
  948.             $amount->andWhere('p.isConsumer = true');
  949.             $amount->getQuery()
  950.             ->getSingleScalarResult();
  951.         return $amount;
  952.     }
  953.     public function countProductsInCategory($category) {
  954.       $count $this->createQueryBuilder('p')
  955.           ->select('count(p.id)')
  956.           ->where('p.categories IN (:category)')
  957.           ->setParameter('category'$category)
  958.           ->OrWhere('p.subCategory IN (:subCategory)')
  959.           ->setParameter('subCategory', [$category$category->getAllSubCategories()])
  960.           ->andWhere('p.draft != 1')
  961.           ->andWhere('p.available = 1')
  962.           ->andWhere('p.mainImage IS NOT NULL')
  963.           ->getQuery()->getSingleScalarResult();
  964.       return $count;
  965.     }
  966.     /*
  967.      * Look products with certain criteria
  968.      */
  969.     public function getProductsFromTags($tags=false){
  970.         $products $this->createQueryBuilder('t');
  971.         $results $products
  972.             ->where('t.draft != 1')
  973.             ->andWhere('t.available = 1')
  974.             ->andWhere('t.mainImage IS NOT NULL')
  975.             ->leftJoin('t.company''company')
  976.             ->andWhere('company.showPublicly = true')
  977.             ->andWhere('t.qtyReadyToShip > 1');
  978.         if(!$this->userServ->getAssociationUserIsBrowsing())
  979.             $results->andWhere('t.isDisplayedInAssociationOnly != true');
  980.         if(true// later check if user is browsing HRI
  981.             $results->andWhere('t.isConsumer = true');
  982.         $results $results->setMaxResults(20)
  983.             ->orderBy('RAND()')
  984.             ->getQuery()->getResult();
  985.         if($results)
  986.             shuffle($results);
  987.         return $results;
  988.     }
  989.     /*
  990.      * Return Products based on a user past 'likes'
  991.      * todo: BASED ON LIKES ? mmmmh... not shure..
  992.      */
  993.     public function getProductsForUser($user){
  994.         $products $this->createQueryBuilder('p');
  995.         $results $products
  996.             ->where('p.draft != 1')
  997.             ->leftJoin('p.company''company');
  998.         $results $this->validateBrowsingAssociationInQuery($results);
  999.         if(true// later check if user is browsing HRI
  1000.             $products->andWhere('p.isConsumer = true');
  1001.         $results->andWhere('company.showPublicly = true')
  1002.             ->andWhere('p.available = 1')
  1003.             ->andWhere('p.mainImage IS NOT NULL')
  1004.             ->andWhere('p.qtyReadyToShip > 1');
  1005.         $results $results
  1006.             ->setMaxResults(20)
  1007.             ->orderBy('RAND()')
  1008.             ->getQuery()
  1009.             ->getResult();
  1010.         if($results)
  1011.             shuffle($results);
  1012.         return $results;
  1013.     }
  1014.     /*
  1015.      * Return Products based on a user past 'likes'
  1016.      */
  1017.     public function getProductsManagedByUser($user=false){
  1018.         $products $this->createQueryBuilder('t');
  1019.         $results $products
  1020.             ->where('t.draft != 1')
  1021.             ->andWhere('t.available = 1')
  1022.             ->andWhere('t.mainImage IS NOT NULL');
  1023.         if(!$this->userServ->getAssociationUserIsBrowsing())
  1024.             $results->andWhere('t.isDisplayedInAssociationOnly != true');
  1025.         if(true// later check if user is browsing HRI
  1026.             $results->andWhere('t.isConsumer = true');
  1027.         $results
  1028.             ->getQuery()
  1029.             ->getResult();
  1030.         if($results)
  1031.             shuffle($results);
  1032.         return $results;
  1033.     }
  1034.     /*
  1035.      * Return the list a products the users can book inventory storage for
  1036.      */
  1037.     public function getInventoryProducts(User $user=null){
  1038.         if(empty($user))
  1039.             $user $this->userServ->getUser();
  1040.         $companies $user->getCompanies();
  1041.         $products $this->createQueryBuilder('p')
  1042.             ->join('p.company''c')
  1043.             ->where('p.draft != 1')
  1044.             ->andWhere('p.company IN (:companies)')
  1045.             ->setParameter('companies'$companies)
  1046.         ;
  1047.         if(!$this->userServ->getAssociationUserIsBrowsing())
  1048.             $products->andWhere('p.isDisplayedInAssociationOnly != true');
  1049.         if(true// later check if user is browsing HRI
  1050.             $products->andWhere('p.isConsumer = true');
  1051.         return $products;
  1052.     }
  1053.     /*
  1054.      * Return Products of a Company'
  1055.      */
  1056.     public function findProductsForReplenishmentByCompany($companyId$noFridge true){
  1057.         $products $this->createQueryBuilder('t');
  1058.         $products
  1059.             ->where('t.draft != 1')
  1060.             ->andWhere('t.company = :companyId')
  1061.             ->andWhere('t.productDelete = 0')
  1062.             ->setParameter('companyId'$companyId)
  1063.             ->andWhere('t.mainImage IS NOT NULL')
  1064.             ;
  1065.         if($noFridge){
  1066.             $products->andWhere('t.conservation=7');
  1067.             ;
  1068.         }
  1069.         if(!$this->userServ->getAssociationUserIsBrowsing())
  1070.             $products->andWhere('t.isDisplayedInAssociationOnly != true');
  1071.         if(true// later check if user is browsing HRI
  1072.             $products->andWhere('t.isConsumer = true');
  1073.         return $products;
  1074.     }
  1075.     /*
  1076.      * Return Products of a Company'
  1077.      */
  1078.     public function getProductsByCompanyId($companyId$sorting=null$order=null$category=false $all=false$results true$available=true){
  1079.         $products $this->createQueryBuilder('t');
  1080.         
  1081.         $products
  1082.             ->where('t.draft != 1')
  1083.             ->leftJoin('t.pricings''pricing')
  1084.             ->leftJoin('t.subCategory''categ')
  1085.             ->leftJoin('categ.subCategory''categSub')
  1086.             ->andWhere('t.company = :companyId')
  1087.             ->setParameter('companyId'$companyId);
  1088.             if ($sorting != null) {
  1089.      
  1090.               if ($sorting == "popularity") {
  1091.                 if (strtolower($order) == "asc") {
  1092.                   $order "ASC";
  1093.                 } else {
  1094.                   $order "DESC";
  1095.                 }
  1096.           
  1097.                  $sorting 'size(t.inCarts)';
  1098.           
  1099.               }
  1100.               if ($sorting == "price") {
  1101.                 if (strtolower($order) == "asc") {
  1102.                   $order "ASC";
  1103.                 } else {
  1104.                   $order "DESC";
  1105.                 }
  1106.                
  1107.                 $sorting "pricing.price";
  1108.                 
  1109.               }
  1110.               if ($sorting == "date") {
  1111.                 if (strtolower($order) == "asc") {
  1112.                   $order "ASC";
  1113.                 } else {
  1114.                   $order "DESC";
  1115.                 }
  1116.           
  1117.                 $sorting ="t.creationDate";
  1118.               }
  1119.               $products->orderBy$sorting $order);
  1120.             }
  1121.           if ($category != false){
  1122.             $products->andWhere('t.categories = :category')
  1123.             ->setParameter('category'$category);
  1124.           }
  1125.         if(!$all){
  1126.             $products
  1127.                 ->andWhere('t.mainImage IS NOT NULL');
  1128.         if($available)
  1129.                 $products->andWhere('t.available = 1');
  1130.         }
  1131.         if(!$this->userServ->getAssociationUserIsBrowsing())
  1132.             $products->andWhere('t.isDisplayedInAssociationOnly != true');
  1133.         if(true// later check if user is browsing HRI
  1134.             $products->andWhere('t.isConsumer = true');
  1135.         if($results)
  1136.             return $products->getQuery()->getResult();
  1137.         else
  1138.             return $products;
  1139.     }
  1140.     /*
  1141.      * Get Products for Searching
  1142.      */
  1143.     public function getBaseProductsForSearching(){
  1144.         $products $this->createQueryBuilder('p')
  1145.             ->leftJoin('p.company''company')
  1146.             ->addSelect('company')
  1147.             ->leftJoin('p.storages''storages')
  1148.             ->addSelect('storages')
  1149.             ->leftJoin('p.pricings''pricings')
  1150.             ->addSelect('pricings')
  1151.             ->leftJoin('pricings.products''pproduct')
  1152.             ->addSelect('pproduct')
  1153.             ->where('p.draft != 1')
  1154.             ->andWhere('p.mainImage IS NOT NULL')
  1155.             ->andWhere('p.available = 1');
  1156.         $products $this->validateBrowsingAssociationInQuery($products);
  1157.         if(true// later check if user is browsing HRI
  1158.             $products->andWhere('p.isConsumer = true');
  1159.         $products->orderBy('p.qtyReadyToShip''DESC');
  1160.         return $products;
  1161.     }
  1162.     /*
  1163.      * Return products after filtering
  1164.      */
  1165.     public function findByFilters($filters$startAt=false$limit false$countOnly false){
  1166.         $pricing = [];
  1167.         $products $this->createQueryBuilder('p');
  1168.         if($countOnly)
  1169.             $products->select('count(p.id)');
  1170.         $products
  1171.             ->where('p.draft != 1')
  1172.             ->leftJoin('p.company''company')
  1173.             ->andWhere('company.showPublicly = true')
  1174.             ->andWhere('p.mainImage IS NOT NULL')
  1175.             ->andWhere('p.available = 1');
  1176.         foreach($filters as $filter){
  1177.             $id $filter->id;
  1178.             switch(strtolower($filter->type)){
  1179.                 case 'shipping':
  1180.                     if($id)
  1181.                         $products->andWhere('p.deliveryType = 1');
  1182.                     else
  1183.                         $products->andWhere('p.deliveryType = 0');
  1184.                     break;
  1185.                 case 'category':
  1186.                     $products
  1187.                         ->andWhere('p.categories= :categoryId OR p.subCategory= :categoryId')
  1188.                         ->setParameter('categoryId'$id);
  1189.                     break;
  1190.                 case 'price':
  1191.                     if(empty($pricing))
  1192.                         $products->leftJoin('p.pricings''pricing');
  1193.                     //Because of the multiple here, we need to do a query build
  1194.                     switch($id){
  1195.                         case '10';
  1196.                             $pricing[] = 'pricing.price < 10';
  1197.                             break;
  1198.                         case '10-25';
  1199.                             $pricing[] = '(pricing.price >= 10 and pricing.price < 25)';
  1200.                             break;
  1201.                         case '25-50';
  1202.                             $pricing[] = '(pricing.price >= 25 and pricing.price < 50)';
  1203.                             break;
  1204.                         case '50';
  1205.                             $pricing[] = 'pricing.price >= 50';
  1206.                             break;
  1207.                     }
  1208.                     break;
  1209.                 case 'conservation':
  1210.                     $products
  1211.                         ->andWhere('p.conservation = :conservationId')
  1212.                         ->setParameter('conservationId'$id);
  1213.                     break;
  1214.                 case 'region':
  1215.                     $products
  1216.                         ->andWhere('p.region = :regionId')
  1217.                         ->setParameter('regionId'$id);
  1218.                     break;
  1219.             }
  1220.         }
  1221.         if(!empty($pricing) && count($pricing) > 0){
  1222.             if(count($pricing) == 1){
  1223.                 $products->andWhere(current($pricing));
  1224.             }else{
  1225.                 $query implode(' OR '$pricing);
  1226.                 $products->andWhere($query);
  1227.             }
  1228.         }
  1229.         if(!$this->userServ->getAssociationUserIsBrowsing())
  1230.             $products->andWhere('p.isDisplayedInAssociationOnly != true');
  1231.         if(true// later check if user is browsing HRI
  1232.             $products->andWhere('p.isConsumer = true');
  1233.         if($countOnly)
  1234.             return $products->getQuery()->getSingleScalarResult();
  1235.         else
  1236.             return $products->getQuery()->getResult();
  1237.     }
  1238.     /*
  1239.      * Used to convert the old products bundle to the new Product Boxes
  1240.      */
  1241.     public function findProductsWithOldBundles()
  1242.     {
  1243.         $products $this->createQueryBuilder('p')
  1244.             ->addSelect('p')
  1245.             ->leftJoin('p.productsInBundle''products')
  1246.             ->addSelect('products')
  1247.             ->andWhere('products IS NOT NULL')
  1248.             ;
  1249.         return $products->getQuery()->getResult();
  1250.     }
  1251.     public function createDefaultQueryBuilder() : QueryBuilder
  1252.     {
  1253.         return $this->createQueryBuilder('product')
  1254.             // pricings
  1255.             ->leftJoin('product.pricings''pricings')
  1256.             ->addSelect('pricings')
  1257.             // storages
  1258.             ->leftJoin('product.storages''storages')
  1259.             ->addSelect('storages')
  1260.             // images
  1261.             ->leftJoin('product.mainImage''mainImage')
  1262.             ->addSelect('mainImage')
  1263.             // deals
  1264.             ->leftJoin('product.deal''deal')
  1265.             ->addSelect('deal')
  1266.             // company
  1267.             ->join('product.company''company')
  1268.             ->addSelect('company')
  1269.             // company deposit infos
  1270.             // TODO : find why it's reauired
  1271.             ->leftJoin('company.depositInfo''depositInfo')
  1272.             ->addSelect('depositInfo')
  1273.             // default shipping
  1274.             ->leftJoin('company.defaultShipping''defaultShipping')
  1275.             ->addSelect('defaultShipping')
  1276.             // association
  1277.             ->join('company.associations''associations')
  1278.             ->addSelect('associations');
  1279.     }
  1280.     public function findAllAssociationProducts(int $associationId){
  1281.         return $this->createDefaultQueryBuilder()
  1282.             ->where('product.draft = false')
  1283.             ->andWhere('associations = :association')
  1284.             ->setParameter('association'$associationId)
  1285.             ->orderBy('product.id''DESC')
  1286.             ->getQuery()
  1287.             ->getResult();
  1288.     }
  1289.     public function getOneProductFromCategory($category$maxResults 1){
  1290.       $products $this->createQueryBuilder('p')
  1291.           ->leftJoin('p.company''company')
  1292.           ->addSelect('company')
  1293.           ->leftJoin('p.storages''storages')
  1294.           ->addSelect('storages')
  1295.           ->leftJoin('p.pricings''pricing')
  1296.           ->andWhere('p.categories = :category')
  1297.           ->OrWhere('p.subCategory = :category')
  1298.           ->OrWhere('p.subCategory IN (:subCategory)')
  1299.           ->setParameter('category'$category)
  1300.           ->setParameter('subCategory'$category->getAllSubCategories())
  1301.           ->andWhere('p.draft != 1')
  1302.           ->andWhere('p.mainImage IS NOT NULL')
  1303.           ->andWhere('p.DeliveryType = 0')
  1304.           ->andWhere('p.available = 1')
  1305.           ->andWhere('company.showPublicly = true')
  1306.           ->andWhere('p.qtyReadyToShip > 0');
  1307.       $products $this->validateBrowsingAssociationInQuery($products);
  1308.       if(true// later check if user is browsing HRI
  1309.           $products->andWhere('p.isConsumer = true');
  1310.       // $products->orderBy('size(p.inCarts)', "desc");
  1311.       $products->groupBy('p.id');
  1312.       return $products->setMaxResults($maxResults)->orderBy('RAND()')->getQuery()->getResult();
  1313.   }
  1314.     public function getProductsWithMaturinUpc(){
  1315.       $products $this->createQueryBuilder('p')
  1316.           ->andWhere('p.maturinUpc IS NOT NULL');
  1317.       return $products->getQuery()->getResult();
  1318.   }
  1319.     public function exportProductsColabor(){
  1320.       $listProductsColabor $this->createQueryBuilder('p')
  1321.         ->leftJoin('p.company''company')
  1322.         ->leftJoin('p.pricings''pricing')
  1323.         ->leftJoin('p.categories''category')
  1324.         ->Where('category.id != 47')
  1325.         ->andWhere('category.id != 48')
  1326.         //->andWhere('company.id != 76')
  1327.         //->andWhere('company.id != 146')
  1328.         //->andWhere('company.id != 165')
  1329.         //->andWhere('company.id != 170')
  1330.         //->andWhere('company.id != 207')
  1331.         //->andWhere('company.id != 219')
  1332.         //->andWhere('company.id != 222')
  1333.         //->andWhere('company.id != 251')
  1334.         ->andWhere('company.exportColabor = true')
  1335.         ->andWhere('p.containsAlcohol = false')
  1336.         ->andWhere('company.showPublicly = true')
  1337.         ->andWhere('pricing.price !=0')
  1338.         ->orderBy('p.id''ASC');
  1339.       return $listProductsColabor->getQuery()->getResult();
  1340.     }
  1341.     public function productsCTAQ(){
  1342.       $productsCTAQ $this->createQueryBuilder('p')
  1343.         ->leftJoin('p.company''company')
  1344.         ->Where('p.categories != 47')
  1345.         ->OrWhere('p.categories != 351')
  1346.         ->andWhere('p.available = true')
  1347.         ->andWhere('p.draft = false')
  1348.         ->andWhere('p.isDisplayedInAssociationOnly = false')
  1349.         ->andWhere('company.showPublicly = true')
  1350.         ->orderBy('p.id''ASC');
  1351.       return $productsCTAQ->getQuery()->getResult();
  1352.     }
  1353.     public function LastInvoiceBeforeChange($idProduct$dateMax){
  1354.       /*$em = $this->getEntityManager();
  1355.       $conn = $em->getConnection();
  1356.       $sql = "SELECT cart_product.pricings_used "
  1357.       ."FROM product "
  1358.       ."RIGHT JOIN cart_product ON product.id=cart_product.product_id "
  1359.       ."WHERE product.id=".$idProduct." "
  1360.       ."AND cart_product.date_created < '2021-04-03 00:00:00' "
  1361.       ."AND cart_product.is_shipped = 1 "
  1362.       ."ORDER BY cart_product.date_created "
  1363.       ."DESC LIMIT 1";
  1364.       $stmt = $conn->prepare($sql);
  1365.       $stmt->execute();
  1366.       $results = $stmt->fetchAll();*/
  1367.       $lastInvoiceBeforeChange $this->createQueryBuilder('p')
  1368.       ->join('p.inCarts''c')
  1369.       ->addSelect('c.pricingsUsed')
  1370.       ->andWhere('c.dateCreated < :dateMax')
  1371.       ->andWhere('c.isShipped = true')
  1372.       ->andwhere('p.id = :idProduct')
  1373.       ->setParameter('idProduct'$idProduct)
  1374.       ->setParameter('dateMax'$dateMax)
  1375.       ->orderBy('c.dateCreated''DESC')
  1376.       ->setMaxResults(1);
  1377.       return $lastInvoiceBeforeChange->getQuery()->getResult();
  1378.   }
  1379.   public function getQuantityProductPreOrder($prod){
  1380.     $products $this->createQueryBuilder('p')
  1381.     ->Select('c.quantity as qty, cart.preOrderNow')
  1382.     ->innerJoin('p.inCarts''c')
  1383.     ->innerJoin('c.cart''cart')
  1384.     ->where('cart.preOrderNow = true')
  1385.     ->andWhere('cart.deliveryChoiceDate IS NOT NULL')
  1386.     ->andWhere('cart.isPaid = true')
  1387.     ->andwhere('c.product = :idProduct')
  1388.     ->setParameter('idProduct'$prod->getId());
  1389.     
  1390.     $results $products->getQuery()
  1391.                ->getResult();
  1392.     $totalSellPreOrder=0;
  1393.     foreach ($results as $sellbyQuantity) {
  1394.       $totalSellPreOrder += $sellbyQuantity['qty'];
  1395.     }
  1396. return $totalSellPreOrder;
  1397.   }
  1398.   public function exportProductsUPA(){
  1399.     $listProductsUPA $this->createQueryBuilder('p')
  1400.     ->leftJoin('p.company''company')
  1401.     ->leftJoin('p.pricings''pricing')
  1402.     ->leftJoin('company.associations''assoc')
  1403.    
  1404.     ->Where('assoc.id = 27')
  1405.     ->andWhere('company.showPublicly = true')
  1406.     ->andWhere('pricing.price > 0')
  1407.     ->orderBy('p.id''ASC');
  1408.   return $listProductsUPA->getQuery()->getResult();
  1409.   }
  1410.   // récuperer les produits qui coorespondent au filtre
  1411.   public function getcategoriesByPlacementWithFilter($category$sorting$order$sub1$region false){
  1412.     $products $this->createQueryBuilder('p')
  1413.     ->Select('p.id AS id','company.id AS selected_company','size(p.inCarts) AS panier')
  1414.     ->leftJoin('p.company''company')
  1415.     ->leftJoin('company.mainLocation''location')
  1416.     ->leftJoin('location.region''region')
  1417.     ->leftJoin('p.diets''diet')
  1418.     ->leftJoin('p.subCategory''categ')
  1419.     ->leftJoin('p.certifications''certif')
  1420.     ->leftJoin('p.conservation''conservat')
  1421.     ->leftJoin('categ.subCategory''categSub')
  1422.     ->addSelect('categ.id AS cateId,categ.placement AS firstnameplacement, categ.name As firstname ,categSub.placement AS secondplacement, categSub.name As secondCateg')
  1423.     ->leftJoin('p.pricings''pricing')
  1424.     ->andWhere('p.categories = :category')
  1425.     ->OrWhere('p.subCategory = :category')
  1426.     ->OrWhere('p.subCategory IN (:subCategory)')
  1427.     ->setParameter('category'$category)
  1428.     ->setParameter('subCategory'$category->getAllSubCategories())
  1429.     ->andWhere('p.draft != 1')
  1430.     ->andWhere('p.mainImage IS NOT NULL')
  1431.     ->andWhere('p.available = 1')
  1432.     ->andWhere('p.isDisplayedInAssociationOnly = 0')
  1433.     ->andWhere('categ.placement > 0')
  1434.     ->andWhere('categSub.placement  > 0')
  1435.     ->andWhere('p.isHri = 0'
  1436.     ->andWhere('p.isConsumer = true')
  1437.     ->andWhere('company.showPublicly = true')
  1438.     ->andWhere('p.qtyReadyToShip > 0');
  1439.     
  1440.     if($this->userServ->getAssociationUserIsBrowsing()){
  1441.       $urlnameAssociation $this->userServ->getAssociationUserIsBrowsing()->getUrlName();
  1442.       $products->leftJoin('company.associations''association')
  1443.               ->andWhere('association.urlName = :urlnameAssociation')
  1444.               ->setParameter('urlnameAssociation'$urlnameAssociation);
  1445.     }
  1446.     if ($sub1 != null) {
  1447.       switch ($sub1) {
  1448.         case 'LivraisonMaturin':
  1449.           $products->andWhere('p.DeliveryType = 0');
  1450.           break;
  1451.         case 'LivraisonIndependante':
  1452.           $products->andWhere('p.DeliveryType = 1');
  1453.           break;
  1454.         case 'EnSolde':
  1455.           $products->andWhere('pricing.isADiscount = true');
  1456.           break;
  1457.         case 'Moins10':
  1458.           $products->andWhere('pricing.price <= 9.99');
  1459.           break;
  1460.         case 'price10-25':
  1461.           $products->andWhere('pricing.price BETWEEN 10 AND 24.99');
  1462.           break;
  1463.         case 'price25-50':
  1464.           $products->andWhere('pricing.price BETWEEN 25 AND 49.99');
  1465.           break;
  1466.         case 'Plus50':
  1467.           $products->andWhere('pricing.price > 50');
  1468.           break;
  1469.         case 'Réfrigéré':
  1470.           $products->andWhere('conservat.id = 5');
  1471.           break;
  1472.         case 'Congelé':
  1473.           $products->andWhere('conservat.id = 6');
  1474.           break;
  1475.         case 'Frais (tablette)':
  1476.           $products->andWhere('conservat.id =  7');
  1477.           break;
  1478.         case 'AlimentsQuebec':
  1479.           $products->andWhere('p.alimentsDuQuebec = 1');;
  1480.           break;
  1481.         case 'PrixLaureat':
  1482.           $products->andWhere('p.isPrixLaureat = true');
  1483.           break;
  1484.         case 'Biologique':
  1485.           $products->andWhere('certif.category = :organic');
  1486.           $products->setParameter('organic''organic');
  1487.           break;
  1488.         case 'Sans_Gluten':
  1489.           $products->andWhere('diet.id = 3');
  1490.           break;
  1491.         case 'Vegane':
  1492.           $products->andWhere('diet.id = 2');
  1493.           break;
  1494.         case 'Sans_Lactose':
  1495.           $products->andWhere('diet.id = 5');
  1496.           break;
  1497.         case 'Sans_Noix':
  1498.            $products->andWhere('diet.id = 6');
  1499.           break;
  1500.         case 'Paleo':
  1501.           $products->andWhere('diet.id = 4');
  1502.           break;
  1503.       
  1504.       }  
  1505.     }
  1506.      if (is_int($region)) {
  1507.         $products->andWhere('region.id = :region');
  1508.         $products->setParameter('region'$region);
  1509.       }
  1510.    
  1511.       $products->orderBy('size(p.inCarts)','DESC');
  1512.     
  1513.     
  1514.     
  1515.     $results =  $products->getQuery()->getResult();
  1516.    
  1517.    $prod= [];
  1518.       foreach($results as $result) {
  1519.         $product $this->createQueryBuilder('p')
  1520.         ->leftJoin('p.company''company')
  1521.         ->andWhere('p.id = :selected_product')
  1522.         ->setParameter('selected_product'$result['id'])
  1523.         ->getQuery()->getResult();
  1524.         ;
  1525.         if ($product) {
  1526.           $prod[current($product)->getId()] = current($product);
  1527.         }
  1528.       }
  1529.     
  1530.   //  dd($categFinal);
  1531.     return $prod;
  1532. }
  1533. }