<commit_before><?php

class importHabilitationCsvTask extends sfBaseTask
{

  const CSV_IDENTIFIANT = 0;

  const CSV_ACTIVITES = 14;
  const CSV_STATUT = 15;
  const CSV_DATE_DEMANDE_HABILITATION = 30;
  const CSV_DATE_HABILITATION = 31;
  const CSV_DATE_ARCHIVAGE = 32;
  const CSV_COMMENTAIRE = 33;


    protected $types_ignore = array();
    protected $dateHabilitation = null;
    protected $dateArchivage = null;
    protected $dateDemande = null;

    protected function configure()
    {
        $this->addArguments(array(
            new sfCommandArgument('fichier_habilitations', sfCommandArgument::REQUIRED, "Fichier csv pour l'import"),
        ));

        $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'),
        ));

        $this->namespace = 'import';
        $this->name = 'habilitation-from-csv';
        $this->briefDescription = 'Import des habilitation (via un csv)';
        $this->detailedDescription = <<<EOF
EOF;

        $this->produitKey = 'certifications/AOP/genres/TRANQ/appellations/CDP';
        $this->dateHabilitation = '2016-08-01';
        $this->dateArchivage = null;

        $this->convert_statut = array();
        $this->convert_statut["En attente d'habilitation"] = HabilitationClient::STATUT_ATTENTE_HABILITATION;
        $this->convert_statut["Archivé"] = HabilitationClient::STATUT_ARCHIVE;
        $this->convert_statut['Habilité'] = HabilitationClient::STATUT_HABILITE;
        $this->convert_statut['Refus'] = HabilitationClient::STATUT_REFUS;
        $this->convert_statut["Retrait d'habilitation"] = HabilitationClient::STATUT_RETRAIT;
        $this->convert_statut["Suspension d'habilitation"] = HabilitationClient::STATUT_SUSPENDU;

        $this->convert_activites = array();
        $this->convert_activites['Producteur de raisins'] = HabilitationClient::ACTIVITE_PRODUCTEUR;
        $this->convert_activites['Vinificateur'] = HabilitationClient::ACTIVITE_VINIFICATEUR;
        $this->convert_activites['Détenteur de vin en vrac'] = HabilitationClient::ACTIVITE_VRAC;
        $this->convert_activites['Conditionneur'] = HabilitationClient::ACTIVITE_CONDITIONNEUR;
        $this->convert_activites['Producteur de moût'] = HabilitationClient::ACTIVITE_PRODUCTEUR_MOUTS;
        $this->convert_activites['Eleveur de DGC'] = HabilitationClient::ACTIVITE_ELEVEUR_DGC;

        $this->convert_activites['elaborateur'] = HabilitationClient::ACTIVITE_ELABORATEUR;
        $this->convert_activites['vente tireuse'] = HabilitationClient::ACTIVITE_VENTE_A_LA_TIREUSE;
    }

    protected function execute($arguments = array(), $options = array())
    {
        // initialize the database connection
        $databaseManager = new sfDatabaseManager($this->configuration);
        $connection = $databaseManager->getDatabase($options['connection'])->getConnection();
        $datas = array();
        $date_dossiers = array();
        foreach(file($arguments['fichier_habilitations']) as $line) {
            $line = str_replace("\n", "", $line);

            if(preg_match("/^\"tbl_CDPOps/", $line)) {
                continue;
            }
            $data = str_getcsv($line, ';');

             if (!$data) {
               continue;
             }

             $identifiant = sprintf('%s', $data[self::CSV_IDENTIFIANT]);
             echo "trying $identifiant \n";
             $eta = EtablissementClient::getInstance()->findByIdentifiant($identifiant."01");
            if (!$eta) {
              echo "WARNING: établissement non trouvé ".$identifiant." : pas d'import\n";
              continue;
            }


            $dateHabilitation = DateTime::createFromFormat("d/m/Y",$this->convertDate($data[self::CSV_DATE_HABILITATION]));
            $this->dateHabilitation = ($dateHabilitation)? $dateHabilitation->format("Y-m-d") : null;

            $dateDemande = DateTime::createFromFormat("d/m/Y",$this->convertDate($data[self::CSV_DATE_DEMANDE_HABILITATION]));
            $this->dateDemande = ($dateDemande)? $dateDemande->format("Y-m-d") : $this->dateHabilitation;
            echo "$eta->identifiant $this->dateDemande\n";

            $habilitation = HabilitationClient::getInstance()->createOrGetDocFromIdentifiantAndDate($eta->identifiant, $this->dateDemande);
            $hab_activites = $habilitation->addProduit($this->produitKey)->add('activites');


            $dateArchivageHabilitation = DateTime::createFromFormat("d/m/Y",$this->convertDate($data[self::CSV_DATE_ARCHIVAGE]));
            $this->dateArchivage = ($dateArchivageHabilitation)? $dateArchivageHabilitation->format("Y-m-d") : null;



            $statut = $this->convert_statut[$data[self::CSV_STATUT]];

            //demande
            if($statut == HabilitationClient::STATUT_ATTENTE_HABILITATION){
                $this->updateHabilitationStatut($hab_activites,$data,HabilitationClient::STATUT_ATTENTE_HABILITATION,$this->dateDemande);
            }

            //statut habilite
            if($statut == HabilitationClient::STATUT_HABILITE){
                $this->updateHabilitationStatut($hab_activites,$data,$statut,$this->dateHabilitation);
            }
            //statut archive<commit_msg>mis à jour de la tache d'habilitation pour virer les vieilles hab avant de sauver<commit_after><?php

class importHabilitationCsvTask extends sfBaseTask
{

  const CSV_IDENTIFIANT = 0;

  const CSV_ACTIVITES = 14;
  const CSV_STATUT = 15;
  const CSV_DATE_DEMANDE_HABILITATION = 30;
  const CSV_DATE_HABILITATION = 31;
  const CSV_DATE_ARCHIVAGE = 32;
  const CSV_COMMENTAIRE = 33;


    protected $types_ignore = array();
    protected $dateHabilitation = null;
    protected $dateArchivage = null;
    protected $dateDemande = null;

    protected function configure()
    {
        $this->addArguments(array(
            new sfCommandArgument('fichier_habilitations', sfCommandArgument::REQUIRED, "Fichier csv pour l'import"),
        ));

        $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'),
        ));

        $this->namespace = 'import';
        $this->name = 'habilitation-from-csv';
        $this->briefDescription = 'Import des habilitation (via un csv)';
        $this->detailedDescription = <<<EOF
EOF;

        $this->produitKey = 'certifications/AOP/genres/TRANQ/appellations/CDP';
        $this->dateHabilitation = '2016-08-01';
        $this->dateArchivage = null;

        $this->convert_statut = array();
        $this->convert_statut["En attente d'habilitation"] = HabilitationClient::STATUT_ATTENTE_HABILITATION;
        $this->convert_statut["Archivé"] = HabilitationClient::STATUT_ARCHIVE;
        $this->convert_statut['Habilité'] = HabilitationClient::STATUT_HABILITE;
        $this->convert_statut['Refus'] = HabilitationClient::STATUT_REFUS;
        $this->convert_statut["Retrait d'habilitation"] = HabilitationClient::STATUT_RETRAIT;
        $this->convert_statut["Suspension d'habilitation"] = HabilitationClient::STATUT_SUSPENDU;

        $this->convert_activites = array();
        $this->convert_activites['Producteur de raisins'] = HabilitationClient::ACTIVITE_PRODUCTEUR;
        $this->convert_activites['Vinificateur'] = HabilitationClient::ACTIVITE_VINIFICATEUR;
        $this->convert_activites['Détenteur de vin en vrac'] = HabilitationClient::ACTIVITE_VRAC;
        $this->convert_activites['Conditionneur'] = HabilitationClient::ACTIVITE_CONDITIONNEUR;
        $this->convert_activites['Producteur de moût'] = HabilitationClient::ACTIVITE_PRODUCTEUR_MOUTS;
        $this->convert_activites['Eleveur de DGC'] = HabilitationClient::ACTIVITE_ELEVEUR_DGC;

        $this->convert_activites['elaborateur'] = HabilitationClient::ACTIVITE_ELABORATEUR;
        $this->convert_activites['vente tireuse'] = HabilitationClient::ACTIVITE_VENTE_A_LA_TIREUSE;
    }

    protected function execute($arguments = array(), $options = array())
    {
        // initialize the database connection
        $databaseManager = new sfDatabaseManager($this->configuration);
        $connection = $databaseManager->getDatabase($options['connection'])->getConnection();
        $datas = array();
        $date_dossiers = array();
        foreach(file($arguments['fichier_habilitations']) as $line) {
            $line = str_replace("\n", "", $line);

            if(preg_match("/^\"tbl_CDPOps/", $line)) {
                continue;
            }
            $data = str_getcsv($line, ';');

             if (!$data) {
               continue;
             }

             $identifiant = sprintf('%s', $data[self::CSV_IDENTIFIANT]);
             echo " => import pour  $identifiant \n";
             $eta = EtablissementClient::getInstance()->findByIdentifiant($identifiant."01");
             if (!$eta) {
                 echo "WARNING: établissement non trouvé ".$identifiant." : pas d'import\n";
                 continue;
             }

             // ON VIRE LES HABILITATIONS DEJA CREER
             $habs = HabilitationClient::getInstance()->getHistory($eta->identifiant);
             foreach ($habs as $key => $hab) {
                echo "/!\ : on vire l'habilitation précédente ".$hab->_id." \n";
                $hab->delete();
             }



            $dateHabilitation = DateTime::createFromFormat("d/m/Y",$this->convertDate($data[self::CSV_DATE_HABILITATION]));
            $this->dateHabilitation = ($dateHabilitation)? $dateHabilitation->format("Y-m-d") : null;

            $dateDemande = DateTime::createFromFormat("d/m/Y",$this->convertDate($data[self::CSV_DATE_DEMANDE_HABILITATION]));
            $this->dateDemande = ($dateDemande)? $dateDemande->format("Y-m-d") : $this->dateHabilitation;


            $habilitation = HabilitationClient::getInstance()->createOrGetDocFromIdentifiantAndDate($eta->identifiant, $this->dateDemande);
            $hab_activites = $habilitation->addProduit($this->produitKey)->add('activites');


            $dateArchivageHabilitation = DateTime::createFromFormat("d/m/Y",$this->convertDate($data[self::CSV_DATE_ARCHIVAGE]));
            $this->dateArchivage = ($dateArchivageHabilitation)? $dateArchivageHabilitation->format("Y-m-d") : null;



            $statut = $this->convert_statut[$data[self::CSV_STATUT]];

            //demande
            if($statut == HabilitationClient::STATUT_ATTENTE_HABILITATION){
                $this->updateHabilitationStatut($hab_activites,$data,HabilitationClient::STATUT_ATTENTE_HABILITATION,$this->dateDemande);
            }

            //statut habilite
            if($statut == HabilitationClient::STATUT_HABILITE){
                $this->updateHabilitationStatut($hab_activites,$data,$statut,$this->dateHabilitation);
            }
            //statut archive
{'fraction_non_alphanumeric': 0.13405443745012854, 'fraction_numerical': 0.004078375742530367, 'mean_word_length': 3.3839875631558494, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 111, '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': '5664400', 'n_tokens_mistral': 3515, 'n_tokens_neox': 3405, 'n_words': 494}