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.

472 lines
15KB

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