<commit_before><?php

namespace AppBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Doctrine\ODM\MongoDB\Mapping\Annotations\HasLifecycleCallbacks;
use Doctrine\Common\Collections\ArrayCollection;
use AppBundle\Document\Etablissement;
use AppBundle\Document\Compte;
use AppBundle\Document\Prestation;
use AppBundle\Document\Societe;
use AppBundle\Document\ContratPassages;
use AppBundle\Document\Mouvement;
use AppBundle\Model\DocumentFacturableInterface;
use AppBundle\Model\DocumentSocieteInterface;
use AppBundle\Manager\ConfigurationManager;
use AppBundle\Manager\ContratManager;

/**
 * @MongoDB\Document(repositoryClass="AppBundle\Repository\ContratRepository") @HasLifecycleCallbacks
 *
 */
class Contrat implements DocumentSocieteInterface, DocumentFacturableInterface {

    /**
     * @MongoDB\Id(strategy="CUSTOM", type="string", options={"class"="AppBundle\Document\Id\ContratGenerator"})
     */
    protected $id;

    /**
     * @MongoDB\ReferenceMany(targetDocument="Etablissement", inversedBy="contrats", simple=true)
     */
    protected $etablissements;

    /**
     * @MongoDB\ReferenceOne(targetDocument="Compte", simple=true)
     */
    protected $commercial;

    /**
     * @MongoDB\ReferenceOne(targetDocument="Compte", simple=true)
     */
    protected $technicien;

    /**
     * @MongoDB\EmbedMany(targetDocument="ContratPassages", strategy="set")
     */
    protected $contratPassages;

    /**
     * @MongoDB\ReferenceOne(targetDocument="Societe", simple=true)
     */
    protected $societe;

    /**
     * @MongoDB\ReferenceOne(targetDocument="Societe", simple=true)
     */
    protected $commanditaire;

    /**
     * @MongoDB\ReferenceOne()
     */
    protected $devisInterlocuteur;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $identifiant;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $numeroArchive;

    /**
     * @MongoDB\Field(type="int")
     */
    protected $multiTechnicien;

    /**
     * @MongoDB\Field(type="bool")
     */
    protected $reconduit;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $typeContrat;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $typeContratOriginal;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $nomenclature;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $description;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $commentaire;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $markdown;

    /**
     * @MongoDB\EmbedMany(targetDocument="Prestation")
     */
    protected $prestations;

    /**
     * @MongoDB\EmbedMany(targetDocument="Produit")
     */
    protected $produits;

    /**
     * @MongoDB\Field(type="date")
     */
    protected $dateCreation;

    /**
     * @MongoDB\Field(type="date")
     */
    protected $dateCreationAuto;

    /**
     * @MongoDB\Field(type="date")
     */
    protected $dateDebut;

    /**
     * @MongoDB\Field(type="date")
     */
    protected $dateFin;

    /**
     * @MongoDB\Field(type="date")
     */
    protected $dateAcceptation;

    /**
     * @MongoDB\Field(type="date")
     */
    protected $dateResiliation;

    /**
     * @MongoDB\Field(type="date")
     */
    protected $dateReconduction;


    /**
     * @MongoDB\Field(type="int")
     */
    protected $duree;

    /**
     * @MongoDB\Field(type="int")
     */
    protected $dureeGarantie;

    /**
     * @MongoDB\Field(type="int")
     */
    protected $nbPassages;

    /**
     * @MongoDB\Field(type="int")
     */
    protected $dureePassage;

    /**
     * @MongoDB\Field(type="int")
     */
    protected $nbFactures;

    /**
     * @MongoDB\Field(type="float")
     */
    protected $prixHt;

    /**
     * @MongoDB\EmbedMany(targetDocument="Mouvement")
     */
    protected $mouvements;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $statut;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $identifiantReprise;

    /**
     * @MongoDB\Field(type="bool")
     */
    protected $tvaReduite;

    /**
     * @MongoDB\Field(type="collection")
     */
    protected $moyens;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $conditionsParticulieres;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $referenceClient;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $factureDestinataire;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $frequencePaiement;

    /**
     * @MongoDB\Field(type="int")
     */
    protected $auditPassage;

    public function __construct() {
        $this->etablissements = new ArrayCollection();
        $this->prestations = new ArrayCollection();
        $this->produits = new ArrayCollection();
        $this->mouvements = new ArrayCollection();
        $this->contratPassages = array();
    }

    /**
     * Set commercial
     *
     * @param AppBundle\Document\Compte $commercial
     * @return self
     */
    public function setCommercial(\AppBundle\Document\Compte $commercial) {
        $this->commercial = $commercial;
        return $this;
    }

    /**
     * Get commercial
     *
     * @return AppBundle\Document\Compte $commercial
     */
    public function getCommercial() {
        return $this->commercial;
    }

    /**
     * Set identifiant
     *
     * @param string $identifiant
     * @return self
     */
    public function setIdentifiant($identifiant) {
        $this->identifiant = $identifiant;
        return $this;
    }

    /**
     * Get identifiant
     *
     * @return string $identifiant
     */
    public function getIdentifiant() {
        return $this->identifiant;
    }

    /**
     * Set typeContrat
     *
     * @param string $typeContrat
     * @return self
     */
    public function setTypeContrat($typeContrat) {
        $this->typeContrat = $typeContrat;
        if ($typeContrat == ContratManager::TYPE_CONTRAT_ANNULE) {
            $this->setStatut(ContratManager::STATUT_FINI);
        }
        return $this;
    }

    /**
     * Get typeContrat
     *
     * @return string $typeContrat
     */
    public function getTypeContrat() {
        return $this->typeContrat;
    }

    /**
     * Set typeContratOriginal
     *
     * @param string $typeContratOriginal
     * @return self
     */
    public function setTypeContratOriginal($typeContrat) {
        $this->typeContratOriginal = $typeContrat;

        return $this;
    }

    /**
     * Get typeContrat
     *
     * @return string $typeContratOriginal
     */
    public function getTypeContratOriginal() {

        return $this->typeContratOriginal;
    }

    /**
     * Set nomenclature
     *
     * @param string $nomenclature
     * @return self
     */
    public function setNomenclature($nomenclature) {
        $this->nomenclature = $nomenclature;
        return $this;
    }

    /**
     * Get nomenclature
     *
     * @return string $nomenclature
     */
    public function getNomenclature() {
        return $this->nomenclature;
    }



    /**
     * Get nomenclature
     *
     * @return string $nomenclature
     */
    public function getHtmlNomenclature() {
    	return preg_replace('/\*\*(.+)\*\*/', '<strong>$1</strong>', $this->nomenclature);
    }

    /**
     * Add prestation
     *
     * @param AppBundle\Document\Prestation $prestation
     */
    public function addPrestation(\AppBundle\Document\Prestation $prestation) {
        foreach ($this->getPrestations() as $prest) {
            if ($prest->getIdentifiant() == $prestation->getIdentifiant()) {
                return;
            }
        }
        $this->prestations[] = $prestation;
    }

    /**
     * Remove prestation
     *
     * @param AppBundle\Document\Prestation $prestation
     */
    public function removePrestation(\AppBundle\Document\Prestation $prestation) {
        $this->prestations->removeElement($prestation);
    }

    /**
     * Get prestations
     *
     * @return \Doctrine\Common\Collections\Collection $prestations
     */
    public function getPrestations() {
        return $this->prestations;
    }

    public function hasPrestation($part_of_name){
      foreach ($this->getPrestations() as $prestation) {
        if(preg_match("/$part_of_name/",$prestation->getNomToString())){
          return true;
        }
      }
      return false;
    }

    public function getUniquePrestations() {
        $prestations = array();

        foreach ($this->getPrestations() as $prestation) {
            $prestations[$prestation->getIdentifiant()] = $prestation->getNom();
        }

        return $prestations;
    }

    public function setUniquePrestations($prestationsIdentifiant) {
        $this->prestations = new ArrayCollection();

        foreach ($prestationsIdentifiant as $identifiant) {
            $prestation = new Prestation();
            $prestation->setIdentifiant($identifiant);
            $prestation->setNbPassages(1);
            $this->addPrestation($prestation);
        }

        return $this;
    }

    /**
     * Add produit
     *
     * @param AppBundle\Document\Produit $prestation
     */
    public function addProduit(\AppBundle\Document\Produit $produit) {
        foreach ($this->getProduits() as $prod) {
            if ($prod == $produit) {
                return;
            }
        }
        $this->produits[] = $produit;
    }

    /**
     * Remove produit
     *
     * @param AppBundle\Document\Produit $produit
     */
    public function removeProduit(\AppBundle\Document\Produit $produit) {
        $this->produits->removeElement($produit);
    }

    /**
     * Get produits
     *
     * @return \Doctrine\Common\Collections\Produit $produits
     */
    public function getProduits() {
        return $this->produits;
    }

    public function getProduit($identifiant){
      foreach ($this->produits as $produit) {
        if($produit->getIdentifiant() == $identifiant){
          return $produit;
        }
      }
      return null;
    }

    /**
     * Set dateCreation
     *
     * @param date $dateCreation
     * @return self
     */
    public function setDateCreation($dateCreation) {
        $this->dateCreation = $dateCreation;
        return $this;
    }

    /**
     * Get dateCreation
     *
     * @return date $dateCreation
     */
    public function getDateCreation() {
        return $this->dateCreation;
    }

    /**
     * Set dateCreationAuto
     *
     * @param date $dateCreationAuto
     * @return self
     */
    public function setDateCreationAuto($dateCreationAuto) {
        $this->dateCreationAuto = $dateCreationAuto;
        return $this;
    }

    /**
     * Get dateCreationAuto
     *
     * @return date $dateCreationAuto
     */
    public function getDateCreationAuto() {
        return $this->dateCreationAuto;
    }

