<commit_before><?php

namespace AppBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use AppBundle\Model\DocumentSocieteInterface;
use AppBundle\Manager\FactureManager;
use AppBundle\Manager\ContratManager;
use Doctrine\ODM\MongoDB\Mapping\Annotations\HasLifecycleCallbacks;

/**
 * @MongoDB\Document(repositoryClass="AppBundle\Repository\FactureRepository") @HasLifecycleCallbacks
 */
class Facture implements DocumentSocieteInterface {

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

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

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

    /**
     * @MongoDB\EmbedOne(targetDocument="FactureSoussigne")
     */
    protected $emetteur;

    /**
     * @MongoDB\EmbedOne(targetDocument="FactureSoussigne")
     */
    protected $destinataire;

    /**
     * @MongoDB\Date
     */
    protected $dateEmission;

    /**
     * @MongoDB\Date
     */
    protected $dateFacturation;

    /**
     * @MongoDB\Date
     */
    protected $dateDevis;

    /**
     * @MongoDB\Date
     */
    protected $datePaiement;

    /**
     * @MongoDB\Date
     */
    protected $dateLimitePaiement;

    /**
     * @MongoDB\Float
     */
    protected $montantHT;

    /**
     * @MongoDB\Float
     */
    protected $montantTTC;

    /**
     * @MongoDB\Float
     */
    protected $montantTaxe;

    /**
     * @MongoDB\Float
     */
    protected $montantPaye;

    /**
     * @MongoDB\Float
     */
    protected $montantAPayer;

    /**
     * @MongoDB\EmbedMany(targetDocument="FactureLigne")
     */
    protected $lignes;

    /**
     * @MongoDB\String
     */
    protected $identifiantReprise;

    /**
     * @MongoDB\String
     */
    protected $description;

    /**
     * @MongoDB\String
     */
    protected $numeroFacture;

    /**
     * @MongoDB\String
     */
    protected $numeroDevis;

    /**
     * @MongoDB\String
     */
    protected $avoir;

     /**
     * @MongoDB\ReferenceOne(targetDocument="Facture", inversedBy="factures", simple=true)
     */
    protected $origineAvoir;

    /**
     * @MongoDB\ReferenceMany(targetDocument="Paiements", mappedBy="paiement.facture", simple=true, repositoryMethod="findPaiementsByFacture")
     */
    protected $paiements;

    /**
     * @MongoDB\String
     */
    protected $frequencePaiement;

    /**
     * @MongoDB\Boolean
     */
    protected $cloture;

    /**
     * @MongoDB\Int
     */
    protected $nbRelance;

    /**
     * @MongoDB\EmbedMany(targetDocument="Relance")
     */
    protected $relances;

    /**
     * @MongoDB\String
     */
    protected $relanceCommentaire;

    /**
     * @MongoDB\Boolean
     */
    protected $avoirPartielRemboursementCheque;

    public function __construct() {
        $this->lignes = new \Doctrine\Common\Collections\ArrayCollection();
        $this->emetteur = new FactureSoussigne();
        $this->destinataire = new FactureSoussigne();
        $this->paiements = new \Doctrine\Common\Collections\ArrayCollection();
        $this->cloture = false;
        $this->avoirPartielRemboursementCheque = false;
    }

    /** @MongoDB\PreUpdate */
    public function preUpdate() {
        if ($this->getMontantTTC() == $this->getMontantPaye()) {
            $this->cloture = true;
        } else {
            $this->cloture = false;
        }
    }

    public function updateCalcul() {
        $montant = 0;
        $montantTaxe = 0;
        foreach ($this->getLignes() as $ligne) {
            $ligne->update();
            $montant = $montant + $ligne->getMontantHT();
            $montantTaxe = $montantTaxe + $ligne->getMontantTaxe();
        }
        $this->setMontantHT(round($montant, 2));
        $this->setMontantTaxe(round($montantTaxe, 2));
        $this->setMontantTTC(round($montant + $montantTaxe, 2));
    }

