<commit_before><?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of class DSCiva
 * @author mathurin
 */
class DSCiva extends DS implements IUtilisateursDocument {

    protected $utilisateurs_document = null;

    public function  __construct() {
        parent::__construct();
        $this->archivage_document = new ArchivageDocument($this);
        $this->utilisateurs_document = new UtilisateursDocument($this);
    }

    public function constructId() {
        if ($this->statut == null) {
            $this->statut = DSClient::STATUT_A_SAISIR;
        }

        $this->set('_id', DSClient::getInstance()->buildId($this->identifiant, $this->periode, $this->getEtablissement()->getLieuStockagePrincipale()->getNumeroIncremental()));
    }


    public function storeStockage() {
        $tiers = $this->getEtablissement();
        if(!$tiers){
            throw new sfException(sprintf("L'etablissement de cvi %s n'existe pas", $this->identifiant));
        }
        $num_lieu = $this->identifiant.$this->getLieuStockage();

        if(!$tiers->getLieuxStockage(true, $this->identifiant)) {
            throw new sfException(sprintf("Aucun lieu de stockage n'existe dans l'etablissement de cvi %s ", $this->identifiant));
        }

        if(!isset($tiers->lieux_stockage[$num_lieu])) {
            throw new sfException(sprintf("Le lieu de stockage %s n'existe pas dans l'etablissement de cvi %s ", $num_lieu, $this->identifiant));
        }

        $this->set('stockage', $tiers->lieux_stockage[$num_lieu]);
    }

    public function storeDeclarant() {
        parent::storeDeclarant();

        $tiers = $this->getEtablissement();
        $this->declaration_commune = $tiers->declaration_commune;
        $this->declaration_insee = $tiers->declaration_insee;

        $this->declarant->email = $tiers->getEmailTeledeclaration();

        if($tiers->exist('civaba') && $tiers->civaba){
            $this->add('civaba', $tiers->civaba);
        }

        $this->declarant->exploitant->sexe = $tiers->exploitant->civilite;
        $this->declarant->exploitant->nom = $tiers->exploitant->nom;
        $this->declarant->exploitant->adresse = $tiers->exploitant->adresse;
        $this->declarant->exploitant->code_postal = $tiers->exploitant->code_postal;
        $this->declarant->exploitant->commune = $tiers->exploitant->commune;
        //$this->declarant->exploitant->date_naissance = $tiers->exploitant->date_naissance;
        $this->declarant->exploitant->telephone = $tiers->exploitant->telephone;
    }

    public function storeInfos() {
        $this->storeStockage();
        $this->storeDeclarant();
    }

    public function getLastDocument() {
        return $this->getLastDR();
    }

    private function getLastDR() {
        return DRClient::getInstance()->retrieveByCampagneAndCvi($this->identifiant, CurrentClient::getCurrent()->campagne);
    }

    public function isDecembre() {

        return substr($this->periode, 4, 2) == "12";
    }

    public function updateProduitsFromLastDs() {
        $ds = $this->getLastDS();
        if ($ds) {
            $this->updateProduitsFromDS($ds);
        }
    }

    public function updateProduitsFromLastDr(){
        if ($this->isTypeDsPropriete()) {
            $dr = $this->getLastDR();
            if($dr){
                $this->drm_origine = $dr->_id;
                $this->addDetailsFromDRPropriete($dr);
            }
        }
        if($this->isTypeDsNegoce()){
            $this->addDetailsFromDRNegoce();
        }
    }

    protected function updateProduitsFromDS($ds) {
        $this->drm_origine = $ds->_id;
        $this->addDetailsFromDS($ds);
    }

    public function isTypeDsPropriete(){
        return ($this->exist('type_ds') && ($this->get('type_ds') == DSCivaClient::TYPE_DS_PROPRIETE));
    }

    public function isTypeDsNegoce(){
        return ($this->exist('type_ds') && ($this->get('type_ds') == DSCivaClient::TYPE_DS_NEGOCE));
    }

    public function getTypeDs() {
        if($this->exist('type_ds')) {

            return $this->_get('type_ds');
        }

        return DSCivaClient::TYPE_DS_PROPRIETE;
    }

    public function getLastDS() {
        return DSCivaClient::getInstance()->getLastDs($this);
    }

