<?php
session_start();
require('../fpdf/fpdf.php');
require('plantilla_general.php');
require '../../../conexion.php';
require_once('../../../include/sesiones.php');
$ordenar=$_POST['ordenar'];
$familia=$_POST['familia'];
$marca=$_POST['marca'];

$pdf=new PDF_MC_Table();
$pdf->AddPage();
$pdf->SetFont('Arial','',14);
$pdf->AliasNbPages();
//Table with 20 rows and 4 columns

$pdf->SetFillColor(255,255,255);
$pdf->SetFont('Arial','B',8);
$pdf->Cell(20,6,'REF',1,0,'C',1);
$pdf->Cell(90,6,'NOMBRE',1,0,'C',1);
$pdf->Cell(18,6,'CANTIDAD',1,0,'C',1);
$pdf->Cell(28,6,'CANTIDAD REAL',1,0,'C',1);
$pdf->Cell(15,6,'PRECIO',1,0,'C',1);
$pdf->Cell(20,6,'NOVEDAD',1,0,'C',1);
$pdf->Ln();  
$pdf->SetFont('Arial','',7);

if ($ordenar=='familias_fam_id')
{
	$sqlf=mysqli_query($con,"SELECT * FROM familias where empresa='$empresa' ");
	while ($rowf=mysqli_fetch_array($sqlf))
	{
	    $fam_id=$rowf['fam_id'];
	    $fam_nombre=$rowf['fam_nombre'];
        
        $pdf->SetFillColor(255,255,0);
		$pdf->Cell(191,5,$fam_nombre,1,0,'C',1);
		$pdf->Ln();  

		if ($marca<>0 ) 
        {
            $query=mysqli_query($con,"SELECT * FROM articulos where familias_fam_id='$fam_id' and marca_mar_id='$marca' and empresa='$empresa' and art_tipo='articulo' and art_estado='Activo' ORDER BY $ordenar,art_referencia ASC ");
        }
        else
        {
            $query=mysqli_query($con,"SELECT * FROM articulos where familias_fam_id='$fam_id' and empresa='$empresa' and art_tipo='articulo' and art_estado='Activo' ORDER BY art_referencia ASC ");
        }

       

		
		while ($roww=mysqli_fetch_array($query))
		{
			$art_id=$roww['art_id'];
			$art_nombre=$roww['art_nombre'];

			$sql=mysqli_query($con,"SELECT * FROM articulos_has_bodegas where bodegas='$bod_id' and empresa='$empresa' and articulos_art_id='$art_id' ");
			while ($row=mysqli_fetch_array($sql))
			{
			    $art_cantidad=$row['art_cantidad'];
			    $atributo1=$row['atributo1'];
		    	$atributo2=$row['atributo2'];

			    $atri1=mysqli_query($con,"SELECT * FROM atributos1 where atri_art_id='$art_id' and atri_id='$atributo1'");
			    $atri1 = mysqli_fetch_array($atri1);
			    if (isset($atri1['atri_nombre'])) { 
			        $atri1_id=$atri1['atri_id']; 
			        $atri1=$atri1['atri_nombre']; 
			    } 
			    else {  
			        $atri1_id='0';
			        $atri1='';
			    }

			    $atri2=mysqli_query($con,"SELECT * FROM atributos2 where atri_art_id2='$art_id' and atri_id2='$atributo2'");
			    $atri2 = mysqli_fetch_array($atri2);
			    if (isset($atri2['atri_nombre2'])) { 
			        $atri2_id=$atri2['atri_id2'];  
			        $atri2=$atri2['atri_nombre2']; 
			    } else { 
			        $atri2_id='0';
			        $atri2='';
			    } 

			    $atri=': ('.$atri1.' - '.$atri2.')';
			    $atributos = str_replace ( "( - )", '', $atri);


				$pdf->SetWidths(array(20,90,18,28,15,20));
				srand(time()*1000000);
				$pdf->Row(array($roww['art_referencia'],utf8_decode($art_nombre.$atributos),$art_cantidad,'',number_format($roww['art_pvp'],0),''));
			}



$seriales = array();

$can1 = mysqli_query($con,"
    SELECT se_codigo 
    FROM seriales_articulos 
    WHERE articulos_art_id='$art_id' 
      AND estado='sin vender'
") or die(mysqli_error($con));

while ($roww = mysqli_fetch_array($can1)) {
    $codigo = trim($roww['se_codigo']);

    // Evita NULL, vacío, 0 o solo espacios
    if (!empty($codigo) && $codigo !== "0") {
        $seriales[] = $codigo;
    }
}


$pdf->SetWidths(array(191));
srand(time()*1000000);

$pdf->Row(array(implode("  -  ", $seriales)));




			
		}
	}

}
else
{
	if ($familia==0 && $marca==0 ) {
		$query=mysqli_query($con,"SELECT * FROM articulos where empresa='$empresa' and art_tipo='articulo' and art_estado='Activo' ORDER BY $ordenar,art_referencia ASC ");
	}

	else if ($familia==0 && $marca<>0 ) {
		$query=mysqli_query($con,"SELECT * FROM articulos where marca_mar_id='$marca' and empresa='$empresa' and art_tipo='articulo' and art_estado='Activo' ORDER BY $ordenar,art_referencia ASC ");
	}
	else if ($familia<>0 && $marca==0 ) {
		$query=mysqli_query($con,"SELECT * FROM articulos where familias_fam_id='$familia' and empresa='$empresa' and art_tipo='articulo' and art_estado='Activo' ORDER BY $ordenar,art_referencia ASC ");
	}
	else
	{
		$query=mysqli_query($con,"SELECT * FROM articulos where familias_fam_id='$familia' and marca_mar_id='$marca' and empresa='$empresa' and art_tipo='articulo' and art_estado='Activo' ORDER BY $ordenar,art_referencia ASC ");
	}
	while ($roww=mysqli_fetch_array($query))
	{
		$art_id=$roww['art_id'];
		$art_nombre=$roww['art_nombre'];


		$sql=mysqli_query($con,"SELECT * FROM articulos_has_bodegas where bodegas='$bod_id' and empresa='$empresa' and articulos_art_id='$art_id' ");
		while ($row=mysqli_fetch_array($sql))
		{
		    $art_cantidad=$row['art_cantidad'];
		    $atributo1=$row['atributo1'];
	    	$atributo2=$row['atributo2'];

		    $atri1=mysqli_query($con,"SELECT * FROM atributos1 where atri_art_id='$art_id' and atri_id='$atributo1'");
		    $atri1 = mysqli_fetch_array($atri1);
		    if (isset($atri1['atri_nombre'])) { 
		        $atri1_id=$atri1['atri_id']; 
		        $atri1=$atri1['atri_nombre']; 
		    } 
		    else {  
		        $atri1_id='0';
		        $atri1='';
		    }

		    $atri2=mysqli_query($con,"SELECT * FROM atributos2 where atri_art_id2='$art_id' and atri_id2='$atributo2'");
		    $atri2 = mysqli_fetch_array($atri2);
		    if (isset($atri2['atri_nombre2'])) { 
		        $atri2_id=$atri2['atri_id2'];  
		        $atri2=$atri2['atri_nombre2']; 
		    } else { 
		        $atri2_id='0';
		        $atri2='';
		    } 

		    $atri=': ('.$atri1.' - '.$atri2.')';
		    $atributos = str_replace ( "( - )", '', $atri);


			$pdf->SetWidths(array(20,90,18,28,15,20));
			srand(time()*1000000);
			$pdf->Row(array($roww['art_referencia'],utf8_decode($art_nombre.$atributos),$art_cantidad,'',number_format($roww['art_pvp'],0),''));
		}

$seriales = array(); 
 
$can1 = mysqli_query($con,"
    SELECT se_codigo
    FROM seriales_articulos
    WHERE articulos_art_id='$art_id'
      AND estado='sin vender'
") or die(mysqli_error($con));

while ($roww = mysqli_fetch_array($can1)) {
    $codigo = trim($roww['se_codigo']);

    // Evita NULL, vacío, 0 o solo espacios
    if (!empty($codigo) && $codigo !== "0") {
        $seriales[] = $codigo;
    }
}

$pdf->SetWidths(array(191));
srand(time()*1000000);

// SOLO imprimir si $seriales NO está vacío
if (!empty($seriales)) {
    $pdf->Row(array(implode('  -  ', $seriales)));
}

	}
}

$pdf->Output();
?>