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.

144 lines
4.9KB

  1. <?php
  2. namespace Lc\CaracoleBundle\EventSubscriber\Product;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
  5. use Lc\CaracoleBundle\Model\Product\ProductInterface;
  6. use Lc\CaracoleBundle\Model\Reduction\ReductionCatalogInterface;
  7. use Lc\SovBundle\Event\EntityManager\EntityManagerEvent;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class UpdateProductfamilyEventSubscriber implements EventSubscriberInterface
  10. {
  11. protected $em;
  12. protected $adminUrlGenerator;
  13. public function __construct(EntityManagerInterface $entityManager)
  14. {
  15. $this->em = $entityManager;
  16. }
  17. public static function getSubscribedEvents()
  18. {
  19. return [
  20. EntityManagerEvent::PRE_UPDATE_EVENT => ['processBeforePersistProductFamily'],
  21. EntityManagerEvent::PRE_CREATE_EVENT => ['processBeforePersistProductFamily'],
  22. ];
  23. }
  24. public function processBeforePersistProductFamily(EntityManagerEvent $event)
  25. {
  26. $entity = $event->getEntity();
  27. if($entity instanceof ProductFamilyInterface) {
  28. $this->processProducts($entity);
  29. $this->processPrice($entity);
  30. $this->processReductionCatalog($entity);
  31. }
  32. //TODO updatePriceByProductFamily
  33. }
  34. protected function processReductionCatalog($entity)
  35. {
  36. $reductionCatalog = $entity->getReductionCatalog();
  37. if ($reductionCatalog instanceof ReductionCatalogInterface) {
  38. if ($reductionCatalog->getValue() && $reductionCatalog->getBehaviorTaxRate() && $reductionCatalog->getUnit()) {
  39. //$reductionCatalog->setSection($entity->getSection());
  40. $reductionCatalog->setProductFamily($entity);
  41. if(is_null($reductionCatalog->getId())) {
  42. $this->em->create($reductionCatalog);
  43. }else {
  44. $this->em->update($reductionCatalog);
  45. }
  46. }
  47. }
  48. }
  49. protected function processPrice($entity)
  50. {
  51. if ($entity->getBehaviorPrice() == 'by-piece') {
  52. $entity->setPriceByRefUnit(null);
  53. $entity->setBuyingPriceByRefUnit(null);
  54. } else {
  55. if ($entity->getBehaviorPrice() == 'by-reference-unit') {
  56. $entity->setPrice(null);
  57. $entity->setBuyingPrice(null);
  58. }
  59. }
  60. }
  61. protected function processProducts($entity)
  62. {
  63. //Récupère le product origin
  64. $originProducts = $this->em->getRepository(ProductInterface::class)
  65. ->findBy(
  66. array(
  67. 'productFamily' => $entity->getId(),
  68. 'originProduct' => true,
  69. )
  70. );
  71. if (count($originProducts) > 1) {
  72. throw new \ErrorException('Plusieurs OriginProduct pour un même produit... Contacter fab');
  73. // Case Nouveau product family
  74. } else {
  75. if (count($originProducts) == 0) {
  76. $entityClassName = $this->em->getEntityName(ProductInterface::class);
  77. $originProduct = new $entityClassName();
  78. $originProduct->setProductFamily($entity);
  79. $originProduct->setOriginProduct(true);
  80. $entity->addProduct($originProduct);
  81. } else {
  82. $originProduct = $originProducts[0];
  83. }
  84. }
  85. if ($entity->getActiveProducts()) {
  86. $originProduct->setStatus(-1);
  87. } else {
  88. $originProduct->setStatus(1);
  89. }
  90. //Enregistrement
  91. $entity->addProduct($originProduct);
  92. foreach ($entity->getProducts() as $product) {
  93. $product->setProductFamily($entity);
  94. if ($entity->getProductsQuantityAsTitle() && $product->getStatus() >= 1) {
  95. $product->setTitle(
  96. str_replace('.', ',', $product->getQuantityInherited()).$product->getUnitInherited(
  97. )->getWording()
  98. );
  99. }
  100. $this->em->persist($product);
  101. $entity->addProduct($product);
  102. }
  103. }
  104. /* protected function processCategories(ProductFamilyInterface $entity)
  105. {
  106. $productCategoryRepository = $this->em->getRepository(ProductCategoryInterface::class);
  107. $productCategories = $entity->getProductCategories();
  108. $entity->initProductCategories();
  109. foreach ($productCategories as $key => $bool) {
  110. if (is_bool($bool) && $bool) {
  111. if (strpos($key, 'category_children_') !== false) {
  112. $idCategory = (int)str_replace('category_children_', '', $key);
  113. } else {
  114. $idCategory = (int)str_replace('category_', '', $key);
  115. }
  116. $category = $productCategoryRepository->find($idCategory);
  117. $entity->addProductCategory($category);
  118. }
  119. }
  120. }*/
  121. }