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.

ProductFamilyUtils.php 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. if($entity->getProductsQuantityAsTitle() && $product->getStatus()>=1){
  126. $product->setTitle(str_replace('.', ',',$product->getQuantityInherited()).$product->getUnitInherited()->getWording());
  127. }
  128. $this->em->persist($product);
  129. $entity->addProduct($product);
  130. }
  131. }
  132. }
  133. protected function processCategories(ProductFamilyInterface $entity)
  134. {
  135. $productCategoryRepository = $this->em->getRepository(ProductCategoryInterface::class);
  136. $productCategories = $entity->getProductCategories();
  137. $entity->initProductCategories();
  138. foreach ($productCategories as $key => $bool) {
  139. if (is_bool($bool) && $bool) {
  140. if (strpos($key, 'category_children_') !== false) {
  141. $idCategory = (int)str_replace('category_children_', '', $key);
  142. } else {
  143. $idCategory = (int)str_replace('category_', '', $key);
  144. }
  145. $category = $productCategoryRepository->find($idCategory);
  146. $entity->addProductCategory($category);
  147. }
  148. }
  149. }
  150. }