<?php 
session_start();
if($_SESSION["sistema"]!= "1"){ header("location: ../login/index.php"); exit();}
date_default_timezone_set('America/Lima');
require_once ("../../config/Cado.php");
require_once ("../compra/cCompra.php");
$oCompra = new cCompra();
require_once ("../producto/cCatalogo.php");
$oCatalogo = new cCatalogo();
require_once("../formatos/formato.php");
	$dts= $oCompra->mostrarUno($_POST['comid']);
	$dt = mysqli_fetch_array($dts);
	$fec	=mostrarFecha($dt['tb_compra_fec']);
	$empid = $dt['tb_empresa_id'];
	$emp = $dt['tb_empresa_nomcom'];
	$emp_dir = $dt['tb_empresa_dir'];
	$prov_id = $dt['tb_proveedor_id'];
	$prov_doc = $dt['tb_proveedor_doc'];
	$prov_dir = $dt['tb_proveedor_dir'];
	$prov = $dt['tb_proveedor_nom'];
	$doc_id	=$dt['tb_documento_id'];
	$monid	=$dt['tb_moneda_id'];
	$numdoc	=$dt['tb_compra_numdoc'];
	$fecha=date('d-m-Y');
	
	
	if($monid==1){
		$lbl = 'S/ ';
	}else if($monid==2){
		$lbl = 'US$ ';
	}
	
	
	if(isset($_SESSION['devol_car']))
	{
	$num_rows=count($_SESSION['devol_car']);
	if($num_rows==0)$num_rows="";
	
	foreach($_SESSION['devol_car'] as $indice=>$cantidad){
		$total_cantidad+=$cantidad;
	}
	}
	else
	{
	$num_rows="0";
	}
