<?php
session_start();
date_default_timezone_set('America/Lima');
require_once ("../../config/Cado.php");
require_once ("../egreso/cEgreso.php");
$oEgreso = new cEgreso();
require_once("../formatos/formato.php");
if($_POST['action']=="insertar"){
	$fec=date('d-m-Y');
	$est='1';
	$caj_id=$_SESSION['caja_id'];
	$doc_id = '';
	
	
	if ($_SESSION['usuariogrupo_id']=='1' || $_SESSION['usuariogrupo_id']=='2' ){
			//	$usu_id = $_SESSION['usuario_id'];
				$punven_id = '';
			}else{
			//	$usu_id = $_SESSION['usuario_id'];
				$punven_id =  $_SESSION['puntoventa_id']; 
			}
			
			
			
}
if($_POST['action']=="editar"){
	$dts= $oEgreso->mostrarUno($_POST['egr_id']);
	$dt = mysqli_fetch_array($dts);
	$fecreg		=mostrarFechaHora($dt['tb_egreso_fecreg']);
	$fecmod		=mostrarFechaHora($dt['tb_egreso_fecmod']);
	$usureg		=$dt['tb_egreso_usureg'];
	$usumod		=$dt['tb_egreso_usumod'];
	$fec		=mostrarFecha($dt['tb_egreso_fec']);
	$doc_id 	=$dt['tb_documento_id'];
	$numdoc		=$dt['tb_egreso_numdoc'];
	$det		=$dt['tb_egreso_det'];
	$cue_id		=$dt['tb_cuenta_id'];
	$subcue_id	=$dt['tb_subcuenta_id'];
	$pro_id		=$dt['tb_proveedor_id'];
	$pro_nom 	=$dt['tb_proveedor_nom'];
	$pro_doc 	=$dt['tb_proveedor_doc'];
	$pro_dir 	=$dt['tb_proveedor_dir'];
	$pro_tip 	=$dt['tb_proveedor_tip'];
	$imp		=$dt['tb_egreso_imp'];
	$caj_id		=$dt['tb_caja_id'];
	$gas_id		=$dt['tb_gasto_id'];
	$est		=$dt['tb_egreso_est'];
	mysqli_free_result($dts);
	//echo '--'.$doc_id;
	if($_POST['vista']=='gasto_egreso_tabla'){
		$gas_imp	=moneda_mysql($_POST['imp']);
		$egr_tot 	=moneda_mysql($_POST['egr_tot']);
		$imp_max=$gas_imp-$egr_tot+$imp;
	}
}
?>
<script type="text/javascript">
	$('#txt_egr_fec').datepicker({
		format: 'dd-mm-yyyy',
		autoclose:true,
		endDate: '+0d'
	});
	function cmb_caj_id(ids,punven_id)
	{	
		$.ajax({
			type: "POST",
			url: "../caja/cmb_caj_id.php",
			async:true,
			dataType: "html",                      
			data: ({
				caj_id: ids,
				punvenid :punven_id
			}),
			beforeSend: function() {
				$('#cmb_caj_id').html('<option value="">Cargando...</option>');
			},
			success: function(html){
				$('#cmb_caj_id').html(html);
			}
		});
	}
	function cmb_doc_id(tipo,docid,vis)
	{	
		$.ajax({
			type: "POST",
			url: "../documento/cmb_doc_id.php",
			async:true,
			dataType: "html",                      
			data: ({
				mod_id:	tipo,
				mod_id: 4,
				doc_id: docid,
				vista:'egreso',
				def:1
				
			}),
			beforeSend: function() {
				$('#cmb_doc_id').html('<option value="">Cargando...</option>');
			},
			success: function(html){			
				$('#cmb_doc_id').html(html);
			},
			complete: function(){
				
		}
	});
	}
	function egreso_txt_numdoc(){	
		$.ajax({
			type: "POST",
			url: "../egreso/egreso_txt_numdoc.php",
			async:false,
			dataType: "json",                      
			data: ({
				doc_id: $('#cmb_doc_id').val()
			}),
			beforeSend: function() {
				$('#txt_egr_numdoc').val('...');
			},
			success: function(data){		
				$('#txt_egr_numdoc').val(data.correlativo);
		
		},
		complete: function(){
			
		}
	});
	}
	
	
	function proveedor_cargar_datos(idf){	
		$.ajax({
			type: "POST",
			url: "../proveedor/proveedor_reg.php",
			async:true,
			dataType: "json",                      
			data: ({
				action: "obtener_datos",
				pro_id:	idf
			}),
			beforeSend: function() {
			
		},
		success: function(data){
			$('#hdd_pro_id').val(idf);	
			$('#txt_pro_nom').val(data.nombre);	
			$('#txt_pro_doc').val(data.documento);						
			$('#txt_pro_dir').val(data.direccion);
			$("#hdd_pro_tip").val(data.tipo);
		}
	});		
	}
	function cmb_cue_id()
	{	
		$.ajax({
			type: "POST",
			url: "../cuentas/cmb_cue_id.php",
			async:true,
			dataType: "html",                      
			data: ({
				cuetip_id:"2",
				orden: "ord",
				cue_id: "<?php echo $cue_id?>"
			}),
			beforeSend: function() {
				$('#cmb_cue_id').html('<option value="">Cargando...</option>');
			},
			success: function(html){
				$('#cmb_cue_id').html(html);
			}
		});
	}
	function cmb_subcue_id(cuenta_id)
	{	
		$.ajax({
			type: "POST",
			url: "../cuentas/cmb_subcue_id.php",
			async:true,
			dataType: "html",                      
			data: ({
				cue_id: cuenta_id,
				subcue_id: "<?php echo $subcue_id?>"
			}),
			beforeSend: function() {
				$('#cmb_subcue_id').html('<option value="">Cargando...</option>');
			},
			success: function(html){
				$('#cmb_subcue_id').html(html);
			}
		});
	}
