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.

ProductFamilySolver.php 12KB

3 anni fa
3 anni fa
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. }