<?php
date_default_timezone_set('America/Lima');
session_start();
if($_SESSION["sistema"]!= "1"){ header("location: ../login/index.php"); exit();}
require_once ("../../config/Cado.php");
require_once ("../cotizacion/cCotizacion.php");
$oCotizacion = new cCotizacion();
	$dts = $oCotizacion->mostrarUno($_POST['cotid']);
	$dt = mysqli_fetch_array($dts);
	$fec = $dt['tb_cotizacion_fec'];
	$dia = $dt['tb_cotizacion_dia'];
	$lle = $dt['tb_cliente_dir'];
	$cliid = $dt['tb_cliente_id'];
	$clidoc = $dt['tb_cliente_doc'];
	$punvenid = $dt['punvenid'];
	mysqli_free_result($dts);
	
	$can = strlen($clidoc);
	
	if($dia>0){
		$forpag_id = '2';
	}else if($dia==0){
		$forpag_id = '1';
	}
?>
<script type="text/javascript">
<?php if($dia>0){
?>	
ver_fecven();
<?php 	
}?>
 function ver_fecven(){	
	$.ajax({
		type: "POST",
		url: "../cotizacion/ver_fecven.php",
		async:false,
		dataType: "json",                      
		data: ({
			ven_fec: 		$('#txt_cot_fec').val(),
			venpag_numdia: 	$('#txt_venpag_numdia').val()
		}),
		beforeSend: function() {
					
		},
		success: function(data){
			$('#txt_venpag_fecven').val(data.fecha);
		}
	});
}
	function cmb_docgen_id(docid,can)
	{	
		$.ajax({
			type: "POST",
			url: "../documento/cmb_doc_id.php",
			async:true,
			dataType: "html",                      
			data: ({
				mod_id: 3,
				doc_id: docid,
				doccan : can ,<?php //echo $can?>
				vista:'',
				def:1
				
			}),
			beforeSend: function() {
				$('#cmb_doc_cot_id').html('<option value="">Cargando...</option>');
			},
			success: function(html){			
				$('#cmb_doc_cot_id').html(html);
			},
			complete: function(){
					
		}
	});
	}
	
	function forpag_id()
{
	var tipo = $("#cmb_forpag_id").val();
	$('#cmb_modpag_id_cot').val('');
	$('#cmb_tar_id_cot').val('');
	$('#cmb_cuedep_id_cot').val('');
	$('#txt_venpag_numope_cot').val('');
	$('#txt_venpag_numdia_cot').val('');
	$('#txt_venpag_fecven_cot').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);
	}
	//credito
	if(tipo == 2){			
		$("#div_dia").show(100);
		$("#div_fecven").show(100);	
	}	
} 
function cliente_cargar_datos(idf){	
	$.ajax({
		type: "POST",
		url: "../cliente/cliente_reg.php",
		async:true,
		dataType: "json",                      
		data: ({
			action: "obtener_datos",
			cli_id:	idf
		}),
		beforeSend: function() {
		
		},
		success: function(data){
			$('#hdd_ven_cli_id1').val(idf);	
			$('#txt_ven_cli_nom1').val(data.nombre);	
			$('#txt_ven_cli_doc1').val(data.documento);						
			$('#txt_ven_cli_dir1').val(data.direccion);
			$("#hdd_ven_cli_tip1").val(data.tipo);
			cargar_direcciones(idf);
			
		can = 	$('#txt_ven_cli_doc1').val().length;
		
		if(can == '8'){
				cmb_docgen_id('4',can);
		}else{
				cmb_docgen_id('5',can);
		}		
			//cargar_vehiculos(idf);
			
		}
	});		
}
	
	
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 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_dir1').html('<option value="">Cargando...</option>');
 					},
 					success: function(html){			
 						$('#cmb_ven_cli_dir1').html(html);
 					},
 					complete: function(){
 						
 						}
 					});
}
$(function() {
	
	
	
cliente_cargar_datos(<?php echo $cliid?>);
$( "#txt_ven_cli_doc1" ).autocomplete({
		minLength: 1,
		source: "../cliente/cliente_complete_doc.php",
		select: function(event, ui){			
			$("#hdd_ven_cli_id1").val(ui.item.id);
			$("#txt_ven_cli_nom1").val(ui.item.nombre);
			$("#txt_ven_cli_dir1").val(ui.item.direccion);
			$("#hdd_ven_cli_tip1").val(ui.item.tipo);
				$("#txt_bus_pro_codbar").focus();
				cargar_direcciones(ui.item.id);
				
		can = $('#txt_ven_cli_doc1').val().length;
		
		if(can == '8'){
				cmb_docgen_id('4',can);
		}else{
				cmb_docgen_id('5',can);
		}	
				
			}
		});
	$( "#txt_ven_cli_nom1" ).autocomplete({
		minLength: 1,
		source: "../cliente/cliente_complete_nom.php",
		select: function(event, ui){			
			$("#hdd_ven_cli_id1").val(ui.item.id);							
			$("#txt_ven_cli_doc1").val(ui.item.documento);
			$("#txt_ven_cli_dir1").val(ui.item.direccion);
			$("#hdd_ven_cli_tip1").val(ui.item.tipo);
				$("#txt_bus_pro_codbar").focus();
				cargar_direcciones(ui.item.id);
				
		can = $('#txt_ven_cli_doc1').val().length;
		
		if(can == '8'){
				cmb_docgen_id('4',can);
		}else{
				cmb_docgen_id('5',can);
		}	
		
			}
		});
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
		$("#txt_venpag_mon").attr('readonly',true);
	 
		 
		 $("#chk_venpag_aut").change(function(){
			venpag_aut();
		}); 
		
		 $("#cmb_forpag_id").change(function(){		
			forpag_id();
		});
		$("#txt_venpag_mon").keydown(function(e){
			if (e.which == 13){
				if($("#txt_venpag_mon").val()>0)
				{
					$("#cmb_forpag_id").focus();
				}
				else
				{
					
				}				
				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;
			}
		});
		$("#cmb_tar_id, #cmb_cuedep_id").keydown(function(e){
			if (e.which == 13){
				$("#txt_venpag_numope").focus();
			}
		});
		$("#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;}
		});
 $('#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_mone_id").change(function(){
			
			$('#txt_mone_id').val($('#cmb_mone_id').val());
			
		}); 
		