    public function update() {
        $this->updateCalcul();
        $this->storeDestinataire();
        $this->setDateLimitePaiement($this->calculDateLimitePaiement());
        $this->updateMouvementsContrat();
        $this->updateMontantPaye();
    }

    public function updateMouvementsContrat() {
        foreach ($this->getLignes() as $ligne) {
            $ligne->updateMouvementContrat();
        }
    }

    public function storeDestinataire() {
        $societe = $this->getSociete();
        $destinataire = $this->getDestinataire();

        $nom = $societe->getRaisonSociale();

        if($this->getContrat() && $this->getContrat()->getFactureDestinataire()) {
            $nom = $this->getContrat()->getFactureDestinataire();
        }

        $destinataire->setRaisonSociale($societe->getRaisonSociale());
        $destinataire->setNom($nom);
        $destinataire->setAdresse($societe->getAdresse()->getAdresseFormatee());
        $destinataire->setCodePostal($societe->getAdresse()->getCodePostal());
        $destinataire->setCommune($societe->getAdresse()->getCommune());
        $destinataire->setCodeComptable($societe->getCodeComptable());
    }

    public function facturerMouvements() {
        foreach ($this->getLignes() as $ligne) {
            $ligne->facturerMouvement();
        }
    }

    public function isPaye() {

        return $this->isCloture();
    }

    public function getContrat() {
        foreach($this->getLignes() as $ligne) {
            if($ligne->isOrigineContrat()) {

                return $ligne->getOrigineDocument();
            }
        }
    }

    public function getOrigines() {
        $origines = array();
        foreach ($this->getLignes() as $ligne) {
            if (!$ligne->getOrigineDocument()) {
                continue;
            }
            $origines[$ligne->getOrigineDocument()->getId()] = $ligne->getOrigineDocument();
        }

        return $origines;
    }

    /**
     * Add ligne
     *
     * @param AppBundle\Document\FactureLigne $ligne
     */
    public function addLigne(\AppBundle\Document\FactureLigne $ligne) {
        $this->lignes[] = $ligne;
    }

    /**
     * Remove ligne
     *
     * @param AppBundle\Document\FactureLigne $ligne
     */
    public function removeLigne(\AppBundle\Document\FactureLigne $ligne) {
        $this->lignes->removeElement($ligne);
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        return $this;
    }

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



    /**
     * Set origineAvoir
     *
     * @param AppBundle\Document\Facture $facture
     * @return self
     */
    public function setOrigineAvoir(\AppBundle\Document\Facture $facture) {
    	$this->origineAvoir = $facture;

    	return $this;
    }

    /**
     * Get origineAvoir
     *
     * @return AppBundle\Document\Facture $facture
     */
    public function getOrigineAvoir() {
    	return $this->origineAvoir;
    }

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

    /**
     * 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 emetteur
     *
     * @param AppBundle\Document\FactureSoussigne $emetteur
     * @return self
     */
    public function setEmetteur(\AppBundle\Document\FactureSoussigne $emetteur) {
        $this->emetteur = $emetteur;
        return $this;
    }

    /**
     * Get emetteur
     *
     * @return AppBundle\Document\FactureSoussigne $emetteur
     */
    public function getEmetteur() {
        return $this->emetteur;
    }

    /**
     * Set destinataire
     *
     * @param AppBundle\Document\FactureSoussigne $destinataire
     * @return self
     */
    public function setDestinataire(\AppBundle\Document\FactureSoussigne $destinataire) {
        $this->destinataire = $destinataire;
        return $this;
    }

    /**
     * Get destinataire
     *
     * @return AppBundle\Document\FactureSoussigne $destinataire
     */
    public function getDestinataire() {
        return $this->destinataire;
    }

