<?php
namespace App\EventSubscriber;
use App\Entity\Demande;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Workflow\Event\Event;
class SoldeSynchronizer implements EventSubscriberInterface
{
protected $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
public function onAccepterConge(Event $event)
{
if ($event->getSubject()->getCurrentPlace() == 'acceptee' && $event->getSubject()->getType() === 'Congé-Annuel')
{
$day1CY = new \DateTime();
$day1CY->setDate($day1CY->format('Y'), 1, 1);
$dayMidCY = new \DateTime();
$dayMidCY->setDate($dayMidCY->format('Y'), 6, 30);
$dayFinCY = new \DateTime();
$dayFinCY->setDate($dayFinCY->format('Y'), 12, 31);
if (($event->getSubject()->getDateDeb() >= $day1CY) && ($event->getSubject()->getDateFin() <= $dayMidCY))
{
// Sem 1 e.g.; from 04-04-2019 to 08-04-2019
$soldeOld = $this->em->getRepository('App:Solde')
->findOneBy([
'Annee' => intval((new \DateTime())->format('Y')) - 1,
'Employe' => $event->getSubject()->getDemandeur()
]);
if ($soldeOld->suffit($event->getSubject()->getNombreJours()))
{
$soldeOld->prend($event->getSubject()->getNombreJours());
$event->getSubject()->setPriseAncien($event->getSubject()->getNombreJours());
$event->getSubject()->setPriseNouveau(0);
}
else
{
$nbPrise = $event->getSubject()->getNombreJours();
$nbPrise -= $soldeOld->getRestant();
$event->getSubject()->setPriseAncien($soldeOld->getRestant());
$soldeOld->prend($soldeOld->getRestant());
$soldeNew = $this->em->getRepository('App:Solde')
->findOneBy([
'Annee' => (new \DateTime())->format('Y'),
'Employe' => $event->getSubject()->getDemandeur()
]);
$event->getSubject()->setPriseNouveau($nbPrise);
$soldeNew->prend($nbPrise);
}
}
elseif (($event->getSubject()->getDateDeb() >= $day1CY) && ($event->getSubject()->getDateDeb() <= $dayMidCY) && ($event->getSubject()->getDateFin() > $dayMidCY) && ($event->getSubject()->getDateFin() <= $dayFinCY)
)
{
// cas particulier : OY + CY e.g.; from 30-06-2019 to 05-07-2019
$diff = $event->getSubject()->getDateDeb()->diff($dayMidCY);
$nbAvant = ($diff->format('%a') * 1) + 1;
if ($event->getSubject()->getHeureDeb() == '13:00')
{
$nbAvant -= 0.5;
}
$diff = $dayMidCY->diff($event->getSubject()->getDateFin());
$nbApres = ($diff->format('%a') * 1) + 1;
if ($event->getSubject()->getHeureFin() == '12:00')
{
$nbApres -= 0.5;
}
$soldeOld = $this->em->getRepository('App:Solde')
->findOneBy([
'Annee' => intval((new \DateTime())->format('Y')) - 1,
'Employe' => $event->getSubject()->getDemandeur()
]);
if ($soldeOld->suffit($nbAvant))
{
$event->getSubject()->setPriseAncien($nbAvant);
$soldeOld->prend($nbAvant);
$event->getSubject()->setPriseNouveau(0);
}
else
{
$nbPrise = $nbAvant;
$nbPrise -= $soldeOld->getRestant();
$event->getSubject()->setPriseAncien($soldeOld->getRestant());
$soldeOld->prend($soldeOld->getRestant());
$soldeNew = $this->em->getRepository('App:Solde')
->findOneBy([
'Annee' => (new \DateTime())->format('Y'),
'Employe' => $event->getSubject()->getDemandeur()
]);
$event->getSubject()->setPriseNouveau($nbPrise);
$soldeNew->prend($nbPrise);
}
$soldeNew = $this->em->getRepository('App:Solde')
->findOneBy([
'Annee' => (new \DateTime())->format('Y'),
'Employe' => $event->getSubject()->getDemandeur()
]);
$event->getSubject()->setPriseNouveau(floatval($event->getSubject()->getPriseNouveau() + $nbApres));
$soldeNew->prend($nbApres);
}
elseif (($event->getSubject()->getDateDeb() > $dayMidCY) && ($event->getSubject()->getDateFin() <= $dayFinCY))
{
// Current year Sem 2
$solde = $this->em->getRepository('App:Solde')
->findOneBy([
'Annee' => (new \DateTime())->format('Y'),
'Employe' => $event->getSubject()->getDemandeur()
]);
$event->getSubject()->setPriseNouveau($event->getSubject()->getNombreJours());
$solde->prend($event->getSubject()->getNombreJours());
}
elseif (($event->getSubject()->getDateDeb() > $dayMidCY) && ($event->getSubject()->getDateDeb() <= $dayFinCY) && ($event->getSubject()->getDateFin() > $dayFinCY))
{
// cas particulier : CY + FY e.g.; from 05-07-2019 to 08-07-2019
$diff = $event->getSubject()->getDateDeb()->diff($dayFinCY);
$nbAvant = ($diff->format('%a') * 1) + 1;
if ($event->getSubject()->getHeureDeb() == '13:00')
{
$nbAvant -= 0.5;
}
$diff = $dayFinCY->diff($event->getSubject()->getDateFin());
$nbApres = ($diff->format('%a') * 1) + 1;
if ($event->getSubject()->getHeureFin() == '12:00')
{
$nbApres -= 0.5;
}
$soldeNew = $this->em->getRepository('App:Solde')
->findOneBy([
'Annee' => (new \DateTime())->format('Y'),
'Employe' => $event->getSubject()->getDemandeur()
]);
$event->getSubject()->setPriseAncien($nbAvant);
$soldeNew->prend($nbAvant);
$soldeComing = $this->em->getRepository('App:Solde')
->findOneBy([
'Annee' => intval((new \DateTime())->format('Y')) + 1,
'Employe' => $event->getSubject()->getDemandeur()
]);
$event->getSubject()->setPriseNouveau($nbApres);
$soldeComing->prend($nbApres);
}
elseif ($event->getSubject()->getDateDeb() > $dayFinCY)
{
// Année prochaine
$solde = $this->em->getRepository('App:Solde')
->findOneBy([
'Annee' => intval((new \DateTime())->format('Y')) + 1,
'Employe' => $event->getSubject()->getDemandeur()
]);
$event->getSubject()->setPriseNouveau($event->getSubject()->getNombreJours());
$solde->prend($event->getSubject()->getNombreJours());
}
$this->em->flush();
}
}
public function onAccepterAnnulation(Event $event)
{
global $oldLeaveType;
if (($event->getSubject()->getCurrentPlace() != 'acceptee' && $event->getSubject()->getType() === 'Congé-Annuel') || $oldLeaveType === 'Congé-Annuel' || ($event->getSubject()->getType() != 'Congé-Annuel' && $oldLeaveType === 'Congé-Annuel'))
{
$day1CY = new \DateTime();
$day1CY->setDate($day1CY->format('Y'), 1, 1);
$dayMidCY = new \DateTime();
$dayMidCY->setDate($dayMidCY->format('Y'), 6, 30);
$dayFinCY = new \DateTime();
$dayFinCY->setDate($dayFinCY->format('Y'), 12, 31);
if (($event->getSubject()->getDateDeb() >= $day1CY) && ($event->getSubject()->getDateFin() <= $dayMidCY))
{
// Sem 1
$soldeOld = $this->em->getRepository('App:Solde')
->findOneBy([
'Annee' => intval((new \DateTime())->format('Y')) - 1,
'Employe' => $event->getSubject()->getDemandeur()
]);
$soldeOld->rendre(floatval($event->getSubject()->getPriseAncien()));
$soldeNew = $this->em->getRepository('App:Solde')
->findOneBy([
'Annee' => (new \DateTime())->format('Y'),
'Employe' => $event->getSubject()->getDemandeur()
]);
$soldeNew->rendre(floatval($event->getSubject()->getPriseNouveau()));
}
elseif (($event->getSubject()->getDateDeb() >= $day1CY) && ($event->getSubject()->getDateDeb() <= $dayMidCY) && ($event->getSubject()->getDateFin() > $dayMidCY) && ($event->getSubject()->getDateFin() <= $dayFinCY)
)
{
// cas particulier : OY + CY
$soldeOld = $this->em->getRepository('App:Solde')
->findOneBy([
'Annee' => intval((new \DateTime())->format('Y')) - 1,
'Employe' => $event->getSubject()->getDemandeur()
]);
$soldeOld->rendre(floatval($event->getSubject()->getPriseAncien()));
$soldeNew = $this->em->getRepository('App:Solde')
->findOneBy([
'Annee' => (new \DateTime())->format('Y'),
'Employe' => $event->getSubject()->getDemandeur()
]);
$soldeNew->rendre(floatval($event->getSubject()->getPriseNouveau()));
}
elseif (($event->getSubject()->getDateDeb() > $dayMidCY) && ($event->getSubject()->getDateFin() <= $dayFinCY))
{
// Sem 2
$soldeNew = $this->em->getRepository('App:Solde')
->findOneBy([
'Annee' => (new \DateTime())->format('Y'),
'Employe' => $event->getSubject()->getDemandeur()
]);
$soldeNew->rendre(floatval($event->getSubject()->getPriseNouveau()));
}
elseif (($event->getSubject()->getDateDeb() > $dayMidCY) && ($event->getSubject()->getDateDeb() <= $dayFinCY) && ($event->getSubject()->getDateFin() > $dayFinCY))
{
// cas particulier : CY + FY
$soldeOld = $this->em->getRepository('App:Solde')
->findOneBy([
'Annee' => (new \DateTime())->format('Y'),
'Employe' => $event->getSubject()->getDemandeur()
]);
$soldeOld->rendre(floatval($event->getSubject()->getPriseAncien()));
$soldeNew = $this->em->getRepository('App:Solde')
->findOneBy([
'Annee' => intval((new \DateTime())->format('Y')) + 1,
'Employe' => $event->getSubject()->getDemandeur()
]);
$soldeNew->rendre(floatval($event->getSubject()->getPriseNouveau()));
}
elseif ($event->getSubject()->getDateDeb() > $dayFinCY)
{
// Année prochaine
$soldeNew = $this->em->getRepository('App:Solde')
->findOneBy([
'Annee' => intval((new \DateTime())->format('Y')) + 1,
'Employe' => $event->getSubject()->getDemandeur()
]);
$soldeNew->rendre(floatval($event->getSubject()->getPriseNouveau()));
}
$this->em->flush();
}
}
/**
* @return array The event names to listen to
*/
public static function getSubscribedEvents()
{
return [
'workflow.workflow_conge.completed.accepter' => 'onAccepterConge',
'workflow.workflow_conge.completed.accepter_annulation' => 'onAccepterAnnulation',
'workflow.workflow_conge.completed.refuser' => 'onAccepterAnnulation'
];
}
}