<?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 ("../venta/cVenta.php");
$oVenta = new cVenta();
require_once("../guiaremision/cGuiaremision.php");
$oGuiaremision = new cGuiaremision();
require_once ("../producto/cCatalogo.php");
$oCatalogo = new cCatalogo();
require_once("../formatos/formato.php");
	$dts= $oVenta->mostrarUno($_POST['venid']);
	$dt = mysqli_fetch_array($dts);
	$fec	=mostrarFecha($dt['tb_compra_fec']);
	$empid = $dt['empven'];
	$emp = $dt['tb_empresa_nomcom'];
	$clinom = $dt['tb_cliente_nom'];
	$cliid = $dt['tb_cliente_id'];
	$doc_id	=$dt['tb_documento_id'];
	$monid	=$dt['tb_moneda_id'];
	$numdoc	=$dt['tb_venta_numdoc'];
	$venrel	=$dt['tb_venta_rel'];
	$punvenid	=$dt['tb_puntoventa_id'];
	$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_mon_id" id="hdd_mon_id" value="<?php echo $monid; ?>" />
		<input type="hidden" name="hdd_emp_id" id="hdd_emp_id" value="<?php echo $empid; ?>" />
		<input type="hidden" name="txt_dev_venid" id="txt_dev_venid" value="<?php echo $_POST['venid']; ?>" />
		<input type="hidden" name="txt_dev_punvenid" id="txt_dev_punvenid" value="<?php echo $punvenid; ?>" />
		<input type="hidden" name="action" id="action" value="<?php echo $_POST['action']; ?>" />
		<input type="hidden" name="hdd_docrel" id="hdd_docrel" value="<?php echo $numdoc; ?>" />
		<input type="hidden" name="hdd_cliid" id="hdd_cliid" value="<?php echo $cliid; ?>" />
		<input type="hidden" name="hdd_venrel" id="hdd_venrel" value="<?php echo $venrel; ?>" />
		<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-6 col-sm-6 col-xs-12"> 
			<label for="txt_dev_empid">Cliente :</label></br>
			<input name="txt_dev_empid" id="txt_dev_empid" type="text" value="<?php echo $clinom; ?>" 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-6 col-xs-12"> 
			<label for="txt_dev_numdoc">Num. Devolucion :</label></br>
			<input name="txt_dev_numdoc" id="txt_dev_numdoc" type="text" value="" readonly style="text-align:right; font-size:14px"  class="form-control text-orange" >
		</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" style="display:none" >
		<br />
		<div class="nav-tabs-custom">
		
				<ul class="nav nav-tabs">
						<li class="active"><a href="#tab_3" data-toggle="tab" aria-expanded="false">Transportista</a></li>
						<li class=""> </li>  
				</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">
			<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">
			<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>
		</div>
		</div>
		</div>
		</div>
		
			<div class="col-lg-12 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 class="col-lg-12 col-sm-12 col-xs-12 no-padding" > 		
		<div class="table-responsive">
		
			<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" >PRE. 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);
		
		$pre_uni=$_SESSION['devol_preuni'][$indice];
		$icbper=$_SESSION['devol_valuni'][$indice];
		
		$pretot= $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($pre_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 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">
function txt_dev_numdoc()
 			{
 				$.ajax({
 					type: "POST",
 					url: "../venta/venta_txt_numdoc.php",
 					async:false,
 					dataType: "json",                      
 					data: ({
 						doc_id: 25
 					}),
 					beforeSend: function() {
 						$('#txt_dev_numdoc').val('Cargando...');
 					},
 					success: function(data){			
 						$('#txt_dev_numdoc').val(data.numero);
 						if(data.msj!="")
 						{
							toastr["success"](data.msj);
 						}
 						else
 						{
 						}
 					},
 					complete: function(){
							verificar_numdoc();							
 						}
 					});
 			}
			
			
	function verificar_numdoc(){	
	$.ajax({
		type: "POST",
		url: "../venta/venta_ver_serie.php",
		async:false,
		dataType: "json",                      
		data: ({
			doc_id: 25,
			numdoc: $('#txt_dev_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);					
			}
		}
	});
		
	}
$('#txt_dev_fec').datepicker({
 				format: 'dd-mm-yyyy',
 				autoclose:true			
});
txt_dev_numdoc();
$('#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'
	      			}
	      		}
	      	}
	      }
	  }).on('success.form.bv', function(e) 
	  {
	  	
		$('#div_mensaje_dialog').modal();
		
	  	$.ajax({
	  		type: "POST",
	  		url: "../devolucioncliente/devolucioncliente_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){
				
				toastr["info"](data.dev_msj);
				
				if(data.dev_guiaid>0){
					guia_ticket_datos(data.dev_guiaid);
				}
				if(data.dev_notcreid>0){
					notacredito_ticket_datos(data.dev_notcreid,data.venid);
				}
			
			if(data.estreem==0){
					
				}else if(data.estreem==1){
					toastr["warning"](data.monreem);
				}
				
				$('.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>