<?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/cProducto.php");
$oProducto = new cProducto();

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['txt_fil_pro_codbar'],$_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 ></th>
			<th nowrap >COD. BAR.</th>
			<th nowrap >NOMBRE</th>
			<th nowrap >MARCA</th>
			<th >UNIDAD</th>  
			<th class="d-none"  align="right" nowrap >COD. BAR.</th>
			<th   align="right" nowrap title="PRECIO DE VENTA">PRECIO VENTA S/</th>
			<th  align="right">&nbsp;</th>
		</tr>
	</thead> 
		<tbody>
			<?php
			if($num_rows>=1){
				
			while($dt = mysqli_fetch_array($dts1)){
					$fam  = '';
				
						$dtsfam = $oProducto->mostrarFamilia($dt['tb_catalogo_id'],'');
							
							while($dtf = mysqli_fetch_array($dtsfam)) {
							$fam .= '<span style="font-size:12px;" class="badge badge-info badge-sm">'.$dtf['tb_familia_nom'].'</span>';
							}
							
					$preven = '';	
					$prepromar_id = '';
					
					$dtspre = $oPreciogestion->mostrar_precioxpuntoventa($dt['tb_productomarca_id'],$_POST['cmb_fil_puntoventa_id']);		
					$dtp = mysqli_fetch_array($dtspre);
					$preven = $dtp['tb_precioproductomarca_preven'];
					$prepromar_id = $dtp['tb_precioproductomarca_id'];
					
			
				?>
				<tr>
					<td>
						
							<?php echo $fam;?>
						
					</td>
					<td>
						<span style="">
							<?php echo $dt['tb_productomarca_codbar']?>
						</span>
					</td>
					<td>
						<span style="">
							<?php echo $dt['tb_producto_nom']?>
						</span>
					</td>
					<td ><?php echo $dt['tb_marca_nom']?></td>
					<td >
						<span style="">
							<?php echo $dt['tb_unidad_abr']?>
						</span>
					</td>
					
                    <td class="d-none" align="right">
					<input placeholder="Cod. Bar." readonly 
					onclick="editarprecos('<?php echo $dt['tb_catalogo_id'].$dt['tb_marca_id']?>',3)"
					data-promarid ="<?php echo $dt['tb_productomarca_id']?>"
					name="txt_cat_codbar_<?php echo $dt['tb_catalogo_id'].$dt['tb_marca_id']?>" type="text" 
					id="txt_cat_codbar_<?php echo $dt['tb_catalogo_id'].$dt['tb_marca_id']?>" 
					value="<?php echo $dt['tb_productomarca_codbar'] ?>" 
					size="6" style="text-align:right" class="text-white" >
					</td>
					
                    <td align="right" nowrap="nowrap"><?php //if($dt['tb_catalogo_preven']!=0)echo formato_money($dt['tb_catalogo_preven'])?>
                    <input placeholder="Precio Venta"  onclick="editarprecos(<?php echo $dt['tb_catalogo_id'].$dt['tb_marca_id']?>,2)" readonly 
					data-prepromarid ="<?php echo $prepromar_id?>"
					name="txt_cat_preven_<?php echo $dt['tb_catalogo_id'].$dt['tb_marca_id']?>" type="text" 
					id="txt_cat_preven_<?php echo $dt['tb_catalogo_id'].$dt['tb_marca_id']?>" 
					 value="<?php if($preven!=0) echo formato_money($preven)?>" size="7" 
					maxlength="8" style="text-align:right" class="moneda_c text-white" >
					</td>
   
                    <td align="right" nowrap>
                              		
						<small class="badge badge-success" style="cursor:pointer;display:none;" id="lbl_sto_<?php echo $dt['tb_catalogo_id'].$dt['tb_marca_id']?>" >DISPONIBLE</small>
                          
                        <span class="btn btn-primary btn-sm" onClick="actualizar_precio('<?php echo $dt['tb_catalogo_id']?>','<?php echo $dt['tb_marca_id']?>')"> <i class="fa fa-save"></i> </span>
                           
					</td>
                        <script>     
							$("#txt_cat_preven_<?php echo $dt['tb_catalogo_id'].$dt['tb_marca_id']?>").blur(function(e){
                        
								quitareditar('<?php echo $dt['tb_catalogo_id'].$dt['tb_marca_id']?>');
							});
						
                        	
                        	$("#txt_cat_preven_<?php echo $dt['tb_catalogo_id'].$dt['tb_marca_id']?>").keydown(function(e){
                        		if (e.keyCode == 13){
									
									codbar = $("#txt_cat_codbar_<?php echo $dt['tb_catalogo_id'].$dt['tb_marca_id']?>").val();
									prepromar_id = $(this).attr('data-prepromar_id');
									actualizar_precio_unit('<?php echo $dt["tb_catalogo_id"]?>','<?php echo $dt['tb_marca_id']?>',prepromar_id,'<?php echo $_POST['cmb_fil_puntoventa_id']; ?>','<?php echo $dt['tb_productomarca_id']; ?>');
									//verificar_codbar(promarid,codbar,'<?php echo $dt["tb_catalogo_id"]?>','<?php echo $dt['tb_marca_id']?>');
				
                        			return false;
								}
                        		if (e.keyCode == 27)
                        		{
                        			quitareditar('<?php echo $dt["tb_catalogo_id"].$dt['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;
	}
	#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_codbar_"+id).parent().addClass('editando');
	
	$("#txt_cat_precos_"+id).removeAttr('readonly');
	$("#txt_cat_preven_"+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;
		
	}
	
}
function quitareditar(id){
	$("#txt_cat_precos_"+id).attr('readonly', 'true');
	$("#txt_cat_preven_"+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_codbar_"+id).parent().removeClass('editando');
}

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_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_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_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,prepromar_id,punven_id,promar_id)
{
	idf = catid+''+marid;
	$('#lbl_sto_'+idf).hide();
	
		var preven	=parseFloat($("#txt_cat_preven_"+idf).val());
		if(preven>0){
			
		$.ajax({
		type: "POST",
		url: "../preciogestion/precio_reg.php",
		async:true,
		dataType: "html",                      
		data: ({
			action:	'actualizar_precio_panel',
			cat_id: catid,				
			mar_id: marid,	
			promar_id : promar_id,
			prepromar_id : prepromar_id,
			punven_id: punven_id, 			
			cat_precos: $("#txt_cat_precos_"+idf).val(),
			cat_preven: $("#txt_cat_preven_"+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);
				
			}
		});
		
			
		}else{
			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(); 
					
                  
					
				}
			}
		});
}
	
	</script> 