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.

406 lines
13KB

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