<?php
    session_start();
    $usuarioSession = $_SESSION['USUARIO'];
    $grupo = $_SESSION['GRUPO'];
    $contacto = $_SESSION['CONTACTO'];
    $awa_session_id = $_SESSION['AWA_SESION_ID'];
    include('../lg/config_vo.php');
    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');
    include('../class/Agenda.php');
    include('../class/Rgpd.php');
    include('../class/Bibliotecas.php');
    include_once("../assets/excel/PHPExcel/IOFactory.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');
    }
    if (isset($_FILES['excel'])) {
        $Schema = new Schema($con);
        $MotherCar = new MotherCar($con);

        $html = '';
        
        $excel = PHPExcel_IOFactory::load($_FILES['excel']['tmp_name']);
        $excel->setActiveSheetIndex(0);
        $codigo_vehiculo = Statics::max_codigo($con, 'VEHICULOS');
        $MotherCar->set_estado('STK');
        $MotherCar->set_activo(0);

        $i = 2;
        while($excel->getActiveSheet()->getCell('A' . $i)->getValue()) {
            $marca_xlsx = $excel->getActiveSheet()->getCell('A' . $i)->getValue();
            $modelo_xlsx = $excel->getActiveSheet()->getCell('B' . $i)->getValue();
            $version_xlsx = $excel->getActiveSheet()->getCell('C' . $i)->getValue();
            $matricula_xlsx = str_replace(' ', '', $excel->getActiveSheet()->getCell('D' . $i)->getValue());
            $bastidor_xlsx = $excel->getActiveSheet()->getCell('E' . $i)->getValue();
            $notas_xlsx = $excel->getActiveSheet()->getCell('F' . $i)->getValue();
            $combustible_xlsx = $excel->getActiveSheet()->getCell('G' . $i)->getValue();
            $carroceria_xlsx = $excel->getActiveSheet()->getCell('H' . $i)->getValue();
            $km_xlsx = $excel->getActiveSheet()->getCell('I' . $i)->getValue();
            $cambio_xlsx = $excel->getActiveSheet()->getCell('I' . $i)->getValue();
            $matriculacion_xlsx = empty($excel->getActiveSheet()->getCell('K' . $i)->getValue()) ? NULL : date('Y-m-d', strtotime($excel->getActiveSheet()->getCell('K' . $i)->getValue()));
            $color_literal_xlsx = $excel->getActiveSheet()->getCell('L' . $i)->getValue();
            $venta_xlsx = $excel->getActiveSheet()->getCell('M' . $i)->getValue();
            $compra_xlsx = $excel->getActiveSheet()->getCell('N' . $i)->getValue();
            $stock_xlsx = $excel->getActiveSheet()->getCell('O' . $i)->getValue();

            $MotherCar->set_usuario_session($usuarioSession);
            $marca_codigo = $Schema->executeQuery('SELECT CODIGO FROM VEHICULOS_MARCAS WHERE DESCRIPCION = ?', 's', [$marca_xlsx])->fetch_array();
            $MotherCar->set_marca(empty($marca_codigo['CODIGO']) ? 0 : $marca_codigo['CODIGO']);
            $modelo_codigo = $Schema->executeQuery('SELECT CODIGO FROM VEHICULOS_MODELOS WHERE DESCRIPCION = ?', 's', [$modelo_xlsx])->fetch_array();
            $MotherCar->set_modelo(empty($modelo_codigo['CODIGO']) ? 0 : $modelo_codigo['CODIGO']);
            $MotherCar->set_version($version_xlsx);
            $MotherCar->set_matricula($matricula_xlsx);
            $MotherCar->set_bastidor($bastidor_xlsx);
            
            $MotherCar->set_notas($notas_xlsx);
            $combustible_codigo = $Schema->executeQuery("SELECT CODIGO FROM VEHICULOS_LITERALES WHERE DESCRIPCION_$lg = ?", 's', [$combustible_xlsx])->fetch_array();
            $MotherCar->set_combustible(empty($combustible_codigo['CODIGO']) ? 0 : $combustible_codigo['CODIGO']);
            $carroceria_codigo = $Schema->executeQuery("SELECT CODIGO FROM VEHICULOS_LITERALES WHERE DESCRIPCION_$lg = ?", 's', [$carroceria_xlsx])->fetch_array();
            $MotherCar->set_carroceria(empty($carroceria_codigo['CODIGO']) ? 0 : $carroceria_codigo['CODIGO']);
            $MotherCar->set_km($km_xlsx);
            $cambio_codigo = $Schema->executeQuery("SELECT CODIGO FROM VEHICULOS_LITERALES WHERE DESCRIPCION_$lg = ?", 's', [$cambio_xlsx])->fetch_array();
            $MotherCar->set_cajacambios(empty($cambio_codigo['CODIGO']) ? 0 : $cambio_codigo['CODIGO']);
            $MotherCar->set_matriculacion(empty($matriculacion_xlsx) ? NULL : $matriculacion_xlsx);
            $MotherCar->set_color_principal_literal($color_literal_xlsx);
            $MotherCar->set_venta_total_sin_dto($venta_xlsx);
            $MotherCar->set_venta_total($venta_xlsx);
            $MotherCar->set_precio_compra($compra_xlsx);
            $MotherCar->set_precio_compra_total($compra_xlsx);
            $MotherCar->set_stock($stock_xlsx);

            if(!empty($bastidor_xlsx)) {
                $matriculaForMostrar =  empty($matricula_xlsx) ? false : " - " . $matricula_xlsx;
                $res = $Schema->executeQuery('SELECT CODIGO, IDCAR FROM VEHICULOS WHERE BASTIDOR = ?', 's', [$bastidor_xlsx]);
                if($res->num_rows){
                    $html = 'UPDATE';
                    $row = $res->fetch_array();
                    $MotherCar->set_codigo_vehiculo_tabla_vehiculos($row['CODIGO']);
                    $MotherCar->set_id_car($row['IDCAR']);
                    $MotherCar->set_mostrar($row['IDCAR'] . " - " . $marca_xlsx . " - " . $modelo_xlsx . $matriculaForMostrar);
                    $MotherCar->update_vehiculo();
                } else {
                    $html = 'INSERT';
                    $idcar = Statics::get_contadores($con,'IDCAR0000000000000');
                    $MotherCar->set_codigo_vehiculo_tabla_vehiculos($codigo_vehiculo++);
                    $MotherCar->set_id_car($idcar);
                    $MotherCar->set_importacion_excel(1);
                    $MotherCar->set_mostrar($idcar . " - " . $marca_xlsx . " - " . $modelo_xlsx . $matriculaForMostrar);
                    $MotherCar->insert_vehiculo();
                }
            }
            $i++;
        }
    }
   
    header('Location:../ajustes.php');

?>