<?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');
$dt_parame=mysqli_fetch_array(mysqli_query($con ,"SELECT * FROM dgparame"));

class PDF extends FPDF{
	//Cabecera de página
	function Header()
	{
	    //Logotipo
	    include("DYIM_VER.php");

	    //Título
		$dt_tipven=mysqli_fetch_array(mysqli_query($con ,"SELECT tip_ven,des_tipven FROM dgtipven WHERE tip_ven='".$_SESSION['tipven']."'"));
		$dt_catven=mysqli_fetch_array(mysqli_query($con ,"SELECT cod_ven,CONCAT(nom_ven,' ',ap_pat_ven,' ',ap_mat_ven) as nomVen FROM dgcatven WHERE cod_ven='".$_REQUEST['vendedor']."'"));
		//$dt_tippag=mysqli_fetch_array(mysqli_query($con ,"SELECT * from dgtippag WHERE tip_pag IN($_REQUEST['tip_pag_pro'])"));
		$arrTipMov=array(1=>'COBRO DE PEDIDOS','COBRO DE FACTURAS','ANTICIPOS','DEVOLUCION DE VENTAS','OTROS');
		if($_REQUEST['select2'] == "TODOS"){
			$corte="CORTE: TODOS";
		}elseif($_REQUEST['select2'] == "SC"){	
			$corte="CORTE: SIN CORTE";
		}else{
			$corte="CORTE: $_REQUEST['select2']";		
		}
		if($_REQUEST['select']  != "TODOS")$tipMov=", MOVIMIENTO: ".$arrTipMov[$_REQUEST['select']];
		if($_REQUEST['vendedor']  != "T")  $cajero=", CAJERO: $_REQUEST['vendedor'] $dt_catven['nomVen']";
		if(trim($_REQUEST['tip_pag_pro']) != "") $tipPag=", TIPO PAGO: $_REQUEST['tip_pag_pro']";
		$this->SetFont('Arial','B',8);
		$arrTipImp=array("1"=>"VERIFICACION","2"=>"ACTUALIZACION","3"=>"REIMPRESION","4"=>"ACTUALIZACION");

	    $this->Cell(180,3,'CORTE DE CAJA '.$dt_tipven['des_tipven'],0,1,'C');
		$this->SetFont('Arial','BI',7);
		$this->Cell(180,3,"(FECHA CORTE: $_REQUEST['t1'], ".$arrTipImp[$_REQUEST['r1']].", $corte $tipMov $cajero $tipPag)",0,1,'C');
	    //Salto de línea
		$this->Ln(5);
		$this->SetFont('Arial','',7);
		$this->SetFillColor(238,238,238);
		//$this->Cell(20,3,'Folio Recibo',1,0,'C',1);
		//if($_SESSION['mod_sis']!="LAB")
		//	$this->Cell(20,3,'Movimiento',1,0,'C',1);
		//$this->Cell(40,3,'Cliente/Persona',1,0,'C',1);
		//$this->Cell(40,3,'Concepto',1,0,'C',1);
		$this->Cell(20,3,'Folio',1,0,'C',1);
		$this->Cell(20,3,'Importe',1,0,'C',1);
		//$this->Cell(20,3,'Num. Fac.',1,0,'C',1);
		$this->Cell(15,3,'Cajero',1,3,'R',1);
		$this->Ln(1);
	}
	//Pie de página
	function Footer()
	{
	    //Posición: a 1,5 cm del final
	    $this->SetY(-15);
	    //Arial italic 8
	    $this->SetFont('Arial','I',8);
	    //Número de página
	    $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
	}
}

//Crear objeto y llamar a constructor
//Recibe:posicion de pagina(P:Vertical, L:Horizontal), unidad de medida(mm:milimetros),tamaño de pagina(A4,Letter)
$pdf=new PDF('P','mm','Letter');
//Agregar Pagina
$pdf->AddPage();
$pdf->AliasNbPages();


