 <?php
 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("../formatos/formato.php");
 if($_POST['action']=="insertar")
 {
 	$fec = date('d-m-Y');
 	$est = 1;
	
	/* if($_SESSION['usuariogrupo_id']==1 || $_SESSION['usuariogrupo_id']==2){
		
	}
	else{
 	} */
	
	$punven_id = $_SESSION['puntoventa_id'];
 	
 	$cli_id = '';
	$mon_id = 1;
 	$venpag_fec = date('d-m-Y');
 	$modo_automatico = 0;
	$vend_id = 1;
	$ven_id = "";
	$emp_id = 1;
	
 }
 ?>
 
 <style type="text/css">
.ui-autocomplete
{
    z-index: 99999; 
}
input[type=checkbox] {
 cursor:pointer;
}
#chk_imprimir{
  visibility: hidden;
}
.div_checkbox {
    width: 100px;
    height: 30px;
    background: #FF8800;
    margin: 0 40px;
    border-radius: 50px;
    position: relative;
  font-family: 'Aldrich', sans-serif; 
}
.div_checkbox:before {
    content: 'Sí';
    position: absolute;
    top: 0;
    left: 20px;
    color: #fff;
    font-size: 14px;
  line-height: 30px;
  text-shadow: 0 0 2px #fff;
}
.div_checkbox:after {
    content: 'No';
  color: #CC0000;
    position: absolute;
    top: 0px;
    right: 20px;
    font-size: 14px;
  line-height: 30px;
  text-shadow: 0 0 1px #CC0000;
}
.div_checkbox label { 
    background: #CC0000;
    display: block;
    width: 45px;
    height: 22px;
    border-radius: 50px;
    transition: all .7s ease;
    cursor: pointer;
    position: absolute;
    top: 4px;
    z-index: 1;
    left: 10px;
}
.div_checkbox input[type=checkbox]:checked + label {
    left: 45px;
    background: #fff;
}
</style>
 			<div class="col-lg-12 col-md-12 col-12 " >
 			
 					<form id="for_cot" action="POST">
 						<input name="action_cotizacion" id="action_cotizacion" type="hidden" value="<?php echo $_POST['action']?>">
 						<input name="hdd_ven_id" id="hdd_ven_id" type="hidden" value="<?php echo $_POST['ven_id']?>">
 						<input name="hdd_usu_id" id="hdd_usu_id" type="hidden" value="<?php echo $_SESSION['usuario_id']?>">
 						<input name="hdd_punven_id" id="hdd_punven_id" type="hidden" value="<?php echo $_SESSION['puntoventa_id']?>">
 						<input name="hdd_emp_id" id="hdd_emp_id" type="hidden" value="<?php echo $_SESSION['empresa_id'] ?>">	
 						
						
					<div class="col-lg-12 col-md-12 col-12 " style="padding:5px;">
					<div class="row">
					
					<div class="col-lg-5 col-md-6 col-12 float-left">
					<h3 >Registrar Cotizacion <span class="text-red"><?php // echo $_SESSION['empresa_nombre'] ?></span> </h3>
					</div>
					
					<div class="col-lg-5 col-md-6 col-12 float-right">
					<div class="div_checkbox float-right">
					<input type="checkbox" name="chk_imprimir" id="chk_imprimir" checked="checked" value="1" />
					<label for="chk_imprimir"></label>
					</div>
					<strong class="float-right" style="font-size:1rem;">Imprimir documento</strong>
					
					</div>
					
					</div>
					
					</div>
					
					
					<div class="col-lg-12 col-md-12 col-12 " >
						<div class="row">
				
				
 						<div class="col-lg-7 col-md-12 col-12 "> 
 							
 							<div id="tabcliente" class="nav-tabs-custom">
 								<nav class="nav nav-tabs">
 									<a href="#tab_1" class="nav-item nav-link active" style="color:#000;" data-toggle="tab" aria-expanded="false">Cliente</a>
								</nav>
 								<div class="tab-content">
 									<div class=" active tab-pane" id="tab_1">
 										<div class="row"> 
 											<input type="hidden" id="hdd_ven_cli_id" name="hdd_ven_cli_id" value="<?php echo $cli_id?>" />
 											<input type="hidden" id="hdd_ven_cli_tip" name="hdd_ven_cli_tip" value="<?php echo $cli_tip?>" />
 											<input type="hidden" id="hdd_val_cli_tip" name="hdd_val_cli_tip" value="<?php if($_POST['action']=='editar')echo $cli_tip;?>" />
											
											<div class="col-lg-4 col-sm-5 col-6 errorprov ">
 												<label for="txt_com_pro_doc">RUC/DNI:</label>
 												<div class="input-group">
														<span id="" style=""  class="input-group-btn">
																<span id="" style="cursor:pointer" class="btn btn-primary btn-flat" onClick="cliente_form('insertar')"><i class="fa fa-cog"></i></span> 
														</span>	
 														
 														<input name="txt_ven_cli_doc" type="text" id="txt_ven_cli_doc" value="<?php echo $cli_doc?>"   maxlength="11" class="form-control form-control-sm"> 
 													</div>
 												</div>
 												<div class="col-lg-8 col-sm-7 col-6">
 													<div id="div_clientecuenta_detalle"></div>
 													<label for="txt_ven_cli_nom">Cliente:</label>
 													<input type="text" id="txt_ven_cli_nom" name="txt_ven_cli_nom"  value="<?php echo $cli_nom?>" class="form-control form-control-sm" />
 												</div> 
 											
 												<div class="col-lg-12 col-sm-6 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 form-control-sm " >
													<option value="">-</option>
													</select>
													   
 												</div>
												
												
												<!-- -->
												<div class="col-lg-12 col-sm-6 col-12 d-none" >
 													<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>
 											
 										</div>
 									
									
									</div>
 									
 								</div> 
 						</div>
 							
 							<div class="col-lg-5 col-md-12 col-12" >
 								<div class="nav-tabs-custom">
 									<nav class="nav nav-tabs">
 									<a href="#tab_3" class="nav-item nav-link active" style="color:#000;" data-toggle="tab" aria-expanded="false">Datos generales</a>
 									<a href="#tab_5" class="nav-item nav-link " style="color:#000;" data-toggle="tab" aria-expanded="false">Datos adicionales</a>
								</nav>
 									<div class="tab-content">
 										<div class="active tab-pane" id="tab_3">
 											<div class="row">
 												<div class="col-lg-4 col-sm-4 col-md-3 col-4">
 													<label for="txt_ven_fec">Fecha:</label>
 													<input name="txt_ven_fec" type="text" class="form-control form-control-sm" id="txt_ven_fec" value="<?php echo $fec?>" >						 
 												</div>
												
												<div class="col-lg-4 col-sm-4 col-md-3 col-4">
 													<label for="txt_ven_feceve">Fecha Ev.:</label>
 													<input name="txt_ven_feceve" style="border:2px solid #FF1744" type="text" class="form-control form-control-sm " id="txt_ven_feceve" value="<?php echo $fec?>" >						 
 												</div>
												
 												<div class="col-lg-4 col-sm-4 col-3 col-md-4 d-none">
 													<label for="txt_cot_dia">Dias:</label>
 													<input type="text" id="txt_cot_dia" name="txt_cot_dia" class="form-control form-control-sm" value="0" />					 
 												</div>
 												<div class="col-lg-4 col-sm-4 col-3 col-md-4">
 													<label for="txt_ven_numdoc">N° Doc:</label>
 													<input name="txt_ven_numdoc" type="text" class="form-control text-orange form-control-sm" id="txt_ven_numdoc" style="text-align:right; font-size:14px"  value="<?php echo $numdoc?>" readonly>						 
 												</div>
												
												
												<div id="div_modo" class="col-lg-4 col-sm-4 col-4">
															<label for="cmb_modpag_id">Modo:</label> 
															<select name="cmb_modpag_id" id="cmb_modpag_id" class="form-control form-control-sm">
 															<option value="1">EFECTIVO</option>
															<option value="2">DEPOSITO</option>
															<option value="4">TRANSFERENCIA</option>
															<option value="5">CHEQUE</option>
															<option value="6">TARJETA</option>
															</select> 
														</div>
														
												<div class="col-lg-4 col-sm-4 col-md-3 col-4">
 													<label for="txt_cot_ade">Adelanto:</label>
 													<input name="txt_cot_ade" type="text" class="form-control form-control-sm" id="txt_cot_ade" value="<?php echo '0' ?>" >						 
 												</div>
												
												
												
												<div class="col-lg-6 col-sm-4 col-6 d-none ">
													<label for="cmb_mone_id">Moneda:</label>
													<select   name="cmb_mone_id" id="cmb_mone_id" class="form-control "></select>
													<input name="txt_mone_id" type="hidden" value="" id="txt_mone_id">	
										
												</div>
												
												<div class="col-lg-6 col-sm-4 col-6 d-none">
													<label for="cmb_puntoventa_id">Punto Venta:</label>
													<select name="cmb_puntoventa_id" id="cmb_puntoventa_id" class="form-control ">
													</select>
													<input name="txt_punven_id" type="hidden" id="txt_punven_id">	
											</div>
												
												<div class="col-lg-12 col-sm-12 col-12" style="display:none" >
 													<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" />
													<input type="hidden" id="hdd_cot_mec_id" name="hdd_cot_mec_id" />
													 
 												</div>
				
			
												<div class="col-lg-4 col-sm-3 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 class="row">
 											 <div class="col-lg-6 col-sm-6 col-6">
 													<label for="cmb_ven_id">Vendedor: </label>
 													<select name="cmb_ven_id" id="cmb_ven_id" class="form-control input-sm" ></select>					 
 												</div> 
											
 												<div class="col-lg-6 col-sm-6 col-6">
 													<label for="cmb_ven_est">Imprimir   Estado:</label>
 													<div class="input-group"> 
 														<span class="input-group-addon">
 															<input style=" width: 16px; padding: 5px; height: 15px;" title="Activar para Imprimir" name="chk_imprimir" id="chk_imprimir"   value="1" type="checkbox">
 														</span> 
 														<select name="cmb_ven_est" id="cmb_ven_est" class="form-control">
 															<option value="">-</option>
 															<option value="1" <?php if($est=='1')echo 'selected'?>>REGISTRADA</option>
 															<option value="2" <?php if($est=='2')echo 'selected'?>>ANULADA</option>
 														</select> 
 													</div>
 												</div> 
 											</div> --> 
 										</div>
										
										<div class="tab-pane" id="tab_5">
 											<div class="row">
 												
 												<div class="col-lg-12 col-sm-12 col-12 col-md-12">
 													<label for="txt_cot_obs">Observacion:</label>
 													<textarea name="txt_cot_obs" rows="4" type="text" class="form-control" 
													id="txt_cot_obs" 
													></textarea>						 
 												</div>
												
											
												
 											</div> 
											
 										
 										</div>
 									</div>
 									
 								</div>   
 							</div>
 							
 								
						</div>
					</div>	
								
					<div class="col-lg-12 col-md-12 col-12" style="padding:5px;"></div>
									
							<div class=" card card-default card-solid card-primary d-none ">
								<div id="fieldset_producto" class="col-lg-12 col-md-12 col-12 " style="padding:0%;" >								
										<div class="card-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" type="text" id="txt_bus_pro_codbar" autocomplete="off" 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">Locacion:</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:30px;" 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">
														<a class="dropdown-item text-white" onClick="agregar_producto_lista();" href="javascript:void(0)"> <i class="fa fa-filter"></i> Seleccionar productos</a>
															<!--	<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>
									
									<div class="col-lg-12 col-md-12 col-12" style="padding:5px;"></div>
									
 									<div class="col-lg-12 col-md-12 col-12">
									
 										<div id="div_venta_detalle_tabla"></div>	
 									</div> 									 
 												<div class="col-lg-12 col-sm-12 col-12" style="height:15px;"></div>							 
 							</form>
							<div class="box-footer float-right">
								<span data-dismiss="modal" style="cursor:pointer" class="btn btn-default">Cancelar</span>
								<span class="btn btn-primary" style="cursor:pointer" onclick="cotizar();" >Guardar</span>
							</div>
							
					</div>
 				
				
								
				
 		<script type="text/javascript"> 
			
			function agregar_producto_lista(codmar,dat1,dat2,dat3,dat4,punven_id){
			if($("#cmb_puntoventa_id").val()==''){
				
				toastr["info"]('Seleccione un punto de venta.');
				
			}else{
			$('#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(),
					codbar: $("#txt_bus_pro_codbar").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(),
					punven_id:punven_id || $("#cmb_puntoventa_id").val(),
					vista:'cotizacion_form'
				}),
				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 cotizar(){
				
				var ok = true;
				var registros = $("#hdd_ven_numite").val();
				
			if(registros == 0){
			msj = "Agregue registro de productos...";
			ok = false;
			//$('#tabcliente .nav-tabs a:last').tab('show');
			toastr["info"](msj);
			
			}
			
			if($("#cmb_puntoventa_id").val()==""){
			msj = "Elija un punto de venta.";
			ok = false;
			
			toastr["warning"](msj);
			
			}
			
			if($("#hdd_ven_cli_id").val()==""){
			msj = "Seleccione un cliente.";
			ok = false;
			
			toastr["warning"](msj);
			
			}
			
			
			if(ok==true){
			$("#for_cot").submit();	
			}
 				
			}
			
 			$('#txt_ven_fec,#txt_ven_feceve').datepicker({
 				format: 'dd-mm-yyyy',
 				autoclose:true			
			});
 			$("#txt_ven_numdoc").addClass("ui-state-active");
			txt_ven_numdoc();
			
			function txt_ven_numdoc()
 			{
 				$.ajax({
 					type: "POST",
 					url: "../cotizacion/cotizacion_txt_numdoc.php",
 					async:false,
 					dataType: "json",                      
 					data: ({
 						doc_id: 32
 					}),
 					beforeSend: function() {
 						$('#txt_ven_numdoc').val('Cargando...');
 					},
 					success: function(data){			
 						$('#txt_ven_numdoc').val(data.numero);
 						if(data.msj!="")
 						{
							toastr["success"](data.msj);
 						}
 						else
 						{
 						}
 					},
 					complete: function(){
							verificar_numdoc();							
 						}
 					});
 			}
 function verificar_cliente(){	
	if($('#cmb_doc_id').val()==4)//boleta
	{
		$('#hdd_val_cli_tip').val('1');
	}
	if($('#cmb_doc_id').val()==5)//factura
	{
		$('#hdd_val_cli_tip').val('2');
	}
	if($('#cmb_doc_id').val()>5)//otros
	{
		$('#hdd_val_cli_tip').val('1');
	}
}
function verificar_numdoc(){	
	$.ajax({
		type: "POST",
		url: "../cotizacion/cotizacion_ver_serie.php",
		async:false,
		dataType: "json",                      
		data: ({
			doc_id: 32,
			numdoc: $('#txt_ven_numdoc').val(),
			ven_est: '1' //$('#cmb_ven_est').val()
		}),
		beforeSend: function() {
		},
		success: function(data){			
			$('#hdd_ven_doc').val(data.valor);
			if(data.msj!="")
			{			
			toastr["info"](data.msj);					
			}
		}
	});
}
function cliente_form(act,idf){
	$('#div_cliente_form_dialog').modal();
	$.ajax({
		type: "POST",
		url: "../cliente/cliente_form.php",
		async:true,
		dataType: "html",                      
		data: ({
			action: act,
			cli_id:	idf,
			vista:	'venta'
		}),
		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_cliente_form').html(div); 
		},
		success: function(html){ 	
			$('.div_cliente_form').html(html); 				
		},
		complete: function(){
				
				
		}
	});
}
function cliente_cargar_datos(idf){	
	$.ajax({
		type: "POST",
		url: "../cliente/cliente_reg.php",
		async:true,
		dataType: "json",                      
		data: ({
			action: "obtener_datos",
			cli_id:	idf
		}),
		beforeSend: function() {
		
		},
		success: function(data){
			$('#hdd_ven_cli_id').val(idf);	
			$('#txt_ven_cli_nom').val(data.nombre);	
			$('#txt_ven_cli_doc').val(data.documento);
			$("#hdd_ven_cli_tip").val(data.tipo);
			$("#hdd_ven_cli_tel").val(data.telefono);
			$("#hdd_ven_cli_corr").val(data.correo);
			
			cargar_direcciones(idf);
			cargar_vehiculos(idf);
		}
	});		
}
function cargar_direcciones(idcli){
		$.ajax({
 					type: "POST",
 					url: "../cliente/cmb_clidir_id.php",
 					async:true,
 					dataType: "html",                      
 					data: ({						
 						cli_id: idcli,
 						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){
	
		$.ajax({
 					type: "POST",
 					url: "../cliente/cmb_clivehi_id.php",
 					async:true,
 					dataType: "html",                      
 					data: ({						
 						cli_id: idcli,
 						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_puntoventa_id(punven){
		$.ajax({
 					type: "POST",
 					url: "../puntoventa/cmb_punven_id.php",
 					async:true,
 					dataType: "html",                      
 					data: ({						
 						emp_id:'',
						punven_id :punven,
 						vista:	''
 					}),
 					beforeSend: function() {
 						$('#cmb_puntoventa_id').html('<option value="">Cargando...</option>');
 					},
 					success: function(html){			
 						$('#cmb_puntoventa_id').html(html);
 					},
 					complete: function(){
 						$('#txt_punven_id').val($('#cmb_puntoventa_id').val());
 						}
 					});
}
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(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(),
			pro_nom: pro_nom,	
			mar_id: marid,	
			ind: ind,	
			cat_des: des,	
			mon: $("#cmb_mone_id").val()
		}),
		beforeSend: function() {
		},
		success: function(data){
			
			if(data.est==false)
			{				
				if(data.dat.tipo=="preven")
				{
					limpiar_campos();
					venta_detalle_tabla(<?php echo $ven_id;?>);
					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();
				}
				venta_detalle_tabla(<?php echo $ven_id;?>);
			}
		}
	});
}
function venta_detalle_tabla(ven_id){
		$.ajax({
			type: "POST",
			url: "../cotizacion/cotizacion_detalle_tabla_insertar.php",
			async:true,
			dataType: "html",                      
			data: ({
				ven_id:	ven_id,
				cli_id:	$("#hdd_ven_cli_id").val(),
			}),
			beforeSend: function() {
				$('#div_venta_detalle_tabla').addClass("ui-state-disabled");
			},
			success: function(html){
				$('#div_venta_detalle_tabla').html(html);
			},
			complete: function(){			
				$('#div_venta_detalle_tabla').removeClass("ui-state-disabled");
				
			}
		});     
}
function cmb_empresa_id(empid){
		$.ajax({
 					type: "POST",
 					url: "../empresa/cmb_emp_id.php",
 					async:true,
 					dataType: "html",                      
 					data: ({						
 						emp_id: empid,
 						vista:	''
 					}),
 					beforeSend: function() {
 						$('#cmb_empresa_id').html('<option value="">Cargando...</option>');
 					},
 					success: function(html){			
 						$('#cmb_empresa_id').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').html('<option value="">Cargando...</option>');
			},
			success: function(html){
				$('#cmb_mone_id').html(html);
				
				//verTipcambio($('#cmb_mone_id').val());
				$('#txt_mone_id').val($('#cmb_mone_id').val());
				
			}
		});
	}
	/* 
	function verTipcambio(moneid){
		$.ajax({
			type: "POST",
			url: "../tipocambio/tipocambio_reg.php",
			async:true,
			dataType: "json",                      
			data: ({
				action_tipocambio:'obtener_tipcam',
				mone_id: moneid
			}),
			beforeSend: function(){
			
			},
			success: function(data){
				$("#lbl_mone_val").html('S./ '+data.moncam_val);
				$("#txt_ven_tipcam").val(data.moncam_val);
				
				if(moneid==1){
					$(".txt_bus_cat_preven").html('S/.:');
				}else if(moneid==2){
					$(".txt_bus_cat_preven").html('US$.:');
				}	
			}
		});
	} */
	
toastr.options = {
  "closeButton": false,
  "debug": false,
  "newestOnTop": false,
  "progressBar": false,
  "positionClass": "toast-top-center",
  "preventDuplicates": false,
  "onclick": null,
  "showDuration": "300",
  "hideDuration": "1000",
  "timeOut": "5000",
  "extendedTimeOut": "1000",
  "showEasing": "swing",
  "hideEasing": "linear",
  "showMethod": "fadeIn",
  "hideMethod": "fadeOut"
}
$(function() {
	/* $('input').iCheck({
		checkboxClass: 'icheckbox_square-blue chk_venpag_aut',
		radioClass: 'iradio_square-blue chk_venpag_aut',
		increaseArea: '20%' 
		}); */
	cmb_puntoventa_id('<?php echo $punven_id;?>');
	venta_detalle_tabla('<?php echo $ven_id;?>');
	//cmb_doc_id('<?php echo $doc_id?>');
	cmb_mone_id(<?php echo $mon_id?>);
	cliente_cargar_datos('<?php echo $cli_id?>');
	$("#cmb_doc_id").focus();
	agregar_producto('restablecer');
	// pago_venta_agregar('restablecer','',0); 
	$('#cmb_mone_id').change(function(){
		$("#txt_mone_id").val($(this).val());
	});
	
	$("#cmb_puntoventa_id").change(function(){		
		$("#txt_punven_id").val($("#cmb_puntoventa_id").val());
	});
	$( "#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);							
		}
	});
	$( "#txt_ven_cli_doc" ).autocomplete({
		minLength: 1,
		source: "../cliente/cliente_complete_doc.php",
		select: function(event, ui){			
			$("#hdd_ven_cli_id").val(ui.item.id);
			$("#txt_ven_cli_nom").val(ui.item.nombre);
			$("#txt_ven_cli_dir").val(ui.item.direccion);
			$("#hdd_ven_cli_tip").val(ui.item.tipo);
				$("#txt_bus_pro_codbar").focus();
				
				cargar_direcciones(ui.item.id);
				cargar_vehiculos(ui.item.id);
				venta_detalle_tabla(<?php echo $ven_id;?>);
				
				
			}
		});
	$( "#txt_ven_cli_nom" ).autocomplete({
		minLength: 1,
		source: "../cliente/cliente_complete_nom.php",
		select: function(event, ui){			
			$("#hdd_ven_cli_id").val(ui.item.id);							
			$("#txt_ven_cli_doc").val(ui.item.documento);
			$("#txt_ven_cli_dir").val(ui.item.direccion);
			$("#hdd_ven_cli_tip").val(ui.item.tipo);
				$("#txt_bus_pro_codbar").focus();
				cargar_direcciones(ui.item.id);
				cargar_vehiculos(ui.item.id);
				venta_detalle_tabla(<?php echo $ven_id;?>);
			}
		});
	$('#txt_ven_cli_nom').keydown(function(e){
		if (e.which == 13){$("#txt_bus_pro_codbar").focus();return false;}
	});
	
	$('#txt_ven_cli_doc').keydown(function(e){
		if (e.which == 13){return false;}
	});
	
	
		
		$("#txt_bus_pro_cod").keydown(function(e){
			if (e.which == 13){
			
			agregar_producto_lista('','','','');
				return false;
			}
		});
		
		$( "#txt_cot_dia" ).keydown(function(e){
			if(e.which==13)
			{
				
				return false;
			}
		});
		
		$("#txt_bus_pro_codbar").keydown(function(e){
			if (e.which == 13){
			
			agregar_producto_lista();
				return false;
			}
		});
		
		
$('#txt_cot_obs').change(function()
{
	$(this).val($(this).val().toUpperCase());
});
	
	
	
	
	
		$( "#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();
				}	
					
			}
		});		
 
	$('#for_cot').bootstrapValidator({
	    
	      message: 'Valor no Valido',
	      feedbackIcons: {
	      	valid: 'glyphicon glyphicon-ok',
	      	invalid: 'glyphicon glyphicon-remove',
	      	validating: 'glyphicon glyphicon-refresh'
	      },
	      fields: { 
	      	hdd_ven_numite: {
	      		group: '.col-lg-8',
	      		validators: {
	      			notEmpty: {
	      				message: 'Agregue producto a detalle de venta.'
	      			}
	      		}
	      	},	      	      
	      	txt_ven_fec: {
	      		group: '.col-lg-6',
	      		validators: { 
	      			date: {
	      				format: 'DD-MM-YYYY',
	      				message: 'Fecha no Valida'
	      			}
	      		}
	      	},
	      	cmb_doc_id: {
	      		group: '.col-lg-6',
	      		validators: {
	      			notEmpty: {
	      				message: '* Requerido'
	      			}
	      		}
	      	},
	      	
	      	txt_ven_numdoc: {
	      		group: '.col-lg-4',
	      		validators: {
	      			notEmpty: {
	      				message: '* Requerido'
	      			}
	      		}
	      	},
	      	hdd_ven_cli_id: {
	      		group: '.col-lg-4',
	      		validators: {
	      			notEmpty: {
	      				message: 'Seleccione Cliente.'
	      			}
	      		}
	      	},	      	
	      
	      	hdd_ven_pag_numite: {
	      		group: '.col-lg-3',
	      		validators: {
	      			notEmpty: {
	      				message: 'Agregue registro de pagos.'
	      			}
	      		}
	      	},
	      	hdd_venpag_tot: {
	      		group: '.col-lg-3',
	      		validators: {
	      			identical: {
	      				field: 'txt_ven_tot',
	      				message: 'No es igual a Monto Total de Venta.'
	      			}
	      		}
	      	},
	      	hdd_ven_cli_tip: {
	      		group: '.col-lg-3',
	      		validators: {
	      			identical: {
	      				field: 'hdd_val_cli_tip',
	      				message: 'Tipo de cliente no concuerda con tipo de documento.'
	      			}
	      		}
	      	},
	      	hdd_ven_doc: {
	      		group: '.col-lg-3',
	      		validators: {
	      			notEmpty: {
	      				message: 'Existe registro con mismo N° Documento.'
	      			}
	      		}
	      	},
	      }
	  }).on('success.form.bv', function(e) {
			$('#div_mensaje_dialog').modal();
	  
	  	$.ajax({
	  		type: "POST",
	  		url: "../cotizacion/cotizacion_reg.php",
	  		async:false,
	  		dataType: "json",
	  		data: $("#for_cot").serialize(),
	  		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> PROCESANDO PETICION.</div>  </center>";	
				$('.div_mensaje').html(div);
				},
				success: function(data)
				{
					//factura_datos(data.ven_id);
					
					generar_pdf(data.cotid);
	
					toastr["success"](data.msj);
	
				if(data.cot_act==1){
					cotizacion_ticket_datos(data.cotid);
				}
	
				abrirEnPestana('="../../../../files/cotizacion/cotizacion_imp_doc.php?cotid='+data.cotid);
			
				$('#div_cotizacion_form_dialog').modal('hide');
				
	
				},
				complete: function(){
					cotizacion_tabla();
				}
			});	
	  });
	  
	  
	  
	  
	}); 
	
	function abrirEnPestana(url) {
		var a = document.createElement("a");
		a.target = "_blank";
		a.href = url;
		a.click();
	}
	
	
</script>
<style>
	.alertar{
		outline: 2px solid white;
		animation: alertar 1000ms;
		animation-iteration-count: infinite;
	}
	@keyframes alertar{
		0% {outline-color: white}
		50% {outline-color: red}
		100% {outline-color: white}
	}
	#div_bus_imgs{
		position: absolute;
		height: 54px;
		width: 54px;
		right: 5px;
		top: 14px;
		overflow: hidden;
		border: 2px solid rgb(200,200,200);
		border-radius: 3px;
	}
	#div_bus_imgs .imglink img{
		width: 100%;
		height: 100%;
	}
/*	#for_ven  input[type=checkbox]{
		padding: 5px;
		width: 16px;
		height: 15px;
		margin: 0 !important;
	}*/
</style> 
<link rel="stylesheet" href="../../admin/dist/js/colorbox/example1/colorbox.css">
<script src="../../admin/dist/js/colorbox/jquery.colorbox-min.js"></script>