src/Repository/ProductRepository.php line 724

  1. <?php
  2. namespace App\Repository;
  3. use App\Classes\AppConfig;
  4. use App\Entity\Category;
  5. use App\Entity\Client;
  6. use App\Entity\Product;
  7. use App\Entity\Sale;
  8. use App\Entity\Supersale;
  9. use DateTimeImmutable;
  10. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  11. use Doctrine\DBAL\Result;
  12. use Doctrine\ORM\Query\ResultSetMapping;
  13. use Doctrine\ORM\QueryBuilder;
  14. use Doctrine\Persistence\ManagerRegistry;
  15. /**
  16.  * @extends ServiceEntityRepository<Product>
  17.  *
  18.  * @method Product|null find($id, $lockMode = null, $lockVersion = null)
  19.  * @method Product|null findOneBy(array $criteria, array $orderBy = null)
  20.  * @method Product[]    findAll()
  21.  * @method Product[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  22.  */
  23. class ProductRepository extends ServiceEntityRepository {
  24.   public function __construct(ManagerRegistry $registry) {
  25.     parent::__construct($registryProduct::class);
  26.   }
  27.   public function save(Product $product): Product {
  28.     if ($product->isSuperProduct()) {
  29.       $productSuper $this->findOneBy(['isSuperProduct' => true]);
  30.       if (!is_null($productSuper)) {
  31.         $productSuper->setIsSuperProduct(false);
  32.       }
  33.     }
  34.     if (is_null($product->getId())) {
  35.       $this->getEntityManager()->persist($product);
  36.     }
  37.     $this->getEntityManager()->flush();
  38.     return $product;
  39.   }
  40.   public function remove(Product $entitybool $flush false): void {
  41.     $this->getEntityManager()->remove($entity);
  42.     if ($flush) {
  43.       $this->getEntityManager()->flush();
  44.     }
  45.   }
  46.   public function delete(Product $product) {
  47.     $em $this->getEntityManager();
  48.     $db $this->getEntityManager()->getConnection();
  49.     $sql "DELETE FROM " $em->getClassMetadata(Product::class)->getTableName() . "
  50.                 WHERE id = :product_id ";
  51.     $result $db->prepare($sql);
  52.     $result->bindValue(':product_id'$product->getId());
  53.     $result->execute();
  54.   }
  55.   public function findForForm(int $id 0): Product {
  56.     if (empty($id)) {
  57.       return new Product();
  58.     }
  59.     return $this->getEntityManager()->getRepository(Product::class)->find($id);
  60.   }
  61.   public function getAllPaginator($filter$suspended): QueryBuilder {
  62.     $qb $this->createQueryBuilder('u')
  63.       ->select('u.id, u.title, u.sku, u.barcode, u.productKey, u.pricePDV, u.isOutOfStock, c.title as category, i.thumbnail100'// Include the root entity 'u'
  64.       ->leftJoin('u.category''c'// Left join with the 'category' entity
  65.       ->leftJoin('u.mainImage''i'// Left join with the 'category' entity
  66.       ->where('u.isSuspended = :suspenzija')
  67.       ->setParameter('suspenzija'$suspended);
  68.     // Optimizovan filter za 'naziv'
  69.     if (!empty($filter['naziv'])) {
  70.       $qb->andWhere('u.title LIKE :naziv')
  71.         ->setParameter('naziv''%' $filter['naziv'] . '%');
  72. //        ->setParameter('naziv', '%' . $filter['naziv']);
  73.     }
  74.     // Optimizovan filter za 'barcode'
  75.     if (!empty($filter['barcode'])) {
  76.       $qb->andWhere('u.barcode LIKE :barcode')
  77.         ->setParameter('barcode''%' $filter['barcode'] . '%');
  78.     }
  79.     // Optimizovan filter za 'sku'
  80.     if (!empty($filter['sku'])) {
  81.       $qb->andWhere('u.sku LIKE :sku')
  82.         ->setParameter('sku''%' $filter['sku'] . '%');
  83.     }
  84.     // Optimizovan filter za 'productKey'
  85.     if (!empty($filter['productKey'])) {
  86.       $qb->andWhere('u.productKey LIKE :productKey')
  87.         ->setParameter('productKey''%' $filter['productKey'] . '%');
  88.     }
  89.     // Optimizovan filter za 'stock'
  90.     if (!empty($filter['stock'])) {
  91.       $qb->andWhere('u.isOutOfStock = :stock')
  92.         ->setParameter('stock'$filter['stock']);
  93.     }
  94.     $qb->orderBy('u.id''ASC');
  95.     return $qb;
  96.   }
  97.     public function getAllBundlePaginator($filter): QueryBuilder {
  98.         $qb $this->createQueryBuilder('u')
  99.             ->select('u.id, u.title, u.sku, u.barcode, u.productKey, u.pricePDV, u.isOutOfStock, c.title as category, i.thumbnail100'// Include the root entity 'u'
  100.             ->leftJoin('u.category''c'// Left join with the 'category' entity
  101.             ->leftJoin('u.mainImage''i'// Left join with the 'category' entity
  102.             ->where('u.isBundle = 1');
  103.         // Optimizovan filter za 'naziv'
  104.         if (!empty($filter['naziv'])) {
  105.             $qb->andWhere('u.title LIKE :naziv')
  106.                 ->setParameter('naziv''%' $filter['naziv'] . '%');
  107. //        ->setParameter('naziv', '%' . $filter['naziv']);
  108.         }
  109.         // Optimizovan filter za 'barcode'
  110.         if (!empty($filter['barcode'])) {
  111.             $qb->andWhere('u.barcode LIKE :barcode')
  112.                 ->setParameter('barcode''%' $filter['barcode'] . '%');
  113.         }
  114.         // Optimizovan filter za 'sku'
  115.         if (!empty($filter['sku'])) {
  116.             $qb->andWhere('u.sku LIKE :sku')
  117.                 ->setParameter('sku''%' $filter['sku'] . '%');
  118.         }
  119.         // Optimizovan filter za 'productKey'
  120.         if (!empty($filter['productKey'])) {
  121.             $qb->andWhere('u.productKey LIKE :productKey')
  122.                 ->setParameter('productKey''%' $filter['productKey'] . '%');
  123.         }
  124.         // Optimizovan filter za 'stock'
  125.         if (!empty($filter['stock'])) {
  126.             $qb->andWhere('u.isOutOfStock = :stock')
  127.                 ->setParameter('stock'$filter['stock']);
  128.         }
  129.         $qb->orderBy('u.id''ASC');
  130.         return $qb;
  131.     }
  132. //  public function getAllPaginator($filter, $suspended): QueryBuilder {
  133. //
  134. //    $qb = $this->createQueryBuilder('u');
  135. //
  136. //    $qb->where('u.isSuspended = :suspenzija')
  137. //      ->setParameter('suspenzija', $suspended);
  138. //
  139. //    if (!empty($filter['naziv'])) {
  140. //      $qb->andWhere($qb->expr()->orX(
  141. //        $qb->expr()->like('u.title', ':naziv'),
  142. //      ))
  143. //        ->setParameter('naziv', '%' . $filter['naziv'] . '%');
  144. //    }
  145. //    if (!empty($filter['barcode'])) {
  146. //      $qb->andWhere($qb->expr()->orX(
  147. //        $qb->expr()->like('u.barcode', ':barcode'),
  148. //      ))
  149. //        ->setParameter('barcode', '%' . $filter['barcode'] . '%');
  150. //    }
  151. //    if (!empty($filter['sku'])) {
  152. //      $qb->andWhere($qb->expr()->orX(
  153. //        $qb->expr()->like('u.sku', ':sku'),
  154. //      ))
  155. //        ->setParameter('sku', '%' . $filter['sku'] . '%');
  156. //    }
  157. //    if (!empty($filter['productKey'])) {
  158. //      $qb->andWhere($qb->expr()->orX(
  159. //        $qb->expr()->like('u.productKey', ':productKey'),
  160. //      ))
  161. //        ->setParameter('productKey', '%' . $filter['productKey'] . '%');
  162. //    }
  163. //    if (!empty($filter['stock'])) {
  164. //      $qb->andWhere('u.isOutOfStock = :stock');
  165. //      $qb->setParameter('stock', $filter['stock']);
  166. //    }
  167. //
  168. //    $qb
  169. //      ->orderBy('u.id', 'ASC')
  170. //      ->getQuery();
  171. //
  172. //    return $qb;
  173. //  }
  174. //  public function getAllPaginatorCategory($filter, $suspended): QueryBuilder {
  175. //
  176. //    $category = $this->getEntityManager()->getRepository(Category::class)->find(AppConfig::KATEGORIJA);
  177. //
  178. //    $qb = $this->createQueryBuilder('u');
  179. //
  180. //    $qb->where('u.isSuspended = :suspenzija')
  181. //      ->setParameter('suspenzija', $suspended)
  182. //      ->andWhere('u.category = :category')
  183. //      ->setParameter('category', $category);
  184. //
  185. //    if (!empty($filter['naziv'])) {
  186. //      $qb->andWhere($qb->expr()->orX(
  187. //        $qb->expr()->like('u.title', ':naziv'),
  188. //      ))
  189. //        ->setParameter('naziv', '%' . $filter['naziv'] . '%');
  190. //    }
  191. //    if (!empty($filter['barcode'])) {
  192. //      $qb->andWhere($qb->expr()->orX(
  193. //        $qb->expr()->like('u.barcode', ':barcode'),
  194. //      ))
  195. //        ->setParameter('barcode', '%' . $filter['barcode'] . '%');
  196. //    }
  197. //    if (!empty($filter['sku'])) {
  198. //      $qb->andWhere($qb->expr()->orX(
  199. //        $qb->expr()->like('u.sku', ':sku'),
  200. //      ))
  201. //        ->setParameter('sku', '%' . $filter['sku'] . '%');
  202. //    }
  203. //    if (!empty($filter['productKey'])) {
  204. //      $qb->andWhere($qb->expr()->orX(
  205. //        $qb->expr()->like('u.productKey', ':productKey'),
  206. //      ))
  207. //        ->setParameter('productKey', '%' . $filter['productKey'] . '%');
  208. //    }
  209. //    if (!empty($filter['stock'])) {
  210. //      $qb->andWhere('u.isOutOfStock = :stock');
  211. //      $qb->setParameter('stock', $filter['stock']);
  212. //    }
  213. //
  214. //    $qb
  215. //      ->orderBy('u.id', 'ASC')
  216. //      ->getQuery();
  217. //
  218. //    return $qb;
  219. //  }
  220.   public function getAllPaginatorCategory($filter$suspended): QueryBuilder {
  221.     $qb $this->createQueryBuilder('u')
  222.       ->select('u.id, u.title, u.sku, u.barcode, u.importCat, u.productKey, u.pricePDV, u.isOutOfStock, c.title as category, i.thumbnail100'// Include the root entity 'u'
  223.       ->leftJoin('u.category''c'// Left join with the 'category' entity
  224.       ->leftJoin('u.mainImage''i'// Left join with the 'category' entity
  225.       ->where('u.isSuspended = :suspenzija')
  226.       ->setParameter('suspenzija'$suspended)
  227.       ->andWhere('u.category = :kategorija')
  228.       ->setParameter('kategorija'AppConfig::KATEGORIJA);
  229.     // Optimizovan filter za 'naziv'
  230.     if (!empty($filter['naziv'])) {
  231.       $qb->andWhere('u.title LIKE :naziv')
  232.                 ->setParameter('naziv''%' $filter['naziv'] . '%');
  233. //        ->setParameter('naziv', '%' . $filter['naziv']);
  234.     }
  235.     // Optimizovan filter za 'barcode'
  236.     if (!empty($filter['barcode'])) {
  237.       $qb->andWhere('u.barcode LIKE :barcode')
  238.         ->setParameter('barcode''%' $filter['barcode'] . '%');
  239.     }
  240.     // Optimizovan filter za 'sku'
  241.     if (!empty($filter['sku'])) {
  242.       $qb->andWhere('u.sku LIKE :sku')
  243.         ->setParameter('sku''%' $filter['sku'] . '%');
  244.     }
  245.     // Optimizovan filter za 'productKey'
  246.     if (!empty($filter['productKey'])) {
  247.       $qb->andWhere('u.productKey LIKE :productKey')
  248.         ->setParameter('productKey''%' $filter['productKey'] . '%');
  249.     }
  250.     // Optimizovan filter za 'stock'
  251.     if (!empty($filter['stock'])) {
  252.       $qb->andWhere('u.isOutOfStock = :stock')
  253.         ->setParameter('stock'$filter['stock']);
  254.     }
  255.     $qb->orderBy('u.id''ASC');
  256.     return $qb;
  257.   }
  258.   //funkcija za vracenje broja artikala koji nisu u tacnim kategorijama
  259.   public function artikliPoKategorijama($kategorije) {
  260.     $qb $this->createQueryBuilder('p')
  261.       ->select('IDENTITY(p.category) as category_id, COUNT(p.id) as product_count')
  262.       ->where('p.category IN (:categoryIds)')
  263.       ->setParameter('categoryIds'$kategorije)
  264.       ->groupBy('p.category');
  265.     $rez $qb->getQuery()->getResult();
  266.     $finish = [];
  267.     foreach ($rez as $item) {
  268.       $categ $this->getEntityManager()->getRepository(Category::class)->find($item['category_id']);
  269.       $finish[] =
  270.         [
  271.           $categ->getTitle(),
  272.           $item['product_count']
  273.         ];
  274.     }
  275.     return $finish;
  276.   }
  277.   public function getAllByPaginatorCategory($filter$suspended$category): QueryBuilder {
  278.     $qb $this->createQueryBuilder('u')
  279.       ->select('u.id, u.title, u.sku, u.barcode, u.productKey, u.pricePDV, u.isOutOfStock, c.title as category, i.thumbnail100'// Include the root entity 'u'
  280.       ->leftJoin('u.category''c'// Left join with the 'category' entity
  281.       ->leftJoin('u.mainImage''i'// Left join with the 'category' entity
  282.       ->where('u.isSuspended = :suspenzija')
  283.       ->setParameter('suspenzija'$suspended)
  284.       ->andWhere('u.category = :kategorija')
  285.       ->setParameter('kategorija'$category);
  286.     // Optimizovan filter za 'naziv'
  287.     if (!empty($filter['naziv'])) {
  288.       $qb->andWhere('u.title LIKE :naziv')
  289.                 ->setParameter('naziv''%' $filter['naziv'] . '%');
  290. //        ->setParameter('naziv', '%' . $filter['naziv']);
  291.     }
  292.     // Optimizovan filter za 'barcode'
  293.     if (!empty($filter['barcode'])) {
  294.       $qb->andWhere('u.barcode LIKE :barcode')
  295.         ->setParameter('barcode''%' $filter['barcode'] . '%');
  296.     }
  297.     // Optimizovan filter za 'sku'
  298.     if (!empty($filter['sku'])) {
  299.       $qb->andWhere('u.sku LIKE :sku')
  300.         ->setParameter('sku''%' $filter['sku'] . '%');
  301.     }
  302.     // Optimizovan filter za 'productKey'
  303.     if (!empty($filter['productKey'])) {
  304.       $qb->andWhere('u.productKey LIKE :productKey')
  305.         ->setParameter('productKey''%' $filter['productKey'] . '%');
  306.     }
  307.     // Optimizovan filter za 'stock'
  308.     if (!empty($filter['stock'])) {
  309.       $qb->andWhere('u.isOutOfStock = :stock')
  310.         ->setParameter('stock'$filter['stock']);
  311.     }
  312.     $qb->orderBy('u.id''ASC');
  313.     return $qb;
  314.   }
  315. //  public function getAllByCategoryPaginator($filter): QueryBuilder {
  316. //
  317. //    $qb = $this->createQueryBuilder('p')
  318. //      ->select('p.id, p.title, v.id as vendor, p.sku, p.barcode, p.productKey, p.pricePDV, p.price, p.vat, p.isOutOfStock as outOfStock, p.isNew as new, p.isDiscount as discount, p.isSuperProduct as superProduct, p.isOutlet as isOutlet, p.isSuspended as suspended, c.title as category, i.thumbnail100, i.thumbnail500') // Include the root entity 'u'
  319. //      ->leftJoin('p.category', 'c') // Left join with the 'category' entity
  320. //      ->leftJoin('p.vendor', 'v') // Left join with the 'category' entity
  321. //      ->leftJoin('p.mainImage', 'i');
  322. //
  323. //
  324. //    $category = ['id' => $filter['catId']];
  325. //    $allCategories = array_merge([$category], $this->getEntityManager()->getRepository(Category::class)->findAllSubcategories($filter['catId']));
  326. ////    $allCategories = array_merge([$category], $this->getEntityManager()->getRepository(Category::class)->findAllSubcategories($filter['catId']));
  327. //
  328. //    $categoryIds = array_map(fn($cat) => $cat['id'], $allCategories);
  329. //
  330. //    if (isset($filter['title'])) {
  331. //      $qb->andWhere($qb->expr()->like('p.title', ':title'))
  332. //        ->setParameter('title', '%' . $filter['title'] . '%');
  333. //    }
  334. //
  335. //    $qb
  336. //      ->andWhere('p.category IN (:categoryIds)')
  337. //      ->andWhere('p.pricePDV > 0')
  338. //      ->setParameter('categoryIds', $categoryIds);
  339. //
  340. //    $qb
  341. //      ->addOrderBy('p.title', 'ASC')
  342. //      ->getQuery();
  343. //
  344. //    return $qb;
  345. //  }
  346.   public function getAllByCategoryPaginator($filter) {
  347. //dd($filter);
  348. //     Prvo učitajte sve podkategorije jednim upitom
  349.     $allCategories $this->getEntityManager()
  350.       ->getRepository(Category::class)
  351.       ->findAllSubcategoryIds($filter['catId']);
  352. //    dd($allCategories);
  353. //
  354. //    // Dodajte osnovnu kategoriju
  355. //    $allCategories[] = ['id' => $filter['catId']];
  356. //
  357. //    // Ekstrahujte ID-eve kategorija
  358. //    $categoryIds = array_map(fn($cat) => $cat['id'], $allCategories);
  359.     // Kreirajte QueryBuilder
  360.     $qb $this->createQueryBuilder('p')
  361.       ->select('p.id, p.title, v.id as vendor, p.productKey, p.pricePDV, p.pricePrikaz, p.price, p.vat, p.isOutOfStock as outOfStock, p.isNew as new, p.isDiscount as discount, p.isSuperProduct as superProduct, p.isOutlet as isOutlet, p.isSuspended as suspended, c.title as category, i.thumbnail500')
  362. //      ->select('p.id, p.title, v.id as vendor, p.sku, p.barcode, p.productKey, p.pricePDV, p.price, p.vat, p.isOutOfStock as outOfStock, p.isNew as new, p.isDiscount as discount, p.isSuperProduct as superProduct, p.isOutlet as isOutlet, p.isSuspended as suspended, c.title as category, i.thumbnail100, i.thumbnail500')
  363.       ->leftJoin('p.category''c')
  364.       ->leftJoin('p.vendor''v')
  365.       ->leftJoin('p.mainImage''i')
  366.       ->where('p.category IN (:categoryIds)')
  367.       ->andWhere('p.pricePDV > 0')
  368.       ->setParameter('categoryIds'$allCategories);
  369.     // Filtrirajte po naslovu ako je postavljen
  370.     if (isset($filter['title'])) {
  371.       $qb->andWhere($qb->expr()->like('p.title'':title'))
  372.                 ->setParameter('title''%' $filter['title'] . '%');
  373. //        ->setParameter('title', '%' . $filter['title']);
  374.     }
  375.     if (isset($filter['pro'])) {
  376.       $qb->andWhere('p.factory IN (:factory)')
  377.         ->setParameter('factory'$filter['pro']);
  378.     }
  379.     if (isset($filter['sale'])) {
  380.       $qb->andWhere('p.isDiscount = 1');
  381.     }
  382.     $qb->addOrderBy('p.isOutOfStock''ASC');
  383. //    dd($qb->addOrderBy('p.title', 'ASC')->getQuery());
  384.     return $qb->addOrderBy('p.title''ASC')->getQuery();
  385.   }
  386. //  public function getAllByCategoryFilter($filter): QueryBuilder {
  387. //    $category = $this->getEntityManager()->getRepository(Category::class)->find($filter['catId']);
  388. //
  389. //    $qb = $this->createQueryBuilder('p');
  390. //
  391. //    $qb
  392. //      ->andWhere('p.category = :category')
  393. //      ->setParameter('category', $category);
  394. //
  395. //    $qb
  396. //      ->addOrderBy('p.title', 'ASC')
  397. //      ->getQuery();
  398. //
  399. //    return $qb;
  400. //  }
  401.   public function getMaxMinPrice($category) {
  402.     $qb $this->createQueryBuilder('p');
  403.     $minPriceSubquery $this->createQueryBuilder('p2')
  404.       ->select('MIN(p2.pricePDV)')
  405.       ->where('p2.category = :category')
  406.       ->getDQL();
  407.     // Podupit za najvišu cenu
  408.     $maxPriceSubquery $this->createQueryBuilder('p3')
  409.       ->select('MAX(p3.pricePDV)')
  410.       ->where('p3.category = :category')
  411.       ->getDQL();
  412.     $qb
  413.       ->select('MIN(p.pricePDV) AS minPrice, MAX(p.pricePDV) AS maxPrice')
  414.       ->where('p.category = :category')
  415.       ->setParameter('category'$category)
  416.       ->addOrderBy('p.title''ASC')
  417.       ->setMaxResults(1);
  418.     $query $qb->getQuery();
  419.     $result $query->getSingleResult();
  420.     return [
  421.       'minPrice' => (float)$result['minPrice'],
  422.       'maxPrice' => (float)$result['maxPrice'],
  423.     ];
  424.   }
  425.   public function getAllByCategoryFilter($filter): QueryBuilder {
  426. //    $category = $this->getEntityManager()->getRepository(Category::class)->find($filter['catId']);
  427.     $qb $this->createQueryBuilder('p')
  428. //      ->select('p.id, p.title, v.id as vendor, p.sku, p.barcode, p.productKey, p.pricePDV, p.price, p.vat, p.isOutOfStock as outOfStock, p.isNew as new, p.isDiscount as discount, p.isSuperProduct as superProduct, p.isOutlet as isOutlet, p.isSuspended as suspended, c.title as category, i.thumbnail100, i.thumbnail500') // Include the root entity 'u'
  429.       ->select('p.id, p.title, v.id as vendor, p.productKey, p.pricePDV, p.pricePrikaz, p.price, p.vat, p.isOutOfStock as outOfStock, p.isNew as new, p.isDiscount as discount, p.isSuperProduct as superProduct, p.isOutlet as isOutlet, p.isSuspended as suspended, c.title as category, i.thumbnail500'// Include the root entity 'u'
  430.       ->leftJoin('p.category''c'// Left join with the 'category' entity
  431.       ->leftJoin('p.vendor''v'// Left join with the 'category' entity
  432.       ->leftJoin('p.mainImage''i');
  433.     $qb
  434.       ->andWhere('p.category = :category')
  435.       ->andWhere('p.pricePDV > 0')
  436.       ->setParameter('category'$filter['catId']);
  437.     if (isset($filter['vendor'])) {
  438.       $qb->andWhere('p.factory IN (:factory)')
  439.         ->setParameter('factory'$filter['vendor']);
  440.     }
  441.     if (isset($filter['title'])) {
  442.       $qb->andWhere($qb->expr()->like('p.title'':title'))
  443.         ->setParameter('title''%' $filter['title'] . '%');
  444.     }
  445.     if (isset($filter['sale'])) {
  446.       $qb->andWhere('p.isDiscount = 1');
  447.     }
  448.     // Dodavanje uslova za priceRange
  449.     if (isset($filter['priceRange'])) {
  450.       [$minPrice$maxPrice] = explode(';'$filter['priceRange']);
  451.       $qb->andWhere('p.pricePDV BETWEEN :minPrice AND :maxPrice')
  452.         ->setParameter('minPrice'$minPrice)
  453.         ->setParameter('maxPrice'$maxPrice);
  454.     }
  455.     function slugify($text) {
  456.       // Zamena specijalnih karaktera
  457.       $text preg_replace('~[^\pL\d]+~u''_'$text);
  458.       // Transliteration
  459.       $text iconv('utf-8''us-ascii//TRANSLIT'$text);
  460.       // Lowercase
  461.       $text strtolower($text);
  462.       // Uklanjanje preostalih neželjenih karaktera
  463.       $text preg_replace('~[^_\w]+~'''$text);
  464.       // Uklanjanje viška _
  465.       $text trim($text'_');
  466.       // Return final alias
  467.       return $text;
  468.     }
  469.     foreach ($filter as $key => $values) {
  470.       if ($key !== 'catId' && $key !== 'vendor' && $key !== 'priceRange' && $key !== 'title' && $key !== 'sale') {
  471. //        $alias = 'a_' . $key;
  472.         $alias 'a_' slugify($key);
  473.         $qb->leftJoin('p.attribute'$alias'WITH'$alias '.title = :title_' $alias)
  474.           ->andWhere($alias '.value IN (:values_' $alias ')')
  475.                ->setParameter('title_' $alias$key)
  476.                ->setParameter('values_' $alias$values);
  477. //        $qb->leftJoin('p.attribute', $alias, 'WITH', $alias . '.title = :title_' . $key)
  478. //          ->andWhere($alias . '.value IN (:values_' . $key . ')')
  479. //          ->setParameter('title_' . $key, $key)
  480. //          ->setParameter('values_' . $key, $values);
  481.       }
  482.     }
  483.     $qb->addOrderBy('p.isOutOfStock''ASC')
  484.       ->addOrderBy('p.title''ASC')
  485.       ->getQuery();
  486.     return $qb;
  487.   }
  488.   public function findUniqueAttributesByCategory($categoryId) {
  489.     $query $this->createQueryBuilder('p')
  490.       ->innerJoin('p.category''c')
  491.       ->innerJoin('p.attribute''a')
  492.       ->where('c.id = :categoryId')
  493.       ->setParameter('categoryId'$categoryId)
  494.       ->select('a.title, a.value')
  495.       ->orderBy('a.title''ASC')
  496.       ->addOrderBy('a.value''ASC')
  497.       ->getQuery();
  498.     $result $query->getResult();
  499.     // Organizovanje rezultata u jedinstvene atribute sa vrednostima
  500.     $attributes = [];
  501.     foreach ($result as $row) {
  502.       $title $row['title'];
  503.       $value $row['value'];
  504.       // preskoči samo ako je null ili prazan string
  505.       if ($title === null || $title === '') {
  506.         continue;
  507.       }
  508.       if ($value === null || $value === '') {
  509.         continue;
  510.       }
  511.       if (!isset($attributes[$title])) {
  512.         $attributes[$title] = [];
  513.       }
  514.       if (!in_array($value$attributes[$title])) {
  515.         $attributes[$title][] = $value;
  516.       }
  517.     }
  518.     // Sortiranje atributa po nazivu
  519.     ksort($attributes);
  520.       return $attributes;
  521. //    return $this->createQueryBuilder('p')
  522. //      ->innerJoin('p.category', 'c')
  523. //      ->innerJoin('p.attribute', 'a')
  524. //      ->where('c.id = :categoryId')
  525. //      ->setParameter('categoryId', $categoryId)
  526. //      ->select('DISTINCT a.title')
  527. //      ->getQuery()
  528. //      ->getResult();
  529.   }
  530.   public function findUniqueAttributesByCategoryVendor($categoryId$vendorId) {
  531.     $query $this->createQueryBuilder('p')
  532.       ->innerJoin('p.category''c')
  533.       ->innerJoin('p.attribute''a')
  534.       ->where('c.id = :categoryId')
  535.       ->setParameter('categoryId'$categoryId)
  536.       ->andWhere('p.factory = :vendorId')
  537.       ->setParameter('vendorId'$vendorId)
  538.       ->select('a.title, a.value')
  539.       ->orderBy('a.title''ASC')
  540.       ->addOrderBy('a.value''ASC')
  541.       ->getQuery();
  542.     $result $query->getResult();
  543.     // Organizovanje rezultata u jedinstvene atribute sa vrednostima
  544.     $attributes = [];
  545.     foreach ($result as $row) {
  546.       $title $row['title'];
  547.       $value $row['value'];
  548.       // preskoči samo ako je null ili prazan string
  549.       if ($title === null || $title === '') {
  550.         continue;
  551.       }
  552.       if ($value === null || $value === '') {
  553.         continue;
  554.       }
  555.       if (!isset($attributes[$title])) {
  556.         $attributes[$title] = [];
  557.       }
  558.       if (!in_array($value$attributes[$title])) {
  559.         $attributes[$title][] = $value;
  560.       }
  561.     }
  562.     // Sortiranje atributa po nazivu
  563.     ksort($attributes);
  564.       return $attributes;
  565. //    return $this->createQueryBuilder('p')
  566. //      ->innerJoin('p.category', 'c')
  567. //      ->innerJoin('p.attribute', 'a')
  568. //      ->where('c.id = :categoryId')
  569. //      ->setParameter('categoryId', $categoryId)
  570. //      ->select('DISTINCT a.title')
  571. //      ->getQuery()
  572. //      ->getResult();
  573.   }
  574.   public function findUniqueFactoriesByCategory($categoryId) {
  575.     return $this->createQueryBuilder('p')
  576.       ->innerJoin('p.category''c')
  577.       ->innerJoin('p.factory''f')
  578.       ->where('c.id = :categoryId')
  579.       ->setParameter('categoryId'$categoryId)
  580.       ->select('DISTINCT f.id, f.title')
  581.       ->orderBy('f.title''ASC')
  582.       ->getQuery()
  583.       ->getResult();
  584.   }
  585.   public function getProductsSearchPaginator($filter): QueryBuilder {
  586.     $qb $this->createQueryBuilder('t')
  587.       ->select('t.id, t.title, t.sku, t.barcode, t.productKey, t.pricePDV, t.isOutOfStock, t.isSuspended, c.title as category, i.thumbnail100'// Include the root entity 'u'
  588.       ->leftJoin('t.category''c'// Left join with the 'category' entity
  589.       ->leftJoin('t.mainImage''i')
  590.       ->where('t.pricePDV > 0')
  591.       ->andWhere('t.isSuspended = 0');
  592.     $keywords explode(" "$filter['tekst']);
  593.     foreach ($keywords as $key => $keyword) {
  594.       $qb
  595.         ->andWhere($qb->expr()->orX(
  596.           $qb->expr()->like('t.title'':keyword'.$key),
  597.           $qb->expr()->like('t.barcode'':keyword'.$key),
  598.           $qb->expr()->like('t.productKey'':keyword'.$key),
  599.           $qb->expr()->like('t.sku'':keyword'.$key)
  600.         ))
  601.         ->setParameter('keyword'.$key'%' $keyword '%');
  602.     }
  603.     $qb
  604.       ->addOrderBy('t.title''ASC')
  605.       ->getQuery();
  606.     return $qb;
  607.   }
  608.   public function getProductsSearch($keywords): QueryBuilder {
  609.     $qb $this->createQueryBuilder('t')
  610.       ->select('t.id, t.title, t.sku, t.barcode, t.productKey, t.pricePDV, t.price, t.vat, t.pricePrikaz, t.isOutOfStock as outOfStock, t.isNew as new, t.isDiscount as discount, t.isSuperProduct as superProduct, t.isOutlet as isOutlet, t.isSuspended as suspended, c.title as category, i.thumbnail100, i.thumbnail500'// Include the root entity 'u'
  611.       ->leftJoin('t.category''c'// Left join with the 'category' entity
  612.       ->leftJoin('t.mainImage''i')
  613.       ->where('t.pricePDV > 0')
  614.       ->andWhere('t.isSuspended = 0');
  615.     $keywords explode(" "$keywords);
  616.     foreach ($keywords as $key => $keyword) {
  617.       $qb
  618.         ->andWhere($qb->expr()->orX(
  619.           $qb->expr()->like('t.title'':keyword'.$key),
  620.           $qb->expr()->like('t.barcode'':keyword'.$key),
  621.           $qb->expr()->like('t.productKey'':keyword'.$key),
  622.           $qb->expr()->like('t.sku'':keyword'.$key)
  623.         ))
  624.         ->setParameter('keyword'.$key'%' $keyword '%');
  625.     }
  626.     $qb
  627.       ->addOrderBy('t.isOutOfStock''ASC')
  628.       ->addOrderBy('t.title''ASC')
  629.       ->getQuery();
  630.     return $qb;
  631.   }
  632.   public function getProductsSale($category$sale): QueryBuilder {
  633.     $qb $this->createQueryBuilder('u')
  634.       ->select('u.id, u.title, u.sku, u.barcode, u.productKey, u.pricePDV, u.price, u.pricePrikaz, u.vat, u.isOutOfStock as outOfStock, u.isNew as new, u.isDiscount as discount, u.isSuperProduct as superProduct, u.isOutlet as isOutlet, u.isSuspended as suspended, c.title as category, c.id as cid, i.thumbnail100, i.thumbnail500'// Include the root entity 'u'
  635.       ->leftJoin('u.category''c'// Left join with the 'category' entity
  636.       ->leftJoin('u.mainImage''i');
  637.     $qb->where('u.isSuspended = :suspenzija')
  638.       ->setParameter('suspenzija'0)
  639.       ->andWhere('u.isDiscount = :discount')
  640.       ->setParameter('discount'1)
  641.       ->andWhere('u.pricePDV > 0');
  642.     if ($category 0) {
  643.       $qb->andWhere('u.category = :category')
  644.         ->setParameter('category'$category);
  645.     }
  646.     if ($sale 0) {
  647.       $qb->andWhere('u.sale = :sale')
  648.         ->setParameter('sale'$sale);
  649.     }
  650.     $qb->orderBy('u.isOutOfStock''ASC')
  651.       ->addOrderBy('u.id''ASC')
  652.       ->getQuery();
  653.     return $qb;
  654.   }
  655. //  public function getProductsSaleHome(): array {
  656. //    $qb = $this->createQueryBuilder('u')
  657. //      ->select('u.id, u.title, u.sku, u.barcode, u.productKey, u.pricePDV, u.pricePrikaz, u.price, u.vat, u.isOutOfStock as outOfStock, u.isNew as new, u.isDiscount as discount, u.isSuperProduct as superProduct, u.isOutlet as isOutlet, u.isSuspended as suspended, c.title as category, i.thumbnail100, i.thumbnail500') // Include the root entity 'u'
  658. //      ->leftJoin('u.category', 'c') // Left join with the 'category' entity
  659. //      ->leftJoin('u.mainImage', 'i');
  660. //
  661. //    $qb->where('u.isSuspended = :suspenzija')
  662. //      ->setParameter('suspenzija', 0)
  663. //      ->andWhere('u.isDiscount = :discount')
  664. //      ->setParameter('discount', 1)
  665. //      ->andWhere('u.pricePDV > 0');
  666. //
  667. //    // Izvršava upit i vraća rezultate kao niz
  668. //    $products = $qb->getQuery()->getResult();
  669. //
  670. //    // Shuffle the products array to get random products
  671. //    shuffle($products);
  672. //
  673. //    // Return only 10 random products
  674. //    return array_slice($products, 0, 10);
  675. //  }
  676.   public function getProductsEditorHome(int $editorId): array {
  677.     $qb $this->createQueryBuilder('u')
  678.       ->select('u.id, u.title, u.sku, u.barcode, u.productKey, u.pricePDV, u.price, u.pricePrikaz, u.vat, u.isOutOfStock as outOfStock, u.isNew as new, u.isDiscount as discount, u.isSuperProduct as superProduct, u.isOutlet as isOutlet, u.isSuspended as suspended, c.title as category, i.thumbnail100, i.thumbnail500'// Include the root entity 'u'
  679.       ->leftJoin('u.category''c'// Left join with the 'category' entity
  680.       ->leftJoin('u.mainImage''i');
  681.     $qb->where('u.isSuspended = :suspenzija')
  682.       ->setParameter('suspenzija'0)
  683.       ->andWhere('u.choice = :choice')
  684.       ->setParameter('choice'$editorId)
  685.       ->andWhere('u.pricePDV > 0');
  686.     // Izvršava upit i vraća rezultate kao niz
  687.     $products $qb->getQuery()->getResult();
  688.     // Shuffle the products array to get random products
  689.     shuffle($products);
  690.     // Return only 10 random products
  691.     return array_slice($products010);
  692.   }
  693.   public function getProductsSaleHome(int $saleId): array {
  694.     $qb $this->createQueryBuilder('u')
  695.       ->select('u.id, u.title, u.sku, u.barcode, u.productKey, u.pricePDV, u.price, u.pricePrikaz, u.vat, u.isOutOfStock as outOfStock, u.isNew as new, u.isDiscount as discount, u.isSuperProduct as superProduct, u.isOutlet as isOutlet, u.isSuspended as suspended, c.title as category, i.thumbnail100, i.thumbnail500'// Include the root entity 'u'
  696.       ->leftJoin('u.category''c'// Left join with the 'category' entity
  697.       ->leftJoin('u.mainImage''i');
  698.     $qb->where('u.isSuspended = :suspenzija')
  699.       ->setParameter('suspenzija'0)
  700.       ->andWhere('u.sale = :sale')
  701.       ->setParameter('sale'$saleId)
  702.       ->andWhere('u.pricePDV > 0');
  703.     // Izvršava upit i vraća rezultate kao niz
  704.     $products $qb->getQuery()->getResult();
  705.     // Shuffle the products array to get random products
  706.     shuffle($products);
  707.     // Return only 10 random products
  708.     return array_slice($products010);
  709.   }
  710. //  public function getProductsPaginatorByClient(Client $client): QueryBuilder {
  711. //
  712. //    $qb = $this->createQueryBuilder('u');
  713. //
  714. //    $qb->where('u.vendor = :client')
  715. //      ->setParameter('client', $client);
  716. //
  717. //    $qb
  718. //      ->orderBy('u.isSuspended', 'ASC')
  719. //      ->getQuery();
  720. //
  721. //    return $qb;
  722. //  }
  723.   public function getProductsPaginatorByClient(Client $client): QueryBuilder {
  724.     $qb $this->createQueryBuilder('u')
  725.       ->select('u.id, u.title, u.sku, u.barcode, u.productKey, u.pricePDV, u.isOutOfStock, u.isSuspended, c.title as category, i.thumbnail100'// Include the root entity 'u'
  726.       ->leftJoin('u.category''c'// Left join with the 'category' entity
  727.       ->leftJoin('u.mainImage''i'// Left join with the 'category' entity
  728.       ->where('u.vendor = :client')
  729.       ->setParameter('client'$client);
  730.     // Optimizovan filter za 'naziv'
  731.     if (!empty($filter['naziv'])) {
  732.       $qb->andWhere('u.title LIKE :naziv')
  733.                 ->setParameter('naziv''%' $filter['naziv'] . '%');
  734. //        ->setParameter('naziv', '%' . $filter['naziv']);
  735.     }
  736.     // Optimizovan filter za 'barcode'
  737.     if (!empty($filter['barcode'])) {
  738.       $qb->andWhere('u.barcode LIKE :barcode')
  739.         ->setParameter('barcode''%' $filter['barcode'] . '%');
  740.     }
  741.     // Optimizovan filter za 'sku'
  742.     if (!empty($filter['sku'])) {
  743.       $qb->andWhere('u.sku LIKE :sku')
  744.         ->setParameter('sku''%' $filter['sku'] . '%');
  745.     }
  746.     // Optimizovan filter za 'productKey'
  747.     if (!empty($filter['productKey'])) {
  748.       $qb->andWhere('u.productKey LIKE :productKey')
  749.         ->setParameter('productKey''%' $filter['productKey'] . '%');
  750.     }
  751.     // Optimizovan filter za 'stock'
  752.     if (!empty($filter['stock'])) {
  753.       $qb->andWhere('u.isOutOfStock = :stock')
  754.         ->setParameter('stock'$filter['stock']);
  755.     }
  756.     $qb->orderBy('u.isSuspended''ASC');
  757.     return $qb;
  758.   }
  759.   public function getProductsNewCount(): int {
  760.     $today = new DateTimeImmutable();
  761.     $danas $today->setTime(0);
  762.     $queryBuilder $this->createQueryBuilder('u');
  763.     $queryBuilder
  764.       ->select('COUNT(u.id)')
  765.       ->where('u.created >= :today')
  766.       ->setParameter('today'$danas);
  767.     return (int) $queryBuilder->getQuery()->getSingleScalarResult();
  768.   }
  769.   public function getProductsNew(): QueryBuilder {
  770.     $qb $this->createQueryBuilder('u')
  771.       ->select('u.id, u.title, u.sku, u.barcode, u.productKey, u.pricePDV, u.price, u.pricePrikaz, u.vat, u.isOutOfStock as outOfStock, u.isNew as new, u.isDiscount as discount, u.isSuperProduct as superProduct, u.isOutlet as isOutlet, u.isSuspended as suspended, c.title as category, i.thumbnail100, i.thumbnail500'// Include the root entity 'u'
  772.       ->leftJoin('u.category''c'// Left join with the 'category' entity
  773.       ->leftJoin('u.mainImage''i');
  774.     $qb->where('u.isSuspended = :suspenzija')
  775.       ->setParameter('suspenzija'0)
  776.       ->andWhere('u.isOutOfStock = :stock')
  777.       ->setParameter('stock'0)
  778.       ->andWhere('u.isNew = :new')
  779.       ->setParameter('new'1)
  780.       ->andWhere('u.pricePDV > 0');
  781.     $qb
  782.       ->orderBy('u.isOutOfStock''ASC')
  783.       ->addOrderBy('u.created''DESC')
  784.       ->getQuery();
  785.     return $qb;
  786.   }
  787.   public function getProductsOutlet(): QueryBuilder {
  788.     $qb $this->createQueryBuilder('u')
  789.       ->select('u.id, u.title, u.sku, u.barcode, u.productKey, u.pricePDV, u.price, u.pricePrikaz, u.vat, u.isOutOfStock as outOfStock, u.isNew as new, u.isDiscount as discount, u.isSuperProduct as superProduct, u.isOutlet as isOutlet, u.isSuspended as suspended, c.title as category, i.thumbnail100, i.thumbnail500'// Include the root entity 'u'
  790.       ->leftJoin('u.category''c'// Left join with the 'category' entity
  791.       ->leftJoin('u.mainImage''i');
  792.     $qb->where('u.isSuspended = :suspenzija')
  793.       ->setParameter('suspenzija'0)
  794.       ->andWhere('u.isOutOfStock = :stock')
  795.       ->setParameter('stock'0)
  796.       ->andWhere('u.isOutlet = :outlet')
  797.       ->setParameter('outlet'1)
  798.       ->andWhere('u.pricePDV > 0');
  799.     $qb
  800.       ->orderBy('u.isOutOfStock''ASC')
  801.       ->addOrderBy('u.id''ASC')
  802.       ->getQuery();
  803.     return $qb;
  804.   }
  805.   public function searchByTerm(string $term) {
  806.     return $this->createQueryBuilder('p')
  807.       ->select('p.id, p.title, p.sku, p.kolicina, p.pricePDV, p.priceNabavna, p.vat, p.isDiscount, p.isOutOfStock, i.thumbnail100'// Include the root entity 'u'
  808.       ->leftJoin('p.mainImage''i')
  809. //      ->where('p.title LIKE :term OR p.sku LIKE :term')
  810.       ->where('p.title LIKE :term')
  811.       ->andWhere('p.pricePDV > 0')
  812.       ->andWhere('p.isSuspended = 0')
  813.       ->setParameter('term''%' $term '%')
  814.       ->getQuery()
  815.       ->getResult();
  816.   }
  817.   public function searchByTermSuper(string $term) {
  818.     return $this->createQueryBuilder('p')
  819.       ->select('p.id, p.title, p.sku, p.kolicina, p.pricePDV, p.priceNabavna, p.vat, p.isDiscount, p.isOutOfStock, i.thumbnail100'// Include the root entity 'u'
  820.       ->leftJoin('p.mainImage''i')
  821. //      ->where('p.title LIKE :term OR p.sku LIKE :term')
  822.       ->where('p.title LIKE :term')
  823.       ->andWhere('p.pricePDV > 0')
  824.       ->andWhere('p.isSuperProduct = 0')
  825.       ->andWhere('p.isDiscount = 0')
  826.       ->andWhere('p.isOutOfStock = 0')
  827.       ->setParameter('term''%' $term '%')
  828.       ->getQuery()
  829.       ->getResult();
  830.   }
  831.   public function searchByTermAjax(string $term) {
  832.     return $this->createQueryBuilder('p')
  833.       ->select('p.id, p.title, p.pricePDV, i.thumbnail100'// Include the root entity 'u'
  834.       ->leftJoin('p.mainImage''i')
  835. //      ->where('p.title LIKE :term OR p.sku LIKE :term')
  836.       ->where('p.title LIKE :term')
  837.       ->andWhere('p.pricePDV > 0')
  838.       ->andWhere('p.isSuspended = 0')
  839.       ->setParameter('term''%' $term '%')
  840.       ->setMaxResults(3)
  841.       ->getQuery()
  842.       ->getResult();
  843.   }
  844.   public function getProductById(int $productId) {
  845.     return $this->createQueryBuilder('p')
  846.       ->select('p.id''p.title''p.barcode''p.productKey''p.sku''p.vat''p.kolicina''p.weight''p.price',
  847.         'p.pricePDV''p.priceNabavna''p.isOutOfStock as outOfStock''p.isNew as new''p.isDiscount as discount',
  848.         'p.isSuperProduct as superProduct''p.isOutlet as isOutlet''p.isSuspended as suspended''p.jedinicaMere',
  849.         'p.priceDiscount''p.percentDiscount''p.priceDelivery''p.daysDelivery''ca.title as category',
  850.         'mi.thumbnail100''mi.thumbnail500''mi.thumbnail1024''f.title as firma''v.title as dobavljac',
  851.         'con.title as ctitle''con.id as cid')
  852.       ->leftJoin('p.mainImage''mi')
  853.       ->leftJoin('p.factory''f')
  854.       ->leftJoin('p.vendor''v')
  855.       ->leftJoin('p.editBy''e')
  856.       ->leftJoin('p.createdBy''cb')
  857.       ->leftJoin('p.connected''con')
  858.       ->leftJoin('p.category''ca')
  859.       ->where('p.id = :id')
  860.       ->setParameter('id'$productId)
  861.       ->getQuery()
  862.       ->getOneOrNullResult();
  863. //    // Kreiraj QueryBuilder
  864. //    return $this->createQueryBuilder('p')
  865. //       ->select('p.id', 'p.title', 'p.barcode', 'p.productKey', 'p.sku', 'p.vat', 'p.kolicina', 'p.weight', 'p.price',
  866. //         'p.pricePDV', 'p.priceNabavna', 'p.isOutOfStock as outOfStock, p.isNew as new, p.isDiscount as discount, p.isSuperProduct as superProduct, p.isOutlet as isOutlet, p.isSuspended as suspended', 'p.jedinicaMere', 'p.priceDiscount', 'p.percentDiscount', 'p.priceDelivery',
  867. //         'p.daysDelivery', 'ca.title as category', 'mi.thumbnail100', 'mi.thumbnail500', 'mi.thumbnail1024', 'f.title as firma', 'v.title as dobavljac', 'ca', 'con.title as ctitle', 'con.id as cid')
  868. //      ->leftJoin('p.mainImage', 'mi')
  869. ////      ->leftJoin('p.label', 'l')
  870. //      ->leftJoin('p.factory', 'f')
  871. //      ->leftJoin('p.vendor', 'v')
  872. ////      ->leftJoin('p.comment', 'c')
  873. ////      ->leftJoin('p.image', 'i')
  874. ////      ->leftJoin('p.pdf', 'pdf')
  875. //      ->leftJoin('p.editBy', 'e')
  876. //      ->leftJoin('p.createdBy', 'cb')
  877. //      ->leftJoin('p.connected', 'con')
  878. //      ->leftJoin('p.category', 'ca')
  879. ////      ->leftJoin('p.sales', 's')
  880. ////      ->leftJoin('p.choice', 'cho')
  881. ////      ->leftJoin('p.lagproducts', 'lag')
  882. //      ->where('p.id = :id')
  883. //      ->setParameter('id', $productId)
  884. //      ->getQuery()
  885. //      ->getOneOrNullResult();
  886.   }
  887.   public function searchByCategory(Category $category): array {
  888.     $qb $this->createQueryBuilder('p')
  889.       ->select('p.id, p.title, p.sku, p.barcode, p.productKey, p.pricePrikaz, p.pricePDV, p.price, p.vat, p.isOutOfStock as outOfStock, p.isNew as new, p.isDiscount as discount, p.isSuperProduct as superProduct, p.isOutlet as isOutlet, p.isSuspended as suspended, c.title as category, i.thumbnail100, i.thumbnail500'// Include the root entity 'u'
  890.       ->leftJoin('p.category''c'// Left join with the 'category' entity
  891.       ->leftJoin('p.mainImage''i')
  892.       ->where('p.category = :categoryId')
  893.       ->andWhere('p.isSuspended = :isSuspended')
  894.       ->andWhere('p.isOutOfStock = :isOutOfStock')
  895.       ->andWhere('p.pricePDV > 0')
  896.       ->setParameter('categoryId'$category)
  897.       ->setParameter('isSuspended'false)
  898.       ->setParameter('isOutOfStock'false);
  899.     $products $qb->getQuery()->getResult();
  900.     // Shuffle the products array to get random products
  901.     shuffle($products);
  902.     // Return only 10 random products
  903.     return array_slice($products010);
  904.   }
  905.   public function startSale(Sale $sale): void {
  906.     $em $this->getEntityManager();
  907.     $db $this->getEntityManager()->getConnection();
  908.     if (!$sale->getCategory()->isEmpty()) {
  909.       $result = [];
  910.       foreach ($sale->getCategory() as $category) {
  911.         $qb $this->createQueryBuilder('p');
  912.         $categoryIds array_merge([$category], $this->getEntityManager()->getRepository(Category::class)->findAllSubcategories($category->getId()));
  913. //        $categoryIds = array_map(fn($cat) => $cat->getId(), $allCategories);
  914.         $qb
  915.           ->andWhere('p.category IN (:categoryIds)')
  916.           ->andWhere('p.pricePDV > 0')
  917.           ->andWhere('p.isSuspended = 0')
  918.           ->andWhere('p.isDiscount = 0')
  919.           ->setParameter('categoryIds'$categoryIds);
  920.         $query $qb->getQuery();
  921.         $products $query->getResult();
  922.         $result array_merge($result$products);
  923.       }
  924.       foreach ($result as $product) {
  925.         $product->setIsDiscount(true);
  926.         if ($product->isOutOfStock()) {
  927.           $product->setIsOutOfStock(false);
  928.           $product->setIsOutOfStockPc(false);
  929.         }
  930.         $originalPricePDV $product->getPricePDV();
  931.         // Dobij procenat popusta
  932.         $discountPercentage $sale->getPercentDiscount();
  933.         // Izračunaj novu cenu sa popustom
  934.         $discountedPrice $originalPricePDV - ($originalPricePDV * ($discountPercentage 100));
  935.         // Dobij nabavnu cenu i procenat poreza
  936.         $costPrice $product->getPriceNabavna();
  937.         $vatPercentage $product->getVat();
  938.         // Izračunaj minimalnu cenu (nabavna cena uvećana za 2% + porez)
  939.         $minimumPrice $costPrice 1.02 * (+ ($vatPercentage 100));
  940.         // Postavi novu cenu (ili minimalnu ako je umanjena cena manja)
  941.         $finalPrice = ($discountedPrice $minimumPrice) ? $minimumPrice $discountedPrice;
  942.         $percentageDecrease = (($originalPricePDV $finalPrice) / $originalPricePDV) * 100;
  943.         if (is_null($product->getPricePrikaz())) {
  944.           $prikaz = ($product->getPrice() * (($product->getVat()/100) + 1));
  945.           $product->setPricePrikaz($prikaz);
  946.         }
  947.         // Postavi vrednosti cena za proizvod
  948.         $product->setPriceDiscount($finalPrice);
  949.         $product->setPricePDV($finalPrice);
  950.         $product->setPercentDiscount($percentageDecrease);
  951.         $product->setSale($sale->getId());
  952.         $em->getRepository(Product::class)->save($product);
  953.       }
  954.     } elseif (!$sale->getFactory()->isEmpty()) {
  955.       $factories = [];
  956.       foreach ($sale->getFactory() as $factory) {
  957.         $factories[] = $factory->getId();
  958.       }
  959.       $qb $this->createQueryBuilder('p');
  960.       $qb
  961.         ->andWhere('p.factory IN (:factories)')
  962.         ->andWhere('p.pricePDV > 0')
  963.         ->andWhere('p.isSuspended = 0')
  964.         ->andWhere('p.isDiscount = 0')
  965.         ->setParameter('factories'$factories);
  966.       $query $qb->getQuery();
  967.       $result $query->getResult();
  968.       foreach ($result as $product) {
  969.         $product->setIsDiscount(true);
  970.         if ($product->isOutOfStock()) {
  971.           $product->setIsOutOfStock(false);
  972.           $product->setIsOutOfStockPc(false);
  973.         }
  974.         $originalPricePDV $product->getPricePDV();
  975.         // Dobij procenat popusta
  976.         $discountPercentage $sale->getPercentDiscount();
  977.         // Izračunaj novu cenu sa popustom
  978.         $discountedPrice $originalPricePDV - ($originalPricePDV * ($discountPercentage 100));
  979.         // Dobij nabavnu cenu i procenat poreza
  980.         $costPrice $product->getPriceNabavna();
  981.         $vatPercentage $product->getVat();
  982.         // Izračunaj minimalnu cenu (nabavna cena uvećana za 2% + porez)
  983.         $minimumPrice $costPrice 1.02 * (+ ($vatPercentage 100));
  984.         // Postavi novu cenu (ili minimalnu ako je umanjena cena manja)
  985.         $finalPrice = ($discountedPrice $minimumPrice) ? $minimumPrice $discountedPrice;
  986.         $percentageDecrease = (($originalPricePDV $finalPrice) / $originalPricePDV) * 100;
  987.         if (is_null($product->getPricePrikaz())) {
  988.           $prikaz = ($product->getPrice() * (($product->getVat()/100) + 1));
  989.           $product->setPricePrikaz($prikaz);
  990.         }
  991.         // Postavi vrednosti cena za proizvod
  992.         $product->setPriceDiscount($finalPrice);
  993.         $product->setPricePDV($finalPrice);
  994.         $product->setPercentDiscount($percentageDecrease);
  995.         $product->setSale($sale->getId());
  996.         $em->getRepository(Product::class)->save($product);
  997.       }
  998.     } else {
  999.       foreach ($sale->getProduct() as $product) {
  1000.         if (!$product->isDiscount()) {
  1001.           $product->setIsDiscount(true);
  1002.           $product->setPricePDV($product->getPriceDiscount());
  1003.           if (is_null($product->getPricePrikaz())) {
  1004.             $prikaz = ($product->getPrice() * (($product->getVat()/100) + 1));
  1005.             $product->setPricePrikaz($prikaz);
  1006.           }
  1007.           if ($product->isOutOfStock()) {
  1008.             $product->setIsOutOfStock(false);
  1009.             $product->setIsOutOfStockPc(false);
  1010.           }
  1011.           $product->setSale($sale->getId());
  1012.           $em->getRepository(Product::class)->save($product);
  1013.         } else {
  1014.           $sale->removeProduct($product);
  1015.         }
  1016.       }
  1017.     }
  1018.     $sale->setIsActive(true);
  1019.     $em->getRepository(Sale::class)->save($sale);
  1020.   }
  1021.   public function stopSale(Sale $sale): void {
  1022.     $em $this->getEntityManager();
  1023.     $db $this->getEntityManager()->getConnection();
  1024.     $productsList = [];
  1025.     if (!$sale->getCategory()->isEmpty()) {
  1026.       $result = [];
  1027.       foreach ($sale->getCategory() as $category) {
  1028.         $qb $this->createQueryBuilder('p');
  1029.         $categoryIds array_merge([$category], $this->getEntityManager()->getRepository(Category::class)->findAllSubcategories($category->getId()));
  1030.         $qb
  1031.           ->andWhere('p.category IN (:categoryIds)')
  1032.           ->andWhere('p.pricePDV > 0')
  1033.           ->andWhere('p.isSuspended = 0')
  1034.           ->andWhere('p.isDiscount = 1')
  1035.           ->setParameter('categoryIds'$categoryIds);
  1036.         $query $qb->getQuery();
  1037.         $products $query->getResult();
  1038.         $result array_merge($result$products);
  1039.       }
  1040.       $productsList $result;
  1041.     }
  1042.     elseif (!$sale->getFactory()->isEmpty()) {
  1043.       $factories = [];
  1044.       foreach ($sale->getFactory() as $factory) {
  1045.         $factories[] = $factory->getId();
  1046.       }
  1047.       $qb $this->createQueryBuilder('p');
  1048.       $qb
  1049.         ->andWhere('p.factory IN (:factories)')
  1050.         ->andWhere('p.pricePDV > 0')
  1051.         ->andWhere('p.isSuspended = 0')
  1052.         ->andWhere('p.isDiscount = 1')
  1053.         ->setParameter('factories'$factories);
  1054.       $query $qb->getQuery();
  1055.       $result $query->getResult();
  1056.       $productsList $result;
  1057.     }
  1058.     else {
  1059.       foreach ($sale->getProduct() as $product) {
  1060.         if ($product->isDiscount()) {
  1061.           $productsList[] = $product;
  1062.         }
  1063.       }
  1064.     }
  1065.     $artikliAkcija = [];
  1066.     foreach ($productsList as $product) {
  1067.       $artikliAkcija[] = [
  1068.         'id' => $product->getId(),
  1069.         'title' => $product->getTitle(),
  1070.         'akcija' => $product->getPricePDV(),
  1071.         'prikaz' => $product->getPricePrikaz(),
  1072.         'prava' => $product->getPrice() * 1.2,
  1073.         'popust' => $product->getPercentDiscount()
  1074.       ];
  1075.     }
  1076.     $archive = [
  1077.       'id' => $sale->getId(),
  1078.       'title' => $sale->getTitle(),
  1079.       'percent' => $sale->getPercentDiscount(),
  1080.       'delivery' => $sale->getDelivery(),
  1081.       'start' => $sale->getStart()->format('d.m.Y'),
  1082.       'stop' => $sale->getStop()->format('d.m.Y'),
  1083.       'artikli' => $artikliAkcija
  1084.     ];
  1085.     foreach ($productsList as $product) {
  1086.       if ($product->isDiscount()) {
  1087.         //ako su proizvodi iz radnje povuci ih posle akcije
  1088.         if ($product->getVendor()->getId() == 11) {
  1089.           $product->setIsSuspended(true);
  1090.         }
  1091.         $product->setIsDiscount(false);
  1092.         $product->setPriceDiscount(null);
  1093.         $product->setPercentDiscount(null);
  1094.         $product->setPricePrikaz(null);
  1095.         $product->setPricePDV($product->getPrice() * (+ ($product->getVat() / 100)));
  1096.         $product->setSale(null);
  1097.         $em->getRepository(Product::class)->save($product);
  1098.       }
  1099.     }
  1100.     $sale->setIsActive(false);
  1101.     $sale->setArchive($archive);
  1102.     $em->getRepository(Sale::class)->save($sale);
  1103.   }
  1104.   public function startSuperSale(Supersale $sale): void {
  1105.     $em $this->getEntityManager();
  1106.     $ss $em->getRepository(Product::class)->findOneBy(['isSuperProduct' => true]);
  1107.     if (is_null($ss)) {
  1108.       $product $sale->getProduct();
  1109.       if (!$product->isDiscount()) {
  1110.         $product->setIsDiscount(true);
  1111.         $product->setIsSuperProduct(true);
  1112.         $product->setPricePDV($sale->getPricePDV());
  1113.         $product->setPricePrikaz($sale->getPricePrikaz());
  1114.         $product->setPercentDiscount($sale->getPercentDiscount());
  1115.         if ($product->isOutOfStock()) {
  1116.           $product->setIsOutOfStock(false);
  1117.           $product->setIsOutOfStockPc(false);
  1118.         }
  1119.         $em->getRepository(Product::class)->save($product);
  1120.       }
  1121.       $sale->setIsActive(true);
  1122.       $em->getRepository(Supersale::class)->save($sale);
  1123.     }
  1124.   }
  1125.   public function stopSuperSale(Supersale $sale): void {
  1126.     $em $this->getEntityManager();
  1127.    $product $sale->getProduct();
  1128.       if ($product->isSuperProduct()) {
  1129.         //ako su proizvodi iz radnje povuci ih posle akcije
  1130.         if ($product->getVendor()->getId() == 11) {
  1131.           $product->setIsSuspended(true);
  1132.         }
  1133.         $product->setIsDiscount(false);
  1134.         $product->setIsSuperProduct(false);
  1135.         $product->setPricePrikaz(null);
  1136.         $product->setPricePDV($product->getPrice() * (+ ($product->getVat() / 100)));
  1137.         $product->setPercentDiscount(null);
  1138.         $em->getRepository(Product::class)->save($product);
  1139.       }
  1140.     $sale->setIsActive(false);
  1141.     $em->getRepository(Supersale::class)->save($sale);
  1142.   }
  1143.   public function removeNewLabel() {
  1144.     $em $this->getEntityManager();
  1145.     $db $this->getEntityManager()->getConnection();
  1146.     $sql "UPDATE " $em->getClassMetadata(Product::class)->getTableName() . "
  1147.             SET is_new = 0
  1148.             WHERE DATEDIFF(NOW(), created) > 30
  1149.             AND is_suspended = 0
  1150.             AND is_new = 1 ";
  1151.     $result $db->prepare($sql);
  1152.     $result->execute();
  1153.   }
  1154. }