您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ProductFamilySolver.php 12KB

3 年前
3 年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 = $product->getTitleInherited();
  115. if (!isset($arrayCountProducts[$titleProduct])) {
  116. $arrayCountProducts[$titleProduct] = [];
  117. }
  118. if (!in_array($product->getQuantityLabelInherited(), $arrayCountProducts[$titleProduct])) {
  119. $arrayCountProducts[$titleProduct][] = $product->getQuantityLabelInherited();
  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 = $product->getTitleInherited();
  135. if (!isset($arrayProductsGroupByTitle[$titleProduct])) {
  136. $arrayProductsGroupByTitle[$titleProduct] = [];
  137. }
  138. $arrayProductsGroupByTitle[$titleProduct][] = $product;
  139. }
  140. }
  141. return $arrayProductsGroupByTitle;
  142. }
  143. public function getOriginProduct(ProductFamilyInterface $productFamily): ?ProductInterface
  144. {
  145. $products = $productFamily->getProducts();
  146. foreach ($products as $product) {
  147. if ($product->getOriginProduct()) {
  148. return $product;
  149. }
  150. }
  151. return null;
  152. }
  153. public function getOriginProductOnline(ProductFamilyInterface $productFamily): ?ProductInterface
  154. {
  155. $originProduct = $this->getOriginProduct($productFamily);
  156. if ($originProduct->getStatus() == 1) {
  157. return $originProduct;
  158. } else {
  159. return null;
  160. }
  161. }
  162. public function hasOneProductOnline(ProductFamilyInterface $productFamily)
  163. {
  164. if (($productFamily->getActiveProducts() && count($this->getProductsOnline($productFamily)) > 0)
  165. || (!$productFamily->getActiveProducts() && $this->getOriginProduct($productFamily))) {
  166. return true;
  167. }
  168. return false;
  169. }
  170. public function getFieldBuyingPrice(ProductFamilyInterface $productFamily): string
  171. {
  172. if ($productFamily->getBehaviorPrice() === ProductFamilyModel::BEHAVIOR_PRICE_BY_PIECE) {
  173. return 'buyingPrice';
  174. } elseif ($productFamily->getBehaviorPrice() === ProductFamilyModel::BEHAVIOR_PRICE_BY_REFERENCE_UNIT) {
  175. return 'buyingPriceByRefUnit';
  176. }
  177. }
  178. public function getFieldPrice(ProductFamilyInterface $productFamily): string
  179. {
  180. if ($productFamily->getBehaviorPrice() === ProductFamilyModel::BEHAVIOR_PRICE_BY_PIECE) {
  181. return 'price';
  182. } elseif ($productFamily->getBehaviorPrice() === ProductFamilyModel::BEHAVIOR_PRICE_BY_REFERENCE_UNIT) {
  183. return 'priceByRefUnit';
  184. }
  185. }
  186. public function getBehaviorPriceInherited(ProductFamilyInterface $productFamily) :string
  187. {
  188. return $productFamily->getBehaviorPrice();
  189. }
  190. public function getBehaviorCountStockChoices(): array
  191. {
  192. return [
  193. ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE,
  194. ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT,
  195. ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY,
  196. ProductFamilyModel::BEHAVIOR_COUNT_STOCK_UNLIMITED,
  197. ];
  198. }
  199. public function getBehaviorDisplaySaleChoices(): array
  200. {
  201. return [
  202. ProductFamilyModel::BEHAVIOR_DISPLAY_SALE_BY_MEASURE,
  203. ProductFamilyModel::BEHAVIOR_DISPLAY_SALE_BY_QUANTITY,
  204. ];
  205. }
  206. public function getBehaviorStockCycleChoices(): array
  207. {
  208. return [
  209. ProductFamilyModel::BEHAVIOR_STOCK_CYCLE_NON_RENEWABLE,
  210. ProductFamilyModel::BEHAVIOR_STOCK_CYCLE_RENEWABLE,
  211. ProductFamilyModel::BEHAVIOR_STOCK_CYCLE_RENEWABLE_VALIDATION,
  212. ];
  213. }
  214. public function getWaringMessageTypeChoices(): array
  215. {
  216. return [
  217. ProductFamilyModel::WARNING_MESSAGE_TYPE_ERROR,
  218. ProductFamilyModel::WARNING_MESSAGE_TYPE_INFO,
  219. ProductFamilyModel::WARNING_MESSAGE_TYPE_SUCCESS,
  220. ProductFamilyModel::WARNING_MESSAGE_TYPE_WARNING,
  221. ];
  222. }
  223. public function getBehaviorAddToCartChoices(): array
  224. {
  225. return [
  226. ProductFamilyModel::BEHAVIOR_ADD_TO_CART_MULTIPLE,
  227. ProductFamilyModel::BEHAVIOR_ADD_TO_CART_SIMPLE,
  228. ];
  229. }
  230. public function getBehaviorPriceChoices(): array
  231. {
  232. return [
  233. ProductFamilyModel::BEHAVIOR_PRICE_BY_PIECE,
  234. ProductFamilyModel::BEHAVIOR_PRICE_BY_REFERENCE_UNIT,
  235. ];
  236. }
  237. public function getPropertyOrganicLabelChoices(): array
  238. {
  239. return [
  240. ProductFamilyModel::PROPERTY_ORGANIC_LABEL_AB,
  241. ProductFamilyModel::PROPERTY_ORGANIC_LABEL_NP,
  242. ProductFamilyModel::PROPERTY_ORGANIC_LABEL_HVE,
  243. ProductFamilyModel::PROPERTY_ORGANIC_LABEL_TVVR,
  244. ];
  245. }
  246. public function getTypeExpirationDateChoices(): array
  247. {
  248. return [
  249. ProductFamilyModel::TYPE_EXPIRATION_DATE_DLC,
  250. ProductFamilyModel::TYPE_EXPIRATION_DATE_DDM,
  251. ProductFamilyModel::TYPE_EXPIRATION_DATE_DLUO,
  252. ];
  253. }
  254. public function getBehaviorExpirationDateChoices(): array
  255. {
  256. return [
  257. ProductFamilyModel::BEHAVIOR_EXPIRATION_DATE_BY_PRODUCT_FAMILY,
  258. ProductFamilyModel::BEHAVIOR_EXPIRATION_DATE_BY_PRODUCT,
  259. ];
  260. }
  261. public function getBuyingPriceByRefUnitInherited(ProductFamilyInterface $productFamily): ?float
  262. {
  263. return $productFamily->getBuyingPriceByRefUnit();
  264. }
  265. public function getBuyingPriceInherited(ProductFamilyInterface $productFamily): ?float
  266. {
  267. return $productFamily->getBuyingPrice();
  268. }
  269. public function getPriceByRefUnitInherited(ProductFamilyInterface $productFamily): ?float
  270. {
  271. return $productFamily->getPriceByRefUnit();
  272. }
  273. public function getQuantityInherited(ProductFamilyInterface $productFamily): ?float
  274. {
  275. return $productFamily->getQuantity();
  276. }
  277. public function getUnitInherited(ProductFamilyInterface $productFamily)
  278. {
  279. return $productFamily->getUnit();
  280. }
  281. public function getPriceInherited(ProductFamilyInterface $productFamily)
  282. {
  283. return $productFamily->getPrice();
  284. }
  285. }