<?php

//ini_set('display_errors',1);

date_default_timezone_set('America/Lima');
session_start();
require_once ("../../config/Cado.php");
require_once ("cTransferencia.php");
$oTransferencia = new cTransferencia();
require_once ("../usuario/cUsuario.php");
$oUsuario = new cUsuario();
require_once("../formatos/formato.php");
if($_POST['action']=="insertar"){
	$fec=date('d-m-Y');
	$est='1';
	$imp=00;
	$caj_id_ori=$_SESSION['caja_id'];
}
if($_POST['action']=="editar"){
	$dts= $oTransferencia->mostrarUno($_POST['tra_id']);
	$dt = mysqli_fetch_array($dts);
	$reg = mostrarFechaHora($dt['tb_transferencia_fecreg']);
	$mod = mostrarFechaHora($dt['tb_transferencia_fecmod']);
	$fec=mostrarFecha($dt['tb_transferencia_fec']);
	$det=$dt['tb_transferencia_det'];
	$imp=$dt['tb_transferencia_imp'];
	$caj_id_ori=$dt['tb_caja_id_ori'];
	$caj_id_des=$dt['tb_caja_id_des'];
	$mon_id=$dt['tb_moneda_id'];
	$est=$dt['tb_transferencia_est'];
	$usu_id_reg	=$dt['tb_transferencia_usureg'];
	$usu_id_mod	=$dt['tb_transferencia_usumod'];
	mysqli_free_result($dts);
}
if($usu_id_reg>0)
{
	$dts=$oUsuario->mostrarUno($usu_id_reg);
	$dt = mysqli_fetch_array($dts);
	$usugru		=$dt['tb_usuariogrupo_id'];
	$usugru_nom	=$dt['tb_usuariogrupo_nom'];
	$usu_nom	=$dt['tb_usuario_nom'];
	$apepat		=$dt['tb_usuario_apepat'];
	$apemat		=$dt['tb_usuario_apemat'];
	$ema		=$dt['tb_usuario_ema'];
	
	mysqli_free_result($dts);
	
	$usuario_reg="$usu_nom $apepat $apemat";
}
if($usu_id_mod>0)
{
	$dts=$oUsuario->mostrarUno($usu_id_mod);
	$dt = mysqli_fetch_array($dts);
	$usugru		=$dt['tb_usuariogrupo_id'];
	$usugru_nom	=$dt['tb_usuariogrupo_nom'];
	$usu_nom		=$dt['tb_usuario_nom'];
	$apepat		=$dt['tb_usuario_apepat'];
	$apemat		=$dt['tb_usuario_apemat'];
	$ema		=$dt['tb_usuario_ema'];
	
	mysqli_free_result($dts);
	
	$usuario_mod="$usu_nom $apepat $apemat";
}
?>
<script type="text/javascript">


$('#txt_tra_fec').datepicker({
		format: 'dd-mm-yyyy',
		autoclose:true, 
		endDate: '+0d'
	})
	
	function cmb_caj_id_ori(ids)
	{	
		$.ajax({
			type: "POST",
			url: "../caja/cmb_caj_id.php",
			async:false,
			dataType: "html",                      
			data: ({
				caj_id: ids
			}),
			beforeSend: function() {
				$('#cmb_caj_id_ori').html('<option value="">Cargando...</option>');
			},
			success: function(html){
				$('#cmb_caj_id_ori').html(html);
			}
		});
	}
	function cmb_caj_id_des(ids)
	{	
		$.ajax({
			type: "POST",
			url: "../caja/cmb_caj_id_tra.php",
			async:false,
			dataType: "html",                      
			data: ({
				caj_id: ids,
				caj_id_ori: $('#cmb_caj_id_ori').val()
			}),
			beforeSend: function() {
				$('#cmb_caj_id_des').html('<option value="">Cargando...</option>');
			},
			success: function(html){
				$('#cmb_caj_id_des').html(html);
			}
		});
	}
	$(function() {
		cmb_caj_id_ori(<?php echo $caj_id_ori?>);
		<?php if($_POST['action']=='insertar'){?>
			$('#cmb_caj_id_ori').change(function(){
				cmb_caj_id_des(<?php echo $caj_id_des?>);
			});
			<?php }?>
		 
				cmb_caj_id_des(<?php echo $caj_id_des?>);
			 
				$( "#txt_tra_det" ).autocomplete({
					minLength: 1,
					source: "../transferencia/complete_transferencia_det.php"
				});
				$('#txt_tra_det').change(function(){
					$(this).val($(this).val().toUpperCase());
				});
$("#btn_trans_guardar").click(function() {
	$("#for_tra").submit();
});
$('#for_tra').bootstrapValidator({ 
	      message: 'Valor no valido',
	      feedbackIcons: {
	      	valid: 'glyphicon glyphicon-ok',
	      	invalid: 'glyphicon glyphicon-remove',
	      	validating: 'glyphicon glyphicon-refresh'
	      },
	      fields: {	      	
	      	txt_tra_fec: {
	      		group: '.col-lg-3',
	      		validators: {	      			
	      			date: {
	      				format: 'DD-MM-YYYY',
	      				message: 'Fecha no Valida'
	      			}
	      		}
	      	},txt_tra_det: {
	      		group: '.col-lg-12',
	      		validators: {
	      			notEmpty: {
	      				message: 'Campo vacio *'
	      			}
	      		}
	      	},cmb_caj_id_ori: {
	      		group: '.col-lg-3',
	      		validators: {
	      			notEmpty: {
	      				message: 'Campo vacio *'
	      			}
	      		}
	      	},cmb_caj_id_des: {
	      		group: '.col-lg-3',
	      		validators: {
	      			notEmpty: {
	      				message: 'Campo vacio *'
	      			}
	      		}
	      	},txt_tra_imp: { 
	      		group: '.col-lg-4',
	      		validators: {
	      			notEmpty: {
	      				message: 'Campo vacio *'
	      			}
	      		}
	      	} 
	      }
	  }).on('success.form.bv', function(e) { 
	  
	  	$.ajax({
			type: "POST",
			url: "../transferencia/transferencia_reg.php",
			async:true,
			dataType: "json",
			data:$("#for_tra").serialize()+ "&nom_cajaori="+$('#cmb_caj_id_ori option:selected').text()+"&nom_cajades="+$('#cmb_caj_id_des option:selected').text(),
			beforeSend: function(){
	  			
			},
			success: function(data){
				$('.modal').modal('hide');
				$('.in').removeClass('modal-backdrop');		
				toastr["success"](data.tra_msj);				
				},
				complete: function(){
					<?php
					if($_POST['vista']=="transferencia_tabla")
					{
						echo $_POST['vista'].'();';
					}
					if($_POST['vista']=="caja_tabla")
					{
						echo $_POST['vista'].'();';
					}
					?>
				}
			});	 
	  });
 $("#txt_tra_fec").keydown(function(e){
	  	if (e.which == 13){$("#cmb_caj_id_ori").focus();return false; } });
  $("#cmb_caj_id_ori").keydown(function(e){
	  	if (e.which == 13){$("#cmb_caj_id_des").focus();return false; } });
   $("#cmb_caj_id_des").keydown(function(e){
	  	if (e.which == 13){$("#txt_tra_det").focus();return false; } });
    $("#txt_tra_det").keydown(function(e){
	  	if (e.which == 13){$("#txt_tra_imp").focus();return false; } });
     $("#txt_tra_imp").keydown(function(e){
	  	if (e.which == 13){$("#btn_trans_guardar").focus();return false; } }); 
});
</script>
<!-- Form content -->
<div class="col-xs-12 col-lg-12 col-md-12 col-sm-12" >

