<?php
session_start();
require_once('conexion.php');
require_once('include/sesiones.php');
if (isset($_SESSION['usu_nombre']))
{
    $regis=mysqli_query($con,"SELECT * from permiso where modulo_mod_id='20' and usuario_usu_id='$usu_id' ") or die(mysqli_error($con));
    $reg=mysqli_fetch_array($regis);
    if($reg['per_visualizar']=="Activo")
    {
      
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include ('head.php');?>
</head>
<body >
<?php
$inicio="";
$M_inventario="";
$M_contactos="";
$M_compras="";
$M_ventas="active";
$M_pedidos="";
$M_pos="";
$R_reportes="";

$cotizacion="";
$ventas="active";
$periodica="";
$clientes="";
$cartera="";
$garantias="";
$abonos=""; 
$cotizacion_online="";

  include ('menu.php');
?> 
<div class="main">
    <div class="col-md-2"> 
        <?php include ('include/menu_ventas.php');?>
    </div> <!-- /span6 -->
<?php
    $docs_id=$_GET["docs_id"];
    $can=mysqli_query($con,"SELECT * FROM dian_documento_soporte where docs_id='$docs_id'") or die(mysqli_error($con)); 
    $dato0=mysqli_fetch_array($can);
    $docs_prefijo=$dato0['docs_prefijo'];
    $docs_numero=$dato0['docs_numero'];
    $dosc_cuds=$dato0['dosc_cuds'];
    $docs_fecha=$dato0['docs_fecha'];
    $docs_observaciones=$dato0['docs_observaciones'];
?>
    <div class="col-md-10">
        <form role="form" id="registrar_documento" name="registrar_documento">
            <div class="widget stacked">
                <div class="widget-header">
                    <i class="icon-file-text"></i>
                    <h3>NOTA DE AJUSTE A DOCUMENTO DE SOPORTE N° <?php echo $docs_prefijo; ?><?php echo $docs_numero; ?></h3>
                </div> <!-- /.widget-header -->
                <?php

                $docs_proveedor=$dato0['docs_proveedor'];
                $can=mysqli_query($con,"SELECT * FROM proveedor where pro_id='$docs_proveedor'") or die(mysqli_error($con)); 
                $dato=mysqli_fetch_array($can);
                $pro_nombre=$dato['pro_nombre'];

                $resol=mysqli_query($con,"SELECT * FROM serie_factura where ser_estado='activo' and ser_tipo='electronica' and ser_tipo_documento='13' and empresa='$empresa'") or die(mysqli_error($con)); 
                $res=mysqli_fetch_array($resol);
                $result = mysqli_num_rows($resol);
                if($result>0)
                {

                    $ser_id=$res['ser_id'];
                    $ser_inicio=$res['ser_inicio'];
                    $ser_fin=$res['ser_fin'];
                    $prefijo=$res['ser_prefijo'];
                    $can=mysqli_query($con,"SELECT MAX(docs_numero) as docs_numero  
                    FROM dian_documento_soporte where docs_numero >='$ser_inicio' and docs_numero <='$ser_fin' and docs_ser_id='$ser_id'and empresa='$empresa'") or die(mysqli_error($con)); 
                    $dato= mysqli_fetch_array($can);
                    $docs_numero = $dato['docs_numero'];
                    if ($docs_numero==null)
                    {
                        $docs_numero=$ser_inicio;
                    }
                    else if ($docs_numero < $ser_fin) 
                    {  
                        $docs_numero=$dato['docs_numero']+1;
                    }
                ?>
                <div class="widget-content">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label>Proveedor</label>            
                            <input type="text" name="prove" value="<?php echo $pro_nombre;?>" class="form-control input-sm" readonly>
                            <input type="hidden" name="pro_id" value="<?php echo $docs_proveedor;?>" class="form-control input-sm" readonly>
                        </div> 
                    </div><!-- /input-group --> 
                       
                    <div class="col-md-3">
                        <div class="form-group">
                            <label>Fecha</label>
                            <input type="date" name="fecha" value="<?php echo date('Y-m-d');?>" class="form-control input-sm" >
                        </div> <!-- /.form-group -->
                    </div>

                    <div class="col-md-3">
                        <div class="form-group">
                            <label>Documento N</label>
                            <input type="hidden" id="docs_id" name="docs_id" value="<?php echo $docs_id;?>" class="form-control input-sm" readonly>
                            <input type="text" id="numero_documento" value="<?php echo $prefijo;?>-<?php echo $docs_numero;?>" class="form-control input-sm" readonly>
                        </div> <!-- /.form-group -->
                    </div>
    
                    <div class="col-md-9">
                        <div class="form-group">
                            <label>Concepto</label>
                            <select id="docs_motivo" name="docs_motivo" class="form-control input-sm" required>
                                <option value=""> - - </option>
                                <?php
                                    $cnd=mysqli_query($api,"SELECT * FROM credit_note_discrepancy_responses_sd");
                                    while( $cnd1 = mysqli_fetch_array( $cnd ) )
                                    {
                                        echo'<option value="'.$cnd1['id'].'">'.$cnd1['name'].'</option>';
                                    }
                                ?>
                         </select>
                        </div> <!-- /.form-group -->
                    </div>
                            
                    <div class="col-md-3" align="right">
                        <label>&nbsp;</label>
                        <div class="form-group"  align="right"  >
                            <button type="button" class="btn btn-info" data-toggle="modal" data-target="#egreso" style="width:100%">
                                <span class="btn-icon-only icon-plus "></span> Agregar Detalles
                            </button>
                        </div> </br> 
                    </div>
                      
                    <div class="">
                        <div class="table-responsive">
                            <div id="resultados_temp"></div>
                        </div>
                        <div class="col-md-12" style="padding:0px;">
                            <div class="form-group">
                                <label>Observaciones</label>
                                <textarea name="observaciones" id="observaciones" rows="2" cols="50" class="form-control input-sm"><?php echo $docs_observaciones;?></textarea>
                            </div> <!-- /.form-group -->
                        </div>

                        <div class="modal-footer">
                            <a href="V_documento_soporte.php" type="button" class="btn btn-danger" data-dismiss="modal"><i class="fa-solid fa-angles-left"></i> Volver Atras</a>
                            <button type="button" class="btn btn-success" data-toggle="modal" data-target="#form_enviar"><i class="btn-icon-only fa fa-upload"></i>Registrar</button>
                        </div>
                    </div>
                </div> <!-- /.table-responsive -->
            <?php } else { ?>
                </br>
                <div class="alert alert-danger alert-dismissable">
                    <strong>No existe una resolución activa para Nota de Ajuste de Documento Soporte. <a href="A_empresa.php">Regístrela aquí.</a></strong>
                </div>
            <?php } ?>
            </div> <!-- /.widget-content -->

            <div id="form_enviar" class="modal fade" tabindex="-1" role="dialog">
                <div class="modal-dialog ">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                            <h4 class="modal-title">Registrar </h4>
                        </div>
                        <div class="modal-body">
                            <div id="resultados"> </div>
                            <input type="hidden" id="ven_id" name="ven_id" value="" class="form-control name_list" readonly>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
                                <button type="submit" name="btn_registrar" class="btn btn-success" value="btn_registrar" id="btn_registrar">Registrar</button>
                            </div>
                        </div>
                    </div><!-- /.modal-content -->
                </div><!-- /.modal-dialog -->
            </div> <!-- /.form-group -->
        </form>
    </div> <!-- /.widget --> 
</div> <!-- /container -->



<div id="egreso" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
                <h4 class="modal-title">Registro</h4>
            </div>
            <div class="modal-body ">
                <form name="form_soporte" id="form_soporte" method="post" action=""  onsubmit="return checkSubmit();">
                    <div class="">
                        <input type="hidden"  name="cantidad" id="cantidad" class="form-control input-sm" autocomplete="off"  value="1">
                    </div> <!-- /.form-group -->
                    <div class="">
                        <label>Concepto</label>
                        <input type="text"  name="concepto" id="concepto" class="form-control input-sm" autocomplete="off"  value="" >
                    </div> <!-- /.form-group -->
                    <div class="">
                        <label>Valor</label>
                        <input type="number" name="valor" id="valor" class="form-control input-sm" autocomplete="off" value="" >
                    </div> <!-- /.form-group -->
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
                        <button name="btn_agregar" value="btn_agregar" type="submit" class="btn btn-success"><i class="icon-ok"></i> <strong>Registrar</strong></button>
                    </div>
                </form>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>


<script>
function loadTabla(){
var ven_id=$("#ven_id").val();
$.get("./php_doc_soporte/tabla_nota_temp.php?ven_id="+ven_id,function(data){
    $("#resultados_temp").html(data);
    })
}
loadTabla();


$("#form_soporte" ).submit(function( event ) {
$('#btn_agregar').attr("disabled", true);

var parametros = $(this).serialize();
   $.ajax({
      type: "POST",
      url: "php_doc_soporte/tabla_nota_temp.php",
      data: parametros,
        beforeSend: function(objeto){
         $('#resultados_temp').html('<center><img src="./img/ajax-loader.gif"> CARGANDO...</center>');
        },
        success: function(datos){
        $("#resultados_temp").html(datos);
        $('#btn_agregar').attr("disabled", false);
        loadTabla();
      }
  });
  event.preventDefault();
})


$("#registrar_documento" ).submit(function( event ) {
$('#btn_registrar').attr("disabled", true);

var parametros = $(this).serialize();
   $.ajax({
      type: "POST",
      url: "php_doc_soporte/registrar_nota_documento.php",
      data: parametros,
        beforeSend: function(objeto){
         $('#resultados').html('<center><img src="./img/ajax-loader.gif"> CARGANDO...</center>');
        },
        success: function(datos){
        $("#resultados").html(datos);
        $('#btn_registrar').attr("disabled", false);
      //  $('#anular').modal('hide');
      //  $('body').removeClass('modal-open');
      //  $('.modal-backdrop').remove();
       loadTabla();
      }
  });
  event.preventDefault();
})
</script>

<?php
    include ('php_caja/caja_abrir.php');
    include ('script.php');
?>
</body>
</html>
<?php
    }
    else
    {
        echo '<script>alert("No tienes permiso para ver esta pagina")</script>';
        echo '<script> setTimeout("history.back(1)", 0); </script>';
    }
} 
else
{
    echo '<script>location.href = "index.php";</script>';
}
?>