CREATE OR REPLACE PROCEDURE REGISTRA_LISTAPRECOTCO_DIA
AS
	V_CDFILIAL VARCHAR2(4);
	V_CDOPERADOR VARCHAR2(12);
	V_NRORG NUMBER;
	V_NRLISTAPRECOTCO NUMBER;
BEGIN
	SELECT MAX(CDFILIAL), MAX(CDOPERINCLUSAO), MAX(NRORG)
	  INTO V_CDFILIAL, V_CDOPERADOR, V_NRORG
	  FROM PARAMTCO;

	IF V_CDFILIAL IS NOT NULL AND V_NRORG IS NOT NULL AND V_CDOPERADOR IS NOT NULL THEN
		FOR C_REGIAO IN
		(
			SELECT CDREGIAO
			  FROM REGIAO	  
		) LOOP
			GRAVA_LISTAPRECOTCO(TRUNC(SYSDATE), C_REGIAO.CDREGIAO, V_NRORG, V_CDOPERADOR, V_NRLISTAPRECOTCO);
		END LOOP;
	END IF;

	COMMIT;
END;
/

/**
 * Registro do job para execucao diaria da rotina REGISTRA_LISTAPRECOTCO_DIA
 */
DECLARE
  V_JOB NUMBER;
BEGIN
  FOR C_JOBS IN (SELECT JOB
                   FROM ALL_JOBS
                  WHERE WHAT LIKE '%REGISTRA_LISTAPRECOTCO_DIA%') LOOP
    DBMS_JOB.REMOVE(C_JOBS.JOB);
  END LOOP;

  DBMS_JOB.SUBMIT(V_JOB, WHAT => 'BEGIN REGISTRA_LISTAPRECOTCO_DIA; END;', 
  NEXT_DATE => TO_DATE(TO_CHAR(TRUNC(SYSDATE + 1), 'DD/MM/YYYY') || ' 00:00', 'DD/MM/YYYY HH24:MI'),
  INTERVAL => 'TO_DATE(TO_CHAR(TRUNC(SYSDATE + 1), ''DD/MM/YYYY'') || '' 00:00'', ''DD/MM/YYYY HH24:MI'')');
  COMMIT;
END;
/