src/Repository/ProductRepository.php line 716
<?phpnamespace App\Repository;use App\Classes\AppConfig;use App\Entity\Category;use App\Entity\Client;use App\Entity\Product;use App\Entity\Sale;use App\Entity\Supersale;use DateTimeImmutable;use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;use Doctrine\DBAL\Result;use Doctrine\ORM\Query\ResultSetMapping;use Doctrine\ORM\QueryBuilder;use Doctrine\Persistence\ManagerRegistry;/*** @extends ServiceEntityRepository<Product>** @method Product|null find($id, $lockMode = null, $lockVersion = null)* @method Product|null findOneBy(array $criteria, array $orderBy = null)* @method Product[] findAll()* @method Product[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)*/class ProductRepository extends ServiceEntityRepository {public function __construct(ManagerRegistry $registry) {parent::__construct($registry, Product::class);}public function save(Product $product): Product {if ($product->isSuperProduct()) {$productSuper = $this->findOneBy(['isSuperProduct' => true]);if (!is_null($productSuper)) {$productSuper->setIsSuperProduct(false);}}if (is_null($product->getId())) {$this->getEntityManager()->persist($product);}$this->getEntityManager()->flush();return $product;}public function remove(Product $entity, bool $flush = false): void {$this->getEntityManager()->remove($entity);if ($flush) {$this->getEntityManager()->flush();}}public function delete(Product $product) {$em = $this->getEntityManager();$db = $this->getEntityManager()->getConnection();$sql = "DELETE FROM " . $em->getClassMetadata(Product::class)->getTableName() . "WHERE id = :product_id ";$result = $db->prepare($sql);$result->bindValue(':product_id', $product->getId());$result->execute();}public function findForForm(int $id = 0): Product {if (empty($id)) {return new Product();}return $this->getEntityManager()->getRepository(Product::class)->find($id);}public function getAllPaginator($filter, $suspended): QueryBuilder {$qb = $this->createQueryBuilder('u')->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'->leftJoin('u.category', 'c') // Left join with the 'category' entity->leftJoin('u.mainImage', 'i') // Left join with the 'category' entity->where('u.isSuspended = :suspenzija')->setParameter('suspenzija', $suspended);// Optimizovan filter za 'naziv'if (!empty($filter['naziv'])) {$qb->andWhere('u.title LIKE :naziv')->setParameter('naziv', '%' . $filter['naziv'] . '%');// ->setParameter('naziv', '%' . $filter['naziv']);}// Optimizovan filter za 'barcode'if (!empty($filter['barcode'])) {$qb->andWhere('u.barcode LIKE :barcode')->setParameter('barcode', '%' . $filter['barcode'] . '%');}// Optimizovan filter za 'sku'if (!empty($filter['sku'])) {$qb->andWhere('u.sku LIKE :sku')->setParameter('sku', '%' . $filter['sku'] . '%');}// Optimizovan filter za 'productKey'if (!empty($filter['productKey'])) {$qb->andWhere('u.productKey LIKE :productKey')->setParameter('productKey', '%' . $filter['productKey'] . '%');}// Optimizovan filter za 'stock'if (!empty($filter['stock'])) {$qb->andWhere('u.isOutOfStock = :stock')->setParameter('stock', $filter['stock']);}$qb->orderBy('u.id', 'ASC');return $qb;}public function getAllBundlePaginator($filter): QueryBuilder {$qb = $this->createQueryBuilder('u')->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'->leftJoin('u.category', 'c') // Left join with the 'category' entity->leftJoin('u.mainImage', 'i') // Left join with the 'category' entity->where('u.isBundle = 1');// Optimizovan filter za 'naziv'if (!empty($filter['naziv'])) {$qb->andWhere('u.title LIKE :naziv')->setParameter('naziv', '%' . $filter['naziv'] . '%');// ->setParameter('naziv', '%' . $filter['naziv']);}// Optimizovan filter za 'barcode'if (!empty($filter['barcode'])) {$qb->andWhere('u.barcode LIKE :barcode')->setParameter('barcode', '%' . $filter['barcode'] . '%');}// Optimizovan filter za 'sku'if (!empty($filter['sku'])) {$qb->andWhere('u.sku LIKE :sku')->setParameter('sku', '%' . $filter['sku'] . '%');}// Optimizovan filter za 'productKey'if (!empty($filter['productKey'])) {$qb->andWhere('u.productKey LIKE :productKey')->setParameter('productKey', '%' . $filter['productKey'] . '%');}// Optimizovan filter za 'stock'if (!empty($filter['stock'])) {$qb->andWhere('u.isOutOfStock = :stock')->setParameter('stock', $filter['stock']);}$qb->orderBy('u.id', 'ASC');return $qb;}// public function getAllPaginator($filter, $suspended): QueryBuilder {//// $qb = $this->createQueryBuilder('u');//// $qb->where('u.isSuspended = :suspenzija')// ->setParameter('suspenzija', $suspended);//// if (!empty($filter['naziv'])) {// $qb->andWhere($qb->expr()->orX(// $qb->expr()->like('u.title', ':naziv'),// ))// ->setParameter('naziv', '%' . $filter['naziv'] . '%');// }// if (!empty($filter['barcode'])) {// $qb->andWhere($qb->expr()->orX(// $qb->expr()->like('u.barcode', ':barcode'),// ))// ->setParameter('barcode', '%' . $filter['barcode'] . '%');// }// if (!empty($filter['sku'])) {// $qb->andWhere($qb->expr()->orX(// $qb->expr()->like('u.sku', ':sku'),// ))// ->setParameter('sku', '%' . $filter['sku'] . '%');// }// if (!empty($filter['productKey'])) {// $qb->andWhere($qb->expr()->orX(// $qb->expr()->like('u.productKey', ':productKey'),// ))// ->setParameter('productKey', '%' . $filter['productKey'] . '%');// }// if (!empty($filter['stock'])) {// $qb->andWhere('u.isOutOfStock = :stock');// $qb->setParameter('stock', $filter['stock']);// }//// $qb// ->orderBy('u.id', 'ASC')// ->getQuery();//// return $qb;// }// public function getAllPaginatorCategory($filter, $suspended): QueryBuilder {//// $category = $this->getEntityManager()->getRepository(Category::class)->find(AppConfig::KATEGORIJA);//// $qb = $this->createQueryBuilder('u');//// $qb->where('u.isSuspended = :suspenzija')// ->setParameter('suspenzija', $suspended)// ->andWhere('u.category = :category')// ->setParameter('category', $category);//// if (!empty($filter['naziv'])) {// $qb->andWhere($qb->expr()->orX(// $qb->expr()->like('u.title', ':naziv'),// ))// ->setParameter('naziv', '%' . $filter['naziv'] . '%');// }// if (!empty($filter['barcode'])) {// $qb->andWhere($qb->expr()->orX(// $qb->expr()->like('u.barcode', ':barcode'),// ))// ->setParameter('barcode', '%' . $filter['barcode'] . '%');// }// if (!empty($filter['sku'])) {// $qb->andWhere($qb->expr()->orX(// $qb->expr()->like('u.sku', ':sku'),// ))// ->setParameter('sku', '%' . $filter['sku'] . '%');// }// if (!empty($filter['productKey'])) {// $qb->andWhere($qb->expr()->orX(// $qb->expr()->like('u.productKey', ':productKey'),// ))// ->setParameter('productKey', '%' . $filter['productKey'] . '%');// }// if (!empty($filter['stock'])) {// $qb->andWhere('u.isOutOfStock = :stock');// $qb->setParameter('stock', $filter['stock']);// }//// $qb// ->orderBy('u.id', 'ASC')// ->getQuery();//// return $qb;// }public function getAllPaginatorCategory($filter, $suspended): QueryBuilder {$qb = $this->createQueryBuilder('u')->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'->leftJoin('u.category', 'c') // Left join with the 'category' entity->leftJoin('u.mainImage', 'i') // Left join with the 'category' entity->where('u.isSuspended = :suspenzija')->setParameter('suspenzija', $suspended)->andWhere('u.category = :kategorija')->setParameter('kategorija', AppConfig::KATEGORIJA);// Optimizovan filter za 'naziv'if (!empty($filter['naziv'])) {$qb->andWhere('u.title LIKE :naziv')->setParameter('naziv', '%' . $filter['naziv'] . '%');// ->setParameter('naziv', '%' . $filter['naziv']);}// Optimizovan filter za 'barcode'if (!empty($filter['barcode'])) {$qb->andWhere('u.barcode LIKE :barcode')->setParameter('barcode', '%' . $filter['barcode'] . '%');}// Optimizovan filter za 'sku'if (!empty($filter['sku'])) {$qb->andWhere('u.sku LIKE :sku')->setParameter('sku', '%' . $filter['sku'] . '%');}// Optimizovan filter za 'productKey'if (!empty($filter['productKey'])) {$qb->andWhere('u.productKey LIKE :productKey')->setParameter('productKey', '%' . $filter['productKey'] . '%');}// Optimizovan filter za 'stock'if (!empty($filter['stock'])) {$qb->andWhere('u.isOutOfStock = :stock')->setParameter('stock', $filter['stock']);}$qb->orderBy('u.id', 'ASC');return $qb;}//funkcija za vracenje broja artikala koji nisu u tacnim kategorijamapublic function artikliPoKategorijama($kategorije) {$qb = $this->createQueryBuilder('p')->select('IDENTITY(p.category) as category_id, COUNT(p.id) as product_count')->where('p.category IN (:categoryIds)')->setParameter('categoryIds', $kategorije)->groupBy('p.category');$rez = $qb->getQuery()->getResult();$finish = [];foreach ($rez as $item) {$categ = $this->getEntityManager()->getRepository(Category::class)->find($item['category_id']);$finish[] =[$categ->getTitle(),$item['product_count']];}return $finish;}public function getAllByPaginatorCategory($filter, $suspended, $category): QueryBuilder {$qb = $this->createQueryBuilder('u')->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'->leftJoin('u.category', 'c') // Left join with the 'category' entity->leftJoin('u.mainImage', 'i') // Left join with the 'category' entity->where('u.isSuspended = :suspenzija')->setParameter('suspenzija', $suspended)->andWhere('u.category = :kategorija')->setParameter('kategorija', $category);// Optimizovan filter za 'naziv'if (!empty($filter['naziv'])) {$qb->andWhere('u.title LIKE :naziv')->setParameter('naziv', '%' . $filter['naziv'] . '%');// ->setParameter('naziv', '%' . $filter['naziv']);}// Optimizovan filter za 'barcode'if (!empty($filter['barcode'])) {$qb->andWhere('u.barcode LIKE :barcode')->setParameter('barcode', '%' . $filter['barcode'] . '%');}// Optimizovan filter za 'sku'if (!empty($filter['sku'])) {$qb->andWhere('u.sku LIKE :sku')->setParameter('sku', '%' . $filter['sku'] . '%');}// Optimizovan filter za 'productKey'if (!empty($filter['productKey'])) {$qb->andWhere('u.productKey LIKE :productKey')->setParameter('productKey', '%' . $filter['productKey'] . '%');}// Optimizovan filter za 'stock'if (!empty($filter['stock'])) {$qb->andWhere('u.isOutOfStock = :stock')->setParameter('stock', $filter['stock']);}$qb->orderBy('u.id', 'ASC');return $qb;}// public function getAllByCategoryPaginator($filter): QueryBuilder {//// $qb = $this->createQueryBuilder('p')// ->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'// ->leftJoin('p.category', 'c') // Left join with the 'category' entity// ->leftJoin('p.vendor', 'v') // Left join with the 'category' entity// ->leftJoin('p.mainImage', 'i');////// $category = ['id' => $filter['catId']];// $allCategories = array_merge([$category], $this->getEntityManager()->getRepository(Category::class)->findAllSubcategories($filter['catId']));//// $allCategories = array_merge([$category], $this->getEntityManager()->getRepository(Category::class)->findAllSubcategories($filter['catId']));//// $categoryIds = array_map(fn($cat) => $cat['id'], $allCategories);//// if (isset($filter['title'])) {// $qb->andWhere($qb->expr()->like('p.title', ':title'))// ->setParameter('title', '%' . $filter['title'] . '%');// }//// $qb// ->andWhere('p.category IN (:categoryIds)')// ->andWhere('p.pricePDV > 0')// ->setParameter('categoryIds', $categoryIds);//// $qb// ->addOrderBy('p.title', 'ASC')// ->getQuery();//// return $qb;// }public function getAllByCategoryPaginator($filter) {//dd($filter);// Prvo učitajte sve podkategorije jednim upitom$allCategories = $this->getEntityManager()->getRepository(Category::class)->findAllSubcategoryIds($filter['catId']);// dd($allCategories);//// // Dodajte osnovnu kategoriju// $allCategories[] = ['id' => $filter['catId']];//// // Ekstrahujte ID-eve kategorija// $categoryIds = array_map(fn($cat) => $cat['id'], $allCategories);// Kreirajte QueryBuilder$qb = $this->createQueryBuilder('p')->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')// ->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')->leftJoin('p.category', 'c')->leftJoin('p.vendor', 'v')->leftJoin('p.mainImage', 'i')->where('p.category IN (:categoryIds)')->andWhere('p.pricePDV > 0')->setParameter('categoryIds', $allCategories);// Filtrirajte po naslovu ako je postavljenif (isset($filter['title'])) {$qb->andWhere($qb->expr()->like('p.title', ':title'))->setParameter('title', '%' . $filter['title'] . '%');// ->setParameter('title', '%' . $filter['title']);}if (isset($filter['pro'])) {$qb->andWhere('p.factory IN (:factory)')->setParameter('factory', $filter['pro']);}if (isset($filter['sale'])) {$qb->andWhere('p.isDiscount = 1');}$qb->addOrderBy('p.isOutOfStock', 'ASC');// dd($qb->addOrderBy('p.title', 'ASC')->getQuery());return $qb->addOrderBy('p.title', 'ASC')->getQuery();}// public function getAllByCategoryFilter($filter): QueryBuilder {// $category = $this->getEntityManager()->getRepository(Category::class)->find($filter['catId']);//// $qb = $this->createQueryBuilder('p');//// $qb// ->andWhere('p.category = :category')// ->setParameter('category', $category);//// $qb// ->addOrderBy('p.title', 'ASC')// ->getQuery();//// return $qb;// }public function getMaxMinPrice($category) {$qb = $this->createQueryBuilder('p');$minPriceSubquery = $this->createQueryBuilder('p2')->select('MIN(p2.pricePDV)')->where('p2.category = :category')->getDQL();// Podupit za najvišu cenu$maxPriceSubquery = $this->createQueryBuilder('p3')->select('MAX(p3.pricePDV)')->where('p3.category = :category')->getDQL();$qb->select('MIN(p.pricePDV) AS minPrice, MAX(p.pricePDV) AS maxPrice')->where('p.category = :category')->setParameter('category', $category)->addOrderBy('p.title', 'ASC')->setMaxResults(1);$query = $qb->getQuery();$result = $query->getSingleResult();return ['minPrice' => (float)$result['minPrice'],'maxPrice' => (float)$result['maxPrice'],];}public function getAllByCategoryFilter($filter): QueryBuilder {// $category = $this->getEntityManager()->getRepository(Category::class)->find($filter['catId']);$qb = $this->createQueryBuilder('p')// ->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'->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'->leftJoin('p.category', 'c') // Left join with the 'category' entity->leftJoin('p.vendor', 'v') // Left join with the 'category' entity->leftJoin('p.mainImage', 'i');$qb->andWhere('p.category = :category')->andWhere('p.pricePDV > 0')->setParameter('category', $filter['catId']);if (isset($filter['vendor'])) {$qb->andWhere('p.factory IN (:factory)')->setParameter('factory', $filter['vendor']);}if (isset($filter['title'])) {$qb->andWhere($qb->expr()->like('p.title', ':title'))->setParameter('title', '%' . $filter['title'] . '%');}if (isset($filter['sale'])) {$qb->andWhere('p.isDiscount = 1');}// Dodavanje uslova za priceRangeif (isset($filter['priceRange'])) {[$minPrice, $maxPrice] = explode(';', $filter['priceRange']);$qb->andWhere('p.pricePDV BETWEEN :minPrice AND :maxPrice')->setParameter('minPrice', $minPrice)->setParameter('maxPrice', $maxPrice);}function slugify($text) {// Zamena specijalnih karaktera$text = preg_replace('~[^\pL\d]+~u', '_', $text);// Transliteration$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);// Lowercase$text = strtolower($text);// Uklanjanje preostalih neželjenih karaktera$text = preg_replace('~[^_\w]+~', '', $text);// Uklanjanje viška _$text = trim($text, '_');// Return final aliasreturn $text;}foreach ($filter as $key => $values) {if ($key !== 'catId' && $key !== 'vendor' && $key !== 'priceRange' && $key !== 'title' && $key !== 'sale') {// $alias = 'a_' . $key;$alias = 'a_' . slugify($key);$qb->leftJoin('p.attribute', $alias, 'WITH', $alias . '.title = :title_' . $alias)->andWhere($alias . '.value IN (:values_' . $alias . ')')->setParameter('title_' . $alias, $key)->setParameter('values_' . $alias, $values);// $qb->leftJoin('p.attribute', $alias, 'WITH', $alias . '.title = :title_' . $key)// ->andWhere($alias . '.value IN (:values_' . $key . ')')// ->setParameter('title_' . $key, $key)// ->setParameter('values_' . $key, $values);}}$qb->addOrderBy('p.isOutOfStock', 'ASC')->addOrderBy('p.title', 'ASC')->getQuery();return $qb;}public function findUniqueAttributesByCategory($categoryId) {$query = $this->createQueryBuilder('p')->innerJoin('p.category', 'c')->innerJoin('p.attribute', 'a')->where('c.id = :categoryId')->setParameter('categoryId', $categoryId)->select('a.title, a.value')->orderBy('a.title', 'ASC')->addOrderBy('a.value', 'ASC')->getQuery();$result = $query->getResult();// Organizovanje rezultata u jedinstvene atribute sa vrednostima$attributes = [];foreach ($result as $row) {$title = $row['title'];$value = $row['value'];// preskoči samo ako je null ili prazan stringif ($title === null || $title === '') {continue;}if ($value === null || $value === '') {continue;}if (!isset($attributes[$title])) {$attributes[$title] = [];}if (!in_array($value, $attributes[$title])) {$attributes[$title][] = $value;}}// Sortiranje atributa po nazivuksort($attributes);return $attributes;// return $this->createQueryBuilder('p')// ->innerJoin('p.category', 'c')// ->innerJoin('p.attribute', 'a')// ->where('c.id = :categoryId')// ->setParameter('categoryId', $categoryId)// ->select('DISTINCT a.title')// ->getQuery()// ->getResult();}public function findUniqueAttributesByCategoryVendor($categoryId, $vendorId) {$query = $this->createQueryBuilder('p')->innerJoin('p.category', 'c')->innerJoin('p.attribute', 'a')->where('c.id = :categoryId')->setParameter('categoryId', $categoryId)->andWhere('p.factory = :vendorId')->setParameter('vendorId', $vendorId)->select('a.title, a.value')->orderBy('a.title', 'ASC')->addOrderBy('a.value', 'ASC')->getQuery();$result = $query->getResult();// Organizovanje rezultata u jedinstvene atribute sa vrednostima$attributes = [];foreach ($result as $row) {$title = $row['title'];$value = $row['value'];// preskoči samo ako je null ili prazan stringif ($title === null || $title === '') {continue;}if ($value === null || $value === '') {continue;}if (!isset($attributes[$title])) {$attributes[$title] = [];}if (!in_array($value, $attributes[$title])) {$attributes[$title][] = $value;}}// Sortiranje atributa po nazivuksort($attributes);return $attributes;// return $this->createQueryBuilder('p')// ->innerJoin('p.category', 'c')// ->innerJoin('p.attribute', 'a')// ->where('c.id = :categoryId')// ->setParameter('categoryId', $categoryId)// ->select('DISTINCT a.title')// ->getQuery()// ->getResult();}public function findUniqueFactoriesByCategory($categoryId) {return $this->createQueryBuilder('p')->innerJoin('p.category', 'c')->innerJoin('p.factory', 'f')->where('c.id = :categoryId')->setParameter('categoryId', $categoryId)->select('DISTINCT f.id, f.title')->orderBy('f.title', 'ASC')->getQuery()->getResult();}public function getProductsSearchPaginator($filter): QueryBuilder {$qb = $this->createQueryBuilder('t')->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'->leftJoin('t.category', 'c') // Left join with the 'category' entity->leftJoin('t.mainImage', 'i')->where('t.pricePDV > 0')->andWhere('t.isSuspended = 0');$keywords = explode(" ", $filter['tekst']);foreach ($keywords as $key => $keyword) {$qb->andWhere($qb->expr()->orX($qb->expr()->like('t.title', ':keyword'.$key),$qb->expr()->like('t.barcode', ':keyword'.$key),$qb->expr()->like('t.productKey', ':keyword'.$key),$qb->expr()->like('t.sku', ':keyword'.$key)))->setParameter('keyword'.$key, '%' . $keyword . '%');}$qb->addOrderBy('t.title', 'ASC')->getQuery();return $qb;}public function getProductsSearch($keywords): QueryBuilder {$qb = $this->createQueryBuilder('t')->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'->leftJoin('t.category', 'c') // Left join with the 'category' entity->leftJoin('t.mainImage', 'i')->where('t.pricePDV > 0')->andWhere('t.isSuspended = 0');$keywords = explode(" ", $keywords);foreach ($keywords as $key => $keyword) {$qb->andWhere($qb->expr()->orX($qb->expr()->like('t.title', ':keyword'.$key),$qb->expr()->like('t.barcode', ':keyword'.$key),$qb->expr()->like('t.productKey', ':keyword'.$key),$qb->expr()->like('t.sku', ':keyword'.$key)))->setParameter('keyword'.$key, '%' . $keyword . '%');}$qb->addOrderBy('t.isOutOfStock', 'ASC')->addOrderBy('t.title', 'ASC')->getQuery();return $qb;}public function getProductsSale($category, $sale): QueryBuilder {$qb = $this->createQueryBuilder('u')->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'->leftJoin('u.category', 'c') // Left join with the 'category' entity->leftJoin('u.mainImage', 'i');$qb->where('u.isSuspended = :suspenzija')->setParameter('suspenzija', 0)->andWhere('u.isDiscount = :discount')->setParameter('discount', 1)->andWhere('u.pricePDV > 0');if ($category > 0) {$qb->andWhere('u.category = :category')->setParameter('category', $category);}if ($sale > 0) {$qb->andWhere('u.sale = :sale')->setParameter('sale', $sale);}$qb->orderBy('u.isOutOfStock', 'ASC')->addOrderBy('u.id', 'ASC')->getQuery();return $qb;}// public function getProductsSaleHome(): array {// $qb = $this->createQueryBuilder('u')// ->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'// ->leftJoin('u.category', 'c') // Left join with the 'category' entity// ->leftJoin('u.mainImage', 'i');//// $qb->where('u.isSuspended = :suspenzija')// ->setParameter('suspenzija', 0)// ->andWhere('u.isDiscount = :discount')// ->setParameter('discount', 1)// ->andWhere('u.pricePDV > 0');//// // Izvršava upit i vraća rezultate kao niz// $products = $qb->getQuery()->getResult();//// // Shuffle the products array to get random products// shuffle($products);//// // Return only 10 random products// return array_slice($products, 0, 10);// }public function getProductsEditorHome(int $editorId): array {$qb = $this->createQueryBuilder('u')->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'->leftJoin('u.category', 'c') // Left join with the 'category' entity->leftJoin('u.mainImage', 'i');$qb->where('u.isSuspended = :suspenzija')->setParameter('suspenzija', 0)->andWhere('u.choice = :choice')->setParameter('choice', $editorId)->andWhere('u.pricePDV > 0');// Izvršava upit i vraća rezultate kao niz$products = $qb->getQuery()->getResult();// Shuffle the products array to get random productsshuffle($products);// Return only 10 random productsreturn array_slice($products, 0, 10);}public function getProductsSaleHome(int $saleId): array {$qb = $this->createQueryBuilder('u')->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'->leftJoin('u.category', 'c') // Left join with the 'category' entity->leftJoin('u.mainImage', 'i');$qb->where('u.isSuspended = :suspenzija')->setParameter('suspenzija', 0)->andWhere('u.sale = :sale')->setParameter('sale', $saleId)->andWhere('u.pricePDV > 0');// Izvršava upit i vraća rezultate kao niz$products = $qb->getQuery()->getResult();// Shuffle the products array to get random productsshuffle($products);// Return only 10 random productsreturn array_slice($products, 0, 10);}// public function getProductsPaginatorByClient(Client $client): QueryBuilder {//// $qb = $this->createQueryBuilder('u');//// $qb->where('u.vendor = :client')// ->setParameter('client', $client);//// $qb// ->orderBy('u.isSuspended', 'ASC')// ->getQuery();//// return $qb;// }public function getProductsPaginatorByClient(Client $client): QueryBuilder {$qb = $this->createQueryBuilder('u')->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'->leftJoin('u.category', 'c') // Left join with the 'category' entity->leftJoin('u.mainImage', 'i') // Left join with the 'category' entity->where('u.vendor = :client')->setParameter('client', $client);// Optimizovan filter za 'naziv'if (!empty($filter['naziv'])) {$qb->andWhere('u.title LIKE :naziv')->setParameter('naziv', '%' . $filter['naziv'] . '%');// ->setParameter('naziv', '%' . $filter['naziv']);}// Optimizovan filter za 'barcode'if (!empty($filter['barcode'])) {$qb->andWhere('u.barcode LIKE :barcode')->setParameter('barcode', '%' . $filter['barcode'] . '%');}// Optimizovan filter za 'sku'if (!empty($filter['sku'])) {$qb->andWhere('u.sku LIKE :sku')->setParameter('sku', '%' . $filter['sku'] . '%');}// Optimizovan filter za 'productKey'if (!empty($filter['productKey'])) {$qb->andWhere('u.productKey LIKE :productKey')->setParameter('productKey', '%' . $filter['productKey'] . '%');}// Optimizovan filter za 'stock'if (!empty($filter['stock'])) {$qb->andWhere('u.isOutOfStock = :stock')->setParameter('stock', $filter['stock']);}$qb->orderBy('u.isSuspended', 'ASC');return $qb;}public function getProductsNewCount(): int {$today = new DateTimeImmutable();$danas = $today->setTime(0, 0 );$queryBuilder = $this->createQueryBuilder('u');$queryBuilder->select('COUNT(u.id)')->where('u.created >= :today')->setParameter('today', $danas);return (int) $queryBuilder->getQuery()->getSingleScalarResult();}public function getProductsNew(): QueryBuilder {$qb = $this->createQueryBuilder('u')->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'->leftJoin('u.category', 'c') // Left join with the 'category' entity->leftJoin('u.mainImage', 'i');$qb->where('u.isSuspended = :suspenzija')->setParameter('suspenzija', 0)->andWhere('u.isOutOfStock = :stock')->setParameter('stock', 0)->andWhere('u.isNew = :new')->setParameter('new', 1)->andWhere('u.pricePDV > 0');$qb->orderBy('u.isOutOfStock', 'ASC')->addOrderBy('u.created', 'DESC')->getQuery();return $qb;}public function getProductsOutlet(): QueryBuilder {$qb = $this->createQueryBuilder('u')->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'->leftJoin('u.category', 'c') // Left join with the 'category' entity->leftJoin('u.mainImage', 'i');$qb->where('u.isSuspended = :suspenzija')->setParameter('suspenzija', 0)->andWhere('u.isOutOfStock = :stock')->setParameter('stock', 0)->andWhere('u.isOutlet = :outlet')->setParameter('outlet', 1)->andWhere('u.pricePDV > 0');$qb->orderBy('u.isOutOfStock', 'ASC')->addOrderBy('u.id', 'ASC')->getQuery();return $qb;}public function searchByTerm(string $term) {return $this->createQueryBuilder('p')->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'->leftJoin('p.mainImage', 'i')// ->where('p.title LIKE :term OR p.sku LIKE :term')->where('p.title LIKE :term')->andWhere('p.pricePDV > 0')->andWhere('p.isSuspended = 0')->setParameter('term', '%' . $term . '%')->getQuery()->getResult();}public function searchByTermSuper(string $term) {return $this->createQueryBuilder('p')->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'->leftJoin('p.mainImage', 'i')// ->where('p.title LIKE :term OR p.sku LIKE :term')->where('p.title LIKE :term')->andWhere('p.pricePDV > 0')->andWhere('p.isSuperProduct = 0')->andWhere('p.isDiscount = 0')->andWhere('p.isOutOfStock = 0')->setParameter('term', '%' . $term . '%')->getQuery()->getResult();}public function searchByTermAjax(string $term) {return $this->createQueryBuilder('p')->select('p.id, p.title, p.pricePDV, i.thumbnail100') // Include the root entity 'u'->leftJoin('p.mainImage', 'i')// ->where('p.title LIKE :term OR p.sku LIKE :term')->where('p.title LIKE :term')->andWhere('p.pricePDV > 0')->andWhere('p.isSuspended = 0')->setParameter('term', '%' . $term . '%')->setMaxResults(3)->getQuery()->getResult();}public function getProductById(int $productId) {return $this->createQueryBuilder('p')->select('p.id', 'p.title', 'p.barcode', 'p.productKey', 'p.sku', 'p.vat', 'p.kolicina', 'p.weight', 'p.price','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', 'p.daysDelivery', 'ca.title as category','mi.thumbnail100', 'mi.thumbnail500', 'mi.thumbnail1024', 'f.title as firma', 'v.title as dobavljac','con.title as ctitle', 'con.id as cid')->leftJoin('p.mainImage', 'mi')->leftJoin('p.factory', 'f')->leftJoin('p.vendor', 'v')->leftJoin('p.editBy', 'e')->leftJoin('p.createdBy', 'cb')->leftJoin('p.connected', 'con')->leftJoin('p.category', 'ca')->where('p.id = :id')->setParameter('id', $productId)->getQuery()->getOneOrNullResult();// // Kreiraj QueryBuilder// return $this->createQueryBuilder('p')// ->select('p.id', 'p.title', 'p.barcode', 'p.productKey', 'p.sku', 'p.vat', 'p.kolicina', 'p.weight', 'p.price',// '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',// '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')// ->leftJoin('p.mainImage', 'mi')//// ->leftJoin('p.label', 'l')// ->leftJoin('p.factory', 'f')// ->leftJoin('p.vendor', 'v')//// ->leftJoin('p.comment', 'c')//// ->leftJoin('p.image', 'i')//// ->leftJoin('p.pdf', 'pdf')// ->leftJoin('p.editBy', 'e')// ->leftJoin('p.createdBy', 'cb')// ->leftJoin('p.connected', 'con')// ->leftJoin('p.category', 'ca')//// ->leftJoin('p.sales', 's')//// ->leftJoin('p.choice', 'cho')//// ->leftJoin('p.lagproducts', 'lag')// ->where('p.id = :id')// ->setParameter('id', $productId)// ->getQuery()// ->getOneOrNullResult();}public function searchByCategory(Category $category): array {$qb = $this->createQueryBuilder('p')->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'->leftJoin('p.category', 'c') // Left join with the 'category' entity->leftJoin('p.mainImage', 'i')->where('p.category = :categoryId')->andWhere('p.isSuspended = :isSuspended')->andWhere('p.isOutOfStock = :isOutOfStock')->andWhere('p.pricePDV > 0')->setParameter('categoryId', $category)->setParameter('isSuspended', false)->setParameter('isOutOfStock', false);$products = $qb->getQuery()->getResult();// Shuffle the products array to get random productsshuffle($products);// Return only 10 random productsreturn array_slice($products, 0, 10);}public function startSale(Sale $sale): void {$em = $this->getEntityManager();$db = $this->getEntityManager()->getConnection();if (!$sale->getCategory()->isEmpty()) {$result = [];foreach ($sale->getCategory() as $category) {$qb = $this->createQueryBuilder('p');$categoryIds = array_merge([$category], $this->getEntityManager()->getRepository(Category::class)->findAllSubcategories($category->getId()));// $categoryIds = array_map(fn($cat) => $cat->getId(), $allCategories);$qb->andWhere('p.category IN (:categoryIds)')->andWhere('p.pricePDV > 0')->andWhere('p.isSuspended = 0')->andWhere('p.isDiscount = 0')->setParameter('categoryIds', $categoryIds);$query = $qb->getQuery();$products = $query->getResult();$result = array_merge($result, $products);}foreach ($result as $product) {$product->setIsDiscount(true);if ($product->isOutOfStock()) {$product->setIsOutOfStock(false);$product->setIsOutOfStockPc(false);}$originalPricePDV = $product->getPricePDV();// Dobij procenat popusta$discountPercentage = $sale->getPercentDiscount();// Izračunaj novu cenu sa popustom$discountedPrice = $originalPricePDV - ($originalPricePDV * ($discountPercentage / 100));// Dobij nabavnu cenu i procenat poreza$costPrice = $product->getPriceNabavna();$vatPercentage = $product->getVat();// Izračunaj minimalnu cenu (nabavna cena uvećana za 2% + porez)$minimumPrice = $costPrice * 1.02 * (1 + ($vatPercentage / 100));// Postavi novu cenu (ili minimalnu ako je umanjena cena manja)$finalPrice = ($discountedPrice < $minimumPrice) ? $minimumPrice : $discountedPrice;$percentageDecrease = (($originalPricePDV - $finalPrice) / $originalPricePDV) * 100;if (is_null($product->getPricePrikaz())) {$prikaz = ($product->getPrice() * (($product->getVat()/100) + 1));$product->setPricePrikaz($prikaz);}// Postavi vrednosti cena za proizvod$product->setPriceDiscount($finalPrice);$product->setPricePDV($finalPrice);$product->setPercentDiscount($percentageDecrease);$product->setSale($sale->getId());$em->getRepository(Product::class)->save($product);}} elseif (!$sale->getFactory()->isEmpty()) {$factories = [];foreach ($sale->getFactory() as $factory) {$factories[] = $factory->getId();}$qb = $this->createQueryBuilder('p');$qb->andWhere('p.factory IN (:factories)')->andWhere('p.pricePDV > 0')->andWhere('p.isSuspended = 0')->andWhere('p.isDiscount = 0')->setParameter('factories', $factories);$query = $qb->getQuery();$result = $query->getResult();foreach ($result as $product) {$product->setIsDiscount(true);if ($product->isOutOfStock()) {$product->setIsOutOfStock(false);$product->setIsOutOfStockPc(false);}$originalPricePDV = $product->getPricePDV();// Dobij procenat popusta$discountPercentage = $sale->getPercentDiscount();// Izračunaj novu cenu sa popustom$discountedPrice = $originalPricePDV - ($originalPricePDV * ($discountPercentage / 100));// Dobij nabavnu cenu i procenat poreza$costPrice = $product->getPriceNabavna();$vatPercentage = $product->getVat();// Izračunaj minimalnu cenu (nabavna cena uvećana za 2% + porez)$minimumPrice = $costPrice * 1.02 * (1 + ($vatPercentage / 100));// Postavi novu cenu (ili minimalnu ako je umanjena cena manja)$finalPrice = ($discountedPrice < $minimumPrice) ? $minimumPrice : $discountedPrice;$percentageDecrease = (($originalPricePDV - $finalPrice) / $originalPricePDV) * 100;if (is_null($product->getPricePrikaz())) {$prikaz = ($product->getPrice() * (($product->getVat()/100) + 1));$product->setPricePrikaz($prikaz);}// Postavi vrednosti cena za proizvod$product->setPriceDiscount($finalPrice);$product->setPricePDV($finalPrice);$product->setPercentDiscount($percentageDecrease);$product->setSale($sale->getId());$em->getRepository(Product::class)->save($product);}} else {foreach ($sale->getProduct() as $product) {if (!$product->isDiscount()) {$product->setIsDiscount(true);$product->setPricePDV($product->getPriceDiscount());if (is_null($product->getPricePrikaz())) {$prikaz = ($product->getPrice() * (($product->getVat()/100) + 1));$product->setPricePrikaz($prikaz);}if ($product->isOutOfStock()) {$product->setIsOutOfStock(false);$product->setIsOutOfStockPc(false);}$product->setSale($sale->getId());$em->getRepository(Product::class)->save($product);} else {$sale->removeProduct($product);}}}$sale->setIsActive(true);$em->getRepository(Sale::class)->save($sale);}public function stopSale(Sale $sale): void {$em = $this->getEntityManager();$db = $this->getEntityManager()->getConnection();$productsList = [];if (!$sale->getCategory()->isEmpty()) {$result = [];foreach ($sale->getCategory() as $category) {$qb = $this->createQueryBuilder('p');$categoryIds = array_merge([$category], $this->getEntityManager()->getRepository(Category::class)->findAllSubcategories($category->getId()));$qb->andWhere('p.category IN (:categoryIds)')->andWhere('p.pricePDV > 0')->andWhere('p.isSuspended = 0')->andWhere('p.isDiscount = 1')->setParameter('categoryIds', $categoryIds);$query = $qb->getQuery();$products = $query->getResult();$result = array_merge($result, $products);}$productsList = $result;}elseif (!$sale->getFactory()->isEmpty()) {$factories = [];foreach ($sale->getFactory() as $factory) {$factories[] = $factory->getId();}$qb = $this->createQueryBuilder('p');$qb->andWhere('p.factory IN (:factories)')->andWhere('p.pricePDV > 0')->andWhere('p.isSuspended = 0')->andWhere('p.isDiscount = 1')->setParameter('factories', $factories);$query = $qb->getQuery();$result = $query->getResult();$productsList = $result;}else {foreach ($sale->getProduct() as $product) {if ($product->isDiscount()) {$productsList[] = $product;}}}$artikliAkcija = [];foreach ($productsList as $product) {$artikliAkcija[] = ['id' => $product->getId(),'title' => $product->getTitle(),'akcija' => $product->getPricePDV(),'prikaz' => $product->getPricePrikaz(),'prava' => $product->getPrice() * 1.2,'popust' => $product->getPercentDiscount()];}$archive = ['id' => $sale->getId(),'title' => $sale->getTitle(),'percent' => $sale->getPercentDiscount(),'delivery' => $sale->getDelivery(),'start' => $sale->getStart()->format('d.m.Y'),'stop' => $sale->getStop()->format('d.m.Y'),'artikli' => $artikliAkcija];foreach ($productsList as $product) {if ($product->isDiscount()) {//ako su proizvodi iz radnje povuci ih posle akcijeif ($product->getVendor()->getId() == 11) {$product->setIsSuspended(true);}$product->setIsDiscount(false);$product->setPriceDiscount(null);$product->setPercentDiscount(null);$product->setPricePrikaz(null);$product->setPricePDV($product->getPrice() * (1 + ($product->getVat() / 100)));$product->setSale(null);$em->getRepository(Product::class)->save($product);}}$sale->setIsActive(false);$sale->setArchive($archive);$em->getRepository(Sale::class)->save($sale);}public function startSuperSale(Supersale $sale): void {$em = $this->getEntityManager();$ss = $em->getRepository(Product::class)->findOneBy(['isSuperProduct' => true]);if (is_null($ss)) {$product = $sale->getProduct();if (!$product->isDiscount()) {$product->setIsDiscount(true);$product->setIsSuperProduct(true);$product->setPricePDV($sale->getPricePDV());$product->setPricePrikaz($sale->getPricePrikaz());$product->setPercentDiscount($sale->getPercentDiscount());if ($product->isOutOfStock()) {$product->setIsOutOfStock(false);$product->setIsOutOfStockPc(false);}$em->getRepository(Product::class)->save($product);}$sale->setIsActive(true);$em->getRepository(Supersale::class)->save($sale);}}public function stopSuperSale(Supersale $sale): void {$em = $this->getEntityManager();$product = $sale->getProduct();if ($product->isSuperProduct()) {//ako su proizvodi iz radnje povuci ih posle akcijeif ($product->getVendor()->getId() == 11) {$product->setIsSuspended(true);}$product->setIsDiscount(false);$product->setIsSuperProduct(false);$product->setPricePrikaz(null);$product->setPricePDV($product->getPrice() * (1 + ($product->getVat() / 100)));$product->setPercentDiscount(null);$em->getRepository(Product::class)->save($product);}$sale->setIsActive(false);$em->getRepository(Supersale::class)->save($sale);}public function removeNewLabel() {$em = $this->getEntityManager();$db = $this->getEntityManager()->getConnection();$sql = "UPDATE " . $em->getClassMetadata(Product::class)->getTableName() . "SET is_new = 0WHERE DATEDIFF(NOW(), created) > 30AND is_suspended = 0AND is_new = 1 ";$result = $db->prepare($sql);$result->execute();}}