<?php
if($_POST['select']==2 && $_POST['r1']=='paso'){
    include("Directo.php");
}else{
include('pdf/fpdf.php');
session_cache_limiter('private');
session_start();
$com = "I";
include("DMMYSQLG.php");
include("DSA3000.php");
require_once("DF80000.php");
$a=0;

class PDF extends FPDF{
//Cabecera de página
	function Header(){
	    //Logo
	         include("DYIM_VER.php");
	    //Arial bold 14
		$this->SetFont('Arial','B',13);
		//Título
		$this->Cell(225,5,'SALIDAS DE ALMACEN',0,1,'C',0);
		//Salto de línea
		$this->SetFont('Arial','I',12);
		$this->Cell(55,5,'Periodo: '.$_POST['fec'].' al '.$_POST['fec2'].'',0,0,'C',0);
		
		$this->Ln(8);
		$this->SetFont('Arial','B',8);//establece tamaño de letra
		$this->SetFillColor(192,192,192);
		$this->Cell(1,5,'',0,0,'C',0);
//		$this->Cell(20,5,'REQUISICION',1,0,'C',0);
//		$this->Cell(60,5,'ARTICULO',1,0,'C',0);
//		$this->Cell(16,5,'CANTIDAD',1,0,'C',0);
//		$this->Cell(13,5,'UNIDAD',1,0,'C',0);
//		$this->cell(14,5,'PRECIO',1,0,'C',0);
//		$this->cell(18,5,'SUBTOTAL',1,0,'C',0);
//		$this->cell(18,5,'IVA',1,0,'C',0);
//		$this->Cell(17,5,'TOTAL',1,1,'C',0);
		$this->Cell(30,5,'ALMACEN / FECHA',1,0,'C',0);
                $this->Cell(50,5,'ARTICULO',1,0,'C',0);
                $this->Cell(20,5,'FACTURA',1,0,'C',0);
                $this->Cell(20,5,'DEPTO',1,0,'C',0);
                $this->Cell(20,5,'SALIDAS',1,0,'C',0);
                $this->Cell(20,5,'COSTOS',1,0,'C',0);
                $this->Cell(20,5,'P.P.',1,0,'C',0);
                $this->Cell(20,5,'TOTAL',1,1,'C',0);
                
	}	
	//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, $this->PageNo(),0,0,'C');
	}
}

$pdf=new PDF('P','mm','Letter');
$pdf->AddPage();

$pdf->SetFillColor(192,192,192);

$vpt=(isset($_POST['tbus']))?$_POST['tbus']:"";
$tbus=mysqli_real_escape_string($con,$vpt);
$fec = $_POST['fec'];
    include('DF20000.php');
    $fecha_inicial = $f_m;
    $fec = $_POST['fec2'];
    include('DF20000.php');
    $fecha_final = $f_m;
	
	$tipEst=$_POST['r1'];
	
//if($tipEst=="clave"){
//		
//	  	@$sql = mysqli_query($con ,"SELECT * FROM dgrequie where fec_ven between '$fecha_inicial' and '$fecha_final'");
	
         if($vpt=="" && $tipEst=="clave" && empty($_POST['departamento'])){
          
	  	@$sql = mysqli_query($con ,"SELECT * FROM dgrequie where fec_ven between '$fecha_inicial' and '$fecha_final'");		
        }else{
           
	  	@$sql = mysqli_query($con ,"SELECT * FROM dgrequie where fec_ven between '$fecha_inicial' and '$fecha_final' and tip_ven='".$_POST['departamento']."'");	
	}if($tipEst=="paso"){
	
		@$sql = mysqli_query($con ,"SELECT * FROM dgrequie where fec_ven between '$fecha_inicial' and '$fecha_final'");
	}