    /**
     * 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;
    }

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

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

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

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

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

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

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

    /**
     * Get dateLimitePaiement
     *
     * @return date $dateLimitePaiement
     */
    public function getDateLimitePaiement() {
        if (is_null($this->dateLimitePaiement)) {

            return clone $this->calculDateLimitePaiement();
        }

        return $this->dateLimitePaiement;
    }

    public function getTva() {
        $tva = 0;
        foreach ($this->getLignes() as $ligne) {
            if (!$tva) {
                $tva = $ligne->getTauxTaxe();
            }
            if ($tva != $ligne->getTauxTaxe()) {
                throw new \Exception("TVA différente dans les lignes de facture.");
            }
        }
        return $tva;
    }

    public function calculDateLimitePaiement() {
        $frequence = $this->getFrequencePaiement();
        $date = null;
        if($this->getDateFacturation()) {
            $date = clone $this->getDateFacturation();
        }
        $date = ($date) ? $date : clone $this->getDateEmission();
        $date = ($date) ? $date : new \DateTime();
        switch ($frequence) {
            case ContratManager::FREQUENCE_30J :
                $date->modify('+30 day');
                break;
            case ContratManager::FREQUENCE_30JMOIS :
                $date->modify('+30 day')->modify('last day of');
                break;
            case ContratManager::FREQUENCE_45JMOIS :
                $date->modify('+45 day')->modify('last day of');
                break;
            case ContratManager::FREQUENCE_60J :
                $date->modify('+60 day');
                break;
            default:
                $date->modify('+' . FactureManager::DEFAUT_FREQUENCE_JOURS . ' day');
        }

        return $date;
    }

    public function getFrequencePaiementLibelle() {

        return ContratManager::$frequences[$this->getFrequencePaiement()];
    }

    /**
     * Get frequencePaiement
     *
     * @return string $frequencePaiement
     */
    public function getFrequencePaiement() {
        if ($this->frequencePaiement) {

            return $this->frequencePaiement;
        }

        if (!$this->frequencePaiement) {
            foreach ($this->getLignes() as $ligne) {
                if ($ligne->isOrigineContrat() && $ligne->getOrigineDocument()->getFrequencePaiement()) {

                    return $ligne->getOrigineDocument()->getFrequencePaiement();
                }
            }
        }

        if (!$this->frequencePaiement) {

            return ContratManager::FREQUENCE_RECEPTION;
        }
    }

    /**
     * Set frequencePaiement
     *
     * @param string $frequencePaiement
     * @return self
     */
    public function setFrequencePaiement($frequencePaiement) {
        $this->frequencePaiement = $frequencePaiement;

        return $this;
    }

    /**
     * Add paiement
     *
     * @param AppBundle\Document\Paiement $paiement
     */
    public function addPaiement(\AppBundle\Document\Paiement $paiement) {
        $this->paiements[] = $paiement;
    }

    /**
     * Remove paiement
     *
     * @param AppBundle\Document\Paiement $paiement
     */
    public function removePaiement(\AppBundle\Document\Paiement $paiement) {
        $this->paiements->removeElement($paiement);
    }

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

    public function __toString() {
        return "N°" . $this->getNumeroFacture() . " " . $this->getDestinataire()->getNom() . " (" . $this->getMontantAPayer() . "€ / " . $this->getMontantTTC() . "€ TTC)";
    }

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

    /**
     * Get montantPaye
     *
     * @return float $montantPaye
     */
    public function getMontantPaye() {
        if (!$this->montantPaye) {
            return 0.0;
        }
        return $this->montantPaye;
    }

    public function ajoutMontantPaye($montant) {
        $this->setMontantPaye($this->getMontantPaye() + $montant);
        return $this;
    }

    public function updateMontantPaye($output = null) {
        $this->setMontantPaye(0.0);
        foreach ($this->getPaiements() as $paiements) {
            foreach ($paiements->getPaiement() as $paiement) {
                if ($paiement->getFacture()->getId() == $this->getId()) {
                    if ($output) {
                        $output->writeln(sprintf("<comment>Ajout d'un paiement de %s euros HT pour facture d'id %s </comment>", $paiement->getMontant(), $this->getId()));
                    }
                    $this->ajoutMontantPaye($paiement->getMontant());
                }
            }
        }
    }

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

