<?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');
 $TotCargo2=0;
 $TotAbono2=0;
 $TotAbono=0;
 $TotCargo=0;
 
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		
	    $this->Cell(190,3,'REPORTE DPTAL DE SALIDA',0,1,'C');
		$this->SetFont('Arial','B',8);
		$this->Cell(190,3,'',0,1,'L');		
		$this->Cell(190,3,'PERIODO DEL '.$_POST['fec'].' AL '.$_POST['fec2'].'',0,1,'C');
		if($_POST['r1']!='T'){
		
			$sq_c = mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"SELECT valeryus09.descripcion,valeryus09.n_reg,valeryus09.cod_var,valeryus09.pmontomax FROM valeryus09 WHERE n_reg='".$_POST['id_ope']."'"));
		$this->Cell(190,3,'CARGO/ABONO: '.$sq_c['n_reg'].' | '.$sq_c['descripcion'].                        '',0,1,'C');	
		}else if($_POST['r1']=='T'){
		$this->Cell(190,3,'CARGO/ABONO: TODOS ',0,1,'C');	
		}	

		
	
	    //Salto de línea
	    $this->Ln(3);
	}
	//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('P','mm','Letter');
$pdf->AddPage();
$pdf->AliasNbPages();
$binit=-1;


$pdf->SetFillColor(240,240,240);
	$pdf->SetFont('ARIAL','B',8);
	$pdf->SetFillColor(230,230,230);
	$pdf->Cell(30,3,'',0,0,'C',0);
	$pdf->Cell(15,3,'CODIGO',0,0,'C',1);	
	$pdf->Cell(50,3,'DESCRIPCION',0,0,'C',1);
	$pdf->Cell(10,3,'',0,0,'C',1);
	$pdf->Cell(30,3,'CARGOS',0,0,'R',1);
	$pdf->Cell(30,3,'ABONOS',0,1,'R',1);


  if ($_POST['r1']=='N')
  $condicion='n_reg="'.$_POST['id_ope'].'"';
  else if ($_POST['r1']=='T')
  $condicion='n_reg > 0';
  else 
  $condicion='n_reg = 0';
  
 	$fecIni=chfecha($_POST['fec'],'d/m/Y','Y-m-d');
	$fecFin=chfecha($_POST['fec2'],'d/m/Y','Y-m-d');
	
  $sql_cb=mysqli_query($GLOBALS['con'] ,"select * from valeryus09 where $condicion and pmontomax='CARGO' order by descripcion ");
  while ($reg=mysqli_fetch_array($sql_cb)){
 	$pdf->SetFont('ARIAL','',7);
	$pdf->Cell(35,3,'',0,0,'C',0);
	$pdf->Cell(10,3,'      '.$reg['n_reg'],0,0,'L',0);	
	$pdf->Cell(50,3,'  '.$reg['descripcion'],0,0,'L',0);
	$pdf->Cell(10,3,' ',0,0,'C',0);

	$sql="select valeryus030.*,valeryus19.n_reg as folh from valeryus19 left join valeryus030 on (valeryus030.nparte1=valeryus19.n_reg) where valeryus030.nparte1_det='".$reg['n_reg']."' and (valeryus19.fecha_real between '".$fecIni."' and '".$fecFin."') ";					
	$sql_dt=mysqli_query($GLOBALS['con'] ,$sql);
	$cta1=0;
	$cargo1=0;
	$abono1=0;
	$cta=0;
	$numVen=0;
	while($red=mysqli_fetch_array($sql_dt)){
	$cta ++; 
		if(($red['cancelado'] == 0) || ($red['cancelado']==2)){  
		$cargo1 +=$red['costo'];
		$abono1 +=$red['total'];
		}
		
	}
		$pdf->Cell(30,3,' $'.number_format($cargo1,2,'.',','),0,0,'R',0);
	    $pdf->Cell(30,3,' $'.number_format($abono1,2,'.',','),0,1,'R',0);

		$numVen +=$cta;
		$TotCargo2 +=$cargo1;
		$TotAbono2 +=$abono1;
  }
  
  $pdf->SetFont('ARIAL','B',8);
	$pdf->SetFillColor(230,230,230);
	$pdf->Cell(35,3,'',0,0,'C',0);
	$pdf->Cell(10,3,'',0,0,'C',1);	
	$pdf->Cell(50,3,'TOTAL DE CARGO',0,0,'R',1);
	$pdf->Cell(10,3,'',0,0,'C',1);
	$pdf->Cell(30,3,' $'.number_format($TotCargo2,2,'.',','),0,0,'R',1);
	$pdf->Cell(30,3,' $'.number_format($TotAbono2,2,'.',','),0,1,'R',1);

$pdf->Cell(35,3,'',0,1,'C',0);
//ABONOS		
  $sql_cba=mysqli_query($GLOBALS['con'] ,"select * from valeryus09 where $condicion and pmontomax='ABONO' order by descripcion ");
  while ($rega=mysqli_fetch_array($sql_cba)){
 	$pdf->SetFont('ARIAL','',7);
	$pdf->Cell(35,3,'',0,0,'C',0);
	if(!isset($rega['n_reg']))$rega['n_reg']=0;
	$pdf->Cell(10,3,'      '.$rega['n_reg'],0,0,'L',0);	
	$pdf->Cell(50,3,'  '.$rega['descripcion'],0,0,'L',0);
	$pdf->Cell(10,3,' ',0,0,'C',0);
	
	$sql="select valeryus030.*,valeryus19.n_reg as folh from valeryus19 left join valeryus030 on (valeryus030.nparte1=valeryus19.n_reg) where valeryus030.nparte1_det='".$rega['n_reg']."' and (valeryus19.fecha_real between '".$fecIni."' and '".$fecFin."') ";						
	$sql_dt=mysqli_query($GLOBALS['con'] ,$sql);
	$cta=0;
	$cargo=0;
	$abono=0;
	while($red=mysqli_fetch_array($sql_dt)){
	$cta ++; 
		if(($red['cancelado'] == 0) || ($red['cancelado']==2)){  
		$cargo +=$red['costo'];
		$abono +=$red['total'];
		}
		
	}
	
		$pdf->Cell(30,3,' $'.number_format($cargo,2,'.',','),0,0,'R',0);
	    $pdf->Cell(30,3,' $'.number_format($abono,2,'.',','),0,1,'R',0);

		$numVen +=$cta;
		$TotCargo +=$cargo;
		$TotAbono +=$abono;

  }
   $pdf->SetFont('ARIAL','B',8);
	$pdf->SetFillColor(230,230,230);
	$pdf->Cell(35,3,'',0,0,'C',0);
	$pdf->Cell(10,3,'',0,0,'C',1);	
	$pdf->Cell(50,3,'TOTAL DE ABONO',0,0,'R',1);
	$pdf->Cell(10,3,'',0,0,'C',1);
	$pdf->Cell(30,3,' $'.number_format($TotCargo,2,'.',','),0,0,'R',1);
	$pdf->Cell(30,3,' $'.number_format($TotAbono,2,'.',','),0,1,'R',1);


$pdf->Output("RESUMEN DPTAL SALIDA.pdf","D");
?>


