No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

281 líneas
9.7KB

  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. public function getByParentCategory(
  38. ProductCategoryInterface $parentCategory,
  39. $user = null,
  40. $query = null
  41. ) {
  42. $productFamiliesArray = [];
  43. foreach ($parentCategory->getChildrens() as $i => $category) {
  44. $productFamiliesCategory = $this->getByCategory($category, $query);
  45. foreach ($productFamiliesCategory as $productFamily) {
  46. $productFamiliesArray[$productFamily->getId()] = $productFamily;
  47. }
  48. }
  49. return $this->getWithReductions($productFamiliesArray, $user, true);
  50. }
  51. // getProductFamiliesByCategory
  52. public function getByCategory(
  53. ProductCategoryInterface $productCategory,
  54. $query = null
  55. ) {
  56. $query = $this->createDefaultQuery($query);
  57. $query
  58. ->filterIsOnline()
  59. ->filterByProductCategory($productCategory);
  60. return $query->find();
  61. }
  62. // getProductFamiliesNovelties
  63. public function getNovelty($user = null, $organizeByParentCategory = true, $query = null)
  64. {
  65. $query = $this->createDefaultQuery($query);
  66. $query
  67. ->filterByPropertyNoveltyExpirationDate()
  68. ->filterIsOnline();
  69. $results = $query->find();
  70. return $this->getWithReductions($results, $user, false, $organizeByParentCategory);
  71. }
  72. // getProductFamiliesOrganics
  73. public function getOrganic($user = null, $organizeByParentCategory = true, $query = null)
  74. {
  75. $query = $this->createDefaultQuery($query);
  76. $query
  77. ->filterIsOrganicLabel()
  78. ->filterIsOnline();
  79. $results = $query->find();
  80. return $this->getWithReductions($results, $user, false, $organizeByParentCategory);
  81. }
  82. // getProductFamiliesOnDiscount
  83. public function getDiscount($user = null, $organizeByParentCategory = true, $query = null)
  84. {
  85. return $this->getWithReductions($this->getOnline($query), $user, false, $organizeByParentCategory, true);
  86. }
  87. // getProductFamiliesFavorites
  88. public function getFavorite($user = null, $organizeByParentCategory = true, $query = null)
  89. {
  90. if ($user) {
  91. return $this->getWithReductions(
  92. $user->getFavoriteProductFamilies(),
  93. $user,
  94. false,
  95. $organizeByParentCategory
  96. );
  97. }
  98. return [];
  99. }
  100. // findByTerms
  101. public function getByTerms(
  102. $terms,
  103. $maxResults = false,
  104. $organizeByParentCategory = false,
  105. $user = null,
  106. $query = null
  107. ) {
  108. $query = $this->createDefaultQuery($query);
  109. $query->filterIsOnline();
  110. $query->filterByTerms($terms);
  111. $query->groupBy('id');
  112. if ($maxResults) {
  113. $query->limit($maxResults);
  114. }
  115. $results = $query->find();
  116. return $this->getWithReductions($results, $user, false, $organizeByParentCategory);
  117. }
  118. public function getBestReductionCatalog(
  119. ProductFamilyInterface $productFamily,
  120. ReductionCatalogInterface $reductionCatalog1,
  121. ReductionCatalogInterface $reductionCatalog2
  122. ) {
  123. $price1 = $this->priceSolver->applyReductionCatalog(
  124. $productFamily,
  125. $this->priceSolver->getPrice($productFamily),
  126. $this->priceSolver->getPriceWithTax($productFamily),
  127. 1,
  128. $reductionCatalog1
  129. );
  130. $price2 = $this->priceSolver->applyReductionCatalog(
  131. $productFamily,
  132. $this->priceSolver->getPrice($productFamily),
  133. $this->priceSolver->getPriceWithTax($productFamily),
  134. 1,
  135. $reductionCatalog2
  136. );
  137. if ($price1 > $price2) {
  138. return $reductionCatalog2;
  139. } else {
  140. return $reductionCatalog1;
  141. }
  142. }
  143. // setReductionForProductFamilies
  144. public function getWithReductions(
  145. $productFamilies,
  146. UserInterface $user = null,
  147. $organizeByCategory = false,
  148. $organizeByParentCategory = false,
  149. $onlyOnDiscount = false
  150. ) {
  151. $conditions = [
  152. 'ids' => [],
  153. 'categories' => [],
  154. ];
  155. foreach ($productFamilies as $productFamily) {
  156. $conditions['ids'][] = $productFamily->getId();
  157. $conditions['categories'] = array_merge(
  158. $conditions['categories'],
  159. $productFamily->getProductCategories()->toArray()
  160. );
  161. }
  162. if ($productFamilies) {
  163. $reductionCatalogs = $this->reductionCatalogStore->getByProductFamiliesConditions(
  164. $conditions,
  165. $user
  166. );
  167. }
  168. $productFamiliesToReturn = array();
  169. foreach ($productFamilies as $productFamily) {
  170. foreach ($reductionCatalogs as $reductionCatalog) {
  171. $conditionProductFamilies = $conditionProductFamily = $conditionProductCategory = false;
  172. if ($reductionCatalog->getProductFamilies()->contains(
  173. $productFamily
  174. ) || $reductionCatalog->getProductFamilies()->isEmpty()) {
  175. $conditionProductFamilies = true;
  176. }
  177. if ($reductionCatalog->getProductFamily() == $productFamily || $reductionCatalog->getProductFamily(
  178. ) === null) {
  179. $conditionProductFamily = true;
  180. }
  181. foreach ($productFamily->getProductCategories() as $productCategory) {
  182. if ($reductionCatalog->getProductCategories()->contains(
  183. $productCategory
  184. ) || $reductionCatalog->getProductCategories()->isEmpty()) {
  185. $conditionProductCategory = true;
  186. }
  187. }
  188. if ($conditionProductFamilies && $conditionProductFamily && $conditionProductCategory) {
  189. if ($productFamily->getReductionCatalog()) {
  190. $productFamily->setReductionCatalog(
  191. $this->getBestReductionCatalog(
  192. $productFamily,
  193. $reductionCatalog,
  194. $productFamily->getReductionCatalog()
  195. )
  196. );
  197. } else {
  198. $productFamily->setReductionCatalog($reductionCatalog);
  199. }
  200. }
  201. }
  202. if (($onlyOnDiscount && $productFamily->getReductionCatalog()) || !$onlyOnDiscount) {
  203. if ($organizeByParentCategory) {
  204. $productCategories = $productFamily->getProductCategories();
  205. if ($productCategories && count($productCategories) > 0) {
  206. $parentCategory = $productCategories[0]->getParentCategory();
  207. if ($this->productCategorySolver->isDisplay($parentCategory, $user)) {
  208. if (!isset($productFamiliesToReturn[$parentCategory->getId()])) {
  209. $productFamiliesToReturn[$parentCategory->getId()] = [
  210. 'category' => $parentCategory,
  211. 'products' => []
  212. ];
  213. }
  214. $productFamiliesToReturn[$parentCategory->getId()]['products'][] = $productFamily;
  215. }
  216. }
  217. } elseif ($organizeByCategory) {
  218. foreach ($productFamily->getProductCategories() as $productCategory) {
  219. if ($this->isDisplay($productFamily)) {
  220. $productFamiliesToReturn[$productCategory->getId()][] = $productFamily;
  221. }
  222. }
  223. } else {
  224. if ($this->isDisplay($productFamily)) {
  225. $productFamiliesToReturn[] = $productFamily;
  226. }
  227. }
  228. }
  229. }
  230. if ($organizeByParentCategory) {
  231. uasort($productFamiliesToReturn, array($this, 'compMethodSortProductFamiliesByParentCategory'));
  232. }
  233. return $productFamiliesToReturn;
  234. }
  235. }