?>
<div class="col-lg-12 col-md-12 col-xs-12 " >
	<form  id="for_dev"  > 
		<div class="box-header with-border">
			<h3 class="box-title">Registrar Devolucion</h3>
		</div>
		
		<input type="hidden" name="hdd_emp_id" id="hdd_emp_id" value="<?php echo $empid; ?>" />
		<input type="hidden" name="hdd_prov_doc" id="hdd_prov_doc" value="<?php echo $prov_doc; ?>" />
		<input type="hidden" name="hdd_prov_id" id="hdd_prov_id" value="<?php echo $prov_id; ?>" />
		<input type="hidden" name="hdd_prov_dir" id="hdd_prov_dir" value="<?php echo $prov_dir; ?>" />
		<input type="hidden" name="hdd_prov_nom" id="hdd_prov_nom" value="<?php echo $prov; ?>" />
		<input type="hidden" name="txt_dev_comid" id="txt_dev_comid" value="<?php echo $_POST['comid']; ?>" />
		<input type="hidden" name="action" id="action" value="<?php echo $_POST['action']; ?>" />
		<div class="row"> 
		
		<div class="col-lg-3 col-sm-6 col-xs-12"> 
			<label for="txt_dev_empid">Empresa :</label></br>
			<input name="txt_dev_empid" id="txt_dev_empid" type="text" value="<?php echo $emp; ?>" readonly class="form-control" >
		</div>
		
		<div class="col-lg-3 col-sm-6 col-xs-12"> 
			<label for="txt_dev_empid">Proveedor :</label></br>
			<input name="txt_dev_empid" id="txt_dev_empid" type="text" value="<?php echo $prov; ?>" readonly class="form-control" >
		</div>
		
		<div class="col-lg-3 col-sm-6 col-xs-12"> 
			<label for="txt_dev_empid">Num Doc :</label></br>
			<input name="txt_dev_empid" id="txt_dev_empid" type="text" value="<?php echo $numdoc; ?>" readonly class="form-control" >
		</div>
		<div class="col-lg-3 col-sm-2 col-xs-6">
			<label for="txt_dev_fec">Fecha Devolucion:</label>
			<input  name="txt_dev_fec" type="text"  class="form-control" autocomplete="off" id="txt_dev_fec" value="<?php echo $fecha?>" >
		</div>
		
		<div class="col-lg-12 col-sm-12 col-xs-12">
		<br />
		<div class="nav-tabs-custom">
		
				<ul class="nav nav-tabs">
						<a href="#tab_3" class="nav-item nav-link active"  data-toggle="tab" aria-expanded="false">Transportista</a>
				</ul>								
				
		<div class="tab-content">
				<div class="active tab-pane" id="tab_3">
						<div class="row">
			
			<div class="col-lg-4 col-sm-2 col-xs-6" style="display:none;">
			<label for="txt_dev_tranom">Nombre:</label>
			<input  name="txt_dev_tranom" type="text"  autocomplete="off" class="form-control"  id="txt_dev_tranom" value="<?php echo ''?>" >
			</div>
			
			<div class="col-lg-4 col-sm-2 col-xs-6" style="display:none;">
			<label for="txt_dev_tracom">Comprobante:</label>
			<input  name="txt_dev_tracom" type="text" autocomplete="off" class="form-control"  id="txt_dev_tracom" value="<?php echo ''?>" >
			</div>
			
			<div class="col-lg-6 col-sm-2 col-xs-6">
			<label for="txt_ori_gui">Punto salida:</label>
			<input  name="txt_ori_gui" type="text" autocomplete="off" class="form-control"  id="txt_ori_gui" value="<?php echo $emp_dir; ?>" >
			</div>
			
			<div class="col-lg-6 col-sm-2 col-xs-6">
			<label for="txt_des_gui">Punto llegada:</label>
			<input  name="txt_des_gui" type="text" autocomplete="off" class="form-control"  id="txt_des_gui" value="<?php echo $prov_dir; ?>" >
			</div>
		 
			<div class="col-lg-6 col-sm-2 col-xs-6">
			<label for="txt_ate_guia">Atencion:</label>
			<input  name="txt_ate_guia" type="text" autocomplete="off" class="form-control"  id="txt_ate_guia" value="<?php echo ''; ?>" >
			</div>
		
			<div class="col-lg-6 col-sm-12 col-xs-12">
			<label for="txt_dev_obs1">Observacion:</label>
			<textarea name="txt_dev_obs1" id="txt_dev_obs1" class="form-control"></textarea>
			</div>
		
		</div>
		</div>
		</div>
		</div>
		</div>
		
			
			
			</div>
		
	<div class="col-lg-12 col-sm-12 col-xs-12" > 
			
	<div class="table-responsive">
		
	<div class="col-lg-12 col-md-12 col-xs-12 " >
	<strong style="font-size:2rem;" >ITEMS A DEVOLVER </strong>
		
	<table cellspacing="1" id="tabla_compra_detalle" class="table table-bordered table-striped"> 
    <thead>
    <tr >       
		<th class="" >CANTIDAD</th>
        <th style="white-space: nowrap;">ARTÍCULO</th>
        <th class="" style="white-space: nowrap;" >MARCA</th>
        <th>UNIDAD</th>
		<th align="right" >V. UNI.</th>		
		<th align="right" >PRECIO TOTAL.</th>       
        </tr>
    </thead>
    <tbody>
    <?php
	
	if($num_rows>0){
	
	foreach($_SESSION['devol_car'] as $indice=>$cantidad)
	{
    
		$dts1=$oCatalogo->mostrarUno($_SESSION['devol_catid'][$indice],$_SESSION['devol_marid'][$indice]);
		$dt1 = mysqli_fetch_array($dts1);
		
		$valor_uni=$_SESSION['devol_valuni'][$indice];
		$valor_uni=$_SESSION['devol_valuni'][$indice];
		
		$pre_uni= formato_money4($valor_uni) * 1.18;
		$pretot= formato_money4($pre_uni) * $cantidad
		
	?>
	
      <tr >
       
		  <td class="" ><strong class="text-primary"> <?php echo $cantidad ;?> </strong></td>
          <td style="white-space: nowrap;"><?php  echo '<strong>'.$dt1['tb_producto_nom'].'</strong>';?></td>
          <td class="" style="white-space: nowrap;"><?php echo $dt1['tb_marca_nom'];?></td>
          <td><?php echo $dt1['tb_unidad_nom']?></td>
          <td class="" align="right"><?php echo formato_money($valor_uni);?></td>
          <td class="" align="right"><?php echo formato_money($pretot);?></td>
      </tr>
    <?php
	  $tot += $pretot;
    }
	}else{
	?>
		<tr>
		<td COLSPAN="6">NO HAY ITEMS SELECCIONADOS</td>
		</tr>
	<?php 
	}
    ?>
	
	<tr>
		<td colspan="4"></td>
		<td align="right" ><?php echo $lbl; ?></td>
		<td colspan="" style="text-align:right;"> 
		<input type="text" name="txt_dev_tot" autocomplete="off" class="form-control" id="txt_dev_tot" value="<?php echo formato_money($tot)?>"  style="text-align:right;font-weight: bold;" readonly >
		</td>
	</tr>
	
  </tbody>
