<?php
date_default_timezone_set('America/Lima');session_start();require_once ("../../config/Cado.php");require_once ("cTraspaso.php");$oTraspaso = new cTraspaso();
require_once("../formatos/formato.php");
if($_POST['action']=="insertar"){	$fec=date('d-m-Y');}
if($_POST['action']=="editar"){	
$dts= $oTraspaso->mostrarUno($_POST['tra_id']);	
$dt = mysqli_fetch_array($dts);
$fec		=mostrarFecha($dt['tb_traspaso_fec']);		
$doc_id		=$dt['tb_documento_id'];		
$cod		=$dt['tb_traspaso_cod'];		
$alm_id_ori	=$dt['tb_almacen_id_ori'];		
$alm_id_des	=$dt['tb_almacen_id_des'];		
$obs=$dt['tb_traspaso_obs'];	
mysqli_free_result($dts);}
?>
 <style type="text/css">.ui-autocomplete{    z-index: 99999; }</style>
<script type="text/javascript">
function cmb_alm_id_ori(){	
	$.ajax({		
	type: "POST",	
	url: "../almacen/cmb_alm_id.php",		
	async:true,		dataType: "html",        
	data: ({			alm_id: '<?php echo $alm_id_ori?>'	
	}),		beforeSend: function() {			
	$('#cmb_alm_id_ori').html('<option value="">Cargando...</option>');		
	},		
	success: function(html){	
	$('#cmb_alm_id_ori').html(html);
	
	}	
	});}
function cmb_alm_id_des(){	
	$.ajax({		
	type: "POST",	
	url: "../almacen/cmb_alm_id.php",	
	async:true,		dataType: "html",    
	data: ({ alm_id: '<?php echo $alm_id_des?>',	
	alm_ori: $('#cmb_alm_id_ori').val()		
	}),		
	beforeSend: function() {	
	$('#cmb_alm_id_des').html('<option value="">Cargando...</option>');		
	},		
	success: function(html){	
	$('#cmb_alm_id_des').html(html);	
	}
	});}