    public function addNoeud($hash) {
        $hash = preg_replace('/^\/recolte/','declaration', $hash);
        $hash = preg_replace("/(mentionVT|mentionSGN)/", "mention", $hash);
        $noeud = $this->getOrAdd($hash);
        $config = $noeud->getConfig();
        $noeud->libelle = $config->getLibelle();
        if($noeud instanceof DSCepage && count($config->getParentNode()->getChildrenNode()) <= 1 && !$config->hasLieuEditable()) {
            $this->addDetail($hash);
        }

        if($config->isAutoDs()) {
            foreach($config->getProduits() as $item) {
                if(!$item->isForDS()) {
                    continue;
                }
                $this->addDetail(HashMapper::inverse($item->getHash()));
            }
        }

        if($noeud instanceof DSAppellation) {
            $this->addNoeud(HashMapper::inverse($config->getChildrenNode()->getFirst()->getHash()));
        } elseif(count($config->getChildrenNode()) == 1) {
            $this->addNoeud(HashMapper::inverse($config->getChildrenNode()->getFirst()->getHash()));
        }

        return $noeud;
    }

    public function addAppellation($hash) {

        return $this->addNoeud($hash);
    }

    public function addLieu($hash) {

        return $this->addNoeud($hash);
    }

    public function addDetailsFromDRPropriete($dr) {
        foreach ($dr->getProduitsDetails() as $detail) {
            if(!$detail->cave_particuliere) {

                continue;
            }

            $this->addDetail($detail->getCepage()->getHash(), $detail->lieu);
        }

        if($dr->recolte->getTotalVci() > 0) {
            $this->addAppellation("declaration/certification/genreVCI");
        }
    }

    public function addDetailsFromDRNegoce()
    {
        $campagne = CurrentClient::getCurrent()->campagne;
        $cvi_acheteur = $this->getEtablissement()->getCvi();
        if(!$cvi_acheteur) {
            return;
        }
        $drs = DRClient::getInstance()->findAllByCampagneAndCviAcheteur($campagne, $cvi_acheteur, acCouchdbClient::HYDRATE_ON_DEMAND);
        foreach ($drs as $id => $doc) {
            $dr = acCouchdbManager::getClient('DR')->find($id);
            foreach ($dr->getProduitsDetails() as $detail) {
                if(!$detail->getVolumeByAcheteur($cvi_acheteur)) {

                    continue;
                }
                $this->addDetail($detail->getCepage()->getHash(), $detail->lieu);
            }
        }
    }

