src/EventSubscriber/SoldeSynchronizer.php line 193

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Demande;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\Workflow\Event\Event;
  7. class SoldeSynchronizer implements EventSubscriberInterface
  8. {
  9.     protected $em;
  10.     public function __construct(EntityManagerInterface $em)
  11.     {
  12.         $this->em $em;
  13.     }
  14.     public function onAccepterConge(Event $event)
  15.     {
  16.         if ($event->getSubject()->getCurrentPlace() == 'acceptee' && $event->getSubject()->getType() === 'Congé-Annuel')
  17.         {
  18.             $day1CY = new \DateTime();
  19.             $day1CY->setDate($day1CY->format('Y'), 11);
  20.             $dayMidCY = new \DateTime();
  21.             $dayMidCY->setDate($dayMidCY->format('Y'), 630);
  22.             $dayFinCY = new \DateTime();
  23.             $dayFinCY->setDate($dayFinCY->format('Y'), 1231);
  24.             if (($event->getSubject()->getDateDeb() >= $day1CY) && ($event->getSubject()->getDateFin() <= $dayMidCY))
  25.             {
  26.                 // Sem 1 e.g.; from 04-04-2019 to 08-04-2019
  27.                 $soldeOld $this->em->getRepository('App:Solde')
  28.                     ->findOneBy([
  29.                     'Annee'   => intval((new \DateTime())->format('Y')) - 1,
  30.                     'Employe' => $event->getSubject()->getDemandeur()
  31.                 ]);
  32.                 
  33.                 if ($soldeOld->suffit($event->getSubject()->getNombreJours()))
  34.                 {   
  35.                     $soldeOld->prend($event->getSubject()->getNombreJours());
  36.                     $event->getSubject()->setPriseAncien($event->getSubject()->getNombreJours());
  37.                     $event->getSubject()->setPriseNouveau(0);
  38.                 }
  39.                 else
  40.                 {
  41.                     $nbPrise $event->getSubject()->getNombreJours();
  42.                     $nbPrise -= $soldeOld->getRestant();
  43.                     $event->getSubject()->setPriseAncien($soldeOld->getRestant());
  44.                     $soldeOld->prend($soldeOld->getRestant());
  45.                     $soldeNew $this->em->getRepository('App:Solde')
  46.                         ->findOneBy([
  47.                         'Annee'   => (new \DateTime())->format('Y'),
  48.                         'Employe' => $event->getSubject()->getDemandeur()
  49.                     ]);
  50.                     $event->getSubject()->setPriseNouveau($nbPrise);
  51.                     $soldeNew->prend($nbPrise);
  52.                 }
  53.             }
  54.             elseif (($event->getSubject()->getDateDeb() >= $day1CY) && ($event->getSubject()->getDateDeb() <= $dayMidCY) && ($event->getSubject()->getDateFin() > $dayMidCY) && ($event->getSubject()->getDateFin() <= $dayFinCY)
  55.             )
  56.             {
  57.                 // cas particulier : OY + CY e.g.; from 30-06-2019 to 05-07-2019
  58.                 $diff    $event->getSubject()->getDateDeb()->diff($dayMidCY);
  59.                 $nbAvant = ($diff->format('%a') * 1) + 1;
  60.                 if ($event->getSubject()->getHeureDeb() == '13:00')
  61.                 {
  62.                     $nbAvant -= 0.5;
  63.                 }
  64.                 $diff    $dayMidCY->diff($event->getSubject()->getDateFin());
  65.                 $nbApres = ($diff->format('%a') * 1) + 1;
  66.                 if ($event->getSubject()->getHeureFin() == '12:00')
  67.                 {
  68.                     $nbApres -= 0.5;
  69.                 }
  70.                 $soldeOld $this->em->getRepository('App:Solde')
  71.                     ->findOneBy([
  72.                     'Annee'   => intval((new \DateTime())->format('Y')) - 1,
  73.                     'Employe' => $event->getSubject()->getDemandeur()
  74.                 ]);
  75.                 if ($soldeOld->suffit($nbAvant))
  76.                 {
  77.                     $event->getSubject()->setPriseAncien($nbAvant);
  78.                     $soldeOld->prend($nbAvant);
  79.                     $event->getSubject()->setPriseNouveau(0);
  80.                 }
  81.                 else
  82.                 {
  83.                     $nbPrise $nbAvant;
  84.                     $nbPrise -= $soldeOld->getRestant();
  85.                     $event->getSubject()->setPriseAncien($soldeOld->getRestant());
  86.                     $soldeOld->prend($soldeOld->getRestant());
  87.                     $soldeNew $this->em->getRepository('App:Solde')
  88.                         ->findOneBy([
  89.                         'Annee'   => (new \DateTime())->format('Y'),
  90.                         'Employe' => $event->getSubject()->getDemandeur()
  91.                     ]);
  92.                     $event->getSubject()->setPriseNouveau($nbPrise);
  93.                     $soldeNew->prend($nbPrise);
  94.                 }
  95.                 $soldeNew $this->em->getRepository('App:Solde')
  96.                     ->findOneBy([
  97.                     'Annee'   => (new \DateTime())->format('Y'),
  98.                     'Employe' => $event->getSubject()->getDemandeur()
  99.                 ]);
  100.                 $event->getSubject()->setPriseNouveau(floatval($event->getSubject()->getPriseNouveau() + $nbApres));
  101.                 $soldeNew->prend($nbApres);
  102.             }
  103.             elseif (($event->getSubject()->getDateDeb() > $dayMidCY) && ($event->getSubject()->getDateFin() <= $dayFinCY))
  104.             {
  105.                 // Current year Sem 2
  106.                 $solde $this->em->getRepository('App:Solde')
  107.                     ->findOneBy([
  108.                     'Annee'   => (new \DateTime())->format('Y'),
  109.                     'Employe' => $event->getSubject()->getDemandeur()
  110.                 ]);
  111.                 $event->getSubject()->setPriseNouveau($event->getSubject()->getNombreJours());
  112.                 $solde->prend($event->getSubject()->getNombreJours());
  113.             }
  114.             elseif (($event->getSubject()->getDateDeb() > $dayMidCY) && ($event->getSubject()->getDateDeb() <= $dayFinCY) && ($event->getSubject()->getDateFin() > $dayFinCY))
  115.             {
  116.                 // cas particulier : CY + FY e.g.; from 05-07-2019 to 08-07-2019
  117.                 $diff    $event->getSubject()->getDateDeb()->diff($dayFinCY);
  118.                 $nbAvant = ($diff->format('%a') * 1) + 1;
  119.                 if ($event->getSubject()->getHeureDeb() == '13:00')
  120.                 {
  121.                     $nbAvant -= 0.5;
  122.                 }
  123.                 $diff    $dayFinCY->diff($event->getSubject()->getDateFin());
  124.                 $nbApres = ($diff->format('%a') * 1) + 1;
  125.                 if ($event->getSubject()->getHeureFin() == '12:00')
  126.                 {
  127.                     $nbApres -= 0.5;
  128.                 }
  129.                 $soldeNew $this->em->getRepository('App:Solde')
  130.                     ->findOneBy([
  131.                     'Annee'   => (new \DateTime())->format('Y'),
  132.                     'Employe' => $event->getSubject()->getDemandeur()
  133.                 ]);
  134.                 $event->getSubject()->setPriseAncien($nbAvant);
  135.                 $soldeNew->prend($nbAvant);
  136.                 $soldeComing $this->em->getRepository('App:Solde')
  137.                     ->findOneBy([
  138.                     'Annee'   => intval((new \DateTime())->format('Y')) + 1,
  139.                     'Employe' => $event->getSubject()->getDemandeur()
  140.                 ]);
  141.                 $event->getSubject()->setPriseNouveau($nbApres);
  142.                 $soldeComing->prend($nbApres);
  143.             }
  144.             elseif ($event->getSubject()->getDateDeb() > $dayFinCY)
  145.             {
  146.                 // Année prochaine
  147.                 $solde $this->em->getRepository('App:Solde')
  148.                     ->findOneBy([
  149.                     'Annee'   => intval((new \DateTime())->format('Y')) + 1,
  150.                     'Employe' => $event->getSubject()->getDemandeur()
  151.                 ]);
  152.                 $event->getSubject()->setPriseNouveau($event->getSubject()->getNombreJours());
  153.                 $solde->prend($event->getSubject()->getNombreJours());
  154.             }
  155.             $this->em->flush();
  156.         }
  157.     }
  158.     public function onAccepterAnnulation(Event $event)
  159.     {
  160.         global $oldLeaveType;
  161.         if (($event->getSubject()->getCurrentPlace() != 'acceptee' && $event->getSubject()->getType() === 'Congé-Annuel') || $oldLeaveType === 'Congé-Annuel' || ($event->getSubject()->getType() != 'Congé-Annuel' && $oldLeaveType === 'Congé-Annuel'))
  162.         {
  163.             $day1CY = new \DateTime();
  164.             $day1CY->setDate($day1CY->format('Y'), 11);
  165.             $dayMidCY = new \DateTime();
  166.             $dayMidCY->setDate($dayMidCY->format('Y'), 630);
  167.             $dayFinCY = new \DateTime();
  168.             $dayFinCY->setDate($dayFinCY->format('Y'), 1231);
  169.             if (($event->getSubject()->getDateDeb() >= $day1CY) && ($event->getSubject()->getDateFin() <= $dayMidCY))
  170.             {
  171.                 // Sem 1
  172.                 $soldeOld $this->em->getRepository('App:Solde')
  173.                     ->findOneBy([
  174.                     'Annee'   => intval((new \DateTime())->format('Y')) - 1,
  175.                     'Employe' => $event->getSubject()->getDemandeur()
  176.                 ]);
  177.                 $soldeOld->rendre(floatval($event->getSubject()->getPriseAncien()));
  178.                 $soldeNew $this->em->getRepository('App:Solde')
  179.                     ->findOneBy([
  180.                     'Annee'   => (new \DateTime())->format('Y'),
  181.                     'Employe' => $event->getSubject()->getDemandeur()
  182.                 ]);
  183.                 $soldeNew->rendre(floatval($event->getSubject()->getPriseNouveau()));
  184.             }
  185.             elseif (($event->getSubject()->getDateDeb() >= $day1CY) && ($event->getSubject()->getDateDeb() <= $dayMidCY) && ($event->getSubject()->getDateFin() > $dayMidCY) && ($event->getSubject()->getDateFin() <= $dayFinCY)
  186.             )
  187.             {
  188.                 // cas particulier : OY + CY
  189.                 $soldeOld $this->em->getRepository('App:Solde')
  190.                     ->findOneBy([
  191.                     'Annee'   => intval((new \DateTime())->format('Y')) - 1,
  192.                     'Employe' => $event->getSubject()->getDemandeur()
  193.                 ]);
  194.                 $soldeOld->rendre(floatval($event->getSubject()->getPriseAncien()));
  195.                 $soldeNew $this->em->getRepository('App:Solde')
  196.                     ->findOneBy([
  197.                     'Annee'   => (new \DateTime())->format('Y'),
  198.                     'Employe' => $event->getSubject()->getDemandeur()
  199.                 ]);
  200.                 $soldeNew->rendre(floatval($event->getSubject()->getPriseNouveau()));
  201.             }
  202.             elseif (($event->getSubject()->getDateDeb() > $dayMidCY) && ($event->getSubject()->getDateFin() <= $dayFinCY))
  203.             {
  204.                 // Sem 2
  205.                 $soldeNew $this->em->getRepository('App:Solde')
  206.                     ->findOneBy([
  207.                     'Annee'   => (new \DateTime())->format('Y'),
  208.                     'Employe' => $event->getSubject()->getDemandeur()
  209.                 ]);
  210.                 $soldeNew->rendre(floatval($event->getSubject()->getPriseNouveau()));
  211.             }
  212.             elseif (($event->getSubject()->getDateDeb() > $dayMidCY) && ($event->getSubject()->getDateDeb() <= $dayFinCY) && ($event->getSubject()->getDateFin() > $dayFinCY))
  213.             {
  214.                 // cas particulier : CY + FY
  215.                 $soldeOld $this->em->getRepository('App:Solde')
  216.                     ->findOneBy([
  217.                     'Annee'   => (new \DateTime())->format('Y'),
  218.                     'Employe' => $event->getSubject()->getDemandeur()
  219.                 ]);
  220.                 $soldeOld->rendre(floatval($event->getSubject()->getPriseAncien()));
  221.                 $soldeNew $this->em->getRepository('App:Solde')
  222.                     ->findOneBy([
  223.                     'Annee'   => intval((new \DateTime())->format('Y')) + 1,
  224.                     'Employe' => $event->getSubject()->getDemandeur()
  225.                 ]);
  226.                 $soldeNew->rendre(floatval($event->getSubject()->getPriseNouveau()));
  227.             }
  228.             elseif ($event->getSubject()->getDateDeb() > $dayFinCY)
  229.             {
  230.                 // Année prochaine
  231.                 $soldeNew $this->em->getRepository('App:Solde')
  232.                     ->findOneBy([
  233.                     'Annee'   => intval((new \DateTime())->format('Y')) + 1,
  234.                     'Employe' => $event->getSubject()->getDemandeur()
  235.                 ]);
  236.                 $soldeNew->rendre(floatval($event->getSubject()->getPriseNouveau()));
  237.             }
  238.             $this->em->flush();
  239.         }
  240.     }
  241.     /**
  242.      * @return array The event names to listen to
  243.      */
  244.     public static function getSubscribedEvents()
  245.     {
  246.         return [
  247.             'workflow.workflow_conge.completed.accepter'            => 'onAccepterConge',
  248.             'workflow.workflow_conge.completed.accepter_annulation' => 'onAccepterAnnulation',
  249.             'workflow.workflow_conge.completed.refuser' => 'onAccepterAnnulation'
  250.         ];
  251.     }
  252. }