

 <?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 ("../venta/cVenta.php");
 $oVenta = new cVenta();

 require_once("../formatos/formato.php");

// echo date('Y-m-d h:i:s a');

 if($_POST['action']=="insertar")
 {
 	
		
	/* if($_SESSION['usuariogrupo_id']==1 || $_SESSION['usuariogrupo_id']==2){
		
	}
	else{} */
 	
	$punven_id = $_SESSION['puntoventa_id'];
 	
	if($_POST['vista']=='cotizacion'){
		
	unset($_SESSION['venta_catid']);
	unset($_SESSION['venta_marid']);
	unset($_SESSION['venta_des']);
	unset($_SESSION['venta_can']);
	unset($_SESSION['venta_preven']);	
		
		
	require_once ("../cotizacion/cCotizacion.php");
	$oCotizacion = new cCotizacion();
	
	$fec = date('d-m-Y');
	
	$dtscot= $oCotizacion->mostrarUno($_POST['cotid']);
	$dtcot= mysqli_fetch_array($dtscot);
	//CLIENTE
	$cli_id=$dtcot['tb_cliente_id'];
	$mon_id=$dtcot['tb_moneda_id'];
	
	mysqli_free_result($dtscot);
	
		
 	$est = 1;
 	
 	$venpag_fec = date('d-m-Y');
 	$modo_automatico = 0;
	$vend_id = 1;
	$ven_id = "";
	
	
	
	$dtsc=$oCotizacion->mostrarCotizaciondetalle($_POST['cotid']);
	$num_rows= mysqli_num_rows($dtsc);
	
	while($dtc = mysqli_fetch_array($dtsc))
    {
	
	$_SESSION['venta_catid'][$dtc['tb_catalogo_id'].$dtc['tb_marca_id']]= $dtc['tb_catalogo_id'];
	$_SESSION['venta_marid'][$dtc['tb_catalogo_id'].$dtc['tb_marca_id']]= $dtc['tb_marca_id'];
	$_SESSION['venta_can'][$dtc['tb_catalogo_id'].$dtc['tb_marca_id']]= $dtc['tb_cotizaciondetalle_can'];
	$_SESSION['venta_est'][$dtc['tb_catalogo_id'].$dtc['tb_marca_id']]='1';
	$_SESSION['venta_des'][$dtc['tb_catalogo_id'].$dtc['tb_marca_id']] = moneda_mysql('0');
	$_SESSION['venta_preven'][$dtc['tb_catalogo_id'].$dtc['tb_marca_id']]= moneda_mysql($dtc['tb_cotizaciondetalle_valuni']); 
	
	
	}
	
	
	
	
	}else{
		
		$fec = date('d-m-Y');
		$est = 1;
		$cli_id = 1;
		$mon_id = 1;
		$venpag_fec = date('d-m-Y');
		$modo_automatico = 0;
		$vend_id = 1;
		$ven_id = "";
	
	}
	
	
	
	
	
	
	
	
 }
 ?>
 
 <style type="text/css">
.ui-autocomplete
{
    z-index: 99999; 
}

input[type=checkbox] {
 cursor:pointer;
}

#chk_imprimir{
  visibility: hidden;
}

.div_checkbox {
    width: 60px;
    height: 20px;
    background: #00ACC1;
    margin: 0 40px;
    border-radius: 6px;
	padding:12px;
    position: relative;
  font-family: 'Aldrich', sans-serif; 
}
.div_checkbox:before {
    content: 'Sí';
    position: absolute;
    top: 5px;
    left: 12px;
    color: #fff;
    font-size: 13px;
  line-height: 15px;
  text-shadow: 0 0 2px #fff;
}

.div_checkbox:after {
    content: 'No';
	color: #FF1744;
    position: absolute;
    top: 5px;
	right: 5px;
	font-size: 13px;
	line-height: 15px;
  text-shadow: 0 0 1px #FF1744;
}

.div_checkbox label { 
    background: #FF1744;
    display: block;
	width: 25px;
	height: 15px;
	padding:10px;
	border-radius: 4px;
    transition: all .7s ease;
    cursor: pointer;
    position: absolute;
    top: 2px;
    z-index: 1;
    left: 5px;
}
.div_checkbox input[type=checkbox]:checked + label {
    left: 30px;
    background: #fff;
}



