Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ProductFamilySolver.php 12KB

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