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';
-- -----------------------------------------------------
-- Table `control_frigo`.`contenedores_arriendos`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `control_frigo`.`contenedores_arriendos` (
    `id` INT(6) UNSIGNED NOT NULL AUTO_INCREMENT,
    `proveedor_id` INT(3) UNSIGNED NOT NULL,
    `creado_por` INT(2) UNSIGNED NOT NULL,
    `tipo_contenedor_id` INT(2) UNSIGNED NOT NULL,
    `tipo_arriendo_id` INT(2) UNSIGNED NOT NULL,
    `identificador` VARCHAR(20) NOT NULL,
    `recepcion_fecha` DATE NOT NULL,
    `recepcion_observaciones` TEXT NULL,
    `despacho_fecha` DATE NULL,
    `despacho_observaciones` TEXT NULL ,
    `dias_pagados` INT UNSIGNED NULL,
    PRIMARY KEY (`id`),
    INDEX `fk_contenedores_arriendos_proveedores` (`proveedor_id` ASC) ,
    INDEX `fk_contenedores_arriendos_creadores` (`creado_por` ASC ),
    INDEX `fk_contenedores_arriendos_tipos_contenedores` (`tipo_contenedor_id` ASC) ,
    INDEX `fk_contenedores_arriendos_tipos_arriendos` (`tipo_arriendo_id` ASC) ,
    CONSTRAINT `fk_contenedores_arriendos_proveedores`
        FOREIGN KEY (`proveedor_id` )
        REFERENCES `control_frigo`.`proveedores` (`id` )
        ON DELETE RESTRICT
        ON UPDATE CASCADE,
    CONSTRAINT `fk_contenedores_arriendos_creadores`
        FOREIGN KEY (`creado_por`)
        REFERENCES `control_frigo`.`sistema_usuarios` (`id`)
        ON DELETE RESTRICT
        ON UPDATE CASCADE,
    CONSTRAINT `fk_contenedores_arriendos_tipos_contenedores`
        FOREIGN KEY (`tipo_contenedor_id` )
        REFERENCES `control_frigo`.`contenedores_tipos` (`id` )
        ON DELETE RESTRICT  
        ON UPDATE CASCADE,
    CONSTRAINT `fk_contenedores_arriendos_tipos_arriendos`
        FOREIGN KEY (`tipo_arriendo_id` )
        REFERENCES `control_frigo`.`arriendos_tipos` (`id` )
        ON DELETE RESTRICT
        ON UPDATE CASCADE
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `control_frigo`.`contenedores_ingresos_patios`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `control_frigo`.`contenedores_ingresos_patios` (
    `id` INT(6) UNSIGNED NOT NULL AUTO_INCREMENT,
    `creacion`    TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `arriendo_id` INT(6) UNSIGNED NOT NULL,
    `patio_id`    INT(2) UNSIGNED NOT NULL,
    `cliente_id`  INT(3) UNSIGNED NOT NULL,
    `ubicacion`   VARCHAR(20) NOT NULL,
    `ingreso_fecha` DATE NOT NULL,
    `ingreso_comentarios` TEXT NULL,
    `ingreso_usuario_id` INT(2) UNSIGNED NOT NULL,
    `salida_fecha` DATE NULL,
    `salida_comentarios` TEXT NULL,
    `salida_usuario_id` INT(2) UNSIGNED NULL,
    `enchufado` ENUM('SI', 'NO') NOT NULL DEFAULT 'SI',
    PRIMARY KEY (`id`),
    INDEX `fk_contenedores_ingresos_patios_arriendos` (`arriendo_id` ASC) ,
    INDEX `fk_contenedores_ingresos_patios_patios` (`patio_id` ASC) ,
    INDEX `fk_contenedores_ingresos_patios_clientes` (`cliente_id` ASC) ,
    INDEX `fk_contenedores_ingresos_patios_usuarios_ingresos` (`ingreso_usuario_id` ASC) ,
    INDEX `fk_contenedores_ingresos_patios_usuarios_salidas` (`salida_usuario_id` ASC) ,
    CONSTRAINT `fk_contenedores_ingresos_patios_arriendos`
        FOREIGN KEY (`arriendo_id` )
        REFERENCES `control_frigo`.`contenedores_arriendos` (`id` )
        ON DELETE CASCADE
        ON UPDATE CASCADE,
    CONSTRAINT `fk_contenedores_ingresos_patios_patios`
        FOREIGN KEY (`patio_id` )
        REFERENCES `control_frigo`.`contenedores_patios` (`id` )
        ON DELETE RESTRICT
        ON UPDATE CASCADE,
    CONSTRAINT `fk_contenedores_ingresos_patios_clientes`
        FOREIGN KEY (`cliente_id` )
        REFERENCES `control_frigo`.`clientes` (`id` )
        ON DELETE RESTRICT
        ON UPDATE CASCADE,
    CONSTRAINT `fk_contenedores_ingresos_patios_usuarios_ingresos`
        FOREIGN KEY (`ingreso_usuario_id` )
        REFERENCES `control_frigo`.`sistema_usuarios` (`id` )
        ON DELETE RESTRICT
        ON UPDATE CASCADE ,
    CONSTRAINT `fk_contenedores_ingresos_patios_usuarios_salidas`
        FOREIGN KEY (`salida_usuario_id` )
        REFERENCES `control_frigo`.`sistema_usuarios` (`id` )
        ON DELETE RESTRICT
        ON UPDATE CASCADE
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `control_frigo`.`contenedores_ingresos_cargas`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `control_frigo`.`contenedores_ingresos_cargas` (
    `id` INT(6) UNSIGNED NOT NULL AUTO_INCREMENT,
    `ingreso_id` INT(6) UNSIGNED NOT NULL,
    `guia_rut` VARCHAR(9) NOT NULL,
    `guia_numero` INT UNSIGNED NOT NULL,
    `numero_tarja_manual` INT(7) UNSIGNED ZEROFILL NULL,
    `resumen_pallets` INT(2)  DEFAULT 0,
    `resumen_tons_normal` DECIMAL(6,3) UNSIGNED NOT NULL  DEFAULT 0,
    `resumen_tons_extra` DECIMAL(6,3) UNSIGNED NOT NULL  DEFAULT 0,
    `resumen_tons_sitrans_costo` DECIMAL(6,3) UNSIGNED NOT NULL  DEFAULT 0,
    `resumen_tons_sitrans_comentarios` TEXT NULL,
    `resumen_horas_normal` DECIMAL(4,2) UNSIGNED NOT NULL  DEFAULT 0,
    `resumen_horas_habilitacion` DECIMAL(4,2) UNSIGNED NOT NULL  DEFAULT 0,
    `resumen_horas_sitrans_costo` DECIMAL(4,2) UNSIGNED NOT NULL DEFAULT 0,
    `resumen_horas_sitrans_comentarios` TEXT NULL,
    `resumen_temperatura_promedio` DECIMAL(4,2) NOT NULL,
    `resumen_temperatura_muestras` TEXT NULL,
    `resumen_cajas_reempacadas` INT UNSIGNED NOT NULL DEFAULT 0,
    `resumen_cajas_inspeccionadas` INT UNSIGNED NOT NULL DEFAULT 0,
    `resumen_extras_comentarios` TEXT NULL,
    `resumen_etiquetado_confeccion` INT(6) UNSIGNED NOT NULL DEFAULT 0,
    `resumen_etiquetado_instalacion` INT(6) UNSIGNED NOT NULL DEFAULT 0,
    `resumen_etiquetado_sacado` INT(6) UNSIGNED NOT NULL DEFAULT 0,
    PRIMARY KEY (`id`),
    INDEX `fk_contenedores_ingresos_guias_ingresos` (`ingreso_id` ASC) ,
    CONSTRAINT `fk_contenedores_ingresos_guias_ingresos`
        FOREIGN KEY (`ingreso_id` )
        REFERENCES `control_frigo`.`contenedores_ingresos_patios` (`id` )
        ON DELETE CASCADE
        ON UPDATE CASCADE
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `control_frigo`.`contenedores_arriendos_archivos`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `control_frigo`.`contenedores_arriendos_archivos` (
    `id` INT(6) UNSIGNED NOT NULL AUTO_INCREMENT,
    `arriendo_id` INT(6) UNSIGNED NOT NULL,
    `tipo` ENUM('RECEPCION', 'DESPACHO') NOT NULL DEFAULT 'RECEPCION',
    `archivo` VARCHAR(100) NOT NULL,
    `texto` VARCHAR(200) NOT NULL,
    `lugar` INT(3) UNSIGNED NOT NULL DEFAULT 0,
    PRIMARY KEY (`id`),
    INDEX `fk_contenedores_arriendos_archivos_arriendos` (`arriendo_id` ASC) ,
    CONSTRAINT `fk_contenedores_arriendos_archivos_arriendos`
        FOREIGN KEY (`arriendo_id` )
        REFERENCES `control_frigo`.`contenedores_arriendos` (`id` )
        ON DELETE CASCADE
        ON UPDATE CASCADE
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;