You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

489 lines
16KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Solver\Product;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Lc\CaracoleBundle\Doctrine\Extension\ProductPropertyInterface;
  6. use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
  7. use Lc\CaracoleBundle\Model\Product\ProductFamilyModel;
  8. use Lc\CaracoleBundle\Model\Product\ProductFamilySectionPropertyInterface;
  9. use Lc\CaracoleBundle\Model\Product\ProductInterface;
  10. use Lc\CaracoleBundle\Model\Reduction\ReductionCatalogInterface;
  11. use Lc\CaracoleBundle\Model\Section\SectionInterface;
  12. class ProductFamilySolver
  13. {
  14. protected ProductSolver $productSolver;
  15. protected ProductFamilySectionPropertySolver $productFamilySectionPropertySolver;
  16. protected ProductCategorySolver $productCategorySolver;
  17. public function __construct(ProductSolver $productSolver, ProductFamilySectionPropertySolver $productFamilySectionPropertySolver, ProductCategorySolver $productCategorySolver)
  18. {
  19. $this->productSolver = $productSolver;
  20. $this->productFamilySectionPropertySolver = $productFamilySectionPropertySolver;
  21. $this->productCategorySolver = $productCategorySolver;
  22. }
  23. public function getBehaviorCountStockChoices(): array
  24. {
  25. return [
  26. ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE,
  27. ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT,
  28. ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY,
  29. ];
  30. }
  31. public function getWaringMessageTypeChoices(): array
  32. {
  33. return [
  34. ProductFamilyModel::WARNING_MESSAGE_TYPE_ERROR,
  35. ProductFamilyModel::WARNING_MESSAGE_TYPE_INFO,
  36. ProductFamilyModel::WARNING_MESSAGE_TYPE_SUCCESS,
  37. ProductFamilyModel::WARNING_MESSAGE_TYPE_WARNING,
  38. ];
  39. }
  40. public function getBehaviorAddToCartChoices(): array
  41. {
  42. return [
  43. ProductFamilyModel::BEHAVIOR_ADD_TO_CART_MULTIPLE,
  44. ProductFamilyModel::BEHAVIOR_ADD_TO_CART_SIMPLE,
  45. ];
  46. }
  47. public function getBehaviorPriceChoices(): array
  48. {
  49. return [
  50. ProductFamilyModel::BEHAVIOR_PRICE_BY_PIECE,
  51. ProductFamilyModel::BEHAVIOR_PRICE_BY_REFERENCE_UNIT,
  52. ];
  53. }
  54. public function getTypeExpirationDateChoices(): array
  55. {
  56. return [
  57. ProductFamilyModel::TYPE_EXPIRATION_DATE_DLC,
  58. ProductFamilyModel::TYPE_EXPIRATION_DATE_DDM,
  59. ProductFamilyModel::TYPE_EXPIRATION_DATE_DLUO,
  60. ];
  61. }
  62. public function getBehaviorExpirationDateChoices(): array
  63. {
  64. return [
  65. ProductFamilyModel::BEHAVIOR_EXPIRATION_DATE_BY_PRODUCT_FAMILY,
  66. ProductFamilyModel::BEHAVIOR_EXPIRATION_DATE_BY_PRODUCT,
  67. ];
  68. }
  69. public function getBehaviorDisplaySaleChoices(): array
  70. {
  71. return [
  72. ProductFamilyModel::BEHAVIOR_DISPLAY_SALE_BY_MEASURE,
  73. ProductFamilyModel::BEHAVIOR_DISPLAY_SALE_BY_QUANTITY,
  74. ];
  75. }
  76. public function countProductFamiliesOrganizedByParentCategory(array $categories): int
  77. {
  78. $count = 0;
  79. foreach ($categories as $category) {
  80. $count += count($category['products']);
  81. }
  82. return $count;
  83. }
  84. public function getAvailableQuantityInherited(ProductFamilyInterface $productFamily)
  85. {
  86. $availableQuantity = 0;
  87. switch ($productFamily->getBehaviorCountStock()) {
  88. case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE :
  89. case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY :
  90. $availableQuantity = $productFamily->getAvailableQuantity();
  91. break;
  92. case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT :
  93. foreach ($this->getProductsOnline($productFamily) as $product) {
  94. $availableQuantity += $this->productSolver->getAvailableQuantityInherited($product);
  95. }
  96. break;
  97. }
  98. return $availableQuantity;
  99. }
  100. public function getAvailableQuantitySupplierInherited(ProductFamilyInterface $productFamily)
  101. {
  102. $availableQuantity = 0;
  103. switch ($productFamily->getBehaviorCountStock()) {
  104. case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE :
  105. case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY :
  106. $availableQuantity = $productFamily->getAvailableQuantitySupplier();
  107. break;
  108. case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT :
  109. foreach ($this->getProductsOnline($productFamily) as $product) {
  110. $availableQuantity += $this->productSolver->getAvailableQuantitySupplierInherited($product);
  111. }
  112. break;
  113. }
  114. return $availableQuantity;
  115. }
  116. public function getTaxRateInherited(ProductPropertyInterface $productFamily)
  117. {
  118. if ($productFamily instanceof ProductInterface) {
  119. $productFamily = $productFamily->getProductFamily();
  120. }
  121. if ($productFamily->getTaxRate()) {
  122. return $productFamily->getTaxRate();
  123. } else {
  124. return $productFamily->getProductFamilySectionProperties()[0]->getSection()->getMerchant()->getTaxRate();
  125. }
  126. }
  127. public function getProductsOnline(ProductFamilyInterface $productFamily): Collection
  128. {
  129. $products = $productFamily->getProducts();
  130. $productsOnlineArray = new ArrayCollection();
  131. foreach ($products as $product) {
  132. if ($product->getStatus() == 1 && $product->getOriginProduct() != true) {
  133. $productsOnlineArray[] = $product;
  134. }
  135. }
  136. return $productsOnlineArray;
  137. }
  138. public function getReductionCatalogInherited(ProductFamilyInterface $productFamily): ?ReductionCatalogInterface
  139. {
  140. return $productFamily->getReductionCatalog();
  141. }
  142. public function getProductCategoryParent(ProductFamilyInterface $productFamily)
  143. {
  144. $productCategories = $productFamily->getProductCategories();
  145. if (count($productCategories) > 0) {
  146. return $productCategories[0]->getParent();
  147. }
  148. return false;
  149. }
  150. public function getProductCategoryChild(ProductFamilyInterface $productFamily)
  151. {
  152. $productCategories = $productFamily->getProductCategories();
  153. foreach ($productCategories as $productCategory) {
  154. if ($productCategory->getParent()) {
  155. return $productCategory;
  156. }
  157. }
  158. return false;
  159. }
  160. public function isPropertyNoveltyOnline(ProductFamilyInterface $productFamily): ?bool
  161. {
  162. if ($productFamily->getPropertyNoveltyExpirationDate()) {
  163. $now = new \DateTime();
  164. if ($now <= $productFamily->getPropertyNoveltyExpirationDate()) {
  165. return true;
  166. }
  167. }
  168. return false;
  169. }
  170. public function countProperties(ProductFamilyInterface $productFamily): bool
  171. {
  172. $count = 0;
  173. $count += (int)strlen($productFamily->getPropertyOrganicLabel()) > 0;
  174. $count += (int)strlen($productFamily->getPropertyWeight()) > 0;
  175. $count += (int)strlen($productFamily->getPropertyFragrances()) > 0;
  176. $count += (int)strlen($productFamily->getPropertyComposition()) > 0;
  177. $count += (int)strlen($productFamily->getPropertyAllergens()) > 0;
  178. $count += (int)strlen($productFamily->getPropertyAlcoholLevel()) > 0;
  179. $count += (int)strlen($productFamily->getPropertyCharacteristics()) > 0;
  180. $count += (int)strlen($productFamily->getPropertyFeature()) > 0;
  181. $count += (int)strlen($productFamily->getPropertyPackaging()) > 0;
  182. $count += (int)strlen($productFamily->getPropertyQuantity()) > 0;
  183. $count += (int)strlen($productFamily->getPropertyVariety()) > 0;
  184. $count += (int)($productFamily->getPropertyExpirationDate() != null);
  185. return $count;
  186. }
  187. public function hasProductsWithVariousWeight(ProductFamilyInterface $productFamily)
  188. {
  189. if ($productFamily->getActiveProducts()) {
  190. $arrayCountProducts = [];
  191. $products = $this->getProductsOnline($productFamily);
  192. foreach ($products as $product) {
  193. $titleProduct = $this->productSolver->getTitleInherited($product);
  194. if (!isset($arrayCountProducts[$titleProduct])) {
  195. $arrayCountProducts[$titleProduct] = [];
  196. }
  197. if (!in_array(
  198. $this->productSolver->getQuantityLabelInherited($product),
  199. $arrayCountProducts[$titleProduct]
  200. )) {
  201. $arrayCountProducts[$titleProduct][] = $this->productSolver->getQuantityLabelInherited($product);
  202. }
  203. if (count($arrayCountProducts[$titleProduct]) > 1) {
  204. return true;
  205. }
  206. }
  207. }
  208. return false;
  209. }
  210. public function getProductsGroupByTitle(ProductFamilyInterface $productFamily): array
  211. {
  212. $arrayProductsGroupByTitle = [];
  213. $products = $this->getProductsOnline($productFamily);
  214. foreach ($products as $product) {
  215. if ($product->getStatus() == 1) {
  216. $titleProduct = $this->productSolver->getTitleInherited($product);
  217. if (!isset($arrayProductsGroupByTitle[$titleProduct])) {
  218. $arrayProductsGroupByTitle[$titleProduct] = [];
  219. }
  220. $arrayProductsGroupByTitle[$titleProduct][] = $product;
  221. }
  222. }
  223. return $arrayProductsGroupByTitle;
  224. }
  225. public function getQuantityTitle(ProductFamilyInterface $productFamily, ProductInterface $product)
  226. {
  227. $title = $this->productSolver->getQuantityLabelInherited($product);
  228. if ($this->hasProductsWithVariousWeight($productFamily)) {
  229. $title .= ', ' . $this->productSolver->getTitleInherited($product);
  230. }
  231. return $title;
  232. }
  233. public function getOriginProduct(ProductFamilyInterface $productFamily): ?ProductInterface
  234. {
  235. $products = $productFamily->getProducts();
  236. foreach ($products as $product) {
  237. if ($product->getOriginProduct()) {
  238. return $product;
  239. }
  240. }
  241. return null;
  242. }
  243. public function getOriginProductOnline(ProductFamilyInterface $productFamily): ?ProductInterface
  244. {
  245. $originProduct = $this->getOriginProduct($productFamily);
  246. if ($originProduct->getStatus() == 1) {
  247. return $originProduct;
  248. } else {
  249. return null;
  250. }
  251. }
  252. public function hasOneProductOnline(ProductFamilyInterface $productFamily)
  253. {
  254. if (($productFamily->getActiveProducts() && count($this->getProductsOnline($productFamily)) > 0)
  255. || (!$productFamily->getActiveProducts() && $this->getOriginProduct($productFamily))) {
  256. return true;
  257. }
  258. return false;
  259. }
  260. public function getFieldBuyingPrice(ProductFamilyInterface $productFamily): string
  261. {
  262. if ($productFamily->getBehaviorPrice() === ProductFamilyModel::BEHAVIOR_PRICE_BY_PIECE) {
  263. return 'buyingPrice';
  264. } elseif ($productFamily->getBehaviorPrice() === ProductFamilyModel::BEHAVIOR_PRICE_BY_REFERENCE_UNIT) {
  265. return 'buyingPriceByRefUnit';
  266. }
  267. }
  268. public function getFieldPrice(ProductFamilyInterface $productFamily): string
  269. {
  270. if ($productFamily->getBehaviorPrice() === ProductFamilyModel::BEHAVIOR_PRICE_BY_PIECE) {
  271. return 'price';
  272. } elseif ($productFamily->getBehaviorPrice() === ProductFamilyModel::BEHAVIOR_PRICE_BY_REFERENCE_UNIT) {
  273. return 'priceByRefUnit';
  274. }
  275. }
  276. public function getBehaviorPriceInherited(ProductFamilyInterface $productFamily): string
  277. {
  278. return $productFamily->getBehaviorPrice();
  279. }
  280. public function getBuyingPriceByRefUnitInherited(ProductFamilyInterface $productFamily): ?float
  281. {
  282. return $productFamily->getBuyingPriceByRefUnit();
  283. }
  284. public function getBuyingPriceInherited(ProductFamilyInterface $productFamily): ?float
  285. {
  286. return $productFamily->getBuyingPrice();
  287. }
  288. public function getPriceByRefUnitInherited(ProductFamilyInterface $productFamily): ?float
  289. {
  290. return $productFamily->getPriceByRefUnit();
  291. }
  292. public function getQuantityInherited(ProductFamilyInterface $productFamily): ?float
  293. {
  294. return $productFamily->getQuantity();
  295. }
  296. public function getUnitInherited(ProductFamilyInterface $productFamily)
  297. {
  298. return $productFamily->getUnit();
  299. }
  300. public function getPriceInherited(ProductFamilyInterface $productFamily)
  301. {
  302. return $productFamily->getPrice();
  303. }
  304. public function getSummaryQualityLabels(ProductFamilyInterface $productFamily): string
  305. {
  306. $strLabels = '';
  307. $qualityLabelArray = $productFamily->getQualityLabels()->toArray();
  308. foreach($qualityLabelArray as $index => $qualityLabel) {
  309. $strLabels .= $qualityLabel->getTitle();
  310. if ($index !== array_key_last($qualityLabelArray)) {
  311. $strLabels .= ', ';
  312. }
  313. }
  314. return $strLabels;
  315. }
  316. public function hasQualityLabel(ProductFamilyInterface $productFamily): bool
  317. {
  318. $qualityLabels = $productFamily->getQualityLabels();
  319. return $qualityLabels && count($qualityLabels) > 0;
  320. }
  321. public function getOrganicLabel(ProductFamilyInterface $productFamily)
  322. {
  323. return $this->getQualityLabelByArray($productFamily, ProductFamilyModel::$organicLabels);
  324. }
  325. public function getGeographicLabel(ProductFamilyInterface $productFamily)
  326. {
  327. return $this->getQualityLabelByArray($productFamily, ProductFamilyModel::$geographicLabels);
  328. }
  329. public function getQualityLabelByArray(ProductFamilyInterface $productFamily, array $labelArray)
  330. {
  331. $qualityLabelArray = $productFamily->getQualityLabels();
  332. foreach ($qualityLabelArray as $qualityLabel) {
  333. if (in_array($qualityLabel->getDevAlias(), $labelArray)) {
  334. return $qualityLabel;
  335. }
  336. }
  337. return false;
  338. }
  339. public function hasOrganicLabel(ProductFamilyInterface $productFamily)
  340. {
  341. return $this->hasQualityLabelByArray($productFamily, ProductFamilyModel::$organicLabels);
  342. }
  343. public function hasGeographicLabel(ProductFamilyInterface $productFamily)
  344. {
  345. return $this->hasQualityLabelByArray($productFamily, ProductFamilyModel::$geographicLabels);
  346. }
  347. public function hasQualityLabelByArray(ProductFamilyInterface $productFamily, array $labelArray)
  348. {
  349. $qualityLabelArray = $productFamily->getQualityLabels();
  350. foreach ($qualityLabelArray as $qualityLabel) {
  351. if (in_array($qualityLabel->getDevAlias(), $labelArray)) {
  352. return true;
  353. }
  354. }
  355. return false;
  356. }
  357. public function getSection(ProductFamilyInterface $productFamily): ?SectionInterface
  358. {
  359. foreach ($productFamily->getProductFamilySectionProperties() as $productFamilySectionProperty) {
  360. if ($productFamilySectionProperty->getStatus()) {
  361. return $productFamilySectionProperty->getSection();
  362. }
  363. }
  364. return null;
  365. }
  366. public function isInSection(ProductFamilyInterface $productFamily, SectionInterface $section): bool
  367. {
  368. foreach ($productFamily->getProductFamilySectionProperties() as $productFamilySectionProperty) {
  369. if ($productFamilySectionProperty->getSection()->getId() == $section->getId(
  370. ) && $productFamilySectionProperty->getStatus()) {
  371. return true;
  372. }
  373. }
  374. return false;
  375. }
  376. public function isCategoriesOnlineInSection(ProductFamilyInterface $productFamily, SectionInterface $section):bool
  377. {
  378. $isCategoriesOnlineInSection =false;
  379. foreach ($productFamily->getProductCategories() as $productCatgory){
  380. if($productCatgory->getSection() === $section && $this->productCategorySolver->isOnline($productCatgory)){
  381. $isCategoriesOnlineInSection = true;
  382. }
  383. }
  384. return $isCategoriesOnlineInSection;
  385. }
  386. }