SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';

CREATE SCHEMA IF NOT EXISTS `truckcode` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ;
USE `truckcode`;
-- -----------------------------------------------------
-- Table `truckcode`.`archivos_etiquetas`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`archivos_etiquetas` (
    `id` INT(2) UNSIGNED NOT NULL AUTO_INCREMENT,
    `nombre` VARCHAR(50) NOT NULL ,
    PRIMARY KEY (`id`)
) 
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
PACK_KEYS = 0
ROW_FORMAT = DEFAULT;
-- -----------------------------------------------------
-- Table `truckcode`.`archivos`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`archivos` (
    `id` INT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
    `creacion` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `creador` INT(3) UNSIGNED NOT NULL,
    `etiqueta_id` INT(2) UNSIGNED NOT NULL,
    `nombre` VARCHAR(100) NOT NULL,
    `archivo` VARCHAR(100) NOT NULL,
    `formato` VARCHAR(20) NULL,
    `peso` DECIMAL(6,2) UNSIGNED NULL,
    PRIMARY KEY (`id`),
    INDEX `fk_archivos_usuarios` (`creador` ASC) ,
    INDEX `fk_archivos_etiquetas` (`etiqueta_id` ASC) ,
    CONSTRAINT `fk_archivos_usuarios`
        FOREIGN KEY (`creador` )
        REFERENCES `truckcode`.`sistema_usuarios` (`id` )
        ON DELETE RESTRICT
        ON UPDATE CASCADE,
    CONSTRAINT `fk_archivos_etiquetas`
        FOREIGN KEY (`etiqueta_id` )
        REFERENCES `truckcode`.`archivos_etiquetas` (`id` )
        ON DELETE CASCADE 
        ON UPDATE CASCADE
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`documentos_tipos`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`documentos_tipos` (
    `id` INT(2) UNSIGNED NOT NULL AUTO_INCREMENT,
    `clase` ENUM('CAMIONES', 'SEMIREMOLQUES', 'CONDUCTORES') NOT NULL,
    `nombre` VARCHAR(50) NOT NULL ,
    `indefinido` INT(1) UNSIGNED NOT NULL DEFAULT '0',
    `critico` INT(1) UNSIGNED NOT NULL DEFAULT '0',
    `dias_aviso` INT(3) UNSIGNED NOT NULL,
    `lugar` INT(3) UNSIGNED NOT NULL,
    PRIMARY KEY (`id`)
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
PACK_KEYS = 0
ROW_FORMAT = DEFAULT;
-- -----------------------------------------------------
-- Table `truckcode`.`documentos`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`documentos` (
    `id` INT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
    `creacion` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `creador` INT(3) UNSIGNED NOT NULL,
    `tipo_id` INT(2) UNSIGNED NOT NULL,
    `descripcion` VARCHAR(50) NULL,
    PRIMARY KEY (`id`),
    INDEX `fk_documentos_usuarios` (`creador` ASC) ,
    INDEX `fk_documentos_tipos` (`tipo_id` ASC) ,
    CONSTRAINT `fk_documentos_usuarios`
        FOREIGN KEY (`creador` )
        REFERENCES `truckcode`.`sistema_usuarios` (`id` )
        ON DELETE RESTRICT
        ON UPDATE CASCADE,
    CONSTRAINT `fk_documentos_tipos`
        FOREIGN KEY (`tipo_id` )
        REFERENCES `truckcode`.`documentos_tipos` (`id` )
        ON DELETE RESTRICT 
        ON UPDATE CASCADE    
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`documentos_emisiones`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`documentos_emisiones` (
    `id` INT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
    `creacion` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `creador` INT(3) UNSIGNED NOT NULL,
    `documento_id` INT(5) UNSIGNED NOT NULL,
    `folio` VARCHAR(50) NULL,
    `fecha_emision` DATE NOT NULL,
    `fecha_vencimiento` DATE NULL,
    PRIMARY KEY (`id`),
    INDEX `fk_documentos_emisiones_usuarios` (`creador` ASC) ,
    INDEX `fk_documentos_emisiones_documentos` (`documento_id` ASC) ,
    CONSTRAINT `fk_documentos_emisiones_usuarios`
        FOREIGN KEY (`creador` )
        REFERENCES `truckcode`.`sistema_usuarios` (`id` )
        ON DELETE RESTRICT
        ON UPDATE CASCADE,
    CONSTRAINT `fk_documentos_emisiones_documentos`
        FOREIGN KEY (`documento_id` )
        REFERENCES `truckcode`.`documentos` (`id` )
        ON DELETE CASCADE 
        ON UPDATE CASCADE
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`documentos_archivos`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`documentos_archivos` (
    `archivo_id` INT(5) UNSIGNED NOT NULL,
    `documento_id` INT(3) UNSIGNED NOT NULL,
    PRIMARY KEY (`archivo_id`, `documento_id`),
    INDEX `fk_documentos_archivos_archivos` (`archivo_id` ASC) ,
    INDEX `fk_documentos_archivos_documentos` (`documento_id` ASC) ,
    CONSTRAINT `fk_documentos_archivos_archivos`
        FOREIGN KEY (`archivo_id` )
        REFERENCES `truckcode`.`archivos` (`id` )
        ON DELETE CASCADE 
        ON UPDATE CASCADE,
    CONSTRAINT `fk_documentos_archivos_documentos`
        FOREIGN KEY (`documento_id` )
        REFERENCES `truckcode`.`documentos` (`id` )
        ON DELETE RESTRICT  
        ON UPDATE CASCADE    
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`marcas`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`marcas` (
    `id` INT(3) UNSIGNED NOT NULL AUTO_INCREMENT,
    `nombre` VARCHAR(50) NOT NULL ,
    `clase` ENUM('CAMIONES','SEMIREMOLQUES', 'NEUMATICOAS') NOT NULL,
    PRIMARY KEY (`id`)
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
PACK_KEYS = 0
ROW_FORMAT = DEFAULT;
-- -----------------------------------------------------
-- Table `truckcode`.`neumaticos_tipos`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`neumaticos_tipos` (
    `id` INT(2) UNSIGNED NOT NULL AUTO_INCREMENT,
    `nombre` VARCHAR(50) NOT NULL ,
    PRIMARY KEY (`id`)
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
PACK_KEYS = 0
ROW_FORMAT = DEFAULT;
-- -----------------------------------------------------
-- Table `truckcode`.`proveedores`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`proveedores` (
    `id` INT(3) UNSIGNED NOT NULL AUTO_INCREMENT,
    `rut` VARCHAR(9) NOT NULL ,
    `razon_social` VARCHAR(100) NOT NULL,
    `nombre_fantasia` VARCHAR(50) NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE `unique_rut_proveedor` (`rut` ASC)
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
PACK_KEYS = 0
ROW_FORMAT = DEFAULT;
-- -----------------------------------------------------
-- Table `truckcode`.`camiones_tipos`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`camiones_tipos` (
    `id` INT(2) UNSIGNED NOT NULL AUTO_INCREMENT,
    `nombre` VARCHAR(50) NOT NULL,
    PRIMARY KEY (`id`)
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`camiones_modelos`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`camiones_modelos` (
    `id` INT(3) UNSIGNED NOT NULL AUTO_INCREMENT,
    `creacion` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `marca_id` INT(3) UNSIGNED NOT NULL,
    `tipo_id` INT(2) UNSIGNED NOT NULL,
    `nombre` VARCHAR(50) NOT NULL ,
    `potencia` INT(4) UNSIGNED NOT NULL,
    `estanque_primario` INT(4) UNSIGNED NOT NULL,
    `estanque_secundario` INT(4) UNSIGNED NULL,
    `info_adicional` TEXT NULL,
    PRIMARY KEY (`id`),
    INDEX `fk_camiones_modelos_marcas` (`marca_id` ASC) ,
    INDEX `fk_camiones_modelos_tipos` (`tipo_id` ASC) ,
    CONSTRAINT `fk_camiones_modelos_marcas`
        FOREIGN KEY (`marca_id` )
        REFERENCES `truckcode`.`marcas` (`id`)
        ON DELETE RESTRICT 
        ON UPDATE CASCADE,
    CONSTRAINT `fk_camiones_modelos_tipos`
        FOREIGN KEY (`tipo_id` )
        REFERENCES `truckcode`.`camiones_tipos` (`id`)
        ON DELETE RESTRICT 
        ON UPDATE CASCADE
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
PACK_KEYS = 0
ROW_FORMAT = DEFAULT;
-- -----------------------------------------------------
-- Table `truckcode`.`camiones_modelos_partes`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`camiones_modelos_partes` (
    `id` INT(6) UNSIGNED NOT NULL AUTO_INCREMENT,
    `modelo_id` INT(3) UNSIGNED NOT NULL,
    `num_parte` VARCHAR(50) NULL,
    `descripcion` VARCHAR(200) NULL,
    `cantidad` INT(3) UNSIGNED NOT NULL,
    PRIMARY KEY (`id`),
    INDEX `fk_camiones_modelos_partes_modelos` (`modelo_id` ASC) ,
    CONSTRAINT `fk_camiones_modelos_partes_modelos`
        FOREIGN KEY (`modelo_id` )
        REFERENCES `truckcode`.`camiones_modelos` (`id` )
        ON DELETE CASCADE  
        ON UPDATE CASCADE 
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`camiones_modelos_archivos`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`camiones_modelos_archivos` (
    `archivo_id` INT(5) UNSIGNED NOT NULL,
    `modelo_id` INT(3) UNSIGNED NOT NULL,
    PRIMARY KEY (`archivo_id`, `modelo_id`),
    INDEX `fk_camiones_modelos_archivos_archivos` (`archivo_id` ASC) ,
    INDEX `fk_camiones_modelos_archivos_modelos` (`modelo_id` ASC) ,
    CONSTRAINT `fk_camiones_modelos_archivos_archivos`
        FOREIGN KEY (`archivo_id` )
        REFERENCES `truckcode`.`archivos` (`id` )
        ON DELETE CASCADE 
        ON UPDATE CASCADE,
    CONSTRAINT `fk_camiones_modelos_archivos_modelos`
        FOREIGN KEY (`modelo_id` )
        REFERENCES `truckcode`.`camiones_modelos` (`id` )
        ON DELETE RESTRICT   
        ON UPDATE CASCADE    
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`semiremolques_tipos`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`semiremolques_tipos` (
    `id` INT(2) UNSIGNED NOT NULL AUTO_INCREMENT,
    `nombre` VARCHAR(50) NOT NULL,
    PRIMARY KEY (`id`)
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`semiremolques_modelos`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`semiremolques_modelos` (
    `id` INT(3) UNSIGNED NOT NULL AUTO_INCREMENT,
    `creacion` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `marca_id` INT(3) UNSIGNED NOT NULL,
    `tipo_id` INT(2) UNSIGNED NOT NULL,
    `nombre` VARCHAR(50) NOT NULL ,
    `ejes_configuracion` VARCHAR(10) NOT NULL,
    `neumaticos_instalados` INT(2) UNSIGNED NOT NULL,
    `neumaticos_repuesto` INT(2) UNSIGNED NOT NULL,
    `neumaticos_tipo` INT(2) UNSIGNED NOT NULL,
    `frenos_tipo` VARCHAR(30) NOT NULL,
    `capacidad` VARCHAR(50) NOT NULL,
    `info_adicional` TEXT NULL,
    PRIMARY KEY (`id`),
    INDEX `fk_semiremolques_modelos_marcas` (`marca_id` ASC) ,
    INDEX `fk_semiremolques_modelos_neumaticos_tipos` (`neumaticos_tipo` ASC) ,
    CONSTRAINT `fk_semiremolques_modelos_marcas`
        FOREIGN KEY (`marca_id` )
        REFERENCES `truckcode`.`marcas` (`id`)
        ON DELETE RESTRICT 
        ON UPDATE CASCADE,
    CONSTRAINT `fk_semiremolques_modelos_tipos`
        FOREIGN KEY (`tipo_id` )
        REFERENCES `truckcode`.`semiremolques_tipos` (`id`)
        ON DELETE RESTRICT 
        ON UPDATE CASCADE,    
    CONSTRAINT `fk_semiremolques_modelos_neumaticos_tipos`
        FOREIGN KEY (`neumaticos_tipo` )
        REFERENCES `truckcode`.`neumaticos_tipos` (`id`)
        ON DELETE RESTRICT 
        ON UPDATE CASCADE
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
PACK_KEYS = 0
ROW_FORMAT = DEFAULT;
-- -----------------------------------------------------
-- Table `truckcode`.`semiremolques_modelos_partes`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`semiremolques_modelos_partes` (
    `id` INT(6) UNSIGNED NOT NULL AUTO_INCREMENT,
    `modelo_id` INT(5) UNSIGNED NOT NULL,
    `num_parte` VARCHAR(50) NULL,
    `descripcion` VARCHAR(200) NULL,
    `cantidad` INT(3) UNSIGNED NOT NULL,
    PRIMARY KEY (`id`),
    INDEX `fk_semiremolques_modelos_partes_modelos` (`modelo_id` ASC) ,
    CONSTRAINT `fk_semiremolques_modelos_partes_modelos`
        FOREIGN KEY (`modelo_id` )
        REFERENCES `truckcode`.`semiremolques_modelos` (`id` )
        ON DELETE CASCADE  
        ON UPDATE CASCADE 
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`semiremolques_modelos_archivos`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`semiremolques_modelos_archivos` (
    `archivo_id` INT(5) UNSIGNED NOT NULL,
    `modelo_id` INT(3) UNSIGNED NOT NULL,
    PRIMARY KEY (`archivo_id`, `modelo_id`),
    INDEX `fk_semiremolques_modelos_archivos_archivos` (`archivo_id` ASC) ,
    INDEX `fk_semiremolques_modelos_archivos_modelos` (`modelo_id` ASC) ,
    CONSTRAINT `fk_semiremolques_modelos_archivos_archivos`
        FOREIGN KEY (`archivo_id` )
        REFERENCES `truckcode`.`archivos` (`id` )
        ON DELETE CASCADE 
        ON UPDATE CASCADE,
    CONSTRAINT `fk_semiremolques_modelos_archivos_modelos`
        FOREIGN KEY (`modelo_id` )
        REFERENCES `truckcode`.`semiremolques_modelos` (`id` )
        ON DELETE RESTRICT   
        ON UPDATE CASCADE    
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`servicios`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`servicios` (
    `id` INT(3) UNSIGNED NOT NULL AUTO_INCREMENT,
    `nombre` VARCHAR(50) NOT NULL,
    `ocultar` INT(1) UNSIGNED NOT NULL DEFAULT '0',
    PRIMARY KEY (`id`)
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`planes`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`planes` (
    `id` INT(4) UNSIGNED NOT NULL AUTO_INCREMENT,
    `tipo` ENUM('CAMION', 'SEMIREMOLQUE') NOT NULL,
    `nombre` VARCHAR(50) NOT NULL,
    `observaciones` TEXT NULL,
    PRIMARY KEY (`id`)
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`planes_items`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`planes_items` (
    `id` INT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
    `plan_id` INT(4) UNSIGNED NOT NULL,
    `servicio_id` INT(3) UNSIGNED NOT NULL,
    `descripcion` VARCHAR(50) NULL,
    `cantidad_kms` INT UNSIGNED NULL DEFAULT NULL,
    `cantidad_dias` INT UNSIGNED NULL DEFAULT NULL,
    `dias_aviso` INT UNSIGNED NULL,
    PRIMARY KEY (`id`),
    INDEX `fk_planes_items_servicios` (`servicio_id` ASC) ,
    INDEX `fk_planes_items_planes` (`plan_id` ASC) ,
    CONSTRAINT `fk_planes_items_servicios`
        FOREIGN KEY (`servicio_id` )
        REFERENCES `truckcode`.`servicios` (`id`)
        ON DELETE RESTRICT 
        ON UPDATE CASCADE,
    CONSTRAINT `fk_planes_items_planes`
        FOREIGN KEY (`plan_id` )
        REFERENCES `truckcode`.`planes` (`id`)
        ON DELETE CASCADE 
        ON UPDATE CASCADE
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`empresas`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`empresas` (
    `id` INT(2) UNSIGNED NOT NULL AUTO_INCREMENT,
    `nombre` VARCHAR(100) NOT NULL,
    `codigo` VARCHAR(10) NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `unique_codigo_agencia` (`codigo` ASC)
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
PACK_KEYS = 0
ROW_FORMAT = DEFAULT;
-- -----------------------------------------------------
-- Table `truckcode`.`conceptos_imputacion`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`conceptos_imputacion` (
    `id` INT(2) UNSIGNED NOT NULL AUTO_INCREMENT,
    `nombre` VARCHAR(50) NOT NULL,
    `remuneraciones` INT(1) UNSIGNED NOT NULL DEFAULT 0,
    `rendiciones` INT(1) UNSIGNED NOT NULL DEFAULT 0,
    `mantenimientos` INT(1) UNSIGNED NOT NULL DEFAULT 0,
    PRIMARY KEY (`id`)
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
PACK_KEYS = 0
ROW_FORMAT = DEFAULT;
-- -----------------------------------------------------
-- Table `truckcode`.`centros_costo`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`centros_costo` (
    `id` INT(6) UNSIGNED NOT NULL,
    `nombre` VARCHAR(50) NULL,
    PRIMARY KEY (`id`)
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
PACK_KEYS = 0
ROW_FORMAT = DEFAULT;
-- -----------------------------------------------------
-- Table `truckcode`.`agencias`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`agencias` (
    `id` INT(6) UNSIGNED NOT NULL,
    `centro_id` INT(6) UNSIGNED NOT NULL, 
    `nombre` VARCHAR(50) NOT NULL,
    `codigo` VARCHAR(10) NOT NULL,
    PRIMARY KEY (`id`),
    INDEX `fk_agencias_centros_costo` (`centro_id` ASC),
    CONSTRAINT `fk_agencias_centros_costo`
        FOREIGN KEY (`centro_id` )
        REFERENCES `truckcode`.`centros_costo` (`id` )
        ON DELETE RESTRICT
        ON UPDATE CASCADE,
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
PACK_KEYS = 0
ROW_FORMAT = DEFAULT;
-- -----------------------------------------------------
-- Table `truckcode`.`camiones`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`camiones` (
    `id` INT(3) UNSIGNED NOT NULL AUTO_INCREMENT,
    `creacion` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `creador` INT(3) UNSIGNED NOT NULL,
    `empresa_id` INT(2) UNSIGNED NOT NULL,
    `ubicacion_id` INT(3) UNSIGNED NULL,
    `asignacion_conductor_id` INT(4) UNSIGNED NULL,
    `asignacion_semiremolque_id` INT(4) UNSIGNED NULL,
    `patente` VARCHAR(6) NOT NULL,
    `modelo_id` INT(3) UNSIGNED NOT NULL,
    `year` YEAR NOT NULL,
    `neumaticos_config` VARCHAR(20) NOT NULL DEFAULT '4x2',
    `neumaticos_instalados` INT(2) UNSIGNED NOT NULL,
    `neumaticos_repuesto` INT(2) UNSIGNED NOT NULL,
    `neumaticos_traccion` INT(2) UNSIGNED NOT NULL,
    `neumaticos_direccion` INT(2) UNSIGNED NOT NULL,
    `neumaticos_rodado_libre` INT(2) UNSIGNED NOT NULL,
    `neumaticos_tipo_direccion` INT(2) UNSIGNED NOT NULL,
    `neumaticos_tipo_traccion` INT(2) UNSIGNED NOT NULL,
    `color` VARCHAR(20) NOT NULL,
    `fotografia` VARCHAR(200) NULL,
    `accesorios` TEXT NULL,
    `plan_id` INT(4) UNSIGNED NULL,
    `inicio_operaciones` DATE NOT NULL,
    `odometro` INT(1) UNSIGNED NOT NULL,
    `promedio_calculado` INT UNSIGNED NULL DEFAULT NULL,
    `promedio_asignado` INT UNSIGNED NULL DEFAULT NULL,
    `promedio_usado` ENUM('C','A') DEFAULT 'C',
    `estado` ENUM('OPERATIVO', 'SINIESTRADO', 'VENDIDO') NOT NULL DEFAULT 'OPERATIVO',
    PRIMARY KEY (`id`),
    INDEX `fk_camiones_usuarios` (`creador` ASC) ,
    INDEX `fk_camiones_empresas` (`empresa_id` ASC) ,
    INDEX `fk_camiones_ubicaciones` (`ubicacion_id` ASC) ,
    INDEX `fk_camiones_asignaciones_conductores` (`asignacion_conductor_id` ASC) ,
    INDEX `fk_camiones_asignaciones_semiremolques` (`asignacion_semiremolque_id` ASC) ,
    INDEX `fk_camiones_modelos` (`modelo_id` ASC) ,
    INDEX `fk_camiones_neumaticos_direccion` (`neumaticos_tipo_direccion` ASC) ,
    INDEX `fk_camiones_neumaticos_traccion` (`neumaticos_tipo_traccion` ASC) ,
    INDEX `fk_camiones_planes` (`plan_id` ASC) ,
    CONSTRAINT `fk_camiones_usuarios`
        FOREIGN KEY (`creador` )
        REFERENCES `truckcode`.`sistema_usuarios` (`id` )
        ON DELETE RESTRICT
        ON UPDATE CASCADE,
    CONSTRAINT `fk_camiones_empresas`
        FOREIGN KEY (`empresa_id` )
        REFERENCES `truckcode`.`empresas` (`id` )
        ON DELETE RESTRICT
        ON UPDATE CASCADE,   
    CONSTRAINT `fk_camiones_ubicaciones`
        FOREIGN KEY (`ubicacion_id` )
        REFERENCES `truckcode`.`camiones_ubicaciones` (`id` )
        ON DELETE RESTRICT
        ON UPDATE CASCADE,  
    CONSTRAINT `fk_camiones_asignaciones_conductores`
        FOREIGN KEY (`asignacion_conductor_id` )
        REFERENCES `truckcode`.`camiones_asignaciones_conductores` (`id` )
        ON DELETE SET NULL
        ON UPDATE CASCADE,  
    CONSTRAINT `fk_camiones_asignaciones_semiremolques`
        FOREIGN KEY (`asignacion_semiremolque_id` )
        REFERENCES `truckcode`.`camiones_asignaciones_semiremolques` (`id` )
        ON DELETE SET NULL 
        ON UPDATE CASCADE,      
    CONSTRAINT `fk_camiones_modelos`
        FOREIGN KEY (`modelo_id` )
        REFERENCES `truckcode`.`camiones_modelos` (`id` )
        ON DELETE RESTRICT
        ON UPDATE CASCADE,
    CONSTRAINT `fk_camiones_neumaticos_direccion`
        FOREIGN KEY (`neumaticos_tipo_direccion` )
        REFERENCES `truckcode`.`neumaticos_tipos` (`id`)
        ON DELETE RESTRICT 
        ON UPDATE CASCADE,
    CONSTRAINT `fk_camiones_neumaticos_traccion`
        FOREIGN KEY (`neumaticos_tipo_traccion` )
        REFERENCES `truckcode`.`neumaticos_tipos_traccion` (`id`)
        ON DELETE RESTRICT 
        ON UPDATE CASCADE,
    CONSTRAINT `fk_camiones_planes`
        FOREIGN KEY (`plan_id` )
        REFERENCES `truckcode`.`planes` (`id` )
        ON DELETE RESTRICT
        ON UPDATE CASCADE    
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`camiones_ubicaciones`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`camiones_ubicaciones` (
    `id` INT(3) UNSIGNED NOT NULL AUTO_INCREMENT,
    `creacion` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `creador` INT(3) UNSIGNED NOT NULL,
    `camion_id` INT(3) UNSIGNED NOT NULL,
    `agencia_id` INT(5) UNSIGNED NOT NULL,
    `fecha_entrada` DATE NOT NULL,
    `fecha_salida` DATE NULL,
    `observaciones` TEXT NULL,
    PRIMARY KEY (`id`),
    INDEX `fk_camiones_ubicaciones_usuarios` (`creador` ASC) ,
    INDEX `fk_camiones_ubicaciones_camiones` (`camion_id` ASC) ,
    INDEX `fk_camiones_ubicaciones_agencias` (`agencia_id` ASC) ,
    CONSTRAINT `fk_camiones_ubicaciones_usuarios`
        FOREIGN KEY (`creador` )
        REFERENCES `truckcode`.`sistema_usuarios` (`id` )
        ON DELETE RESTRICT 
        ON UPDATE CASCADE,
    CONSTRAINT `fk_camiones_ubicaciones_camiones`
        FOREIGN KEY (`camion_id` )
        REFERENCES `truckcode`.`camiones` (`id` )
        ON DELETE CASCADE    
        ON UPDATE CASCADE,
    CONSTRAINT `fk_camiones_ubicaciones_agencias`
        FOREIGN KEY (`agencia_id` )
        REFERENCES `truckcode`.`agencias` (`id` )
        ON DELETE RESTRICT     
        ON UPDATE CASCADE    
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`camiones_documentos`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`camiones_documentos` (
    `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
    `documento_id` INT(5) UNSIGNED NOT NULL,
    `camion_id` INT(3) UNSIGNED NOT NULL,
    PRIMARY KEY (`id`),
    INDEX `fk_camiones_documentos_documentos` (`documento_id` ASC) ,
    INDEX `fk_camiones_documentos_camiones` (`camion_id` ASC) ,
    UNIQUE `unique_documento_camion` (`documento_id`, `camion_id` ASC),
    CONSTRAINT `fk_camiones_documentos_documentos`
        FOREIGN KEY (`documento_id` )
        REFERENCES `truckcode`.`documentos` (`id` )
        ON DELETE CASCADE 
        ON UPDATE CASCADE,
    CONSTRAINT `fk_camiones_documentos_camiones`
        FOREIGN KEY (`camion_id` )
        REFERENCES `truckcode`.`camiones` (`id` )
        ON DELETE RESTRICT   
        ON UPDATE CASCADE    
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`camiones_archivos`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`camiones_archivos` (
    `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
    `archivo_id` INT(5) UNSIGNED NOT NULL,
    `camion_id` INT(3) UNSIGNED NOT NULL,
    PRIMARY KEY (`id`),
    INDEX `fk_camiones_archivos_archivos` (`archivo_id` ASC) ,
    INDEX `fk_camiones_archivos_camiones` (`camion_id` ASC) ,
    UNIQUE `unique_archivo_camion` (`archivo_id`, `camion_id` ASC),
    CONSTRAINT `fk_camiones_archivos_archivos`
        FOREIGN KEY (`archivo_id` )
        REFERENCES `truckcode`.`archivos` (`id` )
        ON DELETE CASCADE 
        ON UPDATE CASCADE,
    CONSTRAINT `fk_camiones_archivos_camiones`
        FOREIGN KEY (`camion_id` )
        REFERENCES `truckcode`.`camiones` (`id` )
        ON DELETE RESTRICT  
        ON UPDATE CASCADE    
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`combustibles`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`combustibles` (
    `id` INT(2) UNSIGNED NOT NULL AUTO_INCREMENT,
    `nombre` VARCHAR(50) NOT NULL ,
    PRIMARY KEY (`id`)
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
PACK_KEYS = 0
ROW_FORMAT = DEFAULT;
-- -----------------------------------------------------
-- Table `truckcode`.`combustibles_impuestos`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`combustibles_impuestos` (
    `id` INT(2) UNSIGNED NOT NULL AUTO_INCREMENT,
    `nombre` VARCHAR(50) NOT NULL,
    PRIMARY KEY (`id`)
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`valores_impuestos_combustibles`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`valores_impuestos_combustibles` (
    `id` INT(6) UNSIGNED NOT NULL AUTO_INCREMENT,
    `creacion` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `creador` INT(3) UNSIGNED NOT NULL,
    `combustible_id` INT(2) UNSIGNED NOT NULL,
    `impuesto_id` INT(2) UNSIGNED NOT NULL,
    `porcentaje_fijo` DECIMAL(4,2) UNSIGNED NOT NULL,
    `porcentaje_variable` DECIMAL(4,2) UNSIGNED NOT NULL,
    `vigencia_desde` DATE NOT NULL,
    PRIMARY KEY (`id`),
    INDEX `fk_valores_impuestos_combustibles_usuarios` (`creador` ASC) ,
    INDEX `fk_valores_impuestos_combustibles_combustibles` (`combustible_id` ASC) ,
    INDEX `fk_valores_impuestos_combustibles_impuestos` (`impuesto_id` ASC) ,
    CONSTRAINT `fk_valores_impuestos_combustibles_usuarios`
        FOREIGN KEY (`creador` )
        REFERENCES `truckcode`.`sistema_usuarios` (`id` )
        ON DELETE RESTRICT
        ON UPDATE CASCADE,
    CONSTRAINT `fk_valores_impuestos_combustibles_combustibles`
        FOREIGN KEY (`combustible_id` )
        REFERENCES `truckcode`.`combustibles` (`id` )
        ON DELETE RESTRICT
        ON UPDATE CASCADE,
    CONSTRAINT `fk_valores_impuestos_combustibles_impuestos`
        FOREIGN KEY (`impuesto_id` )
        REFERENCES `truckcode`.`combustibles_impuestos` (`id` )
        ON DELETE RESTRICT
        ON UPDATE CASCADE    
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`valores_utm`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`valores_utm` (
    `id` INT(3) UNSIGNED NOT NULL AUTO_INCREMENT,
    `year` YEAR UNSIGNED NOT NULL,
    `month` INT(2) UNSIGNED ZEROFILL NOT NULL,
    `valor` INT UNSIGNED NOT NULL ,
    PRIMARY KEY (`id`),
    UNIQUE `unique_valor_utm` (`year`,`month`)
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
PACK_KEYS = 0
ROW_FORMAT = DEFAULT;
-- -----------------------------------------------------
-- Table `truckcode`.`camiones_cargas_combustible`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`camiones_cargas_combustible` (
    `id` INT(6) UNSIGNED NOT NULL AUTO_INCREMENT,
    `creacion` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `creador` INT(3) UNSIGNED NOT NULL,
    `camion_id` INT(5) UNSIGNED NULL,
    `centro_id` INT(6) UNSIGNED NOT NULL,
    `proveedor_id` INT(3) UNSIGNED NOT NULL,
    `combustible_id` INT(2) UNSIGNED NOT NULL,
    `num_guia` INT UNSIGNED NOT NULL,
    `num_factura` INT UNSIGNED NULL,
    `fecha` DATE NOT NULL,
    `locacion` VARCHAR(100) NULL,
    `cant_litros` DECIMAL(6,2) UNSIGNED NOT NULL,
    `val_litro` DECIMAL(5,2) UNSIGNED NOT NULL,
    `val_total` DECIMAL(10,2) UNSIGNED NOT NULL,
    `val_combustible` DECIMAL(10,2) UNSIGNED NULL,
    `val_impuesto_esp` DECIMAL(10,2) UNSIGNED NULL,
    `val_iva` DECIMAL(10,2) UNSIGNED NULL,
    PRIMARY KEY (`id`),
    INDEX `fk_camiones_cargas_combustibles_usuarios` (`creador` ASC) ,
    INDEX `fk_camiones_cargas_combustibles_camiones` (`camion_id` ASC) ,
    INDEX `fk_camiones_cargas_combustibles_centros_costo` (`centro_id` ASC) ,
    INDEX `fk_camiones_cargas_combustibles_proveedores` (`proveedor_id` ASC) ,
    INDEX `fk_camiones_cargas_combustibles_combustibles` (`combustible_id` ASC) ,
    CONSTRAINT `fk_camiones_cargas_combustibles_usuarios`
        FOREIGN KEY (`creador` )
        REFERENCES `truckcode`.`sistema_usuarios` (`id` )
        ON DELETE RESTRICT
        ON UPDATE CASCADE,
    CONSTRAINT `fk_camiones_cargas_combustibles_camiones`
        FOREIGN KEY (`camion_id` )
        REFERENCES `truckcode`.`camiones` (`id` )
        ON DELETE RESTRICT  
        ON UPDATE CASCADE,
    CONSTRAINT `fk_camiones_cargas_combustibles_centros_costo`
        FOREIGN KEY (`centro_id` )
        REFERENCES `truckcode`.`centros_costo` (`id` )
        ON DELETE RESTRICT  
        ON UPDATE CASCADE,    
    CONSTRAINT `fk_camiones_cargas_combustibles_proveedores`
        FOREIGN KEY (`proveedor_id` )
        REFERENCES `truckcode`.`proveedores` (`id` )
        ON DELETE RESTRICT  
        ON UPDATE CASCADE,
    CONSTRAINT `fk_camiones_cargas_combustibles_combustibles`
        FOREIGN KEY (`combustible_id` )
        REFERENCES `truckcode`.`combustibles` (`id` )
        ON DELETE RESTRICT  
        ON UPDATE CASCADE    
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`servicios_tipos` : preventivo, reactivo, etc.
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`servicios_tipos` (
    `id` INT(2) UNSIGNED NOT NULL AUTO_INCREMENT,
    `nombre` VARCHAR(50) NOT NULL ,
    PRIMARY KEY (`id`)
)
ENGINE = InnoDB 
DEFAULT CHARACTER SET = utf8
PACK_KEYS = 0
ROW_FORMAT = DEFAULT;
-- -----------------------------------------------------
-- Table `truckcode`.`camiones_lecturas_odometros`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`camiones_lecturas_odometros` (
    `id` INT(6) UNSIGNED NOT NULL AUTO_INCREMENT,
    `camion_id` INT(5) UNSIGNED NOT NULL,
    `fecha` DATE NOT NULL,
    `lectura` INT UNSIGNED NOT NULL,
    `ingreso_tipo` ENUM('MANUAL', 'MASIVO') NOT NULL DEFAULT 'MASIVO',
    `ingreso_archivo` VARCHAR(20) NULL,
    `estado` INT(1) UNSIGNED NOT NULL DEFAULT '0',
    PRIMARY KEY (`id`),
    INDEX `fk_camiones_lecturas_odometros_camiones` (`camion_id` ASC) ,
    CONSTRAINT `fk_camiones_lecturas_odometros_camiones`
        FOREIGN KEY (`camion_id` )
        REFERENCES `truckcode`.`camiones` (`id` )
        ON DELETE RESTRICT  
        ON UPDATE CASCADE  
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`semiremolques`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `truckcode`.`semiremolques` (
    `id` INT(3) UNSIGNED NOT NULL AUTO_INCREMENT,
    `creacion` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `creador` INT(3) UNSIGNED NOT NULL,
    `patente` VARCHAR(6) NOT NULL,
    `modelo_id` INT(3) UNSIGNED NOT NULL,
    `year` YEAR NOT NULL,
    `color` VARCHAR(20) NULL,
    `fotografia` VARCHAR(200) NULL,
    `modificaciones` TEXT NULL,
    `plan_id` INT(4) UNSIGNED NULL,
    `inicio_operaciones` DATE NOT NULL,
    `promedio_calculado` INT UNSIGNED NULL DEFAULT NULL,
    `promedio_asignado` INT UNSIGNED NULL DEFAULT NULL,
    `promedio_usado` ENUM('C','A') DEFAULT 'C',
    `estado` ENUM('OPERATIVO', 'SINIESTRADO', 'VENDIDO') NOT NULL DEFAULT 'OPERATIVO',
    PRIMARY KEY (`id`),
    INDEX `fk_semiremolques_usuarios` (`creador` ASC) ,
    INDEX `fk_semiremolques_modelos` (`modelo_id` ASC) ,
    INDEX `fk_semiremolques_planes` (`plan_id` ASC) ,
    UNIQUE `placa_patente` (`patente` ASC),
    CONSTRAINT `fk_semiremolques_usuarios`
        FOREIGN KEY (`creador` )
        REFERENCES `truckcode`.`sistema_usuarios` (`id` )
        ON DELETE RESTRICT
        ON UPDATE CASCADE,
    CONSTRAINT `fk_semiremolques_modelos`
        FOREIGN KEY (`modelo_id` )
        REFERENCES `truckcode`.`semiremolques_modelos` (`id` )
        ON DELETE RESTRICT
        ON UPDATE CASCADE,
    CONSTRAINT `fk_semiremolques_planes`
        FOREIGN KEY (`plan_id` )
        REFERENCES `truckcode`.`planes` (`id` )
        ON DELETE RESTRICT
        ON UPDATE CASCADE     
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`camiones_asignaciones_semiremolques`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`camiones_asignaciones_semiremolques` (
    `id` INT(3) UNSIGNED NOT NULL AUTO_INCREMENT,
    `creacion` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `creador` INT(3) UNSIGNED NOT NULL,
    `semiremolque_id` INT(3) UNSIGNED NOT NULL,
    `camion_id` INT(3) UNSIGNED NOT NULL,
    `fecha_inicio` DATE NOT NULL,
    `fecha_termino` DATE NULL,
    `observaciones` TEXT NULL,
    PRIMARY KEY (`id`),
    INDEX `fk_camiones_asignaciones_semiremolques_usuarios` (`creador` ASC) ,
    INDEX `fk_camiones_asignaciones_semiremolques_semiremolques` (`semiremolque_id` ASC) ,
    INDEX `fk_camiones_asignaciones_semiremolques_camiones` (`camion_id` ASC) ,
    CONSTRAINT `fk_camiones_asignaciones_semiremolques_usuarios`
        FOREIGN KEY (`creador` )
        REFERENCES `truckcode`.`sistema_usuarios` (`id` )
        ON DELETE RESTRICT 
        ON UPDATE CASCADE,
    CONSTRAINT `fk_camiones_asignaciones_semiremolques_semiremolques`
        FOREIGN KEY (`semiremolque_id` )
        REFERENCES `truckcode`.`semiremolques` (`id` )
        ON DELETE RESTRICT     
        ON UPDATE CASCADE,
    CONSTRAINT `fk_camiones_asignaciones_semiremolques_camiones`
        FOREIGN KEY (`camion_id` )
        REFERENCES `truckcode`.`camiones` (`id` )
        ON DELETE CASCADE     
        ON UPDATE CASCADE    
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`semiremolques_documentos`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`semiremolques_documentos` (
    `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
    `documento_id` INT(5) UNSIGNED NOT NULL,
    `semiremolque_id` INT(3) UNSIGNED NOT NULL,
    PRIMARY KEY (`id`),
    INDEX `fk_semiremolques_documentos_documentos` (`documento_id` ASC) ,
    INDEX `fk_semiremolques_documentos_semiremolques` (`semiremolque_id` ASC) ,
    UNIQUE  `unique_documento_semiremolque` (`documento_id`, `semiremolque_id` ASC),
    CONSTRAINT `fk_semiremolques_documentos_documentos`
        FOREIGN KEY (`documento_id` )
        REFERENCES `truckcode`.`documentos` (`id` )
        ON DELETE CASCADE 
        ON UPDATE CASCADE,
    CONSTRAINT `fk_semiremolques_documentos_semiremolques`
        FOREIGN KEY (`semiremolque_id` )
        REFERENCES `truckcode`.`semiremolques` (`id` )
        ON DELETE RESTRICT   
        ON UPDATE CASCADE    
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`semiremolques_archivos`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`semiremolques_archivos` (
    `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
    `archivo_id` INT(5) UNSIGNED NOT NULL,
    `semiremolque_id` INT(3) UNSIGNED NOT NULL,
    PRIMARY KEY (`id`),
    INDEX `fk_semiremolques_archivos_archivos` (`archivo_id` ASC) ,
    INDEX `fk_semiremolques_archivos_semiremolques` (`semiremolque_id` ASC) ,
    UNIQUE `unique_archivo_semiremolque` (`archivo_id`, `semiremolque_id` ASC),
    CONSTRAINT `fk_semiremolque_archivos_archivos`
        FOREIGN KEY (`archivo_id` )
        REFERENCES `truckcode`.`archivos` (`id` )
        ON DELETE CASCADE 
        ON UPDATE CASCADE,
    CONSTRAINT `fk_semiremolques_archivos_semiremolques`
        FOREIGN KEY (`semiremolque_id` )
        REFERENCES `truckcode`.`semiremolques` (`id` )
        ON DELETE RESTRICT   
        ON UPDATE CASCADE    
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`semiremolques_lecturas_odometros`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`semiremolques_lecturas_odometros` (
    `id` INT(6) UNSIGNED NOT NULL AUTO_INCREMENT,
    `semiremolque_id` INT(5) UNSIGNED NOT NULL,
    `fecha` DATE NOT NULL,
    `lectura` INT UNSIGNED NOT NULL,
    `estado` INT(1) UNSIGNED NOT NULL DEFAULT '0',
    PRIMARY KEY (`id`),
    INDEX `fk_semiremolques_lecturas_odometros_semiremolques` (`semiremolque_id` ASC) ,
    CONSTRAINT `fk_semiremolques_lecturas_odometros_semiremolques`
        FOREIGN KEY (`semiremolque_id` )
        REFERENCES `truckcode`.`semiremolques` (`id` )
        ON DELETE RESTRICT  
        ON UPDATE CASCADE
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`clientes`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`clientes` (
    `id` INT(3) UNSIGNED NOT NULL AUTO_INCREMENT,
    `rut` VARCHAR(9) NOT NULL ,
    `razon_social` VARCHAR(100) NOT NULL,
    `nombre_fantasia` VARCHAR(50) NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE `unique_rut_cliente` (`rut` ASC)
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
PACK_KEYS = 0
ROW_FORMAT = DEFAULT;
-- -----------------------------------------------------
-- Table `truckcode`.`rutas`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`rutas` (
    `id` INT(3) UNSIGNED NOT NULL AUTO_INCREMENT,
    `nombre` VARCHAR(30) NOT NULL ,
    `kilometros` INT(4) UNSIGNED NOT NULL,
    PRIMARY KEY (`id`)
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
PACK_KEYS = 0
ROW_FORMAT = DEFAULT;
-- -----------------------------------------------------
-- Table `truckcode`.`clientes_viajes`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`clientes_viajes` (
    `id` INT(6) UNSIGNED NOT NULL AUTO_INCREMENT,
    `cliente_id` INT(3) UNSIGNED NULL,
    `ruta_id` INT(3) UNSIGNED NULL,
    `camion_id` INT(3) UNSIGNED NOT NULL,
    `centro_id` INT(6) UNSIGNED NOT NULL,
    `monto_neto` INT UNSIGNED NOT NULL,
    `monto_extra` INT UNSIGNED NOT NULL,
    `monto_seguro` INT UNSIGNED NOT NULL,
    `numero_guia` INT UNSIGNED NOT NULL,
    `numero_orden_servicio` INT UNSIGNED NOT NULL,
    `fecha` DATE NOT NULL,
    `hora` VARCHAR(5) NOT NULL,
    `referencia_ruta` VARCHAR(100) NULL,
    `referencia_cliente` VARCHAR(100) NULL,
    `estado` ENUM('CUMPLIDO', 'CANCELADO') NOT NULL,
    PRIMARY KEY (`id`),
    INDEX `fk_clientes_viajes_clientes` (`cliente_id` ASC) ,
    INDEX `fk_clientes_viajes_rutas` (`ruta_id` ASC) ,
    INDEX `fk_clientes_viajes_camiones` (`camion_id` ASC) ,
    INDEX `fk_clientes_viajes_centros_costo` (`centro_id` ASC) ,
    CONSTRAINT `fk_clientes_viajes_clientes`
        FOREIGN KEY (`cliente_id` )
        REFERENCES `truckcode`.`clientes` (`id` )
        ON DELETE RESTRICT   
        ON UPDATE CASCADE,
    CONSTRAINT `fk_clientes_viajes_rutas`
        FOREIGN KEY (`ruta_id` )
        REFERENCES `truckcode`.`rutas` (`id` )
        ON DELETE RESTRICT  
        ON UPDATE CASCADE,
    CONSTRAINT `fk_clientes_viajes_camiones`
        FOREIGN KEY (`camion_id` )
        REFERENCES `truckcode`.`camiones` (`id` )
        ON DELETE RESTRICT  
        ON UPDATE CASCADE,
    CONSTRAINT `fk_clientes_viajes_centros_costo`
        FOREIGN KEY (`centro_id` )
        REFERENCES `truckcode`.`centros_costo` (`id` )
        ON DELETE RESTRICT  
        ON UPDATE CASCADE    
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`conductores`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`conductores` (
    `id` INT(3) UNSIGNED NOT NULL AUTO_INCREMENT,
    `creacion` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `creador` INT(3) UNSIGNED NOT NULL,
    `asignacion_id` INT(3) UNSIGNED NULL,
    `doc_identidad` VARCHAR(12) NULL,
    `fecha_nacimiento` DATE NOT NULL,
    `nacionalidad` VARCHAR(50) NOT NULL,
    `nombres` VARCHAR(50) NOT NULL,
    `apellido_paterno` VARCHAR(30) NOT NULL,
    `apellido_materno` VARCHAR(30) NOT NULL,
    `telefono` VARCHAR(30) NULL,
    `direccion` VARCHAR(100) NULL,
    `email` VARCHAR(50) NULL,
    `licencias` TEXT NULL,
    `fotografia` VARCHAR(200) NULL,
    `estado` ENUM('ACTIVO', 'FINIQUITADO') NOT NULL DEFAULT 'ACTIVO',
    PRIMARY KEY (`id`),
    INDEX `fk_conductores_usuarios` (`creador` ASC) ,
    INDEX `fk_conductores_asignaciones` (`asignacion_id` ASC) ,
    UNIQUE `documento_identidad_conductores` (`doc_identidad`ASC) ,
    CONSTRAINT `fk_conductores_usuarios`
        FOREIGN KEY (`creador` )
        REFERENCES `truckcode`.`sistema_usuarios` (`id` )
        ON DELETE RESTRICT
        ON UPDATE CASCADE,
    CONSTRAINT `fk_conductores_asignaciones`
        FOREIGN KEY (`asignacion_id` )
        REFERENCES `truckcode`.`conductores_asignaciones` (`id` )
        ON DELETE RESTRICT
        ON UPDATE CASCADE    
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`conductores_remuneraciones`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`conductores_remuneraciones` (
    `id` INT(3) UNSIGNED NOT NULL AUTO_INCREMENT,
    `creacion` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `creador` INT(3) UNSIGNED NOT NULL,
    `conductor_id` INT(3) UNSIGNED NOT NULL,
    `camion_id` INT(3) UNSIGNED NULL,
    `centro_id` INT(6) UNSIGNED NULL,
    `fecha` DATE NOT NULL,
    `total_bruto` INT NOT NULL,
    `observaciones` TEXT NULL,
    PRIMARY KEY (`id`),
    INDEX `fk_conductores_remuneraciones_usuarios` (`creador` ASC) ,
    INDEX `fk_conductores_remuneraciones_conductores` (`conductor_id` ASC) ,
    INDEX `fk_conductores_remuneraciones_camiones` (`camion_id` ASC) ,
    INDEX `fk_conductores_remuneraciones_centros_costo` (`centro_id` ASC) ,
    CONSTRAINT `fk_conductores_remuneraciones_usuarios`
        FOREIGN KEY (`creador` )
        REFERENCES `truckcode`.`sistema_usuarios` (`id` )
        ON DELETE RESTRICT 
        ON UPDATE CASCADE,
    CONSTRAINT `fk_conductores_remuneraciones_conductores`
        FOREIGN KEY (`conductor_id` )
        REFERENCES `truckcode`.`conductores` (`id` )
        ON DELETE RESTRICT     
        ON UPDATE CASCADE,
    CONSTRAINT `fk_conductores_remuneraciones_camiones`
        FOREIGN KEY (`camion_id` )
        REFERENCES `truckcode`.`camiones` (`id` )
        ON DELETE RESTRICT     
        ON UPDATE CASCADE,
    CONSTRAINT `fk_conductores_remuneraciones_centros_costo`
        FOREIGN KEY (`centro_id` )
        REFERENCES `truckcode`.`centros_costo` (`id` )
        ON DELETE RESTRICT     
        ON UPDATE CASCADE    
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`conductores_asignaciones`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`conductores_asignaciones` (
    `id` INT(3) UNSIGNED NOT NULL AUTO_INCREMENT,
    `creacion` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `creador` INT(3) UNSIGNED NOT NULL,
    `conductor_id` INT(3) UNSIGNED NOT NULL,
    `camion_id` INT(3) UNSIGNED NOT NULL,
    `fecha_inicio` DATE NOT NULL,
    `fecha_termino` DATE NULL,
    `observaciones` TEXT NULL,
    PRIMARY KEY (`id`),
    INDEX `fk_conductores_asignaciones_usuarios` (`creador` ASC) ,
    INDEX `fk_conductores_asignaciones_conductores` (`conductor_id` ASC) ,
    INDEX `fk_conductores_asignaciones_camiones` (`camion_id` ASC) ,
    CONSTRAINT `fk_conductores_asignaciones_usuarios`
        FOREIGN KEY (`creador` )
        REFERENCES `truckcode`.`sistema_usuarios` (`id` )
        ON DELETE RESTRICT 
        ON UPDATE CASCADE,
    CONSTRAINT `fk_conductores_asignaciones_conductores`
        FOREIGN KEY (`conductor_id` )
        REFERENCES `truckcode`.`conductores` (`id` )
        ON DELETE RESTRICT     
        ON UPDATE CASCADE,
    CONSTRAINT `fk_conductores_asignaciones_camiones`
        FOREIGN KEY (`camion_id` )
        REFERENCES `truckcode`.`camiones` (`id` )
        ON DELETE CASCADE      
        ON UPDATE CASCADE    
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`conductores_documentos`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`conductores_documentos` (
    `id` INT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
    `documento_id` INT(5) UNSIGNED NOT NULL,
    `conductor_id` INT(3) UNSIGNED NOT NULL,
    PRIMARY KEY (`id`),
    INDEX `fk_conductores_documentos_documentos` (`documento_id` ASC) ,
    INDEX `fk_conductores_documentos_conductores` (`conductor_id` ASC) ,
    UNIQUE `unique_documento_conductor` (`documento_id`, `conductor_id` ASC),
    CONSTRAINT `fk_conductores_documentos_documentos`
        FOREIGN KEY (`documento_id` )
        REFERENCES `truckcode`.`documentos` (`id` )
        ON DELETE CASCADE 
        ON UPDATE CASCADE,
    CONSTRAINT `fk_conductores_documentos_conductores`
        FOREIGN KEY (`conductor_id` )
        REFERENCES `truckcode`.`conductores` (`id` )
        ON DELETE RESTRICT   
        ON UPDATE CASCADE    
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`conductores_archivos`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`conductores_archivos` (
    `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
    `archivo_id` INT(5) UNSIGNED NOT NULL,
    `conductor_id` INT(3) UNSIGNED NOT NULL,
    PRIMARY KEY (`id`),
    INDEX `fk_conductores_archivos_archivos` (`archivo_id` ASC) ,
    INDEX `fk_conductores_archivos_conductores` (`conductor_id` ASC) ,
    UNIQUE `unique_archivo_conductor` (`archivo_id`, `conductor_id` ASC),
    CONSTRAINT `fk_conductores_archivos_archivos`
        FOREIGN KEY (`archivo_id` )
        REFERENCES `truckcode`.`archivos` (`id` )
        ON DELETE CASCADE 
        ON UPDATE CASCADE,
    CONSTRAINT `fk_conductores_archivos_conductores`
        FOREIGN KEY (`conductor_id` )
        REFERENCES `truckcode`.`conductores` (`id` )
        ON DELETE RESTRICT   
        ON UPDATE CASCADE    
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`conductores_rendiciones_vales`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`conductores_rendiciones_vales` (
    `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
    `creacion` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `creador` INT(3) UNSIGNED NOT NULL,
    `conductor_id` INT(3) UNSIGNED NOT NULL,
    `camion_id` INT(3) UNSIGNED NOT NULL,
    `centro_id` INT(6) UNSIGNED NOT NULL,
    `numero` INT UNSIGNED NOT NULL,
    `fecha_entrega` DATE NOT NULL,
    `fecha_rendicion` DATE NOT NULL,
    `fecha_cargo` DATE NOT NULL,
    `monto_entregado` INT UNSIGNED NOT NULL,
    `estado` ENUM('BORRADOR', 'ENTREGADO', 'RENDIDO') NOT NULL DEFAULT 'BORRADOR',
    PRIMARY KEY (`id`),
    INDEX `fk_conductores_rendiciones_vales_usuarios` (`creador` ASC) ,
    INDEX `fk_conductores_rendiciones_vales_conductores` (`conductor_id` ASC) ,
    INDEX `fk_conductores_rendiciones_vales_camiones` (`camion_id` ASC) ,
    INDEX `fk_conductores_rendiciones_vales_centros_costo` (`centro_id` ASC) ,
    UNIQUE `unique_numero_vale` (`numero` ASC),
    CONSTRAINT `fk_conductores_rendiciones_vales_usuarios`
        FOREIGN KEY (`creador` )
        REFERENCES `truckcode`.`sistema_usuarios` (`id` )
        ON DELETE RESTRICT   
        ON UPDATE CASCADE,
    CONSTRAINT `fk_conductores_rendiciones_vales_conductores`
        FOREIGN KEY (`conductor_id` )
        REFERENCES `truckcode`.`conductores` (`id` )
        ON DELETE RESTRICT  
        ON UPDATE CASCADE ,
    CONSTRAINT `fk_conductores_rendiciones_vales_camiones`
        FOREIGN KEY (`camion_id` )
        REFERENCES `truckcode`.`camiones` (`id` )
        ON DELETE RESTRICT  
        ON UPDATE CASCADE,
    CONSTRAINT `fk_conductores_rendiciones_vales_centros_costo`
        FOREIGN KEY (`centro_id` )
        REFERENCES `truckcode`.`centros_costo` (`id` )
        ON DELETE RESTRICT  
        ON UPDATE CASCADE    
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`conductores_rendiciones_items`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`conductores_rendiciones_items` (
    `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
    `vale_id` INT(4) UNSIGNED NOT NULL,
    `concepto_id` INT(4) UNSIGNED NOT NULL,
    `cantidad` INT UNSIGNED NOT NULL,
    `monto_rendido` INT UNSIGNED NOT NULL,
    `doc_tipo` ENUM('BOLETA','FACTURA','VALE') NOT NULL,
    `doc_num` INT UNSIGNED NULL,
    `doc_rut` VARCHAR(9) NULL,
    `comentarios` TEXT NULL,
    PRIMARY KEY (`id`),
    INDEX `fk_conductores_rendiciones_items_vales` (`vale_id` ASC) ,
    INDEX `fk_conductores_rendiciones_items_conceptos` (`concepto_id` ASC) ,
    CONSTRAINT `fk_conductores_rendiciones_items_vales`
        FOREIGN KEY (`vale_id` )
        REFERENCES `truckcode`.`conductores_rendiciones_vales` (`id` )
        ON DELETE CASCADE    
        ON UPDATE CASCADE ,
    CONSTRAINT `fk_conductores_rendiciones_items_conceptos`
        FOREIGN KEY (`concepto_id` )
        REFERENCES `truckcode`.`conceptos_imputacion` (`id` )
        ON DELETE RESTRICT   
        ON UPDATE CASCADE    
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`siniestros`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`siniestros` (
    `id` INT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
    `creacion` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `creador` INT(3) UNSIGNED NOT NULL,
    `camion_id` INT(3) UNSIGNED NOT NULL,
    `numero` VARCHAR(50) NOT NULL,
    `fecha` DATE NOT NULL,
    `detalle` TEXT NOT NULL,
    `valor` INT UNSIGNED NOT NULL,
    `estado` INT(1) UNSIGNED NOT NULL,
    PRIMARY KEY (`id`),
    INDEX `fk_siniestros_usuarios` (`creador` ASC) ,
    INDEX `fk_siniestros_camiones` (`camion_id` ASC) ,
    CONSTRAINT `fk_siniestros_usuarios`
        FOREIGN KEY (`creador` )
        REFERENCES `truckcode`.`sistema_usuarios` (`id` )
        ON DELETE RESTRICT
        ON UPDATE CASCADE,
    CONSTRAINT `fk_siniestros_camiones`
        FOREIGN KEY (`camion_id` )
        REFERENCES `truckcode`.`camiones` (`id` )
        ON DELETE CASCADE 
        ON UPDATE CASCADE  
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`siniestros_archivos`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`siniestros_archivos` (
    `archivo_id` INT(5) UNSIGNED NOT NULL,
    `siniestro_id` INT(5) UNSIGNED NOT NULL,
    PRIMARY KEY (`archivo_id`, `siniestro_id`),
    INDEX `fk_siniestros_archivos_archivos` (`archivo_id` ASC) ,
    INDEX `fk_siniestros_archivos_siniestros` (`siniestro_id` ASC) ,
    CONSTRAINT `fk_siniestros_archivos_archivos`
        FOREIGN KEY (`archivo_id` )
        REFERENCES `truckcode`.`archivos` (`id` )
        ON DELETE CASCADE 
        ON UPDATE CASCADE,
    CONSTRAINT `fk_siniestros_archivos_siniestros`
        FOREIGN KEY (`siniestro_id` )
        REFERENCES `truckcode`.`siniestros` (`id` )
        ON DELETE RESTRICT   
        ON UPDATE CASCADE    
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;

-- -----------------------------------------------------
-- Table `truckcode`.`neumaticos_compra`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`neumaticos_compras` (
    `id` INT(6) UNSIGNED NOT NULL AUTO_INCREMENT,
    `creacion` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `creador` INT(3) UNSIGNED NOT NULL,
    `proveedor_id` INT(3) UNSIGNED NOT NULL,
    `num_factura` INT UNSIGNED NOT NULL,
    `fecha_factura` DATE NOT NULL,
    `fecha_cargo` DATE NOT NULL,
    `estado` ENUM('BORRADOR', 'TERMINADO') NOT NULL DEFAULT 'BORRADOR',
    PRIMARY KEY (`id`),
    INDEX `fk_neumaticos_compras_usuarios` (`creador` ASC) ,
    INDEX `fk_neumaticos_compras_proveedores` (`proveedor_id` ASC) ,
    UNIQUE `unique_factura_neumaticos` (`proveedor_id`, `num_factura` ASC),
    CONSTRAINT `fk_neumaticos_compras_usuarios`
        FOREIGN KEY (`creador` )
        REFERENCES `truckcode`.`sistema_usuarios` (`id` )
        ON DELETE RESTRICT
        ON UPDATE CASCADE,
    CONSTRAINT `fk_neumaticos_compras_proveedores`
        FOREIGN KEY (`proveedor_id` )
        REFERENCES `truckcode`.`proveedores` (`id` )
        ON DELETE RESTRICT  
        ON UPDATE CASCADE  
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`neumaticos_compras_items`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`neumaticos_compras_items` (
    `id` INT(6) UNSIGNED NOT NULL AUTO_INCREMENT,
    `neumatico_compra_id` INT(6) UNSIGNED NOT NULL,
    `camion_id` INT(3) UNSIGNED NULL,
    `semiremolque_id` INT(3) UNSIGNED NULL,
    `centro_id` INT(6) UNSIGNED NOT NULL,
    `tipo_id` INT(2) UNSIGNED NOT NULL DEFAULT '1',
    `marca_id` INT(3) UNSIGNED NOT NULL,
    `num_guia` INT UNSIGNED NULL,
    `fecha` DATE NULL,
    `cantidad` INT(2) UNSIGNED NOT NULL,
    `detalle` TEXT NOT NULL,
    `odometro` INT UNSIGNED NULL,
    `valor_neto` INT UNSIGNED NOT NULL,
    PRIMARY KEY (`id`),
    INDEX `fk_neumaticos_compras_items_neumaticos_compras` (`neumatico_compra_id` ASC) ,
    INDEX `fk_neumaticos_compras_items_camiones` (`camion_id` ASC) ,
    INDEX `fk_neumaticos_compras_items_semiremolques` (`semiremolque_id` ASC) ,
    INDEX `fk_neumaticos_compras_items_centros_costo` (`centro_id` ASC) ,
    INDEX `fk_neumaticos_compras_items_tipos` (`tipo_id` ASC) ,
    INDEX `fk_neumaticos_compras_items_marcas` (`marca_id` ASC) ,
    CONSTRAINT `fk_neumaticos_compras_items_neumaticos_compras`
        FOREIGN KEY (`neumatico_compra_id` )
        REFERENCES `truckcode`.`neumaticos_compras` (`id` )
        ON DELETE CASCADE   
        ON UPDATE CASCADE,
    CONSTRAINT `fk_neumaticos_compras_items_camiones`
        FOREIGN KEY (`camion_id` )
        REFERENCES `truckcode`.`camiones` (`id` )
        ON DELETE RESTRICT  
        ON UPDATE CASCADE,
    CONSTRAINT `fk_neumaticos_compras_items_semiremolques`
        FOREIGN KEY (`semiremolque_id` )
        REFERENCES `truckcode`.`semiremolques` (`id` )
        ON DELETE RESTRICT  
        ON UPDATE CASCADE,
    CONSTRAINT `fk_neumaticos_compras_items_centros_costo`
        FOREIGN KEY (`centro_id` )
        REFERENCES `truckcode`.`centros_costo` (`id` )
        ON DELETE RESTRICT  
        ON UPDATE CASCADE,    
    CONSTRAINT `fk_neumaticos_compras_items_tipos`
        FOREIGN KEY (`tipo_id` )
        REFERENCES `truckcode`.`neumaticos_tipos` (`id` )
        ON DELETE RESTRICT  
        ON UPDATE CASCADE,
    CONSTRAINT `fk_neumaticos_compras_items_marcas`
        FOREIGN KEY (`marca_id` )
        REFERENCES `truckcode`.`marcas` (`id` )
        ON DELETE RESTRICT 
        ON UPDATE CASCADE
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`neumaticos_instalados`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`neumaticos_instalados` (
    `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
    `item_id` INT(6) UNSIGNED NOT NULL,
    `fecha_instalacion` DATE NULL,
    `posicion` VARCHAR(3) NULL,
    `posicion_historia` TEXT NULL,
    `estado` ENUM('OPERATIVO', 'TERMINADO') NOT NULL,
    PRIMARY KEY (`id`),
    INDEX `fk_neumaticos_instalados_items` (`item_id` ASC) ,
    CONSTRAINT `fk_neumaticos_instalados_items`
        FOREIGN KEY (`item_id` )
        REFERENCES `truckcode`.`neumaticos_compras_items` (`id` )
        ON DELETE RESTRICT     
        ON UPDATE CASCADE 
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`servicios_pagados`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`servicios_pagados` (
    `id` INT(6) UNSIGNED NOT NULL AUTO_INCREMENT,
    `creacion` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `creador` INT(3) UNSIGNED NOT NULL,
    `proveedor_id` INT(3) UNSIGNED NOT NULL,
    `num_factura` INT UNSIGNED NOT NULL,
    `fecha_factura` DATE NOT NULL,
    `fecha_cargo` DATE NOT NULL,
    `estado` ENUM('BORRADOR', 'TERMINADO') NOT NULL DEFAULT 'BORRADOR',
    PRIMARY KEY (`id`),
    INDEX `fk_servicios_pagados_usuarios` (`creador` ASC) ,
    INDEX `fk_servicios_pagados_proveedores` (`proveedor_id` ASC) ,
    UNIQUE `unique_factura_proveedor` (`proveedor_id`, `num_factura` ASC),
    CONSTRAINT `fk_servicios_pagados_usuarios`
        FOREIGN KEY (`creador` )
        REFERENCES `truckcode`.`sistema_usuarios` (`id` )
        ON DELETE RESTRICT
        ON UPDATE CASCADE,
    CONSTRAINT `fk_servicios_pagados_proveedores`
        FOREIGN KEY (`proveedor_id` )
        REFERENCES `truckcode`.`proveedores` (`id` )
        ON DELETE RESTRICT  
        ON UPDATE CASCADE  
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`servicios_pagados_items`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`servicios_pagados_items` (
    `id` INT(6) UNSIGNED NOT NULL AUTO_INCREMENT,
    `servicio_pagado_id` INT(6) UNSIGNED NOT NULL,
    `camion_id` INT(3) UNSIGNED NULL,
    `semiremolque_id` INT(3) UNSIGNED NULL,
    `tipo_id` INT(2) UNSIGNED NOT NULL DEFAULT '1',
    `concepto_id` INT(3) UNSIGNED NOT NULL,
    `centro_id` INT(6) UNSIGNED NOT NULL,
    `num_guia` INT UNSIGNED NULL,
    `servicio_id` INT(5) UNSIGNED NOT NULL,
    `servicio_desc` VARCHAR(50) NULL,
    `fecha` DATE NULL,
    `detalles` TEXT NOT NULL,
    `observaciones` TEXT NULL,
    `odometro` INT UNSIGNED NULL,
    `realizado` ENUM('SI','NO') NOT NULL,
    `valor_neto` INT UNSIGNED NOT NULL,
    PRIMARY KEY (`id`),
    INDEX `fk_servicios_pagados_items_servicios_pagados` (`servicio_pagado_id` ASC) ,
    INDEX `fk_servicios_pagados_items_camiones` (`camion_id` ASC) ,
    INDEX `fk_servicios_pagados_items_semiremolques` (`semiremolque_id` ASC) ,
    INDEX `fk_servicios_pagados_items_tipos` (`tipo_id` ASC) ,
    INDEX `fk_servicios_pagados_items_conceptos` (`concepto_id` ASC) ,
    INDEX `fk_servicios_pagados_items_centros_costo` (`centro_id` ASC) ,
    INDEX `fk_servicios_pagados_items_servicios` (`servicio_id` ASC) ,
    CONSTRAINT `fk_servicios_pagados_items_servicios_pagados`
        FOREIGN KEY (`servicio_pagado_id` )
        REFERENCES `truckcode`.`servicios_pagados` (`id` )
        ON DELETE RESTRICT  
        ON UPDATE CASCADE,
    CONSTRAINT `fk_servicios_pagados_items_camiones`
        FOREIGN KEY (`camion_id` )
        REFERENCES `truckcode`.`camiones` (`id` )
        ON DELETE RESTRICT  
        ON UPDATE CASCADE,
    CONSTRAINT `fk_servicios_pagados_items_semiremolques`
        FOREIGN KEY (`semiremolque_id` )
        REFERENCES `truckcode`.`semiremolques` (`id` )
        ON DELETE RESTRICT  
        ON UPDATE CASCADE,
    CONSTRAINT `k_servicios_pagados_items_tipos`
        FOREIGN KEY (`tipo_id` )
        REFERENCES `truckcode`.`servicios_tipos` (`id` )
        ON DELETE RESTRICT  
        ON UPDATE CASCADE, 
    CONSTRAINT `fk_servicios_pagados_items_conceptos`
        FOREIGN KEY (`concepto_id` )
        REFERENCES `truckcode`.`conceptos_imputacion` (`id` )
        ON DELETE RESTRICT  
        ON UPDATE CASCADE,
    CONSTRAINT `fk_servicios_pagados_items_centros_costo`
        FOREIGN KEY (`centro_id` )
        REFERENCES `truckcode`.`centros_costo` (`id` )
        ON DELETE RESTRICT  
        ON UPDATE CASCADE,    
    CONSTRAINT `fk_servicios_pagados_items_servicios`
        FOREIGN KEY (`servicio_id` )
        REFERENCES `truckcode`.`servicios` (`id` )
        ON DELETE RESTRICT  
        ON UPDATE CASCADE    
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`servicios_pagados_archivos`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`servicios_pagados_archivos` (
    `archivo_id` INT(5) UNSIGNED NOT NULL,
    `servicio_id` INT(6) UNSIGNED NOT NULL,
    PRIMARY KEY (`archivo_id`, `servicio_id`),
    INDEX `fk_servicios_pagados_archivos_archivos` (`archivo_id` ASC) ,
    INDEX `fk_servicios_pagados_archivos_servicios` (`servicio_id` ASC) ,
    CONSTRAINT `fk_servicios_pagados_archivos_archivos`
        FOREIGN KEY (`archivo_id` )
        REFERENCES `truckcode`.`archivos` (`id` )
        ON DELETE CASCADE 
        ON UPDATE CASCADE,
    CONSTRAINT `fk_servicios_pagados_archivos_servicios`
        FOREIGN KEY (`servicio_id` )
        REFERENCES `truckcode`.`servicios_pagados` (`id` )
        ON DELETE RESTRICT   
        ON UPDATE CASCADE    
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `truckcode`.`periodos`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `truckcode`.`periodos` (
    `id` INT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
    `creacion` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `creador` INT(3) UNSIGNED NOT NULL,
    `nombre` VARCHAR(20) NOT NULL,
    `fecha_inicio` DATE NOT NULL,
    `fecha_termino` DATE NOT NULL,
    `estado` ENUM('ABIERTO', 'CERRADO') NOT NULL DEFAULT 'ABIERTO',
    PRIMARY KEY (`id`),
    INDEX `fk_periodos_usuarios` (`creador` ASC) ,
    CONSTRAINT `fk_periodos_usuarios`
        FOREIGN KEY (`creador` )
        REFERENCES `truckcode`.`sistema_usuarios` (`id` )
        ON DELETE RESTRICT
        ON UPDATE CASCADE
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------