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

require_once("../producto/cProductomarca.php");
$oProductomarca = new cProductomarca();

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;">
	
		<span style="cursor:pointer" id="btn_excel" class="btn btn-success"> <i class="fas fa-file-excel"></i> DESCARGAR EXCEL </span>
	
					<div class="table-responsive">
					
					
		<table id="tabla_gestion_precios" class="table table-bordered table-striped">
		<thead>
		<tr>
		    <th>COD. INTERNO</th>
			<th>LOTE</th>
			<th nowrap >NOMBRE</th>
			<th nowrap >LABORATORIO</th>
			<th >U</th>  
			<th   align="right" class="d-none" 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">P. VENTA F.</th>
			<th   align="right" nowrap title="PRECIO DE VENTA">BONIFICACION</th>
			<th  align="right">&nbsp;</th>
		</tr>
	</thead> 
		<tbody>
			<?php
			if($num_rows>=1){
			while($dt1 = mysqli_fetch_array($dts1)){
				
				if($_POST['cmb_fil_puntoventa_id']>0){
					
					$dtspre = $oPreciogestion->mostrar_precioxpuntoventa($dt1['tb_catalogo_id'],$_POST['cmb_fil_puntoventa_id']);		
					$numrowspre = mysqli_num_rows($dtspre);
					
					$dtp = mysqli_fetch_array($dtspre);
					$prevenf = $dtp['tb_precioproducto_prevenf'];
					$preven = $dtp['tb_precioproducto_preven'];
					$precos = $dtp['tb_precioproducto_precom'];
					$bon = $dtp['tb_precioproducto_bon'];
					$preproesp_id = $dtp['tb_precioproducto_id'];
					
					
					$dtspl = $oProductolote->mostrarUltimoLote($dt1['tb_catalogo_id']);		
					$numrowspl = mysqli_num_rows($dtspl);
					
					$dtp = mysqli_fetch_array($dtspl);
					$lote = $dtp['tb_productolote_nom'];
					
						
					
				}
				?>
				<tr>
				
					<td><?php echo $dt1['tb_producto_codint']?></td>
				    <td style="font-weight:bold" nowrap=""  > <strong class="" style="padding:5px;background-color:#3F51B5;color:#fff;" > <?php echo $lote?> </strong> </td>
								
		
					<td>
						<span style="">
							<?php echo $dt1['tb_producto_nom']?>
						</span>
					</td>
					
					<td ><?php echo $dt1['tb_marca_nom']?></td>
					<td >
						<span style="">
							<?php echo $dt1['tb_unidad_abr']?>
						</span>
					</td>
					
                    <td align="right" class="d-none" >
					<?php if($_POST['cmb_fil_puntoventa_id']>0){ ?>
					<input placeholder="Cod. Bar." readonly class="text-dark"
					onclick="editarprecos('<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>',3)"
					data-promarid ="<?php echo $dt1['tb_productomarca_id']?>"
					data-catid ="<?php echo $dt1['tb_catalogo_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" >
					<?php }?>
					</td>

					<td align="right">
					<?php if($_POST['cmb_fil_puntoventa_id']>0){ ?>
					<input placeholder="Precio Costo" readonly  class="text-dark"
					onclick="editarprecos('<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>',1)"
					data-preproesp_id ="<?php echo $preproesp_id?>"
					data-promarid ="<?php echo $dt1['tb_productomarca_id']?>"
					data-catid ="<?php echo $dt1['tb_catalogo_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($precos!=0) echo formato_money($precos)?>" 
					size="6" maxlength="8" style="text-align:right" >
					<?php }?>
					</td>

                    <td align="right" nowrap="nowrap"><?php //if($dt1['tb_catalogo_preven']!=0)echo formato_money($dt1['tb_catalogo_preven'])?>
                    <?php if($_POST['cmb_fil_puntoventa_id']>0){ ?>
					<input placeholder="Precio Venta" class="text-dark" onclick="editarprecos(<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>,2)" readonly 
					data-preproesp_id ="<?php echo $preproesp_id?>"
					data-promarid ="<?php echo $dt1['tb_productomarca_id']?>"
					data-catid ="<?php echo $dt1['tb_catalogo_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($preven!=0) echo formato_money($preven)?>" size="7" 
					maxlength="8" style="text-align:right">
					<?php }?>
					</td>
					
					
					 <td align="right" nowrap="nowrap"><?php //if($dt1['tb_catalogo_preven']!=0)echo formato_money($dt1['tb_catalogo_preven'])?>
                    <?php if($_POST['cmb_fil_puntoventa_id']>0){ ?>
					<input placeholder="Precio Venta F" class="text-dark" onclick="editarprecos(<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>,4)" readonly 
					data-preproesp_id ="<?php echo $preproesp_id?>"
					data-promarid ="<?php echo $dt1['tb_productomarca_id']?>"
					data-catid ="<?php echo $dt1['tb_catalogo_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($prevenf!=0) echo formato_money($prevenf)?>" size="7" 
					maxlength="8" style="text-align:right">
					<?php }?>
					</td>
					
					<td ><?php echo $bon?></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>
                           <?php if($_POST['cmb_fil_puntoventa_id']>0){ ?>
                        <span class="btn btn-primary btn-sm" onClick="actualizar_precio('<?php echo $dt1['tb_catalogo_id']?>','<?php echo $dt1['tb_marca_id']?>','<?php echo $_POST['cmb_fil_puntoventa_id']; ?>')"> <i class="fa fa-save"></i> </span>
                          <?php }?> 
					</td>
                        <script>     


							$("#txt_cat_preven_<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>,#txt_cat_prevenf_<?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"]?>','<?php echo $_POST['cmb_fil_puntoventa_id']; ?>');
									actualizar_precio_unit('<?php echo $dt1["tb_catalogo_id"]?>','<?php echo $dt1["tb_marca_id"]?>','<?php echo $_POST['cmb_fil_puntoventa_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"]?>','<?php echo $_POST['cmb_fil_puntoventa_id']; ?>');
								actualizar_precio_unit('<?php echo $dt1["tb_catalogo_id"]?>','<?php echo $dt1["tb_marca_id"]?>','<?php echo $_POST['cmb_fil_puntoventa_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"]?>','<?php echo $_POST['cmb_fil_puntoventa_id']; ?>');
								actualizar_precio_unit('<?php echo $dt1["tb_catalogo_id"]?>','<?php echo $dt1["tb_marca_id"]?>','<?php echo $_POST['cmb_fil_puntoventa_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"]?>','<?php echo $_POST['cmb_fil_puntoventa_id']; ?>');
                        			actualizar_precio_unit('<?php echo $dt1["tb_catalogo_id"]?>','<?php echo $dt1["tb_marca_id"]?>','<?php echo $_POST['cmb_fil_puntoventa_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;
	}
	#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"]]
			});
		}); */
		
		
		$("#btn_excel").click(function(){
			dat1 = '<?php echo $dat1 ?>';
			dat2 = '<?php echo $dat2; ?>';
			dat3 = '<?php echo $dat3; ?>';
			dat4 = '<?php echo $dat4; ?>';
			fil_mar = '<?php echo $_POST['txt_fil_mar']; ?>';
			fil_lim = '<?php echo $_POST['cmb_fil_lim']; ?>';
			punven_id = '<?php echo $_POST['cmb_fil_puntoventa_id']; ?>';
			abrirEnPestana('../preciogestion/preciogestion_excel.php?dat1='+dat1+'&dat2='+dat2+'&dat3='+dat3+'&dat4='+dat4+'&fil_mar='+fil_mar+'&fil_lim='+fil_lim+'&punven_id='+punven_id);
});
	
		
})

