<?php  
session_start();
require_once ("../../config/Cado.php");
require_once("../empresa/cEmpresa.php");
$oEmpresa = new cEmpresa();
require_once("../proveedor/cProveedor.php");
$oProveedor = new cProveedor();
require_once ("../egreso/cEgreso.php");
$oEgreso = new cEgreso();
require_once ("../compra/cComprapago.php");
$oComprapago = new cComprapago();
require_once ("../canjeproveedor/cCanjeproveedor.php");
$oCanjeproveedor = new cCanjeproveedor();
require_once ("../formatos/formato.php");
	$dts=$oEmpresa->mostrarUno($_POST['empid']);
	$dt = mysqli_fetch_array($dts);
	$rucemp=$dt['tb_empresa_ruc'];
	$nomcom=$dt['tb_empresa_nomcom'];
	$razsoc=$dt['tb_empresa_razsoc'];
	$dir=$dt['tb_empresa_dir'];
	$dir2=$dt['tb_empresa_dir2'];
	$tel=$dt['tb_empresa_tel'];
	$ema=$dt['tb_empresa_ema'];
	$fir=$dt['tb_empresa_fir'];
	$rep=$dt['tb_empresa_rep'];		
	mysqli_free_result($dts);
$fec=date('d-m-Y');
require_once("../almacen/cAlmacen.php");
$oAlmacen = new cAlmacen();
$dts1=$oAlmacen->mostrarAlmCajEmp($_POST['empid']);
			$dt1 = mysqli_fetch_array($dts1);
 			$emp=$dt1['tb_empresa_id'];
 			$punven_id=$dt1['tb_puntoventa_id'];
 			$piso_id=$dt1['tb_piso_id'];
 			$almid = $dt1['tb_almacen_id'];
 			$cajid = $dt1['tb_caja_id'];
			mysqli_free_result($dts1);
	$dts=$oProveedor->mostrarUno($_POST['provid']);
	$dt = mysqli_fetch_array($dts);
	$tip=$dt['tb_proveedor_tip'];
	$nompro=$dt['tb_proveedor_nom'];
	$doc=$dt['tb_proveedor_doc'];
	$dirpro=$dt['tb_proveedor_dir'];
	$con=$dt['tb_proveedor_con'];
	$tel=$dt['tb_proveedor_tel'];
	$ema=$dt['tb_proveedor_ema'];
	mysqli_free_result($dts);
foreach($_SESSION['can_numdoc'] as $numdoc){
	$ref .= $numdoc.'/';	
}
?>
<style type="text/css">
.ui-autocomplete
{
    z-index: 99999; 
}
</style>
<div class="col-lg-12 col-sm-12  col-md-12 col-xs-12" >
<form id="for_canjeletras" >
	
	<div class="modal-header">
        	<h5 class="modal-title"> CANJE DE LETRAS <strong id="lbl_canj_numdoc" style="font-size:2rem;text-align:right;" class=" txt_notalm_numdoc text-orange"> </strong> <span class="float-right"> </span> </h5>
    </div>