    /**
     * Get cloture
     *
     * @return boolean $cloture
     */
    public function getCloture() {
        return $this->cloture;
    }

    public function isCloture() {
        return $this->cloture;
    }

    public function updateRestantAPayer() {
        $this->setMontantAPayer(round($this->getMontantTTC() - $this->getMontantPaye(), 2));
    }

    public function getRestantAPayer() {
        return round($this->getMontantTTC() - $this->getMontantPaye(), 2);
    }

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

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

    /* Set dateDevis
     *
     * @param date $dateDevis
     * @return self
     */

    public function setDateDevis($dateDevis) {
        $this->dateDevis = $dateDevis;
        return $this;
    }

    /* Get dateDevis
     *
     * @return date $dateDevis
     */

    public function getDateDevis() {
        return $this->dateDevis;
    }

    public function isDevis() {

        return $this->getDateDevis() && !$this->isFacture();
    }

    public function isFacture() {

        return ($this->getDateFacturation() || $this->getNumeroFacture());
    }

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


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

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

    public function isEnRetardPaiement() {
        if (!$this->isCloture() && ($this->getDateLimitePaiement()->format('Ymd') < (new \DateTime())->format('Ymd'))) {
            return true;
        }
        return false;
    }

    public function isAvoir() {
        return $this->getMontantTTC() < 0;
    }

    public function isRedressee() {
        return boolval($this->getAvoir());
    }

    public function cloturer() {
        $this->setMontantPaye($this->getMontantTTC());
    }
<commit_msg>Exclusion des devis en rouge<commit_after><?php

namespace AppBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use AppBundle\Model\DocumentSocieteInterface;
use AppBundle\Manager\FactureManager;
use AppBundle\Manager\ContratManager;
use Doctrine\ODM\MongoDB\Mapping\Annotations\HasLifecycleCallbacks;

/**
 * @MongoDB\Document(repositoryClass="AppBundle\Repository\FactureRepository") @HasLifecycleCallbacks
 */
class Facture implements DocumentSocieteInterface {

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

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

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

    /**
     * @MongoDB\EmbedOne(targetDocument="FactureSoussigne")
     */
    protected $emetteur;

    /**
     * @MongoDB\EmbedOne(targetDocument="FactureSoussigne")
     */
    protected $destinataire;

    /**
     * @MongoDB\Date
     */
    protected $dateEmission;

    /**
     * @MongoDB\Date
     */
    protected $dateFacturation;

    /**
     * @MongoDB\Date
     */
    protected $dateDevis;

    /**
     * @MongoDB\Date
     */
    protected $datePaiement;

    /**
     * @MongoDB\Date
     */
    protected $dateLimitePaiement;

    /**
     * @MongoDB\Float
     */
    protected $montantHT;

    /**
     * @MongoDB\Float
     */
    protected $montantTTC;

    /**
     * @MongoDB\Float
     */
    protected $montantTaxe;

    /**
     * @MongoDB\Float
     */
    protected $montantPaye;

    /**
     * @MongoDB\Float
     */
    protected $montantAPayer;

    /**
     * @MongoDB\EmbedMany(targetDocument="FactureLigne")
     */
    protected $lignes;

    /**
     * @MongoDB\String
     */
    protected $identifiantReprise;

    /**
     * @MongoDB\String
     */
    protected $description;

    /**
     * @MongoDB\String
     */
    protected $numeroFacture;

    /**
     * @MongoDB\String
     */
    protected $numeroDevis;

    /**
     * @MongoDB\String
     */
    protected $avoir;

     /**
     * @MongoDB\ReferenceOne(targetDocument="Facture", inversedBy="factures", simple=true)
     */
    protected $origineAvoir;

