<?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');
session_write_close();
ini_set('post_max_size','20M');
ini_set('max_execution_time', 1000);
ini_set('upload_max_filesize','2M');
 
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 AGENCIAS',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];
		if($_POST[r1]=='N'){
			$sq_c = mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"SELECT * FROM valeryus07 WHERE agdescrip='".$agencia."'"));
//			echo mysqli_error($GLOBALS['con']);
		$this->Cell(190,3,'MERCADO: '.$sq_c[n_reg].' | '.$sq_c['agdescrip'].                        '',0,1,'C');	
		}else if($_POST[r1]=='T'){
		$this->Cell(190,3,'MERCADO: TODOS ',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,' % ',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 * from valeryus07 $whereAgen order by agdescrip");
while ($reg=mysqli_fetch_array($sql_cb)){
//PERSONAS
	/*$sql="	select fnumadu,fnummenor,fmuncto,n_reg,fcodmeca 
			from valeryus19
			where ((ffecllegada <= '".$fecIni."' and ffecsalida > '".$fecIni."') or (ffecllegada < '".$fecFin."' and ffecsalida > '".$fecFin."') or (ffecllegada > '".$fecIni."' and ffecsalida < '".$fecFin."')) and cancelado='0' and fcodagen='".$reg[agdescrip]."' and ftipocto!='MAESTRA' AND status!='MISCELANEOS' AND extra1='0' and (fcodmeca='AGMAY' or fcodmeca='AGMIN' or fcodmeca='GRUPOS AGENCIA') order by fmuncto ";					
	$sql_dt=mysqli_query($GLOBALS['con'] ,$sql);
	echo mysqli_error($GLOBALS['con']);
//$personas=0;
	$pers=0;
	while($red=mysqli_fetch_array($sql_dt)){
        $pers +=$red[fnumadu]+$red[fnummenor];
		}
		
	 $personas=$pers;*/
	 
	$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,
			sum(fnumadu+fnummenor) AS personas,
			fmuncto,
			n_reg,
			fcodmeca 
		from 
			valeryus19
		where 
			(ffecllegada <= '".$fecIni2."' and ffecsalida > '".$fecIni2."')  and 
			cancelado='0' and 
			fcodagen='".$reg[agdescrip]."' and 
			ftipocto!='MAESTRA' AND 
			status!='MISCELANEOS' AND 
			extra1='0' and 
			(fcodmeca='AGMAY' or fcodmeca='AGMIN' or fcodmeca='GRUPOS AGENCIA' or fcodmeca='OTA') 
		group by cancelado
		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 
		valeryus030.agencia='".$reg[agdescrip]."' and 
		valeryus030.nparte1_det=1 and 
		(valeryus030.acc_fec between '".$fecIni."' and '".$fecFin."') and 
		(valeryus030.cancelado='0' or valeryus030.cancelado='2') and 
		(valeryus030.mercado='AGMAY' or valeryus030.mercado='AGMIN' or valeryus030.mercado='GRUPOS AGENCIA' or valeryus030.mercado='OTA') 
	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)){
		if(($redr[cancelado] == 0) || ($redr[cancelado]==2)){  
			$renta +=$redr[cargo];
		}
	}
	if (($cuartos=='0' or $cuartos=='') and ($personas=='0' or $personas=='') and ($renta=='0' or $renta=='')){
	
	}else{
		$pdf->SetFont('ARIAL','',7);
		$pdf->Cell(20,3,'',0,0,'C',0);
		$pdf->Cell(15,3,'      '.$reg[n_reg],0,0,'L',0);	
		$pdf->Cell(50,3,'  '.$reg[agdescrip],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,number_format($porcentaje,2,'.',',').' % ',0,0,'C',0);
		$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,number_format($Totporcentaje,2,'.',',').' %',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);
if(isset($_GET['show']))
$pdf->Output("Reporte_de_Agencias.pdf","I");
else
$pdf->Output("Reporte_de_Agencias.pdf","D");
?>
