<?php
include ("../conexion.php");

 $cdgart   = strtoupper($_POST["cdgart"]);
 $cdgbar   = strtoupper($_POST["cdgbar"]);
$dscart   = strtoupper($_POST["dscart"]);
$accion   = $_POST["accion"];
$cantidad = $_POST["cantidad"];
$nomusu   = $_POST["nomusu"];

$cadena_busqueda=$_POST["cadena_busqueda"];
$fectom=date("Y-m-d");

$where="1=1";
if ($cdgart <> "") { $where.=" AND cdgart='$cdgart'"; }
if ($cdgbar <> "") { $where.=" AND cdgbar='$cdgbar'"; }


$where.=" ORDER BY numlin DESC";
	
	
 if ($result = $mysqli->query("SELECT * FROM toma_fisica  ORDER BY numlin DESC")) {
   $filas = $result->num_rows;
   $result->close();
   
	}

	$consulta = "SELECT  Sum(cantom) AS cantidad_toma FROM toma_fisica WHERE cdgart='$cdgart' or cdgbar='$cdgbar' ";
	$resultado = $mysqli->query($consulta);
	$row = $resultado->fetch_array();
	$cantidad_toma = $row["cantidad_toma"];	
	


	if ($accion==1){


       $sel_resultado = "SELECT * FROM web_1001 WHERE cdgart='$cdgart'   ";
       $result = $mysqli->query($sel_resultado);
       $row = $result->fetch_array();
     
		$cdgbar  =  $row["CDGBAR"];
		$dscart  =  $row["DSCART"];
		$cob_iv  =  $row["COB_IV"];
		$desart  =  $row["DESART"];
		$exiact  =  $row["EXIACT"];
		$cosart  =  $row["cosart"];
		$utiart1 =  $row["utiart1"];
		$pre001  =  $row["PRE001"];
		$impart  =  $row["impart"];
		$preivi  =  $row["PREIVI"];
		$cdgfam  =  $row["CDGFAM"];
		$cdglin  =  $row["CDGLIN"];
		$ivporc  =  $row["ivporc"];

$query_operacion="INSERT INTO toma_fisica (numlin, cdgart, cdgbar, dscart, cob_iv,  desart,  exiant, cosart, utiart1,  pre001, impart, preivi, cdgfam, cdglin, cdgusu, fectom, cantom , ivporc ) VALUES ('', '$cdgart', '$cdgbar', '$dscart', '$cob_iv','$desart','$exiact','$cosart', '$utiart1',  '$pre001', '$impart', '$preivi', '$cdgfam', '$cdglin',  '$nomusu', '$fectom' , '$cantidad' , '$ivporc' )";					
	$mysqli->query($query_operacion);

	if ($filas > 0){
		
		   $cantidad=$cantidad+$cantidad_toma;
	$sel_articulos="UPDATE web_1001 SET exiact='$cantidad' WHERE cdgart='$cdgart' or cdgbar='$cdgbar' ";
	$mysqli->query($sel_articulos);


}else{
	
		$sel_articulos="UPDATE web_1001 SET exiact='$cantidad' WHERE cdgart='$cdgart' or cdgbar='$cdgbar' ";
		$mysqli->query($sel_articulos);


}
}


?>
<html>
	<head>
		<title>Articulos</title>
		<link href="../estilos/estilos.css" type="text/css" rel="stylesheet">
		<script language="javascript">
		
		
		
		function eliminar_toma(numlin) {
			 if (confirm(" Desea borrar la toma fisica ? "))
			parent.location.href="eliminar_toma.php?numlin=" + numlin  + "&cadena_busqueda=<?php echo $cadena_busqueda?>";
		}

		function inicio() {
			var numfilas=document.getElementById("numfilas").value;
			var indi=parent.document.getElementById("iniciopagina").value;
			var contador=1;
			var indice=0;
			if (indi>numfilas) { 
				indi=1; 
			}
			parent.document.form_busqueda.filas.value=numfilas;
			parent.document.form_busqueda.paginas.innerHTML="";		
			while (contador<=numfilas) {
				texto=contador + "-" + parseInt(contador+100000);
				if (indi==contador) {
					parent.document.form_busqueda.paginas.options[indice]=new Option (texto,contador);
					parent.document.form_busqueda.paginas.options[indice].selected=true;
				} else {
					parent.document.form_busqueda.paginas.options[indice]=new Option (texto,contador);
				}
				indice++;
				contador=contador+10;
			}
		}
		</script>
	</head>

	<body onload=inicio()>	
		<div id="">
			<div id="zonaContenidoPP">
			<div align="center">
			<table class="fuente8" width="100%" cellspacing=0 cellpadding=0 border=0 ID="Table1">
			<input type="hidden" name="numfilas" id="numfilas" value="<?php echo $filas?>">
				<?php 
				
					if ($filas > 0) { 
					
	$consulta = "SELECT * FROM toma_fisica WHERE ".$where;

	if ($resultado = $mysqli->query($consulta)) 
    $contador=0;
 
    while ($fila = $resultado->fetch_assoc()) {
        $cdgart = $fila["CDGART"];
        $cdgbar = $fila["CDGBAR"];
        $dscart = $fila["DSCART"];
        $dscart = substr($dscart, 0, 60);  
			
	if ($contador % 2) { $fondolinea="itemParTabla"; } else { $fondolinea="itemImparTabla"; }?>
						<tr class="<?php echo $fondolinea?>">
                        <?php $fectom =  $fila["fectom"];
                        $fectom= implode("-", array_reverse(explode("-", $fectom))); ?>
                        
						<td  width="1%"></td>
						<td width="10%"><div align="left"><?php echo $cdgart?></div></td>
						<td width="15%"><div align="left"><?php echo $cdgbar?></div></td>
						<td class="aDerecha" width="34%"><div align="left"><?php echo $dscart?></div></td>
						<td   align="right" width="13%"><div align="right"><?php echo $fila["cantom"];?></div></td>
						<td class="aDerecha" width="13%"><div align="right"><?php echo $fectom?></div></td>
							<td width="6%"><div align="center"></div></td>
														<td width="4%"><div align="center"></div></td>
							<td width="6%"><div align="left"><a href="#"><img src="../img/Remove_Male_User_Icon_32.png" width="25" height="25" border="0" onClick="eliminar_toma(<?php echo $fila["numlin"]; ?>)" title="Eliminar Toma"></a></div></td>
						</tr>
						<?php $contador++;
							}
						?>			
					</table>
					<?php } else { ?>
					<table class="fuente8" width="87%" cellspacing=0 cellpadding=3 border=0>
						<tr>
							<td width="100%" class="mensaje"><?php echo "Articulo no posee ninguna toma fisica";?></td
					    ></tr>
					</table>					
					<?php } ?>					
				</div>
			</div>
		  </div>			
		</div>
	</body>
</html>
