<?php
    session_start();
    $usuarioSession = $_SESSION['USUARIO'];
    $grupo = $_SESSION['GRUPO'];
    $contacto = $_SESSION['CONTACTO'];
    $awa_session_id = $_SESSION['AWA_SESION_ID'];
    include('../includes/conex.php');
    include('../includes/eat.php');
    include('../includes/historial.php');
    include('../includes/functions.php');
    include('../includes/esquemas.php');
    include('../class/Schema.php');
    include('../class/MotherCar.php');
    include('../class/Static.php');
    if(!isset($usuarioSession) OR $awa_session_id != $awa_id) {
        eatCookies();
        header('Location:../index.php');
    }
    if(isset($_COOKIE['usuario']) AND $_COOKIE['usuario'] != $staticUsu ) {
        eatCookies();
        header('Loction:../index.php');
    }
    if (isset($_COOKIE['idioma'])) {
        include('../lg/LG_' . $_COOKIE['idioma'] . '.php');
    } else {
        include('../lg/LG_SP.php');
    }
    $Schema = new Schema($con, $database);

    $key = 'COMPRA_BASE = ?, COMPRA_TOTAL = ?';
    $type = 'ii';
    $array_value = [Statics::SafeInput($con, $_POST['compra_base']), Statics::SafeInput($con, $_POST['compra_total'])];
    if(!empty($_POST['procedencia'])) {
        $key .= ',PROCEDENCIA = ?';
        $type .= 'i';
        array_push($array_value, Statics::SafeInput($con, $_POST['procedencia']));
    }
    if(!empty($_POST['importacion'])) {
        $key .= ',IMPORTACION = ?';
        $type .= 'i';
        array_push($array_value, Statics::SafeInput($con, $_POST['importacion']));
    }
    if(!empty($_POST['rebu'])) {
        $key .= ',REBU = ?';
        $type .= 'i';
        array_push($array_value, Statics::SafeInput($con, $_POST['rebu']));
    }
    if(!empty($_POST['compra_iva'])) {
        $key .= ',COMPRA_TIPOIVA = ?';
        $type .= 'i';
        array_push($array_value, Statics::SafeInput($con, $_POST['compra_iva']));
    }
    $key .= ' WHERE CODIGO = ?';
    $type .= 'i';
    array_push($array_value, $_SESSION['VEHICULO_RECIEN_CREADO']);

    $Schema->executeQuery("UPDATE VEHICULOS SET $key", $type, $array_value);
    $_SESSION['MSG_INFO'] = 'DATOS_COMPRA';
    $detalle = $actualizacion_lg . ': ' . $comprar_lg;
    Statics::add_trace_history($con, $usuarioSession, $_SESSION['VEHICULO_RECIEN_CREADO'], 2, $detalle);
    header("Location:../nuevo-vehiculo-datos.php");
?>