<?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');
 $cuartos1=0;
 $cta=0;
 $numVen=0;
 $Totporcentaje=0;
 $Totpax=0;
 $Totrenta=0;
class PDF extends FPDF{
	//Cabecera de Página
	function Header(){
	    //Logotipo
	    include("DYIM_VER.php");
	    //Arial Bold (14)
	    $this->SetFont('Arial','B',12);
	    //Espacio
		
	    //Título		
	    $this->Cell(190,3,'TIPOS DE SEGMENTO ',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');
		if($_POST['r1']=='N'){
			$sq_c = mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"SELECT * FROM tipmerca WHERE tdescrip='".$_POST['mercado']."'"));
//			echo mysqli_error($GLOBALS['con']);
		$this->Cell(190,3,'MERCADO: '.$sq_c['n_reg'].' | '.$sq_c['tdescrip'].                        '',0,1,'C');	
		}else if($_POST['r1']=='T'){
		$this->Cell(190,3,'MERCADO: TODOS ',0,1,'C');	
		}	

		
	
	    //Salto de línea
	    $this->Ln(3);
	}
	//Pie de Página
	function Footer()
	{
	    //Posición: 1,5 cm del Final
	    $this->SetY(-15);
	    //Arial Italic 8
	    $this->SetFont('Arial','I',8);
	    //Número de página
	    $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='descripcion="'.$_POST['mercado'].'"';
  else if ($_POST['r1']=='T')
  $condicion='descripcion <> ""';
  else 
  $condicion='descripcion = "" ';
  
 	$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'); 
	
	$sql_hb=mysqli_query($GLOBALS['con'] ,"select * from valeryus08 where ntipo!='MAESTRA'");	
	$hbt=mysqli_num_rows($sql_hb);
	
  $sql_cb=mysqli_query($GLOBALS['con'] ,"select * from valeryus06 where $condicion order by descripcion");
  while ($reg=mysqli_fetch_array($sql_cb)){
     
	 $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 codseg='".$reg['descripcion']."' and ftipocto!='MAESTRA' AND status!='MISCELANEOS' AND extra1='0' order by fmuncto";					
	$sql_dt=mysqli_query($GLOBALS['con'] ,$sql);
	//echo mysqli_error($GLOBALS['con']);

	$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;
	for ($b=0; $b<= $dias; $b++){	       
	
	if ($b=='0'){
	$r=0;
		}else{
	$r=1;	
		}
	$fecIni2=addDay($fecIni2,$r); 	
		
		//CUARTO HISTORICO
	$sqlct="select count(n_reg) as nREG 
			from valeryus19
			where (ffecllegada <= '".$fecIni2."' and ffecsalida > '".$fecIni2."') and cancelado='0' and codseg='".$reg['descripcion']."' and ftipocto!='MAESTRA' AND status!='MISCELANEOS' AND extra1='0' order by fmuncto";					
	
	$sql_ct=mysqli_query($GLOBALS['con'] ,$sqlct);
	
	$redc=mysqli_fetch_array($sql_ct);
		
		$cuartos+=$redc['nREG'];
		$cuartos1+=$redc['nREG'];
	 
	//CUARTO HISTORICO
	/*$sqlct="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 fcodmeca='".$reg['tdescrip']."' and ftipocto!='MAESTRA' AND status!='MISCELANEOS' AND extra1='0' order by fmuncto";					
	$sql_ct=mysqli_query($GLOBALS['con'] ,$sqlct);
	$cuartos1=0;
	$pers=0;
	while($redc=mysqli_fetch_array($sql_ct)){
		$pers +=$redc['fnumadu']+$redc['fnummenor'];
		//$pers +=$redc['fnumadu'];
		$cuartos1 ++;
		//echo "bbb".$redc['n_reg']."-".$redc['fnumadu']."-".$redc['fnummenor']."-".$redc['fmuncto']."-".$redc['fcodmeca']."<br>";
		}
	 $cuartos=$cuartos1+$cuartos2;	
	 $personas=$pers+$pers2;*/



}




//DESCRIPCINO 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.segmento='".$reg['descripcion']."' and valeryus030.nparte1_det=1 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)){
		
		if((isset($redr['cancelado'])&&$redr['cancelado'] == 0) || (isset($redr['cancelado'])&&$redr['cancelado']==2)){  
		$renta +=$redr['cargo'];
		}
	}

 	$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['descripcion'],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,$cuartos1,0,0,'C',1);			
	$pdf->Cell(20,3,' $'.number_format($Totrenta,2,'.',','),0,1,'R',1);
	
	$pdf->Cell(20,2,'',0,0,'C',0);
	$pdf->Cell(155,2,'',0,1,'C',0);
	
	




  

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