<?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');
$totSnIva=0;
$totSinIva=0;
$totPvIva=0;
$totIva=0;
$totIsh=0;
$totPrp=0;
$edfdfs=0;
$tip=0;
$ser=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,'REPORTE DPTAL ',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']!='T'){
			$sq_c = mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"SELECT valeryus09.descripcion,valeryus09.n_reg,valeryus09.cod_var,valeryus09.pmontomax FROM valeryus09 WHERE n_reg='".$_POST['id_ope']."'"));
		$this->Cell(190,3,'CARGO/ABONO: '.$sq_c['n_reg'].' | '.$sq_c['descripcion'].                        '',0,1,'C');	
		}else if($_POST['r1']=='T'){
		$this->Cell(190,3,'CARGO/ABONO: 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(30,3,'',0,0,'C',0);
	$pdf->Cell(10,3,'CODIGO',0,0,'C',1);	
	$pdf->Cell(40,3,'DESCRIPCION',0,0,'C',1);
	$pdf->Cell(10,3,'',0,0,'C',1);
	$pdf->Cell(19,3,'SUBTOTAL',0,0,'R',1);
	$pdf->Cell(19,3,'I.V.A.',0,0,'R',1);
	$pdf->Cell(18,3,'I.S.H.',0,0,'R',1);
	$pdf->Cell(19,3,'CARGOS',0,1,'R',1);


  if ($_POST['r1']=='N')
  $condicion='n_reg="'.$_POST['id_ope'].'"';
  else if ($_POST['r1']=='T')
  $condicion='n_reg > 0';
  else 
  $condicion='n_reg = 0';
  
 	$fecIni=chfecha($_POST['fec'],'d/m/Y','Y-m-d');
	$fecFin=chfecha($_POST['fec2'],'d/m/Y','Y-m-d');
	
  $sql_cb=mysqli_query($GLOBALS['con'] ,"select * from valeryus09 where $condicion and pmontomax= 'CARGO' and tip='0' order by descripcion ");
 
 while ($reg=mysqli_fetch_array($sql_cb)){
  
  $ptventa=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"select * from ptventa where codigo='".$reg['n_reg']."'"));
      
	      if ($reg['restringido']=='SI'){
		    
			    $pdf->SetFont('ARIAL','',7);
	            $pdf->Cell(30,3,'',0,0,'C',0);
	            $pdf->Cell(10,3,'      '.$reg['n_reg'],0,0,'L',0);	
	            $pdf->Cell(40,3,'  '.$reg['descripcion'],0,0,'L',0);
	            $pdf->Cell(10,3,' ',0,0,'C',0);	
					
			@$rest = mysqli_query($GLOBALS['con'] ,"
		SELECT 
			dgpemema.fol_ven
			
		FROM 
			dgpemema
			
			LEFT JOIN ctmesas ON ctmesas.nparte1=dgpemema.fpos_mesa
			LEFT JOIN ctarea ON ctarea.nparte1=ctmesas.area
			LEFT JOIN ctmeser  ON ctmeser.nparte1=dgpemema.cod_ven
			LEFT JOIN ptventa ON ptventa.nparte1=ctmesas.descripcion
			
			
		where dgpemema.fec_ven between '".$fecIni."' and '".$fecFin."' and ptventa.nparte1='".$ptventa['nparte1']."'
		group by dgpemema.fol_ven
		ORDER BY 
		
			dgpemema.con_ven");
		$comida=0;
		while($rest1 = mysqli_fetch_array($rest)){
		@$sqal = mysqli_query($GLOBALS['con'] ,"SELECT sum((can_ref*pre_bas)-imp_des) as totAli from dgpememd inner join dginventp on dginventp.nparte1=dgpememd.nparte1 left join dgpemema on dgpememd.cons_ven=dgpemema.fol_ven where dgpememd.cons_ven='".$rest1['fol_ven']."' and dgpememd.cancela=0 and dgpemema.cancela='0' and dgpemema.cancelado='0' group by dgpememd.nparte1");
		
		while ($com=mysqli_fetch_array($sqal)){
		
		$comida += $com['totAli'];
		
		}
		}
		$comSnIva=$comida/1.16;
	    $comSlIva=$comSnIva*.16;
	    $ventas=$comSnIva+$comSlIva;
		
	/*	if ($reg['n_reg']=='37'){ //total de propinas
	
	$tip=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"select sum(tip) as propina from dgpemema where fec_ven between '".$fecIni."' and '".$fecFin."' and (cancelado='0' and cancela='0')"));
	
	    $pdf->Cell(19,3,' $'.number_format($tip['propina'],2,'.',','),0,0,'R',0);
		$pdf->Cell(19,3,' $'.number_format(0,2,'.',','),0,0,'R',0);
		$pdf->Cell(18,3,' $'.number_format(0,2,'.',','),0,0,'R',0);
	    $pdf->Cell(19,3,' $'.number_format($tip['propina'],2,'.',','),0,1,'R',0);
	
	}elseif ($reg['n_reg']=='109'){
	
	$ser=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"select sum(servicio) as servicio from dgpemema where fec_ven between '".$fecIni."' and '".$fecFin."' and (cancelado='0' and cancela='0')"));
	
	    $pdf->Cell(19,3,' $'.number_format($ser['servicio'],2,'.',','),0,0,'R',0);
		$pdf->Cell(19,3,' $'.number_format(0,2,'.',','),0,0,'R',0);
		$pdf->Cell(18,3,' $'.number_format(0,2,'.',','),0,0,'R',0);
	    $pdf->Cell(19,3,' $'.number_format($ser['servicio'],2,'.',','),0,1,'R',0);
	
	}else{*/
		
		$pdf->Cell(19,3,' $'.number_format($comSnIva,2,'.',','),0,0,'R',0);
		$pdf->Cell(19,3,' $'.number_format($comSlIva,2,'.',','),0,0,'R',0);
		$pdf->Cell(18,3,' $'.number_format(0,2,'.',','),0,0,'R',0);
	    $pdf->Cell(19,3,' $'.number_format($ventas,2,'.',','),0,1,'R',0);
		    $totSinIva+= $comSnIva;
		    $totPvIva+= $comSlIva;
		
		//}
		}else{
		
		$pdf->SetFont('ARIAL','',7);
	$pdf->Cell(30,3,'',0,0,'C',0);
	$pdf->Cell(10,3,'      '.$reg['n_reg'],0,0,'L',0);	
	$pdf->Cell(40,3,'  '.$reg['descripcion'],0,0,'L',0);
	$pdf->Cell(10,3,' ',0,0,'C',0);

	$sql="select valeryus030.*,valeryus19.n_reg as folh from valeryus19 left join valeryus030 on (valeryus030.nparte1=valeryus19.n_reg) where valeryus030.nparte1_det='".$reg['n_reg']."' and (valeryus030.acc_fec between '".$fecIni."' and '".$fecFin."')";					
	$sql_dt=mysqli_query($GLOBALS['con'] ,$sql);
	$cta1=0;
	$cargo1=0;
	$abono1=0;
	$sin_iva=0;
	$desIva=0;
	$desIsh=0;
	$cta=0;
	$numVen=0;
	$TotCargo2=0;
	while($red=mysqli_fetch_array($sql_dt)){
	$cta ++; 
		if(($red['cancelado'] == 0) || ($red['cancelado']==2)){  
		$cargo1 +=$red['costo'];
		$abono1 +=$red['total'];
		$detDet=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"select * from valeryus09 where n_reg='".$reg['n_reg']."'"));
		
		if ($detDet['gra']=='SI' and $detDet['sobre']=='SI'){
		   $iva=($detDet['gravable']/100);
		   $ish=($detDet['impuesto']/100);
		   $impuesto=($iva+$ish)+1;   
			  $sin_iva=$cargo1/$impuesto;
				 $desIva=$sin_iva*$iva;
				 $desIsh=$sin_iva*$ish;
		
		  }elseif($detDet['gra']=='SI' and $detDet['sobre']!='SI'){
		     $iva=($detDet['gravable']/100);
		     $impuesto=$iva+1;
		        $sin_iva=$cargo1/$impuesto;
		        $desIva=$cargo1-$sin_iva;
		
		    }elseif($detDet['no_gra']=='SI'){
		       $sin_iva=$cargo1;
			  
			  }elseif($detDet['otro']=='SI'){
		         $sin_iva=$cargo1;
			}
		}
	}
		$pdf->Cell(19,3,' $'.number_format($sin_iva,2,'.',','),0,0,'R',0);
		$pdf->Cell(19,3,' $'.number_format($desIva,2,'.',','),0,0,'R',0);
		$pdf->Cell(18,3,' $'.number_format($desIsh,2,'.',','),0,0,'R',0);
	    $pdf->Cell(19,3,' $'.number_format($cargo1,2,'.',','),0,1,'R',0);

		$numVen +=$cta;
		$totSnIva +=$sin_iva;
		$totIva +=$desIva;
		$totIsh +=$desIsh;
		$TotCargo2 +=$cargo1;
  }
  }
        $ivaTotal=$totSinIva+$totSnIva+$tip['pfropina']+$ser['servicio'];
		$totSoloIva=$totIva+$totPvIva;
		$totCargos=$ivaTotal+$totSoloIva+$totIsh;
  
    $pdf->SetFont('ARIAL','B',8);
	$pdf->SetFillColor(230,230,230);
	$pdf->Cell(35,3,'',0,0,'C',0);
	$pdf->Cell(55,3,'TOTAL DE CARGO',0,0,'R',1);	
	$pdf->Cell(19,3,' $'.number_format($ivaTotal,2,'.',','),0,0,'R',1);
	$pdf->Cell(19,3,' $'.number_format($totSoloIva,2,'.',','),0,0,'R',1);
	$pdf->Cell(18,3,' $'.number_format($totIsh,2,'.',','),0,0,'R',1);
	$pdf->Cell(19,3,' $'.number_format($totCargos,2,'.',','),0,1,'R',1);
	
	
	
	
	
	
	
	
	
	////++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	$sqlasd=mysqli_query($GLOBALS['con'] ,"select * from valeryus09 where $condicion and pmontomax='CARGO' and tip='1' order by descripcion ");
  while ($rog=mysqli_fetch_array($sqlasd)){
  
  $pdf->SetFont('ARIAL','',7);
	            $pdf->Cell(30,3,'',0,0,'C',0);
	            $pdf->Cell(10,3,'      '.$rog['n_reg'],0,0,'L',0);	
	            $pdf->Cell(40,3,'  '.$rog['descripcion'],0,0,'L',0);
	            $pdf->Cell(10,3,' ',0,0,'C',0);
  
if ($rog['n_reg']=='37'){ //total de propinas
	
	$tip=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"select sum(tip) as propina from dgpemema where fec_ven between '".$fecIni."' and '".$fecFin."' and (cancelado='0' and cancela='0')"));
	
	    $pdf->Cell(19,3,' $'.number_format($tip['propina'],2,'.',','),0,0,'R',0);
		$pdf->Cell(19,3,' $'.number_format(0,2,'.',','),0,0,'R',0);
		$pdf->Cell(18,3,' $'.number_format(0,2,'.',','),0,0,'R',0);
	    $pdf->Cell(19,3,' $'.number_format($tip['propina'],2,'.',','),0,1,'R',0);
	
	}elseif ($rog['n_reg']=='109'){
	
	$ser=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"select sum(servicio) as servicio from dgpemema where fec_ven between '".$fecIni."' and '".$fecFin."' and (cancelado='0' and cancela='0')"));
	
	    $pdf->Cell(19,3,' $'.number_format($ser['servicio'],2,'.',','),0,0,'R',0);
		$pdf->Cell(19,3,' $'.number_format(0,2,'.',','),0,0,'R',0);
		$pdf->Cell(18,3,' $'.number_format(0,2,'.',','),0,0,'R',0);
	    $pdf->Cell(19,3,' $'.number_format($ser['servicio'],2,'.',','),0,1,'R',0);
	
	}else{
	
	$sqlsd="select valeryus030.*,valeryus19.n_reg as folh from valeryus19 left join valeryus030 on (valeryus030.nparte1=valeryus19.n_reg) where valeryus030.nparte1_det='".$rog['n_reg']."' and (valeryus030.acc_fec between '".$fecIni."' and '".$fecFin."')";					
	$sqldft=mysqli_query($GLOBALS['con'] ,$sqlsd);
	$cta1=0;
	$cargo2=0;
	$abono1=0;
	$sin_iva=0;
	$desIva=0;
	$desIsh=0;
	while($rod=mysqli_fetch_array($sqldft)){
	$cta ++; 
		if(($rod['cancelado'] == 0) || ($rod['cancelado']==2)){  
		$cargo2 +=$rod['costo'];
		$abono1 +=$rod['total'];
		//$detDet=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"select * from valeryus09 where n_reg='".$reg['n_reg']."'"));
		
		
		}
	}
		
		$pdf->Cell(19,3,' $'.number_format($cargo2,2,'.',','),0,0,'R',0);
		$pdf->Cell(19,3,' $'.number_format(0,2,'.',','),0,0,'R',0);
		$pdf->Cell(18,3,' $'.number_format(0,2,'.',','),0,0,'R',0);
	    $pdf->Cell(19,3,' $'.number_format($cargo2,2,'.',','),0,1,'R',0);
		    //$totSinIva+= $comSnIva;
		    $totPrp+= $cargo2;
		
		}


  }	
		$ksjdf=$ivaTotal+$totPrp+$tip['propina']+$ser['servicio'];
		$gdfgd=$totCargos+$totPrp+$ser['servicio']+$tip['propina'];
		
		$pdf->SetFont('ARIAL','B',8);
	$pdf->SetFillColor(230,230,230);
	$pdf->Cell(35,3,'',0,0,'C',0);
	$pdf->Cell(55,3,'TOTALES',0,0,'R',1);	
	$pdf->Cell(19,3,' $'.number_format($ksjdf,2,'.',','),0,0,'R',1);
	$pdf->Cell(19,3,' $'.number_format($totSoloIva,2,'.',','),0,0,'R',1);
	$pdf->Cell(18,3,' $'.number_format($totIsh,2,'.',','),0,0,'R',1);
	$pdf->Cell(19,3,' $'.number_format($gdfgd,2,'.',','),0,1,'R',1);















		