$(function() {
	<?php if($_POST['action']=="insertar"){ ?>
		$('#txt_egr_det').focus();
		proveedor_cargar_datos('<?php echo $pro_id?>');
		<?php }?>
		cmb_caj_id('<?php echo $caj_id?>','<?php echo $punven_id?>');
		cmb_cue_id();
		cmb_subcue_id(<?php echo $cue_id?>);
		cmb_doc_id('4','<?php echo $doc_id?>','<?php echo $_POST['action']?>');
		$('#cmb_doc_id').change(function(event) {
		
	});
	$( "#txt_pro_doc" ).autocomplete({
		minLength: 1,
		source: "../proveedor/proveedor_complete_doc.php",
		select: function(event, ui){			
			$("#hdd_pro_id").val(ui.item.id);
			$("#txt_pro_nom").val(ui.item.nombre);
			$("#txt_pro_dir").val(ui.item.direccion);
			$("#hdd_pro_tip").val(ui.item.tipo);
		}
	});
	$( "#txt_pro_nom" ).autocomplete({
		minLength: 1,
		source: "../proveedor/proveedor_complete_nom.php",
		select: function(event, ui){			
			$("#hdd_pro_id").val(ui.item.id);							
			$("#txt_pro_doc").val(ui.item.documento);
			$("#txt_pro_dir").val(ui.item.direccion);
			$("#hdd_pro_tip").val(ui.item.tipo);	
		}
	});
	
	$('#cmb_cue_id').change(function(){
		cmb_subcue_id($('#cmb_cue_id').val());
	});
	
	$('#txt_egr_det').autocomplete({
		minLength: 1,
		source: "../egreso/complete_egreso_det.php"
	});
	$('#txt_egr_det').change(function(){
		$(this).val($(this).val().toUpperCase());
	});
	<?php if($_POST['vista']=='gasto_egreso_tabla'){ ?>
		$('#txt_egr_imp').change(function(){
			
			if($('#txt_egr_imp').autoNumericGet()><?php echo $imp_max?>)
			{
				toastr["info"](<?php echo "Importe debe ser menor o igual a : ".formato_money($imp_max);?>);			
				$('#txt_egr_imp').val('<?php echo formato_money($imp_max);?>');
			}
		});
		<?php }?>
		<?php if($_POST['action']=="editar"){ ?>
	$('#txt_pro_doc,#txt_pro_nom').attr('disabled','disabled');
	<?php }?>
	
$("#btn_egre_guardar").click(function() {
	$("#for_egr").submit();
});
	
$('#for_egr').bootstrapValidator({ 
	      message: 'Valor no valido',
	      feedbackIcons: {
	      	valid: 'glyphicon glyphicon-ok',
	      	invalid: 'glyphicon glyphicon-remove',
	      	validating: 'glyphicon glyphicon-refresh'
	      },
	      fields: {	      	
	      	txt_egr_fec: {
	      		group: '.col-lg-3',
	      		validators: {	      			 
	      			date: {
	      				format: 'DD-MM-YYYY',
	      				message: 'Fecha no Valida'
	      			}
	      		}
	      	},
	      	
	      	cmb_caj_id: {
	      		group: '.col-lg-3',
	      		validators: {
	      			notEmpty: {
	      				message: 'Requerido *'
	      			}
	      		}
	      	},
	      	txt_egr_imp: {
	      		group: '.col-lg-4',
	      		validators: {
	      			notEmpty: {
	      				message: 'Requerido *'
	      			}
	      		}
	      	} 
	      }
	  }).on('success.form.bv', function(e) { 
	  
	  	$.ajax({
	  		type: "POST",
	  		url: "../egreso/egreso_reg.php",
	  		async:true,
	  		dataType: "json",
	  		data: $("#for_egr").serialize(),
	  		beforeSend: function(){
	  			$('#div_venta_ingreso_form').modal('hide'); 
	  		
	  		},
	  		success: function(data){
	  			$('.modal').modal('hide');
				$('.in').removeClass('modal-backdrop');		
				toastr["success"](data.egr_msj);  			
				
	  		/* egreso_impresion(data.egr_id); */
	  			
	  		},
	  		complete: function(){
	  			<?php
	  			if($_POST['vista']=="egreso_tabla")
	  			{
	  				echo $_POST['vista'].'();';
	  			}
	  			if($_POST['vista']=="gasto_egreso_tabla")
	  			{
	  				echo $_POST['vista'].'();';
	  			}
	  			if($_POST['vista']=="compra_egreso_tabla")
	  			{
	  				echo $_POST['vista'].'();';
	  			}
	  			?>
	  		}
	  	});	
	  });
	  $("#cmb_caj_id").focus();
	  $("#txt_egr_fec").keydown(function(e){
	  	if (e.which == 13){$("#cmb_caj_id").focus();return false; } });
	  $("#cmb_caj_id").keydown(function(e){
	  	if (e.which == 13){$("#cmb_cue_id").focus();return false; } });
	  $("#cmb_cue_id").keydown(function(e){
	  	if (e.which == 13){$("#cmb_subcue_id").focus();return false; } });
	  $("#cmb_subcue_id").keydown(function(e){
	  	if (e.which == 13){$("#txt_pro_doc").focus();return false; } });
	  $("#txt_pro_doc").keydown(function(e){
	  	if (e.which == 13){$("#txt_pro_nom").focus();return false; } });
	  $("#txt_pro_nom").keydown(function(e){
	  	if (e.which == 13){$("#cmb_doc_id").focus();return false; } });
	  $("#cmb_doc_id").keydown(function(e){
	  	if (e.which == 13){$("#txt_egr_numdoc").focus();return false; } });
	  $("#txt_egr_numdoc").keydown(function(e){
	  	if (e.which == 13){$("#txt_egr_det").focus();return false; } });
	  $("#txt_egr_det").keydown(function(e){
	  	if (e.which == 13){$("#txt_egr_imp").focus();return false; } });
	  $("#txt_egr_imp").keydown(function(e){
	  	if (e.which == 13){$("#btn_egre_guardar").focus();return false; } });
	});