function abrirEnPestana(url) {
		var a = document.createElement("a");
		a.target = "_blank";
		a.href = url;
		a.click();
}

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_prevenf_"+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;
		case 4:
		$("#txt_cat_prevenf_"+id).focus().select();
		break;
		
	}
	
}

function quitareditar(id){

	$("#txt_cat_precos_"+id).attr('readonly', 'true');
	$("#txt_cat_prevenf_"+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_prevenf_"+id).parent().removeClass('editando');
	$("#txt_cat_codbar_"+id).parent().removeClass('editando');
}

function actualizar_precio(catid,marid,punvenid)
{
		idf = catid+''+marid;
		
		$('#lbl_sto_'+idf).hide();
		
		$.ajax({
		type: "POST",
		url: "../preciogestion/precio_reg.php",
		async:true,
		dataType: "html",                      
		data: ({
			action:	'actualizar_precio_panel',
			cat_id: catid,				
			mar_id: marid,
			punvenid:punvenid,
			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,punvenid)
{
	idf = catid+''+marid;
	$('#lbl_sto_'+idf).hide();
	
	
	
		var precos	=parseFloat($("#txt_cat_precos_"+idf).val());
		var preven	=parseFloat($("#txt_cat_preven_"+idf).val());
		var prevenf	=parseFloat($("#txt_cat_prevenf_"+idf).val());

		$.ajax({
		type: "POST",
		url: "../preciogestion/precio_reg.php",
		async:true,
		dataType: "html",                      
		data: ({
			action:	'actualizar_precio_panel',
			cat_id: catid,				
			mar_id: marid,
			punvenid:punvenid,
			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);
		
			
	//	alert(espid + ' - '+punvenid);

}

function verificar_codbar(promarid,cod_bar,catid,marid,punvenid){
		$.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(); 
					//alert(espid);
                    actualizar_precio_unit(catid,marid,punvenid);
					
				}
			}
		});
}
	

	</script> 