//selecciona tipo de letra
$pdf->SetFont('Arial','I',7);

//Color de Relleno de Celda RGB
$pdf->SetFillColor(230,230,230);

//Imprime Celca
//Recibe:
/*
* Largo de la celda
* Ancho de Celda
* Texto a imprimir dentro de la celda
* 1 para imprimir borde de celda, 0 para no imprimir borde de celda
* 1 para dar salto de linea despues de imprimir la celda, 0 para no dar salto de linea
* Posicion del texto dentro de la celda('C':Centro,'R': Derecha,'L': Izquierda)
* 1 para Imprimir color de relleno de la celda,0 no imprime color de relleno
*/
$pdf->SetFillColor(245);
require_once("DG32000_PA.php");	 
$tipMov=array(1=>'COBRO DE PEDIDOS','COBRO DE FACTURAS','ANTICIPOS','DEVOLUCION DE VENTAS','OTROS');
$tipAfe="-1";
$arrTipAfe=array("I"=>"01 INGRESOS","E"=>"02 DEVOLUCIONES");
$pdf->SetFillColor(230);
$tabRes=.1;
$saldoInicial=0;
$auxFec='-1';
if(mysqli_num_rows($sql)==0){
	$pdf->Cell(70,3,"SIN FOLIOS.",0,0,'R',0);
	$pdf->Output("CORTE_DE_CAJA.pdf","D");
	exit();	
}
while($reg = mysqli_fetch_array($sql)){

	if($tipAfe!=$reg['tip_afe'] || $auxFec!=$reg['fec_rec']){
		if($tipAfe!="-1"){
			$pdf->Cell($tabRes,3,'',0,0,'C',0);
			//$pdf->Cell(70,3,"",0,0,'R',0);
			$pdf->Cell(25,3,"**SALDO FINAL**",0,1,'R',0);
			$pdf->SetFont('Arial','BI',7);
			$pdf->Cell($tabRes,3,'',0,0,'C',0);
			$pdf->Cell(23,3,'Total Dia '.chfecha($fecCor,'Y-m-d','d/m/Y'),0,0,'L',0);
			//$pdf->Cell(20,3,"",0,0,'R',0);
			$pdf->Cell(23,3,number_format($sumParTipPag,2,'.',','),0,0,'R',0);
			$pdf->Cell(23,3,number_format($sumParCor,2,'.',','),0,1,'R',0);
			$pdf->Cell($tabRes,3,'',0,0,'C',0);
			$pdf->SetFont('Arial','',7);
			$pdf->Cell(70,3,"RESUMEN FORMAS DE PAGO",0,1,'C',0);
			$arrForPag=$sumTotTipPag[$tipAfe];
			$totParTipPag=0;
			foreach($arrForPag as $idTipPag=>$arrDatTipPag){
				$pdf->Cell($tabRes,3,'',0,0,'C',0);
				$pdf->Cell(10,3,$idTipPag,0,0,'L',0);
				$pdf->Cell(35,3,$arrDatTipPag['desPag'],0,0,'L',0);
				$pdf->Cell(25,3,number_format($arrDatTipPag['totPag'],2,'.',','),0,1,'R',0);				
				$totParTipPag +=$arrDatTipPag['totPag'];
			}
			unset($sumTotTipPag);
			$pdf->SetFont('Arial','B',7);
			$pdf->Cell($tabRes,3,'',0,0,'C',0);
			$pdf->Cell(45,3,"SUMA: ",0,0,'R',0);
			$pdf->Cell(25,3,number_format($totParTipPag,2,'.',','),0,1,'R',0);
			$pdf->Ln(2);
			$sumParTipPag=0;										
		}
		$pdf->SetFillColor(230);
		$tipAfe=$reg['tip_afe'];
		$fecCor=$reg['fec_rec'];
		/********************Verifica si cambio de fecha*********************/
		if($auxFec!=$reg['fec_rec']){
			$auxFec=$reg['fec_rec'];
			$dt_tipvenCaj=mysqli_fetch_array(mysqli_query($con ,"SELECT * FROM dgtipven WHERE tip_ven='".$reg['tip_caj']."'"));
			$pdf->Cell(180,5,"CAJA $dt_tipvenCaj['des_tipven'], FECHA:".chfecha($auxFec,'Y-m-d','d/m/Y')." SALDO INICIAL: ".number_format($saldoInicial,2,'.',','),'B',1,'L',0);
			$pdf->Ln(1);
			$subTotPubGen =0;$ivaTotPubGen =0;$totPubGen =0;
			//$sumParCor=0;

		}	
		/********************************************************************/
		$pdf->Cell(175,3,$arrTipAfe[$reg['tip_afe'],0,1,"C",1);
		$pdf->Ln(2);	
	}
	$pdf->SetFillColor(235);
	$pdf->SetFont('Arial','',7);
	if($dt_parame['cod_pubgral']==$dt_pemema['cod_cli']){
		$subTotPubGen +=$dt_pemema['sub_ven'];
		$ivaTotPubGen +=$dt_pemema['iva_ven'];
		$totPubGen    +=$dt_pemema['tot_ven'];
	}
	//$pdf->Cell(20,3,$reg['n_rec'],0,0,'R',0);
	//if($_SESSION['mod_sis']!="LAB")
	//		$pdf->Cell(20,3,$tipMov[$reg['tip_mov']],0,0,'L',1);
	//$pdf->Cell(40,3,substr($reg['nomCliAlt'],0,23),0,0,'L',1);
	//$pdf->Cell(40,3,substr($reg['cveEmp'].'-'.$reg['nomEmp'],0,23),0,0,'L',1);
	$pdf->Cell(20,3,$reg['con_ven'],0,0,'R',1);
	$pdf->Cell(20,3, number_format($reg['impParCaj'],2, '.', ','),0,0,'R',1);
	//if($reg['numFac']=='0' || $reg['numFac']==''){$reg['tipFac']="";$reg['numFac']="";}
	//$pdf->Cell(20,3,$reg['tipFac'].$reg['numFac'],0,0,'L',1);
	$pdf->Cell(15,3,$reg['vendedor'],0,1,'R',1);
	//------>Si Es Pago de Factura Imprimirlas---------->
	if($reg['tip_mov']=='2'){
		$k=0;
		$sq_ger_cajeroso=mysqli_query($con ,"SELECT * FROM dgger_cajeroso WHERE rec_caj='".$reg['n_rec']."'");
		while($dt_ger_cajeroso=mysqli_fetch_array($sq_ger_cajeroso)){
			if((++$k%4)==0) $jump=1; else $jump=0;
			$pdf->Cell(20,3,$dt_ger_cajeroso['num_doc'],0,0,'L',0);
			$pdf->Cell(10,3,number_format($dt_ger_cajeroso['import'],2,'.',','),0,0,'R',0);
			$pdf->Cell(15,3,"",0,$jump,'R',0);								
		}
		if($k%4!=0) $pdf->Ln(3);
	}
	//-------------------------------------------------->
	($reg['tip_afe']=="I")?($polNum=1):($polNum=-1);
	$sumParCor +=$polNum*abs($reg['impParCaj']);
	$saldoInicial +=$polNum*abs($reg['impParCaj']);		
	if (trim($_REQUEST['tip_pag_pro']) == "")//si todos los pagos
		$ss = mysqli_query($con ,"select dgger_cajerosd.*,dgtippag.desc_pag as desTipPag from dgger_cajerosd LEFT JOIN dgtippag ON(dgtippag.tip_pag=dgger_cajerosd.tip_pag) WHERE dgger_cajerosd.n_rec = '".$reg['n_rec']."'");
	else//en caso contrario
		$ss = mysqli_query($con ,"select dgger_cajerosd.*,dgtippag.desc_pag as desTipPag from dgger_cajerosd LEFT JOIN dgtippag ON(dgtippag.tip_pag=dgger_cajerosd.tip_pag) where dgger_cajerosd.n_rec = '".$reg['n_rec']."' and dgger_cajerosd.tip_pag IN($_REQUEST['tip_pag_pro'])");
	$pdf->SetFont('Arial','I',7);
	($_SESSION['mod_sis']!="LAB")?($wCell=50):($wCell=70);
	while ($rrr = mysqli_fetch_array($ss)){
		//$pdf->Cell(20,3,'',0,0,'R',0);
		$sumTotTipPag[$reg['tip_afe'][$rrr['tip_pag']['totPag'] +=abs($rrr['importe']);
		$sumTotTipPag[$reg['tip_afe'][$rrr['tip_pag']['desPag'] =$rrr['desTipPag'];
		$sumParTipPag +=abs($rrr['importe']);
		$sa = mysqli_fetch_array(mysqli_query($con ,"select * from dgtippag where tip_pag = '".$rrr['tip_pag']."'"));
		//if ($sa['tip_mv_pag'] == "N"){
		//	$pdf->Cell($wCell,3,$sa['desc_pag'],'0',0,'L',0);
		//}else{
			$pdf->Cell($wCell,3,$sa['desc_pag']. '         Ref: ' . $rrr['ref'] . '          Aut: ' . $rrr['aut'],'',0,'L',0);
		//}
		$pdf->Cell(20,3,'$' . number_format(abs($rrr['importe']), 2, '.', ','),0,0,'R',0);
		$pdf->Cell(20,3,'','0',1,'C',0);	
		$tt = $rrr['tip_pag'];
		if ($reg['tip_afe'] == 'I'){//si ingreso		
			${'I'.$rrr['tip_pag']} += abs($rrr['importe']);
		}elseif ($reg['tip_afe'] == 'E'){//si egreso	
			${'E'.$rrr['tip_pag']} += abs($rrr['importe']);
		}
	}
}
$pdf->Ln(2);
$pdf->Cell($tabRes,3,'',0,0,'C',0);
//$pdf->Cell(70,3,"",0,0,'R',0);
$pdf->Cell(25,3,"**SALDO FINAL**",0,1,'R',0);
$pdf->SetFont('Arial','BI',7);
$pdf->Cell($tabRes,3,'',0,0,'C',0);
$pdf->Cell(23,3,'Total Dia '.chfecha($fecCor,'Y-m-d','d/m/Y'),0,0,'L',0);
//$pdf->Cell(20,3,"",0,0,'R',0);
$pdf->Cell(23,3,number_format($sumParTipPag,2,'.',','),0,0,'R',0);
$pdf->Cell(23,3,number_format($sumParCor,2,'.',','),0,1,'R',0);
$pdf->Cell($tabRes,3,'',0,0,'C',0);
$pdf->SetFont('Arial','',7);
$pdf->Cell(70,3,"RESUMEN FORMAS DE PAGO",0,1,'C',0);
$arrForPag=$sumTotTipPag[$tipAfe];
$totParTipPag=0;
foreach($arrForPag as $idTipPag=>$arrDatTipPag){
	$pdf->Cell($tabRes,3,'',0,0,'C',0);
	$pdf->Cell(10,3,$idTipPag,0,0,'L',0);
	$pdf->Cell(35,3,$arrDatTipPag['desPag'],0,0,'L',0);
	$pdf->Cell(25,3,number_format($arrDatTipPag['totPag'],2,'.',','),0,1,'R',0);				
	$totParTipPag +=$arrDatTipPag['totPag'];
}
$pdf->SetFont('Arial','B',7);
$pdf->Cell($tabRes,3,'',0,0,'C',0);
$pdf->Cell(45,3,"SUMA: ",0,0,'R',0);
$pdf->Cell(25,3,number_format($totParTipPag,2,'.',','),0,1,'R',0);
$pdf->Output("CORTE_DE_CAJA.pdf","I");	
//Generar archivo pdf
?>