    /**
     * Set dateDebut
     *
     * @param date $dateDebut
     * @return self
     */
    public function setDateDebut($dateDebut) {
        $this->dateDebut = $dateDebut;
        return $this;
    }

    /**
     * Get dateDebut
     *
     * @return date $dateDebut
     */
    public function getDateDebut() {
        return $this->dateDebut;
    }

    /**
     * Set dateFin
     *
     * @param date $dateFin
     * @return self
     */
    public function setDateFin($dateFin) {
        $this->dateFin = $dateFin;
        return $this;
    }

    /**
     * Get dateFin
     *
     * @return date $dateFin
     */
    public function getDateFin() {
        return $this->dateFin;
    }

    /**
     * Set dateAcceptation
     *
     * @param date $dateAcceptation
     * @return self
     */
    public function setDateAcceptation($dateAcceptation) {
        $this->dateAcceptation = $dateAcceptation;
        return $this;
    }

    /**
     * Get dateAcceptation
     *
     * @return date $dateAcceptation
     */
    public function getDateAcceptation() {
        return $this->dateAcceptation;
    }

    /**
     * Set duree
     *
     * @param int $duree
     * @return self
     */
    public function setDuree($duree) {
        $this->duree = $duree;
        return $this;
    }

    /**
     * Get duree
     *
     * @return int $duree
     */
    public function getDuree() {
        return $this->duree;
    }

    /**
     * Set dureeGarantie
     *
     * @param int $dureeGarantie
     * @return self
     */
    public function setDureeGarantie($dureeGarantie) {
        $this->dureeGarantie = $dureeGarantie;
        return $this;
    }

    /**
     * Get dureeGarantie
     *
     * @return int $dureeGarantie
     */
    public function getDureeGarantie() {
        return $this->dureeGarantie;
    }

    /**
     * Set nbPassages
     *
     * @param int $nbPassages
     * @return self
     */
    public function setNbPassages($nbPassages) {
        $this->nbPassages = $nbPassages;
        return $this;
    }

    /**
     * Get nbPassages
     *
     * @return int $nbPassages
     */
    public function getNbPassages() {
        if (!$this->nbPassages) {
            $nbPassagesEtb = array();
            foreach ($this->getContratPassages() as $contratPassage) {
                $nbPassagesEtb[$contratPassage->getEtablissement()->getId()] = 0;
                foreach ($contratPassage->getPassages() as $p) {
                    if ($p->isSousContrat()) {
                        $nbPassagesEtb[$contratPassage->getEtablissement()->getId()] = $nbPassagesEtb[$contratPassage->getEtablissement()->getId()] + 1;
                    }
                }
            }
            if ($nbPassagesEtb && count($nbPassagesEtb)) {
                $this->setNbPassages(max($nbPassagesEtb));
            } else {
                $this->setNbPassages(0);
            }
        }

        return $this->nbPassages;
    }

    /**
     * Set dureePassage
     *
     * @param int $dureePassage
     * @return self
     */
    public function setDureePassage($dureePassage) {
        $this->dureePassage = $dureePassage;
        return $this;
    }

    /**
     * Get dureePassage
     *
     * @return int $dureePassage
     */
    public function getDureePassage() {
        return $this->dureePassage;
    }

    public function getDureePassageFormat() {
        $minute = $this->getDureePassage();
        $heure = intval(abs($minute / 60));
        $minute = $minute - ($heure * 60);
        return sprintf("%02dh%02d", $heure, $minute);
    }

    /**
     * Set nbFactures
     *
     * @param int $nbFactures
     * @return self
     */
    public function setNbFactures($nbFactures) {
        $this->nbFactures = $nbFactures;
        return $this;
    }

    /**
     * Get nbFactures
     *
     * @return int $nbFactures
     */
    public function getNbFactures() {
        return $this->nbFactures;
    }

    /**
     * Set prixHt
     *
     * @param float $prixHt
     * @return self
     */
    public function setPrixHt($prixHt) {
        $this->prixHt = $prixHt;
        return $this;
    }

    /**
     * Get prixHt
     *
     * @return float $prixHt
     */
    public function getPrixHt() {
        return $this->prixHt;
    }

    /**
     * Set statut
     *
     * @param string $statut
     * @return self
     */
    public function setStatut($statut) {
        $this->statut = $statut;
        return $this;
    }

    /**
     * Get statut
     *
     * @return string $statut
     */
    public function getStatut() {
        return $this->statut;
    }

    /**
     * Set auditPassage
     *
     * @param int $auditPassage
     * @return self
     */
    public function setAuditPassage($auditPassage) {
        $this->auditPassage = $auditPassage;
        return $this;
    }

    /**
     * Get auditPassage
     *
     * @return int $auditPassage
     */
    public function getAuditPassage() {
        return $this->auditPassage;
    }

    public function updateObject() {
            $max = 0;
            foreach ($this->getPrestations() as $prestation) {
                if ($prestation->getNbPassages() > $max) {
                    $max = $prestation->getNbPassages();
                }
            }
            $this->setNbPassages($max);
    }

    public function updatePassages() {

    }

    public function updatePrestations($dm) {
        $cm = new ConfigurationManager($dm);
        $configuration = $cm->getRepository()->findOneById(Configuration::PREFIX);
        $prestationArray = $configuration->getPrestationsArray();
        foreach ($this->getPrestations() as $prestation) {
            $prestationNom = $prestationArray[$prestation->getIdentifiant()];
            $prestation->setNom($prestationNom);
        }
    }

    public function updateProduits($dm) {
        $cm = new ConfigurationManager($dm);
        $configuration = $cm->getRepository()->findOneById(Configuration::PREFIX);
        $produitsArray = $configuration->getProduitsArray();
        foreach ($this->getProduits() as $produit) {
            $produitConf = $produitsArray[$produit->getIdentifiant()];
            $produit->setNom($produitConf->getNom());
            $produit->setPrixHt($produitConf->getPrixHt());
            $produit->setPrixPrestation($produitConf->getPrixPrestation());
        }
    }

    public function getHumanDureePassage() {
        $duree = $this->getDureePassage();
        $heure = floor($duree / 60);
        return sprintf('%02d', $heure) . 'h' . sprintf('%02d', ((($duree / 60) - $heure) * 60));
    }

    public function getPrixMouvements() {
        $prix = 0;
        foreach ($this->getMouvements() as $mouvement) {
            $prix = $prix + $mouvement->getPrixUnitaire();
        }

        return $prix;
    }

    public function getPrixFactures() {
        $prix = 0;
        foreach ($this->getMouvements() as $mouvement) {
          if($mouvement->isFacture()){
            $prix = $prix + $mouvement->getPrixUnitaire();
            }
        }
        return $prix;
    }

    public function getPrixRestant() {
        $prixMouvement = $this->getPrixMouvements();

        return $this->getPrixHt() - $this->getPrixMouvements();
    }

    public function getNbPassagesPrevu() {
        return $this->getNbPassages();
    }

    public function getNbFacturesRestantes() {
    $nbFacturesGenerees = 0;
      foreach ($this->getMouvements() as $mouvement) {
        if($mouvement->getPrixUnitaire() > 0){
          $nbFacturesGenerees++;
        }else{
          $nbFacturesGenerees--;
        }
      }
      return $this->getNbFactures() - $nbFacturesGenerees;
    }

    public function generateMouvement($origineDocumentGeneration = null) {
        if ($this->getPrixRestant() <= 0 || $this->getNbFacturesRestantes() <= 0) {
            return null;
        }

        $mouvement = $this->buildMouvement($origineDocumentGeneration);

        $this->addMouvement($mouvement);

        return $mouvement;
    }

    public function buildMouvement($origineDocumentGeneration = null) {
        if ($this->getPrixRestant() <= 0 || $this->getNbFacturesRestantes() <= 0) {
            return null;
        }

        $mouvement = new Mouvement();
        $mouvement->setIdentifiant(uniqid());
        $mouvement->setPrixUnitaire(round($this->getPrixRestant() / $this->getNbFacturesRestantes(), 2));
        $mouvement->setQuantite(1);
        $mouvement->setTauxTaxe($this->getTva());
        $mouvement->setFacturable(true);
        $mouvement->setFacture(false);
        $mouvement->setSociete($this->getSociete());
        $mouvement->setLibelle(sprintf("Facture %s/%s - Proposition n° %s du %s au %s", count($this->getMouvements()) + 1, $this->getNbFactures(), $this->getNumeroArchive(), $this->getDateDebut()->format('m/Y'), $this->getDateFin()->format('m/Y')));

        $mouvement->setDocument($this);
        if ($origineDocumentGeneration) {
            $mouvement->setOrigineDocumentGeneration($origineDocumentGeneration);
        }

        return $mouvement;
    }

    public function restaureMouvements($documentGenere = null) {
      foreach ($this->getMouvements() as $mouvement) {
        if ($documentGenere){
          $mvtToRestaure = null;
          foreach ($documentGenere->getLignes() as $ligne) {
            if($ligne->getOrigineMouvement() == $mouvement->getIdentifiant()){
              $mvtToRestaure = $mouvement;
              break;
            }
          }
          if($mvtToRestaure){
            $mvtToRestaure->setFacturable(true);
            $mvtToRestaure->setFacture(false);

          }
        }else{
          $mouvement->setFacturable(true);
          $mouvement->setFacture(false);
        }
      }
    }

    public function resetFacturableMouvement($identifiant)
    {
    	foreach ($this->getMouvements() as $mouvement) {
    		if ($identifiant == $mouvement->getIdentifiant()) {
    			$this->removeMouvement($mouvement);
    		}
    	}
    }


    public function hasMouvements() {
        return boolval(count($this->getMouvements()));
    }

