Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

372 lines
12KB

  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\Solver\Price\PriceSolver;
  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 getPropertyOrganicLabelChoices(): array
  50. {
  51. return [
  52. ProductFamilyModel::PROPERTY_ORGANIC_LABEL_AB,
  53. ProductFamilyModel::PROPERTY_ORGANIC_LABEL_NP,
  54. ProductFamilyModel::PROPERTY_ORGANIC_LABEL_HVE,
  55. ProductFamilyModel::PROPERTY_ORGANIC_LABEL_TVVR,
  56. ];
  57. }
  58. public function getTypeExpirationDateChoices(): array
  59. {
  60. return [
  61. ProductFamilyModel::TYPE_EXPIRATION_DATE_DLC,
  62. ProductFamilyModel::TYPE_EXPIRATION_DATE_DDM,
  63. ProductFamilyModel::TYPE_EXPIRATION_DATE_DLUO,
  64. ];
  65. }
  66. public function getBehaviorExpirationDateChoices(): array
  67. {
  68. return [
  69. ProductFamilyModel::BEHAVIOR_EXPIRATION_DATE_BY_PRODUCT_FAMILY,
  70. ProductFamilyModel::BEHAVIOR_EXPIRATION_DATE_BY_PRODUCT,
  71. ];
  72. }
  73. public function getBehaviorDisplaySaleChoices(): array
  74. {
  75. return [
  76. ProductFamilyModel::BEHAVIOR_DISPLAY_SALE_BY_MEASURE,
  77. ProductFamilyModel::BEHAVIOR_DISPLAY_SALE_BY_QUANTITY,
  78. ];
  79. }
  80. public function countProductFamiliesOrganizedByParentCategory(array $categories): int
  81. {
  82. $count = 0;
  83. foreach ($categories as $category) {
  84. $count += count($category['products']);
  85. }
  86. return $count;
  87. }
  88. public function getAvailableQuantityInherited(ProductFamilyInterface $productFamily)
  89. {
  90. $availableQuantity = 0;
  91. switch ($productFamily->getBehaviorCountStock()) {
  92. case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE :
  93. case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY :
  94. $availableQuantity = $productFamily->getAvailableQuantity();
  95. break;
  96. case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT :
  97. foreach ($this->getProductsOnline($productFamily) as $product) {
  98. $availableQuantity += $this->productSolver->getAvailableQuantityInherited($product);
  99. }
  100. break;
  101. case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_UNLIMITED :
  102. $availableQuantity = false;
  103. break;
  104. }
  105. return $availableQuantity;
  106. }
  107. public function getTaxRateInherited(ProductPropertyInterface $productFamily)
  108. {
  109. if($productFamily instanceof ProductInterface) {
  110. $productFamily = $productFamily->getProductFamily();
  111. }
  112. if ($productFamily->getTaxRate()) {
  113. return $productFamily->getTaxRate();
  114. } else {
  115. return $productFamily->getProductFamilySectionProperties()[0]->getSection()->getMerchant()->getTaxRate();
  116. }
  117. }
  118. public function getProductsOnline(ProductFamilyInterface $productFamily): Collection
  119. {
  120. $products = $productFamily->getProducts();
  121. $productsOnlineArray = new ArrayCollection();
  122. foreach ($products as $product) {
  123. if ($product->getStatus() == 1 && $product->getOriginProduct() != true) {
  124. $productsOnlineArray[] = $product;
  125. }
  126. }
  127. return $productsOnlineArray;
  128. }
  129. public function getReductionCatalogInherited(ProductFamilyInterface $productFamily): ?ReductionCatalogInterface
  130. {
  131. return $productFamily->getReductionCatalog();
  132. }
  133. public function getProductCategoryParent(ProductFamilyInterface $productFamily)
  134. {
  135. $productCategories = $productFamily->getProductCategories();
  136. if (count($productCategories) > 0) {
  137. return $productCategories[0]->getParent();
  138. }
  139. return false;
  140. }
  141. public function getProductCategoryChild(ProductFamilyInterface $productFamily)
  142. {
  143. $productCategories = $productFamily->getProductCategories();
  144. foreach ($productCategories as $productCategory) {
  145. if ($productCategory->getParent()) {
  146. return $productCategory;
  147. }
  148. }
  149. return false;
  150. }
  151. public function isPropertyNoveltyOnline(ProductFamilyInterface $productFamily): ?bool
  152. {
  153. if ($productFamily->getPropertyNoveltyExpirationDate()) {
  154. $now = new \DateTime();
  155. if ($now <= $productFamily->getPropertyNoveltyExpirationDate()) {
  156. return true;
  157. }
  158. }
  159. return false;
  160. }
  161. public function countProperties(ProductFamilyInterface $productFamily): bool
  162. {
  163. $count = 0;
  164. $count += (int)strlen($productFamily->getPropertyOrganicLabel()) > 0;
  165. $count += (int)strlen($productFamily->getPropertyWeight()) > 0;
  166. $count += (int)strlen($productFamily->getPropertyFragrances()) > 0;
  167. $count += (int)strlen($productFamily->getPropertyComposition()) > 0;
  168. $count += (int)strlen($productFamily->getPropertyAllergens()) > 0;
  169. $count += (int)strlen($productFamily->getPropertyAlcoholLevel()) > 0;
  170. $count += (int)strlen($productFamily->getPropertyCharacteristics()) > 0;
  171. $count += (int)strlen($productFamily->getPropertyFeature()) > 0;
  172. $count += (int)strlen($productFamily->getPropertyPackaging()) > 0;
  173. $count += (int)strlen($productFamily->getPropertyQuantity()) > 0;
  174. $count += (int)strlen($productFamily->getPropertyVariety()) > 0;
  175. $count += (int)($productFamily->getPropertyExpirationDate() != null);
  176. return $count;
  177. }
  178. public function hasProductsWithVariousWeight(ProductFamilyInterface $productFamily)
  179. {
  180. if ($productFamily->getActiveProducts()) {
  181. $arrayCountProducts = [];
  182. $products = $this->getProductsOnline($productFamily);
  183. foreach ($products as $product) {
  184. $titleProduct = $this->productSolver->getTitleInherited($product);
  185. if (!isset($arrayCountProducts[$titleProduct])) {
  186. $arrayCountProducts[$titleProduct] = [];
  187. }
  188. if (!in_array($this->productSolver->getQuantityLabelInherited($product), $arrayCountProducts[$titleProduct])) {
  189. $arrayCountProducts[$titleProduct][] = $this->productSolver->getQuantityLabelInherited($product);
  190. }
  191. if (count($arrayCountProducts[$titleProduct]) > 1) {
  192. return true;
  193. }
  194. }
  195. }
  196. return false;
  197. }
  198. public function getProductsGroupByTitle(ProductFamilyInterface $productFamily): array
  199. {
  200. $arrayProductsGroupByTitle = [];
  201. $products = $this->getProductsOnline($productFamily);
  202. foreach ($products as $product) {
  203. if ($product->getStatus() == 1) {
  204. $titleProduct = $this->productSolver->getTitleInherited($product);
  205. if (!isset($arrayProductsGroupByTitle[$titleProduct])) {
  206. $arrayProductsGroupByTitle[$titleProduct] = [];
  207. }
  208. $arrayProductsGroupByTitle[$titleProduct][] = $product;
  209. }
  210. }
  211. return $arrayProductsGroupByTitle;
  212. }
  213. public function getQuantityTitle(ProductFamilyInterface $productFamily, ProductInterface $product)
  214. {
  215. $title = $this->productSolver->getQuantityLabelInherited($product);
  216. if ($this->hasProductsWithVariousWeight($productFamily)) {
  217. $title .= ', ' . $this->productSolver->getTitleInherited($product);
  218. }
  219. return $title;
  220. }
  221. public function getOriginProduct(ProductFamilyInterface $productFamily): ?ProductInterface
  222. {
  223. $products = $productFamily->getProducts();
  224. foreach ($products as $product) {
  225. if ($product->getOriginProduct()) {
  226. return $product;
  227. }
  228. }
  229. return null;
  230. }
  231. public function getOriginProductOnline(ProductFamilyInterface $productFamily): ?ProductInterface
  232. {
  233. $originProduct = $this->getOriginProduct($productFamily);
  234. if ($originProduct->getStatus() == 1) {
  235. return $originProduct;
  236. } else {
  237. return null;
  238. }
  239. }
  240. public function hasOneProductOnline(ProductFamilyInterface $productFamily)
  241. {
  242. if (($productFamily->getActiveProducts() && count($this->getProductsOnline($productFamily)) > 0)
  243. || (!$productFamily->getActiveProducts() && $this->getOriginProduct($productFamily))) {
  244. return true;
  245. }
  246. return false;
  247. }
  248. public function getFieldBuyingPrice(ProductFamilyInterface $productFamily): string
  249. {
  250. if ($productFamily->getBehaviorPrice() === ProductFamilyModel::BEHAVIOR_PRICE_BY_PIECE) {
  251. return 'buyingPrice';
  252. } elseif ($productFamily->getBehaviorPrice() === ProductFamilyModel::BEHAVIOR_PRICE_BY_REFERENCE_UNIT) {
  253. return 'buyingPriceByRefUnit';
  254. }
  255. }
  256. public function getFieldPrice(ProductFamilyInterface $productFamily): string
  257. {
  258. if ($productFamily->getBehaviorPrice() === ProductFamilyModel::BEHAVIOR_PRICE_BY_PIECE) {
  259. return 'price';
  260. } elseif ($productFamily->getBehaviorPrice() === ProductFamilyModel::BEHAVIOR_PRICE_BY_REFERENCE_UNIT) {
  261. return 'priceByRefUnit';
  262. }
  263. }
  264. public function getBehaviorPriceInherited(ProductFamilyInterface $productFamily) :string
  265. {
  266. return $productFamily->getBehaviorPrice();
  267. }
  268. public function getBuyingPriceByRefUnitInherited(ProductFamilyInterface $productFamily): ?float
  269. {
  270. return $productFamily->getBuyingPriceByRefUnit();
  271. }
  272. public function getBuyingPriceInherited(ProductFamilyInterface $productFamily): ?float
  273. {
  274. return $productFamily->getBuyingPrice();
  275. }
  276. public function getPriceByRefUnitInherited(ProductFamilyInterface $productFamily): ?float
  277. {
  278. return $productFamily->getPriceByRefUnit();
  279. }
  280. public function getQuantityInherited(ProductFamilyInterface $productFamily): ?float
  281. {
  282. return $productFamily->getQuantity();
  283. }
  284. public function getUnitInherited(ProductFamilyInterface $productFamily)
  285. {
  286. return $productFamily->getUnit();
  287. }
  288. public function getPriceInherited(ProductFamilyInterface $productFamily)
  289. {
  290. return $productFamily->getPrice();
  291. }
  292. }