</style>
 			
			
			<div class="col-lg-12 col-md-12 col-12 " >
 			
 					<form id="for_ven" >

 						<input name="action_venta" id="action_venta" 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'] ?>">	
 						<input name="hdd_vis_id" id="hdd_vis_id" type="hidden" value="<?php echo $_POST['vista'] ?>">	
 						<input name="hdd_cot_id" id="hdd_cot_id" type="hidden" value="<?php echo $_POST['cotid'] ?>">	
						<input name="hdd_venpag_val" id="hdd_venpag_val" type="hidden" value="">
						<input name="hdd_imp_val" id="hdd_imp_val" type="hidden" value="">
					
				
				<div class="col-lg-12 col-md-12 col-12 " >
				<div class="row">
				
				
 						<div class="col-lg-7 col-md-12 col-sm-12 col-12 " style="padding:3px;"> 
 							
 							<div id="tabcliente" class="nav-tabs-custom">
 								<nav class="nav nav-tabs">
 									<a href="#tab_1" style="color:#000;" class="nav-item nav-link active"  data-toggle="tab" aria-expanded="false">Cliente</a>
 									<a href="#tab_2" style="color:#000;" class="nav-item nav-link "  id="formpagotitle" data-toggle="tab" aria-expanded="true">Forma de Pago</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 '' ?>" />
 											<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-4 col-12 errorprov ">
												<label for="txt_ven_cli_doc">RUC/DNI:</label>
												<div class="input-group">					
														<span id="" style=""  class="input-group-btn">
																<span id="btn_compag_agregar" style="cursor:pointer" class="btn btn-info btn-flat" onClick="cliente_form('insertar')"><i class="fa fa-plus"></i></span> 
														</span>																
														<input type="text" class="form-control" id="txt_ven_cli_doc" name="txt_ven_cli_doc" value="<?php echo $cli_doc?>" /> 
													</div>
												</div>
												-->

												<div class="col-lg-3 col-sm-3 col-12 errorprov ">
												<label for="txt_ven_cli_doc">RUC/DNI:</label>																											
														<input type="text" class="form-control form-control-sm" maxlength="11" id="txt_ven_cli_doc" name="txt_ven_cli_doc" value="<?php echo $cli_doc?>" /> 												
												</div>
												
												
 												<div class="col-lg-4 col-sm-4 col-12">
 													<div id="div_clientecuenta_detalle"></div>
 													<label for="txt_ven_cli_nom">Cliente/Raz. Social:</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-5 col-sm-5 col-12 div_direcciontext " style="display:none;" >
														<label for="txt_ven_cli_dir">Dirección:</label>																											
														<input type="text" class="form-control form-control-sm" id="txt_ven_cli_dir" name="txt_ven_cli_dir" value="<?php echo '' ?>" /> 												
												</div>
												
												<div class="col-lg-6 col-sm-6 col-6 div_direcciontext " style="display:none;" >
														<label for="txt_ven_cli_cor">Correo:</label>																											
														<input type="text" class="form-control form-control-sm" id="txt_ven_cli_cor" name="txt_ven_cli_cor" value="<?php echo '' ?>" /> 												
												</div>
												
												<div class="col-lg-6 col-sm-6 col-6 div_direcciontext " style="display:none;" >
														<label for="txt_ven_cli_tel">Telefono:</label>																											
														<input type="text" class="form-control form-control-sm" id="txt_ven_cli_tel" name="txt_ven_cli_tel" value="<?php echo '' ?>" /> 												
												</div>
												
												<div class="col-lg-5 col-sm-5 col-12 div_direccioncombo " style="display:none;" >
												<label for="cmb_ven_cli_dir">Dirección:</label>
												<div class="input-group">					
														<span id="" style=""  class="input-group-btn">
																<span id="btn_compag_agregar" style="cursor:pointer" class="btn btn-sm btn-success btn-flat" onclick="direccion_cliente('insertardireccion')"><i class="fa fa-building"></i></span> 
														</span>																
														<select name="cmb_ven_cli_dir" id="cmb_ven_cli_dir"  class="form-control form-control-sm" >
													<option value="">-</option>
													</select>
													</div>
												</div>
												
												
 											<!--	<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 form-control-sm" >
													<option value="">-</option>
													</select>
													   
 												</div>
												-->
				
												<div class="col-lg-6 col-sm-6 col-12 errorprov " style="display:none">
												<label for="cmb_ven_cli_vehi">Vehiculo:</label>
												<div class="input-group">					
														<span id="" style=""  class="input-group-btn">
																<span id="btn_compag_agregar" style="cursor:pointer" class="btn btn-primary btn-flat" onclick="vehiculo_cliente('insertarvehiculo')"><i class="fa fa-bus"></i></span> 
														</span>																
														<select name="cmb_ven_cli_vehi" id="cmb_ven_cli_vehi" class="form-control" >
															<option value="">-</option>
														</select>
													</div>
												</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>
 										</div>
 										
 										<div class="tab-pane " id="tab_2">
 											
 													<div class="row"> 
													
													<div class="col-lg-3 col-sm-4 col-12 form-group">
 														
														<label for="cmb_forpag_id">Forma:</label>
														
														<div class="input-group">
														
																<div class="icheck-primary div_chkcompag" >
																<input type="checkbox" id="chk_venpag_aut" >
																<label for="chk_venpag_aut">
																</label>
																</div>
														
 															<select name="cmb_forpag_id" id="cmb_forpag_id"  class="form-control form-control-sm">
 																<option value="1" selected="selected">CONTADO</option>
 																<option value="2">CREDITO</option>
 																
 															</select> 
														</div>
 													</div>
													
													<div id="div_modo" class="col-lg-3 col-sm-4 col-12">
															<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="7">YAPE</option>
															<option value="8">PLIN</option>
															<option value="2">DEPOSITO</option>
															<option value="4">TRANSFERENCIA</option>
															<option value="5">CHEQUE</option>
															<option value="6">TARJETA</option>
															</select> 
														</div>

													<div id="div_operacion" style="display:none" class="col-lg-3 col-sm-4 col-12"> 
 														<label class="lbl_venpag_numope">N° Operación:</label> 
 														<input type="text" name="txt_venpag_numope" autocomplete="off" id="txt_venpag_numope" class="form-control form-control-sm" >
 													</div>
				
													<div id="div_dia" style="display:none" class="col-lg-3 col-sm-3 col-12"> 
 														<label for="txt_venpag_numdia">N° Días:</label></br>
 														<input name="txt_venpag_numdia" id="txt_venpag_numdia" type="text" class=" form-control form-control-sm dias"  maxlength="3" >
 													</div>

 													<div id="div_fecven" style="display:none" class="col-lg-3 col-sm-3 col-12"> 
 														<label for="txt_venpag_fecven">F. Vencimiento:</label></br>
 														<input type="text" name="txt_venpag_fecven" id="txt_venpag_fecven"  maxlength="10" class=" form-control form-control-sm" readonly>
 													</div>
													
													<div class="col-lg-3 col-sm-3 col-xs-6" id="div_caj" >
														<label for="cmb_caj_id">Caja:</label>
														<select name="cmb_caj_id" id="cmb_caj_id" class="form-control form-control-sm"></select>
													</div>
													
													<div class="col-lg-3 col-sm-4 col-12">
 														<label for="txt_venpag_mon">Monto:</label> 
 														
														<div class="input-group"> 
 															<input name="txt_venpag_mon" type="text" class="form-control form-control-sm" id="txt_venpag_mon" style="text-align:right;" maxlength="10">
 														
															<span id="div_pago_agregar" style="display:none"  class="input-group-btn">
 																<a id="btn_venpag_agregar" class="btn btn-primary btn-flat btn-sm"  href="javascript:void(0)" onClick="pago_venta_agregar('agregar')">Agregar</a> 
 															</span>
 													   </div>
													
													</div>

 													

 												</div>
												
												<div class="col-lg-12 col-md-12 col-12 div_pagos_tabla">
 													<div id="div_venta_pago_car_tabla"></div>
 												</div>
 										
 										</div> 
 									</div>
 									
 								</div> 
 						</div>
 							

 						<div class="col-lg-5 col-md-12 col-sm-12 col-12" style="padding:3px;">

 								<div class="nav-tabs-custom">
 									<nav class="nav nav-tabs">
 									<a href="#tab_3" class="nav-item nav-link active"  data-toggle="tab" aria-expanded="false">Datos Generales</a>
								
										<div style="padding:5px;" class="float-right">
										<div class="div_checkbox float-left">
											<input type="checkbox" name="chk_imprimir" id="chk_imprimir" value="" />
											<label for="chk_imprimir"></label>
										</div>
											<strong class="" style="font-size:15px;">Imprimir documento</strong>
										</div>
							
							
									</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-6 d-none" <?php if ($_SESSION['usuariogrupo_id']=='1' || $_SESSION['usuariogrupo_id']=='2'){}else{ echo 'style="display:none"'; } ?> >
 													<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-6">
 													<label for="cmb_doc_id">Documento:</label>
 													<select name="cmb_doc_id" id="cmb_doc_id" class="form-control form-control-sm"></select>					 
 												</div>
 												<div class="col-lg-4 col-sm-4 col-6">
 													<label for="txt_ven_numdoc">N° Doc:</label>
 													<input name="txt_ven_numdoc" type="text" class="form-control form-control-sm " id="txt_ven_numdoc" 
													style="text-align:right; font-size:14px; color:#00ACC1; font-weight:bold;"  value="<?php echo $numdoc?>" >						 
 												</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 form-control-sm">
													</select>
													<input name="txt_punven_id" type="hidden" value="" id="txt_punven_id">	
											</div>

 											<!-- <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 form-control-sm" ></select>					 
 												</div> 
											-->
																							
												<div class="col-lg-4 col-sm-4 col-6">
													<label for="cmb_mone_id">Moneda:</label>  
													<!--
													<label class="pull-right" id="lbl_mone_val"></label>
													-->
													<select   name="cmb_mone_id" id="cmb_mone_id" class="form-control form-control-sm"></select>
													<input name="txt_mone_id" type="hidden" value="" id="txt_mone_id">	
														
												</div>
												
												
												
												</div> 

 											
 										</div>

 									</div>
 									<!-- /.tab-content -->
 								</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 class="row">
							
											<div class="col-lg-8 col-md-8 col-sm-8 col-12 ">

												<div class="row">

 												<div class="col-lg-2 col-sm-2 col-6">
 													<label for="txt_bus_pro_codbar" class="" style="color:#00ACC1" >COD. BAR.:</label> 
													<input name="txt_bus_pro_codbar" type="text" autocomplete="off" id="txt_bus_pro_codbar" autofocus class="form-control form-control-sm" >
 													<input name="hdd_com_numite" id="hdd_com_numite" type="hidden">
 												</div>

 												<div class="col-lg-1 col-sm-4 col-6 d-none">
													<label for="txt_bus_pro_cod">Codigo:</label>
													<input name="txt_bus_pro_cod" autocomplete="off" type="text" id="txt_bus_pro_cod"  class="form-control form-control-sm" >	
												</div>
												
												<div class="col-lg-2 col-sm-2 col-6">
													<label for="txt_bus_pro_mar">Marca:</label>
													<input name="txt_bus_pro_mar" autocomplete="off" type="text" id="txt_bus_pro_mar"  class="form-control form-control-sm" >	
												</div>
													
												<div class="col-lg-2 col-sm-2 col-6">
													<label for="txt_fil_pro_dat1">Busqueda:</label>
													<input name="txt_fil_pro_dat1" autocomplete="off" type="text" id="txt_fil_pro_dat1" class="form-control form-control-sm">
												</div>
							
												<div class="col-lg-2 col-sm-2 col-6">
													<label for="txt_fil_pro_dat2">-</label>
													<input name="txt_fil_pro_dat2" autocomplete="off" type="text" id="txt_fil_pro_dat2"  class="form-control form-control-sm">
												</div>
							
												<div class="col-lg-2 col-sm-2 col-6">
													<label for="txt_fil_pro_dat3">-</label>
													<input name="txt_fil_pro_dat3" autocomplete="off" type="text" id="txt_fil_pro_dat3"  class="form-control form-control-sm">
												</div>
							
												<div style="display: inline-block; white-space: nowrap;padding-top:26px;" class="col-lg-2 col-sm-2 col-12">
														<div class="btn-group btn-sm">

															<a title="Restablecer cotizacion" onClick="agregar_producto_lista();" id="btn_rest_car" class="btn btn-sm btn-primary" href="javascript:void(0)"><i class="fa fa-search"></i> </a>
															<button type="button" class="btn btn-primary btn-sm dropdown-toggle" data-toggle="dropdown">
																<span class="caret"></span>														
															</button>
															<ul class="dropdown-menu" role="menu">
																<a class="dropdown-item" onClick="agregar_producto('restablecer');" href="javascript:void(0)"> <i class="fa fa-filter"></i> Vaciar productos</a>
															</ul>
														</div> 
													</div>
					
 												</div> 

 												<div class="col-lg-12 col-md-12 col-12" style="padding:5px;"></div> 
									
												<div id="div_producto_tabla" style="overflow-y:scroll;max-height:300px;" ></div>


 											</div>


 											<div class="col-lg-4 col-md-4 col-sm-4 col-12 "> 
												<div id="div_venta_detalle_tabla"></div>
											</div>

 											</div>

 											</div>
 										
																	 
							</form>

					<!--
						<div class="box-footer float-right div_botones_venta ">
							<button type="button" data-dismiss="modal" class="btn btn-lg btn-default btn_cancel">RESTABLECER</button>
							<button class="btn btn-danger btn-lg" id="save_form">VENDER</button>
							<div class="col-lg-12 col-md-12 col-12" style="padding:5px;"></div> 
						</div>
						-->
					
 					
					</div>
 				



 		<script type="text/javascript"> 
		
		
		function abrirEnPestana(url) {
		var a = document.createElement("a");
		a.target = "_blank";
		a.href = url;
		a.click();
	}
	
		
		
	function agregar_producto_lista(codmar,dat1,dat2,dat3,dat4,mar){
		if($("#cmb_puntoventa_id").val()==''){
				
				toastr["info"]('Seleccione un punto de venta.');
				
			}else{
			
			//$('#div_productomarca_form_dialog').modal();
			
		var punven = $("#cmb_puntoventa_id").val();
		 //alert(punven);
		
		
			$.ajax({
				type: "POST",
				url: "../compra/seleccionarproducto1.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(),
					mar:mar || $("#txt_bus_pro_mar").val(),
					punven_id : punven,
					vista:'venta_form'
				}),
				beforeSend: function() {
					$('#div_producto_tabla').addClass("ui-state-disabled");
				},
				success: function(html){
					$('#div_producto_tabla').html(html);			
				},
				complete: function(){			
					$('#div_producto_tabla').removeClass("ui-state-disabled");
			
				}
			});
	}
	}
		
 			$(".btn_cancel").click(function() {
				venta_form();
			});
			

 			$('#txt_ven_fec').datepicker({
 				format: 'dd-mm-yyyy',
 				autoclose:true,
				endDate: '+0d',
				startDate: '-6d'

			});


 			//$("#txt_ven_numdoc").addClass("ui-state-active");

 			/* 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(){			
 					}
 				});
 			} */

 			function cmb_doc_id(idf,doc,tot){
 				
				if(doc==''){
					toastr["warning"]('Debes seleccionar un cliente.');
				}else{
				
				
				$.ajax({
 					type: "POST",
 					url: "../documento/cmb_doc_id.php",
 					async:true,
 					dataType: "html",                      
 					data: ({
 						mod_id: '3',
 						def : '1',
 						doc_id: idf,
						doc : doc,
						tot:tot,
 						vista:	'ven'
 					}),
 					beforeSend: function() {
 						$('#cmb_doc_id').html('<option value="">Cargando...</option>');
 					},
 					success: function(html){			
 						$('#cmb_doc_id').html(html);
 					},
 					complete: function(){
 						
 							txt_ven_numdoc();
 							verificar_cliente();

 						}
 					});
 			
				}
			}

 			function txt_ven_numdoc()
 			{
 				$.ajax({
 					type: "POST",
 					url: "../venta/venta_txt_numdoc.php",
 					async:false,
 					dataType: "json",                      
 					data: ({
 						doc_id: $('#cmb_doc_id').val()
 					}),
 					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: "../venta/venta_ver_serie.php",
		async:false,
		dataType: "json",                      
		data: ({
			doc_id: $('#cmb_doc_id').val(),
			numdoc: $('#txt_ven_numdoc').val(),
			ven_est: $('#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> <br> <span class='spinner-border spinner-border-lg mr-2'></span> <span style='font-size:1.5rem;'>PROCESANDO PETICIÓN</span></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){
			$('.div_direcciontext').hide(100);
			$('.div_direccioncombo').show(100);
			$('#hdd_ven_cli_id').val(idf);	
			$('#txt_ven_cli_nom').val(data.nombre);	
			$('#txt_ven_cli_doc').val(data.documento);						
			$('#txt_ven_cli_dir').val(data.direccion);
			$("#hdd_ven_cli_tip").val(data.tipo);
				
				
				cargar_direcciones(idf);
				cargar_vehiculos(idf);
			
				doc = 	data.documento;
				tot = 	$('#txt_ven_tot').val();
				cmb_doc_id('',doc,tot);
			
		}
	});		
}


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 direccion_cliente(act){
	
	if($("#hdd_ven_cli_id").val()==""){
		toastr["error"]('Busque y seleccione un cliente.');
	}
	else{
	idcli = $("#hdd_ven_cli_id").val();
	$('#div_clientedireccion_form_dialog').modal();
	$.ajax({
		type: "POST",
		url: "../cliente/clientedir_form.php",
		async:true,
		dataType: "html",                      
		data: ({
			action: act,
			cli_id:	idcli,
			vista:	'ventas'
		}),
		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_clientedireccion_form').html(div);
        },
		success: function(html){
			$('.div_clientedireccion_form').html(html);		
		}
	});
	}
}


function vehiculo_cliente(act){
	if($("#hdd_ven_cli_id").val()==""){
		toastr["error"]('Busque y seleccione un cliente.');
	}
	else{
		idcli = $("#hdd_ven_cli_id").val();
	$('#div_clientevehiculos_form_dialog').modal();
	$.ajax({
		type: "POST",
		url: "../cliente/clientevehi_form.php",
		async:true,
		dataType: "html",                      
		data: ({
			action: act,
			cli_id:	idcli,
			vista:	'ventas'
		}),
		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_clientevehiculos_form').html(div);
        },
		success: function(html){
			$('.div_clientevehiculos_form').html(html);		
		}
	});
	}
}
	


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 limpiar_campos()
{
	$('#hdd_bus_cat_id').val('');

	$('#txt_bus_pro_codbar').val('');
	$('#txt_bus_pro_nom_soles').val('');
	$('#txt_bus_pro_nom_dolares').val('');
	$('#hdd_bus_pro_nom').val('');
	$('#txt_bus_cat_preven').val('');
	$('#txt_bus_cat_can').val('1');
	$('#txt_bus_cat_alm').val('');
	$('#txt_bus_cat_premin').val('');
	$('#txt_bus_cat_premay').val('');

	$('#cmb_bus_uni_id').html('<option value="">-</option>');

	$('#txt_bus_pro_codbar').focus();
}

function agregar_producto(act,idf, cantidad,preven,marid,des,mon,emp,est)
{
	// alert(almid);
	
	$.ajax({
		type: "POST",
		url: "../venta/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,	
			cat_des: des,	
			est: est,	
			mon: $("#txt_mone_id").val(),
			punven_id: $("#cmb_puntoventa_id").val(),			
			doc_id: $("#cmb_doc_id").val()			
		}),
		beforeSend: function() {

		},
		success: function(data){
			
			if(data.est==false)
			{
				toastr["info"](data.msj);
					
				if(data.dat.tipo=="interno"){
					
				}
				
				if(data.dat.tipo=="cat_can")
				{
					if(idf>0)
					{
						$('#txt_bus_cat_can').focus();
					}
					else
					{
						limpiar_campos();
					}
				}
				if(data.dat.tipo=="cat_premin")
				{	
					if(!(cantidad||preven)){
						$('#txt_bus_cat_preven').val($('#txt_bus_cat_premin').val());
						$('#txt_bus_cat_preven').focus();
					}else{
						limpiar_campos();
					}
				}
				if(data.dat.tipo=="cat_sto")
				{	
					if(!(cantidad||preven))
						$('#txt_bus_cat_can').val(data.dat.stock);
					
					if($('#chk_modo').is(':checked'))
					{
						limpiar_campos();
					}
					else
					{	
						if(!(cantidad||preven))
							$('#txt_bus_cat_can').focus();
						else limpiar_campos();
					}
					
				}
				if(data.dat.tipo=="cat_sto2")
				{
					limpiar_campos();
				}
			}else if(data.est==true)
			{
				if(data.msj==''){
					
				}else{
					toastr["success"](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;?>);
			}
			
		//	$('#txt_bus_pro_codbar').focus();
			
		}
	});
	
	}
	