function agregar_producto(act,idf, can,preven,marid,des,mon,emp,est)
{
	$.ajax({
		type: "POST",
		url: "../traspaso/agregar_traspaso.php",
		async:true,
		dataType: "json",                      
		data: ({
			action:act,
			cat_id:idf,
			mar_id:marid,	
			alm_id_des: $('#cmb_alm_id_des').val(),	
			alm_id_ori: $('#cmb_alm_id_ori').val(),	
			cat_can : can
							
		}),
		beforeSend: function() {
			
		},
		success: function(data){
			if(data.estado==false)
			{
				toastr["error"](data.msj);
			}else if(data.estado==true)
			{
				$('#txt_bus_pro_codbar').val('');
				$('#txt_bus_pro_codbar').focus();
				toastr["info"](data.msj);
			}
		},
		complete: function(data)
		{
				traspaso_car_tabla();
		} 
	});
	}
	
	function traspaso_car_tabla(tra_id){
		$.ajax({
			type: "POST",
			url: "../traspaso/traspaso_detalle_tabla.php",
			async: true,
			dataType: "html", 
			data:({
				tra_id : tra_id
			}),
			beforeSend: function() {
				$('#div_traspaso_detalle_tabla').addClass("ui-state-disabled");
			},
			success: function(html){			
				$('#div_traspaso_detalle_tabla').html(html);
			},
			complete: function(){			
				$('#div_traspaso_detalle_tabla').removeClass("ui-state-disabled");
			}
		});
	}
	 function buscar_producto(codbar){
	 if(codbar!=""){
		$('#div_mensaje_dialog').modal('show');
	$.ajax({
			type: "POST",
			url: "../producto/producto_codbar_data.php",
			async:true,
			dataType: "json",                      
			data: ({
				codbar: codbar
			}),
			beforeSend: function(){
				var divmen = "<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> PROCESANDO PETICIÓN</div>  </center>";	
				$('.div_mensaje').html(divmen);
			},
			success: function(data){
			if(data.est=='1'){
				agregar_producto('agregar',data.catid,data.can,data.preven,data.marid,'','','');
			}else{
			toastr["error"]('No existe ningun porducto con ese COD. BAR.');	
			}
			$('#div_mensaje_dialog').modal('hide');
			}
		});
	}else{
		toastr["error"]('Ingrese un codigo de barras.');	
	}
 }	
 	function agregar_producto_lista(codmar,dat1,dat2,dat3,dat4,mar){
			$('#div_productomarca_form_dialog').modal();
		
		
			$.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:'transpaso_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);				
				}
			});
		}
	
			function txt_tra_numdoc()
 			{
 				$.ajax({
 					type: "POST",
 					url: "../venta/venta_txt_numdoc.php",
 					async:false,
 					dataType: "json",                      
 					data: ({
 						doc_id: 24
 					}),
 					beforeSend: function() {
 						$('#txt_tra_numdoc').val('Cargando...');
 					},
 					success: function(data){			
 						$('#txt_tra_numdoc').val(data.numero);
 						if(data.msj!="")
 						{
							toastr["success"](data.msj);
 						}
 						else
 						{
 						}
 					},
 					complete: function(){
							verificar_numdoc();							
 						}
 					});
 			}
			
			
			function verificar_numdoc(){	
	$.ajax({
		type: "POST",
		url: "../cotizacion/cotizacion_ver_serie.php",
		async:false,
		dataType: "json",                      
		data: ({
			doc_id: 24,
			numdoc: $('#txt_tra_numdoc').val(),
			ven_est: '1' //$('#cmb_ven_est').val()
		}),
		beforeSend: function() {
		},
		success: function(data){			
			$('#hdd_tra_doc').val(data.valor);
			if(data.msj!="")
			{			
			toastr["info"](data.msj);					
			}
		}
	});
	
}
$(function() {
	
	
		$("#txt_tra_numdoc").addClass("ui-state-active");
			txt_tra_numdoc();
			
	$('#txt_tra_fec').datepicker({ 	format: 'dd-mm-yyyy', 	autoclose:true				});							
		$("#cmb_alm_id_ori").focus();
	cmb_alm_id_ori();
<?php if($_POST['action']=="insertar"){?>
		agregar_producto('restablecer');
		$('#cmb_alm_id_ori').change( function()		{			cmb_alm_id_des();		});
		$("#cmb_alm_id_ori").keydown(function(e){			if (e.keyCode == 13){ $("#cmb_alm_id_des").focus(); return false;}		});
		$("#cmb_alm_id_des").keydown(function(e){			if (e.keyCode == 13){ $("#txt_tra_obs").focus(); return false;}		});
		$("#txt_tra_obs").keydown(function(e){			if (e.keyCode == 13){ $("#txt_bus_pro_codbar").focus(); return false;}		});
		
		
		$( "#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();
				}	
					
			}
		});
	
	
	$("#txt_bus_pro_mar").keydown(function(e){
		if (e.which == 13){			
			agregar_producto_lista('','','','');
			return false;
		}
	});
	
	
	$("#txt_bus_pro_codbar").keydown(function(e){
		if (e.which == 13){			
			buscar_producto($("#txt_bus_pro_codbar").val());
			return false;
		}
	});
<?php }?>
	<?php if($_POST['action']=="editar"){?>		cmb_alm_id_des();
		traspaso_car_tabla(<?php echo $_POST['tra_id'];?>);		$('#cmb_alm_id_ori').attr('disabled', 'disabled');		$('#cmb_alm_id_des').attr('disabled', 'disabled');	<?php } ?>
	$("#btn_tras_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-2',	      		validators: {	      			notEmpty: {	      				message: 'Este campo es requerido'	      		}	      		}	      	},				cmb_alm_id_ori: {	      		group: '.col-lg-2',	      		validators: {	      			notEmpty: {	      				message: 'Este campo es requerido'	      			}	      		}	      	},				cmb_alm_id_des: {	      		group: '.col-lg-2',	      		validators: {	      			notEmpty: {	      				message: 'Este campo es requerido'	      			}	      		}	      	},			hdd_tra_numite: {	      		group: '.col-lg-2',	      		validators: {	      			notEmpty: {	      				message: 'Este campo es requerido'	      			}	      		}	      	}	      }
	  }).on('success.form.bv', function(e) {
	  	$.ajax({	  		type: "POST",	  		url: "../traspaso/traspaso_reg.php",	  		async:true,	  		dataType: "json",	  		data: $("#for_tra").serialize(),	  		beforeSend: function() {					 
				  		},	  		success: function(data){				$('.modal').modal('hide');				$('.in').removeClass('modal-backdrop');		  		toastr["success"](data.msj);				traspaso_tabla();						},
	  		complete: function(){
	  		}	  	}); 
	  });
	  
	  
	  
		
	
});
</script>

