Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

253 Zeilen
9.0KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Repository\Product;
  3. use Lc\CaracoleBundle\Model\Product\ProductCategoryInterface;
  4. use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
  5. use Lc\CaracoleBundle\Model\Reduction\ReductionCatalogInterface;
  6. use Lc\CaracoleBundle\Repository\SectionStoreTrait;
  7. use Lc\CaracoleBundle\Solver\Price\PriceSolver;
  8. use Lc\SovBundle\Model\User\UserInterface;
  9. use Lc\SovBundle\Repository\AbstractStore;
  10. use Lc\SovBundle\Repository\RepositoryQueryInterface;
  11. class ProductFamilyStore extends AbstractStore
  12. {
  13. use SectionStoreTrait;
  14. protected ProductFamilyRepositoryQuery $query;
  15. protected PriceSolver $priceSolver;
  16. public function __construct(ProductFamilyRepositoryQuery $query, PriceSolver $priceSolver)
  17. {
  18. $this->query = $query;
  19. $this->priceSolver = $priceSolver;
  20. }
  21. public function orderByDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
  22. {
  23. $query->orderBy('position');
  24. return $query;
  25. }
  26. public function filtersDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
  27. {
  28. $query->filterBySection($this->section);
  29. return $query;
  30. }
  31. public function relationsDefault($query): RepositoryQueryInterface
  32. {
  33. $query->joinProductCategories();
  34. $query->joinProducts();
  35. return $query;
  36. }
  37. // getProductFamiliesByCategory
  38. public function getByCategory(ProductCategoryInterface $productCategory, $query = null)
  39. {
  40. $query = $this->createDefaultQuery($query);
  41. $query
  42. ->filterIsOnline()
  43. ->filterByProductCategory($productCategory);
  44. return $query->find();
  45. }
  46. // getProductFamiliesNovelties
  47. public function getNovelty($user = null, $organizeByParentCategory = true, $query = null)
  48. {
  49. $query = $this->createDefaultQuery($query);
  50. $query
  51. ->filterByPropertyNoveltyExpirationDate()
  52. ->filterIsOnline();
  53. $results = $query->find();
  54. return $this->getWithReductions($results, $user, false, $organizeByParentCategory);
  55. }
  56. // getProductFamiliesOrganics
  57. public function getOrganic($user = null, $organizeByParentCategory = true, $query = null)
  58. {
  59. $query = $this->createDefaultQuery($query);
  60. $query
  61. ->filterIsOrganicLabel()
  62. ->filterIsOnline();
  63. $results = $query->find();
  64. return $this->getWithReductions($results, $user, false, $organizeByParentCategory);
  65. }
  66. // getProductFamiliesOnDiscount
  67. public function getDiscount($user = null, $organizeByParentCategory = true, $query = null)
  68. {
  69. return $this->getWithReductions($this->getOnline($query), $user, false, $organizeByParentCategory);
  70. }
  71. // getProductFamiliesFavorites
  72. public function getFavorite($user = null, $organizeByParentCategory = true, $query = null)
  73. {
  74. if ($user) {
  75. return $this->getWithReductions($user->getFavoriteProductFamilies(), $user, false, $organizeByParentCategory);
  76. }
  77. return [];
  78. }
  79. // findByTerms
  80. public function getByTerms($terms, $maxResults = false, $organizeByParentCategory = false, $user = null, $query = null)
  81. {
  82. $query = $this->createDefaultQuery($query);
  83. $query->filterIsOnline();
  84. $query->filterByTerms($terms);
  85. $query->groupBy('id');
  86. if($maxResults) {
  87. $query->limit($maxResults);
  88. }
  89. $results = $query->find();
  90. return $this->getWithReductions($results, $user, false, $organizeByParentCategory);
  91. }
  92. public function getBestReductionCatalog(
  93. ProductFamilyInterface $productFamily,
  94. ReductionCatalogInterface $reductionCatalog1,
  95. ReductionCatalogInterface $reductionCatalog2
  96. ) {
  97. $price1 = $this->priceSolver->applyReductionCatalog(
  98. $productFamily,
  99. $this->priceSolver->getPrice($productFamily),
  100. $this->priceSolver->getPriceWithTax($productFamily),
  101. 1,
  102. $reductionCatalog1
  103. );
  104. $price2 = $this->priceSolver->applyReductionCatalog(
  105. $productFamily,
  106. $this->priceSolver->getPrice($productFamily),
  107. $this->priceSolver->getPriceWithTax($productFamily),
  108. 1,
  109. $reductionCatalog2
  110. );
  111. if ($price1 > $price2) {
  112. return $reductionCatalog2;
  113. } else {
  114. return $reductionCatalog1;
  115. }
  116. }
  117. // setReductionForProductFamilies
  118. public function getWithReductions(
  119. $productFamilies,
  120. UserInterface $user = null,
  121. $organizeByCategory = false,
  122. $organizeByParentCategory = false,
  123. $onlyOnDiscount = false
  124. ) {
  125. $conditions = [
  126. 'ids' => [],
  127. 'categories' => [],
  128. ];
  129. foreach ($productFamilies as $productFamily) {
  130. $conditions['ids'][] = $productFamily->getId();
  131. $conditions['categories'] = array_merge(
  132. $conditions['categories'],
  133. $productFamily->getProductCategories()->toArray()
  134. );
  135. }
  136. if ($productFamilies) {
  137. $reductionCatalogs = $this->reductionCatalogStore->getByProductFamiliesConditions(
  138. $conditions,
  139. $user
  140. );
  141. }
  142. $productFamiliesToReturn = array();
  143. foreach ($productFamilies as $productFamily) {
  144. foreach ($reductionCatalogs as $reductionCatalog) {
  145. $conditionProductFamilies = $conditionProductFamily = $conditionProductCategory = false;
  146. if ($reductionCatalog->getProductFamilies()->contains(
  147. $productFamily
  148. ) || $reductionCatalog->getProductFamilies()->isEmpty()) {
  149. $conditionProductFamilies = true;
  150. }
  151. if ($reductionCatalog->getProductFamily() == $productFamily || $reductionCatalog->getProductFamily(
  152. ) === null) {
  153. $conditionProductFamily = true;
  154. }
  155. foreach ($productFamily->getProductCategories() as $productCategory) {
  156. if ($reductionCatalog->getProductCategories()->contains(
  157. $productCategory
  158. ) || $reductionCatalog->getProductCategories()->isEmpty()) {
  159. $conditionProductCategory = true;
  160. }
  161. }
  162. if ($conditionProductFamilies && $conditionProductFamily && $conditionProductCategory) {
  163. if ($productFamily->getReductionCatalog()) {
  164. $productFamily->setReductionCatalog(
  165. $this->getBestReductionCatalog(
  166. $productFamily,
  167. $reductionCatalog,
  168. $productFamily->getReductionCatalog()
  169. )
  170. );
  171. } else {
  172. $productFamily->setReductionCatalog($reductionCatalog);
  173. }
  174. }
  175. }
  176. if (($onlyOnDiscount && $productFamily->getReductionCatalog()) || !$onlyOnDiscount) {
  177. if ($organizeByParentCategory) {
  178. $productCategories = $productFamily->getProductCategories();
  179. if ($productCategories && count($productCategories) > 0) {
  180. $parentCategory = $productCategories[0]->getParentCategory();
  181. if ($this->productCategorySolver->isDisplay($parentCategory, $user)) {
  182. if (!isset($productFamiliesToReturn[$parentCategory->getId()])) {
  183. $productFamiliesToReturn[$parentCategory->getId()] = [
  184. 'category' => $parentCategory,
  185. 'products' => []
  186. ];
  187. }
  188. $productFamiliesToReturn[$parentCategory->getId()]['products'][] = $productFamily;
  189. }
  190. }
  191. } elseif ($organizeByCategory) {
  192. foreach ($productFamily->getProductCategories() as $productCategory) {
  193. if ($this->isDisplay($productFamily)) {
  194. $productFamiliesToReturn[$productCategory->getId()][] = $productFamily;
  195. }
  196. }
  197. } else {
  198. if ($this->isDisplay($productFamily)) {
  199. $productFamiliesToReturn[] = $productFamily;
  200. }
  201. }
  202. }
  203. }
  204. if ($organizeByParentCategory) {
  205. uasort($productFamiliesToReturn, array($this, 'compMethodSortProductFamiliesByParentCategory'));
  206. }
  207. return $productFamiliesToReturn;
  208. }
  209. }