<commit_before><?php
class DR extends BaseDR {
    const ETAPE_EXPLOITATION = 'exploitation';
    const ETAPE_RECOLTE = 'recolte';
    const ETAPE_VALIDATION = 'validation';
    
    public static $_etapes = array(DR::ETAPE_EXPLOITATION, DR::ETAPE_RECOLTE, DR::ETAPE_VALIDATION);
    public static $_etapes_inclusion = array(self::ETAPE_EXPLOITATION => array(), self::ETAPE_RECOLTE => array(self::ETAPE_EXPLOITATION), self::ETAPE_VALIDATION => array(self::ETAPE_EXPLOITATION, self::ETAPE_RECOLTE));
    
    /**
     *
     * @param string $etape
     * @return boolean
     */
    public function addEtape($etape) {
        if (!in_array($etape, self::$_etapes)) {
            throw new sfException("etape does not exist");
        }
        if ($this->checkEtape($etape)) {
            $this->add('etape');
            $this->etape = $etape;
            return true;
        } else {
            return false;
        }
    }
    
    /**
     *
     * @param string $etape
     * @return boolean 
     */
    protected function checkEtape($etape) {
        if ($this->exist('etape') && $this->etape && !in_array($this->etape, self::$_etapes_inclusion[$etape])) {
            return false;
        }      
        if ($etape == self::ETAPE_EXPLOITATION) {
            return true;
        } elseif ($etape == self::ETAPE_RECOLTE) {
            return true;
            //return ($this->recolte->hasOneOrMoreAppellation());
        } elseif ($etape == self::ETAPE_VALIDATION) {
            return true;
        }
        return true;
    }
    
    /**
     *
     */
    public function removeVolumes() {
      $this->lies = null;
      $this->recolte->removeVolumes();
    }
    
    /**
     *
     * @return float 
     */
    public function getTotalSuperficie() {
      $v = 0;
      foreach($this->recolte->filter('^appellation_') as $appellation) {
	$v += $appellation->getTotalSuperficie();
      }
      return $v;
    }
    
    /**
     *
     * @return float 
     */
    public function getTotalVolume() {
      $v = 0;
      foreach($this->recolte->filter('^appellation_') as $appellation) {
	$v += $appellation->getTotalVolume();
      }
      return $v;
    }
    
    /**
     *
     * @return float 
     */
    public function getVolumeRevendique() {
      $v = 0;
      foreach($this->recolte->filter('^appellation_') as $appellation) {
	$v += $appellation->getVolumeRevendique();
      }
      return $v;
    }
    
    /**
     *
     * @return float 
     */
    public function getDplc() {
      $v = 0;
      foreach($this->recolte->filter('^appellation_') as $appellation) {
	$v += $appellation->getDplc();
      }
      return $v;
    }
    
    /**
     *
     * @return float 
     */
    public function getTotalCaveParticuliere() {
      $v = 0;
      foreach($this->recolte->filter('^appellation_') as $appellation) {
	$v += $appellation->getTotalCaveParticuliere();
      }
      return $v;
    }
    
    /**
     *
     * @return float 
     */
    public function getRatioLies() {
      if (!($v = $this->getTotalCaveParticuliere())) {
       return 0;
      }

      if ($this->recolte->exist('appellation_VINTABLE')) {
        $v -= $this->recolte->get('appellation_VINTABLE')->getTotalCaveParticuliere();
      }

      if($v <= 0) {
          return 0;
      }

      return $this->lies / $v;
    }

    /**
     *
     * @return float 
     */
    public function getLies(){
        $v = $this->_get('lies');
        if(!$v)
            return 0;
        else
            return $v;
    }

    /**
     *
     * @return boolean 
     */
    public function canUpdate() {
      return !$this->exist('modifiee') || !$this->modifiee;
    }

    /**
     *
     * @return boolean 
     */
    public function isValideeCiva() {
      if ($this->exist('modifiee')) {
          return $this->modifiee;
      }
      return false;
    }

    /**
     *
     * @return boolean 
     */
    public function isValideeTiers() {
      if ($this->exist('validee')) {
          return $this->validee;
      }
      return false;
    }

    /**
     *
     * @param Tiers $tiers 
     */
    public function validate($tiers, $compte = null, $compteValidateurId = null){
        $this->remove('etape');
        $this->add('modifiee', date('Y-m-d'));
        if (!$this->exist('validee') || !$this->validee) {
            $this->add('validee', date('Y-m-d'));
        }
        $this->declarant->nom =  $tiers->get('nom');
        if ($compte) {
            $this->declarant->email =  $compte->email;
        } else {
            $this->declarant->email =  $tiers->get('email');
        }
        $this->declarant->telephone =  $tiers->get('telephone');
	if ($compteValidateurId) {
	  $this->utilisateurs->validation->add($compteValidateurId, date('d/m/Y'));
	}
    }

