<commit_before><?php

class ExportDSCiva {

    protected $periode;
    protected $campagne;
    protected $ds_ids;
    protected $client_ds;
    protected $ds_liste;
    protected $etablissementsDb2;

    const CSV_DS_TRAITEE = 22; // "O" ou "N"
    const CSV_DS_DATE_SAISIE = 23; // JJMMAAAA
    const CODE_DOUANE_ED = "1B001S";

    public function __construct($periode, $types_ds = array(DSCivaClient::TYPE_DS_PROPRIETE, DSCivaClient::TYPE_DS_NEGOCE)) {
        if (!preg_match('/^[0-9]{6}$/', $periode)) {
            throw new sfException("La période doit être au format yyyymm ($periode)");
        }
        $this->periode = $periode;
        $this->campagne = substr($periode, 0, 4);
        $this->client_ds = DSCivaClient::getInstance();
        $this->ds_ids = $this->client_ds->getAllIdsByPeriode($this->periode, $types_ds);
        $this->ds_liste = array();
        foreach ($this->ds_ids as $ds_id) {
            $ds = $this->client_ds->find($ds_id);
            $ds_principale = $this->client_ds->getDSPrincipaleByDs($ds);
            if (preg_match('/^C?(67|68)/', $ds->identifiant) && $ds_principale->isValidee()) {
                $this->ds_liste[$ds_principale->_id] = $ds_principale;
                $this->ds_liste[$ds->_id] = $ds;
            }
        }
        $tiersCsv = new Db2Tiers2Csv(sfConfig::get('sf_root_dir')."/data/import/Tiers/Tiers-last");
        $this->etablissementsDb2 = $tiersCsv->getEtablissements();
    }

    public function getDSListe() {
        return $this->ds_liste;
    }

    public function getDSIdListe() {
        return $this->ds_ids;
    }

    public function getDSNonValideesListe() {
        $result = array();
        foreach ($this->ds_ids as $ds_id) {
            $ds = $this->client_ds->find($ds_id);
            if (preg_match('/^C?(67|68)/', $ds->identifiant) && $ds->isDsPrincipale() && !$ds->isValidee()) {
                $result[] = $this->client_ds->find($ds_id);
            }
        }
        return $result;
    }

    public function exportXml() {
        $export_xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" . "\r\n\r\n";
        $export_xml.="<listeDecStock>\r\n";
        $tab_cvi = array();
        $current_cvi = 0;
        foreach ($this->ds_liste as $cpt => $ds) {
            if (in_array($ds->declarant->cvi, $tab_cvi) && $ds->declarant->cvi == $current_cvi) {
                $export_xml.= $this->makeXMLDS($ds);
            } else {
                if (($ds->declarant->cvi != $current_cvi) && $current_cvi != 0) {
                    $export_xml.="\t</decStock>\r\n";
                }
                $current_cvi = $ds->declarant->cvi;
                $export_xml.="\t<decStock numCvi=\"" . $current_cvi . "\" dateDepot=\"".$ds->validee."\">\r\n";
                $export_xml.= $this->makeXMLDS($ds);
                $tab_cvi[] = $current_cvi;
            }
        }
        if (count($this->ds_liste)) {
            $export_xml.="\t</decStock>\r\n";
        }
        $export_xml.="</listeDecStock>\r\n";
        return $export_xml;
    }

    protected function makeXMLDS($ds) {
        $lignes = "";
        if ($ds->isDsPrincipale()) {
            $lignes.="\t\t<volLie>" . $this->convertToFloat($ds->lies, false) . "</volLie>\r\n";
            $lignes.="\t\t<volDplc>" . $this->convertToFloat($ds->dplc, false) . "</volDplc>\r\n";
          //  $lignes.="\t\t<volVinNc>" . $this->convertToFloat(DSCivaClient::getInstance()->getTotalSansIG($ds), false) . "</volVinNc>\r\n";
        }

        $lieu_stockage = $ds->identifiant . $ds->getLieuStockage();
        $produitsAgreges = $this->getProduitsAgregesForDS($ds, true, true);

        foreach ($produitsAgreges as $code_douane => $obj) {
            $lignes.= $this->makeXMLDSLigne($lieu_stockage, $code_douane, $obj->volume);
        }

        if ($ds->hasRebechesForXML()) {
            $lignes .= $this->addXMLDSRebeches($ds);
        }

        $lignes .= $this->addXMLDSMouts($ds);
        return $lignes;
    }

