<?php
     $filename=$xlsf.'_fsoft.xls';
     
     $file_content='';
     $file_content.='<?php '."\n";
     $file_content.='header("Content-type: application/vnd.ms-excel");'."\n";
//      $file_content.='header("Content-type: application/vnd.ms-excel"); charset=iso-8859-1'."\n";
     $file_content.='header("Content-Disposition: attachment; filename='.$filename.'");'."\n";
     $file_content.='header("Pragma: no-cache");'."\n";
     $file_content.='header("Expires: 0");'."\n";
     
     $file_content.="\n".'?> ';
     $file_content.="\n";

     $file_content.="<style>"."\n";
     /*force text*/     
     $file_content.='.text{mso-number-format:"\@";}'."\n";
     $file_content.='.num {mso-number-format:General;}'."\n";
     $file_content.="</style>"."\n";

     
// Concepto	Código de Cuenta	Nombre de la Cuenta Contable	Debe	Haber	Centro de Costos	SubCentro de Costos
// CONSUMO MES MAYO 	5.01.01.01.004 	Fertilizantes			HCAMILA	CP101

$sql_fsoft='';
$sql_fsoft.='select ';
// $sql_fsoft.='date_format(fecha,"%Y%m") as Año_y_Mes,';
$sql_fsoft.='"DI" as TipoDeDiario,';
$sql_fsoft.='"" as NúmeroDeDiario,';
$sql_fsoft.='"" as Emisión,';
$sql_fsoft.='bode_descri as Referencia,';
$sql_fsoft.='"Consumo desde '.$fecha1.' hasta '.$fecha2.'" as Concepto,';
$sql_fsoft.='lsub_ctainve as CodigoDeCuenta,';
$sql_fsoft.='ctas_descri as NombreDeLaCuenta,';
$sql_fsoft.='0 as Debe,';
$sql_fsoft.='sum(cant*puni) as Haber,';
$sql_fsoft.='bode_descri as CentroDeCostos,';
$sql_fsoft.='bode_descri as SubCentroDeCostos,';

$sql_fsoft.='from inve1 ';
$sql_fsoft.='left join inve2 on idnum1=idnum2 ';
$sql_fsoft.='left join bode0 on bode=bode_codigo ';
$sql_fsoft.='left join item0 on item=item_codigo ';
$sql_fsoft.='left join lsub0 on item_lsub=lsub_codigo ';
$sql_fsoft.='left join ctas0 on lsub_ctainve=ctas_codigo ';

$sql_fsoft.='where ifnull(status,"") not in("O","X") ';
$sql_fsoft.='and cant*puni!=0 ';
// $sql_fsoft.='and abs(cant*puni)>'.$saldo0.' ';
$sql_fsoft.='and fecha>="'.$fecha1.'" ';
$sql_fsoft.='and fecha<="'.$fecha2.'" ';
$sql_fsoft.='and docu="EB19" ';
if(!empty($lsub))
{$sql_fsoft.='and item_lsub like"'.trim($lsub).'%" ';}

$sql_fsoft.='group by bode, item_lsub ';
$sql_fsoft.='order by bode, item_lsub ';

$res_fsoft=msql_query($sql_fsoft,$link_data);

     
$domg = new DOMDocument();
     $domg->encoding = 'utf-8';
     $domg->xmlVersion = '1.0';
     $domg->formatOutput = true;

     
     $style='';
     $style.='.text{mso-number-format:"\@";}';
     $style.='.num {mso-number-format:General;}';

     $element=$domg->appendChild($domg->createElement("style",htmlspecialchars($style)));     


     $table=$domg->appendChild($domg->createElement("table",""));  
          $xstyle='';
          $xstyle.='font-family:arial;';
          $xstyle.='font-size:12px;';
          $xstyle.='white-space:nowrap;';
          $xstyle.='border-collapse:collapse;';
          $table->setAttribute("style",$xstyle) ;  
          $table->setAttribute("width","300%") ;  

     $trow=$table->appendChild($domg->createElement("tr",""));
          $trow->setAttribute("style",'background:#d3d3d3;') ;  
     for($xcol=0; $xcol<msql_num_fields($res_fsoft); $xcol++)
     {
          $field_name=msql_field_name($res_fsoft,$xcol);
          $field_name=str_replace('_',' ',$field_name);
          $tcol=$trow->appendChild($domg->createElement('th',$field_name));
     }          

     while($aso_fsoft=msql_fetch_assoc($res_fsoft))
     {
          $trow=$table->appendChild($domg->createElement("tr",""));
               if($aso_fsoft['Valor']<0)
               {$trow->setAttribute("style",'background:#d3d3d3;');}

          for($xcol=0; $xcol<msql_num_fields($res_fsoft); $xcol++)
          {
               $value=$aso_fsoft[msql_field_name($res_fsoft,$xcol)];
               $tcol=$trow->appendChild($domg->createElement('td',$value));
          }          
          
     }
     
     $html2=$domg->saveHTML();
     $file_content.=$html2;
     file_put_contents($file_php_xls_fsoft,$file_content); 

?>