Commit 430157d2 by Damien Moulard

switch caissier relation from manytomany to explicit relation table

parent e2c90d3a
...@@ -4,6 +4,7 @@ namespace App\Admin; ...@@ -4,6 +4,7 @@ namespace App\Admin;
use App\Entity\AccountPrestataire; use App\Entity\AccountPrestataire;
use App\Entity\Adherent; use App\Entity\Adherent;
use App\Entity\Caissier;
use App\Entity\ContactPrestataire; use App\Entity\ContactPrestataire;
use App\Entity\CotisationAdherent; use App\Entity\CotisationAdherent;
use App\Entity\CotisationPrestataire; use App\Entity\CotisationPrestataire;
...@@ -179,6 +180,11 @@ class PrestataireAdmin extends AbstractAdmin ...@@ -179,6 +180,11 @@ class PrestataireAdmin extends AbstractAdmin
} }
} }
$existingCaissiers = $this->getConfigurationPool()->getContainer()->get('doctrine')->getRepository(Caissier::class)->findBy(['prestataire' => $presta]);
$existingCaissiersUsers = array_map(function ($caissier) {
return $caissier->getUser();
}, $existingCaissiers);
$formMapper $formMapper
->tab('Prestataire') ->tab('Prestataire')
->with('Prestataire', ['class' => 'col-md-6']) ->with('Prestataire', ['class' => 'col-md-6'])
...@@ -333,11 +339,12 @@ class PrestataireAdmin extends AbstractAdmin ...@@ -333,11 +339,12 @@ class PrestataireAdmin extends AbstractAdmin
->end() ->end()
->with('Caissier(s)', ['class' => 'col-md-6']) ->with('Caissier(s)', ['class' => 'col-md-6'])
->add('caissiers', EntityType::class, [ ->add('caissiers', EntityType::class, [
// 'mapped' => false, 'mapped' => false,
'class' => User::class, 'class' => User::class, // instance of user so we can set existing users as choices
'multiple' => true, 'multiple' => true,
'required' => false, 'required' => false,
'label' => 'Associer à un(des) utilisateur(s) existant :', 'label' => 'Associer à un(des) utilisateur(s) existant :',
'data' => $existingCaissiersUsers,
'choices' => $this->getConfigurationPool()->getContainer()->get('doctrine')->getRepository(User::class)->findOrderByName(), 'choices' => $this->getConfigurationPool()->getContainer()->get('doctrine')->getRepository(User::class)->findOrderByName(),
'choice_label' => function ($user) { 'choice_label' => function ($user) {
return $user->getLastname() . ' ' . $user->getFirstname() . ' (' . $user->getEmail() . ')'; return $user->getLastname() . ' ' . $user->getFirstname() . ' (' . $user->getEmail() . ')';
...@@ -419,31 +426,58 @@ class PrestataireAdmin extends AbstractAdmin ...@@ -419,31 +426,58 @@ class PrestataireAdmin extends AbstractAdmin
// )) // ))
$em = $this->getConfigurationPool()->getContainer()->get('doctrine')->getManager(); $em = $this->getConfigurationPool()->getContainer()->get('doctrine')->getManager();
$formMapper->getFormBuilder()->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) { $formMapper->getFormBuilder()->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) use ($existingCaissiers) {
$prestataire = $event->getData(); $prestataire = $event->getData();
$users = null; $users = null;
$em = $this->getConfigurationPool()->getContainer()->get('doctrine')->getManager();
/* /*
Permet d'ajouter le nouvel utilisateur crée (newusers) aux gestionnaires du presta Permet d'ajouter le nouvel utilisateur créé (newusers) aux gestionnaires du presta
(On crée un compte adhérent en même temps que le prestataire) (On crée un compte adhérent en même temps que le prestataire)
Ajoute et/ou supprime le(s) caissier(s)
*/ */
if (null != $event->getForm()->get('users')->getData()) { if (null != $event->getForm()->get('users')->getData()) {
$users = $event->getForm()->get('users')->getData(); $users = $event->getForm()->get('users')->getData();
$this->addUsersOrCaissers($users, $prestataire); $this->addUsersOrCaissiers($users, $prestataire);
} }
if (null != $event->getForm()->get('caissiers')->getData()) { if (null != $event->getForm()->get('caissiers')->getData()) {
$caissiers = $event->getForm()->get('caissiers')->getData(); $caissiers = $event->getForm()->get('caissiers')->getData(); // instances of user here
$this->addUsersOrCaissers($caissiers, $prestataire, 'ROLE_CAISSIER'); $this->addUsersOrCaissiers($caissiers, $prestataire, 'ROLE_CAISSIER');
// Remove caissiers: get difference between existing caissier and caissiers set in field
$caissiersToRemove = [];
foreach ($existingCaissiers as $existingCaissier) {
$remove = true;
foreach ($caissiers as $caissierUser) {
if ($existingCaissier->getUser()->getId() === $caissierUser->getId()) {
$remove = false;
break;
}
}
if (true === $remove) {
// if user is only caissier for this prestataire and caissier is removed,
// remove role caissier for user
if (count($existingCaissier->getUser()->getCaissiers()) === 1) {
$groupeCaissier = $em->getRepository(Usergroup::class)->findOneByName('Caissier');
$existingCaissier->getUser()->removePossibleGroup($groupeCaissier);
}
// Remove caissier from prestataire
$prestataire->removeCaissier($existingCaissier);
}
}
} }
if (null != $event->getForm()->get('newusers')->getData()) { if (null != $event->getForm()->get('newusers')->getData()) {
$newusers = $event->getForm()->get('newusers')->getData(); $newusers = $event->getForm()->get('newusers')->getData();
$return = $this->addUsersOrCaissers($newusers, $prestataire); $return = $this->addUsersOrCaissiers($newusers, $prestataire);
if (false === $return) { if (false === $return) {
$event->getForm()->get('newusers')->addError(new FormError('Gestionnaires : Courriel(s) déjà utilisé : ' . implode(', ', $return) . '!')); $event->getForm()->get('newusers')->addError(new FormError('Gestionnaires : Courriel(s) déjà utilisé : ' . implode(', ', $return) . '!'));
} }
} }
if (null != $event->getForm()->get('newcaissiers')->getData()) { if (null != $event->getForm()->get('newcaissiers')->getData()) {
$newcaissiers = $event->getForm()->get('newcaissiers')->getData(); $newcaissiers = $event->getForm()->get('newcaissiers')->getData();
$return = $this->addUsersOrCaissers($newcaissiers, $prestataire, 'ROLE_CAISSIER'); $return = $this->addUsersOrCaissiers($newcaissiers, $prestataire, 'ROLE_CAISSIER');
if (count($return) > 0) { if (count($return) > 0) {
$event->getForm()->get('newcaissiers')->addError(new FormError('Caissiers : Courriel(s) déjà utilisé : ' . implode(', ', $return) . '!')); $event->getForm()->get('newcaissiers')->addError(new FormError('Caissiers : Courriel(s) déjà utilisé : ' . implode(', ', $return) . '!'));
} }
...@@ -452,7 +486,7 @@ class PrestataireAdmin extends AbstractAdmin ...@@ -452,7 +486,7 @@ class PrestataireAdmin extends AbstractAdmin
parent::configureFormFields($formMapper); parent::configureFormFields($formMapper);
} }
private function addUsersOrCaissers($users, Prestataire $prestataire, string $role = 'ROLE_PRESTATAIRE') private function addUsersOrCaissiers($users, Prestataire $prestataire, string $role = 'ROLE_PRESTATAIRE')
{ {
$return = []; $return = [];
$em = $this->getConfigurationPool()->getContainer()->get('doctrine')->getManager(); $em = $this->getConfigurationPool()->getContainer()->get('doctrine')->getManager();
...@@ -474,18 +508,27 @@ class PrestataireAdmin extends AbstractAdmin ...@@ -474,18 +508,27 @@ class PrestataireAdmin extends AbstractAdmin
} }
if ('ROLE_PRESTATAIRE' == $role) { if ('ROLE_PRESTATAIRE' == $role) {
$prestataire->addUser($user); $prestataire->addUser($user);
$groupePresta = $em->getRepository(Usergroup::class)->findOneByName('Prestataire');
if ($newUser) { if ($newUser) {
$this->eventDispatcher->dispatch(MLCEvents::REGISTRATION_PRESTATAIRE, new PrestataireEvent($user, $prestataire, $this->getRequest())); $this->eventDispatcher->dispatch(MLCEvents::REGISTRATION_PRESTATAIRE, new PrestataireEvent($user, $prestataire, $this->getRequest()));
} }
$groupePresta = $em->getRepository(Usergroup::class)->findOneByName('Prestataire');
$user->addPossiblegroup($groupePresta);
} elseif ('ROLE_CAISSIER' == $role) { } elseif ('ROLE_CAISSIER' == $role) {
$prestataire->addCaissier($user); $caissier = $this->getConfigurationPool()->getContainer()->get('doctrine')->getRepository(Caissier::class)->findOneBy(['prestataire' => $prestataire, 'user' => $user]);
$groupePresta = $em->getRepository(Usergroup::class)->findOneByName('Caissier'); if (null === $caissier) {
$caissier = new Caissier();
$caissier->setUser($user);
$caissier->setPrestataire($prestataire);
$em->persist($caissier);
}
$prestataire->addCaissier($caissier);
if ($newUser) { if ($newUser) {
$this->eventDispatcher->dispatch(MLCEvents::REGISTRATION_CAISSIER, new PrestataireEvent($user, $prestataire, $this->getRequest())); $this->eventDispatcher->dispatch(MLCEvents::REGISTRATION_CAISSIER, new PrestataireEvent($user, $prestataire, $this->getRequest()));
} }
$groupeCaissier = $em->getRepository(Usergroup::class)->findOneByName('Caissier');
$user->addPossiblegroup($groupeCaissier);
} }
$user->addPossiblegroup($groupePresta);
$this->userManager->updateCanonicalFields($user); $this->userManager->updateCanonicalFields($user);
$em->persist($user); $em->persist($user);
if ($newUser) { if ($newUser) {
......
...@@ -6,6 +6,7 @@ use App\Entity\AccountComptoir; ...@@ -6,6 +6,7 @@ use App\Entity\AccountComptoir;
use App\Entity\AccountGroupe; use App\Entity\AccountGroupe;
use App\Entity\AccountPrestataire; use App\Entity\AccountPrestataire;
use App\Entity\AccountSiege; use App\Entity\AccountSiege;
use App\Entity\Caissier;
use App\Entity\Comptoir; use App\Entity\Comptoir;
use App\Entity\Geoloc; use App\Entity\Geoloc;
use App\Entity\GeolocPrestataire; use App\Entity\GeolocPrestataire;
...@@ -696,11 +697,12 @@ class IndexController extends AbstractController ...@@ -696,11 +697,12 @@ class IndexController extends AbstractController
public function prestaChoiceAction(Usergroup $group, Prestataire $prestataire, Request $request) public function prestaChoiceAction(Usergroup $group, Prestataire $prestataire, Request $request)
{ {
$this->em->refresh($this->getUser()); $this->em->refresh($this->getUser());
if (!(($this->getUser()->getPossiblegroups()->exists(function ($key, $value) {
// prevent operation if user isn't a prestataire
if (
!($this->getUser()->getPossiblegroups()->exists(function ($key, $value) {
return in_array('ROLE_PRESTATAIRE', $value->getRoles()); return in_array('ROLE_PRESTATAIRE', $value->getRoles());
}) and $this->getUser()->getPrestataires()->contains($prestataire)) || (($this->getUser()->getPossiblegroups()->exists(function ($key, $value) { }) and $this->getUser()->getPrestataires()->contains($prestataire))
return in_array('ROLE_CAISSIER', $value->getRoles());
}) and $this->getUser()->getCaissiers()->contains($prestataire))))
) { ) {
$this->addFlash( $this->addFlash(
'error', 'error',
...@@ -718,6 +720,39 @@ class IndexController extends AbstractController ...@@ -718,6 +720,39 @@ class IndexController extends AbstractController
} }
/** /**
* Choix du presta géré.
*
* @Route("/login/caissier/choice/{usergrpid}/{caissierid}", name="caissier_choice")
* @ParamConverter("group", class="App:Usergroup", options={"mapping": {"usergrpid": "id"}})
* @ParamConverter("caissier", class="App:Caissier", options={"mapping": {"caissierid": "id"}})
* @IsGranted("ROLE_USER")
*/
public function caissierChoiceAction(Usergroup $group, Caissier $caissier, Request $request)
{
$this->em->refresh($this->getUser());
// prevent operation if user isn't a caissier
if (
!($this->getUser()->getPossiblegroups()->exists(function ($key, $value) {
return in_array('ROLE_CAISSIER', $value->getRoles());
}) and $this->getUser()->getCaissiers()->contains($caissier))
) {
$this->addFlash(
'error',
'Accès impossible !'
);
return $this->redirectToRoute('index');
}
$this->removeOldSessionParams();
// On enregistre le presta choisit en session
$this->session->set('_prestagere', $caissier->getPrestataire());
$this->reloadUserTokenFromGroup($group, $request);
return $this->redirectToRoute('index');
}
/**
* @Route("/login/group/choice/{id}", name="usergroup_choice") * @Route("/login/group/choice/{id}", name="usergroup_choice")
* @IsGranted("ROLE_USER") * @IsGranted("ROLE_USER")
*/ */
......
...@@ -4,6 +4,7 @@ namespace App\Controller; ...@@ -4,6 +4,7 @@ namespace App\Controller;
use App\Entity\AchatMonnaieAConfirmerPrestataire; use App\Entity\AchatMonnaieAConfirmerPrestataire;
use App\Entity\AchatMonnaiePrestataire; use App\Entity\AchatMonnaiePrestataire;
use App\Entity\Caissier;
use App\Entity\GlobalParameter; use App\Entity\GlobalParameter;
use App\Entity\Payment; use App\Entity\Payment;
use App\Entity\Prestataire; use App\Entity\Prestataire;
...@@ -75,12 +76,17 @@ class UserPrestataireController extends FluxController ...@@ -75,12 +76,17 @@ class UserPrestataireController extends FluxController
$this->translator->trans($errorText) $this->translator->trans($errorText)
); );
} else { } else {
// Remove caissier(s) if delete from the field // Remove caissier(s) if deleted from the field
foreach ($originalCaissiers as $oldCaissier) { foreach ($originalCaissiers as $oldCaissier) {
if (!in_array($oldCaissier->getEmail(), $caissiers)) { if (!in_array($oldCaissier->getUser()->getEmail(), $caissiers)) {
$groupePresta = $this->em->getRepository(Usergroup::class)->findOneByName('Caissier'); // if user is only caissier for this prestataire and caissier is removed,
$oldCaissier->removePossibleGroup($groupePresta); // remove role caissier for user
$this->em->persist($oldCaissier); if (count($oldCaissier->getUser()->getCaissiers()) === 1) {
$groupeCaissier = $this->em->getRepository(Usergroup::class)->findOneByName('Caissier');
$oldCaissier->getUser()->removePossibleGroup($groupeCaissier);
$this->em->persist($oldCaissier->getUser());
}
$form->getData()->getCaissiers()->removeElement($oldCaissier); $form->getData()->getCaissiers()->removeElement($oldCaissier);
} }
} }
...@@ -134,12 +140,19 @@ class UserPrestataireController extends FluxController ...@@ -134,12 +140,19 @@ class UserPrestataireController extends FluxController
$user->setEmail($email); $user->setEmail($email);
$user->setUsername($email); $user->setUsername($email);
} }
$prestataire->addCaissier($user); $caissier = $this->em->getRepository(Caissier::class)->findOneBy(['prestataire' => $prestataire, 'user' => $user]);
$groupePresta = $this->em->getRepository(Usergroup::class)->findOneByName('Caissier'); if (null === $caissier) {
$caissier = new Caissier();
$caissier->setUser($user);
$caissier->setPrestataire($prestataire);
$this->em->persist($caissier);
}
$prestataire->addCaissier($caissier);
$groupeCaissier = $this->em->getRepository(Usergroup::class)->findOneByName('Caissier');
if ($newUser) { if ($newUser) {
$this->eventDispatcher->dispatch(MLCEvents::REGISTRATION_CAISSIER, new PrestataireEvent($user, $prestataire, $request)); $this->eventDispatcher->dispatch(MLCEvents::REGISTRATION_CAISSIER, new PrestataireEvent($user, $prestataire, $request));
} }
$user->addPossiblegroup($groupePresta); $user->addPossiblegroup($groupeCaissier);
$this->userManager->updateCanonicalFields($user); $this->userManager->updateCanonicalFields($user);
$this->em->persist($user); $this->em->persist($user);
if ($newUser) { if ($newUser) {
......
<?php
namespace App\Entity;
use App\Repository\CaissierRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=CaissierRepository::class)
*/
class Caissier
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="caissiers")
* @ORM\JoinColumn(nullable=false)
*/
private $user;
/**
* @ORM\ManyToOne(targetEntity=Prestataire::class, inversedBy="caissiers")
* @ORM\JoinColumn(nullable=false)
*/
private $prestataire;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $lastTransactionsExportDatetime;
public function getId(): ?int
{
return $this->id;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
public function getPrestataire(): ?Prestataire
{
return $this->prestataire;
}
public function setPrestataire(?Prestataire $prestataire): self
{
$this->prestataire = $prestataire;
return $this;
}
public function getLastTransactionsExportDatetime(): ?\DateTimeInterface
{
return $this->lastTransactionsExportDatetime;
}
public function setLastTransactionsExportDatetime(?\DateTimeInterface $lastTransactionsExportDatetime): self
{
$this->lastTransactionsExportDatetime = $lastTransactionsExportDatetime;
return $this;
}
public function __toString()
{
return $this->getUser()->__toString();
}
}
...@@ -291,16 +291,9 @@ class Prestataire extends AccountableObject implements AccountableInterface ...@@ -291,16 +291,9 @@ class Prestataire extends AccountableObject implements AccountableInterface
protected $users; protected $users;
/** /**
* @var ArrayCollection|User[] * @ORM\OneToMany(targetEntity=Caissier::class, mappedBy="prestataire", orphanRemoval=true)
*
* @ORM\ManyToMany(targetEntity="User", inversedBy="caissiers", cascade={"persist"})
* @ORM\JoinTable(name="user_caissier",
* joinColumns={@ORM\JoinColumn(name="prestataire_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")}
* )
* @Groups({"read", "write"})
*/ */
protected $caissiers; private $caissiers;
/** /**
* @var Groupe * @var Groupe
...@@ -794,9 +787,9 @@ class Prestataire extends AccountableObject implements AccountableInterface ...@@ -794,9 +787,9 @@ class Prestataire extends AccountableObject implements AccountableInterface
} }
/** /**
* @return ArrayCollection[]|User * @return Collection<int, Caissier>
*/ */
public function getCaissiers() public function getCaissiers(): Collection
{ {
return $this->caissiers; return $this->caissiers;
} }
...@@ -872,31 +865,23 @@ class Prestataire extends AccountableObject implements AccountableInterface ...@@ -872,31 +865,23 @@ class Prestataire extends AccountableObject implements AccountableInterface
return $this; return $this;
} }
/** public function addCaissier(Caissier $caissier): self
* @param User $caissier
*
* @return $this
*/
public function addCaissier(User $caissier): self
{ {
if (!$this->caissiers->contains($caissier)) { if (!$this->caissiers->contains($caissier)) {
$this->caissiers[] = $caissier; $this->caissiers[] = $caissier;
$caissier->addCaissier($this); $caissier->setPrestataire($this);
} }
return $this; return $this;
} }
/** public function removeCaissier(Caissier $caissier): self
* @param User $caissier
*
* @return $this
*/
public function removeCaissier(User $caissier)
{ {
if ($this->caissiers->contains($caissier)) { if ($this->caissiers->removeElement($caissier)) {
$this->caissiers->removeElement($caissier); // set the owning side to null (unless already changed)
$caissier->removeCaissier($this); if ($caissier->getPrestataire() === $this) {
$caissier->setPrestataire(null);
}
} }
return $this; return $this;
......
...@@ -126,9 +126,9 @@ class User extends BaseUser ...@@ -126,9 +126,9 @@ class User extends BaseUser
protected $prestataires; protected $prestataires;
/** /**
* @ORM\ManyToMany(targetEntity="App\Entity\Prestataire", mappedBy="caissiers", cascade={"persist"}, fetch="EAGER") * @ORM\OneToMany(targetEntity=Caissier::class, mappedBy="user", orphanRemoval=true)
*/ */
protected $caissiers; private $caissiers;
/** /**
* @ORM\ManyToMany(targetEntity="App\Entity\Groupe", mappedBy="gestionnaires", cascade={"persist"}, fetch="EAGER") * @ORM\ManyToMany(targetEntity="App\Entity\Groupe", mappedBy="gestionnaires", cascade={"persist"}, fetch="EAGER")
...@@ -418,48 +418,35 @@ class User extends BaseUser ...@@ -418,48 +418,35 @@ class User extends BaseUser
} }
/** /**
* Get caissiers. * @return Collection<int, Caissier>
*
* @return
*/ */
public function getCaissiers() public function getCaissiers(): Collection
{ {
return $this->caissiers; return $this->caissiers;
} }
/** public function addCaissier(Caissier $caissier): self
* @param Prestataire $caissier
*
* @return $this
*/
public function addCaissier(Prestataire $caissier): self
{ {
if (!$this->caissiers->contains($caissier)) { if (!$this->caissiers->contains($caissier)) {
$this->caissiers[] = $caissier; $this->caissiers[] = $caissier;
$caissier->setUser($this);
} }
return $this; return $this;
} }
/** public function removeCaissier(Caissier $caissier): self
* @param Prestataire $caissier
*
* @return $this
*/
public function removeCaissier(Prestataire $caissier): self
{ {
if ($this->caissiers->contains($caissier)) { if ($this->caissiers->removeElement($caissier)) {
$this->caissiers->removeElement($caissier); // set the owning side to null (unless already changed)
if ($caissier->getUser() === $this) {
$caissier->setUser(null);
}
} }
return $this; return $this;
} }
/**
* Set caissier.
*
* @return $this
*/
public function setCaissiers($caissiers): self public function setCaissiers($caissiers): self
{ {
$this->caissiers = $caissiers; $this->caissiers = $caissiers;
......
...@@ -38,7 +38,7 @@ class LoginListener ...@@ -38,7 +38,7 @@ class LoginListener
if (in_array('ROLE_PRESTATAIRE', $groupe->getRoles()) && count($user->getPrestataires()) >= 1) { if (in_array('ROLE_PRESTATAIRE', $groupe->getRoles()) && count($user->getPrestataires()) >= 1) {
$this->session->set('_prestagere', $user->getPrestataires()[0]); $this->session->set('_prestagere', $user->getPrestataires()[0]);
} elseif (in_array('ROLE_CAISSIER', $groupe->getRoles()) && count($user->getCaissiers()) >= 1) { } elseif (in_array('ROLE_CAISSIER', $groupe->getRoles()) && count($user->getCaissiers()) >= 1) {
$this->session->set('_prestagere', $user->getCaissiers()[0]); $this->session->set('_prestagere', $user->getCaissiers()[0]->getPrestataire());
} elseif (in_array('ROLE_COMPTOIR', $groupe->getRoles()) && count($user->getComptoirsGeres()) >= 1) { } elseif (in_array('ROLE_COMPTOIR', $groupe->getRoles()) && count($user->getComptoirsGeres()) >= 1) {
$this->session->set('_comptoirgere', $user->getComptoirsGeres()[0]); $this->session->set('_comptoirgere', $user->getComptoirsGeres()[0]);
} elseif ((in_array('ROLE_TRESORIER', $groupe->getRoles()) || in_array('ROLE_CONTACT', $groupe->getRoles()) || in_array('ROLE_GESTION_GROUPE', $groupe->getRoles())) && count($user->getGroupesGeres()) >= 1) { } elseif ((in_array('ROLE_TRESORIER', $groupe->getRoles()) || in_array('ROLE_CONTACT', $groupe->getRoles()) || in_array('ROLE_GESTION_GROUPE', $groupe->getRoles())) && count($user->getGroupesGeres()) >= 1) {
......
...@@ -103,7 +103,7 @@ class SwitchUserSubscriber implements EventSubscriberInterface ...@@ -103,7 +103,7 @@ class SwitchUserSubscriber implements EventSubscriberInterface
if (in_array('ROLE_PRESTATAIRE', $groupe->getRoles()) && count($user->getPrestataires()) >= 1) { if (in_array('ROLE_PRESTATAIRE', $groupe->getRoles()) && count($user->getPrestataires()) >= 1) {
$this->session->set('_prestagere', $user->getPrestataires()[0]); $this->session->set('_prestagere', $user->getPrestataires()[0]);
} elseif (in_array('ROLE_CAISSIER', $groupe->getRoles()) && count($user->getCaissiers()) >= 1) { } elseif (in_array('ROLE_CAISSIER', $groupe->getRoles()) && count($user->getCaissiers()) >= 1) {
$this->session->set('_prestagere', $user->getCaissiers()[0]); $this->session->set('_prestagere', $user->getCaissiers()[0]->getPrestataire());
} elseif (in_array('ROLE_COMPTOIR', $groupe->getRoles()) && count($user->getComptoirsGeres()) >= 1) { } elseif (in_array('ROLE_COMPTOIR', $groupe->getRoles()) && count($user->getComptoirsGeres()) >= 1) {
$this->session->set('_comptoirgere', $user->getComptoirsGeres()[0]); $this->session->set('_comptoirgere', $user->getComptoirsGeres()[0]);
} elseif ((in_array('ROLE_TRESORIER', $groupe->getRoles()) || in_array('ROLE_CONTACT', $groupe->getRoles()) || in_array('ROLE_GESTION_GROUPE', $groupe->getRoles())) && count($user->getGroupesGeres()) >= 1) { } elseif ((in_array('ROLE_TRESORIER', $groupe->getRoles()) || in_array('ROLE_CONTACT', $groupe->getRoles()) || in_array('ROLE_GESTION_GROUPE', $groupe->getRoles())) && count($user->getGroupesGeres()) >= 1) {
......
...@@ -45,7 +45,7 @@ class PrestataireInfosFormType extends AbstractType ...@@ -45,7 +45,7 @@ class PrestataireInfosFormType extends AbstractType
if ('' !== $caissiers) { if ('' !== $caissiers) {
$caissiers .= ';'; $caissiers .= ';';
} }
$caissiers .= $caissier->getEmail(); $caissiers .= $caissier->getUser()->getEmail();
} }
} }
$builder $builder
......
...@@ -119,7 +119,7 @@ class AfterLoginRedirection implements AuthenticationSuccessHandlerInterface ...@@ -119,7 +119,7 @@ class AfterLoginRedirection implements AuthenticationSuccessHandlerInterface
if (in_array('ROLE_PRESTATAIRE', $groupe->getRoles()) && count($user->getPrestataires()) >= 1) { if (in_array('ROLE_PRESTATAIRE', $groupe->getRoles()) && count($user->getPrestataires()) >= 1) {
$this->session->set('_prestagere', $user->getPrestataires()[0]); $this->session->set('_prestagere', $user->getPrestataires()[0]);
} elseif (in_array('ROLE_CAISSIER', $groupe->getRoles()) && count($user->getCaissiers()) >= 1) { } elseif (in_array('ROLE_CAISSIER', $groupe->getRoles()) && count($user->getCaissiers()) >= 1) {
$this->session->set('_prestagere', $user->getCaissiers()[0]); $this->session->set('_prestagere', $user->getCaissiers()[0]->getPrestataire());
} elseif (in_array('ROLE_COMPTOIR', $groupe->getRoles()) && count($user->getComptoirsGeres()) >= 1) { } elseif (in_array('ROLE_COMPTOIR', $groupe->getRoles()) && count($user->getComptoirsGeres()) >= 1) {
$this->session->set('_comptoirgere', $user->getComptoirsGeres()[0]); $this->session->set('_comptoirgere', $user->getComptoirsGeres()[0]);
} elseif ((in_array('ROLE_TRESORIER', $groupe->getRoles()) || in_array('ROLE_CONTACT', $groupe->getRoles()) || in_array('ROLE_GESTION_GROUPE', $groupe->getRoles())) && count($user->getGroupesGeres()) >= 1) { } elseif ((in_array('ROLE_TRESORIER', $groupe->getRoles()) || in_array('ROLE_CONTACT', $groupe->getRoles()) || in_array('ROLE_GESTION_GROUPE', $groupe->getRoles())) && count($user->getGroupesGeres()) >= 1) {
......
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20250422095126 extends AbstractMigration
{
public function getDescription() : string
{
return 'Operations related to changing ManyToMany relationship "caissiers" to explicit relation entity "Caissier"';
}
public function up(Schema $schema) : void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE user_caissier DROP FOREIGN KEY FK_FF8E6FC2A76ED395');
$this->addSql('ALTER TABLE user_caissier DROP FOREIGN KEY FK_FF8E6FC2BE3DB2B7');
$this->addSql('ALTER TABLE user_caissier ADD id INT AUTO_INCREMENT NOT NULL, ADD last_transactions_export_datetime DATETIME DEFAULT NULL COMMENT \'(DC2Type:datetime_immutable)\', DROP PRIMARY KEY, ADD PRIMARY KEY (id)');
$this->addSql('ALTER TABLE user_caissier RENAME TO caissier');
$this->addSql('ALTER TABLE caissier ADD CONSTRAINT FK_1F038BC2A76ED395 FOREIGN KEY (user_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE caissier ADD CONSTRAINT FK_1F038BC2BE3DB2B7 FOREIGN KEY (prestataire_id) REFERENCES prestataire (id)');
}
public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE caissier DROP FOREIGN KEY FK_1F038BC2A76ED395');
$this->addSql('ALTER TABLE caissier DROP FOREIGN KEY FK_1F038BC2BE3DB2B7');
$this->addSql('ALTER TABLE caissier DROP PRIMARY KEY, ADD PRIMARY KEY (prestataire_id, user_id), DROP COLUMN id, DROP COLUMN last_transactions_export_datetime');
$this->addSql('ALTER TABLE caissier RENAME TO user_caissier');
$this->addSql('ALTER TABLE user_caissier ADD CONSTRAINT FK_FF8E6FC2A76ED395 FOREIGN KEY (user_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE user_caissier ADD CONSTRAINT FK_FF8E6FC2BE3DB2B7 FOREIGN KEY (prestataire_id) REFERENCES prestataire (id)');
}
}
<?php
namespace App\Repository;
use App\Entity\Caissier;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\ORMException;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Caissier>
*
* @method Caissier|null find($id, $lockMode = null, $lockVersion = null)
* @method Caissier|null findOneBy(array $criteria, array $orderBy = null)
* @method Caissier[] findAll()
* @method Caissier[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class CaissierRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Caissier::class);
}
/**
* @throws ORMException
* @throws OptimisticLockException
*/
public function add(Caissier $entity, bool $flush = true): void
{
$this->_em->persist($entity);
if ($flush) {
$this->_em->flush();
}
}
/**
* @throws ORMException
* @throws OptimisticLockException
*/
public function remove(Caissier $entity, bool $flush = true): void
{
$this->_em->remove($entity);
if ($flush) {
$this->_em->flush();
}
}
// /**
// * @return Caissier[] Returns an array of Caissier objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('c')
->andWhere('c.exampleField = :val')
->setParameter('val', $value)
->orderBy('c.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?Caissier
{
return $this->createQueryBuilder('c')
->andWhere('c.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}
...@@ -177,19 +177,24 @@ class FluxRepository extends ServiceEntityRepository ...@@ -177,19 +177,24 @@ class FluxRepository extends ServiceEntityRepository
} }
/** /**
* @param User $user
* @param Prestataire $presta * @param Prestataire $presta
* @param bool $onlySameDay * @param bool $onlySameDay
* *
* @return Query Returns a query fo finding an array of Flux * @return Query Returns a query fo finding an array of Flux
*/ */
public function getQueryByCaissier(Prestataire $presta, $onlySameDay = true) public function getQueryByCaissier(User $user, Prestataire $presta, $onlySameDay = true)
{ {
$sqlQuery = "SELECT f.id FROM {$this->tableName} f WHERE ((f.type = 'adherent_prestataire' AND f.prestataire_id = :id) OR (f.type = 'prestataire_prestataire' AND f.prestataire_dest_id = :id))"; // TODO change here
$sqlQuery = "SELECT f.id FROM {$this->tableName} f";
$sqlQuery .= " WHERE ((f.type = 'adherent_prestataire' AND f.prestataire_id = :p_id) OR (f.type = 'prestataire_prestataire' AND f.prestataire_dest_id = :id))";
$sqlQuery .= " AND f.user_id = :c_id"; // set operateur
if ($onlySameDay) { if ($onlySameDay) {
$sqlQuery .= ' AND DATE(f.created_at) = CURDATE()'; $sqlQuery .= ' AND DATE(f.created_at) = CURDATE()';
} }
$statement = $this->connection->prepare($sqlQuery); $statement = $this->connection->prepare($sqlQuery);
$statement->bindValue(':id', $presta->getId()); $statement->bindValue(':p_id', $presta->getId());
$statement->bindValue(':c_id', $user->getId());
$statement->execute(); $statement->execute();
$results = $statement->fetchAll(); $results = $statement->fetchAll();
$qb = $this->createQueryBuilder('f'); $qb = $this->createQueryBuilder('f');
......
...@@ -478,7 +478,7 @@ class AppExtension extends AbstractExtension ...@@ -478,7 +478,7 @@ class AppExtension extends AbstractExtension
if ($this->security->isGranted('ROLE_PRESTATAIRE')) { if ($this->security->isGranted('ROLE_PRESTATAIRE')) {
$query = $this->em->getRepository(Flux::class)->getQueryByPrestataire($this->session->get('_prestagere'), $parenttype); $query = $this->em->getRepository(Flux::class)->getQueryByPrestataire($this->session->get('_prestagere'), $parenttype);
} elseif ($this->security->isGranted('ROLE_CAISSIER')) { } elseif ($this->security->isGranted('ROLE_CAISSIER')) {
$query = $this->em->getRepository(Flux::class)->getQueryByCaissier($this->session->get('_prestagere')); $query = $this->em->getRepository(Flux::class)->getQueryByCaissier($user, $this->session->get('_prestagere'));
} }
} elseif (null != $user->getAdherent()) { } elseif (null != $user->getAdherent()) {
$query = $this->em->getRepository(Flux::class)->getQueryByAdherent($user->getAdherent(), $parenttype); $query = $this->em->getRepository(Flux::class)->getQueryByAdherent($user->getAdherent(), $parenttype);
......
...@@ -17,9 +17,9 @@ ...@@ -17,9 +17,9 @@
</div> </div>
{% endfor %} {% endfor %}
{% elseif role == 'ROLE_CAISSIER' %} {% elseif role == 'ROLE_CAISSIER' %}
{% for presta in app.user.caissiers %} {% for caissier in app.user.caissiers %}
<div class='col-6 text-center p-2'> <div class='col-6 text-center p-2'>
<a role="button" class="btn btn-default btn-secondary" href='{{path('presta_choice', {'prestaid' : presta.id, 'usergrpid': group.id})}}'>{{ group.name|trans }} - {{presta}} </a> <a role="button" class="btn btn-default btn-secondary" href='{{path('caissier_choice', {'caissierid' : caissier.id, 'usergrpid': group.id})}}'>{{ group.name|trans }} - {{caissier.prestataire}} </a>
</div> </div>
{% endfor %} {% endfor %}
{% elseif role == 'ROLE_COMPTOIR' %} {% elseif role == 'ROLE_COMPTOIR' %}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment