-- MySQL Script generated by MySQL Workbench
-- Thu 15 Dec 2016 04:42:33 PM EET
-- Model: New Model    Version: 1.0
-- MySQL Workbench Forward Engineering

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='STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,ALLOW_INVALID_DATES';

-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------

-- -----------------------------------------------------
-- Table `accAccountT`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `accAccountT` ;

CREATE TABLE IF NOT EXISTS `accAccountT` (
  `id` INT UNSIGNED NOT NULL COMMENT '',
  `code` VARCHAR(255) NOT NULL COMMENT '',
  `type` ENUM('debit', 'credit', 'none') NOT NULL DEFAULT 'none' COMMENT '',
  PRIMARY KEY (`id`)  COMMENT '',
  UNIQUE INDEX `code_UNIQUE` (`code` ASC)  COMMENT '',
  CONSTRAINT `fk_accAccountT_nodeT1`
    FOREIGN KEY (`id`)
    REFERENCES `nodeT` (`id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `accActivityT`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `accActivityT` ;

CREATE TABLE IF NOT EXISTS `accActivityT` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '',
  `actionStringId` VARCHAR(60) BINARY NOT NULL COMMENT '',
  `added` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '',
  PRIMARY KEY (`id`)  COMMENT '',
  INDEX `fk_accActivityT_actionStringT1_idx` (`actionStringId` ASC)  COMMENT '',
  CONSTRAINT `fk_accActivityT_actionStringT1`
    FOREIGN KEY (`actionStringId`)
    REFERENCES `actionStringT` (`id`)
    ON DELETE NO ACTION
    ON UPDATE CASCADE)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `accTransactionT`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `accTransactionT` ;

CREATE TABLE IF NOT EXISTS `accTransactionT` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '',
  `actionStringId` VARCHAR(60) BINARY NULL COMMENT '',
  `accActivityId` BIGINT UNSIGNED NULL COMMENT '',
  `added` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '',
  PRIMARY KEY (`id`)  COMMENT '',
  INDEX `fk_accTransactionT_accActivityT1_idx` (`accActivityId` ASC)  COMMENT '',
  INDEX `fk_accTransactionT_actionStringT1_idx` (`actionStringId` ASC)  COMMENT '',
  CONSTRAINT `fk_accTransactionT_accActivityT1`
    FOREIGN KEY (`accActivityId`)
    REFERENCES `accActivityT` (`id`)
    ON DELETE NO ACTION
    ON UPDATE CASCADE,
  CONSTRAINT `fk_accTransactionT_actionStringT1`
    FOREIGN KEY (`actionStringId`)
    REFERENCES `actionStringT` (`id`)
    ON DELETE NO ACTION
    ON UPDATE CASCADE)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `accCommodityT`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `accCommodityT` ;

CREATE TABLE IF NOT EXISTS `accCommodityT` (
  `id` INT UNSIGNED NOT NULL COMMENT '',
  `code` VARCHAR(255) NOT NULL COMMENT '',
  PRIMARY KEY (`id`)  COMMENT '',
  UNIQUE INDEX `code_UNIQUE` (`code` ASC)  COMMENT '',
  CONSTRAINT `fk_accCommodityT_nodeT1`
    FOREIGN KEY (`id`)
    REFERENCES `nodeT` (`id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `accEntryT`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `accEntryT` ;

CREATE TABLE IF NOT EXISTS `accEntryT` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '',
  `accTransactionId` BIGINT UNSIGNED NOT NULL COMMENT '',
  `accAccountId` INT UNSIGNED NOT NULL COMMENT '',
  `accCommodityId` INT UNSIGNED NOT NULL COMMENT '',
  `amount` DECIMAL(22,6) NOT NULL COMMENT '',
  PRIMARY KEY (`id`)  COMMENT '',
  INDEX `fk_accEntryT_accTransactionT1_idx` (`accTransactionId` ASC)  COMMENT '',
  INDEX `fk_accEntryT_accAccountT1_idx` (`accAccountId` ASC)  COMMENT '',
  INDEX `fk_accEntryT_accCommodityT1_idx` (`accCommodityId` ASC)  COMMENT '',
  UNIQUE INDEX `acctransactionid_accaccountid_acccommodityid_uq` (`accTransactionId` ASC, `accAccountId` ASC, `accCommodityId` ASC)  COMMENT '',
  CONSTRAINT `fk_accEntryT_accTransactionT1`
    FOREIGN KEY (`accTransactionId`)
    REFERENCES `accTransactionT` (`id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_accEntryT_accAccountT1`
    FOREIGN KEY (`accAccountId`)
    REFERENCES `accAccountT` (`id`)
    ON DELETE NO ACTION
    ON UPDATE CASCADE,
  CONSTRAINT `fk_accEntryT_accCommodityT1`
    FOREIGN KEY (`accCommodityId`)
    REFERENCES `accCommodityT` (`id`)
    ON DELETE NO ACTION
    ON UPDATE CASCADE)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `accTransactionPostT`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `accTransactionPostT` ;

CREATE TABLE IF NOT EXISTS `accTransactionPostT` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '',
  `accTransactionId` BIGINT UNSIGNED NOT NULL COMMENT '',
  `added` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '',
  PRIMARY KEY (`id`)  COMMENT '',
  UNIQUE INDEX `accTransactionId_UNIQUE` (`accTransactionId` ASC)  COMMENT '',
  CONSTRAINT `fk_accTransactionPostT_accTransactionT1`
    FOREIGN KEY (`accTransactionId`)
    REFERENCES `accTransactionT` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `accTotalT`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `accTotalT` ;

CREATE TABLE IF NOT EXISTS `accTotalT` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '',
  `accTransactionId` BIGINT UNSIGNED NOT NULL COMMENT '',
  `accAccountId` INT UNSIGNED NOT NULL COMMENT '',
  `accCommodityId` INT UNSIGNED NOT NULL COMMENT '',
  `amount` DECIMAL(22,6) NULL COMMENT '',
  PRIMARY KEY (`id`)  COMMENT '',
  INDEX `fk_accTotalT_accTransactionT1_idx` (`accTransactionId` ASC)  COMMENT '',
  INDEX `fk_accTotalT_accAccountT1_idx` (`accAccountId` ASC)  COMMENT '',
  INDEX `fk_accTotalT_accCommodityT1_idx` (`accCommodityId` ASC)  COMMENT '',
  UNIQUE INDEX `acctransactionid_accaccountid_acccommodityid_uq` (`accTransactionId` ASC, `accAccountId` ASC, `accCommodityId` ASC)  COMMENT '',
  CONSTRAINT `fk_accTotalT_accTransactionT1`
    FOREIGN KEY (`accTransactionId`)
    REFERENCES `accTransactionT` (`id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_accTotalT_accAccountT1`
    FOREIGN KEY (`accAccountId`)
    REFERENCES `accAccountT` (`id`)
    ON DELETE NO ACTION
    ON UPDATE CASCADE,
  CONSTRAINT `fk_accTotalT_accCommodityT1`
    FOREIGN KEY (`accCommodityId`)
    REFERENCES `accCommodityT` (`id`)
    ON DELETE NO ACTION
    ON UPDATE CASCADE)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `accPriceT`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `accPriceT` ;

CREATE TABLE IF NOT EXISTS `accPriceT` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '',
  `accObjectCommodityId` INT UNSIGNED NOT NULL COMMENT '',
  `accPriceCommodityId` INT UNSIGNED NOT NULL COMMENT '',
  `objectAmount` DECIMAL(22,6) NOT NULL DEFAULT 1 COMMENT '',
  `priceAmount` DECIMAL(22,6) NOT NULL COMMENT '',
  `added` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '',
  `changed` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '',
  PRIMARY KEY (`id`)  COMMENT '',
  INDEX `fk_accPriceT_accCommodityT1_idx` (`accObjectCommodityId` ASC)  COMMENT '',
  INDEX `fk_accPriceT_accCommodityT2_idx` (`accPriceCommodityId` ASC)  COMMENT '',
  CONSTRAINT `fk_accPriceT_accCommodityT1`
    FOREIGN KEY (`accObjectCommodityId`)
    REFERENCES `accCommodityT` (`id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_accPriceT_accCommodityT2`
    FOREIGN KEY (`accPriceCommodityId`)
    REFERENCES `accCommodityT` (`id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `accAccountOptionT`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `accAccountOptionT` ;

CREATE TABLE IF NOT EXISTS `accAccountOptionT` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '',
  `accAccountId` INT UNSIGNED NOT NULL COMMENT '',
  `accCommodityId` INT UNSIGNED NOT NULL COMMENT '',
  `minAmount` DECIMAL(22,6) NULL COMMENT '',
  `maxAmount` DECIMAL(22,6) NULL COMMENT '',
  `amountRound` INT NOT NULL COMMENT '',
  `added` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '',
  `changed` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '',
  PRIMARY KEY (`id`)  COMMENT '',
  INDEX `fk_accAccountOptionT_accAccountT1_idx` (`accAccountId` ASC)  COMMENT '',
  INDEX `fk_accAccountOptionT_accCommodityT1_idx` (`accCommodityId` ASC)  COMMENT '',
  CONSTRAINT `fk_accAccountOptionT_accAccountT11`
    FOREIGN KEY (`accAccountId`)
    REFERENCES `accAccountT` (`id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_accAccountOptionT_accCommodityT11`
    FOREIGN KEY (`accCommodityId`)
    REFERENCES `accCommodityT` (`id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `accAccountTL`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `accAccountTL` ;

CREATE TABLE IF NOT EXISTS `accAccountTL` (
  `id` INT UNSIGNED NOT NULL COMMENT '',
  `languageId` CHAR(3) NOT NULL COMMENT '',
  `name` VARCHAR(255) NOT NULL COMMENT '',
  PRIMARY KEY (`id`, `languageId`)  COMMENT '',
  INDEX `fk_accAccountTL_languageT1_idx` (`languageId` ASC)  COMMENT '',
  CONSTRAINT `fk_accAccountTL_accAccountT1`
    FOREIGN KEY (`id`)
    REFERENCES `accAccountT` (`id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_accAccountTL_languageT1`
    FOREIGN KEY (`languageId`)
    REFERENCES `languageT` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `accCommodityTL`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `accCommodityTL` ;

CREATE TABLE IF NOT EXISTS `accCommodityTL` (
  `id` INT UNSIGNED NOT NULL COMMENT '',
  `languageId` CHAR(3) NOT NULL COMMENT '',
  `name` VARCHAR(255) NOT NULL COMMENT '',
  PRIMARY KEY (`id`, `languageId`)  COMMENT '',
  INDEX `fk_accCommodityTL_languageT1_idx` (`languageId` ASC)  COMMENT '',
  CONSTRAINT `fk_accCommodityTL_accCommodityT1`
    FOREIGN KEY (`id`)
    REFERENCES `accCommodityT` (`id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_accCommodityTL_languageT1`
    FOREIGN KEY (`languageId`)
    REFERENCES `languageT` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `accInvoiceT`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `accInvoiceT` ;

CREATE TABLE IF NOT EXISTS `accInvoiceT` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '',
  `userId` INT UNSIGNED NULL COMMENT '',
  `userName` VARCHAR(255) NOT NULL COMMENT '',
  `accTransactionId` BIGINT UNSIGNED NULL COMMENT '',
  `amount` DECIMAL(22,6) NOT NULL COMMENT '',
  PRIMARY KEY (`id`)  COMMENT '',
  INDEX `fk_accInvoiceT_userT1_idx` (`userId` ASC)  COMMENT '',
  INDEX `fk_accInvoiceT_accTransactionT1_idx` (`accTransactionId` ASC)  COMMENT '',
  CONSTRAINT `fk_accInvoiceT_userT1`
    FOREIGN KEY (`userId`)
    REFERENCES `userT` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_accInvoiceT_accTransactionT1`
    FOREIGN KEY (`accTransactionId`)
    REFERENCES `accTransactionT` (`id`)
    ON DELETE NO ACTION
    ON UPDATE CASCADE)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `accInvoiceStatusT`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `accInvoiceStatusT` ;

CREATE TABLE IF NOT EXISTS `accInvoiceStatusT` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '',
  `accInvoiceId` BIGINT UNSIGNED NOT NULL COMMENT '',
  `actionStringId` VARCHAR(60) BINARY NOT NULL COMMENT '',
  `added` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '',
  PRIMARY KEY (`id`)  COMMENT '',
  INDEX `fk_accInvoiceStatusT_accInvoiceT1_idx` (`accInvoiceId` ASC)  COMMENT '',
  INDEX `fk_accInvoiceStatusT_actionStringT1_idx` (`actionStringId` ASC)  COMMENT '',
  CONSTRAINT `fk_accInvoiceStatusT_accInvoiceT1`
    FOREIGN KEY (`accInvoiceId`)
    REFERENCES `accInvoiceT` (`id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_accInvoiceStatusT_actionStringT1`
    FOREIGN KEY (`actionStringId`)
    REFERENCES `actionStringT` (`id`)
    ON DELETE NO ACTION
    ON UPDATE CASCADE)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `accScheduleT`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `accScheduleT` ;

CREATE TABLE IF NOT EXISTS `accScheduleT` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '',
  `nodeId` INT UNSIGNED NOT NULL COMMENT '',
  `actionStringId` VARCHAR(60) BINARY NOT NULL COMMENT 'what to do',
  `periodActionStringId` VARCHAR(60) BINARY NOT NULL COMMENT 'period of action - days, months etc',
  `periodAmount` INT UNSIGNED NOT NULL DEFAULT 1 COMMENT '',
  `added` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '',
  `changed` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '',
  PRIMARY KEY (`id`)  COMMENT '',
  INDEX `fk_accScheduleT_nodeT1_idx` (`nodeId` ASC)  COMMENT '',
  INDEX `fk_accScheduleT_actionStringT1_idx` (`actionStringId` ASC)  COMMENT '',
  INDEX `fk_accScheduleT_actionStringT2_idx` (`periodActionStringId` ASC)  COMMENT '',
  CONSTRAINT `fk_accScheduleT_nodeT1`
    FOREIGN KEY (`nodeId`)
    REFERENCES `nodeT` (`id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_accScheduleT_actionStringT1`
    FOREIGN KEY (`actionStringId`)
    REFERENCES `actionStringT` (`id`)
    ON DELETE NO ACTION
    ON UPDATE CASCADE,
  CONSTRAINT `fk_accScheduleT_actionStringT2`
    FOREIGN KEY (`periodActionStringId`)
    REFERENCES `actionStringT` (`id`)
    ON DELETE NO ACTION
    ON UPDATE CASCADE)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `accScheduleJobT`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `accScheduleJobT` ;

CREATE TABLE IF NOT EXISTS `accScheduleJobT` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '',
  `accScheduleId` INT UNSIGNED NOT NULL COMMENT '',
  `accTransactionId` BIGINT UNSIGNED NULL COMMENT 'if it is NULL, the job is active',
  `dueOn` DATETIME NOT NULL COMMENT '',
  `added` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '',
  `changed` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '',
  PRIMARY KEY (`id`)  COMMENT '',
  INDEX `fk_accScheduleJobT_accScheduleT1_idx` (`accScheduleId` ASC)  COMMENT '',
  INDEX `fk_accScheduleJobT_accTransactionT1_idx` (`accTransactionId` ASC)  COMMENT '',
  INDEX `accscheduleid_dueon_idx` (`accScheduleId` ASC, `dueOn` ASC)  COMMENT '',
  CONSTRAINT `fk_accScheduleJobT_accScheduleT1`
    FOREIGN KEY (`accScheduleId`)
    REFERENCES `accScheduleT` (`id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_accScheduleJobT_accTransactionT1`
    FOREIGN KEY (`accTransactionId`)
    REFERENCES `accTransactionT` (`id`)
    ON DELETE NO ACTION
    ON UPDATE CASCADE)
ENGINE = InnoDB;


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