|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- <?php
-
- namespace Lc\CaracoleBundle\Repository\Product;
-
- use Lc\CaracoleBundle\Model\Product\ProductCategoryInterface;
- use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
- use Lc\CaracoleBundle\Model\Reduction\ReductionCatalogInterface;
- use Lc\CaracoleBundle\Repository\MerchantStoreTrait;
- use Lc\CaracoleBundle\Repository\SectionStoreTrait;
- use Lc\CaracoleBundle\Solver\Price\PriceSolver;
- use Lc\SovBundle\Model\User\UserInterface;
- use Lc\CaracoleBundle\Repository\AbstractStore;
- use Lc\SovBundle\Repository\RepositoryQueryInterface;
- use Symfony\Contracts\Cache\CacheInterface;
- use Symfony\Contracts\Cache\ItemInterface;
-
- class ProductFamilyStore extends AbstractStore
- {
- use SectionStoreTrait;
- use MerchantStoreTrait;
-
- protected ProductFamilyRepositoryQuery $query;
- protected PriceSolver $priceSolver;
- protected CacheInterface $cache;
-
- public function __construct(
- ProductFamilyRepositoryQuery $query,
- PriceSolver $priceSolver,
- CacheInterface $cache
- ) {
- $this->query = $query;
- $this->priceSolver = $priceSolver;
- $this->cache = $cache;
- }
-
- public function orderByDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
- {
- $query->orderBy('position');
- return $query;
- }
-
- public function filtersDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
- {
- $query->filterIsOnlineAndOffline();
- $this->addFilterBySectionOptionnal($query);
- $this->addFilterByMerchantViaSectionOptionnal($query);
-
- return $query;
- }
-
- public function relationsDefault($query): RepositoryQueryInterface
- {
- $query->joinProductCategories();
- $query->joinProducts();
- $query->joinQualityLabels();
-
- return $query;
- }
-
- public function getByParentCategory(
- ProductCategoryInterface $parentCategory,
- $user = null,
- $query = null
- ) {
- $productFamiliesArray = [];
-
- foreach ($parentCategory->getChildrens() as $i => $category) {
- $productFamiliesCategory = $this->getByCategory($category);
- foreach ($productFamiliesCategory as $productFamily) {
- $productFamiliesArray[$productFamily->getId()] = $productFamily;
- }
- uasort($productFamiliesArray, array($this, 'compMethodSortProductFamiliesByPosition'));
- }
-
- return $this->getWithReductions($productFamiliesArray, $user, true);
- }
-
- private function compMethodSortProductFamiliesByPosition($a, $b)
- {
- return $a->getPosition() > $b->getPosition();
- }
-
- // getProductFamiliesByCategory
- public function getByCategory(
- ProductCategoryInterface $productCategory,
- $query = null
- ) {
- $query = $this->createDefaultQuery($query);
-
- $query
- ->filterIsOnline()
- ->filterByProductCategory($productCategory);
-
- return $query->find();
- }
-
- //TODO ajouter status en donné de contexte pour éviter ce truc
- public function getByCategoryOnlineAndOffline(
- ProductCategoryInterface $productCategory,
- $query = null
- ) {
- $query = $this->createDefaultQuery($query);
-
- $query
- ->filterByProductCategory($productCategory);
-
- return $query->find();
- }
-
- // getProductFamiliesNovelties
- public function getNovelty($user = null, $organizeByParentCategory = true, $query = null)
- {
- $query = $this->createDefaultQuery($query);
-
- $query
- ->filterByPropertyNoveltyExpirationDate()
- ->filterIsOnline();
-
- $results = $query->find();
-
- return $this->getWithReductions($results, $user, false, $organizeByParentCategory);
- }
-
- public function getCachedNovelty($user = null, $organizeByParentCategory = true, $query = null): array
- {
- $productFamilyStore = $this;
- return $this->cache->get($this->getCacheKeyNovelty(), function (ItemInterface $item) use ($productFamilyStore, $user, $organizeByParentCategory, $query) {
- $item->expiresAfter(3600);
- return $productFamilyStore->getNovelty($user, $organizeByParentCategory, $query);
- });
- }
-
- public function deleteCacheNovelty(): void
- {
- $this->cache->delete($this->getCacheKeyNovelty());
- }
-
- public function getCacheKeyNovelty()
- {
- return 'products_novelty_'.$this->section->getId();
- }
-
- // getProductFamiliesOrganics
- public function getOrganic($user = null, $organizeByParentCategory = true, $query = null)
- {
- $query = $this->createDefaultQuery($query);
- $query
- ->filterIsOrganicLabel()
- ->filterIsOnline();
- $results = $query->find();
-
- return $this->getWithReductions($results, $user, false, $organizeByParentCategory);
- }
-
- // getProductFamiliesOnDiscount
- public function getDiscount($user = null, $organizeByParentCategory = true, $query = null)
- {
- return $this->getWithReductions($this->getOnline($query), $user, false, $organizeByParentCategory, true);
- }
-
- public function getCachedDiscount($user = null, $organizeByParentCategory = true, $query = null): array
- {
- $productFamilyStore = $this;
-
- return $this->cache->get($this->getCacheKeyDiscount(), function (ItemInterface $item) use ($productFamilyStore, $user, $organizeByParentCategory, $query) {
- $item->expiresAfter(3600);
- return $productFamilyStore->getDiscount($user, $organizeByParentCategory, $query);
- });
- }
-
- public function deleteCacheDiscount(): void
- {
- $this->cache->delete($this->getCacheKeyDiscount());
- }
-
- public function getCacheKeyDiscount()
- {
- return 'products_discount_'.$this->section->getId();
- }
-
- public function loadAllDatas(ProductFamilyInterface $productFamily)
- {
- $productFamily->getProductFamilySectionProperties()->toArray();
- }
-
- // getProductFamiliesFavorites
- public function getFavorite($user = null, $organizeByParentCategory = true, $query = null)
- {
- if ($user) {
- return $this->getWithReductions(
- $user->getFavoriteProductFamilies(),
- $user,
- false,
- $organizeByParentCategory
- );
- }
-
- return [];
- }
-
- // findByTerms
- public function getByTerms(
- $terms,
- $maxResults = false,
- $organizeByParentCategory = false,
- $user = null,
- $query = null
- ) {
- $query = $this->createDefaultQuery($query);
-
- $query->filterIsOnline();
- $query->filterByTerms($terms);
-
- $query->groupBy('id');
-
- if ($maxResults) {
- $query->limit($maxResults);
- }
-
- $results = $query->find();
-
- return $this->getWithReductions($results, $user, false, $organizeByParentCategory);
- }
-
- public function getBestReductionCatalog(
- ProductFamilyInterface $productFamily,
- ReductionCatalogInterface $reductionCatalog1,
- ReductionCatalogInterface $reductionCatalog2
- ) {
- $price1 = $this->priceSolver->getPriceWithTaxByReduction(
- $productFamily,
- $reductionCatalog1
- );
- $price2 = $this->priceSolver->getPriceWithTaxByReduction(
- $productFamily,
- $reductionCatalog2
- );
-
-
- if ($price1 > $price2) {
- return $reductionCatalog2;
- } else {
- return $reductionCatalog1;
- }
- }
-
- // setReductionForProductFamilies
- public function getWithReductions(
- $productFamilies,
- UserInterface $user = null,
- $organizeByCategory = false,
- $organizeByParentCategory = false,
- $onlyOnDiscount = false
- ) {
- $conditions = [
- 'productFamiliesIds' => [],
- 'categories' => [],
- 'productFamilies' => [],
- ];
-
- foreach ($productFamilies as $productFamily) {
- $conditions['productFamiliesIds'][] = $productFamily->getId();
- $conditions['productFamilies'][] = $productFamily;
- $conditions['categories'] = array_merge(
- $conditions['categories'],
- $productFamily->getProductCategories()->toArray()
- );
- }
-
- if ($productFamilies) {
- $reductionCatalogs = $this->reductionCatalogStore->getByProductFamiliesConditions(
- $conditions,
- $user
- );
- }
-
- $productFamiliesToReturn = array();
- foreach ($productFamilies as $productFamily) {
-
- // Cache : chargement de toutes les données manquantes (fetch eager ne fonctionne pas)
- $this->loadAllDatas($productFamily);
-
- foreach ($reductionCatalogs as $reductionCatalog) {
- $conditionProductFamilies = $conditionProductFamily = $conditionProductCategory = false;
-
- if ($reductionCatalog->getProductFamilies()->contains(
- $productFamily
- ) || $reductionCatalog->getProductFamilies()->isEmpty()) {
- $conditionProductFamilies = true;
- }
-
- if ($reductionCatalog->getProductFamily() == $productFamily || $reductionCatalog->getProductFamily(
- ) === null) {
- $conditionProductFamily = true;
- }
-
- foreach ($productFamily->getProductCategories() as $productCategory) {
- if ($reductionCatalog->getProductCategories()->contains(
- $productCategory
- ) || $reductionCatalog->getProductCategories()->isEmpty()) {
- $conditionProductCategory = true;
- }
- }
-
- if ($conditionProductFamilies && $conditionProductFamily && $conditionProductCategory) {
- if ($productFamily->getReductionCatalog()) {
- $productFamily->setReductionCatalog(
- $this->getBestReductionCatalog(
- $productFamily,
- $reductionCatalog,
- $productFamily->getReductionCatalog()
- )
- );
- } else {
- $productFamily->setReductionCatalog($reductionCatalog);
- }
- }
- }
-
- if (($onlyOnDiscount && $productFamily->getReductionCatalog()) || !$onlyOnDiscount) {
- if ($organizeByParentCategory) {
- $productCategories = $productFamily->getProductCategories();
-
- if ($productCategories && count($productCategories) > 0) {
- $parentCategory = $productCategories[0]->getParentCategory();
- if ($this->productCategorySolver->isDisplay($parentCategory, $user)) {
- if (!isset($productFamiliesToReturn[$parentCategory->getId()])) {
- $productFamiliesToReturn[$parentCategory->getId()] = [
- 'category' => $parentCategory,
- 'products' => []
- ];
- }
- $productFamiliesToReturn[$parentCategory->getId()]['products'][] = $productFamily;
- }
- }
- } elseif ($organizeByCategory) {
- foreach ($productFamily->getProductCategories() as $productCategory) {
- if ($this->isDisplay($productFamily)) {
- $productFamiliesToReturn[$productCategory->getId()][] = $productFamily;
- }
- }
- } else {
- if ($this->isDisplay($productFamily)) {
- $productFamiliesToReturn[] = $productFamily;
- }
- }
- }
- }
-
- if ($organizeByParentCategory) {
- uasort($productFamiliesToReturn, array($this, 'compMethodSortProductFamiliesByParentCategory'));
- }
-
- return $productFamiliesToReturn;
- }
-
- }
|