Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

424 lines
14KB

  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. }
  92. return $availableQuantity;
  93. }
  94. public function getAvailableQuantitySupplierInherited(ProductFamilyInterface $productFamily)
  95. {
  96. $availableQuantity = 0;
  97. switch ($productFamily->getBehaviorCountStock()) {
  98. case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE :
  99. case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY :
  100. $availableQuantity = $productFamily->getAvailableQuantitySupplier();
  101. break;
  102. case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT :
  103. foreach ($this->getProductsOnline($productFamily) as $product) {
  104. $availableQuantity += $this->productSolver->getAvailableQuantitySupplierInherited($product);
  105. }
  106. break;
  107. }
  108. return $availableQuantity;
  109. }
  110. public function getTaxRateInherited(ProductPropertyInterface $productFamily)
  111. {
  112. if($productFamily instanceof ProductInterface) {
  113. $productFamily = $productFamily->getProductFamily();
  114. }
  115. if ($productFamily->getTaxRate()) {
  116. return $productFamily->getTaxRate();
  117. } else {
  118. return $productFamily->getProductFamilySectionProperties()[0]->getSection()->getMerchant()->getTaxRate();
  119. }
  120. }
  121. public function getProductsOnline(ProductFamilyInterface $productFamily): Collection
  122. {
  123. $products = $productFamily->getProducts();
  124. $productsOnlineArray = new ArrayCollection();
  125. foreach ($products as $product) {
  126. if ($product->getStatus() == 1 && $product->getOriginProduct() != true) {
  127. $productsOnlineArray[] = $product;
  128. }
  129. }
  130. return $productsOnlineArray;
  131. }
  132. public function getReductionCatalogInherited(ProductFamilyInterface $productFamily): ?ReductionCatalogInterface
  133. {
  134. return $productFamily->getReductionCatalog();
  135. }
  136. public function getProductCategoryParent(ProductFamilyInterface $productFamily)
  137. {
  138. $productCategories = $productFamily->getProductCategories();
  139. if (count($productCategories) > 0) {
  140. return $productCategories[0]->getParent();
  141. }
  142. return false;
  143. }
  144. public function getProductCategoryChild(ProductFamilyInterface $productFamily)
  145. {
  146. $productCategories = $productFamily->getProductCategories();
  147. foreach ($productCategories as $productCategory) {
  148. if ($productCategory->getParent()) {
  149. return $productCategory;
  150. }
  151. }
  152. return false;
  153. }
  154. public function isPropertyNoveltyOnline(ProductFamilyInterface $productFamily): ?bool
  155. {
  156. if ($productFamily->getPropertyNoveltyExpirationDate()) {
  157. $now = new \DateTime();
  158. if ($now <= $productFamily->getPropertyNoveltyExpirationDate()) {
  159. return true;
  160. }
  161. }
  162. return false;
  163. }
  164. public function countProperties(ProductFamilyInterface $productFamily): bool
  165. {
  166. $count = 0;
  167. $count += (int)strlen($productFamily->getPropertyOrganicLabel()) > 0;
  168. $count += (int)strlen($productFamily->getPropertyWeight()) > 0;
  169. $count += (int)strlen($productFamily->getPropertyFragrances()) > 0;
  170. $count += (int)strlen($productFamily->getPropertyComposition()) > 0;
  171. $count += (int)strlen($productFamily->getPropertyAllergens()) > 0;
  172. $count += (int)strlen($productFamily->getPropertyAlcoholLevel()) > 0;
  173. $count += (int)strlen($productFamily->getPropertyCharacteristics()) > 0;
  174. $count += (int)strlen($productFamily->getPropertyFeature()) > 0;
  175. $count += (int)strlen($productFamily->getPropertyPackaging()) > 0;
  176. $count += (int)strlen($productFamily->getPropertyQuantity()) > 0;
  177. $count += (int)strlen($productFamily->getPropertyVariety()) > 0;
  178. $count += (int)($productFamily->getPropertyExpirationDate() != null);
  179. return $count;
  180. }
  181. public function hasProductsWithVariousWeight(ProductFamilyInterface $productFamily)
  182. {
  183. if ($productFamily->getActiveProducts()) {
  184. $arrayCountProducts = [];
  185. $products = $this->getProductsOnline($productFamily);
  186. foreach ($products as $product) {
  187. $titleProduct = $this->productSolver->getTitleInherited($product);
  188. if (!isset($arrayCountProducts[$titleProduct])) {
  189. $arrayCountProducts[$titleProduct] = [];
  190. }
  191. if (!in_array($this->productSolver->getQuantityLabelInherited($product), $arrayCountProducts[$titleProduct])) {
  192. $arrayCountProducts[$titleProduct][] = $this->productSolver->getQuantityLabelInherited($product);
  193. }
  194. if (count($arrayCountProducts[$titleProduct]) > 1) {
  195. return true;
  196. }
  197. }
  198. }
  199. return false;
  200. }
  201. public function getProductsGroupByTitle(ProductFamilyInterface $productFamily): array
  202. {
  203. $arrayProductsGroupByTitle = [];
  204. $products = $this->getProductsOnline($productFamily);
  205. foreach ($products as $product) {
  206. if ($product->getStatus() == 1) {
  207. $titleProduct = $this->productSolver->getTitleInherited($product);
  208. if (!isset($arrayProductsGroupByTitle[$titleProduct])) {
  209. $arrayProductsGroupByTitle[$titleProduct] = [];
  210. }
  211. $arrayProductsGroupByTitle[$titleProduct][] = $product;
  212. }
  213. }
  214. return $arrayProductsGroupByTitle;
  215. }
  216. public function getQuantityTitle(ProductFamilyInterface $productFamily, ProductInterface $product)
  217. {
  218. $title = $this->productSolver->getQuantityLabelInherited($product);
  219. if ($this->hasProductsWithVariousWeight($productFamily)) {
  220. $title .= ', ' . $this->productSolver->getTitleInherited($product);
  221. }
  222. return $title;
  223. }
  224. public function getOriginProduct(ProductFamilyInterface $productFamily): ?ProductInterface
  225. {
  226. $products = $productFamily->getProducts();
  227. foreach ($products as $product) {
  228. if ($product->getOriginProduct()) {
  229. return $product;
  230. }
  231. }
  232. return null;
  233. }
  234. public function getOriginProductOnline(ProductFamilyInterface $productFamily): ?ProductInterface
  235. {
  236. $originProduct = $this->getOriginProduct($productFamily);
  237. if ($originProduct->getStatus() == 1) {
  238. return $originProduct;
  239. } else {
  240. return null;
  241. }
  242. }
  243. public function hasOneProductOnline(ProductFamilyInterface $productFamily)
  244. {
  245. if (($productFamily->getActiveProducts() && count($this->getProductsOnline($productFamily)) > 0)
  246. || (!$productFamily->getActiveProducts() && $this->getOriginProduct($productFamily))) {
  247. return true;
  248. }
  249. return false;
  250. }
  251. public function getFieldBuyingPrice(ProductFamilyInterface $productFamily): string
  252. {
  253. if ($productFamily->getBehaviorPrice() === ProductFamilyModel::BEHAVIOR_PRICE_BY_PIECE) {
  254. return 'buyingPrice';
  255. } elseif ($productFamily->getBehaviorPrice() === ProductFamilyModel::BEHAVIOR_PRICE_BY_REFERENCE_UNIT) {
  256. return 'buyingPriceByRefUnit';
  257. }
  258. }
  259. public function getFieldPrice(ProductFamilyInterface $productFamily): string
  260. {
  261. if ($productFamily->getBehaviorPrice() === ProductFamilyModel::BEHAVIOR_PRICE_BY_PIECE) {
  262. return 'price';
  263. } elseif ($productFamily->getBehaviorPrice() === ProductFamilyModel::BEHAVIOR_PRICE_BY_REFERENCE_UNIT) {
  264. return 'priceByRefUnit';
  265. }
  266. }
  267. public function getBehaviorPriceInherited(ProductFamilyInterface $productFamily) :string
  268. {
  269. return $productFamily->getBehaviorPrice();
  270. }
  271. public function getBuyingPriceByRefUnitInherited(ProductFamilyInterface $productFamily): ?float
  272. {
  273. return $productFamily->getBuyingPriceByRefUnit();
  274. }
  275. public function getBuyingPriceInherited(ProductFamilyInterface $productFamily): ?float
  276. {
  277. return $productFamily->getBuyingPrice();
  278. }
  279. public function getPriceByRefUnitInherited(ProductFamilyInterface $productFamily): ?float
  280. {
  281. return $productFamily->getPriceByRefUnit();
  282. }
  283. public function getQuantityInherited(ProductFamilyInterface $productFamily): ?float
  284. {
  285. return $productFamily->getQuantity();
  286. }
  287. public function getUnitInherited(ProductFamilyInterface $productFamily)
  288. {
  289. return $productFamily->getUnit();
  290. }
  291. public function getPriceInherited(ProductFamilyInterface $productFamily)
  292. {
  293. return $productFamily->getPrice();
  294. }
  295. public function getOrganicLabel(ProductFamilyInterface $productFamily)
  296. {
  297. return $this->getQualityLabelByArray($productFamily, ProductFamilyModel::$organicLabels);
  298. }
  299. public function getGeographicLabel(ProductFamilyInterface $productFamily)
  300. {
  301. return $this->getQualityLabelByArray($productFamily, ProductFamilyModel::$geographicLabels);
  302. }
  303. public function getQualityLabelByArray(ProductFamilyInterface $productFamily, array $labelArray)
  304. {
  305. $qualityLabelArray = $productFamily->getQualityLabels();
  306. foreach($qualityLabelArray as $qualityLabel) {
  307. if(in_array($qualityLabel->getDevAlias(), $labelArray)) {
  308. return $qualityLabel;
  309. }
  310. }
  311. return false;
  312. }
  313. public function hasOrganicLabel(ProductFamilyInterface $productFamily)
  314. {
  315. return $this->hasQualityLabelByArray($productFamily, ProductFamilyModel::$organicLabels);
  316. }
  317. public function hasGeographicLabel(ProductFamilyInterface $productFamily)
  318. {
  319. return $this->hasQualityLabelByArray($productFamily, ProductFamilyModel::$geographicLabels);
  320. }
  321. public function hasQualityLabelByArray(ProductFamilyInterface $productFamily, array $labelArray)
  322. {
  323. $qualityLabelArray = $productFamily->getQualityLabels();
  324. foreach($qualityLabelArray as $qualityLabel) {
  325. if(in_array($qualityLabel->getDevAlias(), $labelArray)) {
  326. return true;
  327. }
  328. }
  329. return false;
  330. }
  331. }