create or replace TRIGGER USR_INTEGRA_FILIAL
AFTER INSERT ON ENDEFILI FOR EACH ROW
DECLARE
   V_COUNT        PRODUTO.VRFATOCONV%TYPE;
   V_NMFILIAL     FILIAL.NMFILIAL%TYPE;
   V_SGPAIS       PAIS.SGPAIS%TYPE;
   V_NMMUNICIPIO  MUNICIPIO.NMMUNICIPIO%TYPE;
    JOB_N       INT;
    V_COMANDO   VARCHAR2(5000);
    V_URL VARCHAR2(200);
BEGIN

   SELECT MAX(HTTPNEOLOG)
     INTO V_URL
     FROM PARAMGERAL
    WHERE CDPARGERAL = '0001';

   SELECT COUNT(1)
     INTO V_COUNT
     FROM USR_FILIALNEOLOG
    WHERE CDFILIAL = :NEW.CDFILIAL;
   IF V_URL IS NOT NULL THEN
      IF V_COUNT = 0 THEN
         IF (:NEW.IDTPENDEFILI = 'P') THEN

		      SELECT MAX(NMFILIAL)
		        INTO V_NMFILIAL
   		     FROM FILIAL
       	    WHERE CDFILIAL = :NEW.CDFILIAL;

		      SELECT MAX(SGPAIS)
		        INTO V_SGPAIS
   		     FROM PAIS
	   	    WHERE CDPAIS = :NEW.CDPAIS;

		      SELECT MAX(NMMUNICIPIO)
		        INTO V_NMMUNICIPIO
              FROM MUNICIPIO
             WHERE CDPAIS = :NEW.CDPAIS
               AND SGESTADO = :NEW.SGESTADO
               AND CDMUNICIPIO = :NEW.CDMUNICIPIO;

            INSERT INTO USR_FILIALNEOLOG
            (CDFILIAL,NMFILIAL,DTINCLUSAOPED,DSENDEFILI,SGPAIS,SGESTADO,NMMUNICIPIO,NMBAIRFILI,NRENDEFILISEP,DSCOMPENDFIL,NRCEPFILI)
		       VALUES
		      (:NEW.CDFILIAL, V_NMFILIAL, SYSDATE, :NEW.DSENDEFILI, V_SGPAIS, :NEW.SGESTADO, V_NMMUNICIPIO, :NEW.NMBAIRFILI, :NEW.NRENDEFILISEP, :NEW.DSCOMPENDFIL, :NEW.NRCEPFILI);
   	   END IF;

         V_COMANDO := 'DECLARE V_VARIAVEL CLOB; BEGIN SELECT UTL_HTTP.REQUEST('||CHR(39)||V_URL||'createLocalities'||CHR(39)||') INTO V_VARIAVEL FROM DUAL; END;';
       --V_COMANDO := 'DECLARE V_VARIAVEL CLOB; BEGIN SELECT UTL_HTTP.REQUEST('||CHR(39)||'http://172.16.0.120:9595/pebbian-erp-services-madero/backend/service/index.php/createLocalities'||CHR(39)||') INTO V_VARIAVEL FROM DUAL; END;';
         DBMS_JOB.SUBMIT (
             JOB => JOB_N,
             WHAT => V_COMANDO,
             NEXT_DATE =>SYSDATE + INTERVAL '1' SECOND
          );
      END IF;
   END IF;
END;
/
-- VERSAO --
BEGIN atuverobj('USR_REINTEGRA_FILIAL','6.06.113'); END;
/
