src/EventSubscriber/Doctrine/SubscriptionSubscriber.php line 94

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Doctrine;
  3. use App\Enum\ActivityLogCategoryEnum;
  4. use App\Enum\ActivityLogMethodEnum;
  5. use App\Enum\OrganizationStatusPayedEnum;
  6. use App\Enum\ProductKeyEnum;
  7. use App\Enum\SubscriptionPackEnum;
  8. use App\Events\SubscriptionChangedEvent;
  9. use App\Repository\OrganizationRepository;
  10. use App\Repository\SubscriptionModelRepository;
  11. use App\Repository\SubscriptionRepository;
  12. use App\Service\OrganizationUtils;
  13. use App\Service\SegmentAPI;
  14. use App\Service\SubscriptionUtils;
  15. use App\Traits\SentryNotifyTrait;
  16. use Doctrine\Common\EventSubscriber;
  17. use Doctrine\ORM\EntityManagerInterface;
  18. use Doctrine\ORM\Event\LifecycleEventArgs;
  19. use Doctrine\ORM\Event\OnFlushEventArgs;
  20. use Doctrine\ORM\Event\PreUpdateEventArgs;
  21. use Doctrine\ORM\UnitOfWork;
  22. use Evo\Infrastructure\MappingORM\ActivityLog;
  23. use Evo\Infrastructure\MappingORM\Organization;
  24. use Evo\Infrastructure\MappingORM\Product;
  25. use Evo\Infrastructure\MappingORM\Service;
  26. use Evo\Infrastructure\MappingORM\Subscription;
  27. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  28. use Symfony\Component\HttpFoundation\Request;
  29. use Symfony\Component\HttpFoundation\RequestStack;
  30. use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
  31. use Symfony\Component\Security\Core\Security;
  32. use Symfony\Component\Security\Core\User\UserInterface;
  33. class SubscriptionSubscriber implements EventSubscriber
  34. {
  35.     use SentryNotifyTrait;
  36.     public const SERVICE_NAME '[ SUBSCRIPTION SUBSCRIBER ] ::';
  37.     private RequestStack $requestStack;
  38.     private SegmentAPI $segment;
  39.     private OrganizationUtils $organizationUtils;
  40.     private EntityManagerInterface $em;
  41.     private SubscriptionUtils $subscriptionUtils;
  42.     private Security $security;
  43.     private EventDispatcherInterface $eventDispatcher;
  44.     private SubscriptionModelRepository $subscriptionModelRepository;
  45.     private EntityManagerInterface $entityManager;
  46.     public function __construct(
  47.         RequestStack $requestStack,
  48.         SegmentAPI $segmentAPI,
  49.         OrganizationUtils $organizationUtils,
  50.         SubscriptionUtils $subscriptionUtils,
  51.         Security $security,
  52.         EntityManagerInterface $em,
  53.         EventDispatcherInterface $eventDispatcher,
  54.         SubscriptionModelRepository $subscriptionModelRepository,
  55.         EntityManagerInterface $entityManager
  56.     ) {
  57.         $this->requestStack $requestStack;
  58.         $this->segment $segmentAPI;
  59.         $this->organizationUtils $organizationUtils;
  60.         $this->subscriptionUtils $subscriptionUtils;
  61.         $this->security $security;
  62.         $this->em $em;
  63.         $this->eventDispatcher $eventDispatcher;
  64.         $this->subscriptionModelRepository $subscriptionModelRepository;
  65.         $this->entityManager $entityManager;
  66.     }
  67.     public function getSubscribedEvents(): array
  68.     {
  69.         return [
  70.             'preUpdate',
  71.             'prePersist',
  72.             'postPersist',
  73.             'postUpdate',
  74.             'onFlush',
  75.         ];
  76.     }
  77.     private array $processedServices = [];
  78.     private bool $isFlushing false;
  79.     public function onFlush(OnFlushEventArgs $args): void
  80.     {
  81.         if ($this->isFlushing) {
  82.             return;
  83.         }
  84.         $this->isFlushing true;
  85.         $em $args->getEntityManager();
  86.         $uow $em->getUnitOfWork();
  87.         // Détecter les ajouts, mises à jour et suppressions
  88.         foreach ($uow->getScheduledEntityInsertions() as $entity) {
  89.             if ($entity instanceof Service) {
  90.                 $this->handleServiceInsertion($entity);
  91.             }
  92.         }
  93.         foreach ($uow->getScheduledEntityDeletions() as $entity) {
  94.             if ($entity instanceof Service) {
  95.                 $this->handleServiceDeletion($entity);
  96.             }
  97.         }
  98.         // Récupérer les entités Subscription modifiées
  99.         foreach ($uow->getScheduledEntityUpdates() as $entity) {
  100.             if ($entity instanceof Subscription) {
  101.                 if (!$entity->getOrganization()) {
  102.                     continue;
  103.                 }
  104.                 $this->checkServicesChanges($entity$em$uow);
  105.                 $this->checkSubscriptionChanges($entity$uow);
  106.             }
  107.         }
  108.         // Réinitialiser les services traités après flush
  109.         $this->processedServices = [];
  110.         $this->isFlushing false;
  111.     }
  112.     private function checkSubscriptionChanges(Subscription $subscriptionUnitOfWork $uow): void
  113.     {
  114.         // Récupérer les changements sur Subscription
  115.         $changes $uow->getEntityChangeSet($subscription);
  116.         $changeSet = [];
  117.         foreach ($changes as $field => $change) {
  118.             // On surveille les champs spécifiques : prix, type (fréquence), discount
  119.             if (in_array($field, ['priceWithoutTax''type''discountWithoutTax'], true)) {
  120.                 $oldValue $change[0] instanceof \DateTime $change[0]->format('Y-m-d H:i:s') : $change[0];
  121.                 $newValue $change[1] instanceof \DateTime $change[1]->format('Y-m-d H:i:s') : $change[1];
  122.                 // Si la valeur a changé, on l'ajoute au tableau des modifications
  123.                 if ($oldValue !== $newValue) {
  124.                     $changeSet[$field] = [
  125.                         $oldValue => $newValue,
  126.                     ];
  127.                 }
  128.             }
  129.         }
  130.         // Si des changements ont été détectés, on crée un log d'activité
  131.         if (!empty($changeSet)) {
  132.             try {
  133.                 $activityLog ActivityLog::create(
  134.                     $this->getUser(),
  135.                     ActivityLogMethodEnum::UPDATE,
  136.                     'Changements sur L\'abonnement',
  137.                     ActivityLogCategoryEnum::SUBSCRIPTION,
  138.                     $changeSet,
  139.                     $subscription->getOrganization(),
  140.                     null,
  141.                     null
  142.                 );
  143.                 $this->em->persist($activityLog);
  144.                 $this->em->flush();
  145.             } catch (\Exception $e) {
  146.                 $this->captureSentryException($e);
  147.             }
  148.         }
  149.     }
  150.     private function getUser(): ?UserInterface
  151.     {
  152.         $request $this->requestStack->getCurrentRequest();
  153.         $token $this->security->getToken();
  154.         $user null;
  155.         if ($request instanceof Request) {
  156.             if ($token instanceof SwitchUserToken) {
  157.                 $user $token->getOriginalToken()->getUser();
  158.             } else {
  159.                 $user $this->security->getUser();
  160.             }
  161.         }
  162.         return $user;
  163.     }
  164.     private function handleServiceInsertion(Service $service): void
  165.     {
  166.         if (null === $service->getSubscription() || null === $service->getSubscription()->getOrganization()) {
  167.             return;
  168.         }
  169.         try {
  170.             $activityLog ActivityLog::create(
  171.                 $this->getUser(),
  172.                 'CREATION',
  173.                 'Service ajouté: '.$service->getProduct()->getName(),
  174.                 ActivityLogCategoryEnum::SUBSCRIPTION,
  175.                 [
  176.                     'Type' => ['' => $service->getType()],
  177.                     'Prix hors taxes' => ['' => $service->getPriceWithoutTax()],
  178.                     'Période d\'essai' => ['' => $service->isTrial()],
  179.                     'Retirer après l\'essai' => ['' => $service->isRemoveAfterTrial()],
  180.                     'Durée de la période d\'essai' => ['' => $service->getTrialPeriodLength()],
  181.                 ],
  182.                 $service->getSubscription()->getOrganization(),
  183.                 null,
  184.                 null
  185.             );
  186.             $this->em->persist($activityLog);
  187.             $this->em->flush();
  188.         } catch (\Exception $e) {
  189.             $this->captureSentryException($e);
  190.         }
  191.     }
  192.     private function handleServiceDeletion(Service $service): void
  193.     {
  194.         if (null === $service->getSubscription() || null === $service->getSubscription()->getOrganization()) {
  195.             return;
  196.         }
  197.         try {
  198.             $activityLog ActivityLog::create(
  199.                 $this->getUser(),
  200.                 ActivityLogMethodEnum::UPDATE,
  201.                 'Service supprimé: '.$service->getProduct()->getName(),
  202.                 ActivityLogCategoryEnum::SUBSCRIPTION,
  203.                 [
  204.                     'Type' => [$service->getType() => '-'],
  205.                     'Prix hors taxes' => [$service->getPriceWithoutTax() => '-'],
  206.                     'Période d\'essai' => [$service->isTrial() => '-'],
  207.                     'Retirer après l\'essai' => [$service->isRemoveAfterTrial() => '-'],
  208.                     'Durée de la période d\'essai' => [$service->getTrialPeriodLength() => '-'],
  209.                 ],
  210.                 $service->getSubscription()->getOrganization(),
  211.                 null,
  212.                 null
  213.             );
  214.             $this->em->persist($activityLog);
  215.             $this->em->flush();
  216.         } catch (\Exception $e) {
  217.             $this->captureSentryException($e);
  218.         }
  219.     }
  220.     private function checkServicesChanges(Subscription $subscription$emUnitOfWork $uow): void
  221.     {
  222.         // Récupérer les services associés à l'abonnement
  223.         $services $subscription->getServices();
  224.         foreach ($services as $service) {
  225.             $serviceId $service->getId();
  226.             $serviceName $service->getProduct()->getName();
  227.             // Vérifier si le service a déjà été traité
  228.             if (in_array($serviceId$this->processedServicestrue)) {
  229.                 // Si ce service a déjà été traité, passer au suivant
  230.                 continue;
  231.             }
  232.             // Ajouter ce service à la liste des services déjà traités
  233.             $this->processedServices[] = $serviceId;
  234.             // Vérifier si le service a été modifié
  235.             if ($uow->isScheduledForUpdate($service)) {
  236.                 // Récupérer les changements sur le service
  237.                 $changes $uow->getEntityChangeSet($service);
  238.                 $changeSet = [];
  239.                 foreach ($changes as $field => $change) {
  240.                     if (Service::INCLUDED === $service->getType() && 'priceWithoutTax' === $field) {
  241.                         continue;
  242.                     }
  243.                     $oldValue $change[0] instanceof \DateTime $change[0]->format('Y-m-d H:i:s') : $change[0];
  244.                     $newValue $change[1] instanceof \DateTime $change[1]->format('Y-m-d H:i:s') : $change[1];
  245.                     if ($oldValue !== $newValue) {
  246.                         $changeSet[$field] = [
  247.                             $oldValue => $newValue,
  248.                         ];
  249.                     }
  250.                 }
  251.                 if (!empty($changeSet)) {
  252.                     $activityLog ActivityLog::create(
  253.                         $this->getUser(),
  254.                         ActivityLogMethodEnum::UPDATE,
  255.                         "Changements sur service $serviceName",
  256.                         ActivityLogCategoryEnum::SUBSCRIPTION,
  257.                         Service::mapFieldNamesToFrench($changeSet),
  258.                         $service->getSubscription()->getOrganization(),
  259.                         null,
  260.                         null
  261.                     );
  262.                     $this->em->persist($activityLog);
  263.                     $this->em->flush();
  264.                 }
  265.             }
  266.         }
  267.     }
  268.     public function postUpdate(LifecycleEventArgs $args): void
  269.     {
  270.         $entity $args->getEntity();
  271.         if (!$entity instanceof Subscription) {
  272.             return;
  273.         }
  274.         $this->em $args->getObjectManager();
  275.         $uow $args->getEntityManager()->getUnitOfWork();
  276.         $uow->computeChangeSets();
  277.         $changeset $uow->getEntityChangeSet($entity);
  278.         if (=== count($changeset) && isset($changeset['invoiceGenerationDate'])) {
  279.             return;
  280.         }
  281.         $hasBlockedService $this->subscriptionUtils->checkIfServiceShouldBeBlocked($entityOrganizationStatusPayedEnum::UNPAID === $entity->getOrganization()->getStatusInvoicePayed());
  282.         $this->subscriptionUtils->updateSubscriptionBlockedService($entity$hasBlockedService);
  283.         if (isset($changeset['hasBlockedService'])) {
  284.             $this->segment->trackSubscriptionServiceStatus($entity);
  285.         }
  286.         if (isset($changeset['documentRecoveryDelay'])) {
  287.             $this->segment->trackSubscriptionDocumentRecoveryDelay($entity);
  288.         }
  289.         $generateContrat = isset($changeset['priceWithoutTax'])
  290.             || isset($changeset['discountWithoutTax'])
  291.             || isset($changeset['services'])
  292.             || isset($changeset['subscriptionDeposit']);
  293.         if (null !== $entity->getOrganization() && null !== $entity->getPreviousPack()) {
  294.             $organization $entity->getOrganization();
  295.             /** @var OrganizationRepository $organizationRepository */
  296.             $organizationRepository $this->em->getRepository(Organization::class);
  297.             $organizationRepository->uptadeOrganizationPreviousPack($entity->getPreviousPack(), $organization->getId());
  298.         }
  299.         if ($generateContrat && !is_null($entity->getOrganization())) {
  300.             try {
  301.                 $this->organizationUtils->generateDocs($entity->getOrganization(), true);
  302.             } catch (\Exception $e) {
  303.                 $this->captureSentryException($e);
  304.             }
  305.         }
  306.         if (null !== $entity->getOrganization() && isset($changeset['packHistory'])) {
  307.             /** @var SubscriptionRepository $subscriptionRepository */
  308.             $subscriptionRepository $this->em->getRepository(Subscription::class);
  309.             $subscriptionStatus SubscriptionUtils::isActiveSubscription($entity);
  310.             $subscriptionRepository->updateSubscriptionStatus($entity$subscriptionStatus);
  311.         }
  312.         // create ctivity log for track the price
  313.         $this->trackSubscriptionPack($entity$changeset);
  314.         // Vérifier les changements sur les services
  315.         if (isset($changeset['services'])) {
  316.             $oldServices $changeset['services'][0];
  317.             $newServices $changeset['services'][1];
  318.             $activityLogChangeSet = [
  319.                 'SERVICES' => [
  320.                     'old' => $oldServices,
  321.                     'new' => $newServices,
  322.                 ],
  323.             ];
  324.             $request $this->requestStack->getCurrentRequest();
  325.             $token $this->security->getToken();
  326.             $user null;
  327.             if ($request instanceof Request) {
  328.                 if ($token instanceof SwitchUserToken) {
  329.                     $user $token->getOriginalToken()->getUser();
  330.                 } else {
  331.                     $user $this->security->getUser();
  332.                 }
  333.             }
  334.             $activityLog ActivityLog::create(
  335.                 $user,
  336.                 ActivityLogMethodEnum::UPDATE,
  337.                 'MODIFICATION SERVICES SUBSCRIPTION ID: '.$entity->getId(),
  338.                 ActivityLogCategoryEnum::SUBSCRIPTION,
  339.                 $activityLogChangeSet,
  340.                 $entity->getOrganization(),
  341.                 null,
  342.                 null
  343.             );
  344.             $this->em->persist($activityLog);
  345.             $this->em->flush();
  346.         }
  347.     }
  348.     public function preUpdate(PreUpdateEventArgs $args): void
  349.     {
  350.         $entity $args->getEntity();
  351.         if (!$entity instanceof Subscription) {
  352.             return;
  353.         }
  354.         $changeSet $args->getEntityChangeSet();
  355.         if (isset($changeSet['organization']) &&
  356.             null === $changeSet['organization'][1] &&
  357.             isset($_SESSION['remove_subscription'])
  358.         ) {
  359.             $organization $changeSet['organization'][0];
  360.             if ($organization) {
  361.                 $params SubscriptionUtils::getSubscriptionDataToSegment($organization);
  362.                 $documentRIB $this->organizationUtils->getOrganizationRIB($organization);
  363.                 if ($documentRIB) {
  364.                     $this->segment->trackApprovedDocument($documentRIB$params);
  365.                 }
  366.                 unset($_SESSION['remove_subscription']);
  367.             }
  368.         }
  369.         if (=== count($changeSet) && isset($changeSet['invoiceGenerationDate'])) {
  370.             return;
  371.         }
  372.         if (isset($changeSet['isCancelUsingOffer'])) {
  373.             $this->segment->identifyUpdateSubscription($entity$changeSet);
  374.         }
  375.         if (isset($changeSet['nextPayment'])) {
  376.             $this->segment->identifyNextPayment($entity);
  377.         }
  378.         if (null === $entity->getServices()) {
  379.             $args->getObjectManager()->remove($entity);
  380.         }
  381.         if ($this->requestStack->getCurrentRequest() && empty($this->requestStack->getCurrentRequest()->get('subscriptionPreUpdated'))) {
  382.             $this->requestStack->getCurrentRequest()->query->set('subscriptionPreUpdated'true);
  383.         }
  384.         $this->subscriptionUtils->addStoreAdditionStoreForDom($entity);
  385.         SubscriptionUtils::setPrice($entity);
  386.         if (null !== $entity->getOrganization()) {
  387.             /** @var Organization $organization */
  388.             $organization $entity->getOrganization();
  389.             $params SubscriptionUtils::getSubscriptionDataToSegment($organization);
  390.             $documentRIB $this->organizationUtils->getOrganizationRIB($organization);
  391.             if ($documentRIB && !isset($changeSet['isCancelUsingOffer'])) {
  392.                 $this->segment->trackApprovedDocument($documentRIB$params);
  393.             }
  394.         }
  395.     }
  396.     private function trackSubscriptionPack(Subscription $subscription, array $changetSet): void
  397.     {
  398.         $isCourrier $this->isCourrier();
  399.         /**
  400.          * Historique pack.
  401.          */
  402.         $pack $this->subscriptionUtils->getPackUniqueKey($subscription);
  403.         if ($pack) {
  404.             $histories $subscription->getPackHistory();
  405.             $histories[] = $pack;
  406.             $subscription->setPackHistory($histories);
  407.         }
  408.         $previousPack $subscription->getPreviousPack();
  409.         // reperer si le pack a changé
  410.         $packHasChanged $previousPack !== $pack;
  411.         // definir si c'est un upsell ou un downsell
  412.         if ($packHasChanged) {
  413.             $request $this->requestStack->getCurrentRequest();
  414.             $isFromAdmin false;
  415.             if ($request instanceof Request) {
  416.                 $requestContent json_decode($request->getContent(), true);
  417.                 if (isset($requestContent['isAdmin']) && $requestContent['isAdmin']) {
  418.                     $isFromAdmin true;
  419.                 }
  420.             }
  421.             if (!$isFromAdmin) {
  422.                 // check if store addition have the good price
  423.                 $this->checkIfStoreAdditionHaveTheGoodPrice($subscription);
  424.             }
  425.             $currentPack $this->subscriptionModelRepository->findOneBy(['uniqueKey' => $pack]);
  426.             $previousPack $this->subscriptionModelRepository->findOneBy(['uniqueKey' => $previousPack]);
  427.             if ($currentPack && $previousPack) {
  428.                 $currentPackCountServices $currentPack->getIncludedServices()->count();
  429.                 $previousPackCountServices $previousPack->getIncludedServices()->count();
  430.                 $isUpSell $currentPackCountServices $previousPackCountServices;
  431.                 if ($currentPackCountServices === $previousPackCountServices) {
  432.                     if (
  433.                         SubscriptionPackEnum::DIGIPACK === $previousPack->getUniqueKey()
  434.                         && SubscriptionPackEnum::DOMISCAN === $currentPack->getUniqueKey()
  435.                     ) {
  436.                         $isUpSell true;
  437.                     } elseif (
  438.                         SubscriptionPackEnum::DOMISCAN === $previousPack->getUniqueKey()
  439.                         && SubscriptionPackEnum::DIGIPACK === $currentPack->getUniqueKey()
  440.                     ) {
  441.                         $isUpSell false;
  442.                     }
  443.                 }
  444.                 $organization $subscription->getOrganization();
  445.                 /** @var ?UserInterface $user */
  446.                 $user $this->security->getUser();
  447.                 $agent null;
  448.                 $token $this->security->getToken();
  449.                 if ($token instanceof SwitchUserToken) {
  450.                     $user $agent $token->getOriginalToken()->getUser();
  451.                 } elseif ($user instanceof UserInterface && in_array('ROLE_ADMIN'$user->getRoles(), true)) {
  452.                     $agent $user;
  453.                 }
  454.                 if ($isUpSell) {
  455.                     $this->segment->trackUpsell($subscription$isCourrier);
  456.                     $event = new SubscriptionChangedEvent($subscription'UPSELL'$user$organization$agent$changetSet);
  457.                 } else {
  458.                     $this->segment->trackDownsell($subscription$isCourrier);
  459.                     $event = new SubscriptionChangedEvent($subscription'DOWNSELL'$user$organization$agent$changetSet);
  460.                 }
  461.                 /* remove reasonChange when user is admin before create log UPSELL/DOWNSELL */
  462.                 if ($isFromAdmin) {
  463.                     $organization->setReasonChange(null);
  464.                 }
  465.                 $this->segment->identifyCompany($subscription->getOrganization());
  466.                 $this->eventDispatcher->dispatch($eventSubscriptionChangedEvent::NAME);
  467.             }
  468.         } else {
  469.             $subscription->setStoredTotalPriceWithoutTax($subscription->getTotalPriceWithoutTax());
  470.             $this->entityManager->flush();
  471.         }
  472.     }
  473.     public function prePersist(LifecycleEventArgs $args): void
  474.     {
  475.         /** @var Subscription $entity */
  476.         $entity $args->getObject();
  477.         if (!$entity instanceof Subscription) {
  478.             return;
  479.         }
  480.         $this->em $args->getObjectManager();
  481.         SubscriptionUtils::setPrice($entity);
  482.         if (null !== $entity->getOrganization()) {
  483.             /** @var Organization $organization */
  484.             $organization $entity->getOrganization();
  485.             /**
  486.              * Historique pack.
  487.              */
  488.             $pack $this->subscriptionUtils->getPackUniqueKey($entity);
  489.             if ($pack) {
  490.                 $entity->setPackHistory([$pack]);
  491.             }
  492.             if (isset($_SESSION['hasChanged']) && == $_SESSION['hasChanged']) {
  493.                 $this->segment->identifyCompany($organization);
  494.                 unset($_SESSION['hasChanged'], $_SESSION['typeChanged']);
  495.             }
  496.         }
  497.         $this->subscriptionUtils->addStoreAdditionStoreForDom($entity);
  498.     }
  499.     public function postPersist(LifecycleEventArgs $args): void
  500.     {
  501.         /** @var Subscription $entity */
  502.         $entity $args->getEntity();
  503.         if (!$entity instanceof Subscription) {
  504.             return;
  505.         }
  506.         $this->em $args->getObjectManager();
  507.         /** @var Organization $organization */
  508.         $organization $entity->getOrganization();
  509.         if (!is_null($organization) && isset($_SESSION['add_subscription'])
  510.         ) {
  511.             try {
  512.                 $this->organizationUtils->generateDocs($organization);
  513.                 $this->segment->trackNewOrganization($organizationfalse);
  514.                 $params SubscriptionUtils::getSubscriptionDataToSegment($organization);
  515.                 $documentRIB $this->organizationUtils->getOrganizationRIB($organization);
  516.                 if ($documentRIB) {
  517.                     $this->segment->trackApprovedDocument($documentRIB$params);
  518.                 }
  519.                 unset($_SESSION['add_subscription']);
  520.             } catch (\Exception $e) {
  521.                 $this->captureSentryException($e);
  522.             }
  523.         }
  524.         if ($organization) {
  525.             /** @var SubscriptionRepository $subscriptionRepository */
  526.             $subscriptionRepository $this->em->getRepository(Subscription::class);
  527.             $subscriptionStatus SubscriptionUtils::isActiveSubscription($entity);
  528.             $subscriptionRepository->updateSubscriptionStatus($entity$subscriptionStatus);
  529.         }
  530.         try {
  531.             if ($entity->haveDomiciliation() && !$organization->getIsNewDomiciliation()) {
  532.                 $organization->setIsNewDomiciliation(true);
  533.                 $this->em->flush();
  534.             }
  535.         } catch (\Exception $e) {
  536.             $this->captureSentryException($e);
  537.         }
  538.         try {
  539.             $entity->setStoredTotalPriceWithoutTax($entity->getTotalPriceWithoutTax());
  540.             $this->em->flush();
  541.         } catch (\Exception $e) {
  542.             // do nothing
  543.         }
  544.     }
  545.     private function isCourrier(): bool
  546.     {
  547.         $listProducts = [];
  548.         if (isset($_SESSION['products'])) {
  549.             foreach ($_SESSION['products'] as $value) {
  550.                 /** @var Product $product */
  551.                 $product unserialize($value, [Product::class]);
  552.                 $listProducts[] = $product->getUniqueKey();
  553.             }
  554.             unset($_SESSION['products']);
  555.         }
  556.         return === count($listProducts) && ProductKeyEnum::SCAN_ENVELOPPE === $listProducts[0];
  557.     }
  558.     private function checkIfStoreAdditionHaveTheGoodPrice(Subscription $subscription): void
  559.     {
  560.         $organization $subscription->getOrganization();
  561.         if (null !== $organization) {
  562.             $store $organization->getStore();
  563.             $storeAdditionService $subscription->getServiceStoreAddition();
  564.             if (null !== $store && null !== $storeAdditionService) {
  565.                 $productStoreAddition $this->em->getRepository(Product::class)->findStoreAdditionForStoreName($store->getName());
  566.                 if (null !== $productStoreAddition) {
  567.                     $priceWithoutTax $productStoreAddition->getPriceWithoutTax();
  568.                     if (null !== $priceWithoutTax) {
  569.                         $storeAdditionService->setPriceWithoutTax($priceWithoutTax);
  570.                         $this->entityManager->persist($storeAdditionService);
  571.                     } else {
  572.                         $this->sendSentryMessage(self::SERVICE_NAME.'Product store addition price not found');
  573.                     }
  574.                 } else {
  575.                     $this->sendSentryMessage(self::SERVICE_NAME.'Product store addition not found');
  576.                 }
  577.             }
  578.         }
  579.     }
  580. }