|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- <?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;
-
- class ProductFamilyStore extends AbstractStore
- {
- use SectionStoreTrait;
- use MerchantStoreTrait;
-
- protected ProductFamilyRepositoryQuery $query;
- protected PriceSolver $priceSolver;
-
- public function __construct(ProductFamilyRepositoryQuery $query, PriceSolver $priceSolver)
- {
- $this->query = $query;
- $this->priceSolver = $priceSolver;
- }
-
- public function orderByDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
- {
- $query->orderBy('position');
- return $query;
- }
-
- public function filtersDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
- {
-
- $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, $query);
- foreach ($productFamiliesCategory as $productFamily) {
- $productFamiliesArray[$productFamily->getId()] = $productFamily;
- }
- }
-
- return $this->getWithReductions($productFamiliesArray, $user, true);
- }
-
- // getProductFamiliesByCategory
- public function getByCategory(
- ProductCategoryInterface $productCategory,
- $query = null
- ) {
- $query = $this->createDefaultQuery($query);
-
- $query
- ->filterIsOnline()
- ->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);
- }
-
- // 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);
- }
-
- // 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->applyReductionCatalog(
- $productFamily,
- $this->priceSolver->getPrice($productFamily),
- $this->priceSolver->getPriceWithTax($productFamily),
- 1,
- $reductionCatalog1
- );
-
- $price2 = $this->priceSolver->applyReductionCatalog(
- $productFamily,
- $this->priceSolver->getPrice($productFamily),
- $this->priceSolver->getPriceWithTax($productFamily),
- 1,
- $reductionCatalog2
- );
-
- if ($price1 > $price2) {
- return $reductionCatalog2;
- } else {
- return $reductionCatalog1;
- }
- }
-
- // setReductionForProductFamilies
- public function getWithReductions(
- $productFamilies,
- UserInterface $user = null,
- $organizeByCategory = false,
- $organizeByParentCategory = false,
- $onlyOnDiscount = false
- ) {
- $conditions = [
- 'ids' => [],
- 'categories' => [],
- ];
- foreach ($productFamilies as $productFamily) {
- $conditions['ids'][] = $productFamily->getId();
- $conditions['categories'] = array_merge(
- $conditions['categories'],
- $productFamily->getProductCategories()->toArray()
- );
- }
-
- if ($productFamilies) {
- $reductionCatalogs = $this->reductionCatalogStore->getByProductFamiliesConditions(
- $conditions,
- $user
- );
- }
-
- $productFamiliesToReturn = array();
- foreach ($productFamilies as $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;
- }
-
- }
|