<commit_before><?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of DRMImportCsvEdi
 *
 * @author mathurin
 */
class DRMImportCsvEdi extends DRMCsvEdi {

    protected $configuration = null;
    protected $mouvements = array();
    protected $csvDoc = null;
    protected $fromEdi = false;

      public function __construct($file, DRM $drm = null) {
            $this->initConf($drm);

            if(is_null($this->csvDoc)) {
                $this->csvDoc = CSVDRMClient::getInstance()->createOrFindDocFromDRM($file, $drm);
            }
            parent::__construct($file, $drm);
        }

        public function getDrm(){
          return $this->drm;
   }
        public function getCsvDoc() {

            return $this->csvDoc;
        }

        public function getCsvArrayErreurs(){
          $csvErreurs = array();
          $csvErreurs[] = array("#Niveau erreur","Numéro ligne de l'erreur","Parametre en erreur ","Diagnostic");
          if($this->getCsvDoc()->hasErreurs()){
            $erreursRows = $this->getCsvDoc()->getErreurs();
            foreach ($erreursRows as $erreur) {
              $erreurLevel = ($erreur->exist('level'))? CSVDRMClient::$levelErrorsLibelle[$erreur->level] : "";
              $csvErreurs[] = array($erreurLevel,"".$erreur->num_ligne,"".$erreur->csv_erreur,$erreur->diagnostic);
            }
          }
          return $csvErreurs;
        }

        protected function initConf($drm) {
            $this->configuration = ConfigurationClient::getCurrent();
            if($drm) {
                $this->configuration = $drm->getConfig();
            }
            $this->mouvements = $this->buildAllMouvements();
        }

        public function getDocRows() {
            return $this->getCsv($this->csvDoc->getFileContent());
        }

    /**
     * CHECK DU CSV
     */
    public function checkCSV() {
        $this->csvDoc->clearErreurs();
        $this->checkCSVIntegrity();
        if ($this->csvDoc->hasErreurs()) {
            $this->csvDoc->setStatut(self::STATUT_ERREUR);
            $this->csvDoc->save();
            return;
        }
        // Check annexes
        $this->checkImportAnnexesFromCSV();
        // Check mouvements
        $this->checkImportMouvementsFromCSV();
        // Check Crds
        $this->checkImportCrdsFromCSV();
        // Check Crds
        $this->checkHorsRegionFromCSV();

        if ($this->csvDoc->hasErreurs()) {
            $this->csvDoc->setStatut(self::STATUT_WARNING);
            $this->csvDoc->save();
            return false;
        }
        $this->csvDoc->setStatut(self::STATUT_VALIDE);
        $this->csvDoc->save();
        return true;
    }

    /**
     * IMPORT DEPUIS LE CSV
     */
    public function importCSV($withSave = true) {
        if($this->drm->isNew()) {
            $this->drm->constructId();
        }
        $this->importAnnexesFromCSV();

        $this->importMouvementsFromCSV();
        $this->importCrdsFromCSV();
        $this->drm->etape = DRMClient::ETAPE_VALIDATION;
        $this->drm->type_creation = "IMPORT";
        $this->drm->etape = ($this->fromEdi)? DRMClient::ETAPE_VALIDATION_EDI : DRMClient::ETAPE_VALIDATION;
        $this->drm->type_creation = DRMClient::DRM_CREATION_EDI;
        $this->drm->buildFavoris();
        $this->drm->storeDeclarant();
        $this->drm->initSociete();
        $this->updateAndControlCoheranceStocks();

        if($withSave) {
            $this->drm->save();
        }
        return true;
    }

    public function updateAndControlCoheranceStocks() {
        $this->drm->update();
        $this->drm->updateStockFinDeMoisAllCrds();
        if ($this->csvDoc->hasErreurs()) {
            $this->csvDoc->setStatut(self::STATUT_WARNING);
            $this->csvDoc->save();
        }
    }

