<?php

require_once ("../../config/Cado.php");

require_once ("cPreciogestion.php");

$oInventarioinicial = new cPreciogestion();



require_once ("../categoria/cCategoria.php");

$oCategoria = new cCategoria();

require_once("../kardex/cKardex.php");

$oKardex = new cKardex();

require_once ("../producto/cPrecio.php");

$oPrecio = new cPrecio();

require_once ("../producto/cStock.php");

$oStock = new cStock();





require_once ("../formatos/formato.php");



if(isset($_POST['cmb_fil_cat_id']) and $_POST['cmb_fil_cat_id']>0)

{

	$cat_id=$_POST['cmb_fil_cat_id'].'';

	

	$dts2=$oCategoria->mostrar_por_idp($_POST['cmb_fil_cat_id']);

	$num_rows2= mysqli_num_rows($dts2);

	if($num_rows2>0){

		while($dt2 = mysqli_fetch_array($dts2)){

			

			$cat_id.=', '.$dt2['tb_categoria_id'];

			

			$dts3=$oCategoria->mostrar_por_idp($dt2['tb_categoria_id']);

			$num_rows3= mysqli_num_rows($dts3);

			if($num_rows3>0){

				while($dt3 = mysqli_fetch_array($dts3)){

					$cat_id.=', '.$dt3['tb_categoria_id'];			

				}

				mysqli_free_result($dts3);

			}//fin nivel 3



		}

		mysqli_free_result($dts2);

	}//fin nivel 2



//echo $cat_id;			

}



$arttip_id=1;



$dts1 = $oInventarioinicial->filtrar($_POST['txt_fil_pro_nom'],$_POST['txt_fil_pro_codbar'],$cat_id,$_POST['cmb_fil_mar_id'],$_POST['cmb_fil_pro_est'],$arttip_id);



$num_rows= mysqli_num_rows($dts1);





?>

<script type="text/javascript">

	function evaluar(idbtn)

	{



		if($("#stock_id"+idbtn).val() >0)

		{

			$("#btn_guardar"+idbtn).prop( "disabled", true );

			$("#btn_guardar"+idbtn).css({ opacity: 0.5 });

			$("#btn_guardar"+idbtn).prop('onclick',null);

			$("#stock_ini"+idbtn).prop('keydown',null);

			$("#costo_prod_id"+idbtn).prop('readonly', true);

			$("#stock_ini"+idbtn).prop('readonly', true);

			$("#stock_ini"+idbtn).css({"border": "none", "background-color": "transparent"}); 

			/*$("#btn_guardar"+idbtn).hide();*/

		}else{

			$("#stock_ini"+idbtn).keydown(function(e){if(e.which==13){guardar_stock_cantidad(idbtn);}});

		}





	}



	function guardar_stock_cantidad(idcat)

	{

		var obj_costo = eval('costo_prod_id' +idcat);

		var obj_stock = eval('stock_ini' +idcat);		

		var stock_ini = $(obj_stock).val();

		var costo_ini = $(obj_costo).val();

		var indexactual = $("#stock_ini"+idcat).data('index');



		if(stock_ini>0)

		{

			$.ajax({

				type: "POST",

				url: "inventarioinicial_reg.php",

				async : true,

				dataType: "json",

				data: ({

					action : 'guardar_stock_cantidad',				

					cat_id : idcat,

					alm_id: $("#cmb_fil_alm_id").val(),

					stock :stock_ini,

					costo:costo_ini,

					hdd_usu_id :$("#hdd_usu_id").val(),

					hdd_emp_id:$("#hdd_emp_id").val()

				}),

				beforeSend: function() {

					$("#td_fila_"+idcat).css({ opacity: 0.5 });

				},

				success: function(data)

				{

					if (data.okinsert=='Correcto')

					{

						$("#td_fila_"+idcat).html('<td align="left">'+data.nombre+'</td><td align="left">'+data.marca+'</td><td align="left">'+data.categoria+'</td><td align="left">'+data.unidadm+'</td><td align="left">'+data.codbar+'</td><td align="right">'+data.precos+'</td><td align="right">'+data.preven+'</td><td align="right">'+data.premin+'</td><td align="right">'+data.premay+'</td><td align="right"><input type="hidden"name="stock_id'+idcat+'" id="stock_id'+idcat+'" value="'+data.stock_num+'">'+data.stock_num+'</td><td align="right"><input type="hidden"  size="5" value="'+data.precos+'" name="costo_prod_id'+idcat+'" id="costo_prod_id'+idcat+'"><input style="text-align:right" data-index="'+indexactual+'" type="text"  size="7"  name="stock_ini'+idcat+'" id="stock_ini'+idcat+'" value="'+data.stock_kardex+'"><button onClick="guardar_stock_cantidad('+idcat+')" id="btn_guardar'+idcat+'"  class="btn_guardar_stock_cantidad" >GUARDAR</button></td>');

						$('.btn_guardar_stock_cantidad').button({

							icons: {primary: "ui-icon-plus"},

							text: false

						});

						evaluar(idcat);

						$("#td_fila_"+idcat).css({ opacity: 1 });

					//$("#stock_ini145").focus();return false;

					

					$("input[data-index='"+(parseInt(indexactual)+1)+"']").focus();return false;

					//inventarioinicial_tabla()

				}



				if (data.okinsert=='Error')

				{

					alert("Stock inicial ya registrado.");

					$("#td_fila_"+idcat).css({ opacity: 1 });

					inventarioinicial_tabla();

				}

			},

		});



		}

		else

		{

			alert("Ingrese costo y stock inicial.");

		}



	}



	$(document).ready(function() {



		$('.btn_editar').button({

			icons: {primary: "ui-icon-pencil"},

			text: false

		});

		$('.btn_imprimir_codbar').button({

			icons: {primary: "ui-icon-print"},

			text: false

		});

		$('.btn_guardar_stock_cantidad').button({

			icons: {primary: "ui-icon-plus"},

			text: false

		});

		$('.btn_eliminar').button({

			icons: {primary: "ui-icon-trash"},

			text: false

		});



		$.tablesorter.defaults.widgets = ['zebra'];

		$("#tabla_producto").tablesorter({ 

			headers: {

		//0: {sorter: 'true' },

		//7: {sorter: false }

	},

	sortList: [[0,0]]

});

	});

