<?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->SetFont('Arial','B',8);
		$this->SetFillColor(230,230,230);
		$this->Ln(3);
		$artiprep=array('G'=>'General','D'=>'Detalle');
		$this->Cell(195,5,'DOCUMENTOS POR COBRAR',0,1,'C',0);
		$this->Cell(50,5,'',0,0,'C',0);
		$this->Cell(30,5,'Tipo: '.$_POST['setipdoc'],0,0,'C',0);
		if($_POST['setipdoc']!='SALDO')
			$this->Cell(35,5,'Fecha: '.$_POST['fecini'].' al '.$_POST['fecfin'],0,0,'C',0);
		else
			$this->Cell(30,5,'',0,0,'C',0);
		$v=$_POST['s4'];
		$this->Cell(30,5,'   Reporte: '.$artiprep[$v],0,0,'C',0);
		$this->Cell(5,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='DOCXCOB'");
		$dt_clarep=mysqli_fetch_array($sq_clarep);
		$this->Cell(25,5,$dt_clarep['cve_rep'],0,1,'C',0);
		
		$this->Cell(15,5,'Fec. Mov.',1,0,'C',1);
		$this->Cell(23,5,'Num. Doc.',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 Doc.',1,0,'C',1);
		$this->Cell(20,5,'Importe Pag.',1,0,'C',1);
		$this->Cell(20,5,'Notas Cred.',1,0,'C',1);
		$this->Cell(20,5,'Otros Abo.',1,0,'C',1);	
		$this->Cell(20,5,'Notas Car.',1,0,'C',1);		
		$this->Cell(20,5,'Saldo Doc.',1,1,'C',1);
		$this->Ln(2);
		
		
	
	}
	
	//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');
	}
}

$pdf=new PDF('P','mm','LETTER');
$pdf->AddPage();
$pdf->SetFont('Arial','',7);
$pdf->SetFillColor(240,240,240);
//echo $_POST['busqueda'];
$tipmov='C';
$fecini=chfecha($_POST['fecini'],'d/m/Y','Y-m-d');
$fecfin=chfecha($_POST['fecfin'],'d/m/Y','Y-m-d');
//$sq_bascps=mysqli_query($GLOBALS['con'] ,"select * from dxbascps where act1<>0 order by nombre");//busca clientes
	$dt_parame=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"SELECT * from dgparame"));
	$dt_catcli==mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"SELECT cod_cli,codigo from dgcatcli where cod_cli='".$dt_parame['cod_pubgral']."'"));
	$codigo=explode('|',$_POST['seclipro']);
	$andCli=" AND codigo<>'".$dt_catcli['codigo']."'";
	if($codigo[0]=="TODOS" || $codigo[0]==""){
		switch($_POST['setipdoc']){
			case "TODO": 	if($_POST['tefac']=="") 
								$busqueda="SELECT * FROM dvredocl where tip_mov='$tipmov' $andCli and fec_doc between '$fecini' and '$fecfin' order by codigo,fec_doc,num_doc";
							else
								$busqueda="SELECT * FROM dvredocl where  tip_mov='$tipmov' $andCli and fec_doc between '$fecini' and '$fecfin' and num_doc like '$_POST['tefac']%' order by codigo,fec_doc,num_doc";
			break;
			case "PAGADO": if($_POST['tefac']=="") 
								$busqueda="SELECT * FROM dvredocl where  tip_mov='$tipmov' $andCli and fec_doc between '$fecini' and '$fecfin' and tot_sal=0 order by codigo,fec_doc,num_doc";
							else
								$busqueda="SELECT * FROM dvredocl where tip_mov='$tipmov' $andCli and  fec_doc between '$fecini' and '$fecfin' and tot_sal=0 and num_doc like '$_POST['tefac']%'order by codigo,fec_doc,num_doc";
			break;
			case "SALDO": 	if($_POST['tefac']=="") 
								$busqueda="SELECT * FROM dvredocl where tip_mov='$tipmov' $andCli and  fec_doc between '$fecini' and '$fecfin' and  tot_sal<>0 order by codigo,fec_doc,num_doc";
							else
								$busqueda="SELECT * FROM dvredocl where tip_mov='$tipmov' $andCli and  fec_doc between '$fecini' and '$fecfin' and  tot_sal<>0 and num_doc like '$_POST['tefac']%'order by codigo,fec_doc,num_doc";
			break;
		}
		
	}
	
	elseif($codigo[0]!="TODOS"){
		switch($_POST['setipdoc']){
			case "TODO": 	if($_POST['tefac']=="") 
								$busqueda="SELECT * FROM dvredocl where  tip_mov='$tipmov' and codigo='$codigo[0]' and fec_doc between '$fecini' and '$fecfin' order by codigo,fec_doc,num_doc";
							else
								$busqueda="SELECT * FROM dvredocl where  tip_mov='$tipmov' and  codigo='$codigo[0]' and fec_doc between '$fecini' and '$fecfin' and num_doc like '$_POST['tefac']%' order by codigo,fec_doc,num_doc";
			break;
			case "PAGADO": if($_POST['tefac']=="") 
								$busqueda="SELECT * FROM dvredocl where  tip_mov='$tipmov' and  codigo='$codigo[0]' and fec_doc between '$fecini' and '$fecfin' and tot_sal=0 order by codigo,fec_doc,num_doc";
							else
								$busqueda="SELECT * FROM dvredocl where  tip_mov='$tipmov' and  codigo='$codigo[0]' and fec_doc between '$fecini' and '$fecfin' and tot_sal=0 and num_doc like '$_POST['tefac']%'order by codigo,fec_doc,num_doc";
			break;
			case "SALDO": 	if($_POST['tefac']=="") 
								$busqueda="SELECT * FROM dvredocl where  tip_mov='$tipmov'  and fec_doc between '$fecini' and '$fecfin' and  codigo='$codigo[0]' and tot_sal<>0 order by codigo,fec_doc,num_doc";
							else
								$busqueda="SELECT * FROM dvredocl where  tip_mov='$tipmov'  and fec_doc between '$fecini' and '$fecfin' and  codigo='$codigo[0]' and tot_sal<>0 and num_doc like '$_POST['tefac']%'order by codigo,fec_doc,num_doc";
			break;
		}
	}