function venta_detalle_tabla(ven_id){
		$.ajax({
			type: "POST",
			url: "../ventapos/ventapos_detalle_tabla_insertar.php",
			async:true,
			dataType: "html",                      
			data: ({
				ven_id:	ven_id,
				cli_id:	$("#hdd_ven_cli_id").val()
				//vista:'<?php echo $_POST['vista']; ?>'
			}),
			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");
				monto_venpag();
			}
		});     
}


function ver_fecven(){	
	$.ajax({
		type: "POST",
		url: "../venta/ver_fecven.php",
		async:false,
		dataType: "json",                      
		data: ({
			ven_fec: 		$('#txt_ven_fec').val(),
			venpag_numdia: 	$('#txt_venpag_numdia').val()
		}),
		beforeSend: function() {
					
		},
		success: function(data){
			$('#txt_venpag_fecven').val(data.fecha);
		}
	});
}



function pago_venta_agregar(act,idf,tabla='1')
{
	$.ajax({
		type: "POST",
		url: "../venta/pago_venta_agregar.php",
		async:false,
		dataType: "json",                      
		data: ({
			action:	 act,
			item_id:	idf,
			venpag_mon: $('#txt_venpag_mon').val(),
			venpag_for: $('#cmb_forpag_id').val(),
			venpag_mod: $('#cmb_modpag_id').val(),
			venpag_numope: $('#txt_venpag_numope').val(),
			venpag_numdia: $('#txt_venpag_numdia').val(),
			venpag_fecven: $('#txt_venpag_fecven').val(),
			venpag_cajid: $('#cmb_caj_id').val(),
			
			ven_tot: $('#txt_ven_tot').val(),
			venpag_tot: $('#hdd_venpag_tot').val()						
		}),
		beforeSend: function() {
			
		},
		success: function(data){
			if(data.est==false)
			{

				toastr["success"](data.msj);	

				if(data.dat.tipo=="venpag_mon")
				{
					$('#txt_venpag_mon').val(data.dat.mon);
					$('#txt_venpag_mon').focus();
				}
				if(data.dat.tipo=="venpag_mod")
				{
					$('#cmb_modpag_id').focus();
				}
				if(data.dat.tipo=="venpag_numope")
				{
					$('#txt_venpag_numope').focus();
				}
				if(data.dat.tipo=="venpag_numdia")
				{
					$('#txt_venpag_numdia').focus();
				}
			}
			if(data.est==true)
			{
				if(data.dat.tipo=="agregado")
				{
					$('#txt_venpag_mon').focus();
				}
			}
		},
		complete: function(){			
			pago_venta_tabla();
		}
	});

}