    protected function makeXMLDSLigne($lieu_stockage, $code_douane, $volume) {
        $ligne = "\t\t<ligne>\r\n";
        $ligne .= "\t\t\t<codeInstallation>" . $lieu_stockage . "</codeInstallation>\r\n";
        $ligne .= "\t\t\t<codeProduit>" . $code_douane . "</codeProduit>\r\n";
        $ligne .= "\t\t\t<volume>" . $this->convertToFloat($volume, false) . "</volume>\r\n";
        $ligne .= "\t\t</ligne>\r\n";
        return $ligne;
    }

    protected function createProduitsAgregat($products, $vtsgn = false, $xml = false) {
        $resultAgregat = array();
        foreach ($products as $app_produit => $produit) {
            $appellation_key = preg_replace('/^([\/a-zA-Z]+)\/appellation_([A-Z]+)\/([\/0-9a-zA-Z_\-]+)/', '$2', $app_produit);
            if ($appellation_key == 'VINTABLE' && !$xml){
               continue;
            }
            if (!$vtsgn || $produit->hasVtsgn()) {
                $code_douane = $this->getCodeDouane($produit,'',$xml);
                if (!array_key_exists($code_douane, $resultAgregat)) {
                    $resultAgregat[$code_douane] = $this->initProduitAgregat();
                }
                $this->setProduitAgregat($resultAgregat[$code_douane], $this->convertToFloat($produit->total_stock, false), $produit->getHash(), $produit, $this->convertToFloat($produit->total_normal, false), $this->convertToFloat($produit->total_vt, false), $this->convertToFloat($produit->total_sgn, false));
            } else {

                $volume_vt = $produit->total_vt;
                if ($volume_vt > 0) {
                    $code_douane = $this->getCodeDouane($produit, 'VT');
                    if (!array_key_exists($code_douane, $resultAgregat)) {
                        $resultAgregat[$code_douane] = $this->initProduitAgregat();
                    }
                    $this->setProduitAgregat($resultAgregat[$code_douane], $this->convertToFloat($volume_vt, false), $produit->getHash(), $produit);
                }

                $volume_sgn = $produit->total_sgn;
                if ($volume_sgn > 0) {
                    $code_douane = $this->getCodeDouane($produit, 'SGN');
                    if (!array_key_exists($code_douane, $resultAgregat)) {
                        $resultAgregat[$code_douane] = $this->initProduitAgregat();
                    }
                    $this->setProduitAgregat($resultAgregat[$code_douane], $this->convertToFloat($volume_sgn, false), $produit->getHash(), $produit);
                }

                $volume_normal = $produit->total_normal;
                if ($volume_normal > 0) {
                    $code_douane = $this->getCodeDouane($produit,'',$xml);
                    if (!array_key_exists($code_douane, $resultAgregat)) {
                        $resultAgregat[$code_douane] = $this->initProduitAgregat();
                    }
                    $this->setProduitAgregat($resultAgregat[$code_douane], $this->convertToFloat($volume_normal, false), $produit->getHash(), $produit);
                }
            }
        }
        return $resultAgregat;
    }

    protected function addXMLDSMouts($ds) {
        if (!$ds->hasMouts()) {
            return '';
        }
        $lieu_stockage = $ds->identifiant . $ds->getLieuStockage();
        return $this->makeXMLDSLigne($lieu_stockage, "MC", $ds->getMouts());
    }

    protected function addXMLDSRebeches($ds) {
        if (!$ds->hasRebechesForXML()) {
            return '';
        }
        $lieu_stockage = $ds->identifiant . $ds->getLieuStockage();
        return $this->makeXMLDSLigne($lieu_stockage, "4B999B", $ds->getRebeches());
    }

    public function exportEntete() {
        $entete_string = "";
        foreach ($this->ds_liste as $cpt => $ds) {
            $entete_string.= $this->makeEnteteLigne($ds);
            if ($cpt < (count($this->ds_liste) - 1))
                $entete_string.= "\r\n";
        }
        return $entete_string;
    }

    public function exportLigne() {
        $ligne_string = "";
        foreach ($this->ds_liste as $cpt => $ds) {
            $ligne_string.= $this->makeLignes($ds);
        }
        return $ligne_string;
    }<commit_msg>Évolution XML DS Douane 2020<commit_after><?php

class ExportDSCiva {

