<?php
session_start();
$usuarioSession = isset($_SESSION['USUARIO']) ? $_SESSION['USUARIO'] : 'usuario_cron';
$grupo = isset($_SESSION['GRUPO']) ? $_SESSION['GRUPO'] : '-1';
$contacto = isset($_SESSION['CONTACTO']) ? $_SESSION['CONTACTO'] : '-1';
$awa_session_id = isset($_SESSION['AWA_SESION_ID']) ? $_SESSION['AWA_SESION_ID'] : '0000CRON000';
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/Expediente.php');
include('../class/Bibliotecas.php');
include('../class/Static.php');
include('../class/WebServices.php');
include('../lg/config_vo.php');
if(!isset($usuarioSession) OR $awa_session_id != $awa_id) {
    eatCookies();
    header('Location:../index.php');
}
if (isset($_COOKIE['idioma'])) {
    include('../lg/LG_' . $_COOKIE['idioma'] . '.php');
} else {
    include('../lg/LG_SP.php');
}

date_default_timezone_set('Europe/Madrid');

$Schema = new Schema($con);
$ws = new WebServices($con);
$Bibliotecas = new Bibliotecas($con);
$aucDDBB = $Bibliotecas->select_campo_tabla_parametros('BBDD_AUC', 'VALOR');
$res = $con->query('SELECT V.CODIGO AS VEHICULOS_CODIGO, P.*, PO.*
FROM VEHICULOS V
INNER JOIN VEHICULOS_PUBLICACIONES_REL P ON V.STOCK = P.STOCK
INNER JOIN VEHICULOS_PORTALES PO ON P.PORTAL = PO.CODIGO AND PO.ACTIVO = 1
WHERE V.ESTADO = "STK"
AND V.ACTIVO = 1');
if($res->num_rows) {
    $nr = 0;
    while($row = $res->fetch_array()) {
        $port = $row['PORTAL'];
        $limite = empty($row['LIMITE']) ? 99999999 : $row['LIMITE'];
        if($nr == $limite) {
            break;
        }
        if($aucDDBB == "0") {
            $url = $url_api_p0;
            $app_cod_ext = $Bibliotecas->select_campo_tabla_parametros('BBDD_ALIAS', 'VALOR');
            $dataMAIN = [
                'grant_type'        =>      'client_credentials',
                'client_secret'     =>      $client_secret_api_p0,
                'client_id'         =>      $client_id_api_p0,
                'service'           =>      'servToken',
                'code'              =>      '8356e8922cdccb82gh538fu2'
            ];
            $resultMAIN = $ws->curlPost($url, $dataMAIN, $headers);
            $headers = [
                'authorization-token: Bearer ' . $resultMAIN->servToken->client_token,
                'client-id: ' . $client_id_api_p0,
                'p0-api-key: re593876gfsAPteth324601zECE'
            ];
            $data = [
                "service"       =>      "servUndoPosts",
                "portal"        =>      $row['ALIAS'],
                "code_ext"      =>      $app_cod_ext
            ];
            $result = $ws->curlPost($url, $data, $headers);
        }
        $date = date('Y-m-d H:i:s');
        $Schema->executeQuery('UPDATE VEHICULOS_PUBLICACIONES SET FIN = ?, ACTIVO = ? WHERE PORTAL = ? AND VEHICULO = ?', 'siii', [date('Y-m-d H:i:s'), 0, $port, $row['VEHICULOS_CODIGO']]);
        $Schema->executeQuery('INSERT INTO VEHICULOS_PUBLICACIONES(VEHICULO, PORTAL, INICIO, ACTIVO) VALUES(?, ?, ?, ?)', 'iisi', [$row['VEHICULOS_CODIGO'], $port, $date, 1]);
        $nr++;
        
    }
}


?>



