<!DOCTYPE html>
<html lang="es-ES">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Pantalla Reporte Items</title>
<link href="../../maincss.css" rel="stylesheet" type="text/css" />
<link href="../../printcss.css" rel="stylesheet" type="text/css" media="print" />

</head>
<?php
include "../../conn.php";
include "../../general.php";

$rep=$_GET["rep"];
$tipo=$_GET["tipo"];
$familia=$_POST["familia"];
$subfamilia=$_POST["subfamilia"];
$grupo=$_POST["grupo"];
$subgrupo=$_POST["subgrupo"];
$item=$_POST["item"];
$tipoi=$_POST["tipoi"];
$orden=$_POST["orden"];
$usuario=$_POST["usuario"];

//$n_decimales = 6;

?>
<body>
<?php
if($familia == '') { $familia = 0;}
if($subfamilia == '') { $subfamilia = 0;}
if($grupo == '') { $grupo = 0;}
if($subgrupo == '') { $subgrupo = 0;}


//Inicio reporte items

if($rep == 13 && $tipo == 1) { // if 2

  	if ($item == 0) { // if 2.0
		$nom_item = 'Todos';
		$valor_item = ' ';
	} else {
		//nombre item
		$result2 = mysqli_query($link, "select pkyItem as cod_item, fldName as item
								from
								invItem 
								where
								pkyItem = '$item' and
								sysEnabled = 1 and sysAuthorized = 1;");
		while ($row = mysqli_fetch_array($result2)) {
			$nom_item = $row['item'];
		}
		mysqli_free_result($result2);
		$valor_item = ' a.pkyItem = ' .$item. ' and ';
	} //fin if 2.0
	
	if ($tipoi == 0) { // if 2.1
		$nom_tipo = 'Todos';
		$valor_tipo = ' ';
	} else {
		//nombre tipo
		$result3 = mysqli_query($link, "select pkyItemType as cod_tipo, fldName as tipo
								from
								invItemType 
								where
								pkyItemType = '$tipoi' and
								sysEnabled = 1 and sysAuthorized = 1;");
		while ($row = mysqli_fetch_array($result3)) {
			$nom_tipo = $row['tipo'];
		}
		mysqli_free_result($result3);
		$valor_tipo = ' a.codType = ' .$tipoi. ' and ';
	} //fin if 2.1
	
	if ($familia == 0) { // if 2.2
		$nom_familia = 'Todos';
		$valor_familia = ' ';
	} else {
		//nombre familia
		$result3 = mysqli_query($link, "select pkyItemCategory as cod, fldName as nombre
							from 
							invItemCategory
							where
							pkyItemCategory = '$familia' and 
							sysEnabled = 1 and sysAuthorized = 1;");
		while ($row = mysqli_fetch_array($result3)) {
			$nom_familia = $row['nombre'];
		}
		mysqli_free_result($result3);
		$valor_familia = ' a.codFamily = ' .$familia. ' and ';
	} //fin if 2.2
	
	if ($subfamilia == 0) { // if 2.3
		$nom_subfamilia = 'Todos';
		$valor_subfamilia = ' ';
	} else {
		//nombre sub familia
		$result4 = mysqli_query($link, "select pkyItemCategory as cod, fldName as nombre
							from 
							invItemCategory
							where
							pkyItemCategory = '$subfamilia' and 
							sysEnabled = 1 and sysAuthorized = 1;");
		while ($row = mysqli_fetch_array($result4)) {
			$nom_subfamilia = $row['nombre'];
		}
		mysqli_free_result($result4);
		$valor_subfamilia = ' a.codSubFamily = ' .$subfamilia. ' and ';
	} //fin if 2.3
	
	if ($grupo == 0) { // if 2.4
		$nom_grupo = 'Todos';
		$valor_grupo = ' ';
	} else {
		//nombre grupo
		$result5 = mysqli_query($link, "select pkyItemCategory as cod, fldName as nombre
							from 
							invItemCategory
							where
							pkyItemCategory = '$grupo' and 
							sysEnabled = 1 and sysAuthorized = 1;");
		while ($row = mysqli_fetch_array($result5)) {
			$nom_grupo = $row['nombre'];
		}
		mysqli_free_result($result5);
		$valor_grupo = ' a.codGroup = ' .$grupo. ' and ';
	} //fin if 2.4
	
	if ($subgrupo == 0) { // if 2.5
		$nom_subgrupo = 'Todos';
		$valor_subgrupo = ' ';
	} else {
		//nombre sub grupo
		$result6 = mysqli_query($link, "select pkyItemCategory as cod, fldName as nombre
							from 
							invItemCategory
							where
							pkyItemCategory = '$subgrupo' and 
							sysEnabled = 1 and sysAuthorized = 1;");
		while ($row = mysqli_fetch_array($result6)) {
			$nom_subgrupo = $row['nombre'];
		}
		mysqli_free_result($result6);
		$valor_subgrupo = ' a.codSubGroup = ' .$subgrupo. ' and ';
	} //fin if 2.5
	
	if ($orden == 0) { 
		$nom_orden = 'Item (Nombre)';
		$valor_orden = ' order by a.fldName ';
	}
	if ($orden == 1) {
		$nom_orden = 'Item (C&oacute;digo)';
		$valor_orden = ' order by a.pkyItem ';
	} 
	if ($orden == 2) {
		$nom_orden = 'Item (C&oacute;digo de F&aacute;brica)';
		$valor_orden = ' order by a.fldFactoryKey, a.pkyItem ';
	}
	if ($orden == 3) {
		$nom_orden = 'Familia (C&oacute;digo)';
		$valor_orden = ' order by a.codFamily, a.codSubFamily, a.codGroup, a.codSubGroup, a.pkyItem ';
	} 
	

//listado items
$result = mysqli_query($link, "select a.pkyItem as cod_item, a.fldName as item,
a.codType as cod_tipo, b.fldName as tipo, 
a.codFamily as cod_familia, c.fldName as familia, 
a.codSubFamily as cod_subf, 
a.codGroup as cod_grupo, 
a.codSubGroup as cod_subg,
g.fldName as u_mayor, h.fldName as u_menor,
a.fldStorageUnitCoefficient as coeficiente, 
i.fldName as marca, j.fldName as procedencia,
a.fldFactoryKey as cod_fabrica, a.fldAlternativeKey as cod_alter,
a.fldBarcode as cod_barras, a.fldMaximumStock as stock_max,
a.fldMinimumStock as stock_min, a.fldWarranty as garantia,
a.fldPackagedItemWeight as peso_emb, a.fldItemWeight as peso, 
k.fldName as u_peso, fldPackagedItemVolume as vol_emb, 
a.fldItemVolume as volumen, l.fldName as u_volumen, 
a.codBrandModel as cod_modelo
from
invItem a, invItemType b, invItemCategory c, 
invStorageUnit g, invStorageUnit h, 
invBrand i, regCountry j, srvMeasureUnit k, 
srvMeasureUnit l
where
$valor_item 
$valor_tipo 
$valor_familia 
$valor_subfamilia 
$valor_grupo
$valor_subgrupo 
a.codType = b.pkyItemType and 
a.codFamily = c.pkyItemCategory and
a.codMaximumStorageUnit = g.pkyStorageUnit and 
a.codMinimumStorageUnit = h.pkyStorageUnit and 
a.codBrand = i.pkyBrand and 
a.codSource = j.pkyCountry and 
a.codWeightMeasureUnit = k.pkyMeasureUnit and 
a.codVolumeMeasureUnit = l.pkyMeasureUnit and
a.sysEnabled = 1 and a.sysAuthorized = 1 
$valor_orden ;");

//listado categorias
$result2 = mysqli_query($link, "select pkyItemCategory as cod_cat, fldName as categoria,
fldLevel as nivel
from
invItemCategory
where
fldLevel <> 1 and 
sysEnabled = 1 and sysAuthorized = 1;");

$matriz_cat = array();
$i = 1;
while ($row = mysqli_fetch_array($result2)) {
	$matriz_cat [$i][1] = $row['cod_cat'];
	$matriz_cat [$i][2] = $row['categoria'];
	$matriz_cat [$i][3] = $row['nivel'];
	$i++;
}
$fin_cat = $i - 1;
mysqli_free_result($result2);

//listado modelos
$result3 = mysqli_query($link, "select pkyBrandModel as cod_modelo, codBrand as cod_marca,
fldName as modelo
from
invBrandModel
where
sysEnabled = 1 and sysAuthorized = 1 ;");

$matriz_mod = array();
$i = 1;
while ($row = mysqli_fetch_array($result3)) {
	$matriz_mod [$i][1] = $row['cod_modelo'];
	$matriz_mod [$i][2] = $row['cod_marca'];
	$matriz_mod [$i][3] = $row['modelo'];
	$i++;
}
$fin_modelo = $i - 1;
mysqli_free_result($result3);

$matriz_item = array();
$i = 1;
while ($row = mysqli_fetch_array($result)) {
	$matriz_item [$i][1] = $row['cod_item'];
	$matriz_item [$i][2] = $row['item'];
	$matriz_item [$i][3] = $row['cod_tipo'];
	$matriz_item [$i][4] = $row['tipo'];
	$matriz_item [$i][5] = $row['cod_familia'];
	$matriz_item [$i][6] = $row['familia'];
	$matriz_item [$i][7] = $row['cod_subf'];
	for($j = 1; $j <= $fin_cat; $j++) {
		if($row['cod_subf'] == $matriz_cat [$j][1]) {
			$matriz_item [$i][8] = $matriz_cat [$j][2];
		}
	}
	$matriz_item [$i][9] = $row['cod_grupo'];
	for($j = 1; $j <= $fin_cat; $j++) {
		if($row['cod_grupo'] == $matriz_cat [$j][1]) {
			$matriz_item [$i][10] = $matriz_cat [$j][2];
		}
	}
	$matriz_item [$i][11] = $row['cod_subg'];
	for($j = 1; $j <= $fin_cat; $j++) {
		if($row['cod_subg'] == $matriz_cat [$j][1]) {
			$matriz_item [$i][12] = $matriz_cat [$j][2];
		}
	}
	$matriz_item [$i][13] = $row['u_mayor'];
	$matriz_item [$i][14] = $row['u_menor'];
	$matriz_item [$i][15] = $row['coeficiente'];
	$matriz_item [$i][16] = $row['marca'];
	$matriz_item [$i][17] = $row['procedencia'];
	$matriz_item [$i][18] = $row['cod_fabrica'];
	$matriz_item [$i][19] = $row['cod_alter'];
	$matriz_item [$i][20] = $row['cod_barras'];
	$matriz_item [$i][21] = $row['stock_max'];
	$matriz_item [$i][22] = $row['stock_min'];
	$matriz_item [$i][23] = $row['garantia'];
	$matriz_item [$i][24] = $row['peso_emb'];
	$matriz_item [$i][25] = $row['peso'];
	$matriz_item [$i][26] = $row['vol_emb'];
	$matriz_item [$i][27] = $row['volumen'];
	$matriz_item [$i][28] = $row['u_volumen'];
	$matriz_item [$i][29] = $row['cod_modelo'];
	for($j = 1; $j <= $fin_modelo; $j++) {
		if($row['cod_modelo'] == $matriz_mod [$j][1]) {
			$matriz_item [$i][30] = $matriz_mod [$j][3];
		}
	}
	$i++;
}
$fin = $i - 1;
mysqli_free_result($result);


?>
<table border="0" align="left" width="100%" cellpadding="5">
<tr><td><?php echo $mostrar_logo_reportes; ?>
</td><td align="right"><b>Fecha: <?php echo date("Y-m-d"); ?><br/>Hora: <?php echo date("H:i:s"); ?><br/>Usuario: <?php echo $usuario; ?></b>
</td></tr></table>
<p><br /><center><font color="#000099" size="+1"><b>Listado de Item</b></font></center></p>
<table border="0" cellpadding="2" align="center" width="100%">
<tr>
<td>	
    <table border="1" align="center" cellpadding="2" width="100%">
        <TR> 
        <TD width="33%" colspan="2"><b>Item: </b><?php echo $nom_item; ?></TD>
        <TD width="33%"><b>Tipo: </b><?php echo $nom_tipo; ?></TD>
        <TD width="33%"><b>Ordenado por: </b><?php echo $nom_orden; ?></TD>
        </TR>
        <TR>
        <TD><b>Familia: </b><?php echo $nom_familia; ?></TD>
        <TD><b>Sub Familia: </b><?php echo $nom_subfamilia; ?></TD>
        <TD><b>Grupo: </b><?php echo $nom_grupo; ?></TD>
        <TD><b>Sub Grupo: </b><?php echo $nom_subgrupo; ?></TD>
        </TR>
    </table>

</td>
<!--
<td valign="top" width="150">

    <table border="1" align="center" cellpadding="2" width="100%">
    <tr>
    <TD><b>Tipo de Cambio:</b></TD>
    </tr><tr>
    <TD align="center"><a href="../../ModuleAccounting/VoucherExchangeRate/index.php?pkyExchangeRate=<?php echo $ultimo_tc; ?>&user=<?php echo $usuario; ?>" title="Tipo de Cambio"><?php echo $ultimo_tc; ?></a> (<?php echo $tc_fecha; ?> - <?php echo $tc_hora; ?>)<br/ >
        <table border="0" cellpadding="2" align="center">
            <?php
            for($m = 1; $m <= $fin_m; $m++) {
            printf("<tr><td align='left'>1 %s</td>", $ma_tc [$m][1]);//moneda base
            printf("<td align='center'>%s</td>", ' = ');//letra a
            printf("<td align='left'>%s %s</td>", number_format($ma_tc [$m][3],$n_tipocambio), $ma_tc [$m][2]);//moneda target y tasa
            //printf("<td align='right'>%s</td></tr>", number_format($ma_tc [$m][3],$n_tipocambio));//tasa
            }
            ?>
        </table>
    </TD>
    </tr>
	</table>
    
</td>
-->
</tr>
</table>
<br /> 
<table border="1" align="center" cellpadding="2" width="100%">
 	<thead>
        <TR> 
 		<TD align="center"><b>Cod.</b></TD>
        <TD align="center"><b>Cod. Fab.</b></TD>
        <TD align="center"><b>Item</b></TD>
        <TD align="center"><b>Familia</b></TD>
		<TD align="center"><b>Sub Familia</b></TD>
    	<TD align="center"><b>Grupo</b></TD>
        <TD align="center"><b>Sub Grupo</b></TD>
        <TD align="center"><b>Tipo</b></TD>
        <TD align="center"><b>U. May.</b></TD>
        <TD align="center"><b>U. Men.</b></TD>
        <TD align="center"><b>Coef.</b></TD>
        <TD align="center"><b>Marca</b></TD>
        <TD align="center"><b>Proc.</b></TD>
        <TD align="center"><b>Cod. Alt.</b></TD>
        <TD align="center"><b>Cod. Barr.</b></TD>
        <TD align="center"><b>S. Max.</b></TD>
        <TD align="center"><b>S. Min.</b></TD>
        <TD align="center"><b>Garant.</b></TD>
        <TD align="center"><b>P. Emb.</b></TD>
        <TD align="center"><b>Peso</b></TD>
        <TD align="center"><b>V. Emb.</b></TD>
        <TD align="center"><b>Vol.</b></TD>
        <TD align="center"><b>U. Vol.</b></TD>
        <TD align="center"><b>Mod.</b></TD>
        </TR>
	</thead>
    <tbody class="marcafila">
  <?php
  $color = 2;
     
  $j = 1;
  while ($j <= $fin) { //while
    if($color % 2 == 0) { //if color
	printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $matriz_item [$j][1]);//cod item
	} else {
	printf("<tr><td align='center'>%s</td>", $matriz_item [$j][1]);//cod item
	}//fin if color
	printf("<td align='right'>%s</td>", $matriz_item [$j][18]);//cod fabrica
	printf("<td align='left'>%s</td>", $matriz_item [$j][2]);//item
	printf("<td align='left'>%s</td>", $matriz_item [$j][6]);//familia
	printf("<td align='left'>%s</td>", $matriz_item [$j][8]);//subfamilia
	printf("<td align='left'>%s</td>", $matriz_item [$j][10]);//grupo
	printf("<td align='left'>%s</td>", $matriz_item [$j][12]);//subgrupo
	printf("<td align='left'>%s</td>", $matriz_item [$j][4]);//tipo
	printf("<td align='left'>%s</td>", $matriz_item [$j][13]);//unidad mayor
	printf("<td align='left'>%s</td>", $matriz_item [$j][14]);//unidad menor
	printf("<td align='right'>%s</td>", $matriz_item [$j][15]);//coeficiente
	printf("<td align='left'>%s</td>", $matriz_item [$j][16]);//marca
	printf("<td align='left'>%s</td>", $matriz_item [$j][17]);//procedencia
	printf("<td align='right'>%s</td>", $matriz_item [$j][19]);//cod alternativo
	printf("<td align='right'>%s</td>", $matriz_item [$j][20]);//cod barras
	printf("<td align='right'>%s</td>", number_format($matriz_item [$j][21],2));//stock max
	printf("<td align='right'>%s</td>", number_format($matriz_item [$j][22],2));//stock min
	printf("<td align='right'>%s</td>", $matriz_item [$j][23]);//garantia
	printf("<td align='right'>%s</td>", number_format($matriz_item [$j][24],2));//peso embalaje
	printf("<td align='right'>%s</td>", number_format($matriz_item [$j][25],2));//peso
	printf("<td align='right'>%s</td>", number_format($matriz_item [$j][26],2));//volumen embalaje
	printf("<td align='right'>%s</td>", number_format($matriz_item [$j][27],2));//volumen
	printf("<td align='left'>%s</td>", $matriz_item [$j][28]);//unidad volumen
	printf("<td align='left'>%s</td></tr>", $matriz_item [$j][30]);//modelo
	$color++;
	$j++;
  }//fin while
?>
<tr BGCOLOR='#ffcccc'><td colspan="23"><b>Total Items</b></td>
<td align="right"><?php printf("<b>%s</b>", $fin); ?></td></tr>
</tbody>
</table>
<?php
} //fin if 2

?>

</body>
</html>
