# ---------------------------------------------------------------------- #
# Modify table "LOG"                                                     #
# ---------------------------------------------------------------------- #

#ALTER TABLE `LOG` MODIFY `descrizione` TEXT;

# ---------------------------------------------------------------------- #
# Modify table "CLIFOR"                                                  #
# ---------------------------------------------------------------------- #

ALTER TABLE `CLIFOR` ADD COLUMN `id_pianoConti` INTEGER;

# ---------------------------------------------------------------------- #
# Modify table "TIPO_DOCUMENTO"                                          #
# ---------------------------------------------------------------------- #

ALTER TABLE `TIPO_DOCUMENTO` ADD COLUMN `id_causaleContabile` INTEGER;

# ---------------------------------------------------------------------- #
# Modify table "IVA"                                                     #
# ---------------------------------------------------------------------- #

ALTER TABLE `IVA` ADD COLUMN `id_pianoConti` INTEGER;

# ---------------------------------------------------------------------- #
# Modify table "BANCA"                                                   #
# ---------------------------------------------------------------------- #

ALTER TABLE `BANCA` ADD COLUMN `id_pianoConti` INTEGER;

# ---------------------------------------------------------------------- #
# Add table "PIANO_CONTI"                                                #
# ---------------------------------------------------------------------- #

CREATE TABLE `PIANO_CONTI` (
    `id_pianoConti` INTEGER NOT NULL AUTO_INCREMENT,
    `conto` VARCHAR(40),
    `descrizione` VARCHAR(254),
    `flgTipo` INTEGER,
    `flgCFBI` INTEGER,
    `flgMovimentabile` INTEGER,
    CONSTRAINT `PK_PIANO_CONTI` PRIMARY KEY (`id_pianoConti`)
);

# ---------------------------------------------------------------------- #
# Add table "MOV_CONTABILE"                                              #
# ---------------------------------------------------------------------- #

CREATE TABLE `MOV_CONTABILE` (
    `id_movContabile` INTEGER NOT NULL AUTO_INCREMENT,
    `id_causaleContabile` INTEGER,
    `dataMovContabile` DATE,
    `descrizione` VARCHAR(254),
    `flgStato` INTEGER,
    CONSTRAINT `PK_MOV_CONTABILE` PRIMARY KEY (`id_movContabile`)
);

# ---------------------------------------------------------------------- #
# Add table "RIGA_MOV_CONTABILE"                                         #
# ---------------------------------------------------------------------- #

CREATE TABLE `RIGA_MOV_CONTABILE` (
    `id_rigaMovContabile` INTEGER NOT NULL AUTO_INCREMENT,
    `id_movContabile` INTEGER,
    `importo` DECIMAL(18,4),
    `flgDA` INTEGER,
    `id_pianoConti` INTEGER,
    `id_clifor` INTEGER,
    `id_banca` INTEGER,
    `id_documento` INTEGER,
    `id_iva` INTEGER,
    CONSTRAINT `PK_RIGA_MOV_CONTABILE` PRIMARY KEY (`id_rigaMovContabile`)
);

# ---------------------------------------------------------------------- #
# Add table "CAUSALE_CONTABILE"                                          #
# ---------------------------------------------------------------------- #

CREATE TABLE `CAUSALE_CONTABILE` (
    `id_causaleContabile` INTEGER NOT NULL AUTO_INCREMENT,
    `descrizione` VARCHAR(40),
    CONSTRAINT `PK_CAUSALE_CONTABILE` PRIMARY KEY (`id_causaleContabile`)
);

# ---------------------------------------------------------------------- #
# Add table "RIGA_CAUSALE_CONTABILE"                                     #
# ---------------------------------------------------------------------- #