</table>
		
		
		</div>
		</div>
		</div>
		
		
		
		<div class="col-lg-12 col-sm-12 col-xs-12" style="height:15px;"> </div>		
	</form>
	<div class="box-footer float-right ">
			<span  data-dismiss="modal" style="cursor:pointer" class="btn btn-default cancel">Cancelar</span>
			<span class="btn btn-primary" style="cursor:pointer" id="save_form">Guardar</span>
	</div>	
</div>				
<script type="text/javascript">
$('#txt_dev_fec').datepicker({
 				format: 'dd-mm-yyyy',
 				autoclose:true			
});
$("#txt_ate_guia,#txt_des_gui,#txt_ori_gui").keydown(function(e){
		if (e.which == 13){
			return false;
			}
	});
	
		$('#txt_des_gui,#txt_ori_gui,#txt_ate_guia').change(function(){
		$(this).val($(this).val().toUpperCase());
	});
$('#save_form').on('click', function() {  
		$("#for_dev").submit();
});
	
	$('#for_dev').bootstrapValidator({
	     
	      framework: 'bootstrap',
	      message: 'Valor no valido',
	     /* feedbackIcons: {
	      	valid: 'glyphicon glyphicon-ok',
	      	invalid: 'glyphicon glyphicon-remove',
	      	validating: 'glyphicon glyphicon-refresh'
	      },*/
	      fields: {	      	
	      	txt_dev_fec: {
	      		group: '.col-lg-4',
	      		validators: { 
	      			date: {
	      				format: 'DD-MM-YYYY',
	      				message: 'Fecha no Valida'
	      			}
	      		}
	      	},
	      	txt_dev_tracom: {
	      		group: '.col-lg-4',
	      		validators: {
	      			notEmpty: {
	      				message: 'Requerido *'
	      			}
	      		}
	      	}
	      }
	  }).on('success.form.bv', function(e) 
	  {
	  	
		$('#div_mensaje_dialog').modal();
		
	  	$.ajax({
	  		type: "POST",
	  		url: "../devolucionproveedor/devolucionproveedor_reg.php",
	  		async:true,
	  		dataType: "json",
	  		data: $("#for_dev").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){
				
				
				guia_ticket_datos(data.guia_id);
				
				toastr["info"](data.dev_msj);
				$('.modal').modal('hide');
				$('.in').removeClass('modal-backdrop');	
	  		},
	  		complete: function(){  	
			$("#txt_dev_numdoc").val('');
			devolucion_tabla();
			
			
			
			
			}
	  	});	 
	  });
			
			
	$("#txt_dev_tranom").change(function(){
		$(this).val($(this).val().toUpperCase());
	});	
			
	
</script>