<?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 -->
    <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>DOCUMENTO DE SOPORTE</h3>
                </div> <!-- /.widget-header -->
                <?php
                    $resol=mysqli_query($con,"SELECT * FROM serie_factura where ser_estado='activo' and ser_tipo='electronica' and ser_tipo_documento='11' 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-3">
                        <div class="form-group">
                            <label>Proveedor</label>            
                            <section id="intro" >
                                <select name="pro_id" id="pro_id" class="form-control name_list input-sm" required>
                                    <option value=""> - - </option>
                                    <?php
                                        $datoss = mysqli_query($con,"SELECT * FROM proveedor WHERE empresa='$empresa' and pro_id<>'$pro_id' ") ;
                                        while( $filas = mysqli_fetch_array( $datoss ) )
                                            {
                                                echo'<option value="'.$filas['pro_id'].'">'.utf8_encode($filas['pro_id']).' - '.utf8_encode($filas['pro_nombre']).'</option>';
                                            }
                                    ?>
                                </select>
                            </section>
                        </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="text" id="docs_id" value="<?php echo $docs_numero;?>" class="form-control input-sm" readonly>
                        </div> <!-- /.form-group -->
                    </div>

                    <div class="col-md-3" style="padding-top: 0px;">
                        <div class="form-group">
                            <label>Forma de pago</label>
                            <select name="forma_pago" id="forma_pago" class="form-control name_list input-sm" required>
                                <?php
                                    $datoss=mysqli_query($api,"SELECT * FROM payment_forms");
                                    while( $filas = mysqli_fetch_array( $datoss ) )
                                    {
                                        echo'<option value="'.$filas['id'].'">'.$filas['name'].'</option>';
                                    }
                                ?>
                            </select>
                        </div> <!-- /.form-group -->
                    </div>

                    <div class="col-md-3">
                        <div class="form-group">
                            <label>Medio de pago</label>
                            <select name="medio_pago" id="medio_pago" class="form-control name_list input-sm" required>
                                <?php
                                    $datoss=mysqli_query($api,"SELECT * FROM payment_methods");
                                    while( $filas = mysqli_fetch_array( $datoss ) )
                                    {
                                        echo'<option value="'.$filas['id'].'">'.$filas['name'].'</option>';
                                    }
                                ?>
                            </select>
                        </div> <!-- /.form-group -->
                    </div>

                    <div class="col-md-3">
                        <div class="form-group">
                            <label>Plazo de Pago (Dias)</label>
                             <input type="text" name="plazo_dias" id="plazo_dias" value="0" class="form-control input-sm">
                        </div> <!-- /.form-group -->
                    </div>

                    <script>
                    document.addEventListener("DOMContentLoaded", function () {
                        const formaPago = document.getElementById("forma_pago");
                        const plazoDias = document.getElementById("plazo_dias");
                        function controlarPlazo() {
                            if (formaPago.value == "1") { // CONTADO
                                plazoDias.value = 0;
                                plazoDias.readOnly = true;
                            } else { // CRÉDITO
                                plazoDias.readOnly = false;
                            }
                        }
                        // Al cargar la página
                        controlarPlazo();
                        // Al cambiar la forma de pago
                        formaPago.addEventListener("change", controlarPlazo);
                    });
                    </script>

                    <div class="col-md-9 col-sm-9"> </br> </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"></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 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">脳</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="">
                        <label>Tipo de Envio</label>
                        <select name="tipo_envio" id="tipo_envio" class="form-control input-sm">
                            <option value="1"> Por operación</option>
                            <option value="2"> Acumulado Semanal</option>
                        </select>
                    </div> <!-- /.form-group -->
                    <div class="">
                        <label>Fecha de Inicio (solo para tipo de envio: acumulado semanal)</label>
                        <input type="date" name="fecha_inicio" id="fecha_inicio" class="form-control input-sm" autocomplete="off" value="<?php echo date('Y-m-d');?>" >
                    </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_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_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_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>';
}
?>