/**
  * {@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.');
 }
 /**
  * @param UploadedFile $file resources file contzaining data to import.
  * @return mixed Process Import.
  * Process Import.
  */
 public function run(UploadedFile $file)
 {
     if ($file->guessExtension() == "txt") {
         //csv is seen as text/plain
         $i = 1;
         $inserted_projects = 0;
         $inserted_prospects = 0;
         if (($handle = fopen($file->getPathname(), "r")) !== FALSE) {
             $array_manager = array();
             //an array containing references to managers.
             $array_prospect = array();
             //an array containing references to projects.
             //iterate csv, row by row
             while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
                 if ($i > 1 && $this->readArray($data, 'Intitule') != "") {
                     //first row is column headers
                     $etude = $this->em->getRepository('MgateSuiviBundle:Etude')->findOneByNom($this->readArray($data, 'Intitule'));
                     if ($etude === null) {
                         //create project if it doesn't exists in DB
                         $e = new Etude();
                         $inserted_projects++;
                         $e->setMandat($this->readArray($data, 'Exercice comptable'));
                         // $e->setNum($this->readArray($data, 'No Etude')); //untrusted, can be duplicated in siaje.
                         $e->setNom($this->readArray($data, 'Intitule'));
                         $e->setDescription($this->readArray($data, 'Domaine de compétence'));
                         $e->setDateCreation($this->dateManager($this->readArray($data, 'Date d\'ajout')));
                         if (array_key_exists($this->normalize($this->readArray($data, 'Statut')), self::SIAJE_AVAILABLE_STATE)) {
                             $e->setStateID(self::SIAJE_AVAILABLE_STATE[$this->normalize($this->readArray($data, 'Statut'))]);
                         } else {
                             $e->setStateID(self::SIAJE_AVAILABLE_STATE['Contact initial']);
                         }
                         $e->setAcompte(true);
                         if ($this->readArray($data, 'Acompte') !== null) {
                             $rate = explode(',', $this->readArray($data, 'Acompte'));
                             //acompte is a percentage such as "30,00%".
                             $e->setPourcentageAcompte($rate['0'] / 100);
                         }
                         $e->setFraisDossier($this->readArray($data, 'Frais de dossier HT'));
                         $e->setPresentationProjet('Etude importée depuis Siaje');
                         $e->setDescriptionPrestation($this->readArray($data, 'Domaine de compétence'));
                         $e->setPourcentageAcompte($this->readArray($data, 'Acompte'));
                         $this->em->persist($e);
                         /** Prospect management */
                         // Check if a prospect with same already exists in database
                         if ($this->readArray($data, 'Entreprise', true) !== "") {
                             $prospect = $this->em->getRepository('MgatePersonneBundle:Prospect')->findOneByNom($this->readArray($data, 'Entreprise', true));
                             if ($prospect === null) {
                                 //check if prospect already exist in local objects
                                 if (array_key_exists($this->readArray($data, 'Entreprise', true), $array_prospect)) {
                                     $prospect = $array_prospect[$this->readArray($data, 'Entreprise', true)];
                                 }
                             }
                         } else {
                             $prospect = null;
                         }
                         if ($prospect !== null) {
                             $e->setProspect($prospect);
                         } else {
                             $p = new Prospect();
                             $inserted_prospects++;
                             if ($this->readArray($data, 'Entreprise', true) !== "") {
                                 $p->setNom($this->readArray($data, 'Entreprise', true));
                             } else {
                                 $p->setNom('Prospect sans nom ' . rand());
                             }
                             $p->setAdresse($this->readArray($data, 'Adresse'));
                             $p->setCodePostal($this->readArray($data, 'Code Postal'));
                             $p->setVille($this->readArray($data, 'Ville'));
                             $contact = explode(' ', $this->normalize($this->readArray($data, 'Contact', true)));
                             $pe = new Personne();
                             $pe->setPrenom($contact[0]);
                             //whitespace explode : not perfect but better than nothing
                             unset($contact[0]);
                             if (implode(' ', $contact) == "") {
                                 $pe->setNom('inconnu');
                             } else {
                                 $pe->setNom(implode(' ', $contact));
                             }
                             $pe->setEmailEstValide(true);
                             $pe->setEstAbonneNewsletter(false);
                             $pe->setEmail($this->readArray($data, 'Email'));
                             $pe->setAdresse($this->readArray($data, 'Adresse'));
                             $pe->setCodePostal($this->readArray($data, 'Code Postal'));
                             $pe->setVille($this->readArray($data, 'Ville'));
                             $emp = new Employe();
                             $emp->setProspect($p);
                             $p->addEmploye($emp);
                             $emp->setPersonne($pe);
                             $this->em->persist($emp->getPersonne());
                             $this->em->persist($emp);
                             $this->em->persist($p);
                             $e->setProspect($p);
                             $array_prospect[$this->readArray($data, 'Entreprise', true)] = $p;
                         }
                         //create phases
                         $g = new GroupePhases();
                         //default group
                         $g->setTitre('Imported from Siaje');
                         $g->setNumero(1);
                         $g->setDescription('Automatic description');
                         $g->setEtude($e);
                         $this->em->persist($g);
                         $ph = new Phase();
                         $ph->setEtude($e);
                         $ph->setGroupe($g);
                         $ph->setPosition(0);
                         $ph->setNbrJEH($this->readArray($data, 'JEHs'));
                         if ($this->readArray($data, 'JEHs') > 0) {
                             $ph->setPrixJEH(round($this->floatManager($this->readArray($data, 'Montant HT')) / $this->floatManager($this->readArray($data, 'JEHs'))));
                         }
                         $ph->setTitre('Default phase');
                         $ph->setDelai($this->readArray($data, 'Durée en semaine') * 7);
                         $ph->setDateDebut($this->dateManager($this->readArray($data, 'Date signature CC')));
                         $this->em->persist($ph);
                         //manage project manager
                         $contact = explode(' ', $this->normalize($this->readArray($data, 'Suiveur principal', true)));
                         $firstname = $contact[0];
                         unset($contact[0]);
                         $surname = implode(' ', $contact);
                         $pm = $this->em->getRepository('MgatePersonneBundle:Personne')->findOneBy(array('nom' => $surname, 'prenom' => $firstname));
                         if ($pm !== null) {
                             $e->setSuiveur($pm);
                         } else {
                             //create a new member and a new person
                             if (array_key_exists($this->readArray($data, 'Suiveur principal', true), $array_manager) && $this->readArray($data, 'Suiveur principal', true) != '') {
                                 //has already been created before
                                 $e->setSuiveur($array_manager[$this->readArray($data, 'Suiveur principal', true)]);
                             } else {
                                 $pm = new Personne();
                                 $pm->setPrenom($firstname);
                                 if ($surname == "") {
                                     $pm->setNom('inconnu');
                                 } else {
                                     $pm->setNom($surname);
                                 }
                                 $pm->setEmailEstValide(false);
                                 $pm->setEstAbonneNewsletter(false);
                                 $this->em->persist($pm);
                                 $m = new Membre();
                                 $m->setPersonne($pm);
                                 $this->em->persist($m);
                                 $e->setSuiveur($pm);
                                 $array_manager[$this->readArray($data, 'Suiveur principal', true)] = $pm;
                             }
                         }
                         //manage AP & CC
                         if ($this->dateManager($this->readArray($data, 'Date signature CC')) !== null) {
                             $ap = new Ap();
                             $ap->setEtude($e);
                             $this->em->persist($ap);
                             $cc = new Cc();
                             $cc->setEtude($e);
                             $cc->setDateSignature($this->dateManager($this->readArray($data, 'Date signature CC')));
                             if (isset($pe)) {
                                 //if firm has been created in this loop iteration
                                 $cc->setSignataire2($pe);
                             }
                             $this->em->persist($cc);
                         }
                         //manage PVR
                         if ($this->dateManager($this->readArray($data, 'Date signature PV')) !== null) {
                             $pv = new ProcesVerbal();
                             $pv->setEtude($e);
                             $pv->setDateSignature($this->dateManager($this->readArray($data, 'Date signature PV')));
                             $this->em->persist($pv);
                         }
                     }
                 }
                 $i++;
             }
             fclose($handle);
             $this->em->flush();
         }
         return array('inserted_projects' => $inserted_projects, 'inserted_prospects' => $inserted_prospects);
     }
     return array('inserted_projects' => 0, 'inserted_prospects' => 0);
 }