DECLARE
	V_JOB_EXIST USER_SCHEDULER_JOBS.JOB_NAME%TYPE;
BEGIN
	FOR C_CONSPLAN_JOB IN (SELECT JOB_NAME
							FROM ALL_SCHEDULER_JOBS
							WHERE JOB_NAME = 'ZMT_CONSPLAN_RUN'
							OR JOB_NAME = 'ZMT_CONSPLAN_DELETE') LOOP
		DBMS_SCHEDULER.DROP_JOB(C_CONSPLAN_JOB.JOB_NAME);
	END LOOP;
	
	DBMS_SCHEDULER.CREATE_JOB (
	   JOB_NAME           =>  'ZMT_CONSPLAN_RUN',
	   JOB_TYPE           =>  'PLSQL_BLOCK',
	   JOB_ACTION         =>  'DECLARE
								V_NRCONSPLAN VARCHAR2(10);
								BEGIN
								
								INSERT INTO LISTA(NRLISTA, CDLISTA)
								(SELECT '||CHR(39)||'ZMTCONSPLA'||CHR(39)||', CDFILIAL||CDSERVFILI
								FROM SERVICO
								WHERE IDTIPOSERV = '||CHR(39)||'N'||CHR(39)||');
								
								
								CONSPLAN_GERAL('||CHR(39)||'ZMTCONSPLA'||CHR(39)||', TRUNC(SYSDATE)-5, TRUNC(SYSDATE)-1,
												'||CHR(39)||'CFP'||CHR(39)||', '||CHR(39)||'P'||CHR(39)||', 0, '||CHR(39)||'N'||CHR(39)||', '||CHR(39)||' '||CHR(39)||',
												'||CHR(39)||'zzzzzzzzzz'||CHR(39)||', '||CHR(39)||'000000000999'||CHR(39)||', '||CHR(39)||'ZMTCONSPLA'||CHR(39)||', V_NRCONSPLAN);
								END;',
	   START_DATE         =>  TO_DATE(TRUNC(SYSDATE)+(2/24), 'DD/MM/YYYY HH24:MI:SS'),
	   REPEAT_INTERVAL    =>  'FREQ=DAILY;INTERVAL=1', /* EVERY DAY */
	   AUTO_DROP          =>   FALSE,
	   ENABLED => TRUE,
	   COMMENTS           =>  'EXECUTA CONSPLAN_GERAL PARA O ZMART COM O PERIODO DO DIA CORRENTE TODO DIA'
	);
		
	DBMS_SCHEDULER.CREATE_JOB(
		JOB_NAME => 'ZMT_CONSPLAN_DELETE',
		JOB_TYPE => 'PLSQL_BLOCK',
		JOB_ACTION => 'DECLARE
						BEGIN
						DELETE FROM CONSPLAN
						WHERE NRCONSPLAN = '||CHR(39)||'ZMTCONSPLA'||CHR(39)||';
						END;',
		START_DATE => TO_DATE(TRUNC(SYSDATE)+(1/24), 'DD/MM/YYYY HH24:MI:SS'),
		REPEAT_INTERVAL => 'FREQ=DAILY;INTERVAL=1',
		AUTO_DROP => FALSE,
		ENABLED => TRUE,
		COMMENTS => 'DELETA OS DADOS GERADOS PARA O ZMART NA CONSPLAN'
	);
	
	COMMIT;
END;
/