CREATE OR ALTER trigger ttbnfs_item_b1i_01 for tbnfs_item
active before insert position 1
AS
DECLARE VARIABLE EXEC CHAR(3);
Begin
For
Select First 1 IIF (A.Natureza=1 AND A.SITUACAO=2 , 'SIM', IIF (A.Natureza=2 AND A.SITUACAO=1, 'SIM', 'NAO'))
FROM TBNFS A
WHERE A.CONTROLE = NEW.NFS_CONTROLE
  AND(NEW.Nfe_Item_Controle > 0 or
      NEW.NF_EXTERNO_ITEM_CONTROLE > 0 or
      NEW.NF_CONSIGNACAO_ITEM > 0)
Into :EXEC 
Do
begin

IF(COALESCE(NEW.GC_WEB, '0') = '0') THEN
  BEGIN
    if (:EXEC = 'SIM') then
       BEGIN
       if (NEW.Nfe_Item_Controle > 0) then
           Begin
              Update TbNfE_Item_Saldo Set
              Quantidade = Quantidade - NEW.Quantidade,
              T01 = T01 - NEW.T01, T02 = T02 - NEW.T02, T03 = T03 - NEW.T03, T04 = T04 - NEW.T04,
              T05 = T05 - NEW.T05, T06 = T06 - NEW.T06, T07 = T07 - NEW.T07, T08 = T08 - NEW.T08,
              T09 = T09 - NEW.T09, T10 = T10 - NEW.T10, T11 = T11 - NEW.T11, T12 = T12 - NEW.T12,
              T13 = T13 - NEW.T13, T14 = T14 - NEW.T14, T15 = T15 - NEW.T15, T16 = T16 - NEW.T16,
              T17 = T17 - NEW.T17, T18 = T18 - NEW.T18, T19 = T19 - NEW.T19, T20 = T20 - NEW.T20
              Where Nfe_Item_Controle = NEW.Nfe_Item_Controle;
            End
    
       if (NEW.NF_EXTERNO_ITEM_CONTROLE > 0) then
           Begin
              Update TbNfE_Item_Saldo Set
              Quantidade = Quantidade - NEW.Quantidade,
              T01 = T01 - NEW.T01, T02 = T02 - NEW.T02, T03 = T03 - NEW.T03, T04 = T04 - NEW.T04,
              T05 = T05 - NEW.T05, T06 = T06 - NEW.T06, T07 = T07 - NEW.T07, T08 = T08 - NEW.T08,
              T09 = T09 - NEW.T09, T10 = T10 - NEW.T10, T11 = T11 - NEW.T11, T12 = T12 - NEW.T12,
              T13 = T13 - NEW.T13, T14 = T14 - NEW.T14, T15 = T15 - NEW.T15, T16 = T16 - NEW.T16,
              T17 = T17 - NEW.T17, T18 = T18 - NEW.T18, T19 = T19 - NEW.T19, T20 = T20 - NEW.T20
              Where Nfe_Item_Controle = NEW.NF_EXTERNO_ITEM_CONTROLE;
           End
    
       if (NEW.NF_CONSIGNACAO_ITEM > 0) then
           Begin
              Update TbNfS_Item_Saldo Set
              Quantidade = Quantidade - NEW.Quantidade,
              T01 = T01 - NEW.T01, T02 = T02 - NEW.T02, T03 = T03 - NEW.T03, T04 = T04 - NEW.T04,
              T05 = T05 - NEW.T05, T06 = T06 - NEW.T06, T07 = T07 - NEW.T07, T08 = T08 - NEW.T08,
              T09 = T09 - NEW.T09, T10 = T10 - NEW.T10, T11 = T11 - NEW.T11, T12 = T12 - NEW.T12,
              T13 = T13 - NEW.T13, T14 = T14 - NEW.T14, T15 = T15 - NEW.T15, T16 = T16 - NEW.T16,
              T17 = T17 - NEW.T17, T18 = T18 - NEW.T18, T19 = T19 - NEW.T19, T20 = T20 - NEW.T20
              Where NfS_Item_Controle = NEW.NF_CONSIGNACAO_ITEM;
           End
        END
    end
  end

End;