    protected $periode;
    protected $campagne;
    protected $ds_ids;
    protected $client_ds;
    protected $ds_liste;
    protected $etablissementsDb2;

    const CSV_DS_TRAITEE = 22; // "O" ou "N"
    const CSV_DS_DATE_SAISIE = 23; // JJMMAAAA
    const CODE_DOUANE_ED = "1B001S 9";

    public function __construct($periode, $types_ds = array(DSCivaClient::TYPE_DS_PROPRIETE, DSCivaClient::TYPE_DS_NEGOCE)) {
        if (!preg_match('/^[0-9]{6}$/', $periode)) {
            throw new sfException("La période doit être au format yyyymm ($periode)");
        }
        $this->periode = $periode;
        $this->campagne = substr($periode, 0, 4);
        $this->client_ds = DSCivaClient::getInstance();
        $this->ds_ids = $this->client_ds->getAllIdsByPeriode($this->periode, $types_ds);
        $this->ds_liste = array();
        foreach ($this->ds_ids as $ds_id) {
            $ds = $this->client_ds->find($ds_id);
            $ds_principale = $this->client_ds->getDSPrincipaleByDs($ds);
            if (preg_match('/^C?(67|68)/', $ds->identifiant) && $ds_principale->isValidee()) {
                $this->ds_liste[$ds_principale->_id] = $ds_principale;
                $this->ds_liste[$ds->_id] = $ds;
            }
        }
        $tiersCsv = new Db2Tiers2Csv(sfConfig::get('sf_root_dir')."/data/import/Tiers/Tiers-last");
        $this->etablissementsDb2 = $tiersCsv->getEtablissements();
    }

    public function getDSListe() {
        return $this->ds_liste;
    }

    public function getDSIdListe() {
        return $this->ds_ids;
    }

    public function getDSNonValideesListe() {
        $result = array();
        foreach ($this->ds_ids as $ds_id) {
            $ds = $this->client_ds->find($ds_id);
            if (preg_match('/^C?(67|68)/', $ds->identifiant) && $ds->isDsPrincipale() && !$ds->isValidee()) {
                $result[] = $this->client_ds->find($ds_id);
            }
        }
        return $result;
    }

    public function exportXml() {
        $export_xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" . "\r\n\r\n";
        $export_xml.="<listeDecStock>\r\n";
        $tab_cvi = array();
        $current_cvi = 0;
        foreach ($this->ds_liste as $cpt => $ds) {
            if (in_array($ds->declarant->cvi, $tab_cvi) && $ds->declarant->cvi == $current_cvi) {
                $export_xml.= $this->makeXMLDS($ds);
            } else {
                if (($ds->declarant->cvi != $current_cvi) && $current_cvi != 0) {
                    $export_xml.="\t</decStock>\r\n";
                }
                $current_cvi = $ds->declarant->cvi;
                $export_xml.="\t<decStock numCvi=\"" . $current_cvi . "\" dateDepot=\"".$ds->validee."\">\r\n";
                $export_xml.= $this->makeXMLDS($ds);
                $tab_cvi[] = $current_cvi;
            }
        }
        if (count($this->ds_liste)) {
            $export_xml.="\t</decStock>\r\n";
        }
        $export_xml.="</listeDecStock>\r\n";
        return $export_xml;
    }

    protected function makeXMLDS($ds) {
        $lignes = "";
        $lieu_stockage = $ds->identifiant . $ds->getLieuStockage();

        if ($ds->isDsPrincipale()) {
            $lignes.= $this->makeXMLDSLigne($lieu_stockage, "LIES", $this->convertToFloat($ds->lies, false));
            $lignes.= $this->makeXMLDSLigne($lieu_stockage, "VDRA", $this->convertToFloat($ds->dplc, false));
        }

        $produitsAgreges = $this->getProduitsAgregesForDS($ds, true, true);

        foreach ($produitsAgreges as $code_douane => $obj) {
            $lignes.= $this->makeXMLDSLigne($lieu_stockage, $code_douane, $obj->volume);
        }

        if ($ds->hasRebechesForXML()) {
            $lignes .= $this->addXMLDSRebeches($ds);
        }

        $lignes .= $this->addXMLDSMouts($ds);
        return $lignes;
    }