<div class="modal-header">
	<h4 class="modal-title" id="titlemodal">Información de Transferencia</h4> 
</div>
<div class="modal-body">
	<div class="row">
		<div class="col-xs-12 col-lg-12 col-md-12 col-sm-12" >
			
			<form  role="form" id="for_tra" method="POST"> 
				<input name="action" id="action" type="hidden" value="<?php echo $_POST['action']?>">
				<input name="hdd_tra_id" id="hdd_tra_id" type="hidden" value="<?php echo $_POST['tra_id']?>">
				<input type="hidden" id="hdd_tra_usureg" name="hdd_tra_usureg" value="<?php echo $_SESSION['usuario_id']?>">
				<input type="hidden" id="hdd_tra_usumod" name="hdd_tra_usumod" value="<?php echo $_SESSION['usuario_id']?>">
				<input type="hidden" id="hdd_emp_id" name="hdd_emp_id" value="<?php echo $_SESSION['empresa_id']?>">
				
				<div class="row">
					<div class="col-lg-3 col-sm-4 col-xs-6">
						<label for="txt_tra_fec">Fecha:</label>
						<input name="txt_tra_fec" type="text" class="form-control" id="txt_tra_fec" value="<?php echo $fec?>" class="form-control"  maxlength="10">
					</div>
					<div class="col-lg-1 col-sm-4 col-xs-6"> </div>
					<div class="col-lg-4 col-sm-4 col-xs-6">
						<label for="cmb_caj_id_ori">Caja Origen:</label>
						<select name="cmb_caj_id_ori" id="cmb_caj_id_ori" class="form-control"> </select>
					</div>
					<div class="col-lg-4 col-sm-4 col-xs-6">
						<label for="cmb_caj_id_des">Caja Destino:</label>
						<select name="cmb_caj_id_des" id="cmb_caj_id_des" class="form-control">
						</select> 
					</div>
				</div>
				<div class="row">
					<div class="col-lg-12 col-sm-12 col-xs-12">
						<label for="txt_tra_det">Detalle:</label>
						<textarea name="txt_tra_det"  rows="1" class="form-control"  id="txt_tra_det"><?php echo $det?></textarea> 
					</div> 
				</div> 
				<div class="row">
					
					<div class="col-lg-4 col-sm-4 col-xs-6">
						<?php 
						if($_POST['action']=="insertar"){?>
						<label for="chk_imprimir">Responsable de la transferencia</label><br>
						<span class="badge badge-danger"><?php echo $_SESSION['usuario_nombre']?></span>
						<?php } 
						if($_POST['action']=="editar"){?>  <br>
						<span title="REGISTRADO POR" class="badge badge-warning"><i class="fa fa-plus"></i> <?php echo $usuario_reg.' - '.$reg; ?></span>
						<span title="EDITADO POR" class="badge badge-primary"><i class="fa fa-pencil"></i> <?php echo $usuario_mod.' - '.$mod; ?></span>
						<?php } ?>			
					</div>
					<div class="col-lg-4 col-sm-4 col-xs-4 float-right">
						<label for="txt_tra_imp">Importe:</label>
						<input type="text" name="txt_tra_imp" id="txt_tra_imp" class="form-control moneda" style="text-align:right"  maxlength="12" value="<?php echo $imp?>">
					</div> 
				</div> 
			</div>
		</form>
		<!-- FIN CONTENIDO MODAL -->     
	</div>
	
	<div class="modal-footer float-right">	 
		<button type="button" class="btn btn-default " id="btn_trans_cancelar" data-dismiss="modal">Cancelar</button>
		<button type="button" class="btn btn-primary " id="btn_trans_guardar">Guardar</button>
	</div> 
	
			</div>
		</div>
	</div>