$('#cmb_doc_cot_id').change(function(){
	
		if($('#cmb_doc_cot_id').val()==4){
					$("#div_ate").hide(100);
					$("#div_lle").hide(100);
					
		}else if($('#cmb_doc_cot_id').val()==5){
					$("#div_ate").show(100);
					$("#div_lle").show(100);
		}else {
					$("#div_ate").show(100);
					$("#div_lle").show(100);
		}
	
});
$('#txt_atenc_guia,#txt_lle_guia').change(function(){
		$(this).val($(this).val().toUpperCase());
	});
$("#btn_ven_guardar").click(function() {
	$("#for_ven").submit();
});
	
$('#for_ven').bootstrapValidator({ 
	      message: 'Valor no valido',
	      feedbackIcons: {
	      	valid: 'glyphicon glyphicon-ok',
	      	invalid: 'glyphicon glyphicon-remove',
	      	validating: 'glyphicon glyphicon-refresh'
	      },
	      fields: {	      	
	      	
	      	cmb_doc_cot_id: {
	      		group: '.col-lg-12',
	      		validators: {
	      			notEmpty: {
	      				message: 'Requerido *'
	      			}
	      		}
	      	},
			txt_lle_guia: {
	      		group: '.col-lg-12',
	      		validators: {
	      			notEmpty: {
	      				message: 'Requerido *'
	      			}
	      		}
	      	}
	      }
	  }).on('success.form.bv', function(e) { 
				$('#div_mensaje_dialog').modal();
	  	$.ajax({
	  		type: "POST",
	  		url: "../cotizacion/cotizacion_venta.php",
	  		async:true,
	  		dataType: "json",
	  		data: $("#for_ven").serialize(),
	  		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);
	  			toastr["info"]('Validando stock de los productos');  
	  		},
	  		success: function(data){
	  			
			
			if(data.est==true){
				$('#div_venta_form_dialog').modal('hide');
				toastr["info"](data.msj);
				generar_documento(<?php echo $_POST['cotid']?>,<?php echo $punvenid?>);
				
			}else if(data.est==false){
				
				if(data.dat.tipo=='genven'){
					toastr["error"](data.msj);
				}else if(data.dat.tipo=='stock'){
					toastr["warning"](data.msj);
				}
				$('#div_venta_form_dialog').modal('hide');
				$('#div_mensaje_dialog').modal('hide');				
			}	
				
				
				
			/* 	$('.modal').modal('hide');
				$('.in').removeClass('modal-backdrop');		
				toastr["success"](data.msj);  			
				 */
	  		/* egreso_impresion(data.egr_id); */
	  			//cotizacion_tabla();
				
				
	  		},
	  		complete: function(){
	  			
	  		}
	  	});	
		
	  });
	 
	});
	
	
		function generar_documento(cotid,punvenid){
		
			 $.ajax({
	  		type: "POST",
	  		url: "../cotizacion/cotizacion_venta.php",
	  		async:true,
	  		dataType: "json",
	  		data: ({
				action : 'generar_venta',
				punvenid:punvenid,
				cotid:cotid,
				docid:$("#cmb_doc_cot_id").val(),
				forpagid:$("#cmb_forpag_id").val(),
				modpagid:$("#cmb_modpag_id").val(),
				numdia:$("#txt_venpag_numdia").val(),
				fecven:$("#txt_venpag_fecven").val(),
				aten:$("#txt_atenc_guia").val(),
				lleg:$("#txt_lle_guia").val(),
				cli_id:$("#hdd_ven_cli_id1").val(),
				cli_nom:$("#txt_ven_cli_nom1").val(),
				cli_doc:$("#txt_ven_cli_doc1").val(),
				cli_dir:$("#cmb_ven_cli_dir1").val()
			}),
	  		beforeSend: function(){
	  			toastr["info"]('Generando documentos.');			
	  		},
	  		success: function(data){
	  	
		
				toastr["success"](data.msj);  			
				
				if(data.estpdfdoc1>0){
					abrirEnPestana('="../../../../files/venta/venta_impcpe_doc.php?venid='+data.estpdfdoc1);
					venta_ticket_datos(data.estpdfdoc1);
				}
				
				if(data.estpdfguia1>0){
					abrirEnPestana('="../../../../files/guiaremision/guiaremision_impcpe_doc.php?guiaid='+data.estpdfguia1);
					guia_ticket_datos(data.estpdfguia1);
				}
								
			$('#div_venta_form_dialog').modal('hide');
			
			cotizacion_tabla();
					
	  		},
	  		complete: function(){
	  			
	  		}
	  	});	 	
	}
	
	forpag_id();
	
	function abrirEnPestana(url) {
		var a = document.createElement("a");
		a.target = "_blank";
		a.href = url;
		a.click();
	}
	
	
	