    /**
     *
     * @return string 
     */
    public function getDateModifieeFr() {
        return preg_replace('/(\d+)\-(\d+)\-(\d+)/', '\3/\2/\1', $this->get('modifiee'));
    }

    /**
     *
     * @return string 
     */
    public function getDateValideeFr() {
        return preg_replace('/(\d+)\-(\d+)\-(\d+)/', '\3/\2/\1', $this->get('validee'));
    }

    /**
     *
     * @return float 
     */
    public function getJeunesVignes(){
        $v = $this->_get('jeunes_vignes');
        if(!$v)
            return 0;
        else
            return $v;
    }

    /**
     *
     * @return boolean 
     */
    public function clean() {
        $clean = false;
        foreach($this->recolte->getAppellations() as $appellation)  {
            foreach($appellation->getLieux() as $lieu) {
                 foreach($lieu->getCouleurs() as $couleur) {
                    foreach($couleur->getCepages() as $cepage) {
                        if (count($cepage->detail) < 1) {
                            $cepage->getParent()->remove($cepage->getKey());
                            $clean = true;
                        }
                    }
                    if (count($couleur->getCepages()) < 1) {
                        $couleur->getParent()->remove($couleur->getKey());
                        $clean = true;
                    }
                }
                if (count($lieu->getCouleurs()) < 1) {
                    $lieu->getParent()->remove($lieu->getKey());
                    $clean = true;
                }
            }
            if (count($appellation->getLieux()) < 1) {
                $this->recolte->remove($appellation->getKey());
                $this->acheteurs->remove($appellation->getKey());
                $clean = true;
            }
        }

        return $clean;
    }
    
    /**
     *
     * @param array $params 
     */
    public function update($params = array()) {
      parent::update($params);
      $u = $this->add('updated', 1);
    }

    public function getConfigurationCampagne() {
      return sfCouchdbManager::getClient('Configuration')->retrieveConfiguration($this->campagne);
    }

    public function setCampagne($campagne) {
      $nextCampagne = sfCouchdbManager::getClient('Configuration')->retrieveConfiguration($campagne);
      foreach ($this->recolte->getAppellations() as $k => $a) {
	if (!$nextCampagne->get($a->getParent()->getHash())->exist($k)) {
	    $this->recolte->remove($k);
	    continue;
	}
	foreach ($a->filter('^lieu') as $k => $l) {
	  if (!$nextCampagne->get($l->getParent()->getHash())->exist($k)) {
	    $this->recolte->remove($k);
	    continue;
	  }
	  foreach ($l->filter('^couleur') as $k => $co) {
	    if (!$nextCampagne->get($co->getParent()->getHash())->exist($k)) {
	      $this->recolte->remove($k);
	      continue;
	    }
	    foreach ($co->filter('^cepage') as $k => $c) {
	      if (!$nextCampagne->get($c->getParent()->getHash())->exist($k)) {
		$this->recolte->remove($k);
		continue;
	      }
	    }
	  }
	}
      }
      return $this->_set('campagne', $campagne);
    }
    
    public function getRecoltantObject() {
       return sfCouchdbManager::getClient('Recoltant')->retrieveByCvi($this->cvi); 
    }
    
    public function save() {
        parent::save();
    }

    public function check() {
      $onglet = new RecolteOnglets($this);
      $validLogVigilance = array();
      $validLogErreur = array();
      foreach ($this->recolte->filter('appellation_') as $appellation) {
	foreach ($appellation->filter('lieu') as $lieu) {
	  //check le total superficie
	  if ($lieu->getTotalSuperficie() == 0) {
	    array_push($validLogVigilance, array('url_log_param' => $onglet->getUrlParams($appellation->getKey(), $lieu->getKey()), 'log' => $lieu->getLibelleWithAppellation() . ' => ' . sfCouchdbManager::getClient('Messages')->getMessage('err_log_superficie_zero')));
	  }
	  //check le lieu
	  if ($lieu->isNonSaisie()) {
	    array_push($validLogErreur, array('url_log_param' => $onglet->getUrlParams($appellation->getKey(), $lieu->getKey()), 'log' => $lieu->getLibelleWithAppellation() . ' => ' . sfCouchdbManager::getClient('Messages')->getMessage('err_log_lieu_non_saisie')));
	  } else {

	  //verifie les rebeches pour les crémants
          if ($appellation->getConfig()->appellation == 'CREMANT' && round($lieu->getTotalVolumeForMinQuantite(), 2) > 0) {

              $rebeches = false;
              $cepage_RB = $lieu->getCepageRB();
              if(!is_null($cepage_RB)) {

                 $total_non_negociant = $lieu->getTotalVolumeForMinQuantite() ;
                 $min_quantite = round($total_non_negociant *  $cepage_RB->getConfig()->min_quantite, 0 );
                 $max_quantite = round($total_non_negociant * $cepage_RB->getConfig()->max_quantite, 0 );

                  if( !is_null($min_quantite)
                   && !is_null($max_quantite)
                   &&  $min_quantite <= $cepage_RB->getTotalVolume()
                   &&  $max_quantite >= $cepage_RB->getTotalVolume() )
                      $rebeches = true;
                  else
                      $rebeches = false;

                  if (!$rebeches) {

                      // prevoir points de vigilance pour le max et pour le min ??
                      if( !($min_quantite <= $cepage_RB->getVolumeTotal()) )
                         array_push($validLogErreur, array('url_log_param' => $onglet->getUrlParams($appellation->getKey(), $lieu->getKey(), $lieu->getCouleur('cepage_RB')->getKey(), 'cepage_RB'), 'log' => $lieu->getLibelleWithAppellation() . ' => ' . sfCouchdbManager::getClient('Messages')->getMessage('err_log_cremant_pas_rebeches')));
                      elseif( !($max_quantite <= $cepage_RB->getVolumeTotal()) )
                          array_push($validLogErreur, array('url_log_param' => $onglet->getUrlParams($appellation->getKey(), $lieu->getKey(), $lieu->getCouleur('cepage_RB')->getKey(), 'cepage_RB'), 'log' => $lieu->getLibelleWithAppellation() . ' => ' . sfCouchdbManager::getClient('Messages')->getMessage('err_log_cremant_pas_rebeches')));
                  }
              }else{
                  array_push($validLogErreur, array('url_log_param' => $onglet->getUrlParams($appellation->getKey(), $lieu->getKey(), $lieu->getCouleur('cepage_RB')->getKey(), 'cepage_RB'), 'log' => $lieu->getLibelleWithAppellation() . ' => ' . sfCouchdbManager::getClient('Messages')->getMessage('err_log_cremant_pas_rebeches')));
              }
          }

	    //Verifie que le recapitulatif des ventes est rempli
	    if (!$lieu->hasCompleteRecapitulatifVente()) {<commit_msg>correction rebeches, suppression check sur quantite max pr la validation<commit_after><?php
class DR extends BaseDR {
    const ETAPE_EXPLOITATION = 'exploitation';
    const ETAPE_RECOLTE = 'recolte';
    const ETAPE_VALIDATION = 'validation';
    
    public static $_etapes = array(DR::ETAPE_EXPLOITATION, DR::ETAPE_RECOLTE, DR::ETAPE_VALIDATION);
    public static $_etapes_inclusion = array(self::ETAPE_EXPLOITATION => array(), self::ETAPE_RECOLTE => array(self::ETAPE_EXPLOITATION), self::ETAPE_VALIDATION => array(self::ETAPE_EXPLOITATION, self::ETAPE_RECOLTE));
    
    /**
     *
     * @param string $etape
     * @return boolean
     */
    public function addEtape($etape) {
        if (!in_array($etape, self::$_etapes)) {
            throw new sfException("etape does not exist");
        }
        if ($this->checkEtape($etape)) {
            $this->add('etape');
            $this->etape = $etape;
            return true;
        } else {
            return false;
        }
    }
    
    /**
     *
     * @param string $etape
     * @return boolean 
     */
    protected function checkEtape($etape) {
        if ($this->exist('etape') && $this->etape && !in_array($this->etape, self::$_etapes_inclusion[$etape])) {
            return false;
        }      
        if ($etape == self::ETAPE_EXPLOITATION) {
            return true;
        } elseif ($etape == self::ETAPE_RECOLTE) {
            return true;
            //return ($this->recolte->hasOneOrMoreAppellation());
        } elseif ($etape == self::ETAPE_VALIDATION) {
            return true;
        }
        return true;
    }
    
    /**
     *
     */
    public function removeVolumes() {
      $this->lies = null;
      $this->recolte->removeVolumes();
    }
    
    /**
     *
     * @return float 
     */
    public function getTotalSuperficie() {
      $v = 0;
      foreach($this->recolte->filter('^appellation_') as $appellation) {
	$v += $appellation->getTotalSuperficie();
      }
      return $v;
    }
    
    /**
     *
     * @return float 
     */
    public function getTotalVolume() {
      $v = 0;
      foreach($this->recolte->filter('^appellation_') as $appellation) {
	$v += $appellation->getTotalVolume();
      }
      return $v;
    }
    
    /**
     *
     * @return float 
     */
    public function getVolumeRevendique() {
      $v = 0;
      foreach($this->recolte->filter('^appellation_') as $appellation) {
	$v += $appellation->getVolumeRevendique();
      }
      return $v;
    }
    
    /**
     *
     * @return float 
     */
    public function getDplc() {
      $v = 0;
      foreach($this->recolte->filter('^appellation_') as $appellation) {
	$v += $appellation->getDplc();
      }
      return $v;
    }
    
    /**
     *
     * @return float 
     */
    public function getTotalCaveParticuliere() {
      $v = 0;
      foreach($this->recolte->filter('^appellation_') as $appellation) {
	$v += $appellation->getTotalCaveParticuliere();
      }
      return $v;
    }
    
    /**
     *
     * @return float 
     */
    public function getRatioLies() {
      if (!($v = $this->getTotalCaveParticuliere())) {
       return 0;
      }

      if ($this->recolte->exist('appellation_VINTABLE')) {
        $v -= $this->recolte->get('appellation_VINTABLE')->getTotalCaveParticuliere();
      }

      if($v <= 0) {
          return 0;
      }

      return $this->lies / $v;
    }

    /**
     *
     * @return float 
     */
    public function getLies(){
        $v = $this->_get('lies');
        if(!$v)
            return 0;
        else
            return $v;
    }

    /**
     *
     * @return boolean 
     */
    public function canUpdate() {
      return !$this->exist('modifiee') || !$this->modifiee;
    }

    /**
     *
     * @return boolean 
     */
    public function isValideeCiva() {
      if ($this->exist('modifiee')) {
          return $this->modifiee;
      }
      return false;
    }

    /**
     *
     * @return boolean 
     */
    public function isValideeTiers() {
      if ($this->exist('validee')) {
          return $this->validee;
      }
      return false;
    }

    /**
     *
     * @param Tiers $tiers 
     */
    public function validate($tiers, $compte = null, $compteValidateurId = null){
        $this->remove('etape');
        $this->add('modifiee', date('Y-m-d'));
        if (!$this->exist('validee') || !$this->validee) {
            $this->add('validee', date('Y-m-d'));
        }
        $this->declarant->nom =  $tiers->get('nom');
        if ($compte) {
            $this->declarant->email =  $compte->email;
        } else {
            $this->declarant->email =  $tiers->get('email');
        }
        $this->declarant->telephone =  $tiers->get('telephone');
	if ($compteValidateurId) {
	  $this->utilisateurs->validation->add($compteValidateurId, date('d/m/Y'));
	}
    }

    /**
     *
     * @return string 
     */
    public function getDateModifieeFr() {
        return preg_replace('/(\d+)\-(\d+)\-(\d+)/', '\3/\2/\1', $this->get('modifiee'));
    }

    /**
     *
     * @return string 
     */
    public function getDateValideeFr() {
        return preg_replace('/(\d+)\-(\d+)\-(\d+)/', '\3/\2/\1', $this->get('validee'));
    }

    /**
     *
     * @return float 
     */
    public function getJeunesVignes(){
        $v = $this->_get('jeunes_vignes');
        if(!$v)
            return 0;
        else
            return $v;
    }

    /**
     *
     * @return boolean 
     */
    public function clean() {
        $clean = false;
        foreach($this->recolte->getAppellations() as $appellation)  {
            foreach($appellation->getLieux() as $lieu) {
                 foreach($lieu->getCouleurs() as $couleur) {
                    foreach($couleur->getCepages() as $cepage) {
                        if (count($cepage->detail) < 1) {
                            $cepage->getParent()->remove($cepage->getKey());
                            $clean = true;
                        }
                    }
                    if (count($couleur->getCepages()) < 1) {
                        $couleur->getParent()->remove($couleur->getKey());
                        $clean = true;
                    }
                }
                if (count($lieu->getCouleurs()) < 1) {
                    $lieu->getParent()->remove($lieu->getKey());
                    $clean = true;
                }
            }
            if (count($appellation->getLieux()) < 1) {
                $this->recolte->remove($appellation->getKey());
                $this->acheteurs->remove($appellation->getKey());
                $clean = true;
            }
        }

        return $clean;
    }
    
    /**
     *
     * @param array $params 
     */
    public function update($params = array()) {
      parent::update($params);
      $u = $this->add('updated', 1);
    }

    public function getConfigurationCampagne() {
      return sfCouchdbManager::getClient('Configuration')->retrieveConfiguration($this->campagne);
    }

    public function setCampagne($campagne) {
      $nextCampagne = sfCouchdbManager::getClient('Configuration')->retrieveConfiguration($campagne);
      foreach ($this->recolte->getAppellations() as $k => $a) {
	if (!$nextCampagne->get($a->getParent()->getHash())->exist($k)) {
	    $this->recolte->remove($k);
	    continue;
	}
	foreach ($a->filter('^lieu') as $k => $l) {
	  if (!$nextCampagne->get($l->getParent()->getHash())->exist($k)) {
	    $this->recolte->remove($k);
	    continue;
	  }
	  foreach ($l->filter('^couleur') as $k => $co) {
	    if (!$nextCampagne->get($co->getParent()->getHash())->exist($k)) {
	      $this->recolte->remove($k);
	      continue;
	    }
	    foreach ($co->filter('^cepage') as $k => $c) {
	      if (!$nextCampagne->get($c->getParent()->getHash())->exist($k)) {
		$this->recolte->remove($k);
		continue;
	      }
	    }
	  }
	}
      }
      return $this->_set('campagne', $campagne);
    }
    
    public function getRecoltantObject() {
       return sfCouchdbManager::getClient('Recoltant')->retrieveByCvi($this->cvi); 
    }
    
    public function save() {
        parent::save();
    }

    public function check() {
      $onglet = new RecolteOnglets($this);
      $validLogVigilance = array();
      $validLogErreur = array();
      foreach ($this->recolte->filter('appellation_') as $appellation) {
	foreach ($appellation->filter('lieu') as $lieu) {
	  //check le total superficie
	  if ($lieu->getTotalSuperficie() == 0) {
	    array_push($validLogVigilance, array('url_log_param' => $onglet->getUrlParams($appellation->getKey(), $lieu->getKey()), 'log' => $lieu->getLibelleWithAppellation() . ' => ' . sfCouchdbManager::getClient('Messages')->getMessage('err_log_superficie_zero')));
	  }
	  //check le lieu
	  if ($lieu->isNonSaisie()) {
	    array_push($validLogErreur, array('url_log_param' => $onglet->getUrlParams($appellation->getKey(), $lieu->getKey()), 'log' => $lieu->getLibelleWithAppellation() . ' => ' . sfCouchdbManager::getClient('Messages')->getMessage('err_log_lieu_non_saisie')));
	  } else {

	  //verifie les rebeches pour les crémants
          if ($appellation->getConfig()->appellation == 'CREMANT' && round($lieu->getTotalVolumeForMinQuantite(), 2) > 0) {

              $rebeches = false;
              $cepage_RB = $lieu->getCepageRB();
              if(!is_null($cepage_RB)) {

                 $total_non_negociant = $lieu->getTotalVolumeForMinQuantite() ;
                 $min_quantite = round($total_non_negociant *  $cepage_RB->getConfig()->min_quantite, 0 );
                 $max_quantite = round($total_non_negociant * $cepage_RB->getConfig()->max_quantite, 0 );

                  if( !is_null($min_quantite)
                   &&  $min_quantite <= $cepage_RB->getTotalVolume())
                      $rebeches = true;
                  else
                      $rebeches = false;

                  if (!$rebeches) {

                      // prevoir points de vigilance pour le max et pour le min ??
                      if( !($min_quantite <= $cepage_RB->getVolumeTotal()) )
                         array_push($validLogErreur, array('url_log_param' => $onglet->getUrlParams($appellation->getKey(), $lieu->getKey(), $lieu->getCouleur('cepage_RB')->getKey(), 'cepage_RB'), 'log' => $lieu->getLibelleWithAppellation() . ' => ' . sfCouchdbManager::getClient('Messages')->getMessage('err_log_cremant_pas_rebeches')));
                  }
              }else{
                  array_push($validLogErreur, array('url_log_param' => $onglet->getUrlParams($appellation->getKey(), $lieu->getKey(), $lieu->getCouleur('cepage_RB')->getKey(), 'cepage_RB'), 'log' => $lieu->getLibelleWithAppellation() . ' => ' . sfCouchdbManager::getClient('Messages')->getMessage('err_log_cremant_pas_rebeches')));
              }
          }

	    //Verifie que le recapitulatif des ventes est rempli
	    if (!$lieu->hasCompleteRecapitulatifVente()) {
{'fraction_non_alphanumeric': 0.1717325227963526, 'fraction_numerical': 0.002324333988914715, 'mean_word_length': 2.091901603095633, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 384, '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': '2127386', 'n_tokens_mistral': 7456, 'n_tokens_neox': 7185, 'n_words': 1096}