CREATE OR ALTER trigger ttbnfe_item_b1i_01 for tbnfe_item
active before insert position 1
AS
DECLARE VARIABLE EXEC CHAR(3);
Begin
For
Select First 1 IIF (A.Natureza=1 AND A.SITUACAO=2 , 'SIM', IIF (A.Natureza=2 AND A.SITUACAO=1, 'SIM', 'NAO'))
FROM TBNFe A
WHERE A.CONTROLE = NEW.NFe_CONTROLE
  AND(NEW.Nfs_Item_Controle > 0 or
      NEW.NF_EXTERNO_ITEM_CONTROLE > 0)
    /*NEW.NF_CONSIGNACAO_ITEM > 0)*/
Into :EXEC 
Do
begin

IF(COALESCE(NEW.GC_WEB,  '0') = '0') THEN
BEGIN

        if (:EXEC = 'SIM') then
           BEGIN
           if (NEW.Nfs_Item_Controle > 0) then
               Begin
                  Update TbNfs_Item_Saldo Set
                  Quantidade = Quantidade - NEW.Quantidade,
                  T01 = T01 - NEW.T01, T02 = T02 - NEW.T02, T03 = T03 - NEW.T03, T04 = T04 - NEW.T04,
                  T05 = T05 - NEW.T05, T06 = T06 - NEW.T06, T07 = T07 - NEW.T07, T08 = T08 - NEW.T08,
                  T09 = T09 - NEW.T09, T10 = T10 - NEW.T10, T11 = T11 - NEW.T11, T12 = T12 - NEW.T12,
                  T13 = T13 - NEW.T13, T14 = T14 - NEW.T14, T15 = T15 - NEW.T15, T16 = T16 - NEW.T16,
                  T17 = T17 - NEW.T17, T18 = T18 - NEW.T18, T19 = T19 - NEW.T19, T20 = T20 - NEW.T20
                  Where Nfs_Item_Controle = NEW.Nfs_Item_Controle;
                End
        
           if (NEW.NF_EXTERNO_ITEM_CONTROLE > 0) then
               Begin
                  Update TbNfs_Item_Saldo Set
                  Quantidade = Quantidade - NEW.Quantidade,
                  T01 = T01 - NEW.T01, T02 = T02 - NEW.T02, T03 = T03 - NEW.T03, T04 = T04 - NEW.T04,
                  T05 = T05 - NEW.T05, T06 = T06 - NEW.T06, T07 = T07 - NEW.T07, T08 = T08 - NEW.T08,
                  T09 = T09 - NEW.T09, T10 = T10 - NEW.T10, T11 = T11 - NEW.T11, T12 = T12 - NEW.T12,
                  T13 = T13 - NEW.T13, T14 = T14 - NEW.T14, T15 = T15 - NEW.T15, T16 = T16 - NEW.T16,
                  T17 = T17 - NEW.T17, T18 = T18 - NEW.T18, T19 = T19 - NEW.T19, T20 = T20 - NEW.T20
                  Where Nfs_Item_Controle = NEW.NF_EXTERNO_ITEM_CONTROLE;
               End
           /*
           if (NEW.NF_CONSIGNACAO_ITEM > 0) then
               Begin
                  Update TbNfe_Item_Saldo Set
                  Quantidade = Quantidade - NEW.Quantidade,
                  T01 = T01 - NEW.T01, T02 = T02 - NEW.T02, T03 = T03 - NEW.T03, T04 = T04 - NEW.T04,
                  T05 = T05 - NEW.T05, T06 = T06 - NEW.T06, T07 = T07 - NEW.T07, T08 = T08 - NEW.T08,
                  T09 = T09 - NEW.T09, T10 = T10 - NEW.T10, T11 = T11 - NEW.T11, T12 = T12 - NEW.T12,
                  T13 = T13 - NEW.T13, T14 = T14 - NEW.T14, T15 = T15 - NEW.T15, T16 = T16 - NEW.T16,
                  T17 = T17 - NEW.T17, T18 = T18 - NEW.T18, T19 = T19 - NEW.T19, T20 = T20 - NEW.T20
                  Where Nfe_Item_Controle = NEW.NF_CONSIGNACAO_ITEM;
               End */
            END
        end
    END
End;