</script>
			
<div class="col-12 col-lg-12 col-md-12 col-sm-12" >
			<form  role="form" id="for_egr" method="POST">
				<input type="hidden" id="action" name="action" value="<?php echo $_POST['action']?>">
				<input type="hidden" id="hdd_egr_id" name="hdd_egr_id" value="<?php echo $_POST['egr_id']?>">
				<input type="hidden" id="hdd_egr_usureg" name="hdd_egr_usureg" value="<?php echo $_SESSION['usuario_id']?>">
				<input type="hidden" id="hdd_egr_usumod" name="hdd_egr_usumod" value="<?php echo $_SESSION['usuario_id']?>">
				<input type="hidden" id="hdd_emp_id" name="hdd_emp_id" value="<?php echo $_SESSION['empresa_id']?>">
				<input type="hidden" id="hdd_gas_id" name="hdd_gas_id" value="<?php echo $gas_id?>">
				<input type="hidden" id="hdd_tra_id" name="hdd_tra_id" value="<?php echo $tra_id?>">
				<input type="hidden" id="hdd_mod_id" name="hdd_mod_id" value="<?php echo $mod_id?>">
				<input type="hidden" id="hdd_modide_id" name="hdd_modide_id" value="<?php echo $modide?>">
				<div class="box-header">
				<h3 class="box-title">Información de Egreso</h3>
				</div>
	
				<div class="row">
					<div class="col-lg-3 col-sm-4 col-6">
						<label for="txt_egr_fec">Fecha:</label>
						<input name="txt_egr_fec" type="text"  class="form-control" id="txt_egr_fec" value="<?php echo $fec?>" > 
					</div>
					<div class="col-lg-9 col-sm-8 col-6">
						<label for="cmb_caj_id">Caja:</label> 
						<select name="cmb_caj_id" id="cmb_caj_id" class="form-control">  </select> 
					</div>
					<div class="col-lg-3 col-sm-4 col-6 d-none">
						<label for="cmb_cue_id">Cuenta:</label>
						<select name="cmb_cue_id" id="cmb_cue_id" class="form-control"> </select> 
					</div>
					<div class="col-lg-3 col-sm-4 col-6 d-none">
						<label for="cmb_subcue_id">Sub Cuenta:</label>
						<select name="cmb_subcue_id" id="cmb_subcue_id" class="form-control">
						</select>
					</div>
				
				
					<div class="col-lg-4 col-sm-6 col-6 d-none">
						<label for="txt_pro_doc">RUC/DNI:</label>
						<div class="input-group">
							<input name="txt_pro_doc" type="text" id="txt_pro_doc" value="<?php echo $pro_doc?>" class="form-control" />					    
						</div>
					</div>
					<div class="col-lg-8 col-sm-6 col-6 d-none">
						<label for="txt_pro_nom">Proveedor:</label>
						<input type="text" id="txt_pro_nom" name="txt_pro_nom"  class="form-control"  value="<?php echo $pro_nom?>" />
						<input type="hidden" id="hdd_pro_id" class="form-control" name="hdd_pro_id" value="<?php echo $pro_id?>" />
					</div>
				
					<div class="col-lg-4 col-sm-6 col-6 d-none">
						<label for="cmb_doc_id">Documento:</label>
						<select name="cmb_doc_id" id="cmb_doc_id" class="form-control"> </select>
					</div>
					<div class="col-lg-3 col-sm-6 col-6 d-none">
						<label for="txt_egr_numdoc">N° Doc.:</label>
						<input name="txt_egr_numdoc" type="text" id="txt_egr_numdoc"  value="<?php echo $numdoc?>" class="form-control" >
					</div>
				
					<div class="col-lg-12 col-sm-12 col-12">
						<label for="txt_egr_det">Detalle:</label>
						<textarea name="txt_egr_det" class="form-control"  rows="1" id="txt_egr_det"><?php echo $det?></textarea> 
					</div> 
				
					<div class="col-lg-4 col-sm-6 col-6" style="display:none;"> 
						<?php if($_POST['action']=="insertar"){?>
						<label for=""> </label>
						<div    class=" input-group checkbox icheck">
							<label title="Activar para Imprimir" >
								<input title="Activar para Imprimir" name="chk_imprimir" type="checkbox" id="chk_imprimir" value="1"> Imprimir?
							</label> 
						</div>  
						<?php }?> 
					</div> 
					<div class="col-lg-8 col-sm-2 col-6">
					</div> 
					<div class="col-lg-4 col-sm-4 col-6 pull-right">
						<label for="txt_egr_imp">Importe:</label>
						<input type="text" name="txt_egr_imp" id="txt_egr_imp" class=" form-control moneda2" style="text-align:right" size="15" maxlength="12" value="<?php echo formato_money($imp)?>" <?php if($_POST['action2']=='caja')echo 'readonly'?> >
					</div> 
				</div> 
			</form>
		
		<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-primary " id="btn_egre_guardar">Guardar</button>
		</div> 
		</div> 