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 `bd_ventas` DEFAULT CHARACTER SET latin1 COLLATE latin1_spanish_ci ;
USE `bd_ventas`;

-- -----------------------------------------------------
-- Table `bd_ventas`.`caja`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `bd_ventas`.`caja` ;

CREATE  TABLE IF NOT EXISTS `bd_ventas`.`caja` (
  `codcaja` TINYINT NOT NULL ,
  `ubicacion` VARCHAR(100) NULL ,
  PRIMARY KEY (`codcaja`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `bd_ventas`.`deposito`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `bd_ventas`.`deposito` ;

CREATE  TABLE IF NOT EXISTS `bd_ventas`.`deposito` (
  `codeposito` VARCHAR(10) NOT NULL ,
  `descripcion` VARCHAR(45) NULL ,
  PRIMARY KEY (`codeposito`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `bd_ventas`.`turno`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `bd_ventas`.`turno` ;

CREATE  TABLE IF NOT EXISTS `bd_ventas`.`turno` (
  `codturno` INT NOT NULL ,
  `fecha` DATE NULL ,
  `horaini` TIME NULL ,
  `horafin` TIME NULL ,
  `saldoini` DECIMAL(6,2) NULL ,
  `saldofin` DECIMAL(6,2) NULL ,
  `saldoactual` DECIMAL(6,2) NULL ,
  `cerrado` TINYINT NULL ,
  `caja_codcaja` TINYINT NULL ,
  `deposito_codeposito` VARCHAR(10) NULL ,
  PRIMARY KEY (`codturno`) ,
  CONSTRAINT `fk_turno_caja`
    FOREIGN KEY (`caja_codcaja` )
    REFERENCES `bd_ventas`.`caja` (`codcaja` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_turno_deposito`
    FOREIGN KEY (`deposito_codeposito` )
    REFERENCES `bd_ventas`.`deposito` (`codeposito` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

CREATE INDEX `fk_turno_caja` ON `bd_ventas`.`turno` (`caja_codcaja` ASC) ;

CREATE INDEX `fk_turno_deposito` ON `bd_ventas`.`turno` (`deposito_codeposito` ASC) ;


-- -----------------------------------------------------
-- Table `bd_ventas`.`usuario`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `bd_ventas`.`usuario` ;

CREATE  TABLE IF NOT EXISTS `bd_ventas`.`usuario` (
  `codusuario` VARCHAR(15) NOT NULL ,
  `cedula` VARCHAR(15) NULL ,
  `nombre` VARCHAR(100) NULL ,
  `apellido` VARCHAR(100) NULL ,
  PRIMARY KEY (`codusuario`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `bd_ventas`.`venta`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `bd_ventas`.`venta` ;

CREATE  TABLE IF NOT EXISTS `bd_ventas`.`venta` (
  `codventa` INT NOT NULL ,
  `fecha` DATE NULL ,
  `hora` INT NULL ,
  `total` DECIMAL(6,2) NULL ,
  `estatus` VARCHAR(1) NULL ,
  `turno_codturno` INT NULL ,
  PRIMARY KEY (`codventa`) ,
  CONSTRAINT `fk_venta_turno`
    FOREIGN KEY (`turno_codturno` )
    REFERENCES `bd_ventas`.`turno` (`codturno` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

CREATE INDEX `fk_venta_turno` ON `bd_ventas`.`venta` (`turno_codturno` ASC) ;


-- -----------------------------------------------------
-- Table `bd_ventas`.`servicio`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `bd_ventas`.`servicio` ;

CREATE  TABLE IF NOT EXISTS `bd_ventas`.`servicio` (
  `codservicio` VARCHAR(10) NOT NULL ,
  `nombre` VARCHAR(60) NULL ,
  `costo` DECIMAL(6,2) NULL ,
  PRIMARY KEY (`codservicio`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `bd_ventas`.`detventa`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `bd_ventas`.`detventa` ;

CREATE  TABLE IF NOT EXISTS `bd_ventas`.`detventa` (
  `codetventa` INT NOT NULL ,
  `precio` DECIMAL(6,2) NULL ,
  `estatus` VARCHAR(2) NULL ,
  `cargo` DECIMAL(6,2) NULL ,
  `venta_codventa` INT NULL ,
  `servicio_codservicio` VARCHAR(10) NULL ,
  `usuario_codusuario` VARCHAR(15) NULL ,
  PRIMARY KEY (`codetventa`) ,
  CONSTRAINT `fk_detventa_venta`
    FOREIGN KEY (`venta_codventa` )
    REFERENCES `bd_ventas`.`venta` (`codventa` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_detventa_servicio`
    FOREIGN KEY (`servicio_codservicio` )
    REFERENCES `bd_ventas`.`servicio` (`codservicio` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_detventa_usuario`
    FOREIGN KEY (`usuario_codusuario` )
    REFERENCES `bd_ventas`.`usuario` (`codusuario` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

CREATE INDEX `fk_detventa_venta` ON `bd_ventas`.`detventa` (`venta_codventa` ASC) ;

CREATE INDEX `fk_detventa_servicio` ON `bd_ventas`.`detventa` (`servicio_codservicio` ASC) ;

CREATE INDEX `fk_detventa_usuario` ON `bd_ventas`.`detventa` (`usuario_codusuario` ASC) ;


-- -----------------------------------------------------
-- Table `bd_ventas`.`cmpcontable`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `bd_ventas`.`cmpcontable` ;

CREATE  TABLE IF NOT EXISTS `bd_ventas`.`cmpcontable` (
  `comprobante` INT NOT NULL ,
  `descripcion` VARCHAR(255) NULL ,
  `monto` DECIMAL(6,2) NULL ,
  PRIMARY KEY (`comprobante`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `bd_ventas`.`detcontable`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `bd_ventas`.`detcontable` ;

CREATE  TABLE IF NOT EXISTS `bd_ventas`.`detcontable` (
  `detcontable`  NOT NULL ,
  `operacion` VARCHAR(1) NULL ,
  `monto` DECIMAL(6,2) NULL ,
  `descripcion` TINYTEXT NULL ,
  `procede` VARCHAR(5) NULL ,
  `documento` VARCHAR(10) NULL ,
  `fecha` DATE NULL ,
  `deposito_codeposito` VARCHAR(10) NULL ,
  `cmpcontable_comprobante` INT NULL ,
  PRIMARY KEY (`detcontable`) ,
  CONSTRAINT `fk_detcontable_deposito`
    FOREIGN KEY (`deposito_codeposito` )
    REFERENCES `bd_ventas`.`deposito` (`codeposito` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_detcontable_cmpcontable`
    FOREIGN KEY (`cmpcontable_comprobante` )
    REFERENCES `bd_ventas`.`cmpcontable` (`comprobante` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

CREATE INDEX `fk_detcontable_deposito` ON `bd_ventas`.`detcontable` (`deposito_codeposito` ASC) ;

CREATE INDEX `fk_detcontable_cmpcontable` ON `bd_ventas`.`detcontable` (`cmpcontable_comprobante` ASC) ;



SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