$sq_redocl=mysqli_query($GLOBALS['con'] ,$busqueda);
$z=-1;
$totales=NULL;
$totgeneral=NULL;
while(@$dt_redocl=mysqli_fetch_array($sq_redocl)){
	$dt_totNotCar=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"SELECT SUM(tot_det) as totCar FROM dvdetfac WHERE tip_nota='".$dt_redocl['num_doc']."' "));
	$dt_redocl['tot_car']+=$dt_totNotCar['totCar'];
	$dt_bascps=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"select * from dxbascps where codigo='".$dt_redocl['codigo']."'"));
	if($z==-1){
		$pdf->Cell(200,5,$dt_bascps['codigo'].' - '.$dt_bascps['ap_paterno'].' '.$dt_bascps['ap_materno'].' '.$dt_bascps['nombre'],0,1,'C',1);
		$z=$dt_redocl['codigo'];
	}elseif($z!=$dt_redocl['codigo']){
		imptotales("Total x dia: ",$totales,$pdf);
		if($_POST['s5']=='1') $pdf->AddPage();
		$pdf->Cell(200,5,$dt_bascps['codigo'].' - '.$dt_bascps['ap_paterno'].' '.$dt_bascps['ap_materno'].' '.$dt_bascps['nombre'],0,1,'C',1);
		$z=$dt_redocl['codigo'];
		$totales=NULL;
	}
	llenaTotales($totales,$dt_redocl);
	llenaTotales($totgeneral,$dt_redocl);
	$pdf->Cell(15,3,chfecha($dt_redocl['fec_doc'],'Y-m-d','d/m/Y'),0,0,'C',0);
	$pdf->Cell(23,3,$dt_redocl['num_doc'],0,0,'L',0);
	$pdf->Cell(20,3,number_format($dt_redocl['sub_tot'],2,'.',','),0,0,'R',0);
	$pdf->Cell(20,3,number_format($dt_redocl['iva_tot'],2,'.',','),0,0,'R',0);
	$pdf->Cell(20,3,number_format($dt_redocl['total'],2,'.',','),0,0,'R',0);
	$pdf->Cell(20,3,number_format($dt_redocl['tot_pag'],2,'.',','),0,0,'R',0);
	$pdf->Cell(20,3,number_format($dt_redocl['tot_not'],2,'.',','),0,0,'R',0);
	$pdf->Cell(20,3,number_format($dt_redocl['tot_abo'],2,'.',','),0,0,'R',0);
	$pdf->Cell(20,3,number_format($dt_redocl['tot_car'],2,'.',','),0,0,'R',0);
	$pdf->Cell(20,3,number_format($dt_redocl['tot_sal'],2,'.',','),0,1,'R',0);
	if($_POST['s4']=='D'){
		imp_detalle($dt_redocl);
		$pdf->Ln(3);
	}
	$sq_notcre=mysqli_query($GLOBALS['con'] ,"SELECT * FROM dvnotcre WHERE tip_mto='$tipmov' AND num_doc='".$dt_redocl['num_doc']."'");
	$pdf->SetFont('Arial','BI',6.5);
	while($dt_notcre=mysqli_fetch_array($sq_notcre)){
		//$pdf->Cell(25,3,"NOTA CREDITO:",0,0,'L',0);
		$pdf->Cell(15,3,chfecha($dt_notcre['fec_not'],'Y-m-d','d/m/Y'),0,0,'C',0);
		$pdf->Cell(23,3,"NC: ".$dt_notcre['tip_not'].$dt_notcre['num_not'],0,0,'L',0);
		$pdf->Cell(20,3,number_format($dt_notcre['sub_not'],2,'.',','),0,0,'R',0);
		$pdf->Cell(20,3,number_format($dt_notcre['iva_not'],2,'.',','),0,0,'R',0);
		$pdf->Cell(20,3,number_format($dt_notcre['tot_not'],2,'.',','),0,1,'R',0);		
	}
	$pdf->SetFont('Arial','',7);	
}
imptotales("Total x dia: ",$totales,$pdf);
$pdf->Ln(5);
imptotales("Total General: ",$totgeneral,$pdf);
function imp_detalle($dt_redocl){
	global $pdf;
	if ($dt_redocl['tip_cam'] > 0 && $dt_redocl['moneda']=='2') $tipcambio=$dt_redocl['tip_cam'];
	else $tipcambio=1;
	$sq_redocd=mysqli_query($GLOBALS['con'] ,"select * from dvredocd where tip_mov='".$dt_redocl['tip_mov']."' AND codigo='".$dt_redocl['codigo']."' AND num_doc='".$dt_redocl['num_doc']."'");
	while($dt_redocd=mysqli_fetch_array($sq_redocd)){
		$pdf->Cell(20,3,'Mov: '.$dt_redocd['tip_mov'],0,0,'C',0);
		$pdf->Cell(25,3,'Fecha: '.chfecha($dt_redocl['fec_doc'],'Y-m-d','d/m/Y'),0,0,'C',0);
		$pdf->Cell(25,3,'Ref.:'.$dt_redocd['fol_ope'],0,0,'C',0);
		$pdf->Cell(25,3,'Importe:',0,0,'R',0);
		$pdf->Cell(25,3,number_format($dt_redocd['sub_tot']*$tipcambio,2,'.',','),0,0,'C',0);
		$pdf->Cell(25,3,number_format($dt_redocd['can_pro']*$tipcambio,2,'.',','),0,0,'C',0);
		$pdf->Cell(25,3,'Movto.:'.$dt_redocd['cve_mto'],0,0,'C',0);
		$pdf->Cell(25,3,'Cen/Vta: '.$dt_redocd['nom_pac'],0,1,'C',0);
	}
}
	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[6]+=$dat["tot_car"]*$tipcambio;
		$totales[5]+=$dat["tot_abo"]*$tipcambio;
		$totales[7]+=$dat["tot_sal"]*$tipcambio;
	
	}
	
function imptotales($leyenda,$totales,&$pdf){
	$pdf->Cell(43,5,$leyenda,0,0,'R',0);
	$pdf->Cell(20,5,number_format($totales[0],2,'.',','),0,0,'C',0);
	$pdf->Cell(20,5,number_format($totales[1],2,'.',','),0,0,'C',0);
	$pdf->Cell(20,5,number_format($totales[2],2,'.',','),0,0,'C',0);
	$pdf->Cell(20,5,number_format($totales[3],2,'.',','),0,0,'C',0);
	$pdf->Cell(20,5,number_format($totales[4],2,'.',','),0,0,'C',0);
	$pdf->Cell(20,5,number_format($totales[5],2,'.',','),0,0,'C',0);
	$pdf->Cell(20,5,number_format($totales[6],2,'.',','),0,0,'C',0);
	$pdf->Cell(20,5,number_format($totales[7],2,'.',','),0,1,'C',0);
}

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

?>