src/Services/EnrollmentService.php line 158

Open in your IDE?
  1. <?php
  2. namespace EADPlataforma\Services;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use EADPlataforma\Entity\Enrollment;
  5. use EADPlataforma\Entity\Course;
  6. use EADPlataforma\Entity\CourseTeam;
  7. use EADPlataforma\Entity\Group;
  8. use EADPlataforma\Entity\Product;
  9. use EADPlataforma\Entity\ProductOffer;
  10. use EADPlataforma\Entity\Transaction;
  11. use EADPlataforma\Entity\Trash;
  12. use EADPlataforma\Entity\UserSubscription;
  13. use EADPlataforma\Entity\User;
  14. use EADPlataforma\Services\GeneralService;
  15. use EADPlataforma\Enum\EnrollmentEnum;
  16. use EADPlataforma\Enum\GroupEnum;
  17. use EADPlataforma\Enum\ProductEnum;
  18. use EADPlataforma\Enum\CourseEnum;
  19. use EADPlataforma\Enum\TagsMarketingEnum;
  20. use EADPlataforma\Enum\NotificationEnum;
  21. use EADPlataforma\Enum\TrashEnum;
  22. use EADPlataforma\Enum\UserPermissionEnum;
  23. use EADPlataforma\Enum\WebhookEnum;
  24. /**
  25.  * Enrollment Service
  26.  */
  27. class EnrollmentService
  28. {
  29.     /**
  30.      * @var SchoolEntityManager $em
  31.      */
  32.     protected $em;
  33.     /**
  34.      * @var GeneralService $generalService
  35.      */
  36.     protected $generalService;
  37.     /**
  38.      * @var ConfigurationService $configuration
  39.      */
  40.     protected $configuration;
  41.     /**
  42.      * @var Repository $repository
  43.      */
  44.     protected $repository;
  45.     /**
  46.      * @var EntityUtil $entityUtil
  47.      */
  48.     protected $entityUtil;
  49.     /**
  50.      * @var bool
  51.      */
  52.     protected $notification false;
  53.     /**
  54.      * @var bool
  55.      */
  56.     protected $email false;
  57.     /**
  58.      * @var int
  59.      */
  60.     protected $origin EnrollmentEnum::ORIGIN_FREE;
  61.     /**
  62.      * @var int|null
  63.      */
  64.     protected $certificate EnrollmentEnum::NO;
  65.     /**
  66.      * @var int|null
  67.      */
  68.     protected $accessPeriod;
  69.     /**
  70.      * @var int|null
  71.      */
  72.     protected $supportPeriod;
  73.     /**
  74.      * @var Date|null
  75.      */
  76.     protected $accessDate;
  77.     /**
  78.      * @var Date|null
  79.      */
  80.     protected $supportDate;
  81.     /**
  82.      * @var string|null
  83.      *
  84.      */
  85.     private $couponKey;
  86.     /**
  87.      * @var \Group|null
  88.      */
  89.     protected $group;
  90.     /**
  91.      * @var \UserSubscription|null
  92.      */
  93.     protected $userSubscription;
  94.     /**
  95.      * @var \Transaction|null
  96.      */
  97.     protected $transaction;
  98.     /**
  99.      * @var LogService
  100.      */
  101.     private $logService;
  102.     /**
  103.      * @var MarketingService $marketingService
  104.      */
  105.     protected $marketingService;
  106.     /**
  107.      * @var EmailService $emailService
  108.      */
  109.     protected $emailService;
  110.     /**
  111.      * @var NotificationService $notificationService
  112.      */
  113.     protected $notificationService;
  114.     /**
  115.      * @var int|null
  116.      */
  117.     private $deleted;
  118.     /**
  119.      * @var string
  120.      */
  121.     protected $lifetimeDate "9999-09-09 00:00:00";
  122.     /**
  123.      * @var Bool
  124.      */
  125.     protected $debug false;
  126.     /**
  127.      * Constructor
  128.      *
  129.      * @param GeneralService $generalService
  130.      */
  131.     public function __construct(GeneralService $generalService)
  132.     {
  133.         $this->generalService $generalService;
  134.         $this->configuration $this->generalService->getService('ConfigurationService');
  135.         $this->em $this->generalService->getService('SchoolEntityManager');
  136.         $this->logService $this->generalService->getService('LogService');
  137.         $this->repository $this->em->getRepository(Enrollment::class);
  138.         $this->entityUtil $this->generalService->getUtil('EntityUtil');
  139.         $this->marketingService $this->generalService->getService(
  140.             'Marketing\\MarketingService'
  141.         );
  142.         $this->emailService $this->generalService->getService('EmailService');
  143.         $this->notificationService $this->generalService->getService('NotificationService');
  144.     }
  145.     public function setNotification(bool $notification): self
  146.     {
  147.         $this->notification $notification;
  148.         return $this;
  149.     }
  150.     public function getNotification(): bool
  151.     {
  152.         return $this->notification;
  153.     }
  154.     public function setEmail(bool $email): self
  155.     {
  156.         $this->email $email;
  157.         return $this;
  158.     }
  159.     public function getEmail(): bool
  160.     {
  161.         return $this->email;
  162.     }
  163.     public function setOrigin(int $origin): self
  164.     {
  165.         $this->origin $origin;
  166.         return $this;
  167.     }
  168.     public function getOrigin(): int
  169.     {
  170.         return $this->origin;
  171.     }
  172.     public function setCertificate(?int $certificate): self
  173.     {
  174.         $this->certificate $certificate;
  175.         return $this;
  176.     }
  177.     public function getCertificate(): ?int
  178.     {
  179.         return $this->certificate;
  180.     }
  181.     public function setAccessPeriod(?int $accessPeriod): self
  182.     {
  183.         $this->accessPeriod $accessPeriod;
  184.         return $this;
  185.     }
  186.     public function getAccessPeriod(): ?int
  187.     {
  188.         return $this->accessPeriod;
  189.     }
  190.     public function setSupportPeriod(?int $supportPeriod): self
  191.     {
  192.         $this->supportPeriod $supportPeriod;
  193.         return $this;
  194.     }
  195.     public function getSupportPeriod(): ?int
  196.     {
  197.         return $this->supportPeriod;
  198.     }
  199.     public function setAccessDate($accessDate): self
  200.     {
  201.         $this->accessDate $accessDate;
  202.         return $this;
  203.     }
  204.     public function getAccessDate()
  205.     {
  206.         return $this->accessDate;
  207.     }
  208.     public function setSupportDate($supportDate): self
  209.     {
  210.         $this->supportDate $supportDate;
  211.         return $this;
  212.     }
  213.     public function getSupportDate()
  214.     {
  215.         return $this->supportDate;
  216.     }
  217.     public function setCouponKey(?string $couponKey): self
  218.     {
  219.         $this->couponKey $couponKey;
  220.         return $this;
  221.     }
  222.     public function getCouponKey(): ?string
  223.     {
  224.         return $this->couponKey;
  225.     }
  226.     public function setGroup(?Group $group): self
  227.     {
  228.         $this->group $group;
  229.         return $this;
  230.     }
  231.     public function getGroup(): ?Group
  232.     {
  233.         return $this->group;
  234.     }
  235.     public function setUserSubscription(?UserSubscription $userSubscription): self
  236.     {
  237.         $this->userSubscription $userSubscription;
  238.         
  239.         return $this;
  240.     }
  241.     public function getUserSubscription(): ?UserSubscription
  242.     {
  243.         
  244.         return $this->userSubscription;
  245.     }
  246.     public function setTransaction(?Transaction $transaction): self
  247.     {
  248.         $this->transaction $transaction;
  249.         return $this;
  250.     }
  251.     public function getTransaction(): ?Transaction
  252.     {
  253.         return $this->transaction;
  254.     }
  255.     public function restore(int $deleted): self
  256.     {
  257.         $this->deleted $deleted;
  258.         return $this;
  259.     }
  260.     public function isDelete(): int
  261.     {
  262.         return $this->deleted;
  263.     }
  264.     public function setDebug(?bool $debug)
  265.     {
  266.         $this->debug $debug;
  267.         return $this;
  268.     }
  269.     public function enrollUserByProduct(
  270.         User $user
  271.         Product $product
  272.         ?Course $course null,
  273.         ?array $courses null
  274.     )
  275.     {  
  276.         try {
  277.             if((empty($course) && empty($courses)) && $product->isTypeResource()){
  278.                 return;
  279.             }
  280.             if($product->getType() == ProductEnum::CERTIFICATE){
  281.                 $this->setCertificate(EnrollmentEnum::YES);
  282.             }else if($product->getType() == ProductEnum::PERIOD){
  283.                 $this->setAccessPeriod($product->getCycleItem()->getDays());
  284.             }else if($product->getType() == ProductEnum::SUPPORT){
  285.                 $this->setSupportPeriod($product->getCycleItem()->getDays());
  286.             }
  287.             
  288.             $processGeneralService $this->generalService->getService(
  289.                 'Transaction\\ProcessGeneral'
  290.             );
  291.             $processGeneralService->deleteUserLists($user$product);
  292.             if(empty($courses)){
  293.                 $courses $product->getCourse();
  294.                 if(!empty($courseItem)){
  295.                     $courses = [ $course ]; 
  296.                 }
  297.             }
  298.             foreach ($courses as $key => $courseItem){
  299.                 $this->enrollUser(
  300.                     $user
  301.                     $courseItem,
  302.                     false,
  303.                     $product->isTypeResource()
  304.                 );
  305.             }
  306.             
  307.         } catch (Exception $e) {
  308.             if($this->debug){
  309.                 $discordService $this->generalService->getService('DiscordService');
  310.                 $discordService->setChannel('debug');
  311.                 $discordService->setMessage($e->getMessage());
  312.                 $discordService->sendDiscord();
  313.             }
  314.         }
  315.     }
  316.     public function blockEnrollUserByProduct(
  317.         User $user
  318.         Product $product
  319.         ?Course $course null,
  320.         ?array $courses null
  321.     )
  322.     {
  323.         try {
  324.             if(empty($courses)){
  325.                 $courses $product->getCourse();
  326.                 if(!empty($courseItem)){
  327.                     $courses = [ $course ]; 
  328.                 }
  329.             }
  330.             
  331.             foreach ($courses as $key => $course){
  332.                 $this->revertEnroll($user$course$product);
  333.             }
  334.         } catch (Exception $e) {
  335.             if($this->debug){
  336.                 $discordService $this->generalService->getService('DiscordService');
  337.                 $discordService->setChannel('debug');
  338.                 $discordService->setMessage($e->getMessage());
  339.                 $discordService->sendDiscord();
  340.             }
  341.         }
  342.     }
  343.     public function revertEnroll(User $userCourse $courseProduct $product)
  344.     {
  345.         $enrollment $this->repository->findOneBy([ 
  346.             "user" => $user->getId(), 
  347.             "course" => $course->getId(),
  348.         ], [ "id" => "DESC" ]);
  349.         if(!$enrollment){
  350.             return;
  351.         }
  352.         if($product->getType() == ProductEnum::CERTIFICATE){
  353.             $enrollment->setCertificate(EnrollmentEnum::NO);
  354.         }else if($product->getType() == ProductEnum::PERIOD){
  355.             
  356.             $accessPeriod $course->getAccessPeriod();
  357.             $datePeriod date('Y-m-d H:i:s'strtotime("+ {$accessPeriod} day"));
  358.             $enrollment->setDatePeriod($datePeriod);
  359.         }else if($product->getType() == ProductEnum::SUPPORT){
  360.             
  361.             $supportPeriod $course->getSupportPeriod();
  362.             $dateSupport date('Y-m-d H:i:s'strtotime("+ {$supportPeriod} day"));
  363.             $enrollment->setDateSupport($dateSupport);
  364.         }else{
  365.             $enrollment->setStatus(EnrollmentEnum::STATUS_CANCELED);
  366.         }
  367.         $this->em->flush();
  368.         return;
  369.     }
  370.     public function getEnrollmentDates(Course $course)
  371.     {
  372.         $today date('Y-m-d H:i:s');
  373.         $accessPeriod $this->getAccessPeriod(); 
  374.         $supportPeriod $this->getSupportPeriod();
  375.         if(empty($accessPeriod)){
  376.             $accessPeriod $course->getAccessPeriod();
  377.         }
  378.         if(empty($supportPeriod)){
  379.             $supportPeriod $course->getSupportPeriod();
  380.         }
  381.         $datePeriod date('Y-m-d H:i:s'strtotime("+ {$accessPeriod} day"));
  382.         $dateSupport date('Y-m-d H:i:s'strtotime("+ {$supportPeriod} day"));
  383.         
  384.         if(
  385.             empty($this->userSubscription) && 
  386.             $course->getLifetimePeriod() == CourseEnum::YES
  387.         ){
  388.             $datePeriod $this->lifetimeDate;
  389.         }
  390.         if(
  391.             empty($this->userSubscription) && 
  392.             $course->getLifetimeSupport() == CourseEnum::YES
  393.         ){
  394.             $dateSupport $this->lifetimeDate;
  395.         }
  396.         $dateRelease $course->getDateRelease();
  397.         if($dateRelease $today){
  398.             $datePeriod strtotime("+ {$accessPeriod} day"strtotime($dateRelease));
  399.             $datePeriod date('Y-m-d H:i:s'$datePeriod);
  400.             
  401.             $dateSupport strtotime("+ {$supportPeriod} day"strtotime($dateRelease));
  402.             $dateSupport date('Y-m-d H:i:s'$dateSupport);
  403.         }
  404.         if(!empty($this->getAccessDate())){
  405.             $datePeriod $this->getAccessDate();
  406.             $datePeriod date('Y-m-d H:i:s'strtotime($datePeriod));
  407.         }
  408.         if(!empty($this->getSupportDate())){
  409.             $dateSupport $this->getSupportDate();
  410.             $dateSupport date('Y-m-d H:i:s'strtotime($dateSupport));
  411.         }
  412.         return (object)[
  413.             "datePeriod" => $datePeriod,
  414.             "dateSupport" => $dateSupport,
  415.         ];
  416.     }
  417.     public function enrollUserInSubscription(
  418.         Product $product
  419.         array $subscriptions,
  420.         ?array $courses = []
  421.     )
  422.     {
  423.         if (empty($courses)) {
  424.             $courses $product->getCourse();
  425.         }
  426.         $this->setOrigin(EnrollmentEnum::ORIGIN_SUBSCRIPTION);
  427.         $unlimitedCourse $this->configuration->checkModuleIsAbleOnPlan('unlimitedCourseFunction');
  428.         $courseTeamRepository $this->em->getRepository(CourseTeam::class);
  429.         $productOfferRepository $this->em->getRepository(ProductOffer::class);
  430.         $newArray = [];
  431.         foreach ($subscriptions as $userSubscription){
  432.             $user $userSubscription->getUser();
  433.             $this->setUserSubscription($userSubscription);
  434.             $this->setAccessDate($userSubscription->getDateNextPayment());
  435.             $this->setSupportDate($userSubscription->getDateNextPayment());
  436.             foreach ($courses as $course){
  437.                 try{
  438.                     //$this->enrollUser($user, $course);
  439.                     if(!$unlimitedCourse){
  440.                         $isInTeam $courseTeamRepository->userExistInCourseTeam(
  441.                             $course
  442.                             $user
  443.                         );
  444.                         if($course->getStatus() == CourseEnum::DRAFT && !$isInTeam){
  445.                             continue;
  446.                         }
  447.                     }
  448.                     $certificateOffer null;
  449.                     if(
  450.                         $course->getCertificate() == CourseEnum::YES && 
  451.                         $this->certificate == EnrollmentEnum::NO
  452.                     ){
  453.                         
  454.                         $certificateOffer $productOfferRepository->getProductOfferCertificateByCourse(
  455.                             $course
  456.                         );
  457.                     }
  458.                     $enrollment $this->repository->findOneBy([ 
  459.                         "user" => $user->getId(), 
  460.                         "course" => $course->getId(),
  461.                     ], [ "id" => "DESC" ]);
  462.                     $info $this->setEnrollmentUser(
  463.                         $user,
  464.                         $course,
  465.                         $enrollment,
  466.                         $certificateOffer
  467.                     );
  468.                     $new $info->new;
  469.                     $enrollment $info->enrollment;
  470.                     $dates $this->getEnrollmentDates($course);
  471.                     $datePeriod $dates->datePeriod;
  472.                     $dateSupport $dates->dateSupport;
  473.                     $oldDatePeriod $enrollment->getDatePeriod('Y-m-d H:i:s');
  474.                     $oldDateSupport $enrollment->getDateSupport('Y-m-d H:i:s');
  475.                     if($datePeriod $oldDatePeriod){
  476.                         $enrollment->setDatePeriod($datePeriod);
  477.                     }
  478.                     if($dateSupport $oldDateSupport){
  479.                         $enrollment->setDateSupport($dateSupport);
  480.                     }
  481.                     $errors $this->entityUtil->setEntity($enrollment)->validateEntity();
  482.                     if($errors){
  483.                         continue;
  484.                     }
  485.                     if($new){
  486.                         $this->em->persist($enrollment);
  487.                         $newArray[] = $enrollment;
  488.                     }
  489.                 } catch (Exception $e) {
  490.                     if($this->debug){
  491.                         $discordService $this->generalService->getService('DiscordService');
  492.                         $discordService->setChannel('debug');
  493.                         $discordService->setMessage($e->getMessage());
  494.                         $discordService->sendDiscord();
  495.                     }
  496.                 }
  497.             }
  498.         }
  499.         $this->em->flush();
  500.         foreach ($newArray as $key => $enrollment) {
  501.             $this->repository->sendWebhook($enrollment);
  502.             if($this->getNotification()){
  503.                 $this->sendNotification($enrollment);
  504.             }
  505.             if($this->getEmail()){
  506.                 $this->sendEmail($enrollment);
  507.             }
  508.             //$this->enrollmentMarketingTag($enrollment);
  509.         }
  510.     }
  511.     public function enrollUserInGroup(Group $group, ?array $users = [], ?array $courses = [])
  512.     {
  513.         if(empty($users)){
  514.             $users $group->getUser();
  515.         }
  516.         if (empty($courses)) {
  517.             $courses $group->getCourse();
  518.         }
  519.         $this->setOrigin(EnrollmentEnum::ORIGIN_GROUP);
  520.         $this->setGroup($group);
  521.         if($group->getTypeDateAccess() == GroupEnum::TYPE_DATE_FIXED){
  522.             $this->setAccessDate($group->getDateAccessConclusion());
  523.         }else if($group->getTypeDateAccess() == GroupEnum::TYPE_DATE_PERIOD) {
  524.             $this->setAccessPeriod($group->getDateAccessDays());
  525.         }
  526.         if($group->getTypeDateSupport() == GroupEnum::TYPE_DATE_FIXED){
  527.             $this->setSupportDate($group->getDateSupportConclusion());
  528.         }else if($group->getTypeDateSupport() == GroupEnum::TYPE_DATE_PERIOD) {
  529.             $this->setSupportPeriod($group->getDateSupportDays());
  530.         }
  531.         $unlimitedCourse $this->configuration->checkModuleIsAbleOnPlan('unlimitedCourseFunction');
  532.         $courseTeamRepository $this->em->getRepository(CourseTeam::class);
  533.         $productOfferRepository $this->em->getRepository(ProductOffer::class);
  534.         $newArray = [];
  535.         foreach ($users as $user){
  536.             foreach ($courses as $course){
  537.                 try{
  538.                     //$this->enrollUser($user, $course);
  539.                     if(!$unlimitedCourse){
  540.                         $isInTeam $courseTeamRepository->userExistInCourseTeam(
  541.                             $course
  542.                             $user
  543.                         );
  544.                         if($course->getStatus() == CourseEnum::DRAFT && !$isInTeam){
  545.                             continue;
  546.                         }
  547.                     }
  548.                     $certificateOffer null;
  549.                     if(
  550.                         $course->getCertificate() == CourseEnum::YES && 
  551.                         $this->certificate == EnrollmentEnum::NO
  552.                     ){
  553.                         
  554.                         $certificateOffer $productOfferRepository->getProductOfferCertificateByCourse(
  555.                             $course
  556.                         );
  557.                     }
  558.                     $enrollment $this->repository->findOneBy([ 
  559.                         "user" => $user->getId(), 
  560.                         "course" => $course->getId(),
  561.                     ], [ "id" => "DESC" ]);
  562.                     $info $this->setEnrollmentUser(
  563.                         $user,
  564.                         $course,
  565.                         $enrollment,
  566.                         $certificateOffer
  567.                     );
  568.                     $new $info->new;
  569.                     $enrollment $info->enrollment;
  570.                     $dates $this->getEnrollmentDates($course);
  571.                     $datePeriod $dates->datePeriod;
  572.                     $dateSupport $dates->dateSupport;
  573.                     $oldDatePeriod $enrollment->getDatePeriod('Y-m-d H:i:s');
  574.                     $oldDateSupport $enrollment->getDateSupport('Y-m-d H:i:s');
  575.                     if($datePeriod $oldDatePeriod){
  576.                         $enrollment->setDatePeriod($datePeriod);
  577.                     }
  578.                     if($dateSupport $oldDateSupport){
  579.                         $enrollment->setDateSupport($dateSupport);
  580.                     }
  581.                     $errors $this->entityUtil->setEntity($enrollment)->validateEntity();
  582.                     if($errors){
  583.                         continue;
  584.                     }
  585.                     if($new){
  586.                         $this->em->persist($enrollment);
  587.                         $newArray[] = $enrollment;
  588.                     }
  589.                 } catch (Exception $e) {
  590.                     if($this->debug){
  591.                         $discordService $this->generalService->getService('DiscordService');
  592.                         $discordService->setChannel('debug');
  593.                         $discordService->setMessage($e->getMessage());
  594.                         $discordService->sendDiscord();
  595.                     }
  596.                 }
  597.             }
  598.         }
  599.         $this->em->flush();
  600.         foreach ($newArray as $key => $enrollment) {
  601.             $this->repository->sendWebhook($enrollment);
  602.             if($this->getNotification()){
  603.                 $this->sendNotification($enrollment);
  604.             }
  605.             if($this->getEmail()){
  606.                 $this->sendEmail($enrollment);
  607.             }
  608.             //$this->enrollmentMarketingTag($enrollment);
  609.         }
  610.     }
  611.     public function setEnrollmentUser(
  612.         User $user,
  613.         Course $course,
  614.         ?Enrollment $enrollment null,
  615.         ?ProductOffer $certificateOffer null
  616.     ): object
  617.     {
  618.         $new false;
  619.         if(!$enrollment || $enrollment->isDeleted() || $enrollment->isOnTrash()){
  620.             $new true;
  621.             $enrollment = new Enrollment();
  622.         }else{
  623.             $this->repository->restore($enrollmentTrashEnum::ENROLLMENT);
  624.         }
  625.         $enrollment->setStatus(EnrollmentEnum::STATUS_ACTIVE);
  626.         $enrollment->setCouponKey($this->getCouponKey());
  627.         $enrollment->setGroup($this->getGroup());
  628.         $enrollment->setUserSubscription($this->getUserSubscription());
  629.         
  630.         $certificate $enrollment->getCertificate();
  631.         if($this->getTransaction()){
  632.             $enrollment->addTransaction($this->getTransaction());
  633.         }
  634.         
  635.         if($new){
  636.             $enrollment->setOrigin($this->getOrigin());
  637.             $enrollment->setUser($user);
  638.             $enrollment->setCourse($course);
  639.             $enrollment->setCourseCertificateTemplate(
  640.                 $course->getCourseCertificateTemplate()
  641.             );
  642.             $enrollment->setDateRegister(date('Y-m-d H:i:s'));
  643.             $certificate $course->getCertificate();
  644.             
  645.             if($certificateOffer){
  646.                 $productCertificate $certificateOffer->getProduct();
  647.                 $allowOrigin json_decode(
  648.                     $productCertificate->getCertificateEnrollmentOrigin()
  649.                 );
  650.                 $certificate EnrollmentEnum::NO;
  651.                 if(!empty($allowOrigin) && is_array($allowOrigin)){
  652.                     if(in_array($enrollment->getOrigin(), $allowOrigin)){
  653.                         $certificate EnrollmentEnum::YES;
  654.                     }
  655.                 }
  656.             }
  657.         }
  658.         if(!empty($this->certificate)){
  659.             $certificate $this->certificate;
  660.         }
  661.         $enrollment->setCertificate($certificate);
  662.         return (object)[
  663.             "new" => $new,
  664.             "enrollment" => $enrollment,
  665.         ];
  666.     }
  667.     public function enrollUser(
  668.         User $user
  669.         Course $course
  670.         ?bool $manual false
  671.         ?bool $onlyExist false
  672.     )
  673.     {
  674.         try{
  675.             if(!$this->configuration->checkModuleIsAbleOnPlan('unlimitedCourseFunction')){
  676.                 $courseTeamRepository $this->em->getRepository(CourseTeam::class);
  677.                 $isInTeam $courseTeamRepository->userExistInCourseTeam(
  678.                     $course
  679.                     $user
  680.                 );
  681.                 if($course->getStatus() == CourseEnum::DRAFT && !$isInTeam){
  682.                     return (object)[
  683.                         "errors" => [ "course" ],
  684.                         "enrollment" => null,
  685.                     ];
  686.                 }
  687.             }
  688.             $enrollment $this->repository->findOneBy([ 
  689.                 "user" => $user->getId(), 
  690.                 "course" => $course->getId(),
  691.             ], [ "id" => "DESC" ]);
  692.             if(
  693.                 $onlyExist && 
  694.                 (empty($enrollment) || $enrollment->isDeleted() || $enrollment->isOnTrash())
  695.             ){
  696.                 return;
  697.             }
  698.             
  699.             $certificateOffer null;
  700.             if(
  701.                 $course->getCertificate() == CourseEnum::YES && 
  702.                 $this->certificate == EnrollmentEnum::NO
  703.             ){
  704.                 $productOfferRepository $this->em->getRepository(ProductOffer::class);
  705.                 $certificateOffer $productOfferRepository->getProductOfferCertificateByCourse(
  706.                     $course
  707.                 );
  708.             }
  709.             $info $this->setEnrollmentUser(
  710.                 $user,
  711.                 $course,
  712.                 $enrollment,
  713.                 $certificateOffer
  714.             );
  715.             $new $info->new;
  716.             $enrollment $info->enrollment;
  717.             $dates $this->getEnrollmentDates($course);
  718.             $datePeriod $dates->datePeriod;
  719.             $dateSupport $dates->dateSupport;
  720.             $oldDatePeriod $enrollment->getDatePeriod('Y-m-d H:i:s');
  721.             $oldDateSupport $enrollment->getDateSupport('Y-m-d H:i:s');
  722.             if($datePeriod $oldDatePeriod){
  723.                 $enrollment->setDatePeriod($datePeriod);
  724.             }
  725.             if($dateSupport $oldDateSupport){
  726.                 $enrollment->setDateSupport($dateSupport);
  727.             }
  728.             $errors $this->entityUtil->setEntity($enrollment)->validateEntity();
  729.             if($errors){
  730.                 if($this->debug){
  731.                     $discordService $this->generalService->getService('DiscordService');
  732.                     $discordService->setChannel('debug');
  733.                     $discordService->setMessage(json_encode($errors));
  734.                     $discordService->sendDiscord();
  735.                 }
  736.                 return (object)[
  737.                     "errors" => $errors,
  738.                     "enrollment" => null,
  739.                 ];
  740.             }
  741.             if($new){
  742.                 $this->em->persist($enrollment);
  743.             }
  744.             $this->em->flush();
  745.             if($new){
  746.                 $this->repository->sendWebhook($enrollment);
  747.                 if($this->getNotification()){
  748.                     $this->sendNotification($enrollment);
  749.                 }
  750.                 if($this->getEmail()){
  751.                     $this->sendEmail($enrollment);
  752.                 }
  753.                 $this->logService->logInsert(
  754.                     "enrollment"
  755.                     $enrollment->getId(), 
  756.                     $enrollment->toReturn()
  757.                 );
  758.                 $this->enrollmentMarketingTag($enrollment$manual);
  759.             }else{
  760.                 $this->logService->logUpdate(
  761.                     "enrollment"
  762.                     $enrollment->getId(), 
  763.                     $enrollment->toReturn()
  764.                 );
  765.             }
  766.             return (object)[
  767.                 "errors" => false,
  768.                 "enrollment" => $enrollment,
  769.             ];
  770.         } catch (Exception $e) {
  771.             if($this->debug){
  772.                 $discordService $this->generalService->getService('DiscordService');
  773.                 $discordService->setChannel('debug');
  774.                 $discordService->setMessage($e->getMessage());
  775.                 $discordService->sendDiscord();
  776.             }
  777.         }
  778.         return (object)[
  779.             "errors" => true,
  780.             "enrollment" => false,
  781.         ];
  782.     }
  783.     public function sendNotification(Enrollment $enrollment)
  784.     {
  785.         $this->notificationService->create(
  786.             $enrollment->getUser(),
  787.             $enrollment->getCourse()->getUser(),
  788.             NotificationEnum::ORIGIN_ENROLLMENT_NEW,
  789.             $enrollment->getId()
  790.         );
  791.             
  792.         $this->notificationService->create(
  793.             $enrollment->getCourse()->getUser(),
  794.             $enrollment->getUser(),
  795.             NotificationEnum::ORIGIN_ENROLLMENT_NEW,
  796.             $enrollment->getId()
  797.         );
  798.     }
  799.     public function sendEmail(Enrollment $enrollment)
  800.     {
  801.         $user $enrollment->getUser();
  802.         if(!$this->emailService->checkUserToSend($user)){
  803.             return;
  804.         }
  805.         $this->emailService->setToEmail($user->getEmail());
  806.         $this->emailService->setToName($user->getName());
  807.         $subject1 $this->configuration->getLanguage('enrollment.subject1''email');
  808.         $subject2 $this->configuration->getLanguage('enrollment.subject2''email');
  809.         $client $this->configuration->getClient();
  810.         $course $enrollment->getCourse();
  811.         $subject $subject1." "$course->getTitle()." "$subject2;
  812.         $this->emailService->setSubject($subject);
  813.         $this->emailService->setData([
  814.             "userName" => $enrollment->getUser()->getName(),
  815.             "courseTitle" => $course->getTitle(),
  816.             "btnLink" => "https://{$client->getDomainPrimary()}/course/index/{$course->getId()}"
  817.         ]);
  818.         $this->emailService->setTemplateBody("enrollment");
  819.         $this->emailService->send();
  820.     }
  821.     public function enrollmentMarketingTag(Enrollment $enrollmentbool $manual false){
  822.         $userSubscription $enrollment->getUserSubscription();
  823.         $group $enrollment->getGroup();
  824.         $user $enrollment->getUser();
  825.         
  826.         if($manual){
  827.             $this->marketingService->setTag(TagsMarketingEnum::TAG_ENROLLMENT_MANUALLY);
  828.             $this->marketingService->setTextComplement($enrollment->getCourse()->getTitle());
  829.         }else if($enrollment->getOrigin() == EnrollmentEnum::ORIGIN_FREE){
  830.             $this->marketingService->setTag(TagsMarketingEnum::TAG_FREE_COURSE_ACCESS);
  831.             $this->marketingService->setTextComplement($enrollment->getCourse()->getTitle());
  832.         }else if($group){
  833.             $this->marketingService->setTag(TagsMarketingEnum::TAG_ENROLLMENT_GROUP);
  834.             $this->marketingService->setTextComplement($enrollment->getCourse()->getTitle());
  835.         }else if($enrollment->getCouponKey()){
  836.             $this->marketingService->setTag(TagsMarketingEnum::TAG_ENROLLMENT_COUPON);
  837.             $this->marketingService->setTextComplement($enrollment->getCouponKey());
  838.         }else if($userSubscription){
  839.             $this->marketingService->setTag(TagsMarketingEnum::TAG_ENROLLMENT_SUBSCRIPTION);
  840.             $this->marketingService->setTextComplement(
  841.                 $userSubscription->getProduct()->getTitle()
  842.             );
  843.         }
  844.         $this->marketingService->setUser($user);
  845.         $this->marketingService->send();
  846.     }
  847. }