Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

200 lines
8.7KB

  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. } else {
  55. return false;
  56. }
  57. }
  58. public function processBeforePersistProductFamily($productFamily, $editForm = false, $clone = false)
  59. {
  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 processCloneProduct($productFamily)
  91. {
  92. foreach ($productFamily->getProducts() as $i => $product) {
  93. $newProduct = clone $product;
  94. $newProduct->setProductFamily($productFamily);
  95. $this->em->persist($newProduct);
  96. $productFamily->addProduct($newProduct);
  97. }
  98. }
  99. protected function processProducts($entity, $clone = false)
  100. {
  101. if ($clone) {
  102. $this->processCloneProduct($entity);
  103. } else {
  104. //Récupère le product origin
  105. $originProducts = $this->em->getRepository(ProductInterface::class)->findBy(array(
  106. 'productFamily' => $entity->getId(),
  107. 'originProduct' => true
  108. ));
  109. if (count($originProducts) > 1) {
  110. throw new \ErrorException('Plusieurs OriginProduct pour un même produit... Contacter fab');
  111. // Case Nouveau product family
  112. } else if (count($originProducts) == 0) {
  113. $originProduct = new Product();
  114. $originProduct->setProductFamily($entity);
  115. $originProduct->setOriginProduct(true);
  116. $entity->addProduct($originProduct);
  117. } else {
  118. $originProduct = $originProducts[0];
  119. }
  120. if ($entity->getActiveProducts()) {
  121. $originProduct->setStatus(-1);
  122. } else {
  123. $originProduct->setStatus(1);
  124. }
  125. //Enregistrement
  126. $entity->addProduct($originProduct);
  127. foreach ($entity->getProducts() as $product) {
  128. $product->setProductFamily($entity);
  129. if ($entity->getProductsQuantityAsTitle() && $product->getStatus() >= 1) {
  130. $product->setTitle(str_replace('.', ',', $product->getQuantityInherited()) . $product->getUnitInherited()->getWording());
  131. }
  132. $this->em->persist($product);
  133. $entity->addProduct($product);
  134. }
  135. }
  136. }
  137. protected function processCategories(ProductFamilyInterface $entity)
  138. {
  139. $productCategoryRepository = $this->em->getRepository(ProductCategoryInterface::class);
  140. $productCategories = $entity->getProductCategories();
  141. $entity->initProductCategories();
  142. foreach ($productCategories as $key => $bool) {
  143. if (is_bool($bool) && $bool) {
  144. if (strpos($key, 'category_children_') !== false) {
  145. $idCategory = (int)str_replace('category_children_', '', $key);
  146. } else {
  147. $idCategory = (int)str_replace('category_', '', $key);
  148. }
  149. $category = $productCategoryRepository->find($idCategory);
  150. $entity->addProductCategory($category);
  151. }
  152. }
  153. }
  154. public function getMultiplyingFactor($productFamily)
  155. {
  156. if($productFamily->getBehaviorPrice() == ProductFamily::BEHAVIOR_PRICE_BY_PIECE) {
  157. if($productFamily->getBuyingPrice() > 0) {
  158. return number_format($this->priceUtils->getPriceWithTax($productFamily) / $productFamily->getBuyingPrice(), 3) ;
  159. }
  160. }
  161. elseif($productFamily->getBehaviorPrice() == ProductFamily::BEHAVIOR_PRICE_BY_REFERENCE_UNIT) {
  162. if($productFamily->getBuyingPriceByRefUnit() > 0) {
  163. return number_format($this->priceUtils->getPriceByRefUnitWithTax($productFamily) / $productFamily->getBuyingPriceByRefUnit(), 3) ;
  164. }
  165. }
  166. }
  167. }