while($row = mysqli_fetch_array($sql)){
		 
	  $pdf->SetFont('Arial','B',8);
      $pdf-> Cell(5,5,'',0,0,'C',0);
	  if ($a>1){
	  $pdf-> Cell(245,0.3,'',0,1,'L',1);		
	  $pdf-> Cell(5,5,'',0,0,'C',0);
	  }
	  
	  $pdf-> Cell(20,5, '',10,0,'C',0);
	  
	  $tipVen=explode('.',$row['tip_ven']);
	  
	  $proveedor1=mysqli_fetch_array(mysqli_query($con ,"SELECT np_desc1 FROM fdepartamento where nparte1='$tipVen[0]'"));
        //  print_r($tipVen[0]);
	   $proveedor2=mysqli_fetch_array(mysqli_query($con ,"SELECT np_desc1 FROM fdepartamento where nparte1='$tipVen[1]'"));
	  
	  //$proveedor=mysqli_fetch_array(mysqli_query($con ,"SELECT np_desc1 FROM fdepartamento where nparte1='".$row['tip_ven']."'"));
	  $pdf-> Cell(20,5,  $proveedor1["np_desc1"] . "".($proveedor2['np_desc1']!=""?(" | ".$proveedor2['np_desc1']):""),10,1,'L',0);
	  
	  if ($_POST['r1']=="clave"){
	$dataBus=" dgrequid.cons_ven='".$row['fol_ven']."' ";
	
	}elseif($_POST['r1']=="paso"){
	$dataBus=" dgrequid.cons_ven='".$row['fol_ven']."' AND fagrupadores.paso='".$_POST['select']."'";
	}
	
	$WHERE= "WHERE".$dataBus."";
	
	$reqDed=mysqli_query($con ,"SELECT * FROM dgrequid 
	LEFT JOIN farticulos on farticulos.nparte1=dgrequid.nparte1
	LEFT JOIN fagrupadores on fagrupadores.np_desc1=farticulos.t8 
	$WHERE");
	$pdf->SetFont('Arial','',8);
	$totIVA=0;
	$totTOTAL=0;
	$totSUB=0;
	while($req = mysqli_fetch_array($reqDed))
	{
	   $pdf-> Cell(1,5,'',0,0,'C',0);
           $fec1=$row["fec_ven"];
		include('DF20000.php');
                $pdf-> Cell(30,5, $f_f,10,0,'C',0);
	    
          //  print_r(substr($row["folio_control"],0,18));
	     $articulo=mysqli_fetch_array(mysqli_query($con ,"SELECT nparte1,np_desc1,pais,cos_prom FROM farticulos WHERE nparte1='".$req['nparte1']."'"));
		  $medida=mysqli_fetch_array(mysqli_query($con ,"SELECT codigo FROM dgmedida WHERE nparte1='".$articulo['pais']."'")); 	
	       $pdf-> Cell(50,5, utf8_decode($articulo["nparte1"]." | ".$articulo["pais"]),10,0,'L',0);
	       $pdf-> Cell(20,5, substr($row["folio_control"],0,10),10,0,'C',0); 
              $pdf-> Cell(20,5, substr($row["tip_ven"],0,10),10,0,'C',0);
               $pdf-> Cell(20,5, number_format($req["can_ref"],2,'.',','),10,0,'R',0); 
	        // $pdf-> Cell(13,5, $medida["codigo"],10,0,'L',0); 
	          $pdf-> Cell(20,5, number_format($req["pre_uni"],2,'.',','),10,0,'R',0); 
		    $pdf-> Cell(20,5, number_format($articulo["cos_prom"],2,'.',','),10,0,'R',0); 
                  $pdf-> Cell(20,5, $req["subtotal"],10,1,'R',0); 
		        
		          //$pdf-> Cell(18,5, $req["total_cd"],10,1,'R',0); 
				 
	   $totsal+=$req["can_ref"];
	    //$totTOTAL+=$req["total_cd"];
           // print_r($req["subtotal"]);
	     $totSUB+=$req["subtotal"];
		 }
	  
//	    $pdf-> Cell(5,4,'',0,0,'C',0);
//	     $pdf-> Cell(123,4,'TOTALES :',1,0,'R',0);
//	      $pdf-> Cell(18,4,'$ '.number_format($totSUB,2,'.',','),1,0,'R',0);
//	       //$pdf-> Cell(12,4,'',1,0,'R',0);
//		    $pdf-> Cell(17,4,'$ '.number_format($totIVA,2,'.',','),1,0,'R',0);
//			 $pdf-> Cell(20,4,'$ '.number_format($totTOTAL,2,'.',','),1,1,'R',0);	 	 	 
	  
	@$rpo = mysqli_query($con ,"SELECT 
	          distinct(farticulos.sub) as almacen,
			  falmacen.np_desc1 as alm 
	           FROM 
	            dgrequid 
	             inner join farticulos on dgrequid.nparte1=farticulos.nparte1
				  inner join fagrupadores on fagrupadores.np_desc1=farticulos.t8
				   inner join falmacen on falmacen.nparte1=farticulos.sub  
	                $WHERE");
			         while($rows = mysqli_fetch_array($rpo))
			          {
						$pdf-> Cell(5,4,'',0,0,'C',0);
	                     $pdf-> Cell(56,4,$rows["alm"],1,0,'R',0);
			                 
			
			@$rpos = mysqli_query($con ,"SELECT 	
	         sum(dgrequid.subtotal) as suBtotal,
	          sum(dgrequid.des) as deScuento
	           FROM 
	            dgrequid 
	             inner join farticulos on dgrequid.nparte1=farticulos.nparte1
				  inner join fagrupadores on fagrupadores.np_desc1=farticulos.t8 
	              $WHERE AND farticulos.sub='".$rows['almacen']."'");
			         while($dfdsa=mysqli_fetch_array($rpos)){
			          $tcd2=$dfdsa["suBtotal"]-$dfdsa["deScuento"];
					   $pdf-> Cell(22,4,'$ '.number_format($tcd2,2,'.',','),1,1,'R',0);	
		}
    }
}
 $pdf-> Cell(1,4,'',0,0,'C',0);
	      $pdf-> Cell(10,4,'',0,0,'C',0);
            $pdf-> Cell(112,4,'TOTALES :',1,0,'R',0);
			  $pdf-> Cell(20,4,' '.number_format($totsal,2,'.',','),1,0,'C',0);
                         // $pdf-> Cell(20,4,' '.number_format($totCOSTO,2,'.',','),1,0,'C',0);
                        $pdf-> Cell(20,4,' ',1,0,'C',0);
                        $pdf-> Cell(20,4,' ',1,0,'C',0);
                         // $pdf-> Cell(18,4,' '.number_format($totCOSTO,2,'.',','),1,0,'R',0);
                          $pdf-> Cell(20,4,' $ '.number_format($totSUB,2,'.',','),1,1,'C',0);
$pdf->Output("Salidas_De_Almacen.pdf","D");
}
?> 