$pdf->Cell(35,3,'',0,1,'C',0);
//ABONOS		
  $sql_cba=mysqli_query($GLOBALS['con'] ,"select * from valeryus09 where $condicion and pmontomax='ABONO' order by descripcion ");
  while ($rega=mysqli_fetch_array($sql_cba)){
 	$pdf->SetFont('ARIAL','',7);
	$pdf->Cell(35,3,'',0,0,'C',0);
	$pdf->Cell(10,3,'      '.$rega['n_reg'],0,0,'L',0);	
	$pdf->Cell(50,3,'  '.$rega['descripcion'],0,0,'L',0);
	$pdf->Cell(10,3,' ',0,0,'C',0);
	$sql="select valeryus030.*,valeryus19.n_reg as folh from valeryus19 left join valeryus030 on (valeryus030.nparte1=valeryus19.n_reg) where valeryus030.nparte1_det='".$rega['n_reg']."' and (valeryus030.acc_fec between '".$fecIni."' and '".$fecFin."') and depositos='0' ";						
	$sql_dt=mysqli_query($GLOBALS['con'] ,$sql);
	$cta=0;
	$cargo=0;
	$abono=0;
//	$TotAbono=0;
//	$fvisa=0;
//	$visa=0;
//	$fefectivo=0;
//	$famerican=0;
//	$fcuentas=0;
//	$american=0;
//	$cuentas=0;
//	$efectivo=0;
	while($red=mysqli_fetch_array($sql_dt)){
	$cta ++; 
		if(($red['cancelado'] == 0) || ($red['cancelado']==2)){  
		$cargo +=$red['costo'];
		$abono +=$red['total'];
		$efectivo=0;
		}
		
	}
	    if($rega['n_reg']=='6'){
	$fpago=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"select sum(dgger_cajerosd.importe) as paGos from dgpemema left join dgger_cajerosj on dgpemema.fol_ven=dgger_cajerosj.pedido left join dgger_cajerosd on dgger_cajerosj.n_rec=dgger_cajerosd.n_rec where dgpemema.fec_ven between '".$fecIni."' and '".$fecFin."' and (dgpemema.cancelado='0' and dgpemema.cancela='0') and dgger_cajerosd.tip_pag='6'"));

		    $pdf->Cell(60,3,' $'.number_format($efectivo=$abono+$fpago['paGos'],2,'.',','),0,1,'R',0);
			$fefectivo=$fpago['paGos'];
			
	}elseif($rega['n_reg']=='36'){
	$fpago=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"select sum(dgger_cajerosd.importe) as paGos from dgpemema left join dgger_cajerosj on dgpemema.fol_ven=dgger_cajerosj.pedido left join dgger_cajerosd on dgger_cajerosj.n_rec=dgger_cajerosd.n_rec where dgpemema.fec_ven between '".$fecIni."' and '".$fecFin."' and (dgpemema.cancelado='0' and dgpemema.cancela='0') and dgger_cajerosd.tip_pag='7'"));

		    $pdf->Cell(60,3,' $'.number_format($visa=$abono+$fpago['paGos'],2,'.',','),0,1,'R',0);
		    $fvisa=$fpago['paGos'];
			
	}elseif($rega['n_reg']=='17'){
	$fpago=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"select sum(dgger_cajerosd.importe) as paGos from dgpemema left join dgger_cajerosj on dgpemema.fol_ven=dgger_cajerosj.pedido left join dgger_cajerosd on dgger_cajerosj.n_rec=dgger_cajerosd.n_rec where dgpemema.fec_ven between '$fecIni' and '$fecFin' and (dgpemema.cancelado='0' and dgpemema.cancela='0') and dgger_cajerosd.tip_pag='18'"));

		    $pdf->Cell(60,3,' $'.number_format($master=$abono+$fpago['paGos'],2,'.',','),0,1,'R',0);
			$fmaster=$fpago['paGos'];
			
	}elseif($rega['n_reg']=='29'){
	$fpago=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"select sum(dgger_cajerosd.importe) as paGos from dgpemema left join dgger_cajerosj on dgpemema.fol_ven=dgger_cajerosj.pedido left join dgger_cajerosd on dgger_cajerosj.n_rec=dgger_cajerosd.n_rec where dgpemema.fec_ven between '$fecIni' and '$fecFin' and (dgpemema.cancelado='0' and dgpemema.cancela='0') and dgger_cajerosd.tip_pag='9'"));
	
			$pdf->Cell(60,3,' $'.number_format($american=$abono+$fpago['paGos'],2,'.',','),0,1,'R',0);
			$famerican=$fpago['paGos'];
	
	
	}elseif($rega['n_reg']=='19'){
	$fpago=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"select sum(dgger_cajerosd.importe) as paGos from dgpemema left join dgger_cajerosj on dgpemema.fol_ven=dgger_cajerosj.pedido left join dgger_cajerosd on dgger_cajerosj.n_rec=dgger_cajerosd.n_rec where dgpemema.fec_ven between '".$fecIni."' and '".$fecFin."' and (dgpemema.cancelado='0' and dgpemema.cancela='0') and dgger_cajerosd.tip_pag='10'"));
	
			$pdf->Cell(60,3,' $'.number_format($cuentas=$abono+$fpago['paGos'],2,'.',','),0,1,'R',0);
			$fcuentas=$fpago['paGos'];
	
	
	}else{
		
	    $pdf->Cell(60,3,' $'.number_format($abono,2,'.',','),0,1,'R',0);
}
		$numVen +=$cta;
		$TotAbono +=$abono;
		$fiud=$efectivo+$visa+$master+$american+$cuentas;
		$totPagos= $TotAbono+$efectivo+$visa+$master+$american+$cuentas;
		$ccxc=$fefectivo+$fvisa+$fmaster+$famerican+$fcuentas;
		$edfdfs=$TotAbono+$ccxc;

  }
   $pdf->SetFont('ARIAL','B',8);
	$pdf->SetFillColor(230,230,230);
	$pdf->Cell(35,3,'',0,0,'C',0);
	$pdf->Cell(10,3,'',0,0,'C',1);	
	$pdf->Cell(50,3,'TOTAL DE ABONO',0,0,'R',1);
	$pdf->Cell(10,3,'',0,0,'C',1);
	$pdf->Cell(30,3,' ',0,0,'R',1);
	$pdf->Cell(30,3,' $'.number_format($edfdfs,2,'.',','),0,1,'R',1);

	$pdf->Cell(35,3,'',0,1,'C',0);
	$pdf->Cell(35,3,'',0,0,'C',0);
	$pdf->Cell(10,3,'',0,0,'C',1);	
	$pdf->Cell(50,3,'SALDO DE HUESPEDES',0,0,'R',1);
	$pdf->Cell(10,3,'',0,0,'C',1);
	$saldo=$gdfgd-$edfdfs;		
		
	$pdf->Cell(30,3,' $'.number_format($saldo,2,'.',','),0,0,'R',1);
	$pdf->Cell(30,3,' ',0,1,'R',1);
ob_end_clean();	
$pdf->Output("Resumen_Departamental.pdf","D");
?>


