<?php
//ini_set("display_errors",1);
session_start();
require_once ("../../config/Cado.php");
require_once ("cPreciogestion.php");
$oPreciogestion = new cPreciogestion();
require_once("../stock/cStock.php");
$oStock = new cStock();
require_once("../producto/cProductomarca.php");
$oProductomarca = new cProductomarca();
require_once("../producto/cProductomarcacodigo.php");
$oProductomarcacodigo = new cProductomarcacodigo();
require_once ("cst_producto.php");
require_once ("../formatos/formato.php");
$dat1 = $_POST['txt_fil_pro_dat1'];
$dat2 = $_POST['txt_fil_pro_dat2'];
$dat3 = $_POST['txt_fil_pro_dat3'];
$dat4 = $_POST['txt_fil_pro_dat4'];
$dts1=$oPreciogestion->mostrar_filtro($dat1,$dat2,$dat3,$dat4,$_POST['txt_fil_mar'],$_POST['cmb_fil_lim']);
$num_rows= mysqli_num_rows($dts1);
?>
<section class="content">
	<div class="container-fluid">
	<div class="row">
	    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
		    <div class="card">
				<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="padding:5px;">
	
					<div class="table-responsive">
					
					
		<table id="tabla_gestion_precios" class="table table-bordered table-striped">
		<thead>
		<tr>
			<th nowrap >NOMBRE</th>
			<th nowrap >MARCA</th>
			<th nowrap >PRESENTACION</th>
			<th nowrap >EMPAQUE</th>
			<th   align="right" nowrap >COD. BAR.</th>
			<th   align="right" nowrap >P. COSTO </th>
			<th   align="right" nowrap title="PRECIO DE VENTA">P. VENTA </th>
			<th   align="right" nowrap title="PRECIO DE VENTA F">P. VENTA F</th>
			<th  align="right">&nbsp;</th>
		</tr>
	</thead> 
		<tbody>
			<?php
			if($num_rows>=1){
			while($dt1 = mysqli_fetch_array($dts1)){
			
				
								
				?>
				<tr>
					<td>
						<span style="">
							<?php echo $dt1['tb_producto_nom']?>
						</span>
					</td>
					<td ><?php echo $dt1['tb_marca_nom']?></td>
					<td ><?php echo $dt1['tb_presentacion_nom']?></td>
					<td ><?php echo $dt1['tb_empaque_nom']?></td>
					
                    <td align="right">
					<input placeholder="Cod. Bar." readonly 
					onclick="editarprecos('<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>',3)"
					data-promarid ="<?php echo $dt1['tb_productomarca_id']?>"
					name="txt_cat_codbar_<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>" type="text" 
					id="txt_cat_codbar_<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>" 
					value="<?php echo $dt1['tb_productomarca_codbar'] ?>" 
					size="6" style="text-align:right" >
					</td>
					<td align="right">
					<input placeholder="Precio Costo" readonly 
					onclick="editarprecos('<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>',1)"
					data-promarid ="<?php echo $dt1['tb_productomarca_id']?>"
					name="txt_cat_precos_<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>" type="text" 
					id="txt_cat_precos_<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>" class="moneda_c" 
					value="<?php if($dt1['tb_productomarca_precos']!=0) echo formato_money($dt1['tb_productomarca_precos'])?>" 
					size="6" maxlength="8" style="text-align:right" >
					</td>
                    <td align="right" nowrap="nowrap"><?php //if($dt1['tb_catalogo_preven']!=0)echo formato_money($dt1['tb_catalogo_preven'])?>
                    <input placeholder="Precio Venta"  onclick="editarprecos(<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>,2)" readonly 
					data-promarid ="<?php echo $dt1['tb_productomarca_id']?>"
					name="txt_cat_preven_<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>" type="text" 
					id="txt_cat_preven_<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>" 
					class="moneda_cp" value="<?php if($dt1['tb_productomarca_preven']!=0) echo formato_money($dt1['tb_productomarca_preven'])?>" size="7" 
					maxlength="8" style="text-align:right">
					</td>
					
					
					<td align="right" nowrap="nowrap"><?php //if($dt1['tb_catalogo_preven']!=0)echo formato_money($dt1['tb_catalogo_preven'])?>
                    <input placeholder="Precio Venta F"  onclick="editarprecos(<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>,4)" readonly 
					data-promarid ="<?php echo $dt1['tb_productomarca_id']?>"
					name="txt_cat_prevenf_<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>" type="text" 
					id="txt_cat_prevenf_<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>" 
					class="moneda_cp" value="<?php if($dt1['tb_productomarca_prevenf']!=0) echo formato_money($dt1['tb_productomarca_prevenf'])?>" size="7" 
					maxlength="8" style="text-align:right">
					</td>
					
   
                    <td align="right" nowrap>
                              		
						<small class="badge badge-success" style="cursor:pointer;display:none;" id="lbl_sto_<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>" >DISPONIBLE</small>
                          
                        <span class="btn btn-primary btn-sm" onClick="actualizar_precio('<?php echo $dt1['tb_catalogo_id']?>','<?php echo $dt1['tb_marca_id']?>')"> <i class="fa fa-save"></i> </span>
                           
					</td>
                        <script>     
							$("#txt_cat_prevenf_<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>,#txt_cat_preven_<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>,#txt_cat_precos_<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>,#txt_cat_codbar_<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>").blur(function(e){
                        
								quitareditar('<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>');
							});
						
                        	$("#txt_cat_precos_<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>").keydown(function(e){
                        		if (e.keyCode == 13){
									
									
                        			codbar = $("#txt_cat_codbar_<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>").val();
									promarid = $(this).attr('data-promarid');
									verificar_codbar(promarid,codbar,'<?php echo $dt1["tb_catalogo_id"]?>','<?php echo $dt1['tb_marca_id']?>');
									
									return false;
								}
                        		if (e.keyCode == 27)
                        		{
                        			quitareditar('<?php echo $dt1["tb_catalogo_id"].$dt1['tb_marca_id']?>');
                        		}
                        		});
								
                        	$("#txt_cat_preven_<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>").keydown(function(e){
                        		if (e.keyCode == 13){
									
									codbar = $("#txt_cat_codbar_<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>").val();
									promarid = $(this).attr('data-promarid');
									verificar_codbar(promarid,codbar,'<?php echo $dt1["tb_catalogo_id"]?>','<?php echo $dt1['tb_marca_id']?>');
				
                        			return false;
								}
                        		if (e.keyCode == 27)
                        		{
                        			quitareditar('<?php echo $dt1["tb_catalogo_id"].$dt1['tb_marca_id']?>');
                        		}
                        		});
								
								
							$("#txt_cat_prevenf_<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>").keydown(function(e){
                        		if (e.keyCode == 13){
									
									codbar = $("#txt_cat_codbar_<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>").val();
									promarid = $(this).attr('data-promarid');
									verificar_codbar(promarid,codbar,'<?php echo $dt1["tb_catalogo_id"]?>','<?php echo $dt1['tb_marca_id']?>');
				
                        			return false;
								}
                        		if (e.keyCode == 27)
                        		{
                        			quitareditar('<?php echo $dt1["tb_catalogo_id"].$dt1['tb_marca_id']?>');
                        		}
                        		});
								
								
								
							$("#txt_cat_codbar_<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>").keydown(function(e){
                        		if (e.keyCode == 13){
									promarid = $(this).attr('data-promarid');				
									verificar_codbar(promarid,$(this).val(),'<?php echo $dt1["tb_catalogo_id"]?>','<?php echo $dt1['tb_marca_id']?>');
                        			return false;
								}
                        		if (e.keyCode == 27)
                        		{
                        			quitareditar('<?php echo $dt1["tb_catalogo_id"].$dt1['tb_marca_id']?>');
                        		}
                        		});
                        </script>                    
                    </tr>
                    <?php
			
			}
                mysqli_free_result($dts1);
                  }
                ?>
            </tbody> 
		</table>
					
					
					</div>
				</div>
			</div>
			</div>
		</div>
		</div>