    /**
     * @MongoDB\ReferenceMany(targetDocument="Paiements", mappedBy="paiement.facture", simple=true, repositoryMethod="findPaiementsByFacture")
     */
    protected $paiements;

    /**
     * @MongoDB\String
     */
    protected $frequencePaiement;

    /**
     * @MongoDB\Boolean
     */
    protected $cloture;

    /**
     * @MongoDB\Int
     */
    protected $nbRelance;

    /**
     * @MongoDB\EmbedMany(targetDocument="Relance")
     */
    protected $relances;

    /**
     * @MongoDB\String
     */
    protected $relanceCommentaire;

    /**
     * @MongoDB\Boolean
     */
    protected $avoirPartielRemboursementCheque;

    public function __construct() {
        $this->lignes = new \Doctrine\Common\Collections\ArrayCollection();
        $this->emetteur = new FactureSoussigne();
        $this->destinataire = new FactureSoussigne();
        $this->paiements = new \Doctrine\Common\Collections\ArrayCollection();
        $this->cloture = false;
        $this->avoirPartielRemboursementCheque = false;
    }

    /** @MongoDB\PreUpdate */
    public function preUpdate() {
        if ($this->getMontantTTC() == $this->getMontantPaye()) {
            $this->cloture = true;
        } else {
            $this->cloture = false;
        }
    }

    public function updateCalcul() {
        $montant = 0;
        $montantTaxe = 0;
        foreach ($this->getLignes() as $ligne) {
            $ligne->update();
            $montant = $montant + $ligne->getMontantHT();
            $montantTaxe = $montantTaxe + $ligne->getMontantTaxe();
        }
        $this->setMontantHT(round($montant, 2));
        $this->setMontantTaxe(round($montantTaxe, 2));
        $this->setMontantTTC(round($montant + $montantTaxe, 2));
    }

    public function update() {
        $this->updateCalcul();
        $this->storeDestinataire();
        $this->setDateLimitePaiement($this->calculDateLimitePaiement());
        $this->updateMouvementsContrat();
        $this->updateMontantPaye();
    }

    public function updateMouvementsContrat() {
        foreach ($this->getLignes() as $ligne) {
            $ligne->updateMouvementContrat();
        }
    }

    public function storeDestinataire() {
        $societe = $this->getSociete();
        $destinataire = $this->getDestinataire();

        $nom = $societe->getRaisonSociale();

        if($this->getContrat() && $this->getContrat()->getFactureDestinataire()) {
            $nom = $this->getContrat()->getFactureDestinataire();
        }

        $destinataire->setRaisonSociale($societe->getRaisonSociale());
        $destinataire->setNom($nom);
        $destinataire->setAdresse($societe->getAdresse()->getAdresseFormatee());
        $destinataire->setCodePostal($societe->getAdresse()->getCodePostal());
        $destinataire->setCommune($societe->getAdresse()->getCommune());
        $destinataire->setCodeComptable($societe->getCodeComptable());
    }

    public function facturerMouvements() {
        foreach ($this->getLignes() as $ligne) {
            $ligne->facturerMouvement();
        }
    }

    public function isPaye() {

        return $this->isCloture();
    }

    public function getContrat() {
        foreach($this->getLignes() as $ligne) {
            if($ligne->isOrigineContrat()) {

                return $ligne->getOrigineDocument();
            }
        }
    }

    public function getOrigines() {
        $origines = array();
        foreach ($this->getLignes() as $ligne) {
            if (!$ligne->getOrigineDocument()) {
                continue;
            }
            $origines[$ligne->getOrigineDocument()->getId()] = $ligne->getOrigineDocument();
        }

        return $origines;
    }

    /**
     * Add ligne
     *
     * @param AppBundle\Document\FactureLigne $ligne
     */
    public function addLigne(\AppBundle\Document\FactureLigne $ligne) {
        $this->lignes[] = $ligne;
    }

