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.

174 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\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. if($productFamily->getActiveProducts()) {
  43. $products = $productFamily->getProductsOnline()->getValues();
  44. if (count($products) > 0) {
  45. usort($products, $comparisonFunction);
  46. return $products[0];
  47. }
  48. }else{
  49. return $productFamily->getOriginProduct();
  50. }
  51. if ($returnSelfIfNotActiveProducts) {
  52. return $productFamily;
  53. }
  54. else {
  55. return false;
  56. }
  57. }
  58. public function processBeforePersistProductFamily($productFamily, $editForm=false, $clone =false){
  59. if($editForm){
  60. $this->processReductionCatalog($productFamily, $editForm);
  61. $this->processCategories($productFamily);
  62. }
  63. $this->processProducts($productFamily, $clone);
  64. $this->processPrice($productFamily);
  65. return $productFamily;
  66. }
  67. protected function processReductionCatalog($entity, $editForm)
  68. {
  69. $reductionCatalog = $editForm->get('reductionCatalog')->getData();
  70. if ($reductionCatalog instanceof ReductionCatalogInterface) {
  71. if ($reductionCatalog->getValue() && $reductionCatalog->getBehaviorTaxRate() && $reductionCatalog->getUnit()) {
  72. $reductionCatalog->setMerchant($entity->getMerchant());
  73. $reductionCatalog->setStatus($editForm->get('activeReductionCatalog')->getData());
  74. $reductionCatalog->setProductFamily($entity);
  75. $this->em->persist($reductionCatalog);
  76. }
  77. }
  78. }
  79. protected function processPrice($entity)
  80. {
  81. if ($entity->getBehaviorPrice() == 'by-piece') {
  82. $entity->setPriceByRefUnit(null);
  83. $entity->setBuyingPriceByRefUnit(null);
  84. } else if ($entity->getBehaviorPrice() == 'by-reference-unit') {
  85. $entity->setPrice(null);
  86. $entity->setBuyingPrice(null);
  87. }
  88. }
  89. protected function processProducts($entity, $clone = false)
  90. {
  91. if($clone) {
  92. foreach ($entity->getProducts() as $i => $product) {
  93. $newProduct = clone $product;
  94. $newProduct->setProductFamily($entity);
  95. $this->em->persist($newProduct);
  96. $entity->addProduct($newProduct);
  97. }
  98. }else {
  99. //Récupère le product origin
  100. $originProducts = $this->em->getRepository(ProductInterface::class)->findBy(array(
  101. 'productFamily' => $entity->getId(),
  102. 'originProduct' => true
  103. ));
  104. if (count($originProducts) > 1) {
  105. throw new \ErrorException('Plusieurs OriginProduct pour un même produit... Contacter fab');
  106. // Case Nouveau product family
  107. } else if (count($originProducts) == 0) {
  108. $originProduct = new Product();
  109. $originProduct->setProductFamily($entity);
  110. $originProduct->setOriginProduct(true);
  111. $entity->addProduct($originProduct);
  112. } else {
  113. $originProduct = $originProducts[0];
  114. }
  115. if ($entity->getActiveProducts()) {
  116. $originProduct->setStatus(-1);
  117. } else {
  118. $originProduct->setStatus(1);
  119. }
  120. //Enregistrement
  121. $entity->addProduct($originProduct);
  122. foreach ($entity->getProducts() as $product) {
  123. $product->setProductFamily($entity);
  124. $this->em->persist($product);
  125. $entity->addProduct($product);
  126. }
  127. }
  128. }
  129. protected function processCategories(ProductFamilyInterface $entity)
  130. {
  131. $productCategoryRepository = $this->em->getRepository(ProductCategoryInterface::class);
  132. $productCategories = $entity->getProductCategories();
  133. $entity->initProductCategories();
  134. foreach ($productCategories as $key => $bool) {
  135. if (is_bool($bool) && $bool) {
  136. if (strpos($key, 'category_children_') !== false) {
  137. $idCategory = (int)str_replace('category_children_', '', $key);
  138. } else {
  139. $idCategory = (int)str_replace('category_', '', $key);
  140. }
  141. $category = $productCategoryRepository->find($idCategory);
  142. $entity->addProductCategory($category);
  143. }
  144. }
  145. }
  146. }