src/Services/EnrollmentService.php line 170

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.         ?bool $api false
  617.     ): object
  618.     {
  619.         $new false;
  620.         if(!$enrollment || ($api && ($enrollment->isDeleted() || $enrollment->isOnTrash()))){
  621.             $new true;
  622.             $enrollment = new Enrollment();
  623.         }else{
  624.             $this->repository->restore($enrollmentTrashEnum::ENROLLMENT);
  625.         }
  626.         $enrollment->setStatus(EnrollmentEnum::STATUS_ACTIVE);
  627.         $enrollment->setCouponKey($this->getCouponKey());
  628.         $enrollment->setGroup($this->getGroup());
  629.         $enrollment->setUserSubscription($this->getUserSubscription());
  630.         
  631.         $certificate $enrollment->getCertificate();
  632.         if($this->getTransaction()){
  633.             $enrollment->addTransaction($this->getTransaction());
  634.         }
  635.         
  636.         if($new){
  637.             $enrollment->setOrigin($this->getOrigin());
  638.             $enrollment->setUser($user);
  639.             $enrollment->setCourse($course);
  640.             $enrollment->setCourseCertificateTemplate(
  641.                 $course->getCourseCertificateTemplate()
  642.             );
  643.             $enrollment->setDateRegister(date('Y-m-d H:i:s'));
  644.             $certificate $course->getCertificate();
  645.             
  646.             if($certificateOffer){
  647.                 $productCertificate $certificateOffer->getProduct();
  648.                 $allowOrigin json_decode(
  649.                     $productCertificate->getCertificateEnrollmentOrigin()
  650.                 );
  651.                 $certificate EnrollmentEnum::NO;
  652.                 if(!empty($allowOrigin) && is_array($allowOrigin)){
  653.                     if(in_array($enrollment->getOrigin(), $allowOrigin)){
  654.                         $certificate EnrollmentEnum::YES;
  655.                     }
  656.                 }
  657.             }
  658.         }
  659.         if(!empty($this->certificate)){
  660.             $certificate $this->certificate;
  661.         }
  662.         $enrollment->setCertificate($certificate);
  663.         return (object)[
  664.             "new" => $new,
  665.             "enrollment" => $enrollment,
  666.         ];
  667.     }
  668.     public function enrollUser(
  669.         User $user
  670.         Course $course
  671.         ?bool $manual false
  672.         ?bool $onlyExist false,
  673.         ?bool $api false
  674.     )
  675.     {
  676.         try{
  677.             if(!$this->configuration->checkModuleIsAbleOnPlan('unlimitedCourseFunction')){
  678.                 $courseTeamRepository $this->em->getRepository(CourseTeam::class);
  679.                 $isInTeam $courseTeamRepository->userExistInCourseTeam(
  680.                     $course
  681.                     $user
  682.                 );
  683.                 if($course->getStatus() == CourseEnum::DRAFT && !$isInTeam){
  684.                     return (object)[
  685.                         "errors" => [ "course" ],
  686.                         "enrollment" => null,
  687.                     ];
  688.                 }
  689.             }
  690.             $enrollment $this->repository->findOneBy([ 
  691.                 "user" => $user->getId(), 
  692.                 "course" => $course->getId(),
  693.             ], [ "id" => "DESC" ]);
  694.             if(
  695.                 $onlyExist && 
  696.                 (empty($enrollment) || $enrollment->isDeleted() || $enrollment->isOnTrash())
  697.             ){
  698.                 return;
  699.             }
  700.             
  701.             $certificateOffer null;
  702.             if(
  703.                 $course->getCertificate() == CourseEnum::YES && 
  704.                 $this->certificate == EnrollmentEnum::NO
  705.             ){
  706.                 $productOfferRepository $this->em->getRepository(ProductOffer::class);
  707.                 $certificateOffer $productOfferRepository->getProductOfferCertificateByCourse(
  708.                     $course
  709.                 );
  710.             }
  711.             $info $this->setEnrollmentUser(
  712.                 $user,
  713.                 $course,
  714.                 $enrollment,
  715.                 $certificateOffer,
  716.                 $api
  717.             );
  718.             $new $info->new;
  719.             $enrollment $info->enrollment;
  720.             $dates $this->getEnrollmentDates($course);
  721.             $datePeriod $dates->datePeriod;
  722.             $dateSupport $dates->dateSupport;
  723.             $oldDatePeriod $enrollment->getDatePeriod('Y-m-d H:i:s');
  724.             $oldDateSupport $enrollment->getDateSupport('Y-m-d H:i:s');
  725.             if($datePeriod $oldDatePeriod){
  726.                 $enrollment->setDatePeriod($datePeriod);
  727.             }
  728.             if($dateSupport $oldDateSupport){
  729.                 $enrollment->setDateSupport($dateSupport);
  730.             }
  731.             $errors $this->entityUtil->setEntity($enrollment)->validateEntity();
  732.             if($errors){
  733.                 if($this->debug){
  734.                     $discordService $this->generalService->getService('DiscordService');
  735.                     $discordService->setChannel('debug');
  736.                     $discordService->setMessage(json_encode($errors));
  737.                     $discordService->sendDiscord();
  738.                 }
  739.                 return (object)[
  740.                     "errors" => $errors,
  741.                     "enrollment" => null,
  742.                 ];
  743.             }
  744.             if($new){
  745.                 $this->em->persist($enrollment);
  746.             }
  747.             $this->em->flush();
  748.             if($new){
  749.                 $this->repository->sendWebhook($enrollment);
  750.                 if($this->getNotification()){
  751.                     $this->sendNotification($enrollment);
  752.                 }
  753.                 if($this->getEmail()){
  754.                     $this->sendEmail($enrollment);
  755.                 }
  756.                 $this->logService->logInsert(
  757.                     "enrollment"
  758.                     $enrollment->getId(), 
  759.                     $enrollment->toReturn()
  760.                 );
  761.                 $this->enrollmentMarketingTag($enrollment$manual);
  762.             }else{
  763.                 $this->logService->logUpdate(
  764.                     "enrollment"
  765.                     $enrollment->getId(), 
  766.                     $enrollment->toReturn()
  767.                 );
  768.             }
  769.             return (object)[
  770.                 "errors" => false,
  771.                 "enrollment" => $enrollment,
  772.             ];
  773.         } catch (Exception $e) {
  774.             if($this->debug){
  775.                 $discordService $this->generalService->getService('DiscordService');
  776.                 $discordService->setChannel('debug');
  777.                 $discordService->setMessage($e->getMessage());
  778.                 $discordService->sendDiscord();
  779.             }
  780.         }
  781.         return (object)[
  782.             "errors" => true,
  783.             "enrollment" => false,
  784.         ];
  785.     }
  786.     public function sendNotification(Enrollment $enrollment)
  787.     {
  788.         $this->notificationService->create(
  789.             $enrollment->getUser(),
  790.             $enrollment->getCourse()->getUser(),
  791.             NotificationEnum::ORIGIN_ENROLLMENT_NEW,
  792.             $enrollment->getId()
  793.         );
  794.             
  795.         $this->notificationService->create(
  796.             $enrollment->getCourse()->getUser(),
  797.             $enrollment->getUser(),
  798.             NotificationEnum::ORIGIN_ENROLLMENT_NEW,
  799.             $enrollment->getId()
  800.         );
  801.     }
  802.     public function sendEmail(Enrollment $enrollment)
  803.     {
  804.         $user $enrollment->getUser();
  805.         if(!$this->emailService->checkUserToSend($user)){
  806.             return;
  807.         }
  808.         $this->emailService->setToEmail($user->getEmail());
  809.         $this->emailService->setToName($user->getName());
  810.         $subject1 $this->configuration->getLanguage('enrollment.subject1''email');
  811.         $subject2 $this->configuration->getLanguage('enrollment.subject2''email');
  812.         $client $this->configuration->getClient();
  813.         $course $enrollment->getCourse();
  814.         $subject $subject1." "$course->getTitle()." "$subject2;
  815.         $this->emailService->setSubject($subject);
  816.         $this->emailService->setData([
  817.             "userName" => $enrollment->getUser()->getName(),
  818.             "courseTitle" => $course->getTitle(),
  819.             "btnLink" => "https://{$client->getDomainPrimary()}/course/index/{$course->getId()}"
  820.         ]);
  821.         $this->emailService->setTemplateBody("enrollment");
  822.         $this->emailService->send();
  823.     }
  824.     public function enrollmentMarketingTag(Enrollment $enrollmentbool $manual false){
  825.         $userSubscription $enrollment->getUserSubscription();
  826.         $group $enrollment->getGroup();
  827.         $user $enrollment->getUser();
  828.         
  829.         if($manual){
  830.             $this->marketingService->setTag(TagsMarketingEnum::TAG_ENROLLMENT_MANUALLY);
  831.             $this->marketingService->setTextComplement($enrollment->getCourse()->getTitle());
  832.         }else if($enrollment->getOrigin() == EnrollmentEnum::ORIGIN_FREE){
  833.             $this->marketingService->setTag(TagsMarketingEnum::TAG_FREE_COURSE_ACCESS);
  834.             $this->marketingService->setTextComplement($enrollment->getCourse()->getTitle());
  835.         }else if($group){
  836.             $this->marketingService->setTag(TagsMarketingEnum::TAG_ENROLLMENT_GROUP);
  837.             $this->marketingService->setTextComplement($enrollment->getCourse()->getTitle());
  838.         }else if($enrollment->getCouponKey()){
  839.             $this->marketingService->setTag(TagsMarketingEnum::TAG_ENROLLMENT_COUPON);
  840.             $this->marketingService->setTextComplement($enrollment->getCouponKey());
  841.         }else if($userSubscription){
  842.             $this->marketingService->setTag(TagsMarketingEnum::TAG_ENROLLMENT_SUBSCRIPTION);
  843.             $this->marketingService->setTextComplement(
  844.                 $userSubscription->getProduct()->getTitle()
  845.             );
  846.         }
  847.         $this->marketingService->setUser($user);
  848.         $this->marketingService->send();
  849.     }
  850. }