function pago_venta_tabla(act)
{
	$.ajax({
		type: "POST",
		url: "../venta/pago_venta_tabla.php",
		async:false,
		dataType: "html",                      
		data: ({
			action:	 act,
			ven_tot: $('#txt_ven_tot').val()					
		}),
		beforeSend: function() {
			$('#div_venta_pago_car_tabla').addClass("ui-state-disabled");
		},
		success: function(html){
			$('#div_venta_pago_car_tabla').html(html);
		},
		complete: function(){			
			$('#div_venta_pago_car_tabla').removeClass("ui-state-disabled");

			monto_venpag();
		}
	});

}

function monto_venpag()
{
	var total= $('#txt_ven_tot').val();
	$('#txt_venpag_mon').val(total);

	//alert(total);

	if($('#chk_venpag_aut').is(':checked'))
	{		
		var dif = $('#hdd_venpag_dif').val();
		$('#txt_venpag_mon').val(dif);
	}
	
}


function venpag_aut()
{
	if($('#chk_venpag_aut').is(':checked'))
	{  
		$("#div_pago_agregar").show(100);
		pago_venta_tabla('actualizar');

		$("#hdd_ven_pag_numite").attr('disabled', false);
		$("#hdd_venpag_tot").attr('disabled', false);
		$("#hdd_venpag_val").val('1');
		$("#txt_venpag_mon").attr('readonly',false);
		
		$("#cmb_forpag_id").val('2');
		
		//$("#cmb_forpag_id").attr('disabled', true);
		forpag_id();
	}
	else 
	{
		$("#div_pago_agregar").hide(100);

		pago_venta_agregar('restablecer');
		$('#div_venta_pago_car_tabla').html('');

		$("#hdd_ven_pag_numite").attr('disabled', 'disabled');
		$("#hdd_venpag_tot").attr('disabled', 'disabled');
		$("#hdd_venpag_val").val('');
		$("#txt_venpag_mon").attr('readonly',true);
		
		$("#cmb_forpag_id").val('1');
		$("#cmb_forpag_id").attr('disabled', false);
		forpag_id();
		
	}
	/* $("#txt_venpag_mon").focus(); */
}

