SET TERM ^ ;

CREATE OR ALTER trigger ttbestoque_minimo_b9u for tbestoque_minimo
active before update position 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE Grade Integer;
DECLARE VARIABLE G01 VarChar(10); DECLARE VARIABLE G02 VarChar(10);
DECLARE VARIABLE G03 VarChar(10); DECLARE VARIABLE G04 VarChar(10);
DECLARE VARIABLE G05 VarChar(10); DECLARE VARIABLE G06 VarChar(10);
DECLARE VARIABLE G07 VarChar(10); DECLARE VARIABLE G08 VarChar(10);
DECLARE VARIABLE G09 VarChar(10); DECLARE VARIABLE G10 VarChar(10);
DECLARE VARIABLE G11 VarChar(10); DECLARE VARIABLE G12 VarChar(10);
DECLARE VARIABLE G13 VarChar(10); DECLARE VARIABLE G14 VarChar(10);
DECLARE VARIABLE G15 VarChar(10); DECLARE VARIABLE G16 VarChar(10);
DECLARE VARIABLE G17 VarChar(10); DECLARE VARIABLE G18 VarChar(10);
DECLARE VARIABLE G19 VarChar(10); DECLARE VARIABLE G20 VarChar(10);
begin

  For
  Select Coalesce(P.Grade_Codigo,0),
         Coalesce(G.T01,''), Coalesce(G.T02,''), Coalesce(G.T03,''), Coalesce(G.T04,''),
         Coalesce(G.T05,''), Coalesce(G.T06,''), Coalesce(G.T07,''), Coalesce(G.T08,''),
         Coalesce(G.T09,''), Coalesce(G.T10,''), Coalesce(G.T11,''), Coalesce(G.T12,''),
         Coalesce(G.T13,''), Coalesce(G.T14,''), Coalesce(G.T15,''), Coalesce(G.T16,''),
         Coalesce(G.T17,''), Coalesce(G.T18,''), Coalesce(G.T19,''), Coalesce(G.T20,'')
    From TbProduto P
   Left join TbGrade G On G.Codigo = P.Grade_Codigo
   Where P.Codigo = Old.Produto_Codigo
   Into :Grade,:G01,:G02,:G03,:G04,:G05,:G06,:G07,:G08,:G09,
     :G10,:G11,:G12,:G13,:G14,:G15,:G16,:G17,:G18,:G19,:G20
   DO

   HISTORICO = '';

    if (OLD.SEGURANCA <> NEW.SEGURANCA) then
       BEGIN
       HISTORICO = HISTORICO||' EST.SEG: '||OLD.SEGURANCA||'->'||NEW.SEGURANCA;
       END
    if (OLD.PONTO_REPOSICAO <> NEW.PONTO_REPOSICAO) then
       BEGIN
       HISTORICO = HISTORICO||' PTO.REP: '||OLD.PONTO_REPOSICAO||'->'||NEW.PONTO_REPOSICAO;
       END
    if (OLD.TIPO_REPOSICAO <> NEW.TIPO_REPOSICAO) then
       BEGIN
       HISTORICO = HISTORICO||' TP.REP: '||OLD.TIPO_REPOSICAO||'->'||NEW.TIPO_REPOSICAO;
       END
    if (OLD.HABILITA_KANBAN <> NEW.HABILITA_KANBAN) then
       BEGIN
       HISTORICO = HISTORICO||' HAB.KB: '||OLD.HABILITA_KANBAN||'->'||NEW.HABILITA_KANBAN;
       END
    if (OLD.KANBAN_LOCALIZACAO_ID <> NEW.KANBAN_LOCALIZACAO_ID) then
       BEGIN
       HISTORICO = HISTORICO||' LOC.KB: '||OLD.KANBAN_LOCALIZACAO_ID||'->'||NEW.KANBAN_LOCALIZACAO_ID;
       END

   if (:Grade = 0) then
       Begin
       if (OLD.QUANTIDADE <> NEW.QUANTIDADE) then
           BEGIN
           HISTORICO = HISTORICO||' MIN: '||OLD.QUANTIDADE||'->'||NEW.QUANTIDADE;
           END
       if (OLD.ESTOQUE_MAX <> NEW.ESTOQUE_MAX) then
           BEGIN
           HISTORICO = HISTORICO||' MAX: '||OLD.ESTOQUE_MAX||'->'||NEW.ESTOQUE_MAX;
           END
       End

   if (:Grade > 0) then
       Begin

      if (OLD.T01 <> NEW.T01) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G01||' MIN: '||OLD.T01||'->'||NEW.T01;
           END
    
       if (OLD.T02 <> NEW.T02) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G02||' MIN: '||OLD.T02||'->'||NEW.T02;
           END
    
       if (OLD.T03 <> NEW.T03) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G03||' MIN: '||OLD.T03||'->'||NEW.T03;
           END
    
       if (OLD.T04 <> NEW.T04) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G04||' MIN: '||OLD.T04||'->'||NEW.T04;
           END
    
       if (OLD.T05 <> NEW.T05) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G05||' MIN: '||OLD.T05||'->'||NEW.T05;
           END
    
       if (OLD.T06 <> NEW.T06) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G06||' MIN: '||OLD.T06||'->'||NEW.T06;
           END
    
       if (OLD.T07 <> NEW.T07) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G07||' MIN: '||OLD.T07||'->'||NEW.T07;
           END
    
       if (OLD.T08 <> NEW.T08) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G08||' MIN: '||OLD.T08||'->'||NEW.T08;
           END
    
       if (OLD.T09 <> NEW.T09) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G09||' MIN: '||OLD.T09||'->'||NEW.T09;
           END
    
       if (OLD.T10 <> NEW.T10) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G10||' MIN: '||OLD.T10||'->'||NEW.T10;
           END
    
       if (OLD.T11 <> NEW.T11) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G11||' MIN: '||OLD.T11||'->'||NEW.T11;
           END
    
       if (OLD.T12 <> NEW.T12) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G12||' MIN: '||OLD.T12||'->'||NEW.T12;
           END
    
       if (OLD.T13 <> NEW.T13) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G13||' MIN: '||OLD.T13||'->'||NEW.T13;
           END
    
       if (OLD.T14 <> NEW.T14) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G14||' MIN: '||OLD.T14||'->'||NEW.T14;
           END
    
       if (OLD.T15 <> NEW.T15) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G15||' MIN: '||OLD.T15||'->'||NEW.T15;
           END
    
       if (OLD.T16 <> NEW.T16) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G16||' MIN: '||OLD.T16||'->'||NEW.T16;
           END
    
       if (OLD.T17 <> NEW.T17) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G17||' MIN: '||OLD.T17||'->'||NEW.T17;
           END
    
       if (OLD.T18 <> NEW.T18) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G18||' MIN: '||OLD.T18||'->'||NEW.T18;
           END
    
       if (OLD.T19 <> NEW.T19) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G19||' MIN: '||OLD.T19||'->'||NEW.T19;
           END
    
       if (OLD.T20 <> NEW.T20) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G20||' MIN: '||OLD.T20||'->'||NEW.T20;
           END
    
       if (OLD.M01 <> NEW.M01) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G01||' MAX: '||OLD.M01||'->'||NEW.M01;
           END
    
       if (OLD.M02 <> NEW.M02) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G02||' MAX: '||OLD.M02||'->'||NEW.M02;
           END
    
       if (OLD.M03 <> NEW.M03) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G03||' MAX: '||OLD.M03||'->'||NEW.M03;
           END
    
       if (OLD.M04 <> NEW.M04) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G04||' MAX: '||OLD.M04||'->'||NEW.M04;
           END
    
       if (OLD.M05 <> NEW.M05) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G05||' MAX: '||OLD.M05||'->'||NEW.M05;
           END
    
       if (OLD.M06 <> NEW.M06) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G06||' MAX: '||OLD.M06||'->'||NEW.M06;
           END
    
       if (OLD.M07 <> NEW.M07) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G07||' MAX: '||OLD.M07||'->'||NEW.M07;
           END
    
       if (OLD.M08 <> NEW.M08) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G08||' MAX: '||OLD.M08||'->'||NEW.M08;
           END
    
       if (OLD.M09 <> NEW.M09) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G09||' MAX: '||OLD.M09||'->'||NEW.M09;
           END
    
       if (OLD.M10 <> NEW.M10) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G10||' MAX: '||OLD.M10||'->'||NEW.M10;
           END
    
       if (OLD.M11 <> NEW.M11) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G11||' MAX: '||OLD.M11||'->'||NEW.M11;
           END
    
       if (OLD.M12 <> NEW.M12) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G12||' MAX: '||OLD.M12||'->'||NEW.M12;
           END
    
       if (OLD.M13 <> NEW.M13) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G13||' MAX: '||OLD.M13||'->'||NEW.M13;
           END
    
       if (OLD.M14 <> NEW.M14) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G14||' MAX: '||OLD.M14||'->'||NEW.M14;
           END
    
       if (OLD.M15 <> NEW.M15) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G15||' MAX: '||OLD.M15||'->'||NEW.M15;
           END
    
       if (OLD.M16 <> NEW.M16) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G16||' MAX: '||OLD.M16||'->'||NEW.M16;
           END
    
       if (OLD.M17 <> NEW.M17) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G17||' MAX: '||OLD.M17||'->'||NEW.M17;
           END
    
       if (OLD.M18 <> NEW.M18) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G18||' MAX: '||OLD.M18||'->'||NEW.M18;
           END
    
       if (OLD.M19 <> NEW.M19) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G19||' MAX: '||OLD.M19||'->'||NEW.M19;
           END
    
       if (OLD.M20 <> NEW.M20) then
           BEGIN
           HISTORICO = HISTORICO||' TAM.'||:G20||' MAX: '||OLD.M20||'->'||NEW.M20;
           END

       End

   if (HISTORICO <> '') Then
       Begin

        HISTORICO = 'REG.ALTERADO: EST - '||OLD.ESTABELECIMENTO_CODIGO||' /LOC - '||OLD.LOCALIZACAO_CODIGO||' '||HISTORICO;

        INSERT INTO TBHISTORICO (
        TABELA, TABELA_ID, USUARIO, IP, HISTORICO) VALUES (
        'TBESTOQUE_MINIMO', OLD.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

End
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger ttbestoque_minimo_b9i for tbestoque_minimo
active before insert position 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
begin

   HISTORICO = '';

   HISTORICO = HISTORICO||' REG.INCLUIDO: ';
   HISTORICO = HISTORICO||' EST.'||NEW.ESTABELECIMENTO_CODIGO;
   HISTORICO = HISTORICO||'/LOC.'||NEW.LOCALIZACAO_CODIGO;

   if (HISTORICO <> '') Then
       Begin
       INSERT INTO TBHISTORICO (
       TABELA, TABELA_ID, USUARIO, IP, HISTORICO) VALUES (
      'TBESTOQUE_MINIMO', NEW.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

End
^

SET TERM ; ^

SET TERM ^ ;

CREATE trigger ttbestoque_minimo_b09d for tbestoque_minimo
active before delete position 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';

    HISTORICO = HISTORICO||' REG.EXCLUIDO: ';
    HISTORICO = HISTORICO||' EST.'||OLD.ESTABELECIMENTO_CODIGO;
    HISTORICO = HISTORICO||'/LOC.'||OLD.LOCALIZACAO_CODIGO;

    IF (HISTORICO <> '') THEN
    BEGIN
       INSERT INTO TBHISTORICO (
       TABELA, TABELA_ID, USUARIO, IP, HISTORICO) VALUES (
      'TBESTOQUE_MINIMO', OLD.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
END
^

SET TERM ; ^

