SET TERM ^ ;

CREATE trigger ttbproduto_aiud_api for tbproduto
active after insert or update or delete position 99
AS
begin
    if(inserting or updating)then
    begin
        if(new.status is distinct from old.status and new.status = '0')then
        begin
            if(inserting)then execute procedure spi_integracao_api('TBPRODUTO_INATIVO',new.codigo,'I');
            if(updating )then execute procedure spi_integracao_api('TBPRODUTO_INATIVO',new.codigo,'A');
        end
    end

    if(deleting )then execute procedure spi_integracao_api('TBPRODUTO_INATIVO',old.codigo,'D');
end
^

SET TERM ; ^


SET TERM ^ ;

CREATE OR ALTER trigger "TTBMODELO_COR_biud_API" for tbmodelo_cor
active after insert or update or delete position 999
AS
begin
    if(inserting)then execute procedure spi_integracao_api('TBMODELO',new.modelo_id,'I');
    if(updating )then execute procedure spi_integracao_api('TBMODELO',new.modelo_id,'A');
    if(deleting )then execute procedure spi_integracao_api('TBMODELO',old.modelo_id,'D');
end
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger ttbmodelo_cor_b0d for tbmodelo_cor
active before delete position 99
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
begin

       DELETE FROM TBPRODUTO WHERE MODELO_CODIGO = OLD.MODELO_ID AND COR_CODIGO = OLD.COR_ID;

       HISTORICO = 'Cor: '||OLD.COR_ID||'-'||Coalesce((Select First 1 C.descricao From TbCor C Where C.Codigo = Old.Cor_Id),'')||' excluída.';

       INSERT INTO TBHISTORICO (
       TABELA, TABELA_ID, USUARIO, IP, HISTORICO) VALUES (
      'TBMODELO', OLD.MODELO_ID,
      (Select First 1 MON$USER from mon$attachments where mon$attachment_id = current_connection),
      (Select First 1 MON$REMOTE_ADDRESS from mon$attachments where mon$attachment_id = current_connection),
       :Historico);
end
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger ttbmodelo_cor_b997iud for tbmodelo_cor
active before insert or update or delete position 98
AS
declare variable codigo integer;
begin
    if(inserting)then execute procedure spi_integracao_mobile('TBMODELO_COR',new.id,'I');
    if(updating )then execute procedure spi_integracao_mobile('TBMODELO_COR',new.id,'A');
    if(deleting )then execute procedure spi_integracao_mobile('TBMODELO_COR',old.id,'D');

    if(inserting or updating)then
    begin
        if(new.status_produto is distinct from old.status_produto and new.status_produto = '0')then
        begin
            codigo = coalesce((select first 1 p.codigo from tbproduto p where p.modelo_codigo = new.modelo_id and p.cor_codigo = new.cor_id),0);
    
            if(codigo > 0)then
            begin
                if(inserting)then execute procedure spi_integracao_api('TBPRODUTO_INATIVO',:codigo,'I');
                if(updating )then execute procedure spi_integracao_api('TBPRODUTO_INATIVO',:codigo,'A');
            end
        end
    end

    if(deleting)then
    begin
        codigo = coalesce((select first 1 p.codigo from tbproduto p where p.modelo_codigo = old.modelo_id and p.cor_codigo = old.cor_id),0);

        if(codigo > 0)then
        begin
            if(deleting )then execute procedure spi_integracao_api('TBPRODUTO_INATIVO',:codigo,'D');
        end
    end
    

end
^

SET TERM ; ^


