/***********************************************************************************************/
/***********************************************************************************************/
/** TBMODELO_COMBINACAO **/

SET TERM ^ ;

CREATE OR ALTER trigger ttbmodelo_combinacao_b02i for tbmodelo_combinacao
active before insert position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' REGISTRO INCLUIDO';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO_COMBINACAO', NEW.ID, HISTORICO);
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger ttbmodelo_combinacao_b01u for tbmodelo_combinacao
active before update position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(5000);
DECLARE VARIABLE OLD_CATEGORIA VARCHAR(500);
DECLARE VARIABLE NEW_CATEGORIA VARCHAR(500);
BEGIN

    HISTORICO = '';

    OLD_CATEGORIA = COALESCE((SELECT FIRST 1 FN_LPAD(A.ID, 4, '0')||' - '||A.DESCRICAO FROM TBCATEGORIA_LACLOFIT A WHERE A.ID = OLD.CATEGORIA_ID), '');
    NEW_CATEGORIA = COALESCE((SELECT FIRST 1 FN_LPAD(A.ID, 4, '0')||' - '||A.DESCRICAO FROM TBCATEGORIA_LACLOFIT A WHERE A.ID = NEW.CATEGORIA_ID), '');

    IF (OLD.DESCRICAO IS DISTINCT FROM NEW.DESCRICAO) THEN
    BEGIN
        HISTORICO = HISTORICO||' DESCRICAO:'||OLD.DESCRICAO||'->'||NEW.DESCRICAO;
    END

    IF (OLD.STATUS IS DISTINCT FROM NEW.STATUS) THEN
    BEGIN
        HISTORICO = HISTORICO||' STATUS:'||OLD.STATUS||'->'||NEW.STATUS;
    END

    IF (OLD.HABILITA_ECOMMERCE IS DISTINCT FROM NEW.HABILITA_ECOMMERCE) THEN
    BEGIN
        HISTORICO = HISTORICO||' HAB.E-COMMERCE:'||OLD.HABILITA_ECOMMERCE||'->'||NEW.HABILITA_ECOMMERCE;
    END

    IF (OLD.CATEGORIA_ID IS DISTINCT FROM NEW.CATEGORIA_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||' CATEGORIA:'||OLD_CATEGORIA||'->'||NEW_CATEGORIA;
    END

    IF (OLD.OBSERVACAO IS DISTINCT FROM NEW.OBSERVACAO) THEN
    BEGIN
        HISTORICO = HISTORICO||' OBSERVACOES ALTERADAS!';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        HISTORICO = 'REGISTRO ALTERADO:'||HISTORICO;
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO_COMBINACAO', OLD.ID, FN_ELIPSE(HISTORICO, 500));
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger ttbmodelo_combinacao_b02u for tbmodelo_combinacao
active before update position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(5000);
BEGIN

    HISTORICO = '';

    IF (OLD.OBSERVACAO IS DISTINCT FROM NEW.OBSERVACAO) THEN
    BEGIN
        HISTORICO = HISTORICO||' '||FN_ELIPSE2(OLD.OBSERVACAO, 220)||'->'||FN_ELIPSE2(NEW.OBSERVACAO, 220);
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        HISTORICO = 'OBSERVACOES ALTERADAS:'||HISTORICO;
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO_COMBINACAO', OLD.ID, FN_ELIPSE(HISTORICO, 500));
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger ttbmodelo_combinacao_b01d for tbmodelo_combinacao
active before delete position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' REGISTRO EXCLUIDO';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO_COMBINACAO', OLD.ID, HISTORICO);
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger ttbmodelo_combinacao_b02d for tbmodelo_combinacao
active before delete position 1
AS
BEGIN

    DELETE FROM TBMODELO_COMBINACAO_MOD A WHERE A.COMBINACAO_ID = OLD.ID;       -- Excluir modelos vinculados ao agrupamento principal
    DELETE FROM TBMODELO_COMBINACAO_ITEM B WHERE B.COMBINACAO_ID = OLD.ID;      -- Excluir combinacoes vinculadas ao agrupamento principal

END
^

SET TERM ; ^

/***********************************************************************************************/
/***********************************************************************************************/
/** TBMODELO_COMBINACAO_MOD **/

SET TERM ^ ;

CREATE OR ALTER trigger ttbmodelo_combinacao_mod_b02i for tbmodelo_combinacao_mod
active before insert position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE NEW_MODELO VARCHAR(500);
BEGIN

    HISTORICO = '';

    NEW_MODELO = COALESCE((SELECT FIRST 1 FN_LPAD(A.CODIGO, 6, '0')||' - '||A.DESCRICAO FROM TBMODELO A WHERE A.CODIGO = NEW.MODELO_ID), '');

    IF (NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' MODELO: '||NEW_MODELO||' INCLUIDO';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO_COMBINACAO', NEW.COMBINACAO_ID, HISTORICO);
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger ttbmodelo_combinacao_mod_b01u for tbmodelo_combinacao_mod
active before update position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(5000);
DECLARE VARIABLE OLD_MODELO VARCHAR(500);
DECLARE VARIABLE NEW_MODELO VARCHAR(500);
BEGIN

    HISTORICO = '';

    OLD_MODELO = COALESCE((SELECT FIRST 1 FN_LPAD(A.CODIGO, 6, '0')||' - '||A.DESCRICAO FROM TBMODELO A WHERE A.CODIGO = OLD.MODELO_ID), '');
    NEW_MODELO = COALESCE((SELECT FIRST 1 FN_LPAD(A.CODIGO, 6, '0')||' - '||A.DESCRICAO FROM TBMODELO A WHERE A.CODIGO = NEW.MODELO_ID), '');

    IF (OLD.MODELO_ID IS DISTINCT FROM NEW.MODELO_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||' MODELO:'||OLD_MODELO||'->'||NEW_MODELO;
    END

    IF (OLD.QUANTIDADE IS DISTINCT FROM NEW.QUANTIDADE) THEN
    BEGIN
        HISTORICO = HISTORICO||' QTD:'||OLD.QUANTIDADE||'->'||NEW.QUANTIDADE;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        HISTORICO = 'MODELO: '||NEW_MODELO||' ALTERADO:'||HISTORICO;
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO_COMBINACAO', OLD.COMBINACAO_ID, FN_ELIPSE(HISTORICO, 500));
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger ttbmodelo_combinacao_mod_b01d for tbmodelo_combinacao_mod
active before delete position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE OLD_MODELO VARCHAR(500);
BEGIN

    HISTORICO = '';

    OLD_MODELO = COALESCE((SELECT FIRST 1 FN_LPAD(A.CODIGO, 6, '0')||' - '||A.DESCRICAO FROM TBMODELO A WHERE A.CODIGO = OLD.MODELO_ID), '');

    IF (OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' MODELO: '||OLD_MODELO||' EXCLUIDO';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO_COMBINACAO', OLD.COMBINACAO_ID, HISTORICO);
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger ttbmodelo_combinacao_mod_b02d for tbmodelo_combinacao_mod
active before delete position 1
AS
BEGIN

    DELETE FROM TBMODELO_COMBINACAO_COR A WHERE A.MODELO_ID = OLD.MODELO_ID AND A.COMBINACAO_ID = OLD.COMBINACAO_ID;    -- Excluir itens das combinacoes que utilizam este modelo

END
^

SET TERM ; ^

/***********************************************************************************************/
/***********************************************************************************************/
/** TBMODELO_COMBINACAO_ITEM **/

SET TERM ^ ;

CREATE OR ALTER trigger ttbmodelo_combinacao_item_b02i for tbmodelo_combinacao_item
active before insert position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' COMBINACAO: '||NEW.DESCRICAO||' INCLUIDA';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO_COMBINACAO', NEW.COMBINACAO_ID, HISTORICO);
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger ttbmodelo_combinacao_item_b01u for tbmodelo_combinacao_item
active before update position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(5000);
BEGIN

    HISTORICO = '';

    IF (OLD.DESCRICAO IS DISTINCT FROM NEW.DESCRICAO) THEN
    BEGIN
        HISTORICO = HISTORICO||' DESCRICAO:'||OLD.DESCRICAO||'->'||NEW.DESCRICAO;
    END

    IF (OLD.QUANTIDADE IS DISTINCT FROM NEW.QUANTIDADE) THEN
    BEGIN
        HISTORICO = HISTORICO||' QTD:'||OLD.QUANTIDADE||'->'||NEW.QUANTIDADE;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        HISTORICO = 'COMBINACAO: '||NEW.DESCRICAO||' ALTERADA:'||HISTORICO;
        EXECUTE PROCEDURE SPI_HISTORICO('', OLD.COMBINACAO_ID, FN_ELIPSE(HISTORICO, 500));
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger ttbmodelo_combinacao_item_b01d for tbmodelo_combinacao_item
active before delete position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' COMBINACAO: '||OLD.DESCRICAO||' EXCLUIDO';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO_COMBINACAO', OLD.COMBINACAO_ID, HISTORICO);
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger ttbmodelo_combinacao_item_b02d for tbmodelo_combinacao_item
active before delete position 1
AS
BEGIN

    DELETE FROM TBMODELO_COMBINACAO_COR A WHERE A.COMBINACAO_ITEM_ID = OLD.ID AND A.COMBINACAO_ID = OLD.COMBINACAO_ID;  -- Exclui os itens da combinacao

END
^

SET TERM ; ^

/***********************************************************************************************/
/***********************************************************************************************/
/** TBMODELO_COMBINACAO_ITEM **/

SET TERM ^ ;

CREATE OR ALTER trigger ttbmodelo_combinacao_cor_b02i for tbmodelo_combinacao_cor
active before insert position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE COMBINACAO VARCHAR(500);
DECLARE VARIABLE NEW_MODELO VARCHAR(500);
DECLARE VARIABLE NEW_COR VARCHAR(500);
BEGIN

    HISTORICO = '';
    COMBINACAO = COALESCE((SELECT FIRST 1 FN_LPAD(A.ID, 4, '0')||' - '||A.DESCRICAO FROM TBMODELO_COMBINACAO_ITEM A WHERE A.ID = NEW.COMBINACAO_ITEM_ID AND A.COMBINACAO_ID = NEW.COMBINACAO_ID), '');

    NEW_MODELO = COALESCE((SELECT FIRST 1 FN_LPAD(B.CODIGO, 6, '0')||' - '||B.DESCRICAO FROM TBMODELO B WHERE B.CODIGO = NEW.MODELO_ID), '');
    NEW_COR = COALESCE((SELECT FIRST 1 FN_LPAD(C.CODIGO, 6, '0')||' - '||C.DESCRICAO FROM TBCOR C WHERE C.CODIGO = NEW.COR_ID), '');

    IF (NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' MODELO: '||NEW_MODELO||'/COR: '||NEW_COR||' INCLUIDA NA COMBINACAO: '||COMBINACAO||'';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO_COMBINACAO', NEW.COMBINACAO_ID, HISTORICO);
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger ttbmodelo_combinacao_cor_b01u for tbmodelo_combinacao_cor
active before update position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(5000);
DECLARE VARIABLE COMBINACAO VARCHAR(500);
DECLARE VARIABLE OLD_MODELO VARCHAR(500);
DECLARE VARIABLE NEW_MODELO VARCHAR(500);
DECLARE VARIABLE OLD_COR VARCHAR(500);
DECLARE VARIABLE NEW_COR VARCHAR(500);
BEGIN

    HISTORICO = '';
    COMBINACAO = COALESCE((SELECT FIRST 1 FN_LPAD(A.ID, 4, '0')||' - '||A.DESCRICAO FROM TBMODELO_COMBINACAO_ITEM A WHERE A.ID = OLD.COMBINACAO_ITEM_ID AND A.COMBINACAO_ID = OLD.COMBINACAO_ID), '');

    OLD_MODELO = COALESCE((SELECT FIRST 1 FN_LPAD(B.CODIGO, 6, '0')||' - '||B.DESCRICAO FROM TBMODELO B WHERE B.CODIGO = OLD.MODELO_ID), '');
    NEW_MODELO = COALESCE((SELECT FIRST 1 FN_LPAD(B.CODIGO, 6, '0')||' - '||B.DESCRICAO FROM TBMODELO B WHERE B.CODIGO = NEW.MODELO_ID), '');
    OLD_COR = COALESCE((SELECT FIRST 1 FN_LPAD(C.CODIGO, 6, '0')||' - '||C.DESCRICAO FROM TBCOR C WHERE C.CODIGO = OLD.COR_ID), '');
    NEW_COR = COALESCE((SELECT FIRST 1 FN_LPAD(C.CODIGO, 6, '0')||' - '||C.DESCRICAO FROM TBCOR C WHERE C.CODIGO = NEW.COR_ID), '');

    IF (OLD.MODELO_ID IS DISTINCT FROM NEW.MODELO_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||' MODELO:'||OLD_MODELO||'->'||NEW_MODELO;
    END

    IF (OLD.COR_ID IS DISTINCT FROM NEW.COR_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||' COR:'||OLD_COR||'->'||NEW_COR;
    END

    IF (OLD.QUANTIDADE IS DISTINCT FROM NEW.QUANTIDADE) THEN
    BEGIN
        HISTORICO = HISTORICO||' QTD:'||OLD.QUANTIDADE||'->'||NEW.QUANTIDADE;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        HISTORICO = 'MODELO: '||NEW_MODELO||'/COR: '||NEW_COR||' DA COMBINACAO: '||COMBINACAO||' ALTERADO:'||HISTORICO;
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO_COMBINACAO', OLD.COMBINACAO_ID, FN_ELIPSE(HISTORICO, 500));
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger ttbmodelo_combinacao_cor_b01d for tbmodelo_combinacao_cor
active before delete position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE COMBINACAO VARCHAR(500);
DECLARE VARIABLE OLD_MODELO VARCHAR(500);
DECLARE VARIABLE OLD_COR VARCHAR(500);
BEGIN

    HISTORICO = '';
    COMBINACAO = COALESCE((SELECT FIRST 1 FN_LPAD(A.ID, 4, '0')||' - '||A.DESCRICAO FROM TBMODELO_COMBINACAO_ITEM A WHERE A.ID = OLD.COMBINACAO_ITEM_ID AND A.COMBINACAO_ID = OLD.COMBINACAO_ID), '');

    OLD_MODELO = COALESCE((SELECT FIRST 1 FN_LPAD(B.CODIGO, 6, '0')||' - '||B.DESCRICAO FROM TBMODELO B WHERE B.CODIGO = OLD.MODELO_ID), '');
    OLD_COR = COALESCE((SELECT FIRST 1 FN_LPAD(C.CODIGO, 6, '0')||' - '||C.DESCRICAO FROM TBCOR C WHERE C.CODIGO = OLD.COR_ID), '');

    IF (OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' MODELO: '||OLD_MODELO||'/COR: '||OLD_COR||' EXCLUIDA NA COMBINACAO: '||COMBINACAO||'';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMODELO_COMBINACAO', OLD.COMBINACAO_ID, HISTORICO);
    END
END
^

SET TERM ; ^

