/**
  * @Security("has_role('ROLE_SUIVEUR')")
  *
  * @param $id int id of project
  *
  * @return RedirectResponse|Response
  */
 public function modifierAction($id)
 {
     $em = $this->getDoctrine()->getManager();
     if (!($etude = $em->getRepository('Mgate\\SuiviBundle\\Entity\\Etude')->find($id))) {
         throw $this->createNotFoundException('L\'étude demandée n\'existe pas!');
     }
     if ($this->get('Mgate.etude_manager')->confidentielRefus($etude, $this->getUser(), $this->get('security.authorization_checker'))) {
         throw new AccessDeniedException('Cette étude est confidentielle');
     }
     //save missions and repartition before form handling
     $missionList = new ArrayCollection();
     foreach ($etude->getMissions() as $mission) {
         $missionList->add($mission);
     }
     $repartitionList = new ArrayCollection();
     foreach ($missionList as $mission) {
         $repartitionList->add($mission->getRepartitionsJEH());
     }
     /* Form handling */
     $form = $this->createForm(new MissionsType($etude), $etude);
     if ($this->get('request')->getMethod() == 'POST') {
         $form->handleRequest($this->get('request'));
         if ($form->isValid()) {
             //if a new missions set is created
             if ($this->get('request')->get('add')) {
                 $missionNew = new Mission();
                 // add a new empty mission to mission set.
                 $missionNew->setEtude($etude);
                 $etude->addMission($missionNew);
             }
             //if a repartition is added to a mission
             if ($this->get('request')->get('addRepartition')) {
                 $repartitionNew = new RepartitionJEH();
                 if ($this->get('request')->get('idMission') !== null) {
                     $idMission = intval($this->get('request')->get('idMission'));
                     if ($etude->getMissions()->get($idMission)) {
                         $mission = $etude->getMissions()->get($this->get('request')->get('idMission'));
                         $mission->addRepartitionsJEH($repartitionNew);
                         $repartitionNew->setMission($mission);
                         $repartitionNew->setNbrJEH(0);
                         $repartitionNew->setPrixJEH(320);
                     }
                 }
             }
             //removing existing missions from missionList to get missions to delete
             foreach ($etude->getMissions() as $mission) {
                 //compare current missions to initial mission list
                 if ($missionList->contains($mission)) {
                     // mission still exists, let's remove it
                     $missionList->removeElement($mission);
                     //compare current repartition to initial repartition list
                     foreach ($mission->getRepartitionsJEH() as $repartition) {
                         if ($repartitionList->contains($repartition)) {
                             // repartition still exist, let's remove it
                             $repartitionList->removeElement($repartition);
                         }
                     }
                 }
             }
             //at that point we have missionList and repartitionlist are containing only objects to delete. let's iterate on them
             foreach ($missionList as $mission) {
                 $em->remove($mission);
             }
             foreach ($repartitionList as $repartitions) {
                 foreach ($repartitions as $repartition) {
                     $em->remove($repartition);
                 }
             }
             $em->persist($etude);
             $em->flush();
             return $this->redirect($this->generateUrl('MgateSuivi_missions_modifier', array('id' => $etude->getId())));
         }
     }
     return $this->render('MgateSuiviBundle:Mission:missions.html.twig', array('form' => $form->createView(), 'etude' => $etude));
 }
 /**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $em = $this->getContainer()->get('doctrine.orm.entity_manager');
     $array_projects = array(array('nom' => '315GLA', 'description' => 'Realisation site web', 'statut' => 1, 'nbrJEH' => 9, 'duree' => 5, 'dateCC' => 'ok', 'prospect' => array('entreprise' => 'Gladiator Consulting', 'adresse' => '3 rue du chene noir', 'codePostal' => 33100, 'ville' => 'Toulouse', 'entite' => 2, 'email' => '*****@*****.**')), array('nom' => '316BLA', 'description' => 'Electronique avancee', 'statut' => 2, 'nbrJEH' => 5, 'duree' => 3, 'dateCC' => 'ok', 'prospect' => array('entreprise' => 'Blackwater', 'adresse' => '1020 5th Avenue', 'codePostal' => 92200, 'ville' => 'Neuilly', 'entite' => 3, 'email' => '*****@*****.**')), array('nom' => '317IMU', 'description' => 'Design Base de donnes', 'statut' => 4, 'nbrJEH' => 8, 'duree' => 4, 'dateCC' => 'ok', 'prospect' => array('entreprise' => 'Imuka', 'adresse' => 'Kuruma San', 'codePostal' => 91000, 'ville' => 'Evry', 'entite' => 4, 'email' => '*****@*****.**')), array('nom' => '319UNI', 'description' => 'Conception Radar recul', 'statut' => 4, 'nbrJEH' => 12, 'duree' => 8, 'dateCC' => 'ok', 'prospect' => array('entreprise' => 'Universal rad', 'adresse' => '2 rue Marie Curie', 'codePostal' => 35000, 'ville' => 'Rennes', 'entite' => 5, 'email' => '*****@*****.**')), array('nom' => '320TEK', 'description' => 'Refactorisation code Java', 'statut' => 2, 'nbrJEH' => 10, 'duree' => 8, 'dateCC' => 'ok', 'prospect' => array('entreprise' => 'Teknik studio', 'adresse' => '10 impasse sunderland', 'codePostal' => 35000, 'ville' => 'Rennes', 'entite' => 6, 'email' => '*****@*****.**')), array('nom' => '321DUV', 'description' => 'Calcul de flux thermique', 'statut' => 2, 'nbrJEH' => 9, 'duree' => 4, 'dateCC' => 'ok', 'prospect' => array('entreprise' => 'Duvilcolor', 'adresse' => '600 la pyrennene ', 'codePostal' => 33100, 'ville' => 'Labege', 'entite' => 4, 'email' => '*****@*****.**')), array('nom' => '322NIL', 'description' => 'Application Android', 'statut' => 1, 'nbrJEH' => 8, 'duree' => 12, 'dateCC' => 'ok', 'prospect' => array('entreprise' => 'Nilsen Industries', 'adresse' => '2 rue saint-louis', 'codePostal' => 31000, 'ville' => 'Bordeaux', 'entite' => 7, 'email' => '*****@*****.**')), array('nom' => '323PRR', 'description' => 'Etude de faisabilite', 'statut' => 3, 'nbrJEH' => 4, 'duree' => 4, 'dateCC' => 'ok', 'prospect' => array('entreprise' => 'PRR', 'adresse' => 'PRR', 'codePostal' => 35000, 'ville' => 'Rennes', 'entite' => 4, 'email' => '*****@*****.**')));
     $inserted_projects = 0;
     $inserted_prospects = 0;
     foreach ($array_projects as $etude) {
         //create project
         $e = new Etude();
         $inserted_projects++;
         $mandat = rand(2013, 2016);
         $month = rand(1, 10);
         $day = rand(1, 30);
         $e->setMandat($mandat);
         $e->setNom($etude['nom']);
         $e->setDescription($etude['description']);
         $e->setDateCreation(new \DateTime($mandat . '-' . $month . '-' . $day));
         $e->setStateID($etude['statut']);
         $e->setAcompte(true);
         $e->setPourcentageAcompte(0.3);
         $e->setFraisDossier(90);
         $e->setPresentationProjet('Presentation ' . $etude['description']);
         $e->setDescriptionPrestation('Describe what we will do here');
         $e->setSourceDeProspection(rand(1, 10));
         $em->persist($e);
         $c = $em->getRepository('N7consultingRhBundle:Competence')->find(rand(1, 12));
         if ($c !== null) {
             $c->addEtude($e);
         }
         /** Prospect management */
         $p = new Prospect();
         $inserted_prospects++;
         $p->setNom($etude['prospect']['entreprise']);
         $p->setAdresse($etude['prospect']['adresse']);
         $p->setCodePostal($etude['prospect']['codePostal']);
         $p->setVille($etude['prospect']['ville']);
         $p->setEntite($etude['prospect']['entite']);
         $pe = new Personne();
         $pe->setPrenom(self::PRENOM[array_rand(self::PRENOM)]);
         //whitespace explode : not perfect but better than nothing
         $pe->setNom(self::NOM[array_rand(self::NOM)]);
         $pe->setEmailEstValide(true);
         $pe->setEstAbonneNewsletter(false);
         $pe->setEmail($etude['prospect']['email']);
         $pe->setAdresse($etude['prospect']['adresse']);
         $pe->setCodePostal($etude['prospect']['codePostal']);
         $pe->setVille($etude['prospect']['ville']);
         $emp = new Employe();
         $emp->setProspect($p);
         $p->addEmploye($emp);
         $emp->setPersonne($pe);
         $em->persist($emp->getPersonne());
         $em->persist($emp);
         $em->persist($p);
         $e->setProspect($p);
         //create phases
         $g = new GroupePhases();
         //default group
         $g->setTitre('Random generated' . rand());
         $g->setNumero(1);
         $g->setDescription('Automatic description');
         $g->setEtude($e);
         $em->persist($g);
         $ph = new Phase();
         $ph->setEtude($e);
         $ph->setGroupe($g);
         $ph->setPosition(0);
         $ph->setNbrJEH($etude['nbrJEH']);
         $ph->setPrixJEH(340);
         $ph->setTitre('Default phase');
         $ph->setDelai($etude['duree'] * 7);
         $ph->setDateDebut(new \DateTime($mandat . '-' . $month . '-' . $day));
         $em->persist($ph);
         //manage project manager
         $pm = new Personne();
         $pm->setPrenom(self::PRENOM[array_rand(self::PRENOM)]);
         $pm->setNom(self::NOM[array_rand(self::NOM)]);
         $pm->setEmailEstValide(false);
         $pm->setEstAbonneNewsletter(false);
         $em->persist($pm);
         $m = new Membre();
         $m->setPersonne($pm);
         $m->setPromotion($mandat + 2);
         $em->persist($m);
         if ($c !== null) {
             $c->addMembre($m);
         }
         $e->setSuiveur($pm);
         //manage AP & CC
         if ($etude['dateCC'] !== null && $etude['statut'] > 1) {
             $ap = new Ap();
             $ap->setEtude($e);
             $em->persist($ap);
             $cc = new Cc();
             $cc->setEtude($e);
             $cc->setDateSignature(new \DateTime($mandat . '-' . $month . '-' . $day));
             $cc->setSignataire2($pe);
             $em->persist($cc);
         }
         //manage intervenant
         if ($etude['statut'] > 1 && $etude['statut'] < 5) {
             //manage developper
             $dev = new Personne();
             $dev->setPrenom(self::PRENOM[array_rand(self::PRENOM)]);
             $dev->setNom(self::NOM[array_rand(self::NOM)]);
             $dev->setEmailEstValide(false);
             $dev->setEstAbonneNewsletter(false);
             $em->persist($dev);
             $mdev = new Membre();
             $mdev->setPersonne($dev);
             $mdev->setPromotion($mandat + rand(1, 2));
             $em->persist($mdev);
             if ($c !== null) {
                 $c->addMembre($mdev);
             }
             $mi = new Mission();
             $mi->setSignataire2($dev);
             $mi->setEtude($e);
             $mi->setDateSignature(new \DateTime($mandat . '-' . $month . '-' . $day));
             $mi->setDebutOm(new \DateTime($mandat . '-' . $month . '-' . $day));
             $mi->setAvancement(rand(90, 100));
             $mi->setIntervenant($mdev);
             $em->persist($mi);
         }
         //manage PVR
         if ($etude['statut'] > 1 && $etude['statut'] < 5) {
             $pv = new ProcesVerbal();
             $pv->setEtude($e);
             $pv->setDateSignature(new \DateTime($mandat . '-' . ($month + 1) . '-' . $day));
             $pv->setSignataire2($pe);
             $pv->setType('pvr');
             $em->persist($pv);
         }
     }
     $em->flush();
     $output->writeln('Done.');
 }