Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

370 rindas
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 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. }