 <?php
 session_start();
 date_default_timezone_set('America/Lima');
 require_once ("../../config/Cado.php");

 require_once ("../venta/cVenta.php");
 $oVenta = new cVenta();

 require_once ("../producto/cCatalogo.php");
 $oCatalogo = new cCatalogo();

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

// echo date('Y-m-d h:i:s a');
 // echo $_POST['ven_id'].'--';
 
 if($_POST['action']=="insertar")
 {
	if($_POST['vista']=='guia'){ 
	 
 	$fec = date('d-m-Y');
 	$est = 1;
 	$doc_id = 23;
 	$con_id = 1;
 	$cli_id = 1289;
 	$mon_id = 1;
 	$venpag_fec = date('d-m-Y');
 	$modo_automatico = 0;
	$vend_id = 1;
	$ven_id = '';
	$numdoc = '';
	}
	else if($_POST['vista']=='venta')
	{
	
	$ven_id = $_POST['ven_id'];
	
	$dts = $oVenta->mostrarUno($_POST['ven_id']);
	$dt = mysqli_fetch_array($dts);
	$reg = mostrarFechaHora($dt['tb_venta_reg']);
	$fec = $fec = date('d-m-Y');  // mostrarFecha($dt['tb_venta_fec']);

	$doc_id	= $dt['tb_documento_id'];
	$numdoc	= $dt['tb_venta_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'];
	$mon_id = $dt['tb_moneda_id'];
	$tipcam = $dt['tb_venta_tipcam'];
	$valven	= $dt['tb_venta_valven'];
	$igv	= $dt['tb_venta_igv'];
	$tot	= $dt['tb_venta_tot'];
	$est	= $dt['tb_venta_est'];

	$emp_nom	= $dt['tb_empresa_nomcom'];
	$emp_dir	= $dt['tb_empresa_dir'];
	$piso_nom	= $dt['tb_piso_nom'];
	$almacen_nom	= $dt['tb_almacen_nom'];


	// $ven_id	= $dt['tb_vendedor_id'];

	$lab1	= $dt['tb_venta_lab1'];
	$may	= $dt['tb_venta_may'];

	mysqli_free_result($dts);
	
	
	
	
	}
 }
 ?>
 
 <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-xs-12 " >
 			
 					<form id="for_guia" >

 						<input name="action" id="action" type="hidden" value="<?php echo $_POST['action']?>">
 						<input name="hdd_guia_id" id="hdd_guia_id" type="hidden" value="<?php echo $_POST['guiaid']?>">
						<input type="hidden" id="hdd_numdoc" name="hdd_numdoc" value="<?php echo $numdoc?>"  class="" />			 
						<input type="hidden" id="hdd_venta_id" name="hdd_venta_id" value="<?php echo $ven_id?>"  class="" />			 
 						
							
					<div class="col-lg-12 col-md-12 col-xs-12 " style="padding:5px;">
					<div class="row">
					
					<div class="col-lg-5 col-md-6 col-xs-12 float-left">
					<h3 >REGISTRAR GUIA DE REMISION <span class="text-red"><?php // echo $_SESSION['empresa_nombre'] ?></span> </h3>
					</div>
					
					<div class="col-lg-5 col-md-6 col-xs-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-xs-12 " >
				<div class="row">
				
 						<div class="col-lg-5 col-md-12 col-xs-12 "> 
 							
 							<div id="tabcliente" class="nav-tabs-custom">
 							
								<ul class="nav nav-tabs">
								<a href="#tab_1" class="nav-item nav-link active"  data-toggle="tab" aria-expanded="false">Destinatario</a>
 								<!--	<li class=""><a href="#tab_4" id="formpagotitle" data-toggle="tab" aria-expanded="true">Conductor/Transporte</a></li>  
 									<li class=""><a href="#tab_5" id="formpagotitle" data-toggle="tab" aria-expanded="true">Operacion/Vehiculo</a></li>  
 								-->
								</ul>
							
 								<div class="tab-content">
								
										<div class="active tab-pane" id="tab_1">
 										<div class="row"> 

 											<input type="hidden" id="hdd_guia_cli_id" name="hdd_guia_cli_id" value="<?php echo $cli_id?>" />
 											<input type="hidden" id="hdd_guia_cli_tip" name="hdd_guia_cli_tip" value="<?php echo $cli_tip?>" />
 											
											<?php if($_POST['vista']=='venta'){
												?>
												
												
												<div class="col-lg-5 col-sm-4 col-xs-12">															
												<label for="txt_guia_cli_doc">RUC/DNI:</label>
 												<input name="txt_guia_cli_doc" type="text" id="txt_guia_cli_doc" value="<?php echo $cli_doc?>" readonly class="form-control input-sm"> 
 												</div>
												
												<?php 
											}else{ ?>
											<div class="col-lg-5 col-sm-4 col-xs-12">															
												<label for="txt_guia_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-primary btn-flat" onClick="cliente_form('insertar')"><i class="fa fa-plus"></i></span> 
														</span>	
 														
 														<input name="txt_guia_cli_doc" type="text" id="txt_guia_cli_doc" value="<?php echo $cli_doc?>" readonly class="form-control input-sm"> 
 													</div>
 												</div>
											<?php }?>

 												<div class="col-lg-7 col-sm-8 col-xs-12">
 													<label for="txt_guia_cli_nom">Cliente:</label>
 													<input type="text" id="txt_guia_cli_nom" name="txt_guia_cli_nom" readonly  value="<?php echo $cli_nom?>" class="form-control input-sm" />
 												</div> 
 											
 											<!--
 												<div class="col-lg-12 col-sm-4 col-xs-12">
 													<label for="txt_guia_cli_dir">Dirección:</label>
 													<input type="text" id="txt_guia_cli_dir" name="txt_guia_cli_dir" value="<?php echo$cli_dir;?>" readonly class="form-control input-sm" />			 
 												</div>
											-->	
 											<div class="col-lg-12 col-sm-12 col-xs-12" style="height:5px;" ></div>
 											<div class="col-lg-12 col-sm-12 col-xs-12 bg-red" style="height:2px;" ></div>
											<div class="col-lg-12 col-sm-12 col-xs-12" style="height:5px;" ></div>
 											
												<div class="col-lg-6 col-sm-6 col-xs-12">
 													<label for="cmb_guia_mot">Motivo:</label>
													<select name="cmb_guia_mot" id="cmb_guia_mot" class="form-control input-sm" >
													
													<option value="-">-- Seleccione --</option>
													<?php if($_POST['vista']=='venta'){?>
													<option value="1" selected>VENTA</option>
													<?php }?>
													<?php if($_POST['vista']=='guia'){?>
													<option value="2" selected>COMPRA</option>
													<option value="4" selected>TRASLADO ENTRE ESTABLECIMIENTOS DE LA MISMA EMPRESA</option>
													<?php }?>
													</select>
												</div>
												
												<div class="col-lg-6 col-sm-6 col-xs-12">
 													<label for="txt_guia_numdoc_doc">Numero Documento:</label>
 													<input type="text" id="txt_guia_numdoc_doc" name="txt_guia_numdoc_doc" value="<?php echo $numdoc?>"  class="form-control input-sm" />			 
 												</div>
												
												</div>
 									</div>
 							<!--
										<div class="tab-pane" id="tab_4">
 										<div class="row"> 

 											<input type="hidden" id="hdd_guia_con_id" name="hdd_guia_con_id" value="<?php echo $con_id?>" />
 											<input type="hidden" id="hdd_guia_tra_id" name="hdd_guia_tra_id" value="<?php echo $tra_id?>" />
 											
											<div class="col-lg-5 col-sm-4 col-xs-12">															
												<label for="txt_guia_con_doc">RUC:</label>
 												<div class="input-group">
 													<div class="input-group-btn">
 														<button type="button" class="btn btn-warning btn-sm dropdown-toggle" data-toggle="dropdown" aria-expanded="false"><i class="fa fa-cog"></i>
 															<span class="fa fa-caret-down"></span></button>
 															<ul class="dropdown-menu">
 																<li><a  href="javascript:void(0)" onClick="conductor_form('insertar')"><i class="fa fa-plus"></i> Nuevo Conductor</a></li>										 
 																<li><a href="javascript:void(0)" onClick="cliente_form_i('editar',$('#hdd_ven_cli_id').val())"><i class="fa fa-edit"></i> Editar Cliente</a></li> 
 																
 															</ul>
 														</div>
 														
 														<input name="txt_guia_con_doc" type="text" id="txt_guia_con_doc" readonly value="<?php echo $con_doc?>"  class="form-control input-sm"> 
 													</div>
 												</div>


 												<div class="col-lg-7 col-sm-4 col-xs-12">
 													<label for="txt_guia_con_nom">Conductor:</label>
 													<input type="text" id="txt_guia_con_nom" name="txt_guia_con_nom"   value="<?php echo $con_nom?>" class="form-control input-sm" />
 												</div> 
 											
 											
 												<div class="col-lg-6 col-sm-4 col-xs-12">
 													<label for="txt_guia_con_dir">Dirección:</label>
 													<input type="text" id="txt_guia_con_dir" name="txt_guia_con_dir" readonly  value="<?php echo $con_dir?>"  class="form-control input-sm" />			 
 												</div>
												
												<div class="col-lg-6 col-sm-4 col-xs-12">
 													<label for="txt_guia_con_tra">Transporte:</label>
 													<input type="text" id="txt_guia_con_tra" name="txt_guia_con_tra" readonly value="<?php echo $con_dir?>"  class="form-control" />			 
 												</div>
 											
												<div class="col-lg-6 col-sm-4 col-xs-12">
 													<label for="txt_guia_con_lic">Licencia:</label>
 													<input type="text" id="txt_guia_con_lic" name="txt_guia_con_lic" readonly  value="<?php echo $con_nom?>" class="form-control input-sm" />
 												</div> 
												
												<div class="col-lg-6 col-sm-4 col-xs-12">
 													<label for="txt_guia_con_cat">Categoria:</label>
 													<input type="text" id="txt_guia_con_cat" name="txt_guia_con_cat" readonly  value="<?php echo $con_nom?>" class="form-control input-sm" />
 												</div> 
 											
												</div>
										</div>
										
									-->	
								<!--		
										<div class="tab-pane" id="tab_5">
											<div class="row"> 

												<div class="col-lg-6 col-sm-4 col-xs-12">
 													<label for="cmb_guia_mot">Motivo:</label>
													<select name="cmb_guia_mot" id="cmb_guia_mot" class="form-control input-sm" >
													<option value="-">-- Seleccione --</option>
													<option value="1" selected>VENTA</option>
													<option value="4">TRASLADO ENTRE ESTABLECIMIENTOS DE LA MISMA EMPRESA</option>
													</select>
												</div>
												
												<div class="col-lg-6 col-sm-4 col-xs-12">
 													<label for="txt_guia_numdoc_doc">Numero Documento:</label>
 													<input type="text" id="txt_guia_numdoc_doc" name="txt_guia_numdoc_doc" value="<?php echo $numdoc?>"  class="form-control input-sm" />			 
 													<input type="hidden" id="hdd_venta_id" name="hdd_venta_id" value="<?php echo $ven_id?>"  class="" />			 
 												</div>
											
											<div class="col-lg-12 col-sm-12 col-xs-12" >
											<div class="col-lg-12 col-sm-12 col-xs-12" style="height:5px;" ></div>
											<div class="col-lg-12 col-sm-12 col-xs-12 bg-green" style="height:2px;" ></div>
											</div>
											
												<div class="col-lg-6 col-sm-4 col-xs-12">
 													<label for="txt_guia_mar">Marca:</label>
 													<input type="text" id="txt_guia_mar" name="txt_guia_mar"  value="<?php echo ''?>" class="form-control" />
 												</div> 
 											
 												<div class="col-lg-6 col-sm-4 col-xs-12">
 													<label for="txt_guia_pla">Placa:</label>
 													<input type="text" id="txt_guia_pla" name="txt_guia_pla"  value="<?php echo ''?>"  class="form-control" />			 
 												</div>
 											
											</div>
										</div>
										
										-->
									</div>
 									
 								</div> 
 						</div>
 							

 							<div class="col-lg-7 col-md-12 col-sm-12 col-xs-12" >

 								<div class="nav-tabs-custom">
 									<!--
									<ul class="nav nav-tabs">
 										<li class="active"><a href="#tab_3" data-toggle="tab" aria-expanded="false">Datos Generales</a></li>
 										<li class=""> </li>  
 									</ul>
									-->
 									<div class="tab-content">
 										<div class="active tab-pane" id="tab_3">
 											<div class="row">

 												<div class="col-lg-3 col-sm-3 col-xs-6">
 													<label for="txt_guia_fec">Fecha:</label>
 													<input name="txt_guia_fec" type="text" class="form-control input-sm" id="txt_guia_fec" value="<?php echo $fec?>" >						 
 												</div>

 												<div class="col-lg-3 col-sm-4 col-xs-6">
 													<label for="cmb_doc_id">Documento:</label>
 													<select name="cmb_doc_id" id="cmb_doc_id" class="form-control input-sm"></select>					 
 												</div>
 												<div class="col-lg-3 col-sm-4 col-xs-6">
 													<label for="txt_guia_numdoc">N° Doc:</label>
 													<input name="txt_guia_numdoc" type="text" class="form-control input-sm text-yellow" id="txt_guia_numdoc" 
													style="text-align:right; font-size:14px"  value="<?php echo $numdoc?>" >						 
 												</div>
 											
 											

 											<!-- <div class="col-lg-6 col-sm-6 col-xs-6">
 													<label for="cmb_ven_id">Vendedor: </label>
 													<select name="cmb_ven_id" id="cmb_ven_id" class="form-control input-sm" ></select>					 
 												</div> 
											-->
 												
												<!--
													<label for="cmb_ven_est">Moneda:</label> <label class="pull-right" id="lbl_mone_val"></label>
													<select   name="cmb_mone_id" id="cmb_mone_id" class="form-control "></select>
													<input name="txt_ven_tipcam" type="hidden" value="" id="txt_ven_tipcam">	
 												-->
															
												
												
												<div class="col-lg-6 col-sm-4 col-xs-12">
 														<label class="txt_ori_guia">Punto Partida:</label>
 														<input name="txt_ori_guia" type="text" id="txt_ori_guia" value="<?php echo $emp_dir;?>" class="form-control">											 
 												</div>
												
												<div class="col-lg-6 col-sm-4 col-xs-12">
 														<label class="txt_des_guia">Punto Llegada:</label>
 														<input name="txt_des_guia" type="text" id="txt_des_guia" value="<?php echo $cli_dir;?>" class="form-control">											 
 												</div>
												
												<div class="col-lg-12 col-sm-4 col-xs-12">
 														<label class="txt_ate_guia">Atencion:</label>
												<input name="txt_ate_guia" type="text" id="txt_ate_guia" value="<?php echo $ate_guia;?>" class="form-control">											 
 																				 
 												</div>
												
												
												
												</div> 

 											
 										</div>

 									</div>
 									<!-- /.tab-content -->
 								</div>   
 							</div>
 							
							</div>
 						</div>
 							
							
							<?php if($_POST['vista']!='venta'){?>
 								
							<div class="col-lg-12 col-md-12 col-xs-12" style="height:20px;"> </div> 
									
							<div id="fieldset_producto" style="position: relative;" class="col-lg-12 col-md-12 col-xs-12 ">
 									<div class="box box-default box-solid box-primary ">
 										<div class="box-body">
 											<div class="row">

 												<div class="col-lg-1 col-sm-4 col-xs-6">
 													<label for="txt_bus_pro_codbar" class="text-red" >COD. BAR.:</label> 
														
 											<input name="txt_bus_pro_codbar" type="text" autocomplete="off" id="txt_bus_pro_codbar" autofocus class="form-control" >
 														<input name="hdd_com_numite" id="hdd_com_numite" type="hidden">
 												</div>

 												<div class="col-lg-1 col-sm-4 col-xs-6">
													<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" >	
												</div>
												
												<div class="col-lg-2 col-sm-4 col-xs-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" >	
												</div>
													
												<div class="col-lg-2 col-sm-3 col-xs-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">
												</div>
							
												<div class="col-lg-2 col-sm-3 col-xs-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">
												</div>
							
												<div class="col-lg-2 col-sm-3 col-xs-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">
												</div>
							
						<!--
							<div class="col-lg-2 col-sm-3 col-xs-6">
								<label for="txt_fil_pro_dat4">-</label>
								<input name="txt_fil_pro_dat4" autocomplete="off" type="text" id="txt_fil_pro_dat4"  class="form-control">
							</div>
							-->
																
 												<div style="display: inline-block; white-space: nowrap;padding-top:30px;" class="col-lg-2 col-sm-3 col-xs-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" onClick="agregar_producto_lista();" href="javascript:void(0)"> <i class="fa fa-filter"></i> Seleccionar productos</a>
															</ul>
														</div> 
													</div>
 													

 												</div> 

 											</div>
 										</div>
 									</div>
							
							
							<?php }?>		
 									
									
									
									<div class="col-lg-12 col-md-12 col-xs-12">
										<div class="col-lg-12 col-md-12 col-xs-12" style="height:20px;"> </div> 
										
 										<div id="div_venta_detalle_tabla"></div>
 									</div> 									 



 							</form>


 						
						
						<div class="box-footer float-right ">
							<button type="button" data-dismiss="modal" class="btn btn-default cancel">Cancelar</button>
							<button class="btn btn-primary" id="save_form">Guardar</button>
						</div>
				
 					</div>
 				



 		<script type="text/javascript"> 

		
		
			
	function agregar_producto_lista(codmar,dat1,dat2,dat3,dat4,mar){
			$('#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(),
					mar:mar || $("#txt_bus_pro_mar").val(),
					punven_id : '<?php echo $_SESSION['puntoventa_id']?>',
					vista:'compra_form'
				}),
				beforeSend: function() {
					var div = "<center><div class='overlay col-lg-12 col-sm-12 col-xs-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);				
				}
			});
		}
	
	
	
 			$("#save_form").click(function() {
				
				if($("#txt_des_guia").val()=="" || $("#txt_des_guia").val()=="-" || $("#txt_des_guia").val()=="--" )
				{
					toastr["warning"]('El punto de llegada tiene que tener una direccion valida.');
				}else{
						$("#for_guia").submit();
				}
				
				
 			});

 			$('#txt_guia_fec').datepicker({
 				format: 'dd-mm-yyyy',
 				autoclose:true			

			});


 			//$("#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){
 				$.ajax({
 					type: "POST",
 					url: "../documento/cmb_doc_id.php",
 					async:true,
 					dataType: "html",                      
 					data: ({
 						mod_id: '19',
 						def : '1',
 						doc_id: idf,
 						vista:	''
 					}),
 					beforeSend: function() {
 						$('#cmb_doc_id').html('<option value="">Cargando...</option>');
 					},
 					success: function(html){			
 						$('#cmb_doc_id').html(html);
 					},
 					complete: function(){
 						
 							txt_guia_numdoc();
 							verificar_cliente();

 						}
 					});
 			}

 			function txt_guia_numdoc()
 			{
 				$.ajax({
 					type: "POST",
 					url: "../venta/venta_txt_numdoc.php",
 					async:false,
 					dataType: "json",                      
 					data: ({
 						doc_id: $('#cmb_doc_id').val()
 					}),
 					beforeSend: function() {
 						$('#txt_guia_numdoc').val('Cargando...');
 					},
 					success: function(data){			
 						$('#txt_guia_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_guia_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><div class='overlay col-lg-12 col-sm-12 col-xs-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 conductor_form(act,idf){
	$('#div_conductor_form_dialog').modal();
	$.ajax({
		type: "POST",
		url: "../conductor/conductor_form.php",
		async:true,
		dataType: "html",                      
		data: ({
			action: act,
			con_id:	idf,
			vista:	'venta'
		}),
		beforeSend: function() {
			
			var div = "<center><div class='overlay col-lg-12 col-sm-12 col-xs-12' style='background-color:#fff;padding:0%;font-size:3rem;' >  <i class='fa fa-refresh fa-spin ' ></i> </div>  </center>";	
			$('.div_conductor_form').html(div); 
		},
		success: function(html){ 	
			$('.div_conductor_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_guia_cli_id').val(idf);	
			$('#txt_guia_cli_nom').val(data.nombre);	
			$('#txt_guia_cli_doc').val(data.documento);						
			$('#txt_guia_cli_dir').val(data.direccion);
			$("#hdd_guia_cli_tip").val(data.tipo);
		}
	});		
}


function conductor_cargar_datos(idf){	
	$.ajax({
		type: "POST",
		url: "../conductor/conductor_reg.php",
		async:true,
		dataType: "json",                      
		data: ({
			action: "obtener_datos",
			con_id:	idf
		}),
		beforeSend: function() {
		
		},
		success: function(data){
			$("#txt_guia_con_nom").val(data.nom);							
			$("#hdd_guia_con_id").val(idf);							
			$("#txt_guia_con_lic").val(data.lic);
			$("#txt_guia_con_cat").val(data.cat);
			$("#txt_guia_con_dir").val(data.dir);
			$("#txt_guia_con_doc").val(data.doc);
			$("#hdd_guia_tra_id").val(data.traid);
			$("#txt_guia_con_tra").val(data.trarazsoc);
		}
	});		
}

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>');
	$('#div_bus_imgs').html('');

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

function agregar_producto(act,catid,can,pre,marid,venid)
{
	// alert(venid);
		
	$.ajax({
		type: "POST",
		url: "../guiaremision/agregar_producto.php",
		async:true,
		dataType: "json",                      
		data: ({
			action:	act,
			cat_id:	catid,
			mar_id:	marid,
			guia_can: can,
			venid: venid			
		}),
		beforeSend: function() {

		},
		success: function(data){
			
			if(data.est==false)
			{
				toastr["info"](data.msj);
					
				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();
				}
			}
		},
		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();
				}
			
			}
			guia_detalle_tabla(<?php echo $guia_id;?>);
		}
	});
	
	
}

function guia_detalle_tabla(guia_id){
		$.ajax({
			type: "POST",
			url: "../guiaremision/guiaremision_detalle_tabla_insertar.php",
			async:true,
			dataType: "html",                      
			data: ({
				guia_id: guia_id,
				venid: '<?php echo $ven_id; ?>',
				vista: '<?php echo $_POST['vista']; ?>',
				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() {


	// $('input').iCheck({
		// checkboxClass: 'icheckbox_square-blue',
		// radioClass: 'iradio_square-blue',
		// increaseArea: '20%' 
		// });
	$('#txt_des_guia,#txt_ori_guia,#txt_obs_guia,#txt_guia_mar,#txt_ate_guia').change(function(){
		$(this).val($(this).val().toUpperCase());
	});
	
	cmb_doc_id('<?php echo $doc_id?>');
	
	/* cmb_ven_id('<?php echo $vend_id?>'); */
	
	// cliente_cargar_datos('<?php echo $cli_id?>');
	//conductor_cargar_datos('<?php echo $con_id?>');

	$("#cmb_doc_id").focus();
	<?php
	if($_POST['vista']=='guia'){ 
	?>
	agregar_producto('restablecer');
	cliente_cargar_datos('<?php echo $cli_id?>');
	guia_detalle_tabla('<?php echo $guia_id;?>');
	<?php
	}
	else if($_POST['vista']=='venta'){ 
	
	unset($_SESSION['guia_can']);
	unset($_SESSION['guia_catid']);
	unset($_SESSION['guia_marid']);
	
	?>
	$("#txt_guia_numdoc_doc").attr('disabled', true);
	// $("#cmb_guia_mot").attr('disabled', true);
	//agregar_producto('verdesdeventas','','','','','<?php echo $ven_id ?>');
	
	agregar_producto('agregardeventa','','','','',<?php echo $ven_id ?>);
	guia_detalle_tabla('<?php echo $guia_id;?>');
	<?php
	}
	?>
	
	$('#cmb_doc_id').change( function(){
		txt_guia_numdoc();
		verificar_cliente();
	});
	$("#cmb_doc_id").keydown(function(e){
		if (e.which == 13){$("#txt_ven_cli_nom").focus();return false;}
	});
	
	$("#txt_ate_guia,#txt_ori_guia,#txt_des_guia").keydown(function(e){
		if (e.which == 13){
			return false;
			}
	});

/* 	$('#cmb_ven_id').keydown(function(e){
		if (e.which == 13){$("#txt_ven_cli_nom").focus();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_guia_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();
				venta_detalle_tabla(<?php echo $ven_id;?>);
			}
		});

	$( "#txt_guia_cli_nom" ).autocomplete({
		minLength: 1,
		source: "../cliente/cliente_complete_nom.php",
		select: function(event, ui){			
			$("#hdd_guia_cli_id").val(ui.item.id);							
			$("#txt_guia_cli_doc").val(ui.item.documento);
			$("#txt_guia_cli_dir").val(ui.item.direccion);
			$("#hdd_guia_cli_tip").val(ui.item.tipo);
			$("#txt_bus_pro_codbar").focus();
			guia_detalle_tabla(<?php echo $guia_id;?>);
			}
		});
		
		$( "#txt_guia_con_nom" ).autocomplete({
		minLength: 1,
		source: "../conductor/conductor_complete_nom.php",
		select: function(event, ui){			
			$("#hdd_guia_con_id").val(ui.item.id);							
			$("#txt_guia_con_lic").val(ui.item.lic);
			$("#txt_guia_con_cat").val(ui.item.cat);
			$("#txt_guia_con_dir").val(ui.item.dire);
			$("#txt_guia_con_doc").val(ui.item.doc);
			$("#hdd_guia_tra_id").val(ui.item.traid);
			$("#txt_guia_con_tra").val(ui.item.trarazsoc);
			
			}
		});
	$('#txt_ven_cli_nom').keydown(function(e){
		if (e.which == 13){$("#txt_bus_pro_codbar").focus();return false;}
	});

		
			$("#cmb_mone_id").change(function(){
			verTipcambio($('#cmb_mone_id').val());
			$('#txt_mone_id').val($('#cmb_mone_id').val());
			
			if($("#cmb_mone_id").val()==1){				
				$("#txt_bus_pro_nom_dolares").css('display','none');	
				$("#txt_bus_pro_nom_soles").css('display','block');
				$("#txt_bus_pro_nom_soles").focus();
				limpiar_campos();
			}else if($("#cmb_mone_id").val()==2){
				
				$("#txt_bus_pro_nom_dolares").css('display','block');	
				$("#txt_bus_pro_nom_soles").css('display','none');
				$("#txt_bus_pro_nom_dolares").focus();
				limpiar_campos();
			}	
		});
		

		$("#txt_guia_numdoc_doc" ).autocomplete({
			minLength: 1,
			source: "../venta/docnum_complete_nom.php",
			select: function(event, ui){	
			$("#hdd_venta_id").val(ui.item.id);
			$("#txt_guia_mar").focus();
			
			/* if(ui.item.est==0){
				$("#text_not").html('LA VENTA YA ESTA ANULADA.');
					
			}else{
				$("#text_not").html('La Nota de credito se emitira con este docuemnto.*');
				
			} */
		
			}
			});
		
	
	
	
	$("#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_bus_pro_cod').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();
				}	
					
			}
		});
		
	
	

		//FORMAS DE PAGO
		
	
		 
		// $('#chk_venpag_aut').on('ifChecked', function(event){
			// alert(event.type + ' callback');
		// });
		
		
		
 
	$('#for_guia').bootstrapValidator({
	    
	      message: 'Valor no Valido',
	      feedbackIcons: {
	      	valid: 'glyphicon glyphicon-ok',
	      	invalid: 'glyphicon glyphicon-remove',
	      	validating: 'glyphicon glyphicon-refresh'
	      },
	      fields: { 
	      	hdd_guia_numite: {
	      		group: '.col-lg-8',
	      		validators: {
	      			notEmpty: {
	      				message: 'Agregue producto a la guia.'
	      			}
	      		}
	      	},	      	      
	      	txt_guia_fec: {
	      		group: '.col-lg-6',
	      		validators: { 
	      			date: {
	      				format: 'DD-MM-YYYY',
	      				message: 'Fecha no Valida'
	      			}
	      		}
	      	},
			
	      	cmb_doc_id: {
	      		group: '.col-lg-3',
	      		validators: {
	      			notEmpty: {
	      				message: '* Requerido'
	      			}
	      		}
	      	},
			txt_ori_guia: {
	      		group: '.col-lg-6',
	      		validators: {
	      			notEmpty: {
	      				message: '* Requerido'
	      			}
	      		}
	      	},
			txt_des_guia: {
	      		group: '.col-lg-6',
	      		validators: {
	      			notEmpty: {
	      				message: '* Requerido'
	      			}
	      		}
	      	},
	      	
	      	txt_guia_numdoc: {
	      		group: '.col-lg-3',
	      		validators: {
	      			notEmpty: {
	      				message: '* Requerido'
	      			}
	      		}
	      	},
	      	hdd_guia_cli_id: {
	      		group: '.col-lg-4',
	      		validators: {
	      			notEmpty: {
	      				message: 'Seleccione Cliente.'
	      			}
	      		}
	      	},	      	
	      	cmb_guia_mot: {
	      		group: '.col-lg-6',
	      		validators: {
	      			notEmpty: {
	      				message: 'Seleccione Cliente.'
	      			}
	      		}
	      	},
			
		/* 	hdd_venta_id: {
	      		group: '.col-lg-6',
	      		validators: {
	      			identical: {
	      				field: 'txt_guia_numdoc_doc',
	      				message: '*Requerido.'
	      			}
	      		}
	      	}, */
	       	hdd_guia_doc: {
	      		group: '.col-lg-3',
	      		validators: {
	      			notEmpty: {
	      				message: 'Existe registro con mismo N° Documento.'
	      			}
	      		}
	      	},
				
			txt_ate_guia: {
	      		group: '.col-lg-12',
	      		validators: {
	      			notEmpty: {
	      				message: '*Requerido.'
	      			}
	      		}
	      	},
	      }
	  }).on('success.form.bv', function(e) {
	  	
	  	$.ajax({
	  		type: "POST",
	  		url: "../guiaremision/guiaremision_reg.php",
	  		async:false,
	  		dataType: "json",
	  		data: $("#for_guia").serialize(),
	  		beforeSend: function(){
					var div = "<center><div class='overlay col-lg-12 col-sm-12 col-xs-12' style='background-color:#fff;padding:0%;font-size:3rem;' >  <i class='fa fa-refresh fa-spin ' ></i> </div>  </center>";							
					$('.div_venta_form').html(div);
				},
				success: function(data)
				{
					toastr["success"](data.guia_msj);	
					guia_ticket_datos(data.guia_id);
				},
				complete: function(data){
					
					<?php if($_POST['vista']=='guia'){
					?>					
						guiaremision_tabla();
						$('#div_venta_form_dialog').modal('hide');
					<?php
					}else if($_POST['vista']=='venta'){
					?>
					$('#div_guia_form_dialog').modal('hide');
					venta_tabla();
					<?php 
					}
					?>
					
				}
			});	
	  });
	  
	  
	}); 


	$('#txt_guia_cli_nom,#txt_guia_con_nom,#txt_guia_numdoc_doc').keydown(function(ev) {
		if(ev.keyCode==13){return false;}
	});
	
	$('#txt_guia_numdoc_doc').keydown(function(ev) {
		if(ev.keyCode==13){
			$('#txt_guia_mar').focus();
			return false;
			}
	});
	$('#txt_guia_mar').keydown(function(ev) {
		if(ev.keyCode==13){
			$('#txt_guia_pla').focus();
			return false;
			}
	});
	$('#txt_guia_pla').keydown(function(ev) {
		if(ev.keyCode==13){
			$('#txt_ori_guia').focus();
			return false;
			}
	});
	
	$('#txt_ori_guia').keydown(function(ev) {
		if(ev.keyCode==13){
			$('#txt_des_guia').focus();
			return false;
			}
	});
	
	$('#txt_des_guia').keydown(function(ev) {
		if(ev.keyCode==13){
			$('#txt_obs_guia').focus();
			return false;
			}
	});
	  
	
</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>