<?php
//ini_set('display_errors',1);
session_start();
if($_SESSION["sistema"]!= "1"){ header("location: ../login/index.php"); exit();}
date_default_timezone_set('America/Lima');
require_once ("../../config/Cado.php");
require_once ("../cotizacion/cCotizacion.php");
$oCotizacion = new cCotizacion();
require_once ("../producto/cCatalogo.php");
$oCatalogo = new cCatalogo();
require_once("../tipocambiotesoreria/cTipocambiotesoreria.php");
$oTipocambiotesoreria = new cTipocambiotesoreria();
require_once("../formatos/formato.php");
unset($_SESSION['cotizacion_catid']);
	unset($_SESSION['cotizacion_marid']);
	unset($_SESSION['cotizacion_des']);
	unset($_SESSION['cotizacion_can']);
	unset($_SESSION['cotizacion_preven']);
	$dts = $oCotizacion->mostrarUno($_POST['cotid']);
	$dt = mysqli_fetch_array($dts);
	$reg = mostrarFechaHora($dt['tb_cotizacion_reg']);
	$fec = mostrarFecha($dt['tb_cotizacion_fec']);
	$doc_id	= $dt['tb_documento_id'];
	$numdoc	= $dt['tb_cotizacion_numdoc'];
	$cli_id	= $dt['tb_cliente_id'];
	$cli_nom = $dt['tb_cliente_nom'];
	$cli_doc = $dt['tb_cliente_doc'];
	$cli_dir = $dt['tb_cliente_dir'];
	$cli_tip = $dt['tb_cliente_tip'];
	$cli_pla = $dt['tb_cliente_pla'];
	$mec_id = $dt['tb_mecanico_id'];
	$mec_nom = $dt['tb_mecanico_nom'];
	$mon_id = $dt['tb_moneda_id'];
	$tipcam = $dt['tb_cotizacion_tipcam'];
	$valven	= $dt['tb_cotizacion_valven'];
	$igv	= $dt['tb_cotizacion_igv'];
	$tot	= $dt['tb_cotizacion_tot'];
	$est	= $dt['tb_cotizacion_est'];
	$dia	= $dt['tb_cotizacion_dia'];
	$edit	= $dt['tb_cotizacion_edi'];
	$obs	= $dt['tb_cotizacion_obs'];
	$estgenven	= $dt['tb_cotizacion_genven'];
	$punven_id	= $dt['punvenid'];
	$ven_nom	= $dt['tb_usuario_nom'];
	$feceve	= mostrarFecha($dt['tb_cotizacion_feceve']);
	$modpag_id	= $dt['tb_cotizacion_mod'];
	$ade_mon	= $dt['tb_cotizacion_ade'];
	mysqli_free_result($dts);
	
	$fecha = date('Y-m-d');
	$rstes = $oTipocambiotesoreria->consultar($fecha);  
	$dttes = mysqli_fetch_array($rstes);
	$tipcamtes =  formato_money($dttes['tb_tipocambiotesoreria_val']);
	
	
	
	if($edit==1){
	?>
	
<div class="col-lg-12 col-sm-12 col-12"  >
	
	<div class="modal-header">		
		<h4 class="modal-title" id="titlemodal">ADVERTENCIA..! <?php echo $numdoc; ?></h4> 
		<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
	</div>
	
	<div class="modal-body">
	<div class="row">
	<div class="col-12"> 
	<div class="col-lg-12 col-sm-12 col-12"> 
		<strong>ALGUIEN MAS ESTA VIENDO EL PEDIDO, NO PUEDE EDITAR NI HACER NINGUN CAMBIO...O ALGUIEN YA HIZO ALGUNA MODIFICACION.</strong>
	</div>	
	</div>
	</div>
	</div>	
	<div class="box-footer pull-right ">
		<span data-dismiss="modal" id="btn_cancelar" class="btn btn-default ">CANCELAR</span>				
	</div>
</div>
		
	
	<?php 	
	}else{
	
	if($est==1){
	$oCotizacion->modificar_campo($_POST['cotid'],'edi','1');
	}
	
	$dtsc=$oCotizacion->mostrarCotizaciondetalle($_POST['cotid']);
	$num_rows= mysqli_num_rows($dtsc);
	
	
	while($dtc = mysqli_fetch_array($dtsc))
    {
			
		$_SESSION['cotizacion_catcod'][$dtc['tb_catalogo_id'].$dtc['tb_marca_id'].$dtc['tb_cotizaciondetalle_id']]= $dtc['tb_catalogo_cod'];	
		$_SESSION['cotizacion_catid'][$dtc['tb_catalogo_id'].$dtc['tb_marca_id'].$dtc['tb_cotizaciondetalle_id']]= $dtc['tb_catalogo_id'];	
		$_SESSION['cotizacion_marid'][$dtc['tb_catalogo_id'].$dtc['tb_marca_id'].$dtc['tb_cotizaciondetalle_id']]= $dtc['tb_marca_id'];	
		$_SESSION['cotizacion_can'][$dtc['tb_catalogo_id'].$dtc['tb_marca_id'].$dtc['tb_cotizaciondetalle_id']]= $dtc['tb_cotizaciondetalle_can']*1;	
		$_SESSION['cotizacion_preven'][$dtc['tb_catalogo_id'].$dtc['tb_marca_id'].$dtc['tb_cotizaciondetalle_id']]=($dtc['tb_cotizaciondetalle_valuni']);	
		$_SESSION['cotizacion_des'][$dtc['tb_catalogo_id'].$dtc['tb_marca_id'].$dtc['tb_cotizaciondetalle_id']]= $dtc['tb_cotizaciondetalle_des'];	
		$_SESSION['cotizacion_pronom'][$dtc['tb_catalogo_id'].$dtc['tb_marca_id'].$dtc['tb_cotizaciondetalle_id']]= $dtc['tb_cotizaciondetalle_nom'];	
		$_SESSION['cotizacion_ind'][$dtc['tb_catalogo_id'].$dtc['tb_marca_id'].$dtc['tb_cotizaciondetalle_id']]= $dtc['tb_cotizaciondetalle_id'];	
	
	
	}
?>
<style type="text/css">
.ui-autocomplete
{
    z-index: 99999; 
}
</style>
<script type="text/javascript">
	/* function cmb_ven_id(idf){
		$.ajax({
			type: "POST",
			url: "../vendedor/cmb_ven_id.php",
			async:true,
			dataType: "html",                      
			data: ({
				ven_id: idf	
			}),
			beforeSend: function() {
				$('#cmb_ven_id').html('<option value="">Cargando...</option>');
			},
			success: function(html){			
				$('#cmb_ven_id').html(html);
			},
			complete: function(){			
			}
		});
	} */
	
	
<?php if($est==1){
?>
<?php 
}else{
?>	
$("#cmb_ven_cli_dir,#cmb_ven_cli_vehi,#txt_cot_mec_nom,#txt_cot_dia").attr('disabled','disabled');
<?php	
}?>	
	
	
	
function cargar_direcciones(idcli,dir){
		$.ajax({
 					type: "POST",
 					url: "../cliente/cmb_clidir_id.php",
 					async:true,
 					dataType: "html",                      
 					data: ({						
 						cli_id: idcli,
 						dir: dir,
 						vista:	''
 					}),
 					beforeSend: function() {
 						$('#cmb_ven_cli_dir').html('<option value="">Cargando...</option>');
 					},
 					success: function(html){			
 						$('#cmb_ven_cli_dir').html(html);
 					},
 					complete: function(){
 						
 						}
 					});
}
function cargar_vehiculos(idcli,veh){
	
		$.ajax({
 					type: "POST",
 					url: "../cliente/cmb_clivehi_id.php",
 					async:true,
 					dataType: "html",                      
 					data: ({						
 						cli_id: idcli,
 						veh: veh,
 						vista:	''
 					}),
 					beforeSend: function() {
 						$('#cmb_ven_cli_vehi').html('<option value="">Cargando...</option>');
 					},
 					success: function(html){			
 						$('#cmb_ven_cli_vehi').html(html);
 					},
 					complete: function(){
 						
 						}
 					});
}
	
function cmb_mone_id(moneid)
	{	
		$.ajax({
			type: "POST",
			url: "../moneda/cmb_mon_id.php",
			async:true,
			dataType: "html",                      
			data: ({
				mon_id: moneid
			}),
			beforeSend: function(){
				$('#cmb_mone_id_vis').html('<option value="">Cargando...</option>');
			},
			success: function(html){
				$('#cmb_mone_id_vis').html(html);
				
			}
		});
	}
	
	function cmb_doc_id(idf){
		$.ajax({
			type: "POST",
			url: "../documento/cmb_doc_id.php",
			async:true,
			dataType: "html",                      
			data: ({
				mod_id: '15',
				def : '0',
				doc_id: idf,
				vista:	''
			}),
			beforeSend: function() {
				$('#cmb_doc_id_vis').html('<option value="">Cargando...</option>');
			},
			success: function(html){			
				$('#cmb_doc_id_vis').html(html);
			},
			complete: function(){
			}
		});
	}
	function cotizacion_detalle_tabla_vis(cotid,estgenven){
		
		<?php if($est==1){?>
		$.ajax({
			type: "POST",
			url: "../cotizacion/cotizacion_detalle_tabla_insertar.php",
			async:true,
			dataType: "html",                      
			data: ({
				cotid:	cotid,
				estgenven:estgenven
			}),
			beforeSend: function() {
				$('#div_cotizacion_detalle_tabla_vis').addClass("ui-state-disabled");
			},
			success: function(html){
				$('#div_cotizacion_detalle_tabla_vis').html(html);
			},
			complete: function(){			
				$('#div_cotizacion_detalle_tabla_vis').removeClass("ui-state-disabled");
			}
		});  
		<?php }else{
		?>
		
		$.ajax({
			type: "POST",
			url: "../cotizacion/cotizacion_detalle_tabla_vista.php",
			async:true,
			dataType: "html",                      
			data: ({
				cotid:	cotid,
				estgenven:estgenven
			}),
			beforeSend: function() {
				$('#div_cotizacion_detalle_tabla_vis').addClass("ui-state-disabled");
			},
			success: function(html){
				$('#div_cotizacion_detalle_tabla_vis').html(html);
			},
			complete: function(){			
				$('#div_cotizacion_detalle_tabla_vis').removeClass("ui-state-disabled");
			}
		});  
		
		<?php 	
		}?>
		
	}
	$(function() {
		
		cargar_direcciones('<?php echo $cli_id?>','<?php echo $cli_dir?>');
		//cargar_vehiculos('<?php echo $cli_id?>','<?php echo $cli_pla?>');
			cmb_mone_id(<?php echo $mon_id?>);
			cmb_doc_id(<?php echo $doc_id?>);
			$("#txt_ven_numdoc_vis").addClass("ui-state-active");
			/* cmb_ven_id(<?php echo $ven_id?>); */
			
			cotizacion_detalle_tabla_vis('<?php echo $_POST["cotid"]?>','<?php echo $estgenven; ?>');
			// $('#cmb_ven_est, #cmb_doc_id_vis, #cmb_ven_id,#cmb_mone_id_vis').attr('disabled','disabled');
			
		$('#div_cotizacion_form_dialog').keypress(function(e){
            if(e.which == 116){
                //  alert('Has pulsado enter!');
            }
		}); 
		
		
		$( "#txt_cot_mec_nom" ).autocomplete({
		minLength: 1,
		source: "../mecanico/mecanico_complete_nom.php",
		select: function(event, ui){			
			$("#hdd_cot_mec_id").val(ui.item.id);							
		}
	});
	
			
		});
	</script>
	<div class="col-lg-12 col-sm-12 col-12 "  >
	
	<div class="modal-header">		
		<h4 class="modal-title" id="titlemodal">Información de la Cotizacion <?php if($estgenven==1){?> <strong style="margin-left:50px;" class="text-red">YA SE HA GENERADO EL DOCUMENTO</strong>  <?php }?></h4> 
		
		<?php if($estgenven==1){?>
		<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
		<?php } ?>
	</div>
	
	
	<div class="col-lg-12 col-sm-12 col-12 ">
	<div class="row">
	
	
	<input type="hidden" id="txt_mone_id" value="<?php echo $mon_id?>" />
	<input type="hidden" id="txt_puntocotizacion_id" value="<?php echo $punven_id?>" />
	<input type="hidden" id="txt_cot_fec" value="<?php echo $fec?>" />
	
	<div class="col-lg-4 col-sm-12 col-12 no-padding"> 
	
		<div class="box box-widget box-success">
			<div class="box-header with-border">
				<div class="user-block"> 
					<img class="img-circle" src="../../imagenes/users/user1.jpg" alt="User Image" title="">
					<span class="username"><a class="text-dark" title="DNI - RUC : <?php echo $cli_doc?>" href="javascript:void(0)"><?php echo "CLIENTE: ".$cli_nom;?></a></span>
				<!--
					<span class="description"> <?php echo ' DIR: '.$cli_dir?></span>
					<span class="description"> <?php echo ' PLACA: '.$cli_pla?></span>
				-->	
					<div class="col-lg-12 col-sm-12 col-12">
 						<div id="div_clientecuenta_detalle"></div>
 						<label for="cmb_ven_cli_dir">Dirección :</label>
 						<select name="cmb_ven_cli_dir" id="cmb_ven_cli_dir" class="form-control" >
						<option value="">-</option>
						</select>						   
 					</div>
					<!--
					<div class="col-lg-12 col-sm-12 col-12">
 						<div id="div_clientecuenta_detalle"></div>
 						<label for="cmb_ven_cli_vehi">Vehiculo:</label>
						<select name="cmb_ven_cli_vehi" id="cmb_ven_cli_vehi" class="form-control" >
						<option value="">-</option>
						</select>
					</div>	
					-->
				<!--	
		<div class="col-lg-12 col-sm-12 col-12">
			<br />
				<?php if(formato_money($tipcam)<formato_money($tipcamtes)){
				?>
			<strong style="font-size:1rem;" class="text-red" >LOS TIPOS DE CAMBIO NO SON IGUALES : <br />   Antes  <span class="text-green"><?php echo formato_money($tipcam); ?></span> ::  Hoy = <span class="text-blue" ><?php echo formato_money($tipcamtes); ?></span> </strong>
	
			<?php
				}else{
			?>
			<strong style="font-size:1.1rem;" class="text-red" >TIPOS DE CAMBIO :  Antes  <span class="text-green"><?php echo formato_money($tipcam); ?></span> ::  Hoy = <span class="text-blue" ><?php echo formato_money($tipcamtes); ?></span></strong>
	
				<?php }?>
			</div>
			-->
			
				</div>				
			</div>
		</div>
		</div>
	
<div class="col-lg-5 col-5 col-12 " >
						<div class="row">
				
				
			<div class="col-lg-4 col-sm-4 col-4"> 
				<label for="txt_ven_fec_vis">Fecha:</label>
				<input title="FECHA DE REGISTRO : <?php echo "$reg";  ?>" 
				name="txt_ven_fec_vis" type="text" class=" form-control form-control-sm fecha" id="txt_ven_fec_vis" readonly value="<?php echo $fec?>" size="10"  maxlength="10" readonly>
			</div>
			<div class="col-lg-4 col-sm-4 col-4 d-none"> 
				<label for="txt_cot_dia">Dias:</label>
				<input type="text" value="<?php echo $dia ?>" class="form-control input-sm" name="txt_cot_dia" id="txt_cot_dia" />
			</div>
			<div class="col-lg-4 col-sm-4 col-4"> 
				<label for="txt_ven_numdoc_vis">N° Doc:</label>
				<input style="font-weight: bold" name="txt_ven_numdoc_vis" readonly type="text" id="txt_ven_numdoc_vis"   value="<?php echo $numdoc?>"  readonly class=" form-control form-control-sm text-orange">
			</div>
			<div class="col-lg-4 col-sm-4 col-4 d-none ">
				<label for="cmb_puntocotizacion_id">Punto Venta:</label>
				<select name="cmb_puntocotizacion_id" id="cmb_puntocotizacion_id" readonly class="form-control form-control-sm ">
					
				</select>
			</div>
			
			<div class="col-lg-4 col-sm-4 col-md-3 col-4">
 					<label for="txt_ven_feceve_vis">Fecha Ev.:</label>
 					<input name="txt_ven_feceve_vis" style="border:2px solid #FF1744" type="text" class="form-control form-control-sm " id="txt_ven_feceve_vis" value="<?php echo $feceve?>" >						 
 			</div>
			
				<div id="div_modo" class="col-lg-4 col-sm-4 col-4">
						<label for="cmb_modpag_id_vis">Modo:</label> 
						<select name="cmb_modpag_id_vis" id="cmb_modpag_id_vis" class="form-control form-control-sm">
 							<option value="" selected >--- Seleccionar ---</option>
 						
 							<option value="1" <?php if($modpag_id==1) echo 'selected'; ?> >EFECTIVO</option> 
							<option value="2" <?php if($modpag_id==2) echo 'selected'; ?>  >DEPOSITO</option>
							<option value="4" <?php if($modpag_id==4) echo 'selected'; ?>  >TRANSFERENCIA</option>
							<option value="5" <?php if($modpag_id==5) echo 'selected'; ?>  >CHEQUE</option>
							<option value="6" <?php if($modpag_id==6) echo 'selected'; ?>  >TARJETA</option>
						</select> 
				</div>
														
				<div class="col-lg-4 col-sm-4 col-md-3 col-4">
 						<label for="txt_cot_adevis">Adelanto:</label>
 						<input name="txt_cot_adevis" type="text" class="form-control form-control-sm" id="txt_cot_adevis" value="<?php echo $ade_mon ?>" >						 
 				</div>
												
			
			
			<div class="col-lg-4 col-sm-4 col-6 d-none ">
				<label for="cmb_mone_id_vis">Moneda:</label>
				<select   name="cmb_mone_id_vis" id="cmb_mone_id_vis" readonly class="form-control input-sm"></select>
			</div>
			
			<div class="col-lg-4 col-sm-4 col-4" style="padding-top:30px;" >
 				<span title="Agregar Servicio" onClick="AgregarItem('agregar')" id="btn_agre_ser" class="btn btn-sm btn-success pull-right" href="javascript:void(0)">Nuevo item </span>
			</div>
												
			
			
			</div>
	</div>
	
	<div class="col-lg-3 col-md-3 col-12 " >
						<div class="row">
				
				
			<div class="col-lg-12 col-sm-12 col-12 col-md-12">
 													<label for="txt_cot_obs_vis">Observacion:</label>
 													<textarea name="txt_cot_obs_vis" rows="4" type="text" class="form-control" id="txt_cot_obs_vis" ><?php echo $obs; ?></textarea>						 
 												</div>
			
			
			
			</div>
	</div>
	
	
		<!--	
			<div class="col-lg-12 col-sm-12 col-12">
 				<div id="div_clientecuenta_detalle"></div>
 				<label for="txt_cot_mec_nom">Taller/Mec.:</label>
	<input name="txt_cot_mec_nom" id="txt_cot_mec_nom" class="form-control" value="<?php echo $mec_nom?>"/>
				<input type="hidden" id="hdd_cot_mec_id" name="hdd_cot_mec_id" value="<?php echo $mec_id?>" />							 
 			</div>
		-->		
				
						
		<div class="col-lg-12 col-sm-12 col-12" style="height:15px;"></div>
		
		<?php if($estgenven!=1){?>
		
 				<div id="fieldset_producto" style="position:relative" class="col-lg-12 col-md-12 col-12 d-none " >
 									<div class="box box-default box-solid box-primary ">
 										<div class="box-body">
 											<div class="row">
 												<div class="col-lg-1 col-sm-3 col-6">
 													<label for="cmb_forpaf_id" class="text-red" >COD. BAR.:</label> 
														
 														<input name="txt_bus_pro_codbar"  autocomplete="off"   type="text" id="txt_bus_pro_codbar" class="form-control" >
 														<input name="hdd_com_numite" id="hdd_com_numite" type="hidden">
 												</div>
 												<div class="col-lg-1 col-sm-3 col-6">
													<label for="txt_bus_pro_cod">Codigo:</label>
													<input name="txt_bus_pro_cod" type="text" id="txt_bus_pro_cod"  autocomplete="off"  class="form-control" >	
												</div>
													
												<div class="col-lg-2 col-sm-3 col-6">
								<label for="txt_fil_pro_dat1">Busqueda:</label>
								<input name="txt_fil_pro_dat1" type="text" id="txt_fil_pro_dat1"  autocomplete="off"  class="form-control">
												</div>
							
							<div class="col-lg-2 col-sm-3 col-6">
								<label for="txt_fil_pro_dat2">-</label>
								<input name="txt_fil_pro_dat2" type="text" id="txt_fil_pro_dat2"   autocomplete="off"  class="form-control">
							</div>
							
							<div class="col-lg-2 col-sm-3 col-6">
								<label for="txt_fil_pro_dat3">-</label>
								<input name="txt_fil_pro_dat3" type="text" id="txt_fil_pro_dat3"  autocomplete="off"  class="form-control">
							</div>
							
							<div class="col-lg-2 col-sm-3 col-6">
								<label for="txt_fil_pro_dat4">-</label>
								<input name="txt_fil_pro_dat4" type="text" id="txt_fil_pro_dat4"  autocomplete="off"  class="form-control">
							</div>
							
							<div style="display: inline-block; white-space: nowrap;padding-top: 23px;" class="col-lg-2 col-sm-3 col-12">
														<div class="btn-group">
															<a title="Restablecer cotizacion" onClick="agregar_producto('restablecer');" id="btn_rest_car" class="btn btn-primary" href="javascript:void(0)"><i class="fa fa-retweet"></i> </a>
															<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
																<span class="caret"></span>														
															</button>
															<ul class="dropdown-menu" role="menu">
																<li><a onClick="agregar_producto_lista();" href="javascript:void(0)"> <i class="fa fa-filter"></i> Seleccionar productos</a></li>
															<!--	<li><a onClick="producto_form('insertar')" href="javascript:void(0)"><i class="fa fa-plus"></i> Nuevo Producto</a></li> -->
															</ul>
															
															<a title="Agregar Servicio" onClick="AgregarItem('agregar')" id="btn_agre_ser" class="btn btn-success pull-right" href="javascript:void(0)"><i class="fa fa-plus"></i> </a>
						
						
														</div> 
													</div>
													
 												
 													
 												</div> 
 											</div>
 										</div>
 									</div>
								
		
		<?php } ?>
		
			<div class="col-lg-12 col-sm-12 col-12" style="height:20px;"></div>
			<div class="col-lg-12 col-sm-12 col-12"> 
			
				<div id="div_cotizacion_detalle_tabla_vis"></div>
			</div>
	
		
		<input name="action_cotizacion_vis" id="action_cotizacion_vis" type="hidden" value="<?php echo $_POST['action']?>">
  		<div class="col-lg-12 col-sm-12 col-12" style="height:15px;"></div>
	</div>	
	</div>	
	
	
	
	<?php if($estgenven!=1){?>
	<div class="box-footer float-right ">
			<!--	<span onClick="generar_venta('<?php echo $_POST['cotid']?>'); " class="btn btn-success ">Generar venta</span>
				-->
				<span data-dismiss="modal" style="cursor:pointer" id="btn_cancelar" onclick="cambiar_estado('<?php echo $_POST['cotid']; ?>');" class="btn btn-default ">CANCELAR</span>			
				<span data-dismiss="modal" style="cursor:pointer" id="btn_guardar" onclick="guardarcoti('<?php echo $_POST['cotid']; ?>');" class="btn btn-primary ">GUARDAR</span>			
	</div>
	<?php } ?>
</div>
		
		
		
		
<script type="text/javascript">
$('#btn_guardar').hide();
cmb_puntocotizacion_id('<?php echo $punven_id;?>');
$('#cmb_ven_cli_dir,#cmb_ven_cli_vehi,#txt_cot_mec_nom,#txt_cot_dia,#txt_cot_obs_vis,#txt_ven_feceve_vis,#cmb_modpag_id_vis').change(function(){
	$('#btn_guardar').show(100);
});

$('#txt_cot_adevis').blur(function(){
	$('#btn_guardar').show(100);
});

	$("#txt_ven_feceve_vis,#txt_cot_adevis").keydown(function(e){
			if (e.which == 13){
					return false;
			}
		});
		

$("#cmb_puntocotizacion_id,#txt_ven_fec_vis,#cmb_mone_id_vis").attr('disabled','disabled');
$("#txt_bus_pro_cod").keydown(function(e){
			if (e.which == 13){
			
			agregar_producto_lista('','','','');
				return false;
			}
		});
$('#txt_cot_obs_vis').change(function()
{
	$(this).val($(this).val().toUpperCase());
});
	
	
	$("#txt_bus_pro_codbar").keydown(function(e){
			if (e.which == 13){
			
			agregar_producto_lista();
				return false;
			}
		});
	
	
		$( "#txt_fil_pro_dat1" ).keydown(function(e){
			if(e.which==32)
			{
				$( "#txt_fil_pro_dat2" ).focus();
				//producto_tabla();
				//e.preventDefault();
			}
		});
		$( "#txt_fil_pro_dat2" ).keydown(function(e){
			if(e.which==32)
			{
				$( "#txt_fil_pro_dat3" ).focus();
				//producto_tabla();
				//e.preventDefault();
			}
		});
		$( "#txt_fil_pro_dat3" ).keydown(function(e){
			if(e.which==32)
			{
				$( "#txt_fil_pro_dat4" ).focus();
				//producto_tabla();
				//e.preventDefault();
			}
		});
		
		$( "#txt_fil_pro_dat1" ).keydown(function(e){
			if(e.which==13)
			{
				
				agregar_producto_lista();
				return false;
			
			}
		});
		
			$( "#txt_fil_pro_dat2" ).keydown(function(e){
			if(e.which==13)
			{
				
				agregar_producto_lista();
				return false;
			}
		});
		
		
			$( "#txt_fil_pro_dat3" ).keydown(function(e){
			if(e.which==13)
			{
				
				agregar_producto_lista();
				return false;
			
			}
		});
		
		$( "#txt_fil_pro_dat4" ).keydown(function(e){
			if(e.which==13)
			{
			agregar_producto_lista();
				return false;
			
			}
		});
		
		
		
		$("#txt_fil_pro_dat4").keydown(function(e){
			if(e.which==8)
			{
				if($("#txt_fil_pro_dat4").val()==""){
					$("#txt_fil_pro_dat3").focus();
				}	
					
			}
		});
		
		
		$("#txt_fil_pro_dat3").keydown(function(e){
			if(e.which==8)
			{
				if($("#txt_fil_pro_dat3").val()==""){
					$("#txt_fil_pro_dat2").focus();
				}	
					
			}
		});
		
		
		$("#txt_fil_pro_dat2").keydown(function(e){
			if(e.which==8)
			{
				if($("#txt_fil_pro_dat2").val()==""){
					$("#txt_fil_pro_dat1").focus();
				}	
					
			}
		});
		
	
function cmb_puntocotizacion_id(punven_id){
		$.ajax({
 					type: "POST",
 					url: "../puntoventa/cmb_punven_id.php",
 					async:true,
 					dataType: "html",                      
 					data: ({						
 						emp_id:'',
						punven_id : punven_id,
 						vista:	''
 					}),
 					beforeSend: function() {
 						$('#cmb_puntocotizacion_id').html('<option value="">Cargando...</option>');
 					},
 					success: function(html){			
 						$('#cmb_puntocotizacion_id').html(html);
 					},
 					complete: function(){
 						
 						}
 					});
}
	
	$('#txt_cot_fec,#txt_ven_feceve_vis').datepicker({
 				format: 'dd-mm-yyyy',
 				autoclose:true			
			});
	
	function guardarcoti(cotid){
		
		// alert($("#txt_ven_tot_vis").val());
		// alert(cotid);
		$('#div_mensaje_dialog').modal();
		$.ajax({
				type: "POST",
				url: "../cotizacion/cotizacion_reg.php",
				async:true,
				dataType: "json",                      
				data: ({
					action:'editar',
					cotid:cotid,
					valven: $("#txt_ven_valven").val(),
					igv: $("#txt_ven_igv").val(),
					des: $("#txt_ven_des").val(),
					tot: $("#txt_ven_totsdes").val(),
					monid: $("#txt_mone_id").val(),
					docid: '10',
					fec: $("#txt_cot_fec").val(),
					dia: $("#txt_cot_dia").val(),
					mec: $("#hdd_cot_mec_id").val(),
					punvenid: $("#txt_puntocotizacion_id").val(),
					cliid: '<?php echo $cli_id; ?>',
					clinom: '<?php echo $cli_nom; ?>',
					clidir: $("#cmb_ven_cli_dir").val(),
					clipla: $("#cmb_ven_cli_vehi").val(),
					cot_obs: $("#txt_cot_obs_vis").val(),
					feceve: $("#txt_ven_feceve_vis").val(),
					modpag_id: $("#cmb_modpag_id_vis").val(),
					cot_ade: $("#txt_cot_adevis").val()
				}),
				beforeSend: function() {
					
				var div = "<center> <br> <span class='spinner-border spinner-border-lg mr-2'></span> <span style='font-size:1.5rem;'>PROCESANDO PETICIÓN</span></center>";	
				$('.div_mensaje').html(div);
					
				},
				success: function(data){
					
					if(data.cot_act==1){
					cotizacion_ticket_datos(data.cotid);
					}
					
					toastr["info"](data.msj);
					generar_pdf(data.cotid);
					cotizacion_tabla();	
							
					$('.modal').modal('hide');
					$('.in').removeClass('modal-backdrop');
				
				abrirEnPestana('="../../../../files/cotizacion/cotizacion_imp_doc.php?cotid='+data.cotid);
					
					
				}
			});
		
	}
	
	
	
	
	function abrirEnPestana(url) {
		var a = document.createElement("a");
		a.target = "_blank";
		a.href = url;
		a.click();
	}
	
function agregar_producto(act,idf, cantidad, preven, marid, des,mon,pro_nom,ind)
{
	//if(!( idf>0 )) idf = $('#hdd_bus_cat_id').val();		
	$.ajax({
		type: "POST",
		url: "../cotizacion/agregar_producto.php",
		async:true,
		dataType: "json",                      
		data: ({
			action: act,
			cat_id:	idf,
			cat_can: cantidad || $('#txt_bus_cat_can').val(),
			cat_preven: preven || $('#txt_bus_cat_preven').val(),
			mar_id: marid,	
			ind: ind,	
			pro_nom: pro_nom,	
			cat_des: des,	
			mon: $("#txt_mone_id").val()
		}),
		beforeSend: function() {
		},
		success: function(data){
			
			if(data.est==false)
			{				
				if(data.dat.tipo=="preven")
				{
					limpiar_campos();
					cotizacion_detalle_tabla_vis();
					toastr["warning"](data.msj);
				}
			}
		},
		complete: function(data)
		{
			var dat=JSON.parse(data.responseText);
			if(dat.est==true)
			{
				if(dat.dat.tipo=="agregado")
				{
					limpiar_campos();
				}
				
				if(dat.dat.tipo=="foco")
				{
					limpiar_campos();
				}
				cotizacion_detalle_tabla_vis();
				$('#btn_guardar').show(100);
			}
		}
	});
}
	
	
function limpiar_campos()
{
	
	$("#txt_bus_pro_codbar").val('');
	$("#txt_bus_pro_cod").val('');
	$("#txt_fil_pro_dat1").val('');
	$("#txt_fil_pro_dat2").val('');
	$("#txt_fil_pro_dat3").val('');
	$("#txt_fil_pro_dat4").val('');
	$("#txt_bus_pro_cod").focus();
}
	
	
	function agregar_producto_lista(codmar,dat1,dat2,dat3,dat4,punven_id){
			$('#div_productomarca_form_dialog').modal();
		$('body').css('overflow-y','hidden');
		
		
		$.ajax({
				type: "POST",
				url: "../compra/seleccionarproducto.php",
				async:true,
				dataType: "html",                      
				data: ({
					codmar:codmar || $("#txt_bus_pro_cod").val(),
					dat1:dat1 || $("#txt_fil_pro_dat1").val(),
					dat2:dat2 || $("#txt_fil_pro_dat2").val(),
					dat3:dat3 || $("#txt_fil_pro_dat3").val(),
					dat4:dat4 || $("#txt_fil_pro_dat4").val(),
					codbar: $("#txt_bus_pro_codbar").val(),
					punven_id:punven_id || $("#txt_puntocotizacion_id").val(),
					vista:'cotizacion_form'
				}),
				beforeSend: function() {
					var div = "<center> <br> <span class='spinner-border spinner-border-lg mr-2'></span> <span style='font-size:1.5rem;'>PROCESANDO PETICIÓN</span></center>";	
				$('.div_productomarca_form').html(div);
				},
				success: function(html){
					$('.div_productomarca_form').html(html);				
				}
			});
		
		/* 
			$.ajax({
				type: "POST",
				url: "../compra/seleccionarproducto.php",
				async:true,
				dataType: "html",                      
				data: ({
					codmar:codmar || $("#txt_bus_pro_cod").val(),
					dat1:dat1 || $("#txt_fil_pro_dat1").val(),
					dat2:dat2 || $("#txt_fil_pro_dat2").val(),
					dat3:dat3 || $("#txt_fil_pro_dat3").val(),
					dat4:dat4 || $("#txt_fil_pro_dat4").val(),
					vista:'cotizacion_form',
					cotid:<?php echo $_POST['cotid']; ?>
				}),
				beforeSend: function() {
					var div = "<center><div class='overlay col-lg-12 col-sm-12 col-12' style='background-color:#fff;padding:0%;font-size:3rem;' >  <i class='fa fa-refresh fa-spin ' ></i> </div>  </center>";	
					$('.div_productomarca_form').html(div);
				},
				success: function(html){
					$('.div_productomarca_form').html(html);				
				}
			}); */
		}
		function cambiar_estado(cot_id){
					
			$.ajax({
				type: "POST",
				url: "../cotizacion/cotizacion_reg.php",
				async:true,
				dataType: "json",                      
				data: ({
					action:'actualizar_estado',
					cotid:cot_id
				}),
				beforeSend: function() {
					
				},
				success: function(data){
					toastr["success"](data.msj);
					cotizacion_tabla();			
				}
			});
		}
</script>		
	<?php 
	}		
	?>
		
		