|
|
@@ -0,0 +1,68 @@
|
|
|
+<?php
|
|
|
+declare(strict_types=1);
|
|
|
+
|
|
|
+namespace App\EventListener\Organization;
|
|
|
+
|
|
|
+use App\Entity\Billing\BillingSetting;
|
|
|
+use App\Entity\Organization\Organization;
|
|
|
+use App\Entity\Organization\Parameters;
|
|
|
+use App\Enum\Organization\LegalEnum;
|
|
|
+use App\EventListener\Helper;
|
|
|
+use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface;
|
|
|
+use Doctrine\ORM\EntityManagerInterface;
|
|
|
+use Doctrine\ORM\Event\OnFlushEventArgs;
|
|
|
+use Doctrine\ORM\Events;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Classe subscriber qui doit intervenir quand on flush une entité Organization
|
|
|
+ */
|
|
|
+class OrganizationChangedSubscriber implements EventSubscriberInterface
|
|
|
+{
|
|
|
+ use Helper;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * On souscrit à l'événement OnFlush
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function getSubscribedEvents(): array
|
|
|
+ {
|
|
|
+ return [
|
|
|
+ Events::onFlush
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * onFlush Event
|
|
|
+ * @param OnFlushEventArgs $onFlushEventArgs
|
|
|
+ */
|
|
|
+ public function onFlush(OnFlushEventArgs $onFlushEventArgs){
|
|
|
+ $entityManager = $onFlushEventArgs->getEntityManager();
|
|
|
+ $uow = $entityManager->getUnitOfWork();
|
|
|
+ foreach ($uow->getScheduledEntityUpdates() as $entityUpdate){
|
|
|
+ if($entityUpdate instanceof Organization){
|
|
|
+ //Si un update du field legalStatus est modifié
|
|
|
+ if($this->hasChangeField($entityManager, $entityUpdate, 'legalStatus'))
|
|
|
+ $this->handleLegalStatusChanged($entityUpdate, $entityManager);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Changement qui doivent être fait si le statut légale d'une structure est changé.
|
|
|
+ * @param Organization $organization
|
|
|
+ * @param EntityManagerInterface $entityManager
|
|
|
+ */
|
|
|
+ public function handleLegalStatusChanged(Organization $organization, EntityManagerInterface $entityManager){
|
|
|
+ //Si le status légal n'est pas "Association Loi 1901"
|
|
|
+ if($organization->getLegalStatus() !== LegalEnum::ASSOCIATION_LAW_1901()->getValue()){
|
|
|
+ $organization->getParameters()->setShowAdherentList(false);
|
|
|
+ $entityManager->getUnitOfWork()->computeChangeSet($entityManager->getClassMetadata(Parameters::class), $organization->getParameters());
|
|
|
+ }
|
|
|
+
|
|
|
+ //Si le status légal est "Société commerciale"
|
|
|
+ if($organization->getLegalStatus() === LegalEnum::COMMERCIAL_SOCIETY()->getValue()){
|
|
|
+ $organization->getBillingSetting()->setApplyVat(true);
|
|
|
+ $entityManager->getUnitOfWork()->computeChangeSet($entityManager->getClassMetadata(BillingSetting::class), $organization->getBillingSetting());
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|