<?php
include('pdf/fpdf.php');
session_cache_limiter('private');
session_start();
$com = "I";
include("DMMYSQLG.php");
include("DSA3000.php");
include("DMMYSQLG.php");
include('DF80000.php');
 
class PDF extends FPDF{
	//Cabecera de Pgina
	function Header(){
	    //Logotipo
	    include("DYIM_VER.php");
	    //Arial Bold (14)
	    $this->SetFont('Arial','B',12);
	    //Espacio
		
	    //Ttulo		
	    $this->Cell(190,3,'REPORTE DE EMPRESAS CON CONVENIO',0,1,'C');
		$this->SetFont('Arial','B',8);
		$this->Cell(190,3,'',0,1,'L');		
		$this->Cell(190,3,'PERIODO DEL '.$_POST[fec].' AL '.$_POST[fec2].'',0,1,'C');
		$agencia=$_POST[fcodagen];
		
		$this->Cell(190,3,'MERCADO: CONVENIOS ',0,1,'C');	
			

		
	
	    //Salto de lnea
	    $this->Ln(3);
	}
	//Pie de Pgina
	function Footer()
	{
	    //Posicin: 1,5 cm del Final
	    $this->SetY(-15);
	    //Arial Italic 8
	    $this->SetFont('Arial','I',8);
	    //Nmero de pgina
	    $this->Cell(0,0, $this->PageNo().'/{nb}',0,0,'C');
	}
}
$pdf;
$pdf=new PDF('P','mm','Letter');
$pdf->AddPage();
$pdf->AliasNbPages();
$binit=-1;


$pdf->SetFillColor(240,240,240);
	$pdf->SetFont('ARIAL','B',8);
	$pdf->SetFillColor(230,230,230);
	$pdf->Cell(20,3,'',0,0,'C',0);
	$pdf->Cell(15,3,'CODIGO',0,0,'C',1);	
	$pdf->Cell(50,3,'DESCRIPCION',0,0,'C',1);
	$pdf->Cell(10,3,'',0,0,'C',1);
	
	$pdf->Cell(20,3,'PAX',0,0,'C',1);	
	$pdf->Cell(20,3,'CUARTOS',0,0,'C',1);
	$pdf->Cell(20,3,'INGRESOS',0,1,'C',1);

  //if ($_POST[r1]=='N')
  //$condicion='tdescrip="'.$_POST[mercado].'"';
  //else if ($_POST[r1]=='T')
  //$condicion='tdescrip <> ""';
  //else 
  //$condicion='tdescrip = "" ';
  
$fecIni=chfecha($_POST['fec'],'d/m/Y','Y-m-d');
$fecFin=chfecha($_POST['fec2'],'d/m/Y','Y-m-d');
$dias = daydiff ($fecFin,$fecIni,'Y-m-d'); 
$agencia=$_POST[fcodagen];
$whereAgen="";
if($agencia!='') $whereAgen=" WHERE agdescrip='".$agencia."'";

//--Obtiene Numero Habitaciones	
$sql_hb=mysqli_query($GLOBALS['con'] ,"select * from valeryus08 where ntipo!='MAESTRA'");	
$hbt=mysqli_num_rows($sql_hb);