</script>



<table cellspacing="1" id="tabla_producto" class="tablesorter">

	<thead>

		<tr>

			<th>NOMBRE</th>

			<th>MARCA</th>

			<th>CATEGORIA</th>

			<th>UNIDAD</th>

			<th>CÓDIGO BARRAS</th>

			<th align="right">P. COSTO</th>

			<th align="right">P. VENTA</th>

			<th align="right">P. MIN</th>

			<th align="right">P. MAY</th>

			<th align="right">STOCK ACTUAL</th>

			<th nowroap align="center">STOCK INICIAL</th>

		</tr>

	</thead>

	<?php if($num_rows>=1){ 

		$indexfocus = 0;

		?>

	<tbody>	

		<?php while($dt1 = mysqli_fetch_array($dts1)) {



			$preMin = mysqli_fetch_array($oPrecio->consultar_precio('1', $dt1['tb_catalogo_id']))['tb_preciodetalle_val'];

			$preMay = mysqli_fetch_array($oPrecio->consultar_precio('2', $dt1['tb_catalogo_id']))['tb_preciodetalle_val'];

			?>



			<tr  id="td_fila_<?php echo $dt1['tb_catalogo_id']?>">



				<td><?php echo $dt1['tb_producto_nom']?></td>

				<td><?php echo $dt1['tb_marca_nom']?></td>

				<td><?php echo $dt1['tb_categoria_nom']?></td>

				<td><?php echo $dt1['tb_unidad_abr']?></td>

				<td><?php echo $dt1['tb_presentacion_codbar']?></td>

				<td align="right"><?php echo autonumeric($dt1['tb_catalogo_precos'])?></td>

				<td align="right"><?php echo autonumeric($dt1['tb_catalogo_preven'])?></td>

				<td align="right"><?php echo autonumeric($preMin) ?></td>

				<td align="right"><?php echo autonumeric($preMay) ?></td>

				<td id="td_stockactual_<?php echo $dt1['tb_catalogo_id']?>"  style="width:5%" align="right">

					<?php if($_POST['cmb_fil_alm_id']>0)echo $stock = mysqli_fetch_array($oStock->stock_por_presentacion($dt1['tb_presentacion_id'], $_POST['cmb_fil_alm_id']))['tb_stock_num'];?>

					<input type="hidden"name="stock_id<?php echo $dt1['tb_catalogo_id']?>" id="stock_id<?php echo $dt1['tb_catalogo_id']?>" value="<?php echo  $stock?>">



					

				</td>



				<?php 

				$stock_kardex = "";				

				$mod_id=11;



				$dts4 = $oKardex->obtener_stock_inicial($mod_id,$_POST['cmb_fil_alm_id'],$dt1['tb_catalogo_id']);

				while($dt4 = mysqli_fetch_array($dts4))

					$stock_kardex = $dt4['tb_kardexdetalle_can'];					

					mysqli_free_result($dts4);

					?>



					<td  id="td_stockini_<?php echo $dt1['tb_catalogo_id']?>" nowrap style="width:7%" align="right">



						<?php if($_POST['cmb_fil_alm_id']>0):?>

							<input type="hidden"  size="5" value="<?php echo $dt1['tb_catalogo_precos']?>" name="costo_prod_id<?php echo $dt1['tb_catalogo_id']?>" id="costo_prod_id<?php echo $dt1['tb_catalogo_id']?>">

							<input type="text" data-index="<?php echo $indexfocus ?>" style="text-align:right"  size="7" class="inputs" name="stock_ini<?php echo $dt1['tb_catalogo_id']?>" id="stock_ini<?php echo $dt1['tb_catalogo_id']?>" value="<?php echo $stock_kardex ?>">



							<button onClick="guardar_stock_cantidad(<?php echo $dt1['tb_catalogo_id']?>)" id="btn_guardar<?php echo $dt1['tb_catalogo_id']?>"  class="btn_guardar_stock_cantidad" >GUARDAR</button>					

							<script>						

								evaluar(<?php echo $dt1['tb_catalogo_id']?>);

							</script>

						<?php endif;?>



					</td>              

				</tr>





				<?php $indexfocus ++; } mysqli_free_result($dts1); ?>

			</tbody>

			<?php } ?>

			<tr class="even">

				<td colspan="13"><?php echo $num_rows.' registros'?></td>

			</tr>

		</table>