$lAdherent->setDateMaj(StringUtils::dateTimeAujourdhuiDb());
 $lAdherent->setCommentaire(trim($lCommentaire));
 $lAdherent->setEtat(1);
 // Protection des dates vides
 if ($lAdherent->getDateNaissance() == '') {
     $lAdherent->setDateNaissance(StringUtils::FORMAT_DATE_NULLE);
 }
 $lRequete = "INSERT INTO " . AdherentManager::TABLE_ADHERENT . "\n\t\t\t\t\t\t\t(" . AdherentManager::CHAMP_ADHERENT_ID . "\n\t\t\t\t\t\t\t," . AdherentManager::CHAMP_ADHERENT_NUMERO . "\n\t\t\t\t\t\t\t," . AdherentManager::CHAMP_ADHERENT_ID_COMPTE . "\n\t\t\t\t\t\t\t," . AdherentManager::CHAMP_ADHERENT_NOM . "\n\t\t\t\t\t\t\t," . AdherentManager::CHAMP_ADHERENT_PRENOM . "\n\t\t\t\t\t\t\t," . AdherentManager::CHAMP_ADHERENT_COURRIEL_PRINCIPAL . "\n\t\t\t\t\t\t\t," . AdherentManager::CHAMP_ADHERENT_COURRIEL_SECONDAIRE . "\n\t\t\t\t\t\t\t," . AdherentManager::CHAMP_ADHERENT_TELEPHONE_PRINCIPAL . "\n\t\t\t\t\t\t\t," . AdherentManager::CHAMP_ADHERENT_TELEPHONE_SECONDAIRE . "\n\t\t\t\t\t\t\t," . AdherentManager::CHAMP_ADHERENT_ADRESSE . "\n\t\t\t\t\t\t\t," . AdherentManager::CHAMP_ADHERENT_CODE_POSTAL . "\n\t\t\t\t\t\t\t," . AdherentManager::CHAMP_ADHERENT_VILLE . "\n\t\t\t\t\t\t\t," . AdherentManager::CHAMP_ADHERENT_DATE_NAISSANCE . "\n\t\t\t\t\t\t\t," . AdherentManager::CHAMP_ADHERENT_DATE_ADHESION . "\n\t\t\t\t\t\t\t," . AdherentManager::CHAMP_ADHERENT_DATE_MAJ . "\n\t\t\t\t\t\t\t," . AdherentManager::CHAMP_ADHERENT_COMMENTAIRE . "\t\t\t\t\n\t\t\t\t\t\t\t," . AdherentManager::CHAMP_ADHERENT_ETAT . ")\n\t\t\t\t\t\t\tVALUES (NULL\n\t\t\t\t\t\t\t\t,'" . StringUtils::securiser($lAdherent->getNumero()) . "'\n\t\t\t\t\t\t\t\t,'" . StringUtils::securiser($lAdherent->getIdCompte()) . "'\n\t\t\t\t\t\t\t\t,'" . StringUtils::securiser($lAdherent->getNom()) . "'\n\t\t\t\t\t\t\t\t,'" . StringUtils::securiser($lAdherent->getPrenom()) . "'\n\t\t\t\t\t\t\t\t,'" . StringUtils::securiser($lAdherent->getCourrielPrincipal()) . "'\n\t\t\t\t\t\t\t\t,'" . StringUtils::securiser($lAdherent->getCourrielSecondaire()) . "'\n\t\t\t\t\t\t\t\t,'" . StringUtils::securiser($lAdherent->getTelephonePrincipal()) . "'\n\t\t\t\t\t\t\t\t,'" . StringUtils::securiser($lAdherent->getTelephoneSecondaire()) . "'\n\t\t\t\t\t\t\t\t,'" . StringUtils::securiser($lAdherent->getAdresse()) . "'\n\t\t\t\t\t\t\t\t,'" . StringUtils::securiser($lAdherent->getCodePostal()) . "'\n\t\t\t\t\t\t\t\t,'" . StringUtils::securiser($lAdherent->getVille()) . "'\n\t\t\t\t\t\t\t\t,'" . StringUtils::securiser($lAdherent->getDateNaissance()) . "'\n\t\t\t\t\t\t\t\t,'" . StringUtils::securiser($lAdherent->getDateAdhesion()) . "'\n\t\t\t\t\t\t\t\t,'" . StringUtils::securiser($lAdherent->getDateMaj()) . "'\n\t\t\t\t\t\t\t\t,'" . StringUtils::securiser($lAdherent->getCommentaire()) . "'\n\t\t\t\t\t\t\t\t,'" . StringUtils::securiser($lAdherent->getEtat()) . "')";
 $lId = Dbutils::executerRequeteInsertRetourId($lRequete);
 // Execution de la requete et récupération de l'Id généré par la BDD
 // Les modules autorisés des adhérents
 $lAutorisation = new AutorisationVO();
 $lAutorisation->setIdAdherent($lId);
 $lAutorisation->setIdModule(1);
 AutorisationManager::insert($lAutorisation);
 $lAutorisation = new AutorisationVO();
 $lAutorisation->setIdAdherent($lId);
 $lAutorisation->setIdModule(3);
 AutorisationManager::insert($lAutorisation);
 // Insertion des informations de connexion
 $lMdp = MotDePasseUtils::generer();
 $lIdentification = new IdentificationVO();
 $lIdentification->setIdLogin($lId);
 $lIdentification->setLogin($lAdherent->getNumero());
 $lIdentification->setPass(md5($lMdp));
 $lIdentification->setType(1);
 $lIdentification->setAutorise(1);
 IdentificationManager::insert($lIdentification);
 // Ajout à la mailing liste
 $lMailingListeService = new MailingListeService();
 if ($lAdherent->getCourrielPrincipal() != "") {
 /**
  * @name remplirAutorisation($pId, $pIdAdherent, $pIdModule)
  * @param interger
  * @param integer
  * @param integer
  * @return AutorisationVO
  * @desc Retourne un AutorisationVO rempli
  */
 private static function remplirAutorisation($pId, $pIdAdherent, $pIdModule)
 {
     $lAutorisation = new AutorisationVO();
     $lAutorisation->setId($pId);
     $lAutorisation->setIdAdherent($pIdAdherent);
     $lAutorisation->setIdModule($pIdModule);
     return $lAutorisation;
 }
 /**
  * @name update($pAdherent)
  * @param AdherentVO
  * @return AdherentVO
  * @desc Modifie un adherent
  */
 private function update($pAdherent)
 {
     $lAdherentActuel = AdherentManager::select($pAdherent->getId());
     // Si pas de liaison création d'un nouveau compte
     if ($pAdherent->getIdCompte() == 0) {
         // Création d'un nouveau compte
         $lCompte = new CompteVO();
         $lCompteService = new CompteService();
         $lCompte = $lCompteService->set($lCompte);
         $pAdherent->setIdCompte($lCompte->getId());
         // Laision avec l'adhérent
         $lCompte->setIdAdherentPrincipal($pAdherent->getId());
         // Positionnement de l'adhérent en adhérent principal du compte
         $lCompteService->set($lCompte);
     } else {
         if ($pAdherent->getIdCompte() != $lAdherentActuel->getIdCompte()) {
             // Liaison avec un autre compte
             $lAdhesionService = new AdhesionService();
             // Suppression des adhésions actuelles
             $lAdhesionService->deleteAdhesionAdherentByIdAdherent($pAdherent->getId());
             // Les adhérents du compte
             $lListeAdherent = $this->selectActifByIdCompte($pAdherent->getIdCompte());
             // Le premier adhérent
             $lAdherent = $lListeAdherent[0];
             // Les adhésions sur le premier adhérent
             $lAdhesions = $lAdhesionService->getAdhesionSurAdherent($lAdherent->getId());
             // Positionne les mêmes adhésions si elles existent
             foreach ($lAdhesions as $lAdhesion) {
                 $lAdhesionAdherentDetail = $lAdhesionService->getAdhesionAdherent($lAdhesion->getAdadId());
                 if ($lAdhesionAdherentDetail) {
                     $lAdhesionAdherent = $lAdhesionAdherentDetail->getAdhesionAdherent();
                     $lTypeAdhesion = $lAdhesionService->getTypeAdhesion($lAdhesionAdherent->getIdTypeAdhesion());
                     if ($lTypeAdhesion->getIdPerimetre() == 2) {
                         // Si type d'adhésion sur périmètre compte
                         $lAdhesionAdherent->setId('');
                         $lAdhesionAdherent->setIdAdherent($pAdherent->getId());
                         $lAdhesionService->setAdhesionAdherent($lAdhesionAdherent);
                     }
                 }
             }
         }
     }
     // Insertion de la date de mise à jour
     $pAdherent->setDateMaj(StringUtils::dateTimeAujourdhuiDb());
     // On reporte le numero dans la maj
     $pAdherent->setNumero($lAdherentActuel->getNumero());
     // L'adherent n'est pas supprimé
     $pAdherent->setEtat(1);
     // Mise en forme des données
     $pAdherent->setNom(StringUtils::formaterNom(trim($pAdherent->getNom())));
     $pAdherent->setPrenom(StringUtils::formaterPrenom(trim($pAdherent->getPrenom())));
     $pAdherent->setCourrielPrincipal(trim($pAdherent->getCourrielPrincipal()));
     $pAdherent->setCourrielSecondaire(trim($pAdherent->getCourrielSecondaire()));
     $pAdherent->setTelephonePrincipal(trim($pAdherent->getTelephonePrincipal()));
     $pAdherent->setTelephoneSecondaire(trim($pAdherent->getTelephoneSecondaire()));
     $pAdherent->setAdresse(trim($pAdherent->getAdresse()));
     $pAdherent->setCodePostal(trim($pAdherent->getCodePostal()));
     $pAdherent->setVille(StringUtils::formaterVille(trim($pAdherent->getVille())));
     $pAdherent->setCommentaire(trim($pAdherent->getCommentaire()));
     // Protection des dates vides
     if ($pAdherent->getDateNaissance() == '') {
         $pAdherent->setDateNaissance(StringUtils::FORMAT_DATE_NULLE);
     }
     if ($pAdherent->getDateAdhesion() == '') {
         $pAdherent->setDateAdhesion(StringUtils::FORMAT_DATE_NULLE);
     }
     if ($pAdherent->getDateMaj() == '') {
         $pAdherent->setDateMaj(StringUtils::FORMAT_DATE_NULLE);
     }
     // Maj de l'adherent dans la BDD
     $lRetour = AdherentManager::update($pAdherent);
     // Récupération des autorisations acutelles
     $lAutorisationsActuelles = AutorisationManager::selectByIdAdherent($pAdherent->getId());
     $lModuleService = new ModuleService();
     $lModulesDefaut = $lModuleService->selectAllDefautVisible();
     $lIdModuleDefaut = array();
     foreach ($lModulesDefaut as $lModule) {
         array_push($lIdModuleDefaut, $lModule->getId());
     }
     // Suppression des autorisations
     $lIdSuppAutorisation = array();
     foreach ($lAutorisationsActuelles as $lAutorisationActu) {
         // Suppression si ce n'est pas un module par defaut
         if (!in_array($lAutorisationActu->getIdModule(), $lIdModuleDefaut)) {
             $lSupp = true;
             foreach ($pAdherent->getListeModule() as $lIdModule) {
                 if ($lAutorisationActu->getIdModule() == $lIdModule) {
                     $lSupp = false;
                 }
             }
             if ($lSupp) {
                 array_push($lIdSuppAutorisation, $lAutorisationActu->getId());
             }
         }
     }
     if (!empty($lIdSuppAutorisation)) {
         AutorisationManager::deleteByArray($lIdSuppAutorisation);
     }
     // Ajout des nouvelles autorisations du compte
     $lAutorisations = array();
     foreach ($pAdherent->getListeModule() as $lIdModule) {
         $lAjout = true;
         foreach ($lAutorisationsActuelles as $lAutorisationActu) {
             if ($lAutorisationActu->getIdModule() == $lIdModule) {
                 $lAjout = false;
             }
         }
         if ($lAjout) {
             $lAutorisation = new AutorisationVO();
             $lAutorisation->setIdAdherent($pAdherent->getId());
             $lAutorisation->setIdModule($lIdModule);
             array_push($lAutorisations, $lAutorisation);
         }
     }
     if (!empty($lAutorisations)) {
         AutorisationManager::insertByArray($lAutorisations);
     }
     //Mise à jour des inscriptions de mailing liste
     $lMailingListeService = new MailingListeService();
     // Suppression des anciens mails
     if ($lAdherentActuel->getCourrielPrincipal() != "") {
         $lMailingListeService->delete($lAdherentActuel->getCourrielPrincipal());
     }
     if ($lAdherentActuel->getCourrielSecondaire() != "") {
         $lMailingListeService->delete($lAdherentActuel->getCourrielSecondaire());
     }
     // Ajout des nouveaux mails
     if ($pAdherent->getCourrielPrincipal() != "") {
         $lMailingListeService->insert($pAdherent->getCourrielPrincipal());
     }
     if ($pAdherent->getCourrielSecondaire() != "") {
         $lMailingListeService->insert($pAdherent->getCourrielSecondaire());
     }
     return $lRetour;
 }