</section>
<style>
#tabla_gestion_precios td{
		vertical-align: middle;
		padding: 4px 4px !important;
	}
	#tabla_gestion_precios td.editando{
			background-color: rgba(18, 180, 243, 0.05);
			outline: 2px solid #35A4F3;
	}
	#tabla_gestion_precios td input{
		height: 100%;
		width: 100%;
		box-sizing: border-box;
		display: inline-block;
		border: none;
		background: transparent;
		color:#FF6F00;
	}
	#tabla_gestion_precios td input:focus{
		background: transparent;
			height: 100%;
			width: 100%;
			outline: none;
			border: none;
	}
	#tabla_gestion_precios td input[readonly]{
		background: transparent;
	}
 
	</style>
	<script type="text/javascript">
		$(function() {  
		/*	$("#tabla_gestion_precios").DataTable( {
				"columnDefs": [
				{ "orderable": false, "targets": [2] }
				],
				"searching": false,
				"order":[[1,"asc"]]
			});
		}); */
})
function editarprecos(id,foco){
	var $td = $("#txt_cat_precos_"+id).parent().addClass('editando');
	var $td = $("#txt_cat_preven_"+id).parent().addClass('editando');
	var $td = $("#txt_cat_prevenf_"+id).parent().addClass('editando');
	var $td = $("#txt_cat_codbar_"+id).parent().addClass('editando');
	
	$("#txt_cat_precos_"+id).removeAttr('readonly');
	$("#txt_cat_preven_"+id).removeAttr('readonly');
	$("#txt_cat_prevenf_"+id).removeAttr('readonly');
	$("#txt_cat_codbar_"+id).removeAttr('readonly');
	
	switch(foco) {
		case 1:
		$("#txt_cat_precos_"+id).focus().select();
		break;
		case 2:
		$("#txt_cat_preven_"+id).focus().select();
		break;
		case 3:
		$("#txt_cat_codbar_"+id).focus().select();
		break;
		case 4:
		$("#txt_cat_prevenf_"+id).focus().select();
		break;
		
	}
	
}
function quitareditar(id){
	$("#txt_cat_precos_"+id).attr('readonly', 'true');
	$("#txt_cat_preven_"+id).attr('readonly', 'true');
	$("#txt_cat_prevenf_"+id).attr('readonly', 'true');
	$("#txt_cat_codbar_"+id).attr('readonly', 'true');
	
	$("#txt_cat_precos_"+id).parent().removeClass('editando');
	$("#txt_cat_preven_"+id).parent().removeClass('editando');
	$("#txt_cat_prevenf_"+id).parent().removeClass('editando');
	$("#txt_cat_codbar_"+id).parent().removeClass('editando');
}