function forpag_id()
{
	var tipo = $("#cmb_forpag_id").val();

	$('#txt_venpag_numope').val('');
	$('#txt_venpag_numdia').val('');
	$('#txt_venpag_fecven').val('');

	$("#div_modo").hide(100);
	$("#div_tarjeta").hide(100);
	$("#div_cuentadeposito").hide(100);			
	$("#div_operacion").hide(100);
	$("#div_dia").hide(100);
	$("#div_fecven").hide(100);
	
	//contado
	if(tipo == 1)
	{		
		$("#div_modo").show(100);
		$("#div_caj").show(100);
	}
	//credito
	if(tipo == 2){			
		$("#div_dia").show(100);
		$("#div_fecven").show(100);	
		$("#div_operacion").hide(100);
		$("#div_caj").hide(100);
	}	

}




/* 
function modpag_id()
{
	var tipo = $("#cmb_modpag_id").val();

	$('#cmb_tar_id').val('');
	$('#cmb_cuedep_id').val('');
	$('#txt_venpag_numope').val('');
	
	$("#div_tarjeta").hide(100);
	$("#div_cuentadeposito").hide(100);
	$("#div_operacion").hide(100);
	
	//efectivo
	if(tipo == 1)
	{
		
	}
	//tarjeta
	if(tipo == 2)
	{						
		$("#div_tarjeta").show(100);
		$("#div_operacion").show(100);
	}
	//deposito
	if(tipo == 3)
	{
		$("#div_cuentadeposito").show(100);
		$("#div_operacion").show(100);
	}
	
}
 */
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$.:');
				}	
			}
		});
	}
 */


 function buscar_producto(codbar){
	 if(codbar!=""){
		 
		$('#div_mensaje_dialog').modal('show');
	$.ajax({
			type: "POST",
			url: "../producto/producto_codbar_data.php",
			async:true,
			dataType: "json",                      
			data: ({
				codbar: codbar
			}),
			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.est=='1'){
				agregar_producto('agregar',data.catid,data.can,'',data.marid,'','','','');
				
			}else{
			toastr["error"]('No existe ningun porducto con ese COD. BAR.');	
			}
			
			$('#div_mensaje_dialog').modal('hide');
				
			}
		});
	 
	}else{
		toastr["error"]('Ingrese un codigo de barras.');	
	}
 
 }
    


	
function validar_campos(modid){
	
	
	var mon_id = $("#txt_mone_id").val();
	var punven_id = $("#txt_punven_id").val();
	

	if(modid==""){

		
		$("#div_operacion").hide(100);
	
	}else if(modid==1){
	

		$(".lbl_venpag_numope").text('Nº Operacion:');
		$("#div_operacion").hide(100);
		cmb_caja_id('1',punven_id,mon_id);
		
	}else if(modid==2){
		
		$(".lbl_venpag_numope").text('Nº Operacion:');
		$("#div_operacion").show(100);
		cmb_caja_id2(1,mon_id,'2');
		
	}else if(modid==3){
		
		$(".lbl_venpag_numope").text('Nº Operacion:');
			$("#div_operacion").show(100);

	}else if(modid==4){
		
		$(".lbl_venpag_numope").text('Nº Operacion:');
			$("#div_operacion").show(100);
			cmb_caja_id2(1,mon_id,'2');

	}else if(modid==5){
		
		$(".lbl_venpag_numope").text('Nº Cheque:');
			$("#div_operacion").show(100);
			cmb_caja_id('',punven_id,mon_id);

	}
	else if(modid==6){
		$(".lbl_venpag_numope").text('Nº Operacion:');
			$("#div_operacion").show(100);
			cmb_caja_id2(1,mon_id,'2');
	}else if(modid==7){
		$(".lbl_venpag_numope").text('Nº Operacion:');
			$("#div_operacion").show(100);
			cmb_caja_id3(punven_id,mon_id,'3','YAPE');
	}
	else if(modid==8){
		$(".lbl_venpag_numope").text('Nº Operacion:');
			$("#div_operacion").show(100);
			cmb_caja_id3(punven_id,mon_id,'3','PLIN');
	}
}


 
function cmb_caja_id3(punven_id,moneid,tip,ban_nom)
	{	
		$.ajax({
			type: "POST",
			url: "../caja/cmb_cajaxpuntoventa_id3.php",
			async:true,
			dataType: "html",                      
			data: ({
				punven_id: punven_id,
				mon_id: moneid,
				tip: tip,
				bannom : ban_nom,
				vista : 'venta'
			}),
			beforeSend: function(){
				$('#cmb_caj_id').html('<option value="">Cargando...</option>');
			},
			success: function(html){
				$('#cmb_caj_id').html(html);
			}
		});
	}




function cmb_caja_id(tip,punven_id,moneid)
	{	
		$.ajax({
			type: "POST",
			url: "../caja/cmb_cajaxpuntoventa_id.php",
			async:true,
			dataType: "html",                      
			data: ({
				tip : tip,
				punven_id: punven_id,
				mon_id: moneid
			}),
			beforeSend: function(){
				$('#cmb_caj_id').html('<option value="">Cargando...</option>');
			},
			success: function(html){
				$('#cmb_caj_id').html(html);
			}
		});
	}
	