    private function checkCSVIntegrity() {
        $ligne_num = 1;
        foreach ($this->getDocRows() as $csvRow) {
            if(count($csvRow) < 17){
              $this->csvDoc->addErreur($this->createWrongFormatFieldCountError($ligne_num, $csvRow));
              $ligne_num++;
              continue;
            }
            if ($ligne_num == 1 && KeyInflector::slugify($csvRow[self::CSV_TYPE]) == 'TYPE') {
                $ligne_num++;
                continue;
            }
            if (!in_array(KeyInflector::slugify($csvRow[self::CSV_TYPE]), self::$permitted_types)) {
                $this->csvDoc->addErreur($this->createWrongFormatTypeError($ligne_num, $csvRow));
            }
            if (!preg_match('/^[0-9]{6}$/', KeyInflector::slugify($csvRow[self::CSV_PERIODE]))) {
                $this->csvDoc->addErreur($this->createWrongFormatPeriodeError($ligne_num, $csvRow));
            }
            if (!preg_match('/^FR0[0-9A-Z]{10}$/', KeyInflector::slugify($csvRow[self::CSV_NUMACCISE]))) {
                //$this->csvDoc->addErreur($this->createWrongFormatNumAcciseError($ligne_num, $csvRow));
            }
            if($this->drm->getIdentifiant() != KeyInflector::slugify($csvRow[self::CSV_IDENTIFIANT]) && ($this->drm->getEtablissementObject()->getSociete()->identifiant != KeyInflector::slugify($csvRow[self::CSV_IDENTIFIANT]) && (!$csvRow[self::CSV_NUMACCISE] || $this->drm->getEtablissementObject()->no_accises != $csvRow[self::CSV_NUMACCISE]))) {
                $this->csvDoc->addErreur($this->otherNumeroCompteError($ligne_num, $csvRow));
            }
            if($this->drm->getPeriode() != KeyInflector::slugify($csvRow[self::CSV_PERIODE])){
                $this->csvDoc->addErreur($this->otherPeriodeError($ligne_num, $csvRow));
            }
            $ligne_num++;
        }
    }

    private function checkImportMouvementsFromCSV() {
        return $this->importMouvementsFromCSV(true);
    }

    private function checkImportCrdsFromCSV() {
        return $this->importCrdsFromCSV(true);
    }

    private function checkHorsRegionFromCSV() {
    	$etablissementObj = $this->drm->getEtablissementObject();
    	if ($etablissementObj->region == EtablissementClient::REGION_HORS_CVO) {
    		$this->csvDoc->addErreur($this->importHorsRegionError());
    	}
    }

    private function checkImportAnnexesFromCSV() {
        return $this->importAnnexesFromCSV(true);
    }

