create or replace TRIGGER INSEREVALIDACAOADQ 
AFTER INSERT ON ADQUIRENTE
FOR EACH ROW
DECLARE
   V_SEQVARCHAR VARCHAR2(50);
   V_SEQUENCIAL NUMBER;
   V_ORGANIZACAO NUMBER;
   V_OPERADOR NUMBER;
   V_IDUTCLOUD VARCHAR2(01);
   V_NRORG NUMBER;
BEGIN
   BEGIN
      SELECT COUNT(1)
        INTO V_ORGANIZACAO
        FROM ORGANIZACAO;
        
      IF (V_ORGANIZACAO > 1) THEN
         BEGIN
            /*Verifica a tabela OPERADOR*/
            SELECT COUNT(1)
            INTO V_OPERADOR
            FROM OPERADOR;
            
            IF (V_OPERADOR > 0) THEN
                SELECT COUNT(1) 
                  INTO V_IDUTCLOUD
                  FROM PARAMGERAL
                 WHERE ROWNUM = 1;
                SELECT IDUTCLOUD
                  INTO V_IDUTCLOUD
                  FROM PARAMGERAL
                 WHERE ROWNUM = 1;
                IF (V_IDUTCLOUD = 'S') THEN
                   V_NRORG := SYS_CONTEXT('nrorg_ctx', 'nrorg');
                   IF (V_NRORG IS NULL) THEN
                      V_NRORG := 1;
                   END IF;
                ELSE
                   V_NRORG := 1;
                END IF;  
            END IF;
         EXCEPTION
            WHEN OTHERS THEN
               V_NRORG := 1;
         END;
      ELSE
         V_NRORG := 1;
      END IF;
   EXCEPTION
      WHEN OTHERS THEN
         V_NRORG := 1;
   END;
   NOVO_CODIGO_NT('VALIDACAOADQ', V_SEQVARCHAR);
   V_SEQUENCIAL := TO_NUMBER(V_SEQVARCHAR);
   INSERT INTO VALIDACAOADQ
      (NRVALIDACAOADQ, NRADQUIRENTE, NRORG)
   VALUES
      (V_SEQUENCIAL, :NEW.NRADQUIRENTE, V_NRORG);
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('INSEREVALIDACAOADQ','6.10.000'); END;
/
