<?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 Página
	function Header()
	{
	    //Logotipo
	    include("DYIM_VER.php");
		
		$this->Ln(5);
		$this->SetFont('Arial','',8);
		$this->SetFillColor(200,200,200);
		$this->Cell(193,5,'INGRESOS CAJA X DIA',0,1,'C',0);
		$this->Cell(193,5,'Del '.$_POST['fecini'].' al '.$_POST['fecfin'],0,1,'C',0);
		$this->Cell(10,5,'',0,0,'L',0);
		$this->Cell(25,5,'Clave de reporte',0,0,'L',0);
		$sq_clarep=mysqli_query($GLOBALS['con'] ,"select * from dvclarep  where nom_rep='INGXDIA'");
		$dt_clarep=mysqli_fetch_array($sq_clarep);
		$this->Cell(25,5,$dt_clarep['cve_rep'],0,1,'C',0);
		$this->Cell(10,5,'',0,0,'C');
		$this->Cell(20,5,'Folio Caja',1,0,'C',1);
      	$this->Cell(20,5,'Numero Docto.',1,0,'C',1);
        $this->Cell(70,5,'Nombre Cliente',1,0,'C',1);
     	$this->Cell(20,5,'Subtotal',1,0,'C',1);
     	$this->Cell(20,5,'IVA',1,0,'C',1);
     	$this->Cell(20,5,'Total',1,0,'C',1);
      	$this->ln(5);
	}
	
	//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(),0,0,'C');
	}
}

$tipmov='C';
$sq_redocl=mysqli_query($GLOBALS['con'] ,$_POST['busqueda']);

$pdf=new PDF('P','mm','Letter');
$pdf->AddPage();
$pdf->SetFillColor(240,240,240);
$pdf->SetFont('Arial','',7);
$z=-1;
		$totales=NULL;
		$totgeneral=NULL;
		while(@$dt_redocl=mysqli_fetch_array($sq_redocl)){
			$dt_bascps=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"select * from dxbascps where codigo='".$dt_redocl['codigo']."'"));
			if($z==-1){
				$pdf->Cell(193,5,'Movimientos Del '.chfecha($dt_redocl['fec_cob'],'Y-m-d','d de M del Y'),0,1,'C',1);
				$z=$dt_redocl['fec_cob'];
			}elseif($z!=$dt_redocl['fec_cob']){
				imptotales("Total x dia: ",$totales,$pdf);
				$pdf->Cell(193,5,'Movimientos Del '.chfecha($dt_redocl['fec_cob'],'Y-m-d','d de M del Y'),0,1,'C',1);
				$z=$dt_redocl['fec_cob'];
				$totales=NULL;
			}
			llenaTotales($totales,$dt_redocl);
			llenaTotales($totgeneral,$dt_redocl);
			$pdf->Cell(10,5,'',0,0,'C');
			$pdf->Cell(20,5,$dt_redocl['fol_cob'],0,0,'C');
			$pdf->Cell(20,5,$dt_redocl['num_doc'],0,0,'C');
			$pdf->Cell(70,5,$dt_bascps['ap_paterno'].' '.$dt_bascps['ap_materno'].' '.$dt_bascps['nombre'],0,0,'C');
			$pdf->Cell(20,5,number_format($dt_redocl['imp_cob'],2,'.',','),0,0,'C');
			$pdf->Cell(20,5,number_format($dt_redocl['iva_cob'],2,'.',','),0,0,'C');
			$pdf->Cell(20,5,number_format($dt_redocl['tot_cob'],2,'.',','),0,1,'C');
		}
		imptotales("Total x dia: ",$totales,$pdf);
		imptotales("Total General: ",$totgeneral,$pdf);
		//imptotales("Saldo General Documentos: ",$totgeneral);
	
	function llenaTotales(&$totales, $dat){
		$totales[0]+=$dat["imp_cob"];
		$totales[1]+=$dat["iva_cob"];
		$totales[2]+=$dat["tot_cob"];
	}
	
	function imptotales($leyenda,$totales,&$pdf){
		$pdf->Cell(10,5,'',0,0,'C');
		$pdf->Cell(110,5,$leyenda,0,0,'R');
		$pdf->Cell(20,5,number_format($totales[0],2,'.',','),0,0,'C');
		$pdf->Cell(20,5,number_format($totales[1],2,'.',','),0,0,'C');
		$pdf->Cell(20,5,number_format($totales[2],2,'.',','),0,1,'C');
		//$pdf->Cell(16,5,' ',0,1,'C');
	}
$pdf->Output("Ingresos_Cajas_X_Dia.pdf","D");

?>