    public function getPrevisionnel($dateDebut = null) {
        if (!$dateDebut) {
            $dateDebut = new \DateTime();
        }

        $dureeContratMois = $this->getDuree();

        $maxNbPrestations = 0;
        $typePrestationPrincipal = "";
        foreach ($this->getPrestations() as $prestation) {
            if ($prestation->getNbPassages() > $maxNbPrestations) {
                $maxNbPrestations = $prestation->getNbPassages();
                $typePrestationPrincipal = $prestation;
            }
        }
        $passagesDatesArray = array();
        if (!count($this->getPrestations())) {
            return $passagesDatesArray;
        }
        if (!$maxNbPrestations) {
            return $passagesDatesArray;
        }
        $monthInterval = (floatval($dureeContratMois) / floatval($maxNbPrestations));
        $nb_month = intval($monthInterval);
        $dateLastPassage = clone $dateDebut;
        $passagesDatesArray[$dateLastPassage->format('Y-m-d')] = new \stdClass();
        $passagesDatesArray[$dateLastPassage->format('Y-m-d')]->prestations = array();

        foreach ($this->getPrestations() as $prestation) {
            if ($prestation->getNbPassages() > 0) {
                $passagesDatesArray[$dateLastPassage->format('Y-m-d')]->prestations[] = $prestation;
                $passagesDatesArray[$dateLastPassage->format('Y-m-d')]->mouvement_declenchable = 1;
            }
        }

        for ($index = 1; $index < $maxNbPrestations; $index++) {
            $monthDate = clone $dateLastPassage;
            $nextMonth = $monthDate->modify("+" . $nb_month . " month");
            $nb_days = intval(($monthInterval - $nb_month) * cal_days_in_month(CAL_GREGORIAN, $nextMonth->format('m'), $nextMonth->format('Y')));

            $dateLastPassage = $dateLastPassage->modify("+" . $nb_month . " month")->modify("+" . $nb_days . " day");
            $passagesDatesArray[$dateLastPassage->format('Y-m-d')] = new \stdClass();
            $passagesDatesArray[$dateLastPassage->format('Y-m-d')]->prestations = array();
            $passagesDatesArray[$dateLastPassage->format('Y-m-d')]->prestations[] = $typePrestationPrincipal;
        }

        foreach ($this->getPrestations() as $prestation) {
            if (($prestation->getIdentifiant() != $typePrestationPrincipal->getIdentifiant()) && $prestation->getNbPassages() > 1) {
                $nbPassagesPrestationRestant = $prestation->getNbPassages();
                $nbPassagesRestant = count($passagesDatesArray);
                $occurPassage = (floatval($nbPassagesRestant) / floatval($nbPassagesPrestationRestant));
                $compteurPassage = $occurPassage;
                $cpt = 0;
                foreach ($passagesDatesArray as $date => $passage) {
                    if ($cpt < 1) {
                        $cpt++;
                        continue;
                    }
                    if ($cpt >= $compteurPassage) {
                        $passagesDatesArray[$date]->prestations[] = $prestation;
                        $compteurPassage+=$occurPassage;
                    }
                    $cpt++;
                }
            }
        }
        $facturationInterval = 0;
        if(floatval($this->getNbFactures())){
          $facturationInterval = (floatval($maxNbPrestations) / floatval($this->getNbFactures()));
        }
        $compteurFacturation = $facturationInterval;
        $cpt = 0;

        foreach ($passagesDatesArray as $date => $passage) {
            if ($cpt < 1) {
                $passagesDatesArray[$date]->mouvement_declenchable = boolval($this->getNbFactures());
                $compteurFacturation--;
                $cpt++;
                continue;
            }
            if ($cpt >= $compteurFacturation) {
                $passagesDatesArray[$date]->mouvement_declenchable = boolval($this->getNbFactures());
                $compteurFacturation+=$facturationInterval;
            } else {
                $passagesDatesArray[$date]->mouvement_declenchable = 0;
            }
            $cpt++;
        }
        $cpt = 0;
        foreach ($passagesDatesArray as $date => $passage) {
        	$passagesDatesArray[$date]->audit = 0;
        	if ($cpt == ($this->getAuditPassage()-1)) {
        		$passagesDatesArray[$date]->audit = ($this->getCommercial()->getIdentite())? $this->getCommercial()->getIdentite() : 'Commercial';
        	}
        	$cpt++;
        }
        krsort($passagesDatesArray);
        return $passagesDatesArray;
    }

    public function getTypeContratLibelle() {
        if (!$this->getTypeContrat()) {
            return "";
        }
        return ContratManager::$types_contrat_libelles[$this->getTypeContrat()];
    }

    public function getTypeContratOriginalLibelle() {
        if (!$this->getTypeContratOriginal()) {
            return "";
        }
        return ContratManager::$types_contrat_libelles[$this->getTypeContratOriginal()];
    }

    public function getStatutLibelle() {
        $today = new \DateTime();
        if($this->isEnCours() && $this->getDateDebut() && ($today->format("Ymd") < $this->getDateDebut()->format("Ymd"))){
          return "A venir";
        }elseif($this->isFini() && $this->getDateFin() && ($today->format("Ymd") < $this->getDateFin()->format("Ymd"))){
          return "En cours (réalisé)";
        }
        return ContratManager::$statuts_libelles[$this->getStatut()];
    }

    public function getStatutLibelleLong() {
        $today = new \DateTime();
        if($this->isEnCours() && $this->getDateDebut() && ($today->format("Ymd") < $this->getDateDebut()->format("Ymd"))) {

            return "à venir";
        } elseif($this->isFini() && $this->getDateFin() && ($today->format("Ymd") < $this->getDateFin()->format("Ymd"))) {

            return "en cours réalisé";
        }

        return ContratManager::$statuts_libelles_long[$this->getStatut()];
    }

    public function getStatutCouleur() {
        if ($this->isAnnule()) {
            return ContratManager::$statuts_couleurs[$this->getTypeContrat()];
        }
      $today = new \DateTime();
      if($this->isEnCours() && $this->getDateDebut() && ($today->format("Ymd") < $this->getDateDebut()->format("Ymd"))){
        return "default";
      }elseif($this->isFini() && $this->getDateFin() && ($today->format("Ymd") < $this->getDateFin()->format("Ymd"))){
        return "info";
      }

        return ContratManager::$statuts_couleurs[$this->getStatut()];
    }

    public function getPassagesEtablissementNode(Etablissement $etablissement) {
        $contratPassages = $this->getContratPassages();
        if (!isset($contratPassages[$etablissement->getId()])) {
            return null;
        }

        return $contratPassages[$etablissement->getId()];
    }

    public function hasEtablissementNode(Etablissement $etablissement) {
        $etablissements = array();
        foreach ($this->getEtablissements() as $etab) {
        	$etablissements[] = $etab->getId();
        }
        return in_array($etablissement->getId(), $etablissements);
    }

    public static function cmpContrat($a, $b) {
        $statutsPositions = ContratManager::$statuts_positions;
        $pa = ($a->getStatut()) ? $statutsPositions[$a->getStatut()] : 99;
        $pb = ($b->getStatut()) ? $statutsPositions[$b->getStatut()] : 99;
        if ($pa == $pb) {
            $paDate = ($a->getDateDebut()) ? $a->getDateDebut() : $a->getDateCreation();
            $pbDate = ($b->getDateDebut()) ? $b->getDateDebut() : $b->getDateCreation();
            if ($paDate->format('Ymd') == $pbDate->format('Ymd')) {
                return 0;
            } else {
                return ($paDate->format('Ymd') < $pbDate->format('Ymd')) ? +1 : -1;
            }
        }
        return ($pa > $pb) ? +1 : -1;
    }

    public function getPassages(Etablissement $etablissement) {
        if (!isset($this->contratPassages[$etablissement->getId()])) {
            return array();
        }

        return $this->contratPassages[$etablissement->getId()]->getPassagesSorted();
    }

    public function getUniquePassage() {
        if ($this->getNbPassages() != 1 || count($this->getEtablissements()) > 1) {

            throw new \Exception("Il y a plusieurs passage pour ce contrat");
        }
        try{
          foreach ($this->getContratPassages() as $contratPassage) {
              foreach ($contratPassage->getPassagesSorted() as $passage) {
                return $passage;
            }
          }
        }catch(Exception $e){
          throw new \Exception("Il y a un problème avec le contrat ".$this->getId());
        }
        return null;
    }

    /**
     * Add etablissement
     *
     * @param AppBundle\Document\Etablissement $etablissement
     */
    public function addEtablissement(\AppBundle\Document\Etablissement $etablissement) {
        foreach ($this->getEtablissements() as $etb) {
            if ($etb->getId() == $etablissement->getId()) {
                return;
            }
        }
        $this->etablissements[] = $etablissement;
    }

    /**
     * Remove etablissement
     *
     * @param AppBundle\Document\Etablissement $etablissement
     */
    public function removeEtablissement(\AppBundle\Document\Etablissement $etablissement) {
        $this->etablissements->removeElement($etablissement);
    }

    /**
     * Get etablissements
     *
     * @return \Doctrine\Common\Collections\Collection $etablissements
     */
    public function getEtablissements() {
        return $this->etablissements;
    }

    /**
     * Set technicien
     *
     * @param AppBundle\Document\Compte $technicien
     * @return self
     */
    public function setTechnicien(\AppBundle\Document\Compte $technicien) {
        $this->technicien = $technicien;
        return $this;
    }

    /**
     * Get technicien
     *
     * @return AppBundle\Document\Compte $technicien
     */
    public function getTechnicien() {
        return $this->technicien;
    }