<input type="hidden" name="hdd_canje_fec" id="hdd_canje_fec" value="<?php echo $fec; ?>">
<input type="hidden" name="action" id="action" value="insertar">
<input type="hidden" name="hdd_caja_id" id="hdd_caja_id" value="<?php echo $cajid; ?>">
<input type="hidden" name="hdd_emp_id" id="hdd_emp_id" value="<?php echo $_POST['empid']; ?>">
<input type="hidden" name="hdd_prov_id" id="hdd_prov_id" value="<?php echo $_POST['provid']; ?>">
<input type="hidden" name="hdd_mon_id" id="hdd_mon_id" value="<?php echo $_POST['monid']; ?>">
 			<div class="row">
			
 			<div class="col-lg-6 col-sm-12 col-md-6 col-xs-12 no-padding">
				<div class="row">
 				
				<div class="col-lg-12 col-sm-4 col-xs-12"> 
 					<label for="txt_canje_gir">Girador:</label> 
 					<input type="text" name="txt_canje_gir" id="txt_canje_gir" readonly  value="<?php echo $doc.'|'.$nompro; ?>" class="form-control" >
 				</div>
 				<div class="col-lg-6 col-sm-4 col-xs-12"> 
 					<label for="txt_canje_acep">Aceptante:</label> 
 					<input type="text" name="txt_canje_acep" id="txt_canje_acep" readonly value="<?php echo $rucemp.'|'.$nomcom; ?>" class="form-control" >
 				</div>
 				<div class="col-lg-6 col-sm-4 col-xs-12"> 
 					<label for="txt_canje_fec">Fecha de Giro:</label> 
 					<input type="text" name="txt_canje_fec" id="txt_canje_fec" class="form-control" value="<?php echo $fec;?>" >
 				</div>
				<div class="col-lg-6 col-sm-4 col-xs-12"> 
					<label for="txt_canje_aval">Aval:</label> 
					<input type="text" name="txt_canje_aval" id="txt_canje_aval" value="<?php echo $rep; ?>" class="form-control" >
				</div>
				
				<div class="col-lg-6 col-sm-4 col-xs-6">
					<label for="cmb_canje_mon_id">Moneda:</label>
					<select name="cmb_canje_mon_id" id="cmb_canje_mon_id" class="form-control"></select>    
				</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: 5px;"></div>
				<div id="div_numdoc" class="col-lg-12 col-sm-12 col-xs-12" >
					<label for="txt_doc_numdoc">Buscar por Numero de documento:</label> 
					<input type="text" name="txt_doc_numdoc" id="txt_doc_numdoc" class="form-control" >
				</div>
				<div class="col-lg-12 col-sm-12 col-xs-12" style="height: 5px;"></div>
 					
				<div id="div_canje_doc" class="col-lg-12 col-sm-12 col-xs-12" >	</div>
			</div>
			
			</div>
			<div class="col-lg-6 col-sm-12 col-md-6 col-xs-12 no-padding">
 				
 				<div  class="row"> 
				
 				<div  class="col-lg-4 col-sm-4 col-xs-12"> 
 					<label for="txt_canje_numlet">Nro Letra:</label> 
 					<input type="text" name="txt_canje_numlet" id="txt_canje_numlet" class="form-control" >
 				</div>
 				<div class="col-lg-4 col-sm-4 col-xs-12"> 
 					<label for="txt_canje_letuni">Nro Unico:</label> 
 					<input type="text" name="txt_canje_letuni" id="txt_canje_letuni" class="form-control" >
 				</div>
 				<div class="col-lg-4 col-sm-4 col-xs-12"> 
 					<label for="txt_canje_ref">Referencia:</label>
 					<input type="text" name="txt_canje_ref" id="txt_canje_ref"  value="<?php echo $ref ?>" class="form-control" >
 				</div>
 				
 				<div class="col-lg-4 col-sm-4 col-xs-12"> 
 					<label for="txt_canje_lug">Lugar de Giro:</label> 
 					<input type="text" name="txt_canje_lug" id="txt_canje_lug" class="form-control"  value="<?php echo $dirpro; ?>" >
 				</div>
 				<div class="col-lg-4 col-sm-4 col-xs-12"> 
 						<label for="txt_letra_imp">Importe:</label> <span class="lbl_simb" >  </span>
 						<input type="text" name="txt_letra_imp" id="txt_letra_imp" class="form-control" value="<?php echo $_POST['totmon']?>" >
 						<input type="hidden" id="hdd_can_tot" name="hdd_can_tot" value="<?php echo $_POST['totmon']?>">
 				</div>
 				<!--
 				<div class="col-lg-4 col-sm-4 col-xs-12"> 
 					<label for="txt_canje_numdia">N° Días:</label>
 					<input name="txt_canje_numdia" id="txt_canje_numdia" type="text" value="0" class=" form-control dias"  maxlength="3" >
 				</div>
 			-->
 				<div class="col-lg-4 col-sm-4 col-xs-12"> 
 					<label for="txt_canje_fecven">F. Venc.:</label>
 					<input type="text" name="txt_canje_fecven" id="txt_canje_fecven" value="<?php echo $fec; ?>" class="form-control" >
 				</div>
 				<div class="col-lg-4 col-sm-4 col-xs-12"> 
 					<br>
 					<span class="btn btn-primary" style="cursor:pointer" onclick="canje_pago_agregar('agregar')">Agregar</span>
 				</div>
 				<!--
 				<div class="col-lg-3 col-sm-4 col-xs-12"> 
 					<label for="txt_mon_nom">Moneda:</label> 
 					<input type="text" name="txt_mon_nom" id="txt_mon_nom" class="form-control" value="<?php echo $lblmon; ?>" readonly >
 				</div>
 				-->
 				<div class="col-lg-12 col-sm-12 col-xs-12" style="height: 10px;"></div>
 					
 				<div id="div_canje_car_tabla" class="col-lg-12 col-sm-12 col-xs-12"></div>
 					
		
 			</div>
			
 			</div>
 			
			</div>							
</form>
 	<div class="box-footer float-right ">
		<button title="CERRAR VENTANA" type="button" data-dismiss="modal" class="btn btn-danger "><i class="fas fa-times"></i></button>
		<button class="btn btn-primary" style="display: none" id="save_form" >Guardar</button>			
	</div>
</div>
<script type="text/javascript">
	
		
function txt_canje_numdoc()
 			{
 				$.ajax({
 					type: "POST",
 					url: "../venta/venta_txt_numdoc.php",
 					async:false,
 					dataType: "json",                      
 					data: ({
 						doc_id: 28
 					}),
 					beforeSend: function() {
 						$('#lbl_canj_numdoc').text('Cargando...');
 					},
 					success: function(data){			
 						$('#lbl_canj_numdoc').text(data.numero);
 						if(data.msj!="")
 						{
							toastr["success"](data.msj);
 						}
 						else
 						{
 						}
 					},
 					complete: function(){
							//verificar_numdoc();							
 						}
 					});
 			}
	function cmb_canje_mon_id(moneid)
	{	
		$.ajax({
			type: "POST",
			url: "../moneda/cmb_mon_id.php",
			async:true,
			dataType: "html",                      
			data: ({
				mon_id: moneid
			}),
			beforeSend: function(){
				$('#cmb_canje_mon_id').html('<option value="">Cargando...</option>');
			},
			success: function(html){
				$('#cmb_canje_mon_id').html(html);
			}
		});
	}
	
