CREATE OR REPLACE PROCEDURE GERAWORKFLOW_SUBSCOMPMENU (
    P_NRSUBSCOMPMENU IN VARCHAR2,
    P_CDFILIAL       IN VARCHAR2,
    P_NRORG          IN VARCHAR2,
    P_CDOPERADOR     IN VARCHAR2)
AS
  V_NRWORKFLOWAUX VARCHAR2(20);
  V_NRWORKFLOW FLOAT;
  V_NMITEMWFLOW VARCHAR2(2000);
  V_DTLIMITEWFLOW DATE;
BEGIN
  NOVO_CODIGO_NT('WORKFLOW', V_NRWORKFLOWAUX);
  V_NRWORKFLOW := TO_NUMBER(V_NRWORKFLOWAUX);
  
  SELECT DSSUBSCOMPMENU INTO V_NMITEMWFLOW FROM SUBSCOMPMENU WHERE NRSUBSCOMPMENU = P_NRSUBSCOMPMENU AND NRORG = P_NRORG;
  SELECT (SYSDATE + NRDIASPROCAPROVA) INTO V_DTLIMITEWFLOW FROM ITEMPROCAPROVA WHERE NRPROCESSOAPROVA = 4 AND NRITEMPROCAPROVA = 27;
  
  INSERT INTO WORKFLOW (NRWORKFLOW,
CDOPERADOR,
NRPROCESSOAPROVA,
NRITEMPROCAPROVA,
NMITEMWFLOW,
DSMOTIVOWFLOW,
IDTIPOPERWFLOW,
IDSITWORKFLOW,
NRORG,
DTINCLUSAO,
CDOPERINCLUSAO,
DTULTATU,
NRORGINCLUSAO,
NRORGULTATU,
CDOPERULTATU,
IDATIVO) VALUES (V_NRWORKFLOW,
P_CDOPERADOR,
4,
27,
'Substuicao de Composicao Menu Grid',
V_NMITEMWFLOW,
1,
1,
P_NRORG,
SYSDATE,
P_CDOPERADOR,
SYSDATE,
P_NRORG,
P_NRORG,
P_CDOPERADOR,
'S');
  INSERT INTO DADOSWFLOW (NRWORKFLOW,
NMTABELAWFLOW,
NMCAMPOWFLOW,
DSINFORANTERIOR,
DSINFORATUAL,
DSPRIMARYKEY,
NRORG,
DTINCLUSAO,
CDOPERINCLUSAO,
DTULTATU,
NRORGINCLUSAO,
NRORGULTATU,
CDOPERULTATU,
IDATIVO) VALUES (V_NRWORKFLOW,
'SUBSCOMPMENU',
'NRSUBSCOMPMENU',
NULL,
' ',
P_NRSUBSCOMPMENU,
P_NRORG,
SYSDATE,
P_CDOPERADOR,
SYSDATE,
P_NRORG,
P_NRORG,
P_CDOPERADOR,
'S');
  FOR OPERADOR IN (SELECT * FROM OPERITEMPAPROVA WHERE NRPROCESSOAPROVA = 4 AND NRITEMPROCAPROVA = 27 AND NRORG = P_NRORG) LOOP
    INSERT INTO OPERWORKFLOW (NRWORKFLOW,
CDOPERADOR,
IDSITOPERWFLOW,
DTACAOWFLOW,
DSMOTIVOACAO,
DTLIMITEWFLOW,
NRORG,
DTINCLUSAO,
CDOPERINCLUSAO,
DTULTATU,
NRORGINCLUSAO,
NRORGULTATU,
CDOPERULTATU,
IDATIVO) VALUES (V_NRWORKFLOW,
OPERADOR.CDOPERADOR,
'P',
NULL,
NULL,
V_DTLIMITEWFLOW,
P_NRORG,
SYSDATE,
P_CDOPERADOR,
SYSDATE,
P_NRORG,
P_NRORG,
P_CDOPERADOR,
'S');
  END LOOP;
  UPDATE SUBSCOMPMENU SET IDSITUACAO = 'P', CDOPERAPROVA = P_CDOPERADOR, DTULTATU = SYSDATE, CDOPERULTATU = P_CDOPERADOR, NRORGULTATU = P_NRORG WHERE NRSUBSCOMPMENU = P_NRSUBSCOMPMENU AND NRORG = P_NRORG;
END;
/