<div class="col-xs-12 col-lg-12 col-md-12 col-sm-12" >
	
	<form id="for_tra">
		<input name="action_traspaso" id="action_traspaso" 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 name="hdd_usu_id" id="hdd_usu_id" type="hidden" value="<?php echo $_SESSION['usuario_id']?>">
		<input name="hdd_emp_id" id="hdd_emp_id" type="hidden" value="<?php echo $_SESSION['empresa_id']?>">
		
			<div class="box-header with-border">
					<h3 class="box-title">Registrar traspaso</h3>
			</div>
						
			<div class="row">
			
			<div class="col-lg-3 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?>" readonly>
			</div>
			
			<div class="col-lg-3 col-sm-6 col-xs-6 col-md-4">
 				<label for="txt_tra_numdoc">N° Doc:</label>
 				<input name="txt_tra_numdoc" type="text" class="form-control text-orange" id="txt_tra_numdoc" style="text-align:right;font-size:14px"  value="<?php echo $numdoc?>" readonly>						 
 			</div>
			
		<div class="col-lg-3 col-xs-6">	
		<label for="cmb_alm_id_ori">Origen:</label>
		<select  class="form-control" name="cmb_alm_id_ori" id="cmb_alm_id_ori"></select>		
		</div>	
		
		<div class="col-lg-3 col-xs-6">	
		<label for="cmb_alm_id_des">Destino:</label>
		<select class="form-control" name="cmb_alm_id_des" id="cmb_alm_id_des"></select>
		</div>	
		
		<div class="col-lg-6 col-xs-12">	
		<label for="txt_tra_obs">Observación:</label>
		<input name="txt_tra_obs" class="form-control" type="text" id="txt_tra_obs" value="<?php echo $obs?>" >
		</div>
		
		</div>
		
							
	      
    	<div class="col-lg-12 col-md-12 col-xs-12" style="height:5px;"></div>	
		
		
		
		<?php if($_POST['action']=="insertar"){?>
	   
<div class=" div_pro card card-default card-solid card-primary " style="padding:0px;" >
								<div id="fieldset_producto" class="col-lg-12 col-md-12 col-xs-12 " style="padding:0px;" >								
										<div class="card-body">
											<div class="row">
											
 												<div class="col-lg-2 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-2 col-sm-5 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-4 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="fas 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">
												<li><a class="dropdown-item text-white" onClick="agregar_producto_lista();" href="javascript:void(0)"> <i class="fas fa-filter"></i> Seleccionar productos</a></li>
															<!--	<li><a onClick="producto_form('insertar')" href="javascript:void(0)"><i class="fa fa-plus"></i> Nuevo Producto</a></li> -->
												</ul>
												</div> 
											</div>
													
 												
 													
 										</div> 
 									</div>
 							</div>
 					</div>
									
	
    	<?php }?>
    	
    </form>
			
			<div class="col-lg-12 col-xs-12" style="height:5px;">	
			
			</div>
			
			<div class="col-lg-12 col-xs-12" >	
			<div id="div_traspaso_detalle_tabla"></div>
			</div>
			
	
	
			<div class="modal-footer float-right">	 
			<button type="button" class="btn btn-default " id="" data-dismiss="modal">Cancelar</button>
			<?php if($_POST['action']=="insertar"){?>
			<button type="button" class="btn btn-primary " id="btn_tras_guardar">Guardar</button>
			<?php }?>
			</div> 
		
		
		
			</div>