$('#txt_canje_fec,#txt_canje_fecven').datepicker({
 
format: 'dd-mm-yyyy',
autoclose:true			
});	
//
		$('#txt_doc_numdoc').autocomplete({
            source: function(request, response) {
            $.ajax({
            url: "../canjeproveedor/buscar_numdoc_canje.php",
            dataType: "json",
            data: {
              term : request.term,
              emp : $("#hdd_emp_id").val(),
              prov : $("#hdd_prov_id").val()
             },
            success: function(rpt) {
			response(rpt);
 		//
               }
                  });
                  },
                  select: function( event, ui )
			{
					agregar_documento('agregarnumdoc',ui.item.value,ui.item.id);
			},
                  minLength: 2
             });
		$('#txt_canje_numdia').change(function()
		{
			if($('#txt_canje_numdia').val()!="")
			{
				txt_canje_fecven();
			}
			else
			{
				$('#txt_canje_fecven').val('');
			}
		});
		$("#txt_canje_numdia").keydown(function(e){
			if (e.which == 13){
				txt_canje_fecven();
				$("#btn_compag_agregar").focus();
				return false;
			}
		});
function agregar_documento(act,numdoc,compag_id){
	if(act=="restablecer"){
		
		$('#hdd_can_tot').val(0.00);
		$("#txt_canje_ref").val('');
	}
	//alert(comid);
	
	$.ajax({
		type: "POST",
		url: "../canjeproveedor/canjeproveedor_reg.php",
		async:false,
		dataType: "html",                  
		data: ({
			action:	 act,
			can_numdoc: numdoc,
			compag_id:compag_id //$('#txt_doc_numdoc').val()
		}),
		beforeSend: function() {
			
		},
		success: function(html){
			
			document.getElementById("txt_doc_numdoc").value = "";
		
		},
		complete: function(){			
			canje_documentos_tabla('',$('#hdd_prov_id').val(),$('#hdd_emp_id').val(),'',$("#cmb_canje_mon_id").val());
			canje_pago_tabla();
			
			
		}
	});
}
function txt_canje_fecven(){	
	$.ajax({
		type: "POST",
		url: "../compra/ver_fecven.php",
		async:false,
		dataType: "json",                      
		data: ({
			com_fec: $('#txt_canje_fec').val(),
			compag_numdia: $('#txt_canje_numdia').val()
		}),
		beforeSend: function() {
			//$('#txt_ven_numdoc').val('Cargando...');			
		},
		success: function(data){
			$('#txt_canje_fecven').val(data.fecha);
		}
	});
}
function guardar_letras(){	
     $('#div_mensaje_dialog').modal();
	$.ajax({
		type: "POST",
		url: "../canjeproveedor/canjeproveedor_reg.php",
		async:true,
		dataType: "json",                      
		data: $("#for_canjeletras").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);
		},
		success: function(data){
			
			$('.modal').modal('hide');
				$('.in').removeClass('modal-backdrop');	
			
			toastr["info"](data.msj);
		}
	});
}
txt_canje_numdoc();
cmb_canje_mon_id();
agregar_documento('restablecer');
//canje_pago_agregar('restablecer');
$("#cmb_canje_mon_id").change(function(){
	agregar_documento('restablecer');
	if($("#cmb_canje_mon_id").val()!=""){
		
		if($(this).val()==1){
			$(".lbl_simb").text('S/');
			$("#hdd_mon_id").val('1');
		}else if($(this).val()==2){
			$(".lbl_simb").text('US$');
			$("#hdd_mon_id").val('2');
		}
		
		
		$("#div_numdoc").show(100);
		$("#div_canje_doc").show(100);
		canje_documentos_tabla('','<?php echo $_POST['provid']?>','<?php echo $_POST['empid']?>','',$("#cmb_canje_mon_id").val());
	}else{
		$("#div_numdoc").hide(100);
		$("#div_canje_doc").hide(100);
		toastr["error"]('Debe seleccionar una moneda para canjear documentos.');
	}	
});
	if($("#cmb_canje_mon_id").val()!=""){
		canje_documentos_tabla('','<?php echo $_POST['provid']?>','<?php echo $_POST['empid']?>','',$("#cmb_canje_mon_id").val());
		$("#div_numdoc").show(100);
		$("#div_canje_doc").show(100);
	}else{
		$("#div_canje_doc").hide(100);
		$("#div_numdoc").hide(100);
		toastr["error"]('Debe seleccionar una moneda para canjear documentos.');
	}
//canje_pago_tabla();
</script>