CREATE TABLE `RIGA_CAUSALE_CONTABILE` (
    `id_rigaCausaleContabile` INTEGER NOT NULL AUTO_INCREMENT,
    `id_causaleContabile` INTEGER,
    `id_pianoConti` INTEGER,
    `flgDA` INTEGER,
    CONSTRAINT `PK_RIGA_CAUSALE_CONTABILE` PRIMARY KEY (`id_rigaCausaleContabile`)
);

# ---------------------------------------------------------------------- #
# Add foreign key constraints                                            #
# ---------------------------------------------------------------------- #

ALTER TABLE `CLIFOR` ADD CONSTRAINT `PIANO_CONTI_CLIFOR` 
    FOREIGN KEY (`id_pianoConti`) REFERENCES `PIANO_CONTI` (`id_pianoConti`);

ALTER TABLE `TIPO_DOCUMENTO` ADD CONSTRAINT `CAUSALE_CONTABILE_TIPO_DOCUMENTO` 
    FOREIGN KEY (`id_causaleContabile`) REFERENCES `CAUSALE_CONTABILE` (`id_causaleContabile`);

ALTER TABLE `IVA` ADD CONSTRAINT `PIANO_CONTI_IVA` 
    FOREIGN KEY (`id_pianoConti`) REFERENCES `PIANO_CONTI` (`id_pianoConti`);

ALTER TABLE `BANCA` ADD CONSTRAINT `PIANO_CONTI_BANCA` 
    FOREIGN KEY (`id_pianoConti`) REFERENCES `PIANO_CONTI` (`id_pianoConti`);

ALTER TABLE `MOV_CONTABILE` ADD CONSTRAINT `CAUSALE_CONTABILE_MOV_CONTABILE` 
    FOREIGN KEY (`id_causaleContabile`) REFERENCES `CAUSALE_CONTABILE` (`id_causaleContabile`);

ALTER TABLE `RIGA_MOV_CONTABILE` ADD CONSTRAINT `MOV_CONTABILE_RIGA_MOV_CONTABILE` 
    FOREIGN KEY (`id_movContabile`) REFERENCES `MOV_CONTABILE` (`id_movContabile`);

ALTER TABLE `RIGA_MOV_CONTABILE` ADD CONSTRAINT `PIANO_CONTI_RIGA_MOV_CONTABILE` 
    FOREIGN KEY (`id_pianoConti`) REFERENCES `PIANO_CONTI` (`id_pianoConti`);

ALTER TABLE `RIGA_MOV_CONTABILE` ADD CONSTRAINT `CLIFOR_RIGA_MOV_CONTABILE` 
    FOREIGN KEY (`id_clifor`) REFERENCES `CLIFOR` (`id_clifor`);

ALTER TABLE `RIGA_MOV_CONTABILE` ADD CONSTRAINT `BANCA_RIGA_MOV_CONTABILE` 
    FOREIGN KEY (`id_banca`) REFERENCES `BANCA` (`id_banca`);

ALTER TABLE `RIGA_MOV_CONTABILE` ADD CONSTRAINT `DOCUMENTO_RIGA_MOV_CONTABILE` 
    FOREIGN KEY (`id_documento`) REFERENCES `DOCUMENTO` (`id_documento`);

ALTER TABLE `RIGA_MOV_CONTABILE` ADD CONSTRAINT `IVA_RIGA_MOV_CONTABILE` 
    FOREIGN KEY (`id_iva`) REFERENCES `IVA` (`id_iva`);

ALTER TABLE `RIGA_CAUSALE_CONTABILE` ADD CONSTRAINT `PIANO_CONTI_RIGA_CAUSALE_CONTABILE` 
    FOREIGN KEY (`id_pianoConti`) REFERENCES `PIANO_CONTI` (`id_pianoConti`);

ALTER TABLE `RIGA_CAUSALE_CONTABILE` ADD CONSTRAINT `CAUSALE_CONTABILE_RIGA_CAUSALE_CONTABILE` 
    FOREIGN KEY (`id_causaleContabile`) REFERENCES `CAUSALE_CONTABILE` (`id_causaleContabile`);
