<?php

include('pdf/fpdf.php');

session_start();
$com = "I";
include("DMMYSQLG.php");
include("DSA3000.php");
include("DMMYSQLG.php");
include('DF80000.php');
 
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
		if($_POST['setipven']!='T'){
			$dt_tipven = mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"select * from dgtipven where tip_ven='".$_POST['setipven']."'"));
			$tipoventa=$dt_tipven["des_tipven"];
		}else {
			$tipoventa="TODAS";
		}	
	if($_POST['setipvent']!='T'){
			$dt_tipven = mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"select * from ptventa where nparte1='".$_POST['setipvent']."'"));
			$pventa=$dt_tipven["np_desc1"];
		}else {
			$pventa="TODOS";
		}	
		
		if($_POST['secodven']!='T'){
			$dt_caj=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"select CONCAT(dgcatven.nom_ven,' ',dgcatven.ap_pat_ven,' ',dgcatven.ap_mat_ven) as nomVen from dgcatven where cod_ven='".$_POST['secodven']."'"));
			$tipcaj=$dt_caj["nomVen"];	
		}
		else {
			$tipcaj="TODOS";
		}	
	    $this->Cell(230,3,'LISTADO DE VENTAS',0,1,'C');
		$this->SetFont('Arial','B',8);
		$this->Cell(230,3,'',0,1,'L');		
		$this->Cell(230,3,'PERIODO DEL '.$_POST['fecini'].' AL '.$_POST['fecfin'].'',0,1,'C');
		$this->Cell(230,3,'PUNTO DE VENTA:'.$pventa.                        '',0,1,'C');
		$this->Cell(230,3,'CAJERO:'.       $tipcaj.                           '',0,1,'C');		
	
	    //Salto de línea
	    $this->Ln(3);
		$this->SetFont('ARIAL','B',9);
		$this->SetFillColor(230,230,230);
		$this->Cell(23,4,'Fol. Vta.',1,0,'C',1);
		$this->Cell(23,4,'P. Vta./Mesa',1,0,'C',1);
		$this->Cell(16,4,'Hora',1,0,'C',1);
		$this->Cell(18,4,'Fecha',1,0,'C',1);	
		$this->Cell(14,4,'Tickets',1,0,'C',1);		
		$this->Cell(23,4,'Mesero',1,0,'C',1);
		$this->Cell(35,4,'FP',1,0,'C',1);		
		$this->Cell(20,4,'Subtotal',1,0,'C',1);
			
		$this->Cell(20,4,'Desc $',1,0,'C',1);				
		$this->Cell(20,4,'N.E.x.C',1,0,'C',1);		
		$this->Cell(20,4,'N.E.x.C (S)',1,0,'C',1);
		$this->Cell(20,4,'Total',1,1,'C',1);
	}
	//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('L','mm','Letter');
$pdf->AddPage();
$pdf->AliasNbPages();
$binit=-1;
$pdf->SetFillColor(240,240,240);
$pdf->SetFont('Arial','',8);

	$fecIni=chfecha($_POST['fecini'],'d/m/Y','Y-m-d');
	$fecFin=chfecha($_POST['fecfin'],'d/m/Y','Y-m-d');
	
	if ($fecIni<'2013-07-01'){
	$table='pvrescaj';
	$taDet='pvrescad';
	$posicion='fposres';
	$tip='pvrestip';
	$impresion='pvresimp';
	$memaDet='pvresmemd';
	//$mema='pvresmema';
	}else{
	$table='dgger_cajerosj';
	$taDet='dgger_cajerosd';
	$posicion='fpos';
	$tip='fpropina';
	$impresion='fimpresion';
	$memaDet='dgpememd';
	//$mema='dgpemema';
	}
	
	$arrBus[]="(dgpemema.fec_ven between '$fecIni' and '$fecFin')";	
	
	if($_POST['secodven']!='T') {
		 $arrBus[]="$table.vendedor='".$_POST['secodven']."'";		
	}
	if($_POST['setipven']!='T') 
	{
	 	$arrBus[]="dgpemema.tip_ven='".$_POST['setipven']."'";
	}	
	if($_POST['setipvent']!='T') {
  		$arrBus[]=' ptventa.nparte1='.$_POST['setipvent'].' ';
	}else {
		$arrBus[]=' ptventa.nparte1!=0 ';
    }
	
	
 	$WHERE="WHERE ".implode(" AND ",$arrBus);
		//dgpemema.iva_ven,
