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.

494 line
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 static 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 static 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 static function getBehaviorAddToCartChoices(): array
  41. {
  42. return [
  43. ProductFamilyModel::BEHAVIOR_ADD_TO_CART_MULTIPLE,
  44. ProductFamilyModel::BEHAVIOR_ADD_TO_CART_SIMPLE,
  45. ];
  46. }
  47. public static function getBehaviorPriceChoices(): array
  48. {
  49. return [
  50. ProductFamilyModel::BEHAVIOR_PRICE_BY_PIECE,
  51. ProductFamilyModel::BEHAVIOR_PRICE_BY_REFERENCE_UNIT,
  52. ];
  53. }
  54. public static 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 static 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 static 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, SectionInterface $section)
  143. {
  144. $productCategories = $productFamily->getProductCategories();
  145. if (count($productCategories) > 0) {
  146. foreach ($productCategories as $productCategory) {
  147. if($productCategory->getSection() === $section && $productCategory->getParent()!==null){
  148. return $productCategory->getParent();
  149. }
  150. }
  151. }
  152. return false;
  153. }
  154. public function getProductCategoryChild(ProductFamilyInterface $productFamily)
  155. {
  156. $productCategories = $productFamily->getProductCategories();
  157. foreach ($productCategories as $productCategory) {
  158. if ($productCategory->getParent()) {
  159. return $productCategory;
  160. }
  161. }
  162. return false;
  163. }
  164. public function isPropertyNoveltyOnline(ProductFamilyInterface $productFamily): ?bool
  165. {
  166. if ($productFamily->getPropertyNoveltyExpirationDate()) {
  167. $now = new \DateTime();
  168. if ($now <= $productFamily->getPropertyNoveltyExpirationDate()) {
  169. return true;
  170. }
  171. }
  172. return false;
  173. }
  174. public function countProperties(ProductFamilyInterface $productFamily): bool
  175. {
  176. $count = 0;
  177. $count += (int)strlen($productFamily->getPropertyOrganicLabel()) > 0;
  178. $count += (int)strlen($productFamily->getPropertyWeight()) > 0;
  179. $count += (int)strlen($productFamily->getPropertyFragrances()) > 0;
  180. $count += (int)strlen($productFamily->getPropertyComposition()) > 0;
  181. $count += (int)strlen($productFamily->getPropertyAllergens()) > 0;
  182. $count += (int)strlen($productFamily->getPropertyAlcoholLevel()) > 0;
  183. $count += (int)strlen($productFamily->getPropertyCharacteristics()) > 0;
  184. $count += (int)strlen($productFamily->getPropertyFeature()) > 0;
  185. $count += (int)strlen($productFamily->getPropertyPackaging()) > 0;
  186. $count += (int)strlen($productFamily->getPropertyQuantity()) > 0;
  187. $count += (int)strlen($productFamily->getPropertyVariety()) > 0;
  188. $count += (int)($productFamily->getPropertyExpirationDate() != null);
  189. return $count;
  190. }
  191. public function hasProductsWithVariousWeight(ProductFamilyInterface $productFamily)
  192. {
  193. if ($productFamily->getActiveProducts()) {
  194. $arrayCountProducts = [];
  195. $products = $this->getProductsOnline($productFamily);
  196. foreach ($products as $product) {
  197. $titleProduct = $this->productSolver->getTitleInherited($product);
  198. if (!isset($arrayCountProducts[$titleProduct])) {
  199. $arrayCountProducts[$titleProduct] = [];
  200. }
  201. if (!in_array(
  202. $this->productSolver->getQuantityLabelInherited($product),
  203. $arrayCountProducts[$titleProduct]
  204. )) {
  205. $arrayCountProducts[$titleProduct][] = $this->productSolver->getQuantityLabelInherited($product);
  206. }
  207. if (count($arrayCountProducts[$titleProduct]) > 1) {
  208. return true;
  209. }
  210. }
  211. }
  212. return false;
  213. }
  214. public function getProductsGroupByTitle(ProductFamilyInterface $productFamily): array
  215. {
  216. $arrayProductsGroupByTitle = [];
  217. $products = $this->getProductsOnline($productFamily);
  218. foreach ($products as $product) {
  219. if ($product->getStatus() == 1) {
  220. $titleProduct = $this->productSolver->getTitleInherited($product);
  221. if (!isset($arrayProductsGroupByTitle[$titleProduct])) {
  222. $arrayProductsGroupByTitle[$titleProduct] = [];
  223. }
  224. $arrayProductsGroupByTitle[$titleProduct][] = $product;
  225. }
  226. }
  227. return $arrayProductsGroupByTitle;
  228. }
  229. public function getQuantityTitle(ProductFamilyInterface $productFamily, ProductInterface $product)
  230. {
  231. $title = $this->productSolver->getQuantityLabelInherited($product);
  232. if ($this->hasProductsWithVariousWeight($productFamily)) {
  233. $title .= ', ' . $this->productSolver->getTitleInherited($product);
  234. }
  235. return $title;
  236. }
  237. public function getOriginProduct(ProductFamilyInterface $productFamily): ?ProductInterface
  238. {
  239. $products = $productFamily->getProducts();
  240. foreach ($products as $product) {
  241. if ($product->getOriginProduct()) {
  242. return $product;
  243. }
  244. }
  245. return null;
  246. }
  247. public function getOriginProductOnline(ProductFamilyInterface $productFamily): ?ProductInterface
  248. {
  249. $originProduct = $this->getOriginProduct($productFamily);
  250. if ($originProduct->getStatus() == 1) {
  251. return $originProduct;
  252. } else {
  253. return null;
  254. }
  255. }
  256. public function hasOneProductOnline(ProductFamilyInterface $productFamily)
  257. {
  258. if (($productFamily->getActiveProducts() && count($this->getProductsOnline($productFamily)) > 0)
  259. || (!$productFamily->getActiveProducts() && $this->getOriginProduct($productFamily))) {
  260. return true;
  261. }
  262. return false;
  263. }
  264. public function getFieldBuyingPrice(ProductFamilyInterface $productFamily): string
  265. {
  266. if ($productFamily->getBehaviorPrice() === ProductFamilyModel::BEHAVIOR_PRICE_BY_PIECE) {
  267. return 'buyingPrice';
  268. } elseif ($productFamily->getBehaviorPrice() === ProductFamilyModel::BEHAVIOR_PRICE_BY_REFERENCE_UNIT) {
  269. return 'buyingPriceByRefUnit';
  270. }
  271. }
  272. public function getFieldPrice(ProductFamilyInterface $productFamily): string
  273. {
  274. if ($productFamily->getBehaviorPrice() === ProductFamilyModel::BEHAVIOR_PRICE_BY_PIECE) {
  275. return 'price';
  276. } elseif ($productFamily->getBehaviorPrice() === ProductFamilyModel::BEHAVIOR_PRICE_BY_REFERENCE_UNIT) {
  277. return 'priceByRefUnit';
  278. }
  279. }
  280. public function getBehaviorPriceInherited(ProductFamilyInterface $productFamily): string
  281. {
  282. return $productFamily->getBehaviorPrice();
  283. }
  284. public function getBuyingPriceByRefUnitInherited(ProductFamilyInterface $productFamily): ?float
  285. {
  286. return $productFamily->getBuyingPriceByRefUnit();
  287. }
  288. public function getBuyingPriceInherited(ProductFamilyInterface $productFamily): ?float
  289. {
  290. return $productFamily->getBuyingPrice();
  291. }
  292. public function getPriceByRefUnitInherited(ProductFamilyInterface $productFamily): ?float
  293. {
  294. return $productFamily->getPriceByRefUnit();
  295. }
  296. public function getQuantityInherited(ProductFamilyInterface $productFamily): ?float
  297. {
  298. return $productFamily->getQuantity();
  299. }
  300. public function getUnitInherited(ProductFamilyInterface $productFamily)
  301. {
  302. return $productFamily->getUnit();
  303. }
  304. public function getPriceInherited(ProductFamilyInterface $productFamily)
  305. {
  306. return $productFamily->getPrice();
  307. }
  308. public function getSummaryQualityLabels(ProductFamilyInterface $productFamily): string
  309. {
  310. $strLabels = '';
  311. $qualityLabelArray = $productFamily->getQualityLabels()->toArray();
  312. foreach($qualityLabelArray as $index => $qualityLabel) {
  313. $strLabels .= $qualityLabel->getTitle();
  314. if ($index !== array_key_last($qualityLabelArray)) {
  315. $strLabels .= ', ';
  316. }
  317. }
  318. return $strLabels;
  319. }
  320. public function hasQualityLabel(ProductFamilyInterface $productFamily): bool
  321. {
  322. $qualityLabels = $productFamily->getQualityLabels();
  323. return $qualityLabels && count($qualityLabels) > 0;
  324. }
  325. public function getOrganicLabel(ProductFamilyInterface $productFamily)
  326. {
  327. return $this->getQualityLabelByArray($productFamily, ProductFamilyModel::$organicLabels);
  328. }
  329. public function getGeographicLabel(ProductFamilyInterface $productFamily)
  330. {
  331. return $this->getQualityLabelByArray($productFamily, ProductFamilyModel::$geographicLabels);
  332. }
  333. public function getQualityLabelByArray(ProductFamilyInterface $productFamily, array $labelArray)
  334. {
  335. $qualityLabelArray = $productFamily->getQualityLabels();
  336. foreach ($qualityLabelArray as $qualityLabel) {
  337. if (in_array($qualityLabel->getDevAlias(), $labelArray)) {
  338. return $qualityLabel;
  339. }
  340. }
  341. return false;
  342. }
  343. public function hasOrganicLabel(ProductFamilyInterface $productFamily)
  344. {
  345. return $this->hasQualityLabelByArray($productFamily, ProductFamilyModel::$organicLabels);
  346. }
  347. public function hasGeographicLabel(ProductFamilyInterface $productFamily)
  348. {
  349. return $this->hasQualityLabelByArray($productFamily, ProductFamilyModel::$geographicLabels);
  350. }
  351. public function hasQualityLabelByArray(ProductFamilyInterface $productFamily, array $labelArray)
  352. {
  353. $qualityLabelArray = $productFamily->getQualityLabels();
  354. foreach ($qualityLabelArray as $qualityLabel) {
  355. if (in_array($qualityLabel->getDevAlias(), $labelArray)) {
  356. return true;
  357. }
  358. }
  359. return false;
  360. }
  361. public function getSection(ProductFamilyInterface $productFamily): ?SectionInterface
  362. {
  363. foreach ($productFamily->getProductFamilySectionProperties() as $productFamilySectionProperty) {
  364. if ($productFamilySectionProperty->getStatus()) {
  365. return $productFamilySectionProperty->getSection();
  366. }
  367. }
  368. return null;
  369. }
  370. public function isInSection(ProductFamilyInterface $productFamily, SectionInterface $section): bool
  371. {
  372. foreach ($productFamily->getProductFamilySectionProperties() as $productFamilySectionProperty) {
  373. if ($productFamilySectionProperty->getSection()->getId() == $section->getId(
  374. ) && $productFamilySectionProperty->getStatus()) {
  375. return true;
  376. }
  377. }
  378. return false;
  379. }
  380. public function isCategoriesOnlineInSection(ProductFamilyInterface $productFamily, SectionInterface $section):bool
  381. {
  382. $isCategoriesOnlineInSection =false;
  383. foreach ($productFamily->getProductCategories() as $productCatgory){
  384. if($productCatgory->getSection() === $section && $this->productCategorySolver->isOnline($productCatgory)){
  385. $isCategoriesOnlineInSection = true;
  386. }
  387. }
  388. return $isCategoriesOnlineInSection;
  389. }
  390. }