    public function changeTechnicien($newTechnicien) {
        if (!$newTechnicien) {
            return false;
        }
        $this->setTechnicien($newTechnicien);
        foreach ($this->getContratPassages() as $contratPassage) {
            foreach ($contratPassage->getPassagesSorted() as $passage) {
                if ($passage->isAPlanifie()) {
                    $passage->removeAllTechniciens();
                    $passage->addTechnicien($newTechnicien);
                }
            }
        }
    }

    public function getTechnicienPlusUtilise(){
      $techniciensArray = array();
      foreach ($this->getContratPassages() as $contratPassage) {
          foreach ($contratPassage->getPassagesSorted() as $passage) {
              foreach ($passage->getTechniciens() as $technicien) {
                if(!array_key_exists($technicien->getId(),$techniciensArray)){
                  $techniciensArray[$technicien->getId()] = new \stdClass();
                  $techniciensArray[$technicien->getId()]->occurs = 0;
                  $techniciensArray[$technicien->getId()]->technicien = $technicien;
                }
                $techniciensArray[$technicien->getId()]->occurs = $techniciensArray[$technicien->getId()]->occurs + 1;
              }
          }
      }
      $techNum = 0;
      $tech = null;
      foreach ($techniciensArray as $techId => $techRefs) {
        if($techRefs->occurs > $techNum){
          $tech = $techRefs->technicien;
        }
      }
      return $tech;
    }

    /**
     * Add contratPassage
     *
     * @param AppBundle\Document\Passage $passage
     */
    public function addPassage(\AppBundle\Document\Etablissement $etablissement, Passage $passage) {
        $contratPassagesToSet = new ContratPassages();
        foreach ($this->getContratPassages() as $contratPassages) {
            if ($etablissement->getId() == $contratPassages->getEtablissement()->getId()) {
                $contratPassagesToSet = $contratPassages;
            }
        }
        $contratPassagesToSet->addPassage($passage);
        $contratPassagesToSet->setEtablissement($etablissement);

        $this->addContratPassage($etablissement, $contratPassagesToSet);
    }

    /**
     * Add contratPassage
     *
     * @param AppBundle\Document\ContratPassages $contratPassage
     */
    public function addContratPassage($etablissement, \AppBundle\Document\ContratPassages $contratPassage) {
        $this->contratPassages[$etablissement->getId()] = $contratPassage;
    }

    /**
     * Remove contratPassage
     *
     * @param AppBundle\Document\ContratPassages $contratPassage
     */
    public function removeContratPassage(\AppBundle\Document\ContratPassages $contratPassage) {
        $this->contratPassages->removeElement($contratPassage);
    }

    /**
     * Get contratPassages
     *
     * @return \Doctrine\Common\Collections\Collection $contratPassages
     */
    public function getContratPassages() {
        return $this->contratPassages;
    }

    public function reInitContratPassages() {
        $this->contratPassages = array();
    }

    /**
     * Set societe
     *
     * @param AppBundle\Document\Societe $societe
     * @return self
     */
    public function setSociete(\AppBundle\Document\Societe $societe) {
        $this->societe = $societe;
        return $this;
    }

    /**
     * Get societe
     *
     * @return AppBundle\Document\Societe $societe
     */
    public function getSociete() {
        return $this->societe;
    }

    /**
     * Set multiTechnicien
     *
     * @param int $multiTechnicien
     * @return self
     */
    public function setMultiTechnicien($multiTechnicien) {
        $this->multiTechnicien = $multiTechnicien;
        return $this;
    }

    /**
     * Get multiTechnicien
     *
     * @return int $multiTechnicien
     */
    public function getMultiTechnicien() {
        return $this->multiTechnicien;
    }

    /**
     * Set numeroArchive
     *
     * @param string $numeroArchive
     * @return self
     */
    public function setNumeroArchive($numeroArchive) {
        $this->numeroArchive = $numeroArchive;
        return $this;
    }

    /**
     * Get numeroArchive
     *
     * @return string $numeroArchive
     */
    public function getNumeroArchive() {
        return $this->numeroArchive;
    }

    /**
     * Set identifiantReprise
     *
     * @param string $identifiantReprise
     * @return self
     */
    public function setIdentifiantReprise($identifiantReprise) {
        $this->identifiantReprise = $identifiantReprise;
        return $this;
    }

    /**
     * Get identifiantReprise
     *
     * @return string $identifiantReprise
     */
    public function getIdentifiantReprise() {
        return $this->identifiantReprise;
    }

    /**
     * Get id
     *
     * @return string $id
     */
    public function getId() {
        return $this->id;
    }

    /**
     * Set dateResiliation
     *
     * @param date $dateResiliation
     * @return self
     */
    public function setDateResiliation($dateResiliation) {
        $this->dateResiliation = $dateResiliation;
        return $this;
    }

    /**
     * Get dateResiliation
     *
     * @return date $dateResiliation
     */
    public function getDateResiliation() {
        return $this->dateResiliation;
    }

    /**
     * Set tvaReduite
     *
     * @param boolean $tvaReduite
     * @return self
     */
    public function setTvaReduite($tvaReduite) {
        $this->tvaReduite = $tvaReduite;
        return $this;
    }

    /**
     * Get tvaReduite
     *
     * @return boolean $tvaReduite
     */
    public function getTvaReduite() {

        return $this->tvaReduite;
    }

    public function isModifiable() {

        if($this->hasMouvements()) {

            return false;
        }

        if ($this->isEnAttenteAcceptation() || $this->isBrouillon()) {
            return true;
        }
        if ($this->isEnCours()) {
            foreach ($this->getContratPassages() as $contratPassage) {
                foreach ($contratPassage->getPassages() as $p) {
                    if ($p->isPlanifie() || $p->isRealise() || $p->isAnnule()) {
                        return false;
                    }
                }
            }
            return true;
        }
        return false;
    }

    public function isAnnulable() {
        return (($this->isEnCours()  || $this->isFini()) && !$this->isAnnule());
    }

    public function isReactivable() {
        return ($this->isAnnule() && $this->getTypeContratOriginal());
    }

    /*
     * Fonction à retiré => un contrat ne doit pas être resilié sous forme de statut mais sous forme de type
     */

    public function isResilie() {

        return ($this->statut == ContratManager::STATUT_RESILIE);
    }

    public function isAnnule() {

        return ($this->typeContrat == ContratManager::TYPE_CONTRAT_ANNULE);
    }

    public function isBrouillon() {
        return ($this->statut == ContratManager::STATUT_BROUILLON);
    }

    public function isEnCours() {
        return ($this->statut == ContratManager::STATUT_EN_COURS);
    }


    public function isEnAttenteAcceptation() {

        return ($this->statut == ContratManager::STATUT_EN_ATTENTE_ACCEPTATION);
    }

    public function isFini() {

        return ($this->statut == ContratManager::STATUT_FINI);
    }

    public function removeId() {
        $this->id = null;
    }

    public function removeAllEtablissements(){
      $this->etablissements = new ArrayCollection();
    }

    public function copier() {
        $contrat = clone $this;
        $contrat->removeId();
        $contrat->setNumeroArchive(null);
        $contrat->setIdentifiant(null);
        $contrat->setReferenceClient(null);
        $contrat->setDateAcceptation(null);
        $contrat->setDateDebut(null);
        $contrat->setDateFin(null);
        $contrat->setDateCreation(new \DateTime());
        $contrat->setDateCreationAuto(new \DateTime());
        $contrat->setStatut(ContratManager::STATUT_EN_ATTENTE_ACCEPTATION);
        $contrat->contratPassages = array();
        $contrat->cleanMouvements();
        $contrat->setReconduit(false);
        $contrat->updateObject();

        return $contrat;
    }