    protected function makeXMLDSLigne($lieu_stockage, $code_douane, $volume) {
        $ligne = "\t\t<ligne>\r\n";
        $ligne .= "\t\t\t<codeInstallation>" . $lieu_stockage . "</codeInstallation>\r\n";
        $ligne .= "\t\t\t<codeProduit>" . $code_douane . "</codeProduit>\r\n";
        $ligne .= "\t\t\t<volume>" . $this->convertToFloat($volume, false) . "</volume>\r\n";
        $ligne .= "\t\t</ligne>\r\n";
        return $ligne;
    }

    protected function createProduitsAgregat($products, $vtsgn = false, $xml = false) {
        $resultAgregat = array();
        foreach ($products as $app_produit => $produit) {
            $appellation_key = preg_replace('/^([\/a-zA-Z]+)\/appellation_([A-Z]+)\/([\/0-9a-zA-Z_\-]+)/', '$2', $app_produit);
            if ($appellation_key == 'VINTABLE' && !$xml){
               continue;
            }
            if (!$vtsgn || $produit->hasVtsgn()) {
                $code_douane = $this->getCodeDouane($produit,'',$xml);
                if (!array_key_exists($code_douane, $resultAgregat)) {
                    $resultAgregat[$code_douane] = $this->initProduitAgregat();
                }
                $this->setProduitAgregat($resultAgregat[$code_douane], $this->convertToFloat($produit->total_stock, false), $produit->getHash(), $produit, $this->convertToFloat($produit->total_normal, false), $this->convertToFloat($produit->total_vt, false), $this->convertToFloat($produit->total_sgn, false));
            } else {

                $volume_vt = $produit->total_vt;
                if ($volume_vt > 0) {
                    $code_douane = $this->getCodeDouane($produit, 'VT');
                    if (!array_key_exists($code_douane, $resultAgregat)) {
                        $resultAgregat[$code_douane] = $this->initProduitAgregat();
                    }
                    $this->setProduitAgregat($resultAgregat[$code_douane], $this->convertToFloat($volume_vt, false), $produit->getHash(), $produit);
                }

                $volume_sgn = $produit->total_sgn;
                if ($volume_sgn > 0) {
                    $code_douane = $this->getCodeDouane($produit, 'SGN');
                    if (!array_key_exists($code_douane, $resultAgregat)) {
                        $resultAgregat[$code_douane] = $this->initProduitAgregat();
                    }
                    $this->setProduitAgregat($resultAgregat[$code_douane], $this->convertToFloat($volume_sgn, false), $produit->getHash(), $produit);
                }

                $volume_normal = $produit->total_normal;
                if ($volume_normal > 0) {
                    $code_douane = $this->getCodeDouane($produit,'',$xml);
                    if (!array_key_exists($code_douane, $resultAgregat)) {
                        $resultAgregat[$code_douane] = $this->initProduitAgregat();
                    }
                    $this->setProduitAgregat($resultAgregat[$code_douane], $this->convertToFloat($volume_normal, false), $produit->getHash(), $produit);
                }
            }
        }
        return $resultAgregat;
    }

    protected function addXMLDSMouts($ds) {
        if (!$ds->hasMouts()) {
            return '';
        }
        $lieu_stockage = $ds->identifiant . $ds->getLieuStockage();
        return $this->makeXMLDSLigne($lieu_stockage, "MCR", $ds->getMouts());
    }

    protected function addXMLDSRebeches($ds) {
        if (!$ds->hasRebechesForXML()) {
            return '';
        }
        $lieu_stockage = $ds->identifiant . $ds->getLieuStockage();
        return $this->makeXMLDSLigne($lieu_stockage, "REBECHE", $ds->getRebeches());
    }

    public function exportEntete() {
        $entete_string = "";
        foreach ($this->ds_liste as $cpt => $ds) {
            $entete_string.= $this->makeEnteteLigne($ds);
            if ($cpt < (count($this->ds_liste) - 1))
                $entete_string.= "\r\n";
        }
        return $entete_string;
    }

    public function exportLigne() {
        $ligne_string = "";
        foreach ($this->ds_liste as $cpt => $ds) {
            $ligne_string.= $this->makeLignes($ds);
        }
        return $ligne_string;
    }
{'fraction_non_alphanumeric': 0.1731884952475003, 'fraction_numerical': 0.005616590544377237, 'mean_word_length': 2.171462125660599, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 2, '>': 265, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 28}, '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': '366739', 'n_tokens_mistral': 5768, 'n_tokens_neox': 5514, 'n_words': 769}