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