    public function reconduire($augmentation = 0) {
        if($this->getReconduit()) {

            throw new \Exception("Ce contrat à déjà été reconduit");
        }
        $contrat = clone $this;
        $contrat->removeId();
        $contrat->setIdentifiant(null);
        if (!$contrat->isKeepNumeroArchivage()) {
            $contrat->setNumeroArchive(null);
        }
        foreach ($contrat->getEtablissements() as $etb) {
          $this->addEtablissement($etb);
        }
        if ($augmentation > 0) {
        	$contrat->setPrixHt(round($this->getPrixHt() * (1+($augmentation/100)),2));
        }

        $dateDebut = clone $contrat->getDateDebut();
        $dateAcceptation = clone $contrat->getDateAcceptation();
        $dateFin= clone $contrat->getDateFin();
        $nbMois = $contrat->getDuree();

        $dateDebut = $dateDebut->modify("+" . $nbMois . " month");
        $dateFin = $dateFin->modify("+" . $nbMois . " month");

        $contrat->setDateDebut($dateDebut);
        $contrat->setDateFin($dateFin);
        $contrat->setDateCreation(new \DateTime());
        $contrat->setDateCreationAuto(new \DateTime());
        $contrat->setAuditPassage(null);

        if($contrat->isTypeReconductionTacite()){
          $contrat->setStatut(ContratManager::STATUT_EN_COURS);
          $contrat->setDateAcceptation($dateAcceptation);
        }else{
          $contrat->setStatut(ContratManager::STATUT_EN_ATTENTE_ACCEPTATION);
          $contrat->setDateAcceptation(null);
        }

        $contrat->removeAllEtablissements();

        foreach ($this->getEtablissements() as $etablissement) {
          $contrat->addEtablissement($etablissement);
        }
        $contrat->contratPassages = array();
        $contrat->cleanMouvements();
        $contrat->setReconduit(false);
        $contrat->setDateReconduction(new \DateTime());
        $contrat->setReferenceClient(null);
        if(!$contrat->getTechnicien()){
            $technicien = $this->getTechnicienPlusUtilise();
            if($technicien) {
                $contrat->setTechnicien($technicien);<commit_msg>lors de la reconduction on garde le commercial pour les audit<commit_after><?php

namespace AppBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Doctrine\ODM\MongoDB\Mapping\Annotations\HasLifecycleCallbacks;
use Doctrine\Common\Collections\ArrayCollection;
use AppBundle\Document\Etablissement;
use AppBundle\Document\Compte;
use AppBundle\Document\Prestation;
use AppBundle\Document\Societe;
use AppBundle\Document\ContratPassages;
use AppBundle\Document\Mouvement;
use AppBundle\Model\DocumentFacturableInterface;
use AppBundle\Model\DocumentSocieteInterface;
use AppBundle\Manager\ConfigurationManager;
use AppBundle\Manager\ContratManager;

/**
 * @MongoDB\Document(repositoryClass="AppBundle\Repository\ContratRepository") @HasLifecycleCallbacks
 *
 */
class Contrat implements DocumentSocieteInterface, DocumentFacturableInterface {

    /**
     * @MongoDB\Id(strategy="CUSTOM", type="string", options={"class"="AppBundle\Document\Id\ContratGenerator"})
     */
    protected $id;

    /**
     * @MongoDB\ReferenceMany(targetDocument="Etablissement", inversedBy="contrats", simple=true)
     */
    protected $etablissements;

    /**
     * @MongoDB\ReferenceOne(targetDocument="Compte", simple=true)
     */
    protected $commercial;

    /**
     * @MongoDB\ReferenceOne(targetDocument="Compte", simple=true)
     */
    protected $technicien;

    /**
     * @MongoDB\EmbedMany(targetDocument="ContratPassages", strategy="set")
     */
    protected $contratPassages;

    /**
     * @MongoDB\ReferenceOne(targetDocument="Societe", simple=true)
     */
    protected $societe;

    /**
     * @MongoDB\ReferenceOne(targetDocument="Societe", simple=true)
     */
    protected $commanditaire;

    /**
     * @MongoDB\ReferenceOne()
     */
    protected $devisInterlocuteur;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $identifiant;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $numeroArchive;

    /**
     * @MongoDB\Field(type="int")
     */
    protected $multiTechnicien;

    /**
     * @MongoDB\Field(type="bool")
     */
    protected $reconduit;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $typeContrat;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $typeContratOriginal;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $nomenclature;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $description;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $commentaire;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $markdown;

    /**
     * @MongoDB\EmbedMany(targetDocument="Prestation")
     */
    protected $prestations;

    /**
     * @MongoDB\EmbedMany(targetDocument="Produit")
     */
    protected $produits;

    /**
     * @MongoDB\Field(type="date")
     */
    protected $dateCreation;

    /**
     * @MongoDB\Field(type="date")
     */
    protected $dateCreationAuto;

    /**
     * @MongoDB\Field(type="date")
     */
    protected $dateDebut;

    /**
     * @MongoDB\Field(type="date")
     */
    protected $dateFin;

    /**
     * @MongoDB\Field(type="date")
     */
    protected $dateAcceptation;

    /**
     * @MongoDB\Field(type="date")
     */
    protected $dateResiliation;

    /**
     * @MongoDB\Field(type="date")
     */
    protected $dateReconduction;


    /**
     * @MongoDB\Field(type="int")
     */
    protected $duree;

    /**
     * @MongoDB\Field(type="int")
     */
    protected $dureeGarantie;

    /**
     * @MongoDB\Field(type="int")
     */
    protected $nbPassages;

    /**
     * @MongoDB\Field(type="int")
     */
    protected $dureePassage;

    /**
     * @MongoDB\Field(type="int")
     */
    protected $nbFactures;

    /**
     * @MongoDB\Field(type="float")
     */
    protected $prixHt;

    /**
     * @MongoDB\EmbedMany(targetDocument="Mouvement")
     */
    protected $mouvements;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $statut;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $identifiantReprise;

    /**
     * @MongoDB\Field(type="bool")
     */
    protected $tvaReduite;

    /**
     * @MongoDB\Field(type="collection")
     */
    protected $moyens;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $conditionsParticulieres;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $referenceClient;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $factureDestinataire;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $frequencePaiement;

    /**
     * @MongoDB\Field(type="int")
     */
    protected $auditPassage;

    public function __construct() {
        $this->etablissements = new ArrayCollection();
        $this->prestations = new ArrayCollection();
        $this->produits = new ArrayCollection();
        $this->mouvements = new ArrayCollection();
        $this->contratPassages = array();
    }

    /**
     * Set commercial
     *
     * @param AppBundle\Document\Compte $commercial
     * @return self
     */
    public function setCommercial(\AppBundle\Document\Compte $commercial) {
        $this->commercial = $commercial;
        return $this;
    }

    /**
     * Get commercial
     *
     * @return AppBundle\Document\Compte $commercial
     */
    public function getCommercial() {
        return $this->commercial;
    }

    /**
     * Set identifiant
     *
     * @param string $identifiant
     * @return self
     */
    public function setIdentifiant($identifiant) {
        $this->identifiant = $identifiant;
        return $this;
    }

    /**
     * Get identifiant
     *
     * @return string $identifiant
     */
    public function getIdentifiant() {
        return $this->identifiant;
    }

    /**
     * Set typeContrat
     *
     * @param string $typeContrat
     * @return self
     */
    public function setTypeContrat($typeContrat) {
        $this->typeContrat = $typeContrat;
        if ($typeContrat == ContratManager::TYPE_CONTRAT_ANNULE) {
            $this->setStatut(ContratManager::STATUT_FINI);
        }
        return $this;
    }

    /**
     * Get typeContrat
     *
     * @return string $typeContrat
     */
    public function getTypeContrat() {
        return $this->typeContrat;
    }

    /**
     * Set typeContratOriginal
     *
     * @param string $typeContratOriginal
     * @return self
     */
    public function setTypeContratOriginal($typeContrat) {
        $this->typeContratOriginal = $typeContrat;

        return $this;
    }

    /**
     * Get typeContrat
     *
     * @return string $typeContratOriginal
     */
    public function getTypeContratOriginal() {

        return $this->typeContratOriginal;
    }

    /**
     * Set nomenclature
     *
     * @param string $nomenclature
     * @return self
     */
    public function setNomenclature($nomenclature) {
        $this->nomenclature = $nomenclature;
        return $this;
    }

    /**
     * Get nomenclature
     *
     * @return string $nomenclature
     */
    public function getNomenclature() {
        return $this->nomenclature;
    }



    /**
     * Get nomenclature
     *
     * @return string $nomenclature
     */
    public function getHtmlNomenclature() {
    	return preg_replace('/\*\*(.+)\*\*/', '<strong>$1</strong>', $this->nomenclature);
    }

    /**
     * Add prestation
     *
     * @param AppBundle\Document\Prestation $prestation
     */
    public function addPrestation(\AppBundle\Document\Prestation $prestation) {
        foreach ($this->getPrestations() as $prest) {
            if ($prest->getIdentifiant() == $prestation->getIdentifiant()) {
                return;
            }
        }
        $this->prestations[] = $prestation;
    }

    /**
     * Remove prestation
     *
     * @param AppBundle\Document\Prestation $prestation
     */
    public function removePrestation(\AppBundle\Document\Prestation $prestation) {
        $this->prestations->removeElement($prestation);
    }

    /**
     * Get prestations
     *
     * @return \Doctrine\Common\Collections\Collection $prestations
     */
    public function getPrestations() {
        return $this->prestations;
    }

    public function hasPrestation($part_of_name){
      foreach ($this->getPrestations() as $prestation) {
        if(preg_match("/$part_of_name/",$prestation->getNomToString())){
          return true;
        }
      }
      return false;
    }

    public function getUniquePrestations() {
        $prestations = array();

        foreach ($this->getPrestations() as $prestation) {
            $prestations[$prestation->getIdentifiant()] = $prestation->getNom();
        }

        return $prestations;
    }

    public function setUniquePrestations($prestationsIdentifiant) {
        $this->prestations = new ArrayCollection();

        foreach ($prestationsIdentifiant as $identifiant) {
            $prestation = new Prestation();
            $prestation->setIdentifiant($identifiant);
            $prestation->setNbPassages(1);
            $this->addPrestation($prestation);
        }

        return $this;
    }

    /**
     * Add produit
     *
     * @param AppBundle\Document\Produit $prestation
     */
    public function addProduit(\AppBundle\Document\Produit $produit) {
        foreach ($this->getProduits() as $prod) {
            if ($prod == $produit) {
                return;
            }
        }
        $this->produits[] = $produit;
    }

    /**
     * Remove produit
     *
     * @param AppBundle\Document\Produit $produit
     */
    public function removeProduit(\AppBundle\Document\Produit $produit) {
        $this->produits->removeElement($produit);
    }

    /**
     * Get produits
     *
     * @return \Doctrine\Common\Collections\Produit $produits
     */
    public function getProduits() {
        return $this->produits;
    }

    public function getProduit($identifiant){
      foreach ($this->produits as $produit) {
        if($produit->getIdentifiant() == $identifiant){
          return $produit;
        }
      }
      return null;
    }

    /**
     * Set dateCreation
     *
     * @param date $dateCreation
     * @return self
     */
    public function setDateCreation($dateCreation) {
        $this->dateCreation = $dateCreation;
        return $this;
    }

    /**
     * Get dateCreation
     *
     * @return date $dateCreation
     */
    public function getDateCreation() {
        return $this->dateCreation;
    }

    /**
     * Set dateCreationAuto
     *
     * @param date $dateCreationAuto
     * @return self
     */
    public function setDateCreationAuto($dateCreationAuto) {
        $this->dateCreationAuto = $dateCreationAuto;
        return $this;
    }

    /**
     * Get dateCreationAuto
     *
     * @return date $dateCreationAuto
     */
    public function getDateCreationAuto() {
        return $this->dateCreationAuto;
    }

    /**
     * Set dateDebut
     *
     * @param date $dateDebut
     * @return self
     */
    public function setDateDebut($dateDebut) {
        $this->dateDebut = $dateDebut;
        return $this;
    }

    /**
     * Get dateDebut
     *
     * @return date $dateDebut
     */
    public function getDateDebut() {
        return $this->dateDebut;
    }

    /**
     * Set dateFin
     *
     * @param date $dateFin
     * @return self
     */
    public function setDateFin($dateFin) {
        $this->dateFin = $dateFin;
        return $this;
    }

    /**
     * Get dateFin
     *
     * @return date $dateFin
     */
    public function getDateFin() {
        return $this->dateFin;
    }

    /**
     * Set dateAcceptation
     *
     * @param date $dateAcceptation
     * @return self
     */
    public function setDateAcceptation($dateAcceptation) {
        $this->dateAcceptation = $dateAcceptation;
        return $this;
    }

    /**
     * Get dateAcceptation
     *
     * @return date $dateAcceptation
     */
    public function getDateAcceptation() {
        return $this->dateAcceptation;
    }

    /**
     * Set duree
     *
     * @param int $duree
     * @return self
     */
    public function setDuree($duree) {
        $this->duree = $duree;
        return $this;
    }

    /**
     * Get duree
     *
     * @return int $duree
     */
    public function getDuree() {
        return $this->duree;
    }

    /**
     * Set dureeGarantie
     *
     * @param int $dureeGarantie
     * @return self
     */
    public function setDureeGarantie($dureeGarantie) {
        $this->dureeGarantie = $dureeGarantie;
        return $this;
    }

    /**
     * Get dureeGarantie
     *
     * @return int $dureeGarantie
     */
    public function getDureeGarantie() {
        return $this->dureeGarantie;
    }

    /**
     * Set nbPassages
     *
     * @param int $nbPassages
     * @return self
     */
    public function setNbPassages($nbPassages) {
        $this->nbPassages = $nbPassages;
        return $this;
    }

    /**
     * Get nbPassages
     *
     * @return int $nbPassages
     */
    public function getNbPassages() {
        if (!$this->nbPassages) {
            $nbPassagesEtb = array();
            foreach ($this->getContratPassages() as $contratPassage) {
                $nbPassagesEtb[$contratPassage->getEtablissement()->getId()] = 0;
                foreach ($contratPassage->getPassages() as $p) {
                    if ($p->isSousContrat()) {
                        $nbPassagesEtb[$contratPassage->getEtablissement()->getId()] = $nbPassagesEtb[$contratPassage->getEtablissement()->getId()] + 1;
                    }
                }
            }
            if ($nbPassagesEtb && count($nbPassagesEtb)) {
                $this->setNbPassages(max($nbPassagesEtb));
            } else {
                $this->setNbPassages(0);
            }
        }

        return $this->nbPassages;
    }

    /**
     * Set dureePassage
     *
     * @param int $dureePassage
     * @return self
     */
    public function setDureePassage($dureePassage) {
        $this->dureePassage = $dureePassage;
        return $this;
    }

    /**
     * Get dureePassage
     *
     * @return int $dureePassage
     */
    public function getDureePassage() {
        return $this->dureePassage;
    }

    public function getDureePassageFormat() {
        $minute = $this->getDureePassage();
        $heure = intval(abs($minute / 60));
        $minute = $minute - ($heure * 60);
        return sprintf("%02dh%02d", $heure, $minute);
    }

    /**
     * Set nbFactures
     *
     * @param int $nbFactures
     * @return self
     */
    public function setNbFactures($nbFactures) {
        $this->nbFactures = $nbFactures;
        return $this;
    }

    /**
     * Get nbFactures
     *
     * @return int $nbFactures
     */
    public function getNbFactures() {
        return $this->nbFactures;
    }

    /**
     * Set prixHt
     *
     * @param float $prixHt
     * @return self
     */
    public function setPrixHt($prixHt) {
        $this->prixHt = $prixHt;
        return $this;
    }

    /**
     * Get prixHt
     *
     * @return float $prixHt
     */
    public function getPrixHt() {
        return $this->prixHt;
    }

    /**
     * Set statut
     *
     * @param string $statut
     * @return self
     */
    public function setStatut($statut) {
        $this->statut = $statut;
        return $this;
    }

    /**
     * Get statut
     *
     * @return string $statut
     */
    public function getStatut() {
        return $this->statut;
    }

    /**
     * Set auditPassage
     *
     * @param int $auditPassage
     * @return self
     */
    public function setAuditPassage($auditPassage) {
        $this->auditPassage = $auditPassage;
        return $this;
    }

    /**
     * Get auditPassage
     *
     * @return int $auditPassage
     */
    public function getAuditPassage() {
        return $this->auditPassage;
    }

    public function updateObject() {
            $max = 0;
            foreach ($this->getPrestations() as $prestation) {
                if ($prestation->getNbPassages() > $max) {
                    $max = $prestation->getNbPassages();
                }
            }
            $this->setNbPassages($max);
    }

    public function updatePassages() {

    }

    public function updatePrestations($dm) {
        $cm = new ConfigurationManager($dm);
        $configuration = $cm->getRepository()->findOneById(Configuration::PREFIX);
        $prestationArray = $configuration->getPrestationsArray();
        foreach ($this->getPrestations() as $prestation) {
            $prestationNom = $prestationArray[$prestation->getIdentifiant()];
            $prestation->setNom($prestationNom);
        }
    }

    public function updateProduits($dm) {
        $cm = new ConfigurationManager($dm);
        $configuration = $cm->getRepository()->findOneById(Configuration::PREFIX);
        $produitsArray = $configuration->getProduitsArray();
        foreach ($this->getProduits() as $produit) {
            $produitConf = $produitsArray[$produit->getIdentifiant()];
            $produit->setNom($produitConf->getNom());
            $produit->setPrixHt($produitConf->getPrixHt());
            $produit->setPrixPrestation($produitConf->getPrixPrestation());
        }
    }

    public function getHumanDureePassage() {
        $duree = $this->getDureePassage();
        $heure = floor($duree / 60);
        return sprintf('%02d', $heure) . 'h' . sprintf('%02d', ((($duree / 60) - $heure) * 60));
    }

    public function getPrixMouvements() {
        $prix = 0;
        foreach ($this->getMouvements() as $mouvement) {
            $prix = $prix + $mouvement->getPrixUnitaire();
        }

        return $prix;
    }

    public function getPrixFactures() {
        $prix = 0;
        foreach ($this->getMouvements() as $mouvement) {
          if($mouvement->isFacture()){
            $prix = $prix + $mouvement->getPrixUnitaire();
            }
        }
        return $prix;
    }

    public function getPrixRestant() {
        $prixMouvement = $this->getPrixMouvements();

        return $this->getPrixHt() - $this->getPrixMouvements();
    }

    public function getNbPassagesPrevu() {
        return $this->getNbPassages();
    }

    public function getNbFacturesRestantes() {
    $nbFacturesGenerees = 0;
      foreach ($this->getMouvements() as $mouvement) {
        if($mouvement->getPrixUnitaire() > 0){
          $nbFacturesGenerees++;
        }else{
          $nbFacturesGenerees--;
        }
      }
      return $this->getNbFactures() - $nbFacturesGenerees;
    }

    public function generateMouvement($origineDocumentGeneration = null) {
        if ($this->getPrixRestant() <= 0 || $this->getNbFacturesRestantes() <= 0) {
            return null;
        }

        $mouvement = $this->buildMouvement($origineDocumentGeneration);

        $this->addMouvement($mouvement);

        return $mouvement;
    }

    public function buildMouvement($origineDocumentGeneration = null) {
        if ($this->getPrixRestant() <= 0 || $this->getNbFacturesRestantes() <= 0) {
            return null;
        }

        $mouvement = new Mouvement();
        $mouvement->setIdentifiant(uniqid());
        $mouvement->setPrixUnitaire(round($this->getPrixRestant() / $this->getNbFacturesRestantes(), 2));
        $mouvement->setQuantite(1);
        $mouvement->setTauxTaxe($this->getTva());
        $mouvement->setFacturable(true);
        $mouvement->setFacture(false);
        $mouvement->setSociete($this->getSociete());
        $mouvement->setLibelle(sprintf("Facture %s/%s - Proposition n° %s du %s au %s", count($this->getMouvements()) + 1, $this->getNbFactures(), $this->getNumeroArchive(), $this->getDateDebut()->format('m/Y'), $this->getDateFin()->format('m/Y')));

        $mouvement->setDocument($this);
        if ($origineDocumentGeneration) {
            $mouvement->setOrigineDocumentGeneration($origineDocumentGeneration);
        }

        return $mouvement;
    }

    public function restaureMouvements($documentGenere = null) {
      foreach ($this->getMouvements() as $mouvement) {
        if ($documentGenere){
          $mvtToRestaure = null;
          foreach ($documentGenere->getLignes() as $ligne) {
            if($ligne->getOrigineMouvement() == $mouvement->getIdentifiant()){
              $mvtToRestaure = $mouvement;
              break;
            }
          }
          if($mvtToRestaure){
            $mvtToRestaure->setFacturable(true);
            $mvtToRestaure->setFacture(false);

          }
        }else{
          $mouvement->setFacturable(true);
          $mouvement->setFacture(false);
        }
      }
    }

    public function resetFacturableMouvement($identifiant)
    {
    	foreach ($this->getMouvements() as $mouvement) {
    		if ($identifiant == $mouvement->getIdentifiant()) {
    			$this->removeMouvement($mouvement);
    		}
    	}
    }


    public function hasMouvements() {
        return boolval(count($this->getMouvements()));
    }

    public function getPrevisionnel($dateDebut = null) {
        if (!$dateDebut) {
            $dateDebut = new \DateTime();
        }

        $dureeContratMois = $this->getDuree();

        $maxNbPrestations = 0;
        $typePrestationPrincipal = "";
        foreach ($this->getPrestations() as $prestation) {
            if ($prestation->getNbPassages() > $maxNbPrestations) {
                $maxNbPrestations = $prestation->getNbPassages();
                $typePrestationPrincipal = $prestation;
            }
        }
        $passagesDatesArray = array();
        if (!count($this->getPrestations())) {
            return $passagesDatesArray;
        }
        if (!$maxNbPrestations) {
            return $passagesDatesArray;
        }
        $monthInterval = (floatval($dureeContratMois) / floatval($maxNbPrestations));
        $nb_month = intval($monthInterval);
        $dateLastPassage = clone $dateDebut;
        $passagesDatesArray[$dateLastPassage->format('Y-m-d')] = new \stdClass();
        $passagesDatesArray[$dateLastPassage->format('Y-m-d')]->prestations = array();

        foreach ($this->getPrestations() as $prestation) {
            if ($prestation->getNbPassages() > 0) {
                $passagesDatesArray[$dateLastPassage->format('Y-m-d')]->prestations[] = $prestation;
                $passagesDatesArray[$dateLastPassage->format('Y-m-d')]->mouvement_declenchable = 1;
            }
        }

        for ($index = 1; $index < $maxNbPrestations; $index++) {
            $monthDate = clone $dateLastPassage;
            $nextMonth = $monthDate->modify("+" . $nb_month . " month");
            $nb_days = intval(($monthInterval - $nb_month) * cal_days_in_month(CAL_GREGORIAN, $nextMonth->format('m'), $nextMonth->format('Y')));

            $dateLastPassage = $dateLastPassage->modify("+" . $nb_month . " month")->modify("+" . $nb_days . " day");
            $passagesDatesArray[$dateLastPassage->format('Y-m-d')] = new \stdClass();
            $passagesDatesArray[$dateLastPassage->format('Y-m-d')]->prestations = array();
            $passagesDatesArray[$dateLastPassage->format('Y-m-d')]->prestations[] = $typePrestationPrincipal;
        }

        foreach ($this->getPrestations() as $prestation) {
            if (($prestation->getIdentifiant() != $typePrestationPrincipal->getIdentifiant()) && $prestation->getNbPassages() > 1) {
                $nbPassagesPrestationRestant = $prestation->getNbPassages();
                $nbPassagesRestant = count($passagesDatesArray);
                $occurPassage = (floatval($nbPassagesRestant) / floatval($nbPassagesPrestationRestant));
                $compteurPassage = $occurPassage;
                $cpt = 0;
                foreach ($passagesDatesArray as $date => $passage) {
                    if ($cpt < 1) {
                        $cpt++;
                        continue;
                    }
                    if ($cpt >= $compteurPassage) {
                        $passagesDatesArray[$date]->prestations[] = $prestation;
                        $compteurPassage+=$occurPassage;
                    }
                    $cpt++;
                }
            }
        }
        $facturationInterval = 0;
        if(floatval($this->getNbFactures())){
          $facturationInterval = (floatval($maxNbPrestations) / floatval($this->getNbFactures()));
        }
        $compteurFacturation = $facturationInterval;
        $cpt = 0;

        foreach ($passagesDatesArray as $date => $passage) {
            if ($cpt < 1) {
                $passagesDatesArray[$date]->mouvement_declenchable = boolval($this->getNbFactures());
                $compteurFacturation--;
                $cpt++;
                continue;
            }
            if ($cpt >= $compteurFacturation) {
                $passagesDatesArray[$date]->mouvement_declenchable = boolval($this->getNbFactures());
                $compteurFacturation+=$facturationInterval;
            } else {
                $passagesDatesArray[$date]->mouvement_declenchable = 0;
            }
            $cpt++;
        }
        $cpt = 0;
        foreach ($passagesDatesArray as $date => $passage) {
        	$passagesDatesArray[$date]->audit = 0;
        	if ($cpt == ($this->getAuditPassage()-1)) {
        		$passagesDatesArray[$date]->audit = ($this->getCommercial()->getIdentite())? $this->getCommercial()->getIdentite() : 'Commercial';
        	}
        	$cpt++;
        }
        krsort($passagesDatesArray);
        return $passagesDatesArray;
    }

    public function getTypeContratLibelle() {
        if (!$this->getTypeContrat()) {
            return "";
        }
        return ContratManager::$types_contrat_libelles[$this->getTypeContrat()];
    }

    public function getTypeContratOriginalLibelle() {
        if (!$this->getTypeContratOriginal()) {
            return "";
        }
        return ContratManager::$types_contrat_libelles[$this->getTypeContratOriginal()];
    }

    public function getStatutLibelle() {
        $today = new \DateTime();
        if($this->isEnCours() && $this->getDateDebut() && ($today->format("Ymd") < $this->getDateDebut()->format("Ymd"))){
          return "A venir";
        }elseif($this->isFini() && $this->getDateFin() && ($today->format("Ymd") < $this->getDateFin()->format("Ymd"))){
          return "En cours (réalisé)";
        }
        return ContratManager::$statuts_libelles[$this->getStatut()];
    }

    public function getStatutLibelleLong() {
        $today = new \DateTime();
        if($this->isEnCours() && $this->getDateDebut() && ($today->format("Ymd") < $this->getDateDebut()->format("Ymd"))) {

            return "à venir";
        } elseif($this->isFini() && $this->getDateFin() && ($today->format("Ymd") < $this->getDateFin()->format("Ymd"))) {

            return "en cours réalisé";
        }

        return ContratManager::$statuts_libelles_long[$this->getStatut()];
    }

    public function getStatutCouleur() {
        if ($this->isAnnule()) {
            return ContratManager::$statuts_couleurs[$this->getTypeContrat()];
        }
      $today = new \DateTime();
      if($this->isEnCours() && $this->getDateDebut() && ($today->format("Ymd") < $this->getDateDebut()->format("Ymd"))){
        return "default";
      }elseif($this->isFini() && $this->getDateFin() && ($today->format("Ymd") < $this->getDateFin()->format("Ymd"))){
        return "info";
      }

        return ContratManager::$statuts_couleurs[$this->getStatut()];
    }

    public function getPassagesEtablissementNode(Etablissement $etablissement) {
        $contratPassages = $this->getContratPassages();
        if (!isset($contratPassages[$etablissement->getId()])) {
            return null;
        }

        return $contratPassages[$etablissement->getId()];
    }

    public function hasEtablissementNode(Etablissement $etablissement) {
        $etablissements = array();
        foreach ($this->getEtablissements() as $etab) {
        	$etablissements[] = $etab->getId();
        }
        return in_array($etablissement->getId(), $etablissements);
    }

    public static function cmpContrat($a, $b) {
        $statutsPositions = ContratManager::$statuts_positions;
        $pa = ($a->getStatut()) ? $statutsPositions[$a->getStatut()] : 99;
        $pb = ($b->getStatut()) ? $statutsPositions[$b->getStatut()] : 99;
        if ($pa == $pb) {
            $paDate = ($a->getDateDebut()) ? $a->getDateDebut() : $a->getDateCreation();
            $pbDate = ($b->getDateDebut()) ? $b->getDateDebut() : $b->getDateCreation();
            if ($paDate->format('Ymd') == $pbDate->format('Ymd')) {
                return 0;
            } else {
                return ($paDate->format('Ymd') < $pbDate->format('Ymd')) ? +1 : -1;
            }
        }
        return ($pa > $pb) ? +1 : -1;
    }

    public function getPassages(Etablissement $etablissement) {
        if (!isset($this->contratPassages[$etablissement->getId()])) {
            return array();
        }

        return $this->contratPassages[$etablissement->getId()]->getPassagesSorted();
    }

    public function getUniquePassage() {
        if ($this->getNbPassages() != 1 || count($this->getEtablissements()) > 1) {

            throw new \Exception("Il y a plusieurs passage pour ce contrat");
        }
        try{
          foreach ($this->getContratPassages() as $contratPassage) {
              foreach ($contratPassage->getPassagesSorted() as $passage) {
                return $passage;
            }
          }
        }catch(Exception $e){
          throw new \Exception("Il y a un problème avec le contrat ".$this->getId());
        }
        return null;
    }

    /**
     * Add etablissement
     *
     * @param AppBundle\Document\Etablissement $etablissement
     */
    public function addEtablissement(\AppBundle\Document\Etablissement $etablissement) {
        foreach ($this->getEtablissements() as $etb) {
            if ($etb->getId() == $etablissement->getId()) {
                return;
            }
        }
        $this->etablissements[] = $etablissement;
    }

    /**
     * Remove etablissement
     *
     * @param AppBundle\Document\Etablissement $etablissement
     */
    public function removeEtablissement(\AppBundle\Document\Etablissement $etablissement) {
        $this->etablissements->removeElement($etablissement);
    }

    /**
     * Get etablissements
     *
     * @return \Doctrine\Common\Collections\Collection $etablissements
     */
    public function getEtablissements() {
        return $this->etablissements;
    }

    /**
     * Set technicien
     *
     * @param AppBundle\Document\Compte $technicien
     * @return self
     */
    public function setTechnicien(\AppBundle\Document\Compte $technicien) {
        $this->technicien = $technicien;
        return $this;
    }

    /**
     * Get technicien
     *
     * @return AppBundle\Document\Compte $technicien
     */
    public function getTechnicien() {
        return $this->technicien;
    }

    public function changeTechnicien($newTechnicien) {
        if (!$newTechnicien) {
            return false;
        }
        $this->setTechnicien($newTechnicien);
        foreach ($this->getContratPassages() as $contratPassage) {
            foreach ($contratPassage->getPassagesSorted() as $passage) {
                if ($passage->isAPlanifie()) {
                    $passage->removeAllTechniciens();
                    $passage->addTechnicien($newTechnicien);
                }
            }
        }
    }

    public function getTechnicienPlusUtilise(){
      $techniciensArray = array();
      foreach ($this->getContratPassages() as $contratPassage) {
          foreach ($contratPassage->getPassagesSorted() as $passage) {
              foreach ($passage->getTechniciens() as $technicien) {
                if(!array_key_exists($technicien->getId(),$techniciensArray)){
                  $techniciensArray[$technicien->getId()] = new \stdClass();
                  $techniciensArray[$technicien->getId()]->occurs = 0;
                  $techniciensArray[$technicien->getId()]->technicien = $technicien;
                }
                $techniciensArray[$technicien->getId()]->occurs = $techniciensArray[$technicien->getId()]->occurs + 1;
              }
          }
      }
      $techNum = 0;
      $tech = null;
      foreach ($techniciensArray as $techId => $techRefs) {
        if($techRefs->occurs > $techNum){
          $tech = $techRefs->technicien;
        }
      }
      return $tech;
    }

    /**
     * Add contratPassage
     *
     * @param AppBundle\Document\Passage $passage
     */
    public function addPassage(\AppBundle\Document\Etablissement $etablissement, Passage $passage) {
        $contratPassagesToSet = new ContratPassages();
        foreach ($this->getContratPassages() as $contratPassages) {
            if ($etablissement->getId() == $contratPassages->getEtablissement()->getId()) {
                $contratPassagesToSet = $contratPassages;
            }
        }
        $contratPassagesToSet->addPassage($passage);
        $contratPassagesToSet->setEtablissement($etablissement);

        $this->addContratPassage($etablissement, $contratPassagesToSet);
    }

    /**
     * Add contratPassage
     *
     * @param AppBundle\Document\ContratPassages $contratPassage
     */
    public function addContratPassage($etablissement, \AppBundle\Document\ContratPassages $contratPassage) {
        $this->contratPassages[$etablissement->getId()] = $contratPassage;
    }

    /**
     * Remove contratPassage
     *
     * @param AppBundle\Document\ContratPassages $contratPassage
     */
    public function removeContratPassage(\AppBundle\Document\ContratPassages $contratPassage) {
        $this->contratPassages->removeElement($contratPassage);
    }

    /**
     * Get contratPassages
     *
     * @return \Doctrine\Common\Collections\Collection $contratPassages
     */
    public function getContratPassages() {
        return $this->contratPassages;
    }

    public function reInitContratPassages() {
        $this->contratPassages = array();
    }

    /**
     * Set societe
     *
     * @param AppBundle\Document\Societe $societe
     * @return self
     */
    public function setSociete(\AppBundle\Document\Societe $societe) {
        $this->societe = $societe;
        return $this;
    }

    /**
     * Get societe
     *
     * @return AppBundle\Document\Societe $societe
     */
    public function getSociete() {
        return $this->societe;
    }

    /**
     * Set multiTechnicien
     *
     * @param int $multiTechnicien
     * @return self
     */
    public function setMultiTechnicien($multiTechnicien) {
        $this->multiTechnicien = $multiTechnicien;
        return $this;
    }

    /**
     * Get multiTechnicien
     *
     * @return int $multiTechnicien
     */
    public function getMultiTechnicien() {
        return $this->multiTechnicien;
    }

    /**
     * Set numeroArchive
     *
     * @param string $numeroArchive
     * @return self
     */
    public function setNumeroArchive($numeroArchive) {
        $this->numeroArchive = $numeroArchive;
        return $this;
    }

    /**
     * Get numeroArchive
     *
     * @return string $numeroArchive
     */
    public function getNumeroArchive() {
        return $this->numeroArchive;
    }

    /**
     * Set identifiantReprise
     *
     * @param string $identifiantReprise
     * @return self
     */
    public function setIdentifiantReprise($identifiantReprise) {
        $this->identifiantReprise = $identifiantReprise;
        return $this;
    }

    /**
     * Get identifiantReprise
     *
     * @return string $identifiantReprise
     */
    public function getIdentifiantReprise() {
        return $this->identifiantReprise;
    }

    /**
     * Get id
     *
     * @return string $id
     */
    public function getId() {
        return $this->id;
    }

    /**
     * Set dateResiliation
     *
     * @param date $dateResiliation
     * @return self
     */
    public function setDateResiliation($dateResiliation) {
        $this->dateResiliation = $dateResiliation;
        return $this;
    }

    /**
     * Get dateResiliation
     *
     * @return date $dateResiliation
     */
    public function getDateResiliation() {
        return $this->dateResiliation;
    }

    /**
     * Set tvaReduite
     *
     * @param boolean $tvaReduite
     * @return self
     */
    public function setTvaReduite($tvaReduite) {
        $this->tvaReduite = $tvaReduite;
        return $this;
    }

    /**
     * Get tvaReduite
     *
     * @return boolean $tvaReduite
     */
    public function getTvaReduite() {

        return $this->tvaReduite;
    }

    public function isModifiable() {

        if($this->hasMouvements()) {

            return false;
        }

        if ($this->isEnAttenteAcceptation() || $this->isBrouillon()) {
            return true;
        }
        if ($this->isEnCours()) {
            foreach ($this->getContratPassages() as $contratPassage) {
                foreach ($contratPassage->getPassages() as $p) {
                    if ($p->isPlanifie() || $p->isRealise() || $p->isAnnule()) {
                        return false;
                    }
                }
            }
            return true;
        }
        return false;
    }

    public function isAnnulable() {
        return (($this->isEnCours()  || $this->isFini()) && !$this->isAnnule());
    }

    public function isReactivable() {
        return ($this->isAnnule() && $this->getTypeContratOriginal());
    }

    /*
     * Fonction à retiré => un contrat ne doit pas être resilié sous forme de statut mais sous forme de type
     */

    public function isResilie() {

        return ($this->statut == ContratManager::STATUT_RESILIE);
    }

    public function isAnnule() {

        return ($this->typeContrat == ContratManager::TYPE_CONTRAT_ANNULE);
    }

    public function isBrouillon() {
        return ($this->statut == ContratManager::STATUT_BROUILLON);
    }

    public function isEnCours() {
        return ($this->statut == ContratManager::STATUT_EN_COURS);
    }


    public function isEnAttenteAcceptation() {

        return ($this->statut == ContratManager::STATUT_EN_ATTENTE_ACCEPTATION);
    }

    public function isFini() {

        return ($this->statut == ContratManager::STATUT_FINI);
    }

    public function removeId() {
        $this->id = null;
    }

    public function removeAllEtablissements(){
      $this->etablissements = new ArrayCollection();
    }

    public function copier() {
        $contrat = clone $this;
        $contrat->removeId();
        $contrat->setNumeroArchive(null);
        $contrat->setIdentifiant(null);
        $contrat->setReferenceClient(null);
        $contrat->setDateAcceptation(null);
        $contrat->setDateDebut(null);
        $contrat->setDateFin(null);
        $contrat->setDateCreation(new \DateTime());
        $contrat->setDateCreationAuto(new \DateTime());
        $contrat->setStatut(ContratManager::STATUT_EN_ATTENTE_ACCEPTATION);
        $contrat->contratPassages = array();
        $contrat->cleanMouvements();
        $contrat->setReconduit(false);
        $contrat->updateObject();

        return $contrat;
    }

    public function reconduire($augmentation = 0) {
        if($this->getReconduit()) {

            throw new \Exception("Ce contrat à déjà été reconduit");
        }
        $contrat = clone $this;
        $contrat->removeId();
        $contrat->setIdentifiant(null);
        if (!$contrat->isKeepNumeroArchivage()) {
            $contrat->setNumeroArchive(null);
        }
        foreach ($contrat->getEtablissements() as $etb) {
          $this->addEtablissement($etb);
        }
        if ($augmentation > 0) {
        	$contrat->setPrixHt(round($this->getPrixHt() * (1+($augmentation/100)),2));
        }

        $dateDebut = clone $contrat->getDateDebut();
        $dateAcceptation = clone $contrat->getDateAcceptation();
        $dateFin= clone $contrat->getDateFin();
        $nbMois = $contrat->getDuree();

        $dateDebut = $dateDebut->modify("+" . $nbMois . " month");
        $dateFin = $dateFin->modify("+" . $nbMois . " month");

        $contrat->setDateDebut($dateDebut);
        $contrat->setDateFin($dateFin);
        $contrat->setDateCreation(new \DateTime());
        $contrat->setDateCreationAuto(new \DateTime());

        if($contrat->isTypeReconductionTacite()){
          $contrat->setStatut(ContratManager::STATUT_EN_COURS);
          $contrat->setDateAcceptation($dateAcceptation);
        }else{
          $contrat->setStatut(ContratManager::STATUT_EN_ATTENTE_ACCEPTATION);
          $contrat->setDateAcceptation(null);
        }

        $contrat->removeAllEtablissements();

        foreach ($this->getEtablissements() as $etablissement) {
          $contrat->addEtablissement($etablissement);
        }
        $contrat->contratPassages = array();
        $contrat->cleanMouvements();
        $contrat->setReconduit(false);
        $contrat->setDateReconduction(new \DateTime());
        $contrat->setReferenceClient(null);
        if(!$contrat->getTechnicien()){
            $technicien = $this->getTechnicienPlusUtilise();
            if($technicien) {
                $contrat->setTechnicien($technicien);
{'fraction_non_alphanumeric': 0.1395505916124908, 'fraction_numerical': 0.0016885983427613407, 'mean_word_length': 2.1978246615497357, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 824, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2390077', 'n_tokens_mistral': 26653, 'n_tokens_neox': 25757, 'n_words': 4012}