UPDATE OR INSERT INTO TBPARAMETRO (TABELA, CODIGO, DESCRICAO, VALOR, OBSERVACAO, GRUPO, DATAHORA, USUARIO_ID)
                           VALUES ('SISTEMA', 'EAN_AUTO_INTERVALO', 'PREENCHER EAN CONSIDERANDO INTERVALO', '1', 'PREENCHER BRECHAS DO CODIGO EAN
0 - NAO / 1 - SIM;', 'WEB22430', '2021-11-04 18:49:43', 837)
                         MATCHING (TABELA, CODIGO);

COMMIT WORK;

CREATE OR ALTER trigger ttbnfe_item_saldo_b0u_02 for tbnfe_item_saldo
active before update position 2
AS
DECLARE VARIABLE VNFS_ITEM_CONTROLE INTEGER;
DECLARE VARIABLE VNF_EXTERNO_ITEM_CONTROLE INTEGER;
DECLARE VARIABLE QUANTIDADE NUMERIC(10,5);
DECLARE VARIABLE T01 NUMERIC(10,5);
DECLARE VARIABLE T02 NUMERIC(10,5);
DECLARE VARIABLE T03 NUMERIC(10,5);
DECLARE VARIABLE T04 NUMERIC(10,5);
DECLARE VARIABLE T05 NUMERIC(10,5);
DECLARE VARIABLE T06 NUMERIC(10,5);
DECLARE VARIABLE T07 NUMERIC(10,5);
DECLARE VARIABLE T08 NUMERIC(10,5);
DECLARE VARIABLE T09 NUMERIC(10,5);
DECLARE VARIABLE T10 NUMERIC(10,5);
DECLARE VARIABLE T11 NUMERIC(10,5);
DECLARE VARIABLE T12 NUMERIC(10,5);
DECLARE VARIABLE T13 NUMERIC(10,5);
DECLARE VARIABLE T14 NUMERIC(10,5);
DECLARE VARIABLE T15 NUMERIC(10,5);
DECLARE VARIABLE T16 NUMERIC(10,5);
DECLARE VARIABLE T17 NUMERIC(10,5);
DECLARE VARIABLE T18 NUMERIC(10,5);
DECLARE VARIABLE T19 NUMERIC(10,5);
DECLARE VARIABLE T20 NUMERIC(10,5);
BEGIN

  IF (OLD.QUANTIDADE <> NEW.QUANTIDADE OR
      OLD.T01 <> NEW.T01 OR OLD.T02 <> NEW.T02 OR
      OLD.T03 <> NEW.T03 OR OLD.T04 <> NEW.T04 OR
      OLD.T05 <> NEW.T05 OR OLD.T06 <> NEW.T06 OR
      OLD.T07 <> NEW.T07 OR OLD.T08 <> NEW.T08 OR
      OLD.T09 <> NEW.T09 OR OLD.T10 <> NEW.T10 OR
      OLD.T11 <> NEW.T11 OR OLD.T12 <> NEW.T12 OR
      OLD.T13 <> NEW.T13 OR OLD.T14 <> NEW.T14 OR
      OLD.T15 <> NEW.T15 OR OLD.T16 <> NEW.T16 OR
      OLD.T17 <> NEW.T17 OR OLD.T18 <> NEW.T18 OR
      OLD.T19 <> NEW.T19 OR OLD.T20 <> NEW.T20) THEN
      BEGIN

        FOR
        SELECT A.NFS_ITEM_CONTROLE, A.NF_EXTERNO_ITEM_CONTROLE
          FROM TBNFE_ITEM A
         WHERE A.CONTROLE = OLD.NFE_ITEM_CONTROLE
         AND ((A.NF_SITUACAO = 2 AND A.NF_NATUREZA = 1) OR (A.NF_SITUACAO = 1 AND A.NF_NATUREZA = 2))
          INTO :VNFS_ITEM_CONTROLE, :VNF_EXTERNO_ITEM_CONTROLE
            DO BEGIN
               IF (VNFS_ITEM_CONTROLE > 0 OR VNF_EXTERNO_ITEM_CONTROLE > 0) THEN
                   BEGIN
                     UPDATE TBNFS_ITEM_SALDO B SET
                     B.QUANTIDADE = QUANTIDADE + (OLD.QUANTIDADE - NEW.QUANTIDADE),
                     B.T01 = T01 + (OLD.T01 - NEW.T01),
                     B.T02 = T02 + (OLD.T02 - NEW.T02),
                     B.T03 = T03 + (OLD.T03 - NEW.T03),
                     B.T04 = T04 + (OLD.T04 - NEW.T04),
                     B.T05 = T05 + (OLD.T05 - NEW.T05),
                     B.T06 = T06 + (OLD.T06 - NEW.T06),
                     B.T07 = T07 + (OLD.T07 - NEW.T07),
                     B.T08 = T08 + (OLD.T08 - NEW.T08),
                     B.T09 = T09 + (OLD.T09 - NEW.T09),
                     B.T10 = T10 + (OLD.T10 - NEW.T10),
                     B.T11 = T11 + (OLD.T11 - NEW.T11),
                     B.T12 = T12 + (OLD.T12 - NEW.T12),
                     B.T13 = T13 + (OLD.T13 - NEW.T13),
                     B.T14 = T14 + (OLD.T14 - NEW.T14),
                     B.T15 = T15 + (OLD.T15 - NEW.T15),
                     B.T16 = T16 + (OLD.T16 - NEW.T16),
                     B.T17 = T17 + (OLD.T17 - NEW.T17),
                     B.T18 = T18 + (OLD.T18 - NEW.T18),
                     B.T19 = T19 + (OLD.T19 - NEW.T19),
                     B.T20 = T20 + (OLD.T20 - NEW.T20)
                     WHERE B.NFS_ITEM_CONTROLE = IIF(:VNFS_ITEM_CONTROLE>0,:VNFS_ITEM_CONTROLE,:VNF_EXTERNO_ITEM_CONTROLE);
                   END
               END
      END

END;

DROP TRIGGER TTBNFSE_ITEM_SALDO_B0U_02;

CREATE OR ALTER trigger ttbnfs_item_saldo_b0u_02 for tbnfs_item_saldo
active before update position 2
AS
declare variable vNFE_ITEM_CONTROLE integer;
declare variable vNF_EXTERNO_ITEM_CONTROLE integer;
declare variable NF_CONSIGNACAO_ITEM integer;
declare variable QUANTIDADE numeric(10,5);
declare variable T01 numeric(10,5);
declare variable T02 numeric(10,5);
declare variable T03 numeric(10,5);
declare variable T04 numeric(10,5);
declare variable T05 numeric(10,5);
declare variable T06 numeric(10,5);
declare variable T07 numeric(10,5);
declare variable T08 numeric(10,5);
declare variable T09 numeric(10,5);
declare variable T10 numeric(10,5);
declare variable T11 numeric(10,5);
declare variable T12 numeric(10,5);
declare variable T13 numeric(10,5);
declare variable T14 numeric(10,5);
declare variable T15 numeric(10,5);
declare variable T16 numeric(10,5);
declare variable T17 numeric(10,5);
declare variable T18 numeric(10,5);
declare variable T19 numeric(10,5);
declare variable T20 numeric(10,5);
Begin

  if (old.quantidade <> new.quantidade or
      old.t01 <> new.t01 or old.t02 <> new.t02 or
      old.t03 <> new.t03 or old.t04 <> new.t04 or
      old.t05 <> new.t05 or old.t06 <> new.t06 or
      old.t07 <> new.t07 or old.t08 <> new.t08 or
      old.t09 <> new.t09 or old.t10 <> new.t10 or
      old.t11 <> new.t11 or old.t12 <> new.t12 or
      old.t13 <> new.t13 or old.t14 <> new.t14 or
      old.t15 <> new.t15 or old.t16 <> new.t16 or
      old.t17 <> new.t17 or old.t18 <> new.t18 or
      old.t19 <> new.t19 or old.t20 <> new.t20) then
      BEGIN

        FOR
        SELECT A.NFE_ITEM_CONTROLE, A.NF_EXTERNO_ITEM_CONTROLE, A.NF_CONSIGNACAO_ITEM
          FROM TBNFS_ITEM A
         WHERE A.CONTROLE = OLD.NFS_ITEM_CONTROLE
         AND ((A.NF_SITUACAO = 2 AND A.NF_NATUREZA = 1) OR (A.NF_SITUACAO = 1 AND A.NF_NATUREZA = 2))
          INTO :VNFE_ITEM_CONTROLE, :VNF_EXTERNO_ITEM_CONTROLE, :NF_CONSIGNACAO_ITEM
            DO BEGIN
               IF (VNFE_ITEM_CONTROLE > 0 OR VNF_EXTERNO_ITEM_CONTROLE > 0) THEN
                   BEGIN
                     UPDATE TBNFE_ITEM_SALDO B SET
                     B.QUANTIDADE = QUANTIDADE + (OLD.QUANTIDADE - NEW.QUANTIDADE),
                     B.T01 = T01 + (OLD.T01 - NEW.T01),
                     B.T02 = T02 + (OLD.T02 - NEW.T02),
                     B.T03 = T03 + (OLD.T03 - NEW.T03),
                     B.T04 = T04 + (OLD.T04 - NEW.T04),
                     B.T05 = T05 + (OLD.T05 - NEW.T05),
                     B.T06 = T06 + (OLD.T06 - NEW.T06),
                     B.T07 = T07 + (OLD.T07 - NEW.T07),
                     B.T08 = T08 + (OLD.T08 - NEW.T08),
                     B.T09 = T09 + (OLD.T09 - NEW.T09),
                     B.T10 = T10 + (OLD.T10 - NEW.T10),
                     B.T11 = T11 + (OLD.T11 - NEW.T11),
                     B.T12 = T12 + (OLD.T12 - NEW.T12),
                     B.T13 = T13 + (OLD.T13 - NEW.T13),
                     B.T14 = T14 + (OLD.T14 - NEW.T14),
                     B.T15 = T15 + (OLD.T15 - NEW.T15),
                     B.T16 = T16 + (OLD.T16 - NEW.T16),
                     B.T17 = T17 + (OLD.T17 - NEW.T17),
                     B.T18 = T18 + (OLD.T18 - NEW.T18),
                     B.T19 = T19 + (OLD.T19 - NEW.T19),
                     B.T20 = T20 + (OLD.T20 - NEW.T20)
                     WHERE B.NFE_ITEM_CONTROLE = IIF(:VNFE_ITEM_CONTROLE>0,:VNFE_ITEM_CONTROLE,:VNF_EXTERNO_ITEM_CONTROLE);
                   END ELSE
               IF (NF_CONSIGNACAO_ITEM > 0) THEN
                   BEGIN
                     UPDATE TBNFS_ITEM_SALDO B SET
                     B.QUANTIDADE = QUANTIDADE + (OLD.QUANTIDADE - NEW.QUANTIDADE),
                     B.T01 = T01 + (OLD.T01 - NEW.T01),
                     B.T02 = T02 + (OLD.T02 - NEW.T02),
                     B.T03 = T03 + (OLD.T03 - NEW.T03),
                     B.T04 = T04 + (OLD.T04 - NEW.T04),
                     B.T05 = T05 + (OLD.T05 - NEW.T05),
                     B.T06 = T06 + (OLD.T06 - NEW.T06),
                     B.T07 = T07 + (OLD.T07 - NEW.T07),
                     B.T08 = T08 + (OLD.T08 - NEW.T08),
                     B.T09 = T09 + (OLD.T09 - NEW.T09),
                     B.T10 = T10 + (OLD.T10 - NEW.T10),
                     B.T11 = T11 + (OLD.T11 - NEW.T11),
                     B.T12 = T12 + (OLD.T12 - NEW.T12),
                     B.T13 = T13 + (OLD.T13 - NEW.T13),
                     B.T14 = T14 + (OLD.T14 - NEW.T14),
                     B.T15 = T15 + (OLD.T15 - NEW.T15),
                     B.T16 = T16 + (OLD.T16 - NEW.T16),
                     B.T17 = T17 + (OLD.T17 - NEW.T17),
                     B.T18 = T18 + (OLD.T18 - NEW.T18),
                     B.T19 = T19 + (OLD.T19 - NEW.T19),
                     B.T20 = T20 + (OLD.T20 - NEW.T20)
                     WHERE B.NFS_ITEM_CONTROLE = :NF_CONSIGNACAO_ITEM;
                   END
               END
      END

End;