@$sql = mysqli_query($GLOBALS['con'] ,"
		SELECT 
			dgpemema.fol_ven,
			dgpemema.con_ven,
			dgpemema.tip_ven,
			dgpemema.fec_ven,
			dgpemema.cod_ven,
			
			ctmeser.np_desc1 as nomVen,
			ctmeser.iniciales as iniVen,
			dgpemema.tot_ven,
			dgpemema.imp_pag,
			dgpemema.sub_ven,
			dgpemema.por_des as descuento,
			$posicion.mesa as mesa,
			ctmesas.np_desc1 as dmesa,
			ctarea.np_desc1 as  darea,
			ptventa.tip_ven as dptvta,
			$table.acc_hor as hora,
			$tip.tip as propina,
			$taDet.tip_pag as tipag,
			dgtippag.ini_pag as descpag,
			dgpemema.cancela as cancela,
			$memaDet.imp_des as desc_uento,
			dgpemema.servicio as servicio,
			dgpemema.mt_cancela as comentario
		FROM 
			dgpemema
			LEFT JOIN $posicion ON $posicion.fol_ven=dgpemema.fol_ven
			LEFT JOIN ctmesas ON ctmesas.nparte1=$posicion.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
			LEFT JOIN $table ON $table.pedido=dgpemema.fol_ven		
			LEFT JOIN  $tip ON $tip.fol_ven=dgpemema.fol_ven
			LEFT JOIN $taDet ON $taDet.n_rec=$table.n_rec	
			LEFT JOIN  dgtippag ON dgtippag.tip_pag=$taDet.tip_pag
			LEFT JOIN dgcatven ON dgpemema.cod_ven=dgcatven.cod_ven
			LEFT JOIN $memaDet ON $memaDet.cons_ven=dgpemema.fol_ven
			
		$WHERE
		group by dgpemema.fol_ven
		ORDER BY 
			dgpemema.con_ven");
	echo mysqli_error($GLOBALS['con']);
	while($reg = mysqli_fetch_array($sql)){
//------suma de productos
	@$s = mysqli_query($GLOBALS['con'] ,"SELECT SUM($memaDet.pre_bas*$memaDet.can_ref) as sumat, sum($memaDet.imp_des) as desCuento FROM $memaDet, $table, dgpemema WHERE $memaDet.cons_ven='".$reg['fol_ven']."' and $table.pedido='".$reg['fol_ven']."' and $memaDet.cons_ven=dgpemema.fol_ven and dgpemema.cancelado!=1 and dgpemema.cancela=0 and $table.cancelado=0 and $memaDet.cancela=0");
		echo mysqli_error($GLOBALS['con']);
		while($r = mysqli_fetch_array($s)){
		$suma=$r["sumat"];
		$desCuento=$r['desCuento'];
		}
////---------------------------------	

//------suma de productos solo cortesia
	@$cr=mysqli_query($GLOBALS['con'] ,"select $taDet.importe as sumat from $table left join $taDet on ($taDet.n_rec=$table.n_rec) where $table.pedido='".$reg['fol_ven']."' and $taDet.tip_pag=3");
	echo mysqli_error($GLOBALS['con']);
	$ccr = mysqli_fetch_array($cr);
	if ($ccr['sumat']!=0){
	$sumacr=$ccr["sumat"]+$reg['propina'];
	}else{
	$sumacr=$ccr["sumat"];
	}
////---------------------------------
	//IMPRESION DE CUENTAS NO PAGADAS
	$pdf->SetFont('Arial','',8);
	 if (($reg["imp_pag"]==0.00) && ($reg["cancela"]==1)){
	$pdf->SetFillColor(230,230,230);
	$pdf->Cell(23,4,$reg["con_ven"],0,0,'C',1);
	$pdf->Cell(23,4,$reg["dptvta"].' / '.$reg["dmesa"],0,0,'L',1);
	$pdf->Cell(16,4,$reg["hora"],0,0,'C',1);
	$pdf->Cell(18,4,chfecha($reg['fec_ven'],'Y-m-d','d/m/Y'),0,0,'C',1);	
	$tickets=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"select count(*) as num_imp from $impresion where fol_ven='".$reg['fol_ven']."'"));
	$pdf->Cell(14,4,$tickets['num_imp'],0,0,'C',1);
	$pdf->Cell(23,4,$reg["iniVen"],0,0,'C',1);			
	$pdf->Cell(30,4,'CANCELADO',0,0,'L',1);			
	$pdf->Cell(105,4,$reg["comentario"],0,1,'L',1); 			
	}else{
	$pdf->SetFont('ARIAL','',8);
	$pdf->SetFillColor(260,260,260);
	$pdf->Cell(23,4,$reg["con_ven"],0,0,'C',1);	
	$pdf->Cell(23,4,$reg["dptvta"].' / '.$reg["dmesa"],0,0,'L',1);
	$pdf->Cell(16,4,$reg["hora"],0,0,'C',1);
	$pdf->Cell(18,4,chfecha($reg['fec_ven'],'Y-m-d','d/m/Y'),0,0,'C',1);
	$tickets=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"select count(*) as num_imp from $impresion where fol_ven='".$reg['fol_ven']."'"));	
	$pdf->Cell(14,4,$tickets['num_imp'],0,0,'C',1);
	$pdf->Cell(23,4,$reg["iniVen"],0,0,'C',1);	
	if ($reg['tipag']==''){				
	$pdf->Cell(142,4,'CUENTA ABIERTA',0,0,'L',1);			
	}else{
		$folio=mysqli_query($GLOBALS['con'] ,"select $table.num_cli as cliente,$taDet.tip_pag as tppag,dgtippag.ini_pag as pagoo from $taDet inner join dgtippag on dgtippag.tip_pag=$taDet.tip_pag inner join $table on $table.n_rec=$taDet.n_rec
	   where $table.pedido='".$reg['fol_ven']."'");
	
		$pdf->SetFont('ARIAL','',8);
		$a5=0;
		$cont=0;
		$_SESSION['clientec']=0;
		 while($rg=mysqli_fetch_array($folio)){
		 	if ($rg['tppag']==1){
			$cont++;
			$_SESSION['clientec']=$rg['cliente'];
			}
			$pdf->Cell(4,4,$rg['pagoo'].' | ',0,0,'C',0);
		$a5++; 
		}
		$dest=35-($a5*4);	
		 if ($GLOBALS['con']t>=1){	
	   	$sql_cli=mysqli_query($GLOBALS['con'] ,"select * from valeryus19 where n_reg='".$_SESSION['clientec']."'");
	$reg_hb=mysqli_fetch_array($sql_cli);
		if ($reg_hb['fnumcto']!='')
		$hbit=$reg_hb['fnumcto'];
		else
		$hbit=$reg_hb['fmuncto'];
		$pdf->Cell($dest,4,'F.'.$reg_hb['n_reg'].'H.'.$hbit,0,0,'C',0);
		}else{
		$pdf->Cell($dest,4,'',0,0,'C',0);
		}		
	}	
		$pdf->SetFont('ARIAL','',8);
	
	$pdf->Cell(20,4,number_format($suma,2,'.',','),0,0,'R',1);			//subt
	$pdf->Cell(20,4,'$'.number_format($desCuento,2,'.',','),2,0,'R',1);  //descuento
	$pdf->Cell(20,4,number_format($reg["propina"],2,'.',','),0,0,'R',1);			//propina	
	$pdf->Cell(20,4,number_format($reg["servicio"],2,'.',','),0,0,'R',1);		//iva				
	$pdf->Cell(20,4,number_format($sumtot=($suma+$reg['propina']+$reg['servicio']-$desCuento),2,'.',','),0,1,'R',1);			//total	
		$granDesVen +=$desCuento;  //descuento
		$granCorVen +=$sumacr;  //cortesia
		$granSer +=$reg["servicio"];  //servicio
		$granTotVen +=$suma;  //subtotal
		//$granPagVen +=$reg["iva_ven"];  //iva
		if($reg["imp_pag"]!=0){
		 $granSalVen +=$sumtot;  //total
		}
		$granPro    +=$reg["propina"];  //propina		
		$numVen++;
		}
	
		
}

    $pdf->SetFont('ARIAL','B',9);
	$pdf->SetFillColor(230,230,230);
		$pdf->Cell(152,3,'TOTALES',0,0,'L',1);
		//$pdf->Cell(20,3,number_format($granDesVen,2,'.',','),0,0,'R',1);
		$pdf->Cell(20,3,number_format($granTotVen,2,'.',','),0,0,'R',1);
		$pdf->Cell(20,3,number_format($granDesVen,2,'.',','),0,0,'R',1);
		$pdf->Cell(20,3,number_format($granPro,2,'.',','),0,0,'R',1);						
		$pdf->Cell(20,3,number_format($granSer,2,'.',','),0,0,'R',1);
		$pdf->Cell(20,3,number_format($granSalVen,2,'.',','),0,1,'R',1);				
		
$pdf->Output("Listado_de_Ventas_Diarias.pdf","D");
//441-->400
//373-->300
?>