function Generate()
 {
     global $conf, $langs, $mysoc;
     $result = 0;
     dol_syslog(get_class($this) . "::Generate build file " . $this->filename);
     $this->file = fopen($this->filename, "w");
     // TODO Move code for es and fr into an external module file with selection into setup of prelevement module
     $found = 0;
     // Build file for Spain
     if ($mysoc->country_code == 'ES') {
         if (!empty($conf->esaeb->enabled)) {
             $found++;
             dol_include_once('/esaeb/class/esaeb19.class.php');
             //Head
             $esaeb19 = new AEB19DocWritter();
             $esaeb19->configuraPresentador($this->numero_national_emetteur, $conf->global->ESAEB_SUFIX_PRESENTADOR, $this->raison_sociale, $this->emetteur_code_banque, $this->emetteur_code_guichet);
             $idOrdenante = $esaeb19->agregaOrdenante($this->numero_national_emetteur, $conf->global->ESAEB_SUFIX_ORDENANTE, $this->raison_sociale, $this->emetteur_code_banque, $this->emetteur_code_guichet, $this->emetteur_number_key, $this->emetteur_numero_compte);
             $this->total = 0;
             $sql = "SELECT pl.rowid, pl.fk_soc, pl.client_nom, pl.code_banque, pl.code_guichet, pl.cle_rib, pl.number, pl.amount,";
             $sql .= " f.facnumber, pf.fk_facture";
             $sql .= " FROM";
             $sql .= " " . MAIN_DB_PREFIX . "prelevement_lignes as pl,";
             $sql .= " " . MAIN_DB_PREFIX . "facture as f,";
             $sql .= " " . MAIN_DB_PREFIX . "prelevement_facture as pf";
             $sql .= " WHERE pl.fk_prelevement_bons = " . $this->id;
             $sql .= " AND pl.rowid = pf.fk_prelevement_lignes";
             $sql .= " AND pf.fk_facture = f.rowid";
             //Lines
             $i = 0;
             $resql = $this->db->query($sql);
             if ($resql) {
                 $num = $this->db->num_rows($resql);
                 $client = new Societe($this->db);
                 while ($i < $num) {
                     $obj = $this->db->fetch_object($resql);
                     $client->fetch($obj->fk_soc);
                     $esaeb19->agregaRecibo($idOrdenante, $client->idprof1, $obj->client_nom, $obj->code_banque, $obj->code_guichet, $obj->cle_rib, $obj->number, $obj->amount, "Fra." . $obj->facnumber . " " . $obj->amount);
                     $this->total = $this->total + $obj->amount;
                     $i++;
                 }
             } else {
                 $result = -2;
             }
             fputs($this->file, $esaeb19->generaRemesa());
         }
     }
     // Build file for European countries
     if (!$found && $mysoc->isInEEC()) {
         $found++;
         /**
          * SECTION CREATION FICHIER SEPA
          * SECTION CREATION FICHIER SEPA
          * SECTION CREATION FICHIER SEPA
          */
         // SEPA Initialisation
         $CrLf = "\n";
         $date_actu = dol_now();
         $dateTime_YMD = dol_print_date($date_actu, '%Y%m%d');
         $dateTime_YMDHMS = dol_print_date($date_actu, '%Y%m%d%H%M%S');
         $dateTime_ECMA = dol_print_date($date_actu, '%Y-%m-%dT%H:%M:%S');
         $fileDebiteurSection = '';
         $fileEmetteurSection = '';
         $i = 0;
         $this->total = 0;
         /*
          * section Debiteur (sepa Debiteurs bloc lines)
          */
         $sql = "SELECT soc.code_client as code, soc.address, soc.zip, soc.town, soc.datec, p.code as country_code,";
         $sql .= " pl.client_nom as nom, pl.code_banque as cb, pl.code_guichet as cg, pl.number as cc, pl.amount as somme,";
         $sql .= " f.facnumber as fac, pf.fk_facture as idfac, rib.iban_prefix as iban, rib.bic as bic, rib.rowid as drum";
         $sql .= " FROM";
         $sql .= " " . MAIN_DB_PREFIX . "prelevement_lignes as pl,";
         $sql .= " " . MAIN_DB_PREFIX . "facture as f,";
         $sql .= " " . MAIN_DB_PREFIX . "prelevement_facture as pf,";
         $sql .= " " . MAIN_DB_PREFIX . "societe as soc,";
         $sql .= " " . MAIN_DB_PREFIX . "c_pays as p,";
         $sql .= " " . MAIN_DB_PREFIX . "societe_rib as rib";
         $sql .= " WHERE pl.fk_prelevement_bons = " . $this->id;
         $sql .= " AND pl.rowid = pf.fk_prelevement_lignes";
         $sql .= " AND pf.fk_facture = f.rowid";
         $sql .= " AND soc.fk_pays = p.rowid";
         $sql .= " AND soc.rowid = f.fk_soc";
         $sql .= " AND rib.fk_soc = f.fk_soc";
         $sql .= " AND rib.default_rib = 1";
         //echo $sql;
         $resql = $this->db->query($sql);
         if ($resql) {
             $num = $this->db->num_rows($resql);
             while ($i < $num) {
                 $obj = $this->db->fetch_object($resql);
                 $fileDebiteurSection .= $this->EnregDestinataireSEPA($obj->code, $obj->nom, $obj->address, $obj->zip, $obj->town, $obj->country_code, $obj->cb, $obj->cg, $obj->cc, $obj->somme, $obj->facnumber, $obj->idfac, $obj->iban, $obj->bic, $obj->datec, $obj->drum);
                 $this->total = $this->total + $obj->somme;
                 $i++;
             }
         } else {
             fputs($this->file, 'ERREUR DEBITEUR ' . $sql . $CrLf);
             $result = -2;
         }
         /*
          * section Emetteur(sepa Emetteur bloc lines)
          */
         if ($result != -2) {
             $fileEmetteurSection .= $this->EnregEmetteurSEPA($conf, $date_actu, $i, $this->total, $CrLf);
         } else {
             fputs($this->file, 'ERREUR EMETTEUR' . $CrLf);
         }
         /**
          * SECTION CREATION FICHIER SEPA
          * SECTION CREATION FICHIER SEPA
          * SECTION CREATION FICHIER SEPA
          */
         // SEPA File Header
         fputs($this->file, '<' . '?xml version="1.0" encoding="UTF-8" standalone="yes"?' . '>' . $CrLf);
         fputs($this->file, '<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.008.001.02" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' . $CrLf);
         fputs($this->file, '	<CstmrDrctDbtInitn>' . $CrLf);
         // SEPA Group header
         fputs($this->file, '		<GrpHdr>' . $CrLf);
         fputs($this->file, '			<MsgId>' . ('PREL' . $dateTime_YMD . '/REF' . $this->id) . '</MsgId>' . $CrLf);
         fputs($this->file, '			<CreDtTm>' . $dateTime_ECMA . '</CreDtTm>' . $CrLf);
         fputs($this->file, '			<NbOfTxs>' . $i . '</NbOfTxs>' . $CrLf);
         fputs($this->file, '			<CtrlSum>' . $this->total . '</CtrlSum>' . $CrLf);
         fputs($this->file, '			<InitgPty>' . $CrLf);
         fputs($this->file, '				<Nm>' . $this->raison_sociale . '</Nm>' . $CrLf);
         /*			fputs($this->file, '				<Id>'.$CrLf);
         			fputs($this->file, '					<Othr>'.$CrLf);
         			fputs($this->file, '						<Id>0533883248</Id>'.$CrLf);
         			fputs($this->file, '						<Issr>KBO-BCE</Issr>'.$CrLf);
         			fputs($this->file, '					<Othr>'.$CrLf);
         			fputs($this->file, '				</Id>'.$CrLf);
         */
         fputs($this->file, '			</InitgPty>' . $CrLf);
         fputs($this->file, '		</GrpHdr>' . $CrLf);
         // SEPA File Emetteur
         if ($result != -2) {
             fputs($this->file, $fileEmetteurSection);
         }
         // SEPA File Debiteurs
         if ($result != -2) {
             fputs($this->file, $fileDebiteurSection);
         }
         // SEPA FILE FOOTER
         fputs($this->file, '		</PmtInf>' . $CrLf);
         fputs($this->file, '	</CstmrDrctDbtInitn>' . $CrLf);
         fputs($this->file, '</Document>' . $CrLf);
         /*$sql = "SELECT pl.amount";
         			$sql.= " FROM";
         			$sql.= " ".MAIN_DB_PREFIX."prelevement_lignes as pl,";
         			$sql.= " ".MAIN_DB_PREFIX."facture as f,";
         			$sql.= " ".MAIN_DB_PREFIX."prelevement_facture as pf";
         			$sql.= " WHERE pl.fk_prelevement_bons = ".$this->id;
         			$sql.= " AND pl.rowid = pf.fk_prelevement_lignes";
         			$sql.= " AND pf.fk_facture = f.rowid";
         
         			//Lines
         			$i = 0;
         			$resql=$this->db->query($sql);
         			if ($resql)
         			{
         				$num = $this->db->num_rows($resql);
         
         				while ($i < $num)
         				{
         					$obj = $this->db->fetch_object($resql);
         					$this->total = $this->total + $obj->amount;
         					$i++;
         				}
         			}
         			else
         			{
         				$result = -2;
         			}*/
     }
     // Build file for Other Countries with unknow format
     if (!$found) {
         $this->total = 0;
         $sql = "SELECT pl.amount";
         $sql .= " FROM";
         $sql .= " " . MAIN_DB_PREFIX . "prelevement_lignes as pl,";
         $sql .= " " . MAIN_DB_PREFIX . "facture as f,";
         $sql .= " " . MAIN_DB_PREFIX . "prelevement_facture as pf";
         $sql .= " WHERE pl.fk_prelevement_bons = " . $this->id;
         $sql .= " AND pl.rowid = pf.fk_prelevement_lignes";
         $sql .= " AND pf.fk_facture = f.rowid";
         //Lines
         $i = 0;
         $resql = $this->db->query($sql);
         if ($resql) {
             $num = $this->db->num_rows($resql);
             while ($i < $num) {
                 $obj = $this->db->fetch_object($resql);
                 $this->total = $this->total + $obj->amount;
                 $i++;
             }
         } else {
             $result = -2;
         }
         $langs->load('withdrawals');
         fputs($this->file, $langs->trans('WithdrawalFileNotCapable'));
     }
     fclose($this->file);
     if (!empty($conf->global->MAIN_UMASK)) {
         @chmod($this->file, octdec($conf->global->MAIN_UMASK));
     }
     return $result;
 }
    function Generate()
    {
    	global $conf,$langs,$mysoc;

        $result = 0;

        dol_syslog("BonPrelevement::Generate build file ".$this->filename);

        $this->file = fopen ($this->filename,"w");

        //Build file for Spain
        if ($mysoc->pays_code=='ES')
        {
        	if ($conf->esaeb->enabled)
        	{
	        	//Head
	        	$esaeb19 = new AEB19DocWritter;
	        	$esaeb19->configuraPresentador($this->numero_national_emetteur,$conf->global->ESAEB_SUFIX_PRESENTADOR,$this->raison_sociale,$this->emetteur_code_banque,$this->emetteur_code_guichet);
	        	$idOrdenante = $esaeb19->agregaOrdenante($this->numero_national_emetteur,$conf->global->ESAEB_SUFIX_ORDENANTE,$this->raison_sociale,$this->emetteur_code_banque,$this->emetteur_code_guichet, $this->emetteur_number_key, $this->emetteur_numero_compte);
	        	$this->total = 0;
	        	$sql = "SELECT pl.rowid, pl.client_nom, pl.code_banque, pl.code_guichet, pl.cle_rib, pl.number, pl.amount,";
	        	$sql.= " f.facnumber, pf.fk_facture";
	        	$sql.= " FROM";
	        	$sql.= " ".MAIN_DB_PREFIX."prelevement_lignes as pl,";
	        	$sql.= " ".MAIN_DB_PREFIX."facture as f,";
	        	$sql.= " ".MAIN_DB_PREFIX."prelevement_facture as pf";
	        	$sql.= " WHERE pl.fk_prelevement_bons = ".$this->id;
	        	$sql.= " AND pl.rowid = pf.fk_prelevement_lignes";
	        	$sql.= " AND pf.fk_facture = f.rowid";

	        	//Lines
	        	$i = 0;
	        	$resql=$this->db->query($sql);
	        	if ($resql)
	        	{
	            	$num = $this->db->num_rows($resql);

	            	while ($i < $num)
	            	{
	                	$obj = $this->db->fetch_object($resql);

	                	$esaeb19->agregaRecibo($idOrdenante,
	                	"idcliente".$i+1,
	                	$obj->client_nom,
	                	$obj->code_banque,
	                	$obj->code_guichet,
	                	$obj->cle_rib,
	                	$obj->number,
	                	$obj->amount,
	                	"Fra.".$obj->facnumber." ".$obj->amount);

	                	$this->total = $this->total + $obj->amount;

	                	$i++;
	            	}
	        	}

        		else
        		{
            		$result = -2;
        		}

        		fputs ($this->file, $esaeb19->generaRemesa());
        	}
    		else
        	{
        		$this->total = 0;
	        	$sql = "SELECT pl.amount";
	        	$sql.= " FROM";
	        	$sql.= " ".MAIN_DB_PREFIX."prelevement_lignes as pl,";
	        	$sql.= " ".MAIN_DB_PREFIX."facture as f,";
	        	$sql.= " ".MAIN_DB_PREFIX."prelevement_facture as pf";
	        	$sql.= " WHERE pl.fk_prelevement_bons = ".$this->id;
	        	$sql.= " AND pl.rowid = pf.fk_prelevement_lignes";
	        	$sql.= " AND pf.fk_facture = f.rowid";

	        	//Lines
	        	$i = 0;
	        	$resql=$this->db->query($sql);
	        	if ($resql)
	        	{
	            	$num = $this->db->num_rows($resql);

	            	while ($i < $num)
	            	{
	                	$obj = $this->db->fetch_object($resql);
	                	$this->total = $this->total + $obj->amount;
	                	$i++;
	            	}
	        	}
        		else
        		{
            		$result = -2;
        		}
        		$langs->load('withdrawals');
        		fputs ($this->file, $langs->trans('WithdrawalFileNotCapable'));
        	}

        }

        //Build file for France
        elseif ($mysoc->pays_code=='FR')
        {
        	/*
         	* En-tete Emetteur
         	*/
        	$this->EnregEmetteur();

        	/*
         	* Lines
         	*/
        	$this->total = 0;

        	$sql = "SELECT pl.rowid, pl.client_nom, pl.code_banque, pl.code_guichet, pl.number, pl.amount,";
        	$sql.= " f.facnumber, pf.fk_facture";
        	$sql.= " FROM";
        	$sql.= " ".MAIN_DB_PREFIX."prelevement_lignes as pl,";
        	$sql.= " ".MAIN_DB_PREFIX."facture as f,";
        	$sql.= " ".MAIN_DB_PREFIX."prelevement_facture as pf";
        	$sql.= " WHERE pl.fk_prelevement_bons = ".$this->id;
        	$sql.= " AND pl.rowid = pf.fk_prelevement_lignes";
        	$sql.= " AND pf.fk_facture = f.rowid";

        	$i = 0;

        	$resql=$this->db->query($sql);
        	if ($resql)
        	{
            	$num = $this->db->num_rows($resql);

            	while ($i < $num)
            	{
                	$row = $this->db->fetch_row($resql);

                	$this->EnregDestinataire($row[0],
                	$row[1],
                	$row[2],
                	$row[3],
                	$row[4],
                	$row[5],
                	$row[6],
                	$row[7]);

                	$this->total = $this->total + $row[5];

                	$i++;
            	}
        	}
        	else
        	{
            	$result = -2;
        	}

        	/*
        	* Pied de page total
        	*/

        	$this->EnregTotal($this->total);
        }

        //Build file for Other Countries with unknow format
    	else
        {
        	$this->total = 0;
	        $sql = "SELECT pl.amount";
	        $sql.= " FROM";
	        $sql.= " ".MAIN_DB_PREFIX."prelevement_lignes as pl,";
	        $sql.= " ".MAIN_DB_PREFIX."facture as f,";
	        $sql.= " ".MAIN_DB_PREFIX."prelevement_facture as pf";
	        $sql.= " WHERE pl.fk_prelevement_bons = ".$this->id;
	        $sql.= " AND pl.rowid = pf.fk_prelevement_lignes";
	        $sql.= " AND pf.fk_facture = f.rowid";

	        	//Lines
	        $i = 0;
	        $resql=$this->db->query($sql);
	        if ($resql)
	        {
	            $num = $this->db->num_rows($resql);

	            while ($i < $num)
	            {
	                $obj = $this->db->fetch_object($resql);
	                $this->total = $this->total + $obj->amount;
	                $i++;
	            }
	        }
        	else
        	{
            	$result = -2;
        	}
    		$langs->load('withdrawals');
        	fputs ($this->file, $langs->trans('WithdrawalFileNotCapable'));
        }

        fclose($this->file);
		if (! empty($conf->global->MAIN_UMASK))
		@chmod($this->file, octdec($conf->global->MAIN_UMASK));
		return $result;

    }