$('.cantidad').autoNumeric({
		aSep: '',
		aDec: '.',
		vMin: '0',
		vMax: '99999'
	});
	$('.moneda_cp').autoNumeric({
		aSep: ',',
		aDec: '.',
	//aSign: 'S/. ',
	//pSign: 's',
	vMin: '0.00',
	vMax: '99999.99'
});
	$('.porcentaje').autoNumeric({
		aSep: ',',
		aDec: '.',
	//aSign: 'S/. ',
	//pSign: 's',
	vMin: '0.00',
	vMax: '99.99'
});
function actualizar_precio(catid,marid)
{
		idf = catid+''+marid;
		
		$('#lbl_sto_'+idf).hide();
	
		var precos	=parseFloat($("#txt_cat_precos_"+idf).val());
		var preven	=parseFloat($("#txt_cat_preven_"+idf).val());
		
		if(preven>0 || precos>0){
			
			if(precos>=0){
			if(preven>precos){
				
		$.ajax({
		type: "POST",
		url: "../preciogestion/precio_reg.php",
		async:true,
		dataType: "html",                      
		data: ({
			action:	'actualizar_precio_panel',
			cat_id: catid,				
			mar_id: marid,				
			cat_precos: $("#txt_cat_precos_"+idf).val(),
			cat_preven: $("#txt_cat_preven_"+idf).val(),		
			cat_prevenf: $("#txt_cat_prevenf_"+idf).val(),		
			cat_codbar: $("#txt_cat_codbar_"+idf).val()			
		}),
		beforeSend: function() {
			$('#lbl_sto_'+idf).html('G...');
			$('#lbl_sto_'+idf).show(100);
				//$('#div_catalogo_filtro').html('Cargando <img src="images/loadingf11.gif" align="absmiddle"/>');
			},
			success: function(html){
				$('#lbl_sto_'+idf).html(html);
				//$('#div_catalogo_filtro').html(html);
			},
			complete: function(){
				//$('#lbl_sto_'+idf).hide();
				//catalogo_tabla();
			}
		});
	quitareditar(idf);
				
			}else{
					toastr["error"]('Precio de Venta debe ser mayor que Precio Costo.');	
			}
			}else{
				
				$.ajax({
		type: "POST",
		url: "../preciogestion/precio_reg.php",
		async:true,
		dataType: "html",                      
		data: ({
			action:	'actualizar_precio_panel',
			cat_id: catid,				
			mar_id: marid,				
			cat_precos: $("#txt_cat_precos_"+idf).val(),
			cat_preven: $("#txt_cat_preven_"+idf).val(),		
			cat_prevenf: $("#txt_cat_prevenf_"+idf).val(),		
			cat_codbar: $("#txt_cat_codbar_"+idf).val()			
		}),
		beforeSend: function() {
			$('#lbl_sto_'+idf).html('G...');
			$('#lbl_sto_'+idf).show(100);
				//$('#div_catalogo_filtro').html('Cargando <img src="images/loadingf11.gif" align="absmiddle"/>');
			},
			success: function(html){
				$('#lbl_sto_'+idf).html(html);
				//$('#div_catalogo_filtro').html(html);
			},
			complete: function(){
				//$('#lbl_sto_'+idf).hide();
				//catalogo_tabla();
			}
		});
	quitareditar(idf);
				
			}
			
			}else{
				
	$.ajax({
		type: "POST",
		url: "../preciogestion/precio_reg.php",
		async:true,
		dataType: "html",                      
		data: ({
			action:	'actualizar_precio_panel',
			cat_id: catid,				
			mar_id: marid,				
			cat_precos: $("#txt_cat_precos_"+idf).val(),
			cat_preven: $("#txt_cat_preven_"+idf).val(),
			cat_prevenf: $("#txt_cat_prevenf_"+idf).val(),
			cat_codbar: $("#txt_cat_codbar_"+idf).val()				
		}),
		beforeSend: function() {
			$('#lbl_sto_'+idf).html('G...');
			$('#lbl_sto_'+idf).show(100);
				//$('#div_catalogo_filtro').html('Cargando <img src="images/loadingf11.gif" align="absmiddle"/>');
			},
			success: function(html){
				$('#lbl_sto_'+idf).html(html);
				//$('#div_catalogo_filtro').html(html);
			},
			complete: function(){
				//$('#lbl_sto_'+idf).hide();
				//catalogo_tabla();
			}
		});
	quitareditar(idf);
}
}
function actualizar_precio_unit(catid,marid)
{
	idf = catid+''+marid;
	$('#lbl_sto_'+idf).hide();
	
		var precos	=parseFloat($("#txt_cat_precos_"+idf).val());
		var preven	=parseFloat($("#txt_cat_preven_"+idf).val());
		if(preven>0 || precos>0){
			if(precos>=0){
			if(preven>precos){
				
				$.ajax({
		type: "POST",
		url: "../preciogestion/precio_reg.php",
		async:true,
		dataType: "html",                      
		data: ({
			action:	'actualizar_precio_panel',
			cat_id: catid,				
			mar_id: marid,				
			cat_precos: $("#txt_cat_precos_"+idf).val(),
			cat_preven: $("#txt_cat_preven_"+idf).val(),
			cat_prevenf: $("#txt_cat_prevenf_"+idf).val(),
			cat_codbar: $("#txt_cat_codbar_"+idf).val()
		}),
		beforeSend: function() {
			$('#lbl_sto_'+idf).html('G...');
			$('#lbl_sto_'+idf).show(100);
				//$('#div_catalogo_filtro').html('Cargando <img src="images/loadingf11.gif" align="absmiddle"/>');
			},
			success: function(html){
				$('#lbl_sto_'+idf).html(html);
				//$('#div_catalogo_filtro').html(html);
			},
			complete: function(){
				setTimeout(function() {$('#lbl_sto_'+idf).hide(100);$('#lbl_sto_'+idf).html('');}, 2000);
				
			}
		});
		quitareditar(idf);
			}else{
				toastr["error"]('Precio de Venta debe ser mayor que Precio Costo.');	
			}
			
			
			}else{
				
				$.ajax({
		type: "POST",
		url: "../preciogestion/precio_reg.php",
		async:true,
		dataType: "html",                      
		data: ({
			action:	'actualizar_precio_panel',
			cat_id: catid,				
			mar_id: marid,				
			cat_precos: $("#txt_cat_precos_"+idf).val(),
			cat_preven: $("#txt_cat_preven_"+idf).val(),
			cat_prevenf: $("#txt_cat_prevenf_"+idf).val(),
			cat_codbar: $("#txt_cat_codbar_"+idf).val()
		}),
		beforeSend: function() {
			$('#lbl_sto_'+idf).html('G...');
			$('#lbl_sto_'+idf).show(100);
				//$('#div_catalogo_filtro').html('Cargando <img src="images/loadingf11.gif" align="absmiddle"/>');
			},
			success: function(html){
				$('#lbl_sto_'+idf).html(html);
				//$('#div_catalogo_filtro').html(html);
			},
			complete: function(){
				setTimeout(function() {$('#lbl_sto_'+idf).hide(100);$('#lbl_sto_'+idf).html('');}, 2000);
				
			}
		});
		quitareditar(idf);
				
			}
		}else{
			
		$.ajax({
		type: "POST",
		url: "../preciogestion/precio_reg.php",
		async:true,
		dataType: "html",                      
		data: ({
			action:	'actualizar_precio_panel',
			cat_id: catid,				
			mar_id: marid,				
			cat_precos: $("#txt_cat_precos_"+idf).val(),
			cat_preven: $("#txt_cat_preven_"+idf).val(),
			cat_prevenf: $("#txt_cat_prevenf_"+idf).val(),
			cat_codbar: $("#txt_cat_codbar_"+idf).val()
		}),
		beforeSend: function() {
			$('#lbl_sto_'+idf).html('G...');
			$('#lbl_sto_'+idf).show(100);
				//$('#div_catalogo_filtro').html('Cargando <img src="images/loadingf11.gif" align="absmiddle"/>');
			},
			success: function(html){
				$('#lbl_sto_'+idf).html(html);
				//$('#div_catalogo_filtro').html(html);
			},
			complete: function(){
				setTimeout(function() {$('#lbl_sto_'+idf).hide(100);$('#lbl_sto_'+idf).html('');}, 2000);
				
			}
		});
		quitareditar(idf);
		}
}
function verificar_codbar(promarid,cod_bar,catid,marid){
		$.ajax({
			type : "POST",
			url : "../producto/producto_reg.php",
			async : false,
			dataType : "json",
			data: ({
				action:'verificar_codbar',
				codbar: cod_bar,
				promar_id:promarid
			}),
			beforeSend: function(){
				toastr["warning"]('Verificando Existencia de Código de Barras...');
			},
			success: function(data){
				if(data.pro_est==1)
				{
					$('#txt_cat_codbar_'+catid+''+marid).val('');
					toastr["error"](data.pro_msj);
				}
				else
				{					
					toastr["success"](data.pro_msj);
					$("#txt_cat_precos_"+catid+''+marid).focus(); 
					
                    actualizar_precio_unit(catid,marid);
					
				}
			}
		});
}
	
	</script> 