<?php
		session_start();

		$id=isset($_REQUEST['id'])?$_REQUEST['id']:0;
		include ("coneccionfe.php");
		 $link = pg_connect("host=$dbhost port=$dbport user=$dbuser password=$dbpwd dbname=$dbname")or die(pg_last_error($link));
        
        $sql = "set search_path to 'SHFE'; SELECT a.id_febinario_tc, 'R' || '-' || b.rucemisor || '-' ||  a.tipodoc || '-' || a.serie || '-' || a.correlativo || '.xml' nombre,
		coalesce(filecdrxml,'-1') as filecdrxml FROM fa_febinario_tc a
		inner join fa_faele_tc b on a.tipodoc = b.tipodoc and a.serie = b.serie and a.correlativo = b.correlativo  
		 where id_febinario_tc=$id";
        $result=pg_query($link, $sql);
        while($row=pg_fetch_array($result)){
          $doc = $row;
        }
        pg_query($link, "begin");
       	# $file=pg_lo_open($link, $doc[6], "r");

        $file=pg_unescape_bytea($doc['filecdrxml']);

        header("Cache-control: private");
        header("Content-type: txt/xml");
        header("Content-Disposition: attachment; filename=\"$doc[1]\"");
        //header("Content-length: 9420");
        header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
        header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
        header("Cache-Control: no-cache, must-revalidate");
        header("Pragma: no-cache");
        
        print $file;
        pg_close($link);    


?>