SET TERM ^ ;

CREATE OR ALTER trigger ttbmodelo_kit_b02i for tbmodelo_kit
active before insert position 2
AS
declare variable COR_ID integer;
begin

    IF(NEW.COR_ID = 0) THEN
    BEGIN
        :COR_ID = GEN_ID(GTBCOR,1);
        INSERT INTO TBCOR (DESCRICAO, CODIGO, TONALIDADE, STATUS, DESCRICAO_COMERCIAL, KIT, CLASSE, NIVEL_CONTAMINACAO) VALUES (NEW.DESCRICAO, :COR_ID, 0, 1, NEW.DESCRICAO, '1', 0, 0);
        INSERT INTO TBMODELO_COR (MODELO_ID, COR_ID, STATUS, STATUS_PRODUTO) VALUES (NEW.MODELO_ID, :COR_ID, '1', '1');

        NEW.COR_ID = :COR_ID;
    END ELSE
    BEGIN
        INSERT INTO TBMODELO_COR (MODELO_ID, COR_ID, STATUS, STATUS_PRODUTO) VALUES (NEW.MODELO_ID, NEW.COR_ID, '1', '1');
    END
end
^

SET TERM ; ^

SET TERM ^ ;

CREATE trigger ttbmodelo_kit_a01d for tbmodelo_kit
active after delete position 0
AS
begin
    DELETE FROM TBMODELO_COR MC WHERE MC.MODELO_ID = OLD.MODELO_ID AND MC.COR_ID = OLD.COR_ID;
end
^

SET TERM ; ^