    /**
     * Remove ligne
     *
     * @param AppBundle\Document\FactureLigne $ligne
     */
    public function removeLigne(\AppBundle\Document\FactureLigne $ligne) {
        $this->lignes->removeElement($ligne);
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        return $this;
    }

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



    /**
     * Set origineAvoir
     *
     * @param AppBundle\Document\Facture $facture
     * @return self
     */
    public function setOrigineAvoir(\AppBundle\Document\Facture $facture) {
    	$this->origineAvoir = $facture;

    	return $this;
    }

    /**
     * Get origineAvoir
     *
     * @return AppBundle\Document\Facture $facture
     */
    public function getOrigineAvoir() {
    	return $this->origineAvoir;
    }

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

    /**
     * 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 emetteur
     *
     * @param AppBundle\Document\FactureSoussigne $emetteur
     * @return self
     */
    public function setEmetteur(\AppBundle\Document\FactureSoussigne $emetteur) {
        $this->emetteur = $emetteur;
        return $this;
    }

    /**
     * Get emetteur
     *
     * @return AppBundle\Document\FactureSoussigne $emetteur
     */
    public function getEmetteur() {
        return $this->emetteur;
    }

    /**
     * Set destinataire
     *
     * @param AppBundle\Document\FactureSoussigne $destinataire
     * @return self
     */
    public function setDestinataire(\AppBundle\Document\FactureSoussigne $destinataire) {
        $this->destinataire = $destinataire;
        return $this;
    }

    /**
     * Get destinataire
     *
     * @return AppBundle\Document\FactureSoussigne $destinataire
     */
    public function getDestinataire() {
        return $this->destinataire;
    }

    /**
     * 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;
    }

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

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

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

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

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

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

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

    /**
     * Get dateLimitePaiement
     *
     * @return date $dateLimitePaiement
     */
    public function getDateLimitePaiement() {
        if (is_null($this->dateLimitePaiement)) {

            return clone $this->calculDateLimitePaiement();
        }

        return $this->dateLimitePaiement;
    }

    public function getTva() {
        $tva = 0;
        foreach ($this->getLignes() as $ligne) {
            if (!$tva) {
                $tva = $ligne->getTauxTaxe();
            }
            if ($tva != $ligne->getTauxTaxe()) {
                throw new \Exception("TVA différente dans les lignes de facture.");
            }
        }
        return $tva;
    }

    public function calculDateLimitePaiement() {
        $frequence = $this->getFrequencePaiement();
        $date = null;
        if($this->getDateFacturation()) {
            $date = clone $this->getDateFacturation();
        }
        $date = ($date) ? $date : clone $this->getDateEmission();
        $date = ($date) ? $date : new \DateTime();
        switch ($frequence) {
            case ContratManager::FREQUENCE_30J :
                $date->modify('+30 day');
                break;
            case ContratManager::FREQUENCE_30JMOIS :
                $date->modify('+30 day')->modify('last day of');
                break;
            case ContratManager::FREQUENCE_45JMOIS :
                $date->modify('+45 day')->modify('last day of');
                break;
            case ContratManager::FREQUENCE_60J :
                $date->modify('+60 day');
                break;
            default:
                $date->modify('+' . FactureManager::DEFAUT_FREQUENCE_JOURS . ' day');
        }

        return $date;
    }

    public function getFrequencePaiementLibelle() {

        return ContratManager::$frequences[$this->getFrequencePaiement()];
    }

    /**
     * Get frequencePaiement
     *
     * @return string $frequencePaiement
     */
    public function getFrequencePaiement() {
        if ($this->frequencePaiement) {

            return $this->frequencePaiement;
        }

        if (!$this->frequencePaiement) {
            foreach ($this->getLignes() as $ligne) {
                if ($ligne->isOrigineContrat() && $ligne->getOrigineDocument()->getFrequencePaiement()) {

                    return $ligne->getOrigineDocument()->getFrequencePaiement();
                }
            }
        }

        if (!$this->frequencePaiement) {

            return ContratManager::FREQUENCE_RECEPTION;
        }
    }