</script>
<style type="text/css">
	.ui-autocomplete
{
    z-index: 99999; 
}
	</style>
<div class="col-xs-12 col-lg-12 col-md-12 col-sm-12" >
			<form  role="form" id="for_ven" method="POST">
				<?php  //echo $can;?>
				<input type="hidden" id="hdd_cot_id" name="hdd_cot_id" value="<?php echo $_POST['cotid']?>">
				<input type="hidden" id="hdd_punven_id" name="hdd_punven_id" value="<?php echo $punvenid?>">
				<input type="hidden" id="txt_cot_fec" name="txt_cot_fec" value="<?php echo $fec;?>">
				<input type="hidden" id="action" name="action" value="<?php echo 'validar_stock'; ?>">
			
			
					
					<div class="col-lg-12 col-md-12 col-xs-12 " style="padding:5px;">
					<div class="row">
					
					<div class="box-header">
						<h3 class="box-title">Elija el documento a emitir</h3>
					</div>
	
					
 											<input type="hidden" id="hdd_ven_cli_id1" name="hdd_ven_cli_id1" value="<?php echo $cli_id ?>" />
 											
											<div class="col-lg-5 col-sm-4 col-xs-12">															
												<label for="txt_com_pro_doc">RUC/DNI:</label>
													<div class="input-group">
														<span id="" style=""  class="input-group-btn">
																<span id="" style="cursor:pointer" class="btn btn-info btn-flat" onClick="cliente_form('insertar')"><i class="fa fa-cog"></i></span> 
														</span>	
 														
 														<input name="txt_ven_cli_doc1" type="text" id="txt_ven_cli_doc1" value="<?php echo $cli_doc?>"   maxlength="11" class="form-control input-sm"> 
 													</div>
 												</div>
 												<div class="col-lg-7 col-sm-4 col-xs-12">
 													<div id="div_clientecuenta_detalle"></div>
 													<label for="txt_ven_cli_nom1">Cliente:</label>
 													<input type="text" id="txt_ven_cli_nom1" name="txt_ven_cli_nom1"  value="<?php echo $cli_nom?>" class="form-control input-sm" />
 												</div> 
 											
 											
 												<div class="col-lg-12 col-sm-12 col-xs-12">
 													<div id="div_clientecuenta_detalle"></div>
 													<label for="cmb_ven_cli_dir1">Dirección:</label>
 													
													<select name="cmb_ven_cli_dir1" id="cmb_ven_cli_dir1" class="form-control input-sm" >
		<option value="">-</option>
		</select>										   
 		</div>
											
		
		
	
					<div class="col-lg-6 col-sm-6 col-xs-6">
						<label for="cmb_doc_cot_id">Documento:</label>
						<select name="cmb_doc_cot_id" id="cmb_doc_cot_id" class="form-control"> </select>
					</div>
				
					<div class="col-xs-12 col-lg-12 col-md-12 col-sm-12" style="height:10px;"></div>
 					<div class="col-lg-4 col-sm-12 col-xs-12">
 							<label for="cmb_forpag_id">Forma:</label>													
							<select name="cmb_forpag_id" id="cmb_forpag_id"  class="form-control">
 							<option value="1" <?php if($forpag_id==1) echo 'selected'; ?> >CONTADO</option>
 							<option value="2" <?php if($forpag_id==2) echo 'selected'; ?> >CREDITO</option>
 							</select> 														
 					</div>
 					<div id="div_modo" class="col-lg-4 col-sm-12 col-xs-12">
 							<label for="cmb_modpag_id">Modo:</label> 
 							<select name="cmb_modpag_id" id="cmb_modpag_id" class="form-control">
 							<option value="">-- Seleccione --</option>
 							<option value="1" selected="selected">EFECTIVO</option>
							</select> 
					</div>								
					<div id="div_dia" style="display:none" class="col-lg-2 col-sm-6 col-xs-12"> 
 							<label for="txt_venpag_numdia">N° Días:</label></br>
 							<input name="txt_venpag_numdia" id="txt_venpag_numdia" type="text" value="<?php echo $dia; ?>" class=" form-control dias"  maxlength="3" >
 					</div>
 					<div id="div_fecven" style="display:none" class="col-lg-3 col-sm-6 col-xs-12"> 
 							<label for="txt_venpag_fecven">F. Venc:</label></br>
 							<input type="text" name="txt_venpag_fecven" id="txt_venpag_fecven"  maxlength="10" class=" form-control" readonly>
 					</div>
					
					<div class="col-xs-12 col-lg-12 col-md-12 col-sm-12" style="height:10px;"></div>
					
					<div  class="col-lg-12 col-sm-6 col-xs-12" id="div_ate_" style="display:none"  > 
 							<label for="txt_atenc_guia">ATENCION:</label></br>
 							<input type="text" name="txt_atenc_guia" id="txt_atenc_guia"  class=" form-control">
 					</div>
					
					<div  class="col-lg-12 col-sm-6 col-xs-12" id="div_lle_" style="display:none"  > 
 							<label for="txt_lle_guia">DIRECCION LLEGADA:</label></br>
 							<input type="text" name="txt_lle_guia" id="txt_lle_guia" value="<?php echo $lle?>" class=" form-control">
 					</div>
					
					
					
					<div class="col-xs-12 col-lg-12 col-md-12 col-sm-12" style="height:10px;"></div>
				
			</form>
		
		
		</div> 
		</div> 
		
		<div class="modal-footer float-right">	 
			<button type="button" class="btn btn-default " id="" data-dismiss="modal">Cancelar</button>
			<button type="button" class="btn btn-info " id="btn_ven_guardar">Guardar</button>
		</div> 
		
		
		
		</div> 