<?php

include('pdf/fpdf.php');
session_cache_limiter('private');
session_start();
$com = "I";
include("DMMYSQLG.php");
include("DSA3000.php");
include("DMMYSQLG.php");
require_once("DF80000.php"); 
class PDF extends FPDF
{
	//Cabecera de Página
	function Header()
	{
	    //Logotipo
	         include("DYIM_VER.php");
	    //Arial Bold (14)
	    $this->SetFont('Arial','B',11);
	    //Espacio
		
	    //Título
	    $this->Cell(250,3,'INVENTARIO - REPORTE CARDEX',0,1,'C');
		$this->SetFont('Arial','I',8);
		 $this->Cell(250,3,"(Entre el $_POST['fec_ini'] y $_POST['fec_fin'])",0,1,'C');
	    //Salto de línea
	    $this->Ln(3);
		$this->SetFont('Times','',7);
		$this->SetFillColor(192,192, 192);
		$this->Cell(8,5,'',0,0,'C');
		$this->Cell(15,5,'Fecha',1,0,'C',1);
		$this->Cell(10,5,'No. Parte',1,0,'C',1);
		$this->Cell(30,5,'Almacen',1,0,'C',1);
		$this->Cell(10,5,'Tip. Mov.',1,0,'C',1);
		$this->Cell(20,5,'No. Factura',1,0,'C',1);
		$this->Cell(20,5,'Pro. Clien.Cod.',1,0,'C',1);
		$this->Cell(20,5,'Ultimo costo',1,0,'C',1);
		$this->Cell(20,5,'Pre. Mov. Cos',1,0,'C',1);
		$this->Cell(20,5,'Exi. Inicial',1,0,'C',1);
		$this->Cell(20,5,'Mov. Entrada',1,0,'C',1);
		$this->Cell(20,5,'Mov. Salida',1,0,'C',1);
		$this->Cell(20,5,'Exi. Final ',1,0,'C',1);
		$this->Cell(20,5,'Folio Interno',1,0,'C',1);
		$this->Cell(20,5,'No. Poliza',1,1,'C',1);
	}
	//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('L','mm','A4');
$pdf->AddPage();

//se da formato a fechas
$fecha_inicial = chfecha($_POST['fec_ini'],'d/m/Y','Y-m-d');
$fecha_final = chfecha($_POST['fec_fin'],'d/m/Y','Y-m-d');
$dataBus=NULL;
if(trim($_POST['id_prod'])!="")
	$dataBus[]="nparte1='".$_POST['id_prod']."'";
$dataBus[]="fec_mov between '$fecha_inicial' AND '$fecha_final'";

$WHERE=implode(" AND ",$dataBus);
$sq="select * from dgcardex WHERE $WHERE order by nparte1,fec_mov,n_reg";
$sql = mysqli_query($GLOBALS['con'] ,$sq);
$idProdAct='-1';
$pdf->SetFillColor(248);
while(@$reg = mysqli_fetch_array($sql)){
		if($reg["nparte1"]!=$idProdAct){
			if($idProdAct!='-1'){
				$pdf->Cell(170,4,'MOVIMIENTOS: '.$conMov,'T',0,'C',0);
				$pdf->Cell(20,4,$canMovEntTot,'T',0,'C',0);
				$pdf->Cell(20,4,$canMovSalTot,'T',0,'C',0);
				$pdf->Cell(20,4,$exiIni,'T',0,'C',0);
				$pdf->Cell(20,4,'','T',0,'C',0);
				$pdf->Cell(20,4,'','T',1,'C',0);
				$conMov=0;
				$canMovEntTot=0;
				$canMovSalTot=0;
			}		
			$fecMovExp=explode("-",$reg['fec_mov']);
			$dt_invacuIni=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"SELECT dginvacu.*,farticulos.np_desc1 FROM dginvacu INNER JOIN farticulos ON(farticulos.nparte1=dginvacu.nparte1) WHERE farticulos.modulo='".$_SESSION['mod_sis']."' AND   dginvacu.axo='$fecMovExp[0]' AND dginvacu.mes='$fecMovExp[1]' AND dginvacu.nparte1 ='".$reg['nparte1']."'"));
			$dt_cardexAnt=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"SELECT SUM(mov_ent) as movEnt, SUM(mov_sal) as movSal FROM dgcardex WHERE nparte1='".$reg['nparte1']."' AND (fec_mov >='$fecMovExp[0]-$fecMovExp[1]-01' AND fec_mov <'$fecha_inicial')"));
			//echo "SELECT SUM(mov_ent) as movEnt, SUM(mov_sal) as movSal FROM dgcardex WHERE nparte1='".$reg['nparte1']."' AND (fec_mov >='$fecMovExp[0]-$fecMovExp[1]-01' AND fec_mov <'$fecha_inicial')";
			$exiIni=$dt_invacuIni['exi_ini']+$dt_cardexAnt['movEnt']-$dt_cardexAnt['movSal'];
			$pdf->Cell(0,3,$dt_invacuIni['nparte1'].'-'.$dt_invacuIni['np_desc1'],0,1,'C',1);
			$pdf->Cell(0,3,' EXISTENCIA INICIAL: '.$exiIni,0,1,'C',1);
			$idProdAct=$reg["nparte1"];
		}		
		$conMov++;
		$canMovEntTot +=$reg['mov_ent'];
		$canMovSalTot +=$reg['mov_sal'];
		$fec1 = $reg["fec_mov"];
		include('DF20000.php');
		$pdf->Cell(8,3,'',0,0,'C');
		$pdf->Cell(15,3,$f_f,0,0,'C');
		$pdf->Cell(10,3,$reg["nparte1"],0,0,'C');
		$pdf->Cell(30,3,$reg["almacen"],0,0,'C');
		$pdf->Cell(10,3,$reg["tip_mov"],0,0,'C');
		$bus=mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"select trefe from dgreqdev where fol_ven='".$reg["fol_recep"]."' "));
		$pdf->Cell(20,3,$bus["trefe"],0,0,'C');
		$pdf->Cell(20,3,$reg["codigo"],0,0,'C');
		$pdf->Cell(20,3,'$'.number_format($reg["cos_prom_ant"], 2, '.', ','),0,0,'R');
		$pdf->Cell(20,3,'$'.number_format($reg["cos_pro_mov"], 2, '.', ','),0,0,'R');
		$pdf->Cell(20,3,$exiIni,0,0,'C');
		$pdf->Cell(20,3,$reg["mov_ent"],0,0,'C');
		$pdf->Cell(20,3,$reg["mov_sal"],0,0,'C');
		$pdf->Cell(20,3,($exiIni +=$reg["mov_ent"]-$reg["mov_sal"]),0,0,'C');
		//sin campos
		$pdf->Cell(20,3,'',0,0,'C');
		$pdf->Cell(20,3,'',0,1,'C');
	
	}
$pdf->Cell(170,4,'MOVIMIENTOS: '.$conMov,'T',0,'C',0);
$pdf->Cell(20,4,$canMovEntTot,'T',0,'C',0);
$pdf->Cell(20,4,$canMovSalTot,'T',0,'C',0);
$pdf->Cell(20,4,$exiIni,'T',0,'C',0);
$pdf->Cell(20,4,'','T',0,'C',0);
$pdf->Cell(20,4,'','T',1,'C',0);	
$pdf->Output("Cardex.pdf","D");
?>