    /**
     * Set frequencePaiement
     *
     * @param string $frequencePaiement
     * @return self
     */
    public function setFrequencePaiement($frequencePaiement) {
        $this->frequencePaiement = $frequencePaiement;

        return $this;
    }

    /**
     * Add paiement
     *
     * @param AppBundle\Document\Paiement $paiement
     */
    public function addPaiement(\AppBundle\Document\Paiement $paiement) {
        $this->paiements[] = $paiement;
    }

    /**
     * Remove paiement
     *
     * @param AppBundle\Document\Paiement $paiement
     */
    public function removePaiement(\AppBundle\Document\Paiement $paiement) {
        $this->paiements->removeElement($paiement);
    }

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

    public function __toString() {
        return "N°" . $this->getNumeroFacture() . " " . $this->getDestinataire()->getNom() . " (" . $this->getMontantAPayer() . "€ / " . $this->getMontantTTC() . "€ TTC)";
    }

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

    /**
     * Get montantPaye
     *
     * @return float $montantPaye
     */
    public function getMontantPaye() {
        if (!$this->montantPaye) {
            return 0.0;
        }
        return $this->montantPaye;
    }

    public function ajoutMontantPaye($montant) {
        $this->setMontantPaye($this->getMontantPaye() + $montant);
        return $this;
    }

    public function updateMontantPaye($output = null) {
        $this->setMontantPaye(0.0);
        foreach ($this->getPaiements() as $paiements) {
            foreach ($paiements->getPaiement() as $paiement) {
                if ($paiement->getFacture()->getId() == $this->getId()) {
                    if ($output) {
                        $output->writeln(sprintf("<comment>Ajout d'un paiement de %s euros HT pour facture d'id %s </comment>", $paiement->getMontant(), $this->getId()));
                    }
                    $this->ajoutMontantPaye($paiement->getMontant());
                }
            }
        }
    }

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

    /**
     * Get cloture
     *
     * @return boolean $cloture
     */
    public function getCloture() {
        return $this->cloture;
    }

    public function isCloture() {
        return $this->cloture;
    }

    public function updateRestantAPayer() {
        $this->setMontantAPayer(round($this->getMontantTTC() - $this->getMontantPaye(), 2));
    }

    public function getRestantAPayer() {
        return round($this->getMontantTTC() - $this->getMontantPaye(), 2);
    }

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

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

    /* Set dateDevis
     *
     * @param date $dateDevis
     * @return self
     */

    public function setDateDevis($dateDevis) {
        $this->dateDevis = $dateDevis;
        return $this;
    }

    /* Get dateDevis
     *
     * @return date $dateDevis
     */

    public function getDateDevis() {
        return $this->dateDevis;
    }

    public function isDevis() {

        return $this->getDateDevis() && !$this->isFacture();
    }

    public function isFacture() {

        return ($this->getDateFacturation() || $this->getNumeroFacture());
    }

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


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

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

    public function isEnRetardPaiement() {
        if($this->isDevis()) {

            return false;
        }

        if (!$this->isCloture() && ($this->getDateLimitePaiement()->format('Ymd') < (new \DateTime())->format('Ymd'))) {
            return true;
        }
        return false;
    }

    public function isAvoir() {
        return $this->getMontantTTC() < 0;
    }

    public function isRedressee() {
        return boolval($this->getAvoir());
    }

    public function cloturer() {
        $this->setMontantPaye($this->getMontantTTC());
    }

{'fraction_non_alphanumeric': 0.13088702687084658, 'fraction_numerical': 0.0013965135317345662, 'mean_word_length': 2.011601769269814, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 364, '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': '2638916', 'n_tokens_mistral': 13615, 'n_tokens_neox': 13224, 'n_words': 2173}