<?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','B',10);
		$this->SetFillColor(200,200,200);
		$this->Cell(220,5,'FACTURACION X DIA',0,1,'C',0);
		$this->SetFont('Arial','I',7);
		$this->Cell(220,5,'Del '.$_POST['fecini'].' al '.$_POST['fecfin'],0,1,'C',0);
		$this->Cell(25,5,'Clave de reporte',0,0,'L',0);
		$sq_clarep=mysqli_query($GLOBALS['con'] ,"select * from dvclarep  where nom_rep='FACXDIA'");
		$dt_clarep=mysqli_fetch_array($sq_clarep);
		$this->SetFont('Arial','B',6);
		$this->Cell(25,5,$dt_clarep['cve_rep'],0,1,'C',0);
		$this->Cell(10,3,'Codigo',1,0,'C',1);
      	$this->Cell(27,3,'Nombre',1,0,'C',1);
        $this->Cell(5,3,'Mov',1,0,'C',1);
     	$this->Cell(15,3,'Num. Doc.',1,0,'C',1);
     	$this->Cell(18,3,'Subtotal',1,0,'C',1);
     	$this->Cell(18,3,'IVA',1,0,'C',1);
      	$this->Cell(18,3,'Total Doc.',1,0,'C',1);
     	$this->Cell(18,3,'Imp. Pag.',1,0,'C',1);
     	$this->Cell(18,3,'Not. Cre.',1,0,'C',1);
      	$this->Cell(18,3,'Car/Abo',1,0,'C',1);
		//$this->Cell(10,3,'Abo.',1,0,'C',1);
      	$this->Cell(18,3,'Saldo Doc.',1,0,'C',1);
     	$this->Cell(14,3,'Num.Fac.Ant.',1,1,'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.5);
$z=-1;
		$totales=NULL;
		$totgeneral=NULL;
		while(@$dt_redocl=mysqli_fetch_array($sq_redocl)){
			if ($dt_redocl['tip_cam'] > 0 && $dt_redocl['moneda']=='2') $tipcambio=$dt_redocl['tip_cam'];
			else $tipcambio=1;
			$dt_bascps=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"select * from dxbascps where codigo='".$dt_redocl['codigo']."'"));
			if($z==-1){
				$pdf->Cell(195,3,'Movimientos Del '.chfecha($dt_redocl['fec_doc'],'Y-m-d','d de M del Y'),0,1,'C',1);
				$z=$dt_redocl['fec_doc'];
			}elseif($z!=$dt_redocl['fec_doc']){
				imptotales("Total x dia: ",$totales,$pdf);
				$pdf->Cell(195,3,'Movimientos Del '.chfecha($dt_redocl['fec_doc'],'Y-m-d','d de M del Y'),0,1,'C',1);
				$z=$dt_redocl['fec_doc'];
				$totales=NULL;
			}
			if($dt_redocl['tip_mot']!="R"){
				llenaTotales($totales,$dt_redocl);
				llenaTotales($totgeneral,$dt_redocl);
			}
			$pdf->Cell(10,3,$dt_redocl['codigo'],0,0,'L');
			$pdf->Cell(27,3,substr(trim($dt_bascps['ap_paterno'].' '.$dt_bascps['ap_materno'].' '.$dt_bascps['nombre']),0,15),0,0,'L');
			$pdf->Cell(5,3,$dt_redocl['tip_mot'],0,0,'L');
			$pdf->Cell(15,3,$dt_redocl['num_doc'],0,0,'L');
			$pdf->Cell(18,3,number_format($dt_redocl['sub_tot']*$tipcambio,2,'.',','),0,0,'R');
			$pdf->Cell(18,3,number_format($dt_redocl['iva_tot']*$tipcambio,2,'.',','),0,0,'R');
			$pdf->Cell(18,3,number_format($dt_redocl['total']*$tipcambio,2,'.',','),0,0,'R');
			$pdf->Cell(18,3,number_format($dt_redocl['tot_pag']*$tipcambio,2,'.',','),0,0,'R');
			$pdf->Cell(16,3,number_format($dt_redocl['tot_not']*$tipcambio,2,'.',','),0,0,'R');
			if($dt_redocl['tot_car']!=0)
				$pdf->Cell(16,3,number_format($dt_redocl['tot_car']*$tipcambio,2,'.',','),0,0,'R');
			else
				$pdf->Cell(16,3,number_format($dt_redocl['tot_abo']*$tipcambio,2,'.',','),0,0,'R');
			$pdf->Cell(18,3,number_format($dt_redocl['tot_sal']*$tipcambio,2,'.',','),0,0,'R');
			$pdf->Cell(14,3,$dt_redocl['num_fac_ant'],0,1,'L');
		}
		imptotales("Total x dia: ",$totales,$pdf);
		$pdf->SetFillColor(230,230,230);
		imptotales("Total General: ",$totgeneral,$pdf);
		//imptotales("Saldo General Documentos: ",$totgeneral);
	
	function llenaTotales(&$totales, $dat){
		if ($dat['tip_cam'] > 0 && $dat['tip_mot']!='N') $tipcambio=$dat['tip_cam'];
		else $tipcambio=1;
		$totales[0]+=$dat["sub_tot"]*$tipcambio;
		$totales[1]+=$dat["iva_tot"]*$tipcambio;
		$totales[2]+=$dat["total"]*$tipcambio;
		$totales[3]+=$dat["tot_pag"]*$tipcambio;
		$totales[4]+=$dat["tot_not"]*$tipcambio;
		$totales[5]+=$dat["tot_car"]*$tipcambio;
		$totales[6]+=$dat["tot_abo"]*$tipcambio;
		$totales[7]+=$dat["tot_sal"]*$tipcambio;
	}
	
	function imptotales($leyenda,$totales,&$pdf){
		$pdf->Cell(57,5,$leyenda,0,0,'R');
		$pdf->Cell(18,5,number_format($totales[0],2,'.',','),0,0,'R');
		$pdf->Cell(18,5,number_format($totales[1],2,'.',','),0,0,'R');
		$pdf->Cell(18,5,number_format($totales[2],2,'.',','),0,0,'R');
		$pdf->Cell(18,5,number_format($totales[3],2,'.',','),0,0,'R');
		$pdf->Cell(16,5,number_format($totales[4],2,'.',','),0,0,'R');
		if($totales[5]!=0)
			$pdf->Cell(16,5,number_format($totales[5],2,'.',','),0,0,'R');
		else
			$pdf->Cell(16,5,number_format($totales[6],2,'.',','),0,0,'R');
		$pdf->Cell(18,5,number_format($totales[7],2,'.',','),0,0,'R');
		$pdf->Cell(14,5,' ',0,1,'L');
	}
$pdf->Output("Facturacion_X_Dia.pdf","D");

?>