//--Obtiene Listado Agencias
$sql_cb = mysqli_query($GLOBALS['con'] ,"select distinct(fempresa) as emp from valeryus19 where 
((ffecllegada <= '$fecIni' and ffecsalida > '$fecIni')		
  or (ffecllegada <= '$fecFin'  and ffecsalida > '$fecFin' ) 
or (ffecllegada >= '$fecIni' and ffecsalida <= '$fecFin' ))
 and cancelado='0' and 
 ftipocto!='MAESTRA' AND fcodmeca='CONVENIOS' 
AND status!='MISCELANEOS' AND extra1='0' group by fempresa order by fmuncto ");
while ($reg=mysqli_fetch_array($sql_cb)){

	$fecIni2=chfecha($_POST['fec'],'d/m/Y','Y-m-d');
	$cuartos=0;
	$personas=0;
	for ($b=0; $b<= $dias; $b++){//Recorre Dias   
		$r=$b==0?0:1;
		
		//-->Agregar Un Da por cada bucle
		$fecIni2=$b>0?addDay($fecIni2,$r):$fecIni2; 
	
		   $sql="
		select 
			count(*) as nReg,
                        fempresa,
			sum(fnumadu+fnummenor) AS personas,
			fmuncto,
			n_reg,
			fcodmeca,fempresa
		from 
			valeryus19
		where 
			(ffecllegada <= '".$fecIni2."' and ffecsalida > '".$fecIni2."')  and 
			cancelado='0' and 
			(fempresa='".$reg['emp']."' or fempresa='".$reg['emp']."') and 
			ftipocto!='MAESTRA' AND 
                        fcodmeca='CONVENIOS' AND
			status!='MISCELANEOS' AND 
			extra1='0' 
		group by fempresa
		order by fmuncto ";			
		$sql_dt=mysqli_query($GLOBALS['con'] ,$sql);
	
		$red=mysqli_fetch_array($sql_dt);
		$personas +=$red['personas'];
		$cuartos +=$red['nReg'];
	 
	}//Termina Recorre Dias
	
	//DESCRIPCION DE RENTA POR TIPO DE MARCADO
	$sqlrt="
	select 
		SUM(valeryus030.costo) as cargo,
		valeryus030.total as abono,
		valeryus030.nparte1  
	from 
		valeryus030
		left join valeryus19 on (valeryus19.n_reg=valeryus030.nparte1)
	where 
		valeryus19.fempresa='".$reg['emp']."' and 
		valeryus030.nparte1_det=1 and 
                mercado='CONVENIOS' and
		(valeryus030.acc_fec between '".$fecIni."' and '".$fecFin."') and 
		(valeryus030.cancelado='0' or valeryus030.cancelado='2') 
	group by valeryus030.nparte1";	
	$sql_rt=mysqli_query($GLOBALS['con'] ,$sqlrt);
	echo mysqli_error($GLOBALS['con']);
	$renta=0;
	while($redr=mysqli_fetch_array($sql_rt)){
			$renta +=$redr[cargo];
		
	}
	if (($cuartos=='0' or $cuartos=='') and ($personas=='0' or $personas=='') and ($renta=='0' or $renta=='')){
	
	}else{
                  $sqlemp = mysqli_query($GLOBALS['con'] ,"select * from fcompas where (n_reg='". $reg['emp'] . "' or pdesco='". $reg['emp'] . "')");
$empresa = mysqli_fetch_array($sqlemp);
		$pdf->SetFont('ARIAL','',7);
		$pdf->Cell(20,3,'',0,0,'C',0);
		$pdf->Cell(15,3,'      '.$empresa['n_reg'],0,0,'L',0);	
		$pdf->Cell(50,3,'  '.$empresa['pdesco'],0,0,'L',0);
		//$pdf->Cell(50,3,'  '.$reg[ttipodaily],0,0,'L',0);
		$pdf->Cell(10,3,' ',0,0,'C',0);
		//porcentaje
		if ($dias==0)
			$cthb=($hbt*1);
		else 
			$cthb=($hbt*$dias);
		$porcentaje=($cuartos*100)/$cthb;
		
		$pdf->Cell(20,3,$personas,0,0,'C',0);
		$pdf->Cell(20,3,$cuartos,0,0,'C',0);
		$pdf->Cell(20,3,' $'.number_format($renta,2,'.',','),0,1,'R',0);
		
		$numVen 	+=$cta;
		$Totporcentaje +=$porcentaje;
		$Totpax 	+=$personas;
		$Totcuarto 	+=$cuartos;
		$Totrenta 	+=$renta;		
	}
}
  $pdf->SetFont('ARIAL','B',8);
	$pdf->SetFillColor(230,230,230);
	$pdf->Cell(20,3,'',0,0,'C',0);
	$pdf->Cell(15,3,'',0,0,'C',1);	
	$pdf->Cell(50,3,'TOTAL ',0,0,'R',1);
	$pdf->Cell(10,3,'',0,0,'C',1);
		
	$pdf->Cell(20,3,$Totpax,0,0,'C',1);		
	$pdf->Cell(20,3,$Totcuarto,0,0,'C',1);			
	$pdf->Cell(20,3,' $'.number_format($Totrenta,2,'.',','),0,1,'R',1);

$pdf->Output("Reporte_de_Empresas.pdf","D");
?>