    private function importMouvementsFromCSV($just_check = false) {
            $aggregatedEdiList = null;
            if(DRMConfiguration::getInstance()->hasAggregatedEdi()){
              $aggregatedEdiList = DRMConfiguration::getInstance()->getAggregatedEdi();
            }
            $all_produits = $this->configuration->declaration->getProduitsAll();

        $num_ligne = 1;
        foreach ($this->getDocRows() as $csvRow) {
            if (KeyInflector::slugify($csvRow[self::CSV_TYPE] != self::TYPE_CAVE)) {
                $num_ligne++;
                continue;
            }
            $csvLibelleProductArray = $this->buildLibellesArrayWithRow($csvRow, true);
 	    $csvLibelleProductComplet = $this->slugifyProduitArrayOrString($csvLibelleProductArray);
            $founded_produit = false;
            $keys_libelle = preg_replace("/[ ]+/", " ", sprintf("%s %s %s %s %s %s %s", $csvRow[self::CSV_CAVE_CERTIFICATION], $csvRow[self::CSV_CAVE_GENRE], $csvRow[self::CSV_CAVE_APPELLATION], $csvRow[self::CSV_CAVE_MENTION], $csvRow[self::CSV_CAVE_LIEU], $csvRow[self::CSV_CAVE_COULEUR], $csvRow[self::CSV_CAVE_CEPAGE]));
            if(!$founded_produit && ($keys_libelle != '      ')) {
                $founded_produit = $this->configuration->identifyProductByLibelle($keys_libelle);
            }
            if(!$founded_produit && preg_match('/(.*) *\(([^\)]+)\)/', $csvRow[self::CSV_CAVE_LIBELLE_PRODUIT], $m)) {
              $produits = $this->configuration->identifyProductByCodeDouane(trim($m[2]));
              if (count($produits) == 1) {
                $founded_produit = $produits[0];
              }else {
                foreach($produits as $p) {
                  if (preg_match('/'.preg_replace('/[\/\(\)]/', '.', $m[1]).'/', $p->getLibelleFormat())) {
                    $founded_produit = $p;
                    break;
                  }
                }
              }
            }
            if(!$founded_produit) {
                $founded_produit = $this->configuration->identifyProductByLibelle(trim(preg_replace('/ *\(.*/', '', preg_replace("/[ ]+/", " ", $csvRow[self::CSV_CAVE_LIBELLE_PRODUIT]))));
            }


            if (!$founded_produit) {
                foreach ($all_produits as $produit) {
                    if ($founded_produit) {
                        break;<commit_msg>pour le civa (et pour les autres) on cherche les produits par libellé unique avec la clé mention à l'endroit classique et la clé mention à la fin du produit .<commit_after><?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of DRMImportCsvEdi
 *
 * @author mathurin
 */
class DRMImportCsvEdi extends DRMCsvEdi {

    protected $configuration = null;
    protected $mouvements = array();
    protected $csvDoc = null;
    protected $fromEdi = false;

      public function __construct($file, DRM $drm = null) {
            $this->initConf($drm);

            if(is_null($this->csvDoc)) {
                $this->csvDoc = CSVDRMClient::getInstance()->createOrFindDocFromDRM($file, $drm);
            }
            parent::__construct($file, $drm);
        }

        public function getDrm(){
          return $this->drm;
   }
        public function getCsvDoc() {

            return $this->csvDoc;
        }

        public function getCsvArrayErreurs(){
          $csvErreurs = array();
          $csvErreurs[] = array("#Niveau erreur","Numéro ligne de l'erreur","Parametre en erreur ","Diagnostic");
          if($this->getCsvDoc()->hasErreurs()){
            $erreursRows = $this->getCsvDoc()->getErreurs();
            foreach ($erreursRows as $erreur) {
              $erreurLevel = ($erreur->exist('level'))? CSVDRMClient::$levelErrorsLibelle[$erreur->level] : "";
              $csvErreurs[] = array($erreurLevel,"".$erreur->num_ligne,"".$erreur->csv_erreur,$erreur->diagnostic);
            }
          }
          return $csvErreurs;
        }

        protected function initConf($drm) {
            $this->configuration = ConfigurationClient::getCurrent();
            if($drm) {
                $this->configuration = $drm->getConfig();
            }
            $this->mouvements = $this->buildAllMouvements();
        }

        public function getDocRows() {
            return $this->getCsv($this->csvDoc->getFileContent());
        }

    /**
     * CHECK DU CSV
     */
    public function checkCSV() {
        $this->csvDoc->clearErreurs();
        $this->checkCSVIntegrity();
        if ($this->csvDoc->hasErreurs()) {
            $this->csvDoc->setStatut(self::STATUT_ERREUR);
            $this->csvDoc->save();
            return;
        }
        // Check annexes
        $this->checkImportAnnexesFromCSV();
        // Check mouvements
        $this->checkImportMouvementsFromCSV();
        // Check Crds
        $this->checkImportCrdsFromCSV();
        // Check Crds
        $this->checkHorsRegionFromCSV();

        if ($this->csvDoc->hasErreurs()) {
            $this->csvDoc->setStatut(self::STATUT_WARNING);
            $this->csvDoc->save();
            return false;
        }
        $this->csvDoc->setStatut(self::STATUT_VALIDE);
        $this->csvDoc->save();
        return true;
    }

    /**
     * IMPORT DEPUIS LE CSV
     */
    public function importCSV($withSave = true) {
        if($this->drm->isNew()) {
            $this->drm->constructId();
        }
        $this->importAnnexesFromCSV();

        $this->importMouvementsFromCSV();
        $this->importCrdsFromCSV();
        $this->drm->etape = DRMClient::ETAPE_VALIDATION;
        $this->drm->type_creation = "IMPORT";
        $this->drm->etape = ($this->fromEdi)? DRMClient::ETAPE_VALIDATION_EDI : DRMClient::ETAPE_VALIDATION;
        $this->drm->type_creation = DRMClient::DRM_CREATION_EDI;
        $this->drm->buildFavoris();
        $this->drm->storeDeclarant();
        $this->drm->initSociete();
        $this->updateAndControlCoheranceStocks();

        if($withSave) {
            $this->drm->save();
        }
        return true;
    }

    public function updateAndControlCoheranceStocks() {
        $this->drm->update();
        $this->drm->updateStockFinDeMoisAllCrds();
        if ($this->csvDoc->hasErreurs()) {
            $this->csvDoc->setStatut(self::STATUT_WARNING);
            $this->csvDoc->save();
        }
    }

    private function checkCSVIntegrity() {
        $ligne_num = 1;
        foreach ($this->getDocRows() as $csvRow) {
            if(count($csvRow) < 17){
              $this->csvDoc->addErreur($this->createWrongFormatFieldCountError($ligne_num, $csvRow));
              $ligne_num++;
              continue;
            }
            if ($ligne_num == 1 && KeyInflector::slugify($csvRow[self::CSV_TYPE]) == 'TYPE') {
                $ligne_num++;
                continue;
            }
            if (!in_array(KeyInflector::slugify($csvRow[self::CSV_TYPE]), self::$permitted_types)) {
                $this->csvDoc->addErreur($this->createWrongFormatTypeError($ligne_num, $csvRow));
            }
            if (!preg_match('/^[0-9]{6}$/', KeyInflector::slugify($csvRow[self::CSV_PERIODE]))) {
                $this->csvDoc->addErreur($this->createWrongFormatPeriodeError($ligne_num, $csvRow));
            }
            if (!preg_match('/^FR0[0-9A-Z]{10}$/', KeyInflector::slugify($csvRow[self::CSV_NUMACCISE]))) {
                //$this->csvDoc->addErreur($this->createWrongFormatNumAcciseError($ligne_num, $csvRow));
            }
            if($this->drm->getIdentifiant() != KeyInflector::slugify($csvRow[self::CSV_IDENTIFIANT]) && ($this->drm->getEtablissementObject()->getSociete()->identifiant != KeyInflector::slugify($csvRow[self::CSV_IDENTIFIANT]) && (!$csvRow[self::CSV_NUMACCISE] || $this->drm->getEtablissementObject()->no_accises != $csvRow[self::CSV_NUMACCISE]))) {
                $this->csvDoc->addErreur($this->otherNumeroCompteError($ligne_num, $csvRow));
            }
            if($this->drm->getPeriode() != KeyInflector::slugify($csvRow[self::CSV_PERIODE])){
                $this->csvDoc->addErreur($this->otherPeriodeError($ligne_num, $csvRow));
            }
            $ligne_num++;
        }
    }

    private function checkImportMouvementsFromCSV() {
        return $this->importMouvementsFromCSV(true);
    }

    private function checkImportCrdsFromCSV() {
        return $this->importCrdsFromCSV(true);
    }

    private function checkHorsRegionFromCSV() {
    	$etablissementObj = $this->drm->getEtablissementObject();
    	if ($etablissementObj->region == EtablissementClient::REGION_HORS_CVO) {
    		$this->csvDoc->addErreur($this->importHorsRegionError());
    	}
    }

    private function checkImportAnnexesFromCSV() {
        return $this->importAnnexesFromCSV(true);
    }

    private function importMouvementsFromCSV($just_check = false) {
            $aggregatedEdiList = null;
            if(DRMConfiguration::getInstance()->hasAggregatedEdi()){
              $aggregatedEdiList = DRMConfiguration::getInstance()->getAggregatedEdi();
            }
            $all_produits = $this->configuration->declaration->getProduitsAll();

        $num_ligne = 1;
        foreach ($this->getDocRows() as $csvRow) {
            if (KeyInflector::slugify($csvRow[self::CSV_TYPE] != self::TYPE_CAVE)) {
                $num_ligne++;
                continue;
            }
            $csvLibelleProductArray = $this->buildLibellesArrayWithRow($csvRow, true);
 	    $csvLibelleProductComplet = $this->slugifyProduitArrayOrString($csvLibelleProductArray);
            $founded_produit = false;
            $keys_libelle = preg_replace("/[ ]+/", " ", sprintf("%s %s %s %s %s %s %s", $csvRow[self::CSV_CAVE_CERTIFICATION], $csvRow[self::CSV_CAVE_GENRE], $csvRow[self::CSV_CAVE_APPELLATION], $csvRow[self::CSV_CAVE_MENTION], $csvRow[self::CSV_CAVE_LIEU], $csvRow[self::CSV_CAVE_COULEUR], $csvRow[self::CSV_CAVE_CEPAGE]));

            $keys_libelle_mention_fin = preg_replace("/[ ]+/", " ", sprintf("%s %s %s %s %s %s %s", $csvRow[self::CSV_CAVE_CERTIFICATION], $csvRow[self::CSV_CAVE_GENRE], $csvRow[self::CSV_CAVE_APPELLATION], $csvRow[self::CSV_CAVE_LIEU], $csvRow[self::CSV_CAVE_COULEUR], $csvRow[self::CSV_CAVE_CEPAGE],$csvRow[self::CSV_CAVE_MENTION]));

            if(!$founded_produit && ($keys_libelle != '      ')) {
                $founded_produit = $this->configuration->identifyProductByLibelle(KeyInflector::slugify(str_replace("AOC AOC","AOC",$keys_libelle)));
            }
            if(!$founded_produit && ($keys_libelle != '      ')) {
                $founded_produit = $this->configuration->identifyProductByLibelle(KeyInflector::slugify(str_replace("AOC AOC","AOC",$keys_libelle_mention_fin)));
            }
            
            if(!$founded_produit && preg_match('/(.*) *\(([^\)]+)\)/', $csvRow[self::CSV_CAVE_LIBELLE_PRODUIT], $m)) {
              $produits = $this->configuration->identifyProductByCodeDouane(trim($m[2]));
              if (count($produits) == 1) {
                $founded_produit = $produits[0];
              }else {
                foreach($produits as $p) {
                  if (preg_match('/'.preg_replace('/[\/\(\)]/', '.', $m[1]).'/', $p->getLibelleFormat())) {
                    $founded_produit = $p;
                    break;
                  }
                }
              }
            }
            if(!$founded_produit) {
                $founded_produit = $this->configuration->identifyProductByLibelle(trim(preg_replace('/ *\(.*/', '', preg_replace("/[ ]+/", " ", $csvRow[self::CSV_CAVE_LIBELLE_PRODUIT]))));
            }


            if (!$founded_produit) {
                foreach ($all_produits as $produit) {
                    if ($founded_produit) {
                        break;
{'fraction_non_alphanumeric': 0.15855331760057076, 'fraction_numerical': 0.0018659788156522694, 'mean_word_length': 2.4485238455715366, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 275, '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': '2651057', 'n_tokens_mistral': 5925, 'n_tokens_neox': 5757, 'n_words': 748}