    public function addDetailsFromDS($ds)
    {
        foreach ($ds->declaration->getProduitsDetails() as $detail) {
            $this->addDetail($detail->getCepage()->getHash(), $detail->lieu, true);
        }<commit_msg>On utilise plutôt getTotalVciCaveParticuliere<commit_after><?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of class DSCiva
 * @author mathurin
 */
class DSCiva extends DS implements IUtilisateursDocument {

    protected $utilisateurs_document = null;

    public function  __construct() {
        parent::__construct();
        $this->archivage_document = new ArchivageDocument($this);
        $this->utilisateurs_document = new UtilisateursDocument($this);
    }

    public function constructId() {
        if ($this->statut == null) {
            $this->statut = DSClient::STATUT_A_SAISIR;
        }

        $this->set('_id', DSClient::getInstance()->buildId($this->identifiant, $this->periode, $this->getEtablissement()->getLieuStockagePrincipale()->getNumeroIncremental()));
    }


    public function storeStockage() {
        $tiers = $this->getEtablissement();
        if(!$tiers){
            throw new sfException(sprintf("L'etablissement de cvi %s n'existe pas", $this->identifiant));
        }
        $num_lieu = $this->identifiant.$this->getLieuStockage();

        if(!$tiers->getLieuxStockage(true, $this->identifiant)) {
            throw new sfException(sprintf("Aucun lieu de stockage n'existe dans l'etablissement de cvi %s ", $this->identifiant));
        }

        if(!isset($tiers->lieux_stockage[$num_lieu])) {
            throw new sfException(sprintf("Le lieu de stockage %s n'existe pas dans l'etablissement de cvi %s ", $num_lieu, $this->identifiant));
        }

        $this->set('stockage', $tiers->lieux_stockage[$num_lieu]);
    }

    public function storeDeclarant() {
        parent::storeDeclarant();

        $tiers = $this->getEtablissement();
        $this->declaration_commune = $tiers->declaration_commune;
        $this->declaration_insee = $tiers->declaration_insee;

        $this->declarant->email = $tiers->getEmailTeledeclaration();

        if($tiers->exist('civaba') && $tiers->civaba){
            $this->add('civaba', $tiers->civaba);
        }

        $this->declarant->exploitant->sexe = $tiers->exploitant->civilite;
        $this->declarant->exploitant->nom = $tiers->exploitant->nom;
        $this->declarant->exploitant->adresse = $tiers->exploitant->adresse;
        $this->declarant->exploitant->code_postal = $tiers->exploitant->code_postal;
        $this->declarant->exploitant->commune = $tiers->exploitant->commune;
        //$this->declarant->exploitant->date_naissance = $tiers->exploitant->date_naissance;
        $this->declarant->exploitant->telephone = $tiers->exploitant->telephone;
    }

    public function storeInfos() {
        $this->storeStockage();
        $this->storeDeclarant();
    }

    public function getLastDocument() {
        return $this->getLastDR();
    }

    private function getLastDR() {
        return DRClient::getInstance()->retrieveByCampagneAndCvi($this->identifiant, CurrentClient::getCurrent()->campagne);
    }

    public function isDecembre() {

        return substr($this->periode, 4, 2) == "12";
    }

    public function updateProduitsFromLastDs() {
        $ds = $this->getLastDS();
        if ($ds) {
            $this->updateProduitsFromDS($ds);
        }
    }

    public function updateProduitsFromLastDr(){
        if ($this->isTypeDsPropriete()) {
            $dr = $this->getLastDR();
            if($dr){
                $this->drm_origine = $dr->_id;
                $this->addDetailsFromDRPropriete($dr);
            }
        }
        if($this->isTypeDsNegoce()){
            $this->addDetailsFromDRNegoce();
        }
    }

    protected function updateProduitsFromDS($ds) {
        $this->drm_origine = $ds->_id;
        $this->addDetailsFromDS($ds);
    }

    public function isTypeDsPropriete(){
        return ($this->exist('type_ds') && ($this->get('type_ds') == DSCivaClient::TYPE_DS_PROPRIETE));
    }

    public function isTypeDsNegoce(){
        return ($this->exist('type_ds') && ($this->get('type_ds') == DSCivaClient::TYPE_DS_NEGOCE));
    }

    public function getTypeDs() {
        if($this->exist('type_ds')) {

            return $this->_get('type_ds');
        }

        return DSCivaClient::TYPE_DS_PROPRIETE;
    }

    public function getLastDS() {
        return DSCivaClient::getInstance()->getLastDs($this);
    }

    public function addNoeud($hash) {
        $hash = preg_replace('/^\/recolte/','declaration', $hash);
        $hash = preg_replace("/(mentionVT|mentionSGN)/", "mention", $hash);
        $noeud = $this->getOrAdd($hash);
        $config = $noeud->getConfig();
        $noeud->libelle = $config->getLibelle();
        if($noeud instanceof DSCepage && count($config->getParentNode()->getChildrenNode()) <= 1 && !$config->hasLieuEditable()) {
            $this->addDetail($hash);
        }

        if($config->isAutoDs()) {
            foreach($config->getProduits() as $item) {
                if(!$item->isForDS()) {
                    continue;
                }
                $this->addDetail(HashMapper::inverse($item->getHash()));
            }
        }

        if($noeud instanceof DSAppellation) {
            $this->addNoeud(HashMapper::inverse($config->getChildrenNode()->getFirst()->getHash()));
        } elseif(count($config->getChildrenNode()) == 1) {
            $this->addNoeud(HashMapper::inverse($config->getChildrenNode()->getFirst()->getHash()));
        }

        return $noeud;
    }

    public function addAppellation($hash) {

        return $this->addNoeud($hash);
    }

    public function addLieu($hash) {

        return $this->addNoeud($hash);
    }

    public function addDetailsFromDRPropriete($dr) {
        foreach ($dr->getProduitsDetails() as $detail) {
            if(!$detail->cave_particuliere) {

                continue;
            }

            $this->addDetail($detail->getCepage()->getHash(), $detail->lieu);
        }

        if($dr->recolte->getVciCaveParticuliere() > 0) {
            $this->addAppellation("declaration/certification/genreVCI");
        }
    }

    public function addDetailsFromDRNegoce()
    {
        $campagne = CurrentClient::getCurrent()->campagne;
        $cvi_acheteur = $this->getEtablissement()->getCvi();
        if(!$cvi_acheteur) {
            return;
        }
        $drs = DRClient::getInstance()->findAllByCampagneAndCviAcheteur($campagne, $cvi_acheteur, acCouchdbClient::HYDRATE_ON_DEMAND);
        foreach ($drs as $id => $doc) {
            $dr = acCouchdbManager::getClient('DR')->find($id);
            foreach ($dr->getProduitsDetails() as $detail) {
                if(!$detail->getVolumeByAcheteur($cvi_acheteur)) {

                    continue;
                }
                $this->addDetail($detail->getCepage()->getHash(), $detail->lieu);
            }
        }
    }

    public function addDetailsFromDS($ds)
    {
        foreach ($ds->declaration->getProduitsDetails() as $detail) {
            $this->addDetail($detail->getCepage()->getHash(), $detail->lieu, true);
        }
{'fraction_non_alphanumeric': 0.1583030820698982, 'fraction_numerical': 0.000996512207274539, 'mean_word_length': 2.623001547189273, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 297, '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': '1562078', 'n_tokens_mistral': 4515, 'n_tokens_neox': 4342, 'n_words': 627}