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.

147 lines
6.3KB

  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\ReductionCatalogInterface;
  9. use Lc\ShopBundle\Model\ProductFamily;
  10. class ProductFamilyUtils
  11. {
  12. protected $priceUtils ;
  13. protected $em ;
  14. public function __construct(PriceUtilsInterface $priceUtils, EntityManagerInterface $em)
  15. {
  16. $this->priceUtils = $priceUtils ;
  17. $this->em = $em;
  18. }
  19. public function getCheapestProduct($productFamily)
  20. {
  21. $priceUtils = $this->priceUtils ;
  22. return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($priceUtils) {
  23. return $priceUtils->getPriceWithTaxAndReduction($a) > $priceUtils->getPriceWithTaxAndReduction($b) ;
  24. }, true);
  25. }
  26. public function getCheapestProductByRefUnit($productFamily)
  27. {
  28. $priceUtils = $this->priceUtils ;
  29. return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($priceUtils) {
  30. return $priceUtils->getPriceByRefUnitWithTaxAndReduction($a) > $priceUtils->getPriceByRefUnitWithTaxAndReduction($b) ;
  31. }, false);
  32. }
  33. public function getMostExpensiveProductByRefUnit($productFamily)
  34. {
  35. $priceUtils = $this->priceUtils ;
  36. return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($priceUtils) {
  37. return $priceUtils->getPriceByRefUnitWithTaxAndReduction($a) < $priceUtils->getPriceByRefUnitWithTaxAndReduction($b) ;
  38. }, false);
  39. }
  40. private function getCheapestOrMostExpensiveProduct($productFamily, $comparisonFunction, $returnSelfIfNotActiveProducts)
  41. {
  42. $products = $productFamily->getProductsOnline()->getValues() ;
  43. if (count($products) > 0) {
  44. usort($products, $comparisonFunction);
  45. return $products[0];
  46. }
  47. if ($returnSelfIfNotActiveProducts) {
  48. return $productFamily;
  49. }
  50. else {
  51. return false;
  52. }
  53. }
  54. public function processBeforePersistProductFamily($productFamily, $editForm=false, $clone =false){
  55. if($editForm){
  56. $this->processReductionCatalog($productFamily, $editForm);
  57. $this->processCategories($productFamily);
  58. }
  59. $this->processProducts($productFamily, $clone);
  60. $this->processPrice($productFamily);
  61. return $productFamily;
  62. }
  63. protected function processReductionCatalog($entity, $editForm)
  64. {
  65. $reductionCatalog = $editForm->get('reductionCatalog')->getData();
  66. if ($reductionCatalog instanceof ReductionCatalogInterface) {
  67. if ($reductionCatalog->getValue() && $reductionCatalog->getBehaviorTaxRate() && $reductionCatalog->getUnit()) {
  68. $reductionCatalog->setMerchant($entity->getMerchant());
  69. $reductionCatalog->setStatus($editForm->get('activeReductionCatalog')->getData());
  70. $reductionCatalog->setProductFamily($entity);
  71. $this->em->persist($reductionCatalog);
  72. }
  73. }
  74. }
  75. protected function processPrice($entity)
  76. {
  77. if ($entity->getBehaviorPrice() == 'by-piece') {
  78. $entity->setPriceByRefUnit(null);
  79. $entity->setBuyingPriceByRefUnit(null);
  80. } else if ($entity->getBehaviorPrice() == 'by-reference-unit') {
  81. $entity->setPrice(null);
  82. $entity->setBuyingPrice(null);
  83. }
  84. }
  85. protected function processProducts($entity, $clone = false)
  86. {
  87. //si il existe un et un seul produit pour ce product family n'ajoute rien supprime rien
  88. if (count($entity->getProducts()) == 0) {
  89. $product = new Product();
  90. $product->setProductFamily($entity);
  91. $this->em->persist($product);
  92. $entity->addProduct($product);
  93. } else {
  94. foreach ($entity->getProducts() as $i => $product) {
  95. if ($clone) {
  96. $newProduct = clone $product;
  97. $newProduct->setProductFamily($entity);
  98. $this->em->persist($newProduct);
  99. $entity->addProduct($newProduct);
  100. } else {
  101. $product->setProductFamily($entity);
  102. $this->em->persist($product);
  103. $entity->addProduct($product);
  104. }
  105. }
  106. }
  107. }
  108. protected function processCategories(ProductFamilyInterface $entity)
  109. {
  110. $productCategoryRepository = $this->em->getRepository(ProductCategoryInterface::class);
  111. $productCategories = $entity->getProductCategories();
  112. $entity->initProductCategories();
  113. foreach ($productCategories as $key => $bool) {
  114. if (is_bool($bool) && $bool) {
  115. if (strpos($key, 'category_children_') !== false) {
  116. $idCategory = (int)str_replace('category_children_', '', $key);
  117. } else {
  118. $idCategory = (int)str_replace('category_', '', $key);
  119. }
  120. $category = $productCategoryRepository->find($idCategory);
  121. $entity->addProductCategory($category);
  122. }
  123. }
  124. }
  125. }