<?php
/*-----------------------
Autor: Delmar Lopez
http://www.softwys.com
Fecha: 03-01-2018
Version de PHP: 7.0.0
----------------------------*/
include 'is_logged.php'; //Verifica que el usuario esté logueado
require_once "../db.php";
require_once "../php_conexion.php";

/* Validación */
if (empty($_POST['id_factura'])) {
    $errors[] = "ID vacío.";
} else {
    $id_factura = intval($_POST['id_factura']);

    // Eliminar primero detalle de la factura
    $del2 = "DELETE FROM detalle_fact_cot WHERE id_factura='$id_factura'";
    $delete2 = mysqli_query($conexion, $del2);

    // Luego eliminar la factura
    $del1 = "DELETE FROM facturas_cot WHERE id_factura='$id_factura'";
    $delete1 = mysqli_query($conexion, $del1);

    if ($delete1 && $delete2) {
        ?>
        <div class="alert alert-success alert-dismissible" role="alert">
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
            <strong>Aviso!</strong> Datos eliminados exitosamente.
        </div>
        <?php
    } else {
        ?>
        <div class="alert alert-danger alert-dismissible" role="alert">
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
            <strong>Error!</strong> No se pudo eliminar los datos.
        </div>
        <?php
    }
}

/* Mostrar errores */
if (isset($errors)) {
    ?>
    <div class="alert alert-danger" role="alert">
        <button type="button" class="close" data-dismiss="alert">&times;</button>
        <strong>Error!</strong>
        <?php
        foreach ($errors as $error) {
            echo $error;
        }
        ?>
    </div>
    <?php
}
?>