function cmb_caja_id2(punven_id,moneid,tip)
	{	
		$.ajax({
			type: "POST",
			url: "../caja/cmb_cajaxpuntoventa_id2.php",
			async:true,
			dataType: "html",                      
			data: ({
				punven_id: punven_id,
				mon_id: moneid,
				tip: tip // 1 : bancos // 2 : detracciones
			}),
			beforeSend: function(){
				$('#cmb_caj_id').html('<option value="">Cargando...</option>');
			},
			success: function(html){
				$('#cmb_caj_id').html(html);
			}
		});
	}




 

function buscarruc(){	
	toastr["info"]('Buscando datos...');
	 $('#div_mensaje_dialog').modal('show');
	$.ajax({	
		type: "POST",					
		url: "../../libreriasphp/sunat/consulta.php",
		async:true,
		dataType: "json",                      
		data: ({
			ruc: $("#txt_ven_cli_doc").val()
		}),
		beforeSend: function() {
			var divmen = "<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(divmen);
		},
		success: function(data){
			
			$('#div_mensaje_dialog').modal('hide');
			
					doc = 	$("#txt_ven_cli_doc").val();
					tot = 	$('#txt_ven_tot').val();
					cmb_doc_id('',doc,tot);
			
				var datos = eval(data);	
				
				$('#txt_ven_cli_nom').val('');
				$('#txt_ven_cli_dir').val('');
				
				if(datos['api_est']==true){
					$('.div_direcciontext').show(100);
					$('.div_direccioncombo').hide(100);
					$('#txt_ven_cli_doc').val(datos['api_doc']);
					$('#txt_ven_cli_nom').val(datos['api_nom']);
					$('#txt_ven_cli_dir').val(datos['api_dir']);
					toastr["success"](datos['api_msj']);
				}else{
					
					if(datos['api_estapi']==0 ){
						toastr["warning"](datos['api_msj']);
					}else if(datos['api_estapi']==1 ){
						toastr["error"](datos['api_msj']);
					}
					
				}
				
			
		},
		complete: function(){			
		
		}
	});   
		
}



function buscardni(){	
	toastr["info"]('Buscando en Reniec...');
	 $('#div_mensaje_dialog').modal('show');
	$.ajax({	
		type: "POST",					
		url: "../../libreriasphp/reniec/consulta.php",
		async:true,
		dataType: "json",                      
		data: ({
			dni: $("#txt_ven_cli_doc").val()
		}),
		beforeSend: function() {
			var divmen = "<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(divmen);
		},
		success: function(data){
			
					doc = 	$("#txt_ven_cli_doc").val();
					tot = 	$('#txt_ven_tot').val();
					cmb_doc_id('',doc,tot);
			
				$('#div_mensaje_dialog').modal('hide');
				var datos = eval(data);
				var nada ='nada';
				if(datos[0]==nada){
				
					toastr["error"]('DNI no válido o no registrado');
					
				}else{
					$('.div_direcciontext').show(100);
					$('.div_direccioncombo').hide(100);
					
					if(datos[4] == null ){
						toastr["error"]('Dni no encontrado o servicio no disponible');
						$('#txt_ven_cli_nom').val('');
						
						$('#txt_ven_cli_dir').val('');
						cmb_doc_id('','','');
					}else{
					
					
					$('#txt_ven_cli_doc').val(datos[0]);
					$('#txt_ven_cli_nom').val(datos[4] +' '+ datos[2]+' '+ datos[3]);
					$('#txt_ven_cli_dir').val('--');
					toastr["success"]('Datos encontrados.');
				}	
			}	
				
		},
		complete: function(){			
		
		}
	});   
		
}

 
 
$(function() {
	
	cliente_cargar_datos('<?php echo $cli_id?>');
	
	cmb_puntoventa_id('<?php echo $punven_id;?>');
	venta_detalle_tabla('<?php echo $ven_id;?>');
	
	cmb_mone_id(<?php echo $mon_id?>);
	
	cmb_caja_id('1',1,1);
	
	//cmb_doc_id('<?php echo '' ?>','','');

<?php if($_POST['vista']=='cotizacion'){}else{?>
	agregar_producto('restablecer');
<?php }?>
	agregar_producto('restablecer');
	pago_venta_agregar('restablecer','',0);

	
	$("#cmb_puntoventa_id").change(function(){
			
					$("#txt_punven_id").val($("#cmb_puntoventa_id").val());
		});
	
	
	$('#cmb_doc_id').change( function(){
		txt_ven_numdoc();
		verificar_cliente();
	});
	$("#cmb_doc_id").keydown(function(e){
		if (e.which == 13){$("#txt_ven_cli_nom").focus();return false;}
	});



	$('#txt_venpag_numdia').keydown(function(e){ 
		if (e.which == 13){
			if($('#txt_venpag_numdia').val()!="")
			{
				ver_fecven();
			}
			else
			{
				$('#txt_venpag_fecven').val('');
			}	
			
			
			return false;}
		});


	$("#cmb_ven_est").change(function(){
		
		verificar_numdoc();	
		var est = $("#cmb_ven_est").val();

		if(est==2){			
			$("#hdd_ven_numite").attr('disabled', 'disabled');
			$("#hdd_ven_cli_id").attr('disabled', 'disabled');	
		}
		if(est==1){			
			$("#hdd_ven_numite").attr('disabled', false);
			$("#hdd_ven_cli_id").attr('disabled', false);
		}
		
	});

	$( "#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();
				
				if(ui.item.id!=''){
					cliente_cargar_datos(ui.item.id);
					venta_detalle_tabla(<?php echo $ven_id;?>);
					return false;
				}else{
					
					
				/* 	$("#hdd_ven_cli_id").val('');
			if($(this).val().length == 8){
					buscardni();
			}else if ($(this).val().length == 11){
					buscarruc();
			}else{
				$(this).val('');
				toastr["error"]('Ingrese un Nº de documento valido');
				limpiar_inputs();
			} */
					
				}
				
				
				
			}
		});

	$( "#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();
				cliente_cargar_datos(ui.item.id);
				venta_detalle_tabla(<?php echo $ven_id;?>);
				
			}
		});
		
	$('#txt_ven_cli_nom').keydown(function(e){
		if (e.which == 13){return false;}
	});
	
	$('#txt_ven_cli_nom').keydown(function(e){
		$("#hdd_ven_cli_id").val('');
	});
	
	$('#txt_ven_cli_doc').keydown(function(e){
		if (e.which == 13){
			
			 
			if($(this).val().length == 8){
					buscardni();
			}else if ($(this).val().length == 11){
					buscarruc();
			}else{
				$(this).val('');
				toastr["error"]('Ingrese un Nº de documento valido');
				limpiar_inputs();
			} 
			
			return false;
			
			}
	});

$('#txt_ven_cli_doc').keydown(function(e){
	
	$("#hdd_ven_cli_id").val('');		
	
});


$('#txt_ven_cli_nom').blur(function(e){
		
		doc = 	$("#txt_ven_cli_doc").val();
		tot = 	$('#txt_ven_tot').val();
		cmb_doc_id('',doc,tot);
		
		$('.div_direcciontext').show(100);
		$('.div_direccioncombo').hide(100);
			
	});




		$("#cmb_mone_id").change(function(){
				
			if($("#cmb_mone_id").val()==1){		
			
					$("#txt_mone_id").val('1');
			
			}else if($("#cmb_mone_id").val()==2){
				
				$("#txt_mone_id").val('2');
				
			}	
		});
	
	$("#txt_bus_pro_codbar").keydown(function(e){
		if (e.which == 13){			
			
			buscar_producto($("#txt_bus_pro_codbar").val());
			
			return false;
		}
	});
		
	$("#txt_bus_pro_mar").keydown(function(e){
		if (e.which == 13){			
			
			agregar_producto_lista('','','','');
			
			return false;
		}
	});
		
		$("#txt_bus_pro_cod").keydown(function(e){
			if (e.which == 13){
			
			agregar_producto_lista('','','','');
				return false;
			}
		});

$('#txt_ven_cli_nom,#txt_ven_cli_dir').change(function()
{
	$(this).val($(this).val().toUpperCase());
});

$('#hdd_imp_val').val('0');
$('#chk_imprimir').change(function()
{
	if($('#chk_imprimir').is(':checked'))
	{		
		$('#hdd_imp_val').val('1');
	}else{
		$('#hdd_imp_val').val('0');
	}
});





	
		$( "#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();
				}	
					
			}
		});
		
	
	
	


		//FORMAS DE PAGO
		
		
		$("#txt_venpag_mon").attr('readonly',true);
		//cmb_tar_id('<?php echo $tar_id?>');
		//cmb_cuedep_id('<?php echo $cuedep_id?>');
		
		$("#chk_venpag_aut").click(function(){
			venpag_aut();
			
		});
		 
		// $('#chk_venpag_aut').on('ifChecked', function(event){
			// alert(event.type + ' callback');
		// });
		
		$("#cmb_mone_id").change(function(){
			verTipcambio($('#cmb_mone_id').val());
		});
		
		$("#cmb_forpag_id").change(function(){		
			forpag_id();
		});
		
		$("#cmb_modpag_id").change(function(){		
			validar_campos($("#cmb_modpag_id").val());
		});

		$("#txt_venpag_mon").keydown(function(e){
			if (e.which == 13){
				if($("#txt_venpag_mon").val()>0)
				{
					$("#cmb_forpag_id").focus();
				}
				else
				{
					$("#txt_bus_pro_codbar").focus();
				}				
				return false;
			}
		});

		$("#cmb_forpag_id").keydown(function(e){
			if (e.which == 13){
				if($("#cmb_forpag_id").val()==1)$("#cmb_modpag_id").focus();
				if($("#cmb_forpag_id").val()==2)$("#txt_venpag_numdia").focus();
				return false;
			}
		});

		/* $("#cmb_modpag_id").keydown(function(e){
			if (e.which == 13){
				if($("#cmb_modpag_id").val()==1)$("#btn_venpag_agregar").focus();
				if($("#cmb_modpag_id").val()==2)$("#cmb_tar_id").focus();
				if($("#cmb_modpag_id").val()==3)$("#cmb_cuedep_id").focus();
				return false;
			}
		}); */

	
		$("#txt_venpag_numope").keydown(function(e){
			if (e.which == 13){$("#btn_venpag_agregar").focus();return false;}
		});
		
		$('#txt_venpag_numdia').change( function() {
			if($('#txt_venpag_numdia').val()!="")
			{
				ver_fecven();
			}
			else
			{
				$('#txt_venpag_fecven').val('');
			}
		});

		$("#txt_venpag_numdia").keydown(function(e){
			if (e.which == 13){$("#btn_venpag_agregar").focus();return false;}
		});
		

	$('#for_ven').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'
	      			}
	      		}
	      	},
	      	  
	      	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_doc: {
	      		group: '.col-lg-3',
	      		validators: {
	      			notEmpty: {
	      				message: 'Existe registro con mismo N° Documento.'
	      			}
	      		}
	      	},
	      }
	  }).on('success.form.bv', function(e) {
		  
	  	
		
					
	  	$.ajax({
	  		type: "POST",
	  		url: "../venta/venta_reg.php",
	  		async:false,
	  		dataType: "json",
	  		data: $("#for_ven").serialize(),
	  		beforeSend: function(){
				
				
				
				},
				success: function(data)
				{

					
					//$('#div_mensaje_dialog').modal('hide');
					
					if(data.ven_est==0){
						toastr["error"](data.ven_msj);
					}else{
					
					toastr["success"](data.ven_msj);
					
					ventapos_form();				

					
					venta_ticket_datos(data.ven_id,data.ven_act);		
						
					

					
					
					}
				},
				complete: function(){
					
					$(".div_botones_venta").show(100);
				}
			});	
	  });
	  
	  

	  
	}); 


</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>