Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

175 lines
7.5KB

  1. <?php
  2. namespace Lc\ShopBundle\Services ;
  3. use App\Entity\Product;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Lc\ShopBundle\Context\PriceUtilsInterface;
  6. use Lc\ShopBundle\Context\ProductCategoryInterface;
  7. use Lc\ShopBundle\Context\ProductFamilyInterface;
  8. use Lc\ShopBundle\Context\ProductInterface;
  9. use Lc\ShopBundle\Context\ReductionCatalogInterface;
  10. use Lc\ShopBundle\Model\ProductFamily;
  11. class ProductFamilyUtils
  12. {
  13. protected $priceUtils ;
  14. protected $em ;
  15. public function __construct(PriceUtilsInterface $priceUtils, EntityManagerInterface $em)
  16. {
  17. $this->priceUtils = $priceUtils ;
  18. $this->em = $em;
  19. }
  20. public function getCheapestProduct($productFamily)
  21. {
  22. $priceUtils = $this->priceUtils ;
  23. return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($priceUtils) {
  24. return $priceUtils->getPriceWithTaxAndReduction($a) > $priceUtils->getPriceWithTaxAndReduction($b) ;
  25. }, true);
  26. }
  27. public function getCheapestProductByRefUnit($productFamily)
  28. {
  29. $priceUtils = $this->priceUtils ;
  30. return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($priceUtils) {
  31. return $priceUtils->getPriceByRefUnitWithTaxAndReduction($a) > $priceUtils->getPriceByRefUnitWithTaxAndReduction($b) ;
  32. }, false);
  33. }
  34. public function getMostExpensiveProductByRefUnit($productFamily)
  35. {
  36. $priceUtils = $this->priceUtils ;
  37. return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($priceUtils) {
  38. return $priceUtils->getPriceByRefUnitWithTaxAndReduction($a) < $priceUtils->getPriceByRefUnitWithTaxAndReduction($b) ;
  39. }, false);
  40. }
  41. private function getCheapestOrMostExpensiveProduct($productFamily, $comparisonFunction, $returnSelfIfNotActiveProducts)
  42. {
  43. if($productFamily->getActiveProducts()) {
  44. $products = $productFamily->getProductsOnline()->getValues();
  45. if (count($products) > 0) {
  46. usort($products, $comparisonFunction);
  47. return $products[0];
  48. }
  49. }else{
  50. return $productFamily->getOriginProduct();
  51. }
  52. if ($returnSelfIfNotActiveProducts) {
  53. return $productFamily;
  54. }
  55. else {
  56. return false;
  57. }
  58. }
  59. public function processBeforePersistProductFamily($productFamily, $editForm=false, $clone =false){
  60. if($editForm){
  61. $this->processReductionCatalog($productFamily, $editForm);
  62. $this->processCategories($productFamily);
  63. }
  64. $this->processProducts($productFamily, $clone);
  65. $this->processPrice($productFamily);
  66. return $productFamily;
  67. }
  68. protected function processReductionCatalog($entity, $editForm)
  69. {
  70. $reductionCatalog = $editForm->get('reductionCatalog')->getData();
  71. if ($reductionCatalog instanceof ReductionCatalogInterface) {
  72. if ($reductionCatalog->getValue() && $reductionCatalog->getBehaviorTaxRate() && $reductionCatalog->getUnit()) {
  73. $reductionCatalog->setMerchant($entity->getMerchant());
  74. $reductionCatalog->setStatus($editForm->get('activeReductionCatalog')->getData());
  75. $reductionCatalog->setProductFamily($entity);
  76. $this->em->persist($reductionCatalog);
  77. }
  78. }
  79. }
  80. protected function processPrice($entity)
  81. {
  82. if ($entity->getBehaviorPrice() == 'by-piece') {
  83. $entity->setPriceByRefUnit(null);
  84. $entity->setBuyingPriceByRefUnit(null);
  85. } else if ($entity->getBehaviorPrice() == 'by-reference-unit') {
  86. $entity->setPrice(null);
  87. $entity->setBuyingPrice(null);
  88. }
  89. }
  90. protected function processProducts($entity, $clone = false)
  91. {
  92. if($clone) {
  93. foreach ($entity->getProducts() as $i => $product) {
  94. $newProduct = clone $product;
  95. $newProduct->setProductFamily($entity);
  96. $this->em->persist($newProduct);
  97. $entity->addProduct($newProduct);
  98. }
  99. }else {
  100. //Récupère le product origin
  101. $originProducts = $this->em->getRepository(ProductInterface::class)->findBy(array(
  102. 'productFamily' => $entity->getId(),
  103. 'originProduct' => true
  104. ));
  105. if (count($originProducts) > 1) {
  106. throw new \ErrorException('Plusieurs OriginProduct pour un même produit... Contacter fab');
  107. // Case Nouveau product family
  108. } else if (count($originProducts) == 0) {
  109. $originProduct = new Product();
  110. $originProduct->setProductFamily($entity);
  111. $originProduct->setOriginProduct(true);
  112. $entity->addProduct($originProduct);
  113. } else {
  114. $originProduct = $originProducts[0];
  115. }
  116. if ($entity->getActiveProducts()) {
  117. $originProduct->setStatus(-1);
  118. } else {
  119. $originProduct->setStatus(1);
  120. }
  121. //Enregistrement
  122. $entity->addProduct($originProduct);
  123. foreach ($entity->getProducts() as $product) {
  124. $product->setProductFamily($entity);
  125. $this->em->persist($product);
  126. $entity->addProduct($product);
  127. }
  128. }
  129. }
  130. protected function processCategories(ProductFamilyInterface $entity)
  131. {
  132. $productCategoryRepository = $this->em->getRepository(ProductCategoryInterface::class);
  133. $productCategories = $entity->getProductCategories();
  134. $entity->initProductCategories();
  135. foreach ($productCategories as $key => $bool) {
  136. if (is_bool($bool) && $bool) {
  137. if (strpos($key, 'category_children_') !== false) {
  138. $idCategory = (int)str_replace('category_children_', '', $key);
  139. } else {
  140. $idCategory = (int)str_replace('category_', '', $key);
  141. }
  142. $category = $productCategoryRepository->find($idCategory);
  143. $entity->addProductCategory($category);
  144. }
  145. }
  146. }
  147. }