<commit_before><?php

class maintenanceRegionTask extends sfBaseTask
{
  protected function configure()
  {
    // // add your own arguments here
    $this->addArguments(array(
       new sfCommandArgument('document_id', sfCommandArgument::REQUIRED, 'Document ID to change'),
    ));

    $this->addOptions(array(
      new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name'),
      new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
      new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'default'),
      // add your own options here
    ));

    $this->namespace        = 'maintenance';
    $this->name             = 'region';
    $this->briefDescription = '';
    $this->detailedDescription = <<<EOF
The [maintenanceCompteStatut|INFO] task does things.
Call it with:

  [php symfony maintenanceCompteStatut|INFO]
EOF;
  }

  protected function execute($arguments = array(), $options = array())
  {
    // initialize the database connection
    $databaseManager = new sfDatabaseManager($this->configuration);
    $connection = $databaseManager->getDatabase($options['connection'])->getConnection();
    $this->document_id = $arguments['document_id'];
    $doc = acCouchdbManager::getClient()->find($this->document_id);
    if (!$doc) {
      throw new sfException('document '.$arguments['document_id'].' not found');
    }
    if ($doc->type == 'DRM') {
      $nregion = $this->getNewRegion($doc->region, $doc->declarant->code_postal);
      $doc->region = $nregion;
      $doc->declarant->region = $nregion;
    }elseif ($doc->type == 'Etablissement') {
      $doc->region = $this->getNewRegion($doc->region, $doc->siege->code_postal);
    }elseif( $doc->type == 'Vrac') {
      $doc->acheteur->region =  $this->getNewRegion($doc->acheteur->region, $doc->acheteur->code_postal);
      $doc->vendeur->region =  $this->getNewRegion($doc->vendeur->region, $doc->vendeur->code_postal);
    }else{
      return ;
    }
    echo "Region changed for ".$doc->_id."\n";
    $doc->save();
  }

  private function getNewRegion($oldregion, $codepostal) {
      $dep = substr(sprintf('%05d', $codepostal), 0, 2);
      $preregion = '';
      if (in_array($dep, array('85', '44', '49', '79', '86'))) {
        $preregion = 'PDL';
      }elseif(in_array($dep, array('72', '37', '41', '36', '45', '18', '58', '03', '63'))) {
        $preregion = 'CENTRE';
      }else{
        if ($oldregion == EtablissementClient::REGION_HORSINTERLOIRE)
          return EtablissementClient::REGION_HORS_REGION;
      }
      if (!$preregion) {
        throw new sfException($this->document_id.' : Strange region from '.$codepostal.' - '.$oldregion);
      }
      $postregion = 'IGP';
      if ($oldregion != EtablissementClient::REGION_HORSINTERLOIRE) {
        $postregion = 'AOP';
      }
      if (<commit_msg>Ne bloque pas si l'établissement a déjà été édité<commit_after><?php

class maintenanceRegionTask extends sfBaseTask
{
  protected function configure()
  {
    // // add your own arguments here
    $this->addArguments(array(
       new sfCommandArgument('document_id', sfCommandArgument::REQUIRED, 'Document ID to change'),
    ));

    $this->addOptions(array(
      new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name'),
      new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
      new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'default'),
      // add your own options here
    ));

    $this->namespace        = 'maintenance';
    $this->name             = 'region';
    $this->briefDescription = '';
    $this->detailedDescription = <<<EOF
The [maintenanceCompteStatut|INFO] task does things.
Call it with:

  [php symfony maintenanceCompteStatut|INFO]
EOF;
  }

  protected function execute($arguments = array(), $options = array())
  {
    // initialize the database connection
    $databaseManager = new sfDatabaseManager($this->configuration);
    $connection = $databaseManager->getDatabase($options['connection'])->getConnection();
    $this->document_id = $arguments['document_id'];
    $doc = acCouchdbManager::getClient()->find($this->document_id);
    if (!$doc) {
      throw new sfException('document '.$arguments['document_id'].' not found');
    }
    if ($doc->type == 'DRM') {
      $nregion = $this->getNewRegion($doc->region, $doc->declarant->code_postal);
      $doc->region = $nregion;
      $doc->declarant->region = $nregion;
    }elseif ($doc->type == 'Etablissement') {
      $doc->region = $this->getNewRegion($doc->region, $doc->siege->code_postal);
    }elseif( $doc->type == 'Vrac') {
      $doc->acheteur->region =  $this->getNewRegion($doc->acheteur->region, $doc->acheteur->code_postal);
      $doc->vendeur->region =  $this->getNewRegion($doc->vendeur->region, $doc->vendeur->code_postal);
    }else{
      return ;
    }
    echo "Region changed for ".$doc->_id."\n";
    $doc->save();
  }

  private function getNewRegion($oldregion, $codepostal) {
      $dep = substr(sprintf('%05d', $codepostal), 0, 2);
      $preregion = '';
      if (in_array($dep, array('85', '44', '49', '79', '86'))) {
        $preregion = 'PDL';
      }elseif(in_array($dep, array('72', '37', '41', '36', '45', '18', '58', '03', '63'))) {
        $preregion = 'CENTRE';
      }else{
        if ($oldregion == EtablissementClient::REGION_HORSINTERLOIRE || $oldregion == EtablissementClient::REGION_HORS_REGION)
          return EtablissementClient::REGION_HORS_REGION;
      }
      if (!$preregion) {
        throw new sfException($this->document_id.' : Strange region from '.$codepostal.' - '.$oldregion);
      }
      $postregion = 'IGP';
      if ($oldregion != EtablissementClient::REGION_HORSINTERLOIRE) {
        $postregion = 'AOP';
      }
      if (
{'fraction_non_alphanumeric': 0.1712846347607053, 'fraction_numerical': 0.010747271200671704, 'mean_word_length': 3.6134779240898527, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 91, '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': '2360756', 'n_tokens_mistral': 1900, 'n_tokens_neox': 1886, 'n_words': 376}