<?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");
	    //Arial Bold (14)
	    $this->SetFont('Arial','B',11);
	    //Espacio
		
	    //Título
	    $this->Cell(250,3,'REPORTE DEVOLUCION DE COMPRAS',0,1,'C');
	    //Salto de línea
	    $this->Ln(3);
		$this->SetFont('Times','',7);
		$this->SetFillColor(192,192, 192);
		$this->Cell(25,5,'',0,0,'C');
		$this->Cell(12,5,'Factura',1,0,'C',1);
		$this->Cell(20,5,'Num.Parte',1,0,'C',1);
		$this->Cell(70,5,'Descrip.',1,0,'C',1);
		$this->Cell(10,5,'Cantidad',1,0,'C',1);
		$this->Cell(17,5,'Precio Unita.',1,0,'C',1);
		$this->Cell(17,5,'Importe',1,0,'C',1);
		$this->Cell(17,5,'I.V.A',1,0,'C',1);
		$this->Cell(17,5,'Total Fac.',1,0,'C',1);
		$this->Cell(15,5,'Costo Unita.',1,0,'C',1);
		$this->Cell(17,5,'Costo Total',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();
$pdf->SetFillColor(230,230,230);
	$fec = $_POST['fecini'];
	include('DF20000.php');
		$fecha_inicial = $f_m;
		//$pdf->Cell(15,5,$fecha_inicial,1,0,'C');
	$fec = $_POST['fecfin'];
	include('DF20000.php');
		$fecha_final = $f_m;
		//$pdf->Cell(15,5,$fecha_final,1,0,'C');
	$sq_refcom=mysqli_query($GLOBALS['con'] ,$_POST['sbus']);
	while(@$dt_refcom = mysqli_fetch_array($sq_refcom)){
		if($dt_refcom["tip_mon"]!='1') $tipoCambio=$dt_refcom["tip_cam"];
		else
			$tipoCambio=1;
		if($ven_cor == '' || $ven_cor != $dt_refcom["tip_com"]){
			$ven_cor = $dt_refcom["tip_com"];
			@$dt_tipcom = mysqli_fetch_array(mysqli_query($GLOBALS['con'] ,"SELECT * FROM dgtipcom where tip_com = '$ven_cor'"));
		}
		if($fec_cor == '' || $fec_cor != $dt_refcom["tip_com"].$dt_refcom["fec_dev"]){
			$fec_cor = $dt_refcom["tip_com"].$dt_refcom["fec_dev"];		
			$fecone=chfecha($dt_refcom["fec_dev"],'Y-m-d','d/m/Y');
			$pdf->Cell(25,3,'',0,0,'C');
			$pdf->Cell(212,3,$dt_tipcom["des_tipcom"].' DEL : '. $fecone,0,1,'C',1);
		}
		//Si Detalle
		@$sql_fade=mysqli_query($GLOBALS['con'] ,"SELECT * FROM dgrefded where cons_dev = '".$dt_refcom['fol_recep']."'");
		while(@$reg_fade = mysqli_fetch_array($sql_fade)){
			$cos_tot += $reg_fade["cos_proact"] * $reg_fade["can_dev"];
			if($_POST['setipdoc']=='DETALLE'){
				$sql_dapo=mysqli_query($GLOBALS['con'] ,"SELECT * FROM dginvent WHERE dginvent.modulo='".$_SESSION['mod_sis']."' AND   nparte1 = '".$reg_fade['nparte1']."'");
				$reg_dapo=mysqli_fetch_array($sql_dapo);
				$pdf->Cell(25,3,'',0,0,'L');
				$pdf->Cell(17,3,$dt_refcom["fol_recep"],0,0,'C',0);
				$pdf->Cell(20,3,$reg_dapo["nparte1"],0,0,'L',0);
				$pdf->Cell(65,3,$reg_dapo["np_desc1"],0,0,'L',0);
				$pdf->Cell(10,3,$reg_fade["can_dev"],0,0,'L',0);
				$pdf->Cell(17,3,number_format($reg_fade["pu_ven"]*$tipoCambio, 4, '.', ','),0,0,'R',0);
				$pdf->Cell(17,3,number_format($reg_fade["subtotal"]*$tipoCambio, 2, '.', ','),0,0,'R',0);
				$pdf->Cell(17,3,number_format($reg_fade["iva_tot"]*$tipoCambio, 2, '.', ','),0,0,'R',0);
				$pdf->Cell(17,3,number_format($reg_fade["tot_dev"]*$tipoCambio, 2, '.', ','),0,0,'R',0);
				$pdf->Cell(17,3,number_format($reg_fade["cos_proact"]*$tipoCambio, 2, '.', ','),0,0,'R',0);
				$pdf->Cell(17,3,number_format($reg_fade["cos_proact"] * $reg_fade["can_dev"]*$tipoCambio, 2, '.', ','),0,1,'R',0);
			}
		}
		if($_POST['setipdoc']=='DETALLE'){
			$pdf->Cell(25,0.1,'',0,0,'C',0);
			$pdf->Cell(212,0.1,'',0,1,'C',1);
		}
		if ($dt_refcom['sub_dev']==0){
			$nom_cli = ' C   A   N   C   E   L   A   D   A ';
		}
		else{
			$sql_cli=mysqli_query($GLOBALS['con'] ,"SELECT * FROM dgcatpro where cod_pro = '".$dt_refcom['cod_pro']."'");
			$reg_cli=mysqli_fetch_array($sql_cli);
			$nom_cli = $reg_cli['nom_pro'].' '.$reg_cli['ap_pat_pro'].' '.$reg_cli['ap_mat_pro'];
		}
		$pdf->Cell(25,3,'',0,0,'C');
		$pdf->Cell(129,3,'FACTURA : '.$dt_refcom["fol_recep"].'  '.$nom_cli,0,0,'L',0);
		$pdf->Cell(17,3,number_format($dt_refcom["sub_tot"]*$tipoCambio, 2, '.', ',') ,0,0,'R',0);
		$pdf->Cell(17,3,number_format($dt_refcom["iva"]*$tipoCambio, 2, '.', ','),0,0,'R',0);
		$pdf->Cell(17,3,number_format($dt_refcom["totalf"]*$tipoCambio, 2, '.', ',') ,0,0,'R',0);
		$pdf->Cell(17,3,'',0,0,'L',0);
		$pdf->Cell(17,3,number_format($cos_tot*$tipoCambio, 2, '.', ','),0,1,'R',0);
		impLn();
		llenaTotalGeneral($dt_refcom,$sumTotGen,$cos_tot,$dt_tipcom);
		$cos_tot=0;
	}
	imp_totalGeneral($sumTotGen);

/**************** llena array total general *****************/
function llenaTotalGeneral($dt_factudmix,&$sumTotGen,$cos_tot,$dt_tipcom){
	if($dt_factudmix["tip_mon"]!='1') $tipoCambio=$dt_factudmix["tip_cam"];
	else $tipoCambio=1;
	$tipventa=$dt_factudmix["tip_com"];
	$sumTotGen[$tipventa]["descripcion"] = $dt_tipcom["des_tipcom"];
	$sumTotGen[$tipventa]["compra"] +=$dt_factudmix["sub_tot"]*$tipoCambio;
	$sumTotGen[$tipventa]["iva"] +=$dt_factudmix["iva"]*$tipoCambio;
	$sumTotGen[$tipventa]["costo"] +=$cos_tot*$tipoCambio;
}

/*********Funciones de Impresion de Datos**********/

function impLn(){
	global $pdf;
	$pdf->Ln(3);
}


function imp_totalGeneral($sumTotGen){
	global $pdf;
	$pdf->Cell(54.5,3,'',0,0,'C',0);
	$pdf->Cell(20,3,'CVE. ',1,0,'C',1);
	$pdf->Cell(70,3,'Descripcion',1,0,'C',1);
	$pdf->Cell(20,3,'Costo',1,0,'C',1);
	$pdf->Cell(20,3,'Compra',1,0,'C',1);
	$pdf->Cell(20,3,'I.V.A.',1,1,'C',1);
	while(@list($clave,$des) = each($sumTotGen)){
		$pdf->Cell(54.5,3,'',0,0,'C');
		$pdf->Cell(20,3,$clave,0,0,'C',0);
		$pdf->Cell(70,3,$des["descripcion"],0,0,'C',0);
		$pdf->Cell(20,3,number_format($des["costo"],2,'.',','),0,0,'R',0);
		$pdf->Cell(20,3,number_format($des["compra"],2,'.',','),0,0,'R',0);
		$pdf->Cell(20,3,number_format($des["iva"],2,'.',','),0,1,'R',0);
		$stot[0] +=$des["costo"];
		$stot[1] +=$des["compra"];
		$stot[2] +=$des["iva"];
	}
	$pdf->Cell(54.5,0.1,'',0,0,'C',0);
	$pdf->Cell(150,0.1,'',0,1,'C',1);
	$pdf->Cell(54.5,3,'',0,0,'C');
	$pdf->Cell(90,3,'Total:',0,0,'R',0);
	$pdf->Cell(20,3,number_format($stot[0],2,'.',','),0,0,'R',0);
	$pdf->Cell(20,3,number_format($stot[1],2,'.',','),0,0,'R',0);
	$pdf->Cell(20,3,number_format($stot[2],2,'.',','),0,1,'R',0);
}

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