CREATE OR REPLACE PROCEDURE APROVA_SUBSCOMPMENU (
    P_NRSUBSCOMPMENU IN VARCHAR2,
    P_CDFILIAL       IN VARCHAR2,
    P_NRORG          IN VARCHAR2,
    P_CDOPERADOR     IN VARCHAR2)
AS
  V_NRSEQSUBGRUSEMAM NUMBER;
  V_DTEFETIVACAO     DATE;
FUNCTION DUPLICA_SUBGRUPO(
    P_CDFILIAL         VARCHAR2,
    P_CDCIA            VARCHAR2,
    P_CDMANCIA         VARCHAR2,
    P_NRSERVICOCIAFILI NUMBER,
    P_CDCLASSE         VARCHAR2,
    P_CDSUBGRU         VARCHAR2,
    P_NRSEQSUBGRUSEMAM NUMBER,
    P_NRORG            NUMBER,
    P_DTEFETIVACAO     DATE,
    P_CDOPERADOR       VARCHAR2 )
  RETURN NUMBER
AS
  V_VIGFIMORIG DATE; V_VIGFIMNOVO DATE; V_IDPROPSUBSEMAM VARCHAR(2); V_QTPROPSUBSEMAM FLOAT; V_NOVOSEQUENCIALAUX NUMBER; V_NOVOSEQUENCIAL NUMBER;
BEGIN
  SELECT DTFIMVGSUMA, IDPROPSUBSEMAM, QTPROPSUBSEMAM
  INTO V_VIGFIMNOVO, V_IDPROPSUBSEMAM, V_QTPROPSUBSEMAM
  FROM SUBGRUSEMAM
  WHERE CDFILIAL       = P_CDFILIAL
  AND CDCIA            = P_CDCIA
  AND CDMANCIA         = P_CDMANCIA
  AND NRSERVICOCIAFILI = P_NRSERVICOCIAFILI
  AND CDCLASSE         = P_CDCLASSE
  AND CDSUBGRU         = P_CDSUBGRU
  AND NRSEQSUBGRUSEMAM = P_NRSEQSUBGRUSEMAM
  AND NRORG            = P_NRORG;
  V_VIGFIMORIG        := P_DTEFETIVACAO - 1;
  NOVO_CODIGO_NT('SUBGRUSEMAM', V_NOVOSEQUENCIALAUX);
  V_NOVOSEQUENCIAL := TO_NUMBER(V_NOVOSEQUENCIALAUX);
  UPDATE SUBGRUSEMAM
  SET DTFIMVGSUMA      = V_VIGFIMORIG,
    DTULTATU           = SYSDATE,
    NRORGULTATU        = P_NRORG,
    CDOPERULTATU       = P_CDOPERADOR
  WHERE CDFILIAL       = P_CDFILIAL
  AND CDCIA            = P_CDCIA
  AND CDMANCIA         = P_CDMANCIA
  AND NRSERVICOCIAFILI = P_NRSERVICOCIAFILI
  AND CDCLASSE         = P_CDCLASSE
  AND CDSUBGRU         = P_CDSUBGRU
  AND NRSEQSUBGRUSEMAM = P_NRSEQSUBGRUSEMAM
  AND NRORG            = P_NRORG;
  INSERT
  INTO SUBGRUSEMAM
    (
      CDFILIAL,
      CDCIA,
      CDMANCIA,
      NRSERVICOCIAFILI,
      CDCLASSE,
      CDSUBGRU,
      NRSEQSUBGRUSEMAM,
      DTINIVGSUMA,
      DTFIMVGSUMA,
      IDPROPSUBSEMAM,
      QTPROPSUBSEMAM,
      NRORG,
      DTINCLUSAO,
      CDOPERINCLUSAO,
      DTULTATU,
      NRORGINCLUSAO,
      NRORGULTATU,
      CDOPERULTATU,
      IDATIVO,
      IDSITCUSTEIO
    )
    VALUES
    (
      P_CDFILIAL,
      P_CDCIA,
      P_CDMANCIA,
      P_NRSERVICOCIAFILI,
      P_CDCLASSE,
      P_CDSUBGRU,
      V_NOVOSEQUENCIAL,
      P_DTEFETIVACAO,
      V_VIGFIMNOVO,
      V_IDPROPSUBSEMAM,
      V_QTPROPSUBSEMAM,
      P_NRORG,
      SYSDATE,
      P_CDOPERADOR,
      SYSDATE,
      P_NRORG,
      P_NRORG,
      P_CDOPERADOR,
      'S',
      'P'
    );
  FOR PROPSUBGRUSE IN
  (SELECT           *
    FROM PROPSUBGRUSE
    WHERE CDFILIAL       = P_CDFILIAL
    AND CDCIA            = P_CDCIA
    AND CDMANCIA         = P_CDMANCIA
    AND NRSERVICOCIAFILI = P_NRSERVICOCIAFILI
    AND CDCLASSE         = P_CDCLASSE
    AND CDSUBGRU         = P_CDSUBGRU
    AND NRSEQSUBGRUSEMAM = P_NRSEQSUBGRUSEMAM
    AND NRORG            = P_NRORG
  )
  LOOP
    INSERT
    INTO PROPSUBGRUSE
      (
        CDFILIAL,
        CDCIA,
        CDMANCIA,
        NRSERVICOCIAFILI,
        CDCLASSE,
        CDSUBGRU,
        NRSEQSUBGRUSEMAM,
        QTPAXDE,
        QTPAXATE,
        QTSUBGRUPO,
        NRORG,
        DTINCLUSAO,
        CDOPERINCLUSAO,
        DTULTATU,
        NRORGINCLUSAO,
        NRORGULTATU,
        CDOPERULTATU,
        IDATIVO
      )
      VALUES
      (
        PROPSUBGRUSE.CDFILIAL,
        PROPSUBGRUSE.CDCIA,
        PROPSUBGRUSE.CDMANCIA,
        PROPSUBGRUSE.NRSERVICOCIAFILI,
        PROPSUBGRUSE.CDCLASSE,
        PROPSUBGRUSE.CDSUBGRU,
        V_NOVOSEQUENCIAL,
        PROPSUBGRUSE.QTPAXDE,
        PROPSUBGRUSE.QTPAXATE,
        PROPSUBGRUSE.QTSUBGRUPO,
        PROPSUBGRUSE.NRORG,
        PROPSUBGRUSE.DTINCLUSAO,
        PROPSUBGRUSE.CDOPERINCLUSAO,
        PROPSUBGRUSE.DTULTATU,
        PROPSUBGRUSE.NRORGINCLUSAO,
        PROPSUBGRUSE.NRORGULTATU,
        PROPSUBGRUSE.CDOPERULTATU,
        PROPSUBGRUSE.IDATIVO
      );
  END LOOP;
  FOR PROCIASUBSEMAM IN
  (SELECT             *
    FROM PROCIASUBSEMAM
    WHERE CDFILIAL       = P_CDFILIAL
    AND CDCIA            = P_CDCIA
    AND CDMANCIA         = P_CDMANCIA
    AND NRSERVICOCIAFILI = P_NRSERVICOCIAFILI
    AND CDCLASSE         = P_CDCLASSE
    AND CDSUBGRU         = P_CDSUBGRU
    AND NRSEQSUBGRUSEMAM = P_NRSEQSUBGRUSEMAM
    AND NRORG            = P_NRORG
  )
  LOOP
    INSERT
    INTO PROCIASUBSEMAM
      (
        CDFILIAL,
        CDCIA,
        CDMANCIA,
        NRSERVICOCIAFILI,
        CDCLASSE,
        CDSUBGRU,
        NRSEQSUBGRUSEMAM,
        CDPRODUTO,
        QTPRODUTOCIA,
        NRORDEM,
        IDANALIZEATIV,
        NRORG,
        DTINCLUSAO,
        CDOPERINCLUSAO,
        DTULTATU,
        NRORGINCLUSAO,
        NRORGULTATU,
        CDOPERULTATU,
        IDATIVO
      )
      VALUES
      (
        PROCIASUBSEMAM.CDFILIAL,
        PROCIASUBSEMAM.CDCIA,
        PROCIASUBSEMAM.CDMANCIA,
        PROCIASUBSEMAM.NRSERVICOCIAFILI,
        PROCIASUBSEMAM.CDCLASSE,
        PROCIASUBSEMAM.CDSUBGRU,
        V_NOVOSEQUENCIAL,
        PROCIASUBSEMAM.CDPRODUTO,
        PROCIASUBSEMAM.QTPRODUTOCIA,
        PROCIASUBSEMAM.NRORDEM,
        PROCIASUBSEMAM.IDANALIZEATIV,
        PROCIASUBSEMAM.NRORG,
        PROCIASUBSEMAM.DTINCLUSAO,
        PROCIASUBSEMAM.CDOPERINCLUSAO,
        PROCIASUBSEMAM.DTULTATU,
        PROCIASUBSEMAM.NRORGINCLUSAO,
        PROCIASUBSEMAM.NRORGULTATU,
        PROCIASUBSEMAM.CDOPERULTATU,
        PROCIASUBSEMAM.IDATIVO
      );
  END LOOP;
  FOR CICLOSUBGRUSEMAM IN
  (SELECT               *
    FROM CICLOSUBGRUSEMAM
    WHERE CDFILIAL       = P_CDFILIAL
    AND CDCIA            = P_CDCIA
    AND CDMANCIA         = P_CDMANCIA
    AND NRSERVICOCIAFILI = P_NRSERVICOCIAFILI
    AND CDCLASSE         = P_CDCLASSE
    AND CDSUBGRU         = P_CDSUBGRU
    AND NRSEQSUBGRUSEMAM = P_NRSEQSUBGRUSEMAM
    AND NRORG            = P_NRORG
  )
  LOOP
    INSERT
    INTO CICLOSUBGRUSEMAM
      (
        CDFILIAL,
        CDCIA,
        CDMANCIA,
        NRSERVICOCIAFILI,
        CDCLASSE,
        CDSUBGRU,
        NRSEQSUBGRUSEMAM,
        CDCICLO,
        NRITAIRVISION,
        VRCUSTOCICSEMAM,
        FGFOTOSUBSEMAM,
        FGFOTOSUBSEMAMT,
        DTEFETSUMA,
        DTREVISUMA,
        DSSUBGRUPOIMP,
        VRMMPMCICSEMAM,
        IDANALIZEATIV,
        NRORG,
        DTINCLUSAO,
        CDOPERINCLUSAO,
        DTULTATU,
        NRORGINCLUSAO,
        NRORGULTATU,
        CDOPERULTATU,
        IDATIVO,
        DTCUSTO,
        IDTIPOCUSTO,        
        DSCICLOSUBGRUSEMAM
      )
      VALUES
      (
        CICLOSUBGRUSEMAM.CDFILIAL,
        CICLOSUBGRUSEMAM.CDCIA,
        CICLOSUBGRUSEMAM.CDMANCIA,
        CICLOSUBGRUSEMAM.NRSERVICOCIAFILI,
        CICLOSUBGRUSEMAM.CDCLASSE,
        CICLOSUBGRUSEMAM.CDSUBGRU,
        V_NOVOSEQUENCIAL,
        CICLOSUBGRUSEMAM.CDCICLO,
        CICLOSUBGRUSEMAM.NRITAIRVISION,
        CICLOSUBGRUSEMAM.VRCUSTOCICSEMAM,
        CICLOSUBGRUSEMAM.FGFOTOSUBSEMAM,
        CICLOSUBGRUSEMAM.FGFOTOSUBSEMAMT,
        CICLOSUBGRUSEMAM.DTEFETSUMA,
        CICLOSUBGRUSEMAM.DTREVISUMA,
        CICLOSUBGRUSEMAM.DSSUBGRUPOIMP,
        CICLOSUBGRUSEMAM.VRMMPMCICSEMAM,
        CICLOSUBGRUSEMAM.IDANALIZEATIV,
        CICLOSUBGRUSEMAM.NRORG,
        CICLOSUBGRUSEMAM.DTINCLUSAO,
        CICLOSUBGRUSEMAM.CDOPERINCLUSAO,
        CICLOSUBGRUSEMAM.DTULTATU,
        CICLOSUBGRUSEMAM.NRORGINCLUSAO,
        CICLOSUBGRUSEMAM.NRORGULTATU,
        CICLOSUBGRUSEMAM.CDOPERULTATU,
        CICLOSUBGRUSEMAM.IDATIVO,
        CICLOSUBGRUSEMAM.DTCUSTO,
        CICLOSUBGRUSEMAM.IDTIPOCUSTO,        
        CICLOSUBGRUSEMAM.DSCICLOSUBGRUSEMAM
      );
  END LOOP;
  FOR MONTASUBSEMAM IN
  (SELECT               *
    FROM MONTASUBSEMAM
    WHERE CDFILIAL       = P_CDFILIAL
    AND CDCIA            = P_CDCIA
    AND CDMANCIA         = P_CDMANCIA
    AND NRSERVICOCIAFILI = P_NRSERVICOCIAFILI
    AND CDCLASSE         = P_CDCLASSE
    AND CDSUBGRU         = P_CDSUBGRU
    AND NRSEQSUBGRUSEMAM = P_NRSEQSUBGRUSEMAM
    AND NRORG            = P_NRORG
  )
  LOOP
    INSERT
    INTO MONTASUBSEMAM
      (
        CDFILIAL,
        CDCIA,
        CDMANCIA,
        NRSERVICOCIAFILI,
        CDCLASSE,
        CDSUBGRU,
        CDCICLO,
        DSMONTASUBSEMAM,
        NRSEQSUBGRUSEMAM,
        DSMONTASUBSEMAMHTML,
        IDANALIZEATIV,
        NRORG,
        DTINCLUSAO,
        CDOPERINCLUSAO,
        DTULTATU,
        NRORGINCLUSAO,
        NRORGULTATU,
        CDOPERULTATU,
        IDATIVO
      )
      VALUES
      (
        MONTASUBSEMAM.CDFILIAL,
        MONTASUBSEMAM.CDCIA,
        MONTASUBSEMAM.CDMANCIA,
        MONTASUBSEMAM.NRSERVICOCIAFILI,
        MONTASUBSEMAM.CDCLASSE,
        MONTASUBSEMAM.CDSUBGRU,
        MONTASUBSEMAM.CDCICLO,
        MONTASUBSEMAM.DSMONTASUBSEMAM,
        V_NOVOSEQUENCIAL,
        MONTASUBSEMAM.DSMONTASUBSEMAMHTML,
        MONTASUBSEMAM.IDANALIZEATIV,
        MONTASUBSEMAM.NRORG,
        MONTASUBSEMAM.DTINCLUSAO,
        MONTASUBSEMAM.CDOPERINCLUSAO,
        MONTASUBSEMAM.DTULTATU,
        MONTASUBSEMAM.NRORGINCLUSAO,
        MONTASUBSEMAM.NRORGULTATU,
        MONTASUBSEMAM.CDOPERULTATU,
        MONTASUBSEMAM.IDATIVO
      );
  END LOOP;
  FOR RECSUBSEMAM IN
  (SELECT          *
    FROM RECSUBSEMAM
    WHERE CDFILIAL       = P_CDFILIAL
    AND CDCIA            = P_CDCIA
    AND CDMANCIA         = P_CDMANCIA
    AND NRSERVICOCIAFILI = P_NRSERVICOCIAFILI
    AND CDCLASSE         = P_CDCLASSE
    AND CDSUBGRU         = P_CDSUBGRU
    AND NRSEQSUBGRUSEMAM = P_NRSEQSUBGRUSEMAM
    AND NRORG            = P_NRORG
  )
  LOOP
    INSERT
    INTO RECSUBSEMAM
      (
        CDFILIAL,
        CDCIA,
        CDMANCIA,
        NRSERVICOCIAFILI,
        CDCLASSE,
        CDSUBGRU,
        NRSEQSUBGRUSEMAM,
        CDCICLO,
        CDRECEITA,
        QTRECSUMA,
        VRCUSRESUBSEMAM,
        TXCOMRECSUMA,
        NRITAIRVISION,
        IDCUSTOPRODZERO,
        NRORDEM,
        QTGRAMATURA,
        VRPESOGRAMATURA,
        IDANALIZEATIV,
        NRORG,
        DTINCLUSAO,
        CDOPERINCLUSAO,
        DTULTATU,
        NRORGINCLUSAO,
        NRORGULTATU,
        CDOPERULTATU,
        IDATIVO
      )
      VALUES
      (
        RECSUBSEMAM.CDFILIAL,
        RECSUBSEMAM.CDCIA,
        RECSUBSEMAM.CDMANCIA,
        RECSUBSEMAM.NRSERVICOCIAFILI,
        RECSUBSEMAM.CDCLASSE,
        RECSUBSEMAM.CDSUBGRU,
        V_NOVOSEQUENCIAL,
        RECSUBSEMAM.CDCICLO,
        RECSUBSEMAM.CDRECEITA,
        RECSUBSEMAM.QTRECSUMA,
        RECSUBSEMAM.VRCUSRESUBSEMAM,
        RECSUBSEMAM.TXCOMRECSUMA,
        RECSUBSEMAM.NRITAIRVISION,
        RECSUBSEMAM.IDCUSTOPRODZERO,
        RECSUBSEMAM.NRORDEM,
        RECSUBSEMAM.QTGRAMATURA,
        RECSUBSEMAM.VRPESOGRAMATURA,
        RECSUBSEMAM.IDANALIZEATIV,
        RECSUBSEMAM.NRORG,
        RECSUBSEMAM.DTINCLUSAO,
        RECSUBSEMAM.CDOPERINCLUSAO,
        RECSUBSEMAM.DTULTATU,
        RECSUBSEMAM.NRORGINCLUSAO,
        RECSUBSEMAM.NRORGULTATU,
        RECSUBSEMAM.CDOPERULTATU,
        RECSUBSEMAM.IDATIVO
      );
  END LOOP;
  FOR PRODSUBSEMAM IN
  (SELECT           *
    FROM PRODSUBSEMAM
    WHERE CDFILIAL       = P_CDFILIAL
    AND CDCIA            = P_CDCIA
    AND CDMANCIA         = P_CDMANCIA
    AND NRSERVICOCIAFILI = P_NRSERVICOCIAFILI
    AND CDCLASSE         = P_CDCLASSE
    AND CDSUBGRU         = P_CDSUBGRU
    AND NRSEQSUBGRUSEMAM = P_NRSEQSUBGRUSEMAM
    AND NRORG            = P_NRORG
  )
  LOOP
    INSERT
    INTO PRODSUBSEMAM
      (
        CDFILIAL,
        CDCIA,
        CDMANCIA,
        NRSERVICOCIAFILI,
        CDCLASSE,
        CDSUBGRU,
        NRSEQSUBGRUSEMAM,
        CDCICLO,
        CDPRODUTO,
        VRPESOGRAMATURA,
        QTGRAMATURA,
        QTPRODSUMA,
        TXCOPRODSUMA,
        VRCUSPRSUBSEMAM,
        NRITAIRVISION,
        NRORDEM,
        IDANALIZEATIV,
        NRORG,
        DTINCLUSAO,
        CDOPERINCLUSAO,
        DTULTATU,
        NRORGINCLUSAO,
        NRORGULTATU,
        CDOPERULTATU,
        IDATIVO
      )
      VALUES
      (
        PRODSUBSEMAM.CDFILIAL,
        PRODSUBSEMAM.CDCIA,
        PRODSUBSEMAM.CDMANCIA,
        PRODSUBSEMAM.NRSERVICOCIAFILI,
        PRODSUBSEMAM.CDCLASSE,
        PRODSUBSEMAM.CDSUBGRU,
        V_NOVOSEQUENCIAL,
        PRODSUBSEMAM.CDCICLO,
        PRODSUBSEMAM.CDPRODUTO,
        PRODSUBSEMAM.VRPESOGRAMATURA,
        PRODSUBSEMAM.QTGRAMATURA,
        PRODSUBSEMAM.QTPRODSUMA,
        PRODSUBSEMAM.TXCOPRODSUMA,
        PRODSUBSEMAM.VRCUSPRSUBSEMAM,
        PRODSUBSEMAM.NRITAIRVISION,
        PRODSUBSEMAM.NRORDEM,
        PRODSUBSEMAM.IDANALIZEATIV,
        PRODSUBSEMAM.NRORG,
        PRODSUBSEMAM.DTINCLUSAO,
        PRODSUBSEMAM.CDOPERINCLUSAO,
        PRODSUBSEMAM.DTULTATU,
        PRODSUBSEMAM.NRORGINCLUSAO,
        PRODSUBSEMAM.NRORGULTATU,
        PRODSUBSEMAM.CDOPERULTATU,
        PRODSUBSEMAM.IDATIVO
      );
  END LOOP;
  FOR SUBGRUPRINCIPAL IN
  (SELECT              *
    FROM SUBGRUPRINCIPALMENU
    WHERE CDFILIAL           = P_CDFILIAL
    AND CDCIA                = P_CDCIA
    AND CDMANCIA             = P_CDMANCIA
    AND NRSERVICOCIAFILI     = P_NRSERVICOCIAFILI
    AND CDCLASSE             = P_CDCLASSE
    AND CDSUBGRUPRIN         = P_CDSUBGRU
    AND NRSEQSUBGRUSEMAMPRIN = P_NRSEQSUBGRUSEMAM
    AND NRORG                = P_NRORG
  )
  LOOP
    INSERT
    INTO SUBGRUPRINCIPALMENU
      (
        CDFILIAL,
        CDCIA,
        CDMANCIA,
        NRSERVICOCIAFILI,
        CDCLASSE,
        CDSUBGRU,
        NRSEQSUBGRUSEMAM,
        CDCICLO,
        CDSUBGRUPRIN,
        NRSEQSUBGRUSEMAMPRIN,
        NRORG,
        DTINCLUSAO,
        CDOPERINCLUSAO,
        DTULTATU,
        NRORGINCLUSAO,
        NRORGULTATU,
        CDOPERULTATU,
        IDATIVO
      )
      VALUES
      (
        SUBGRUPRINCIPAL.CDFILIAL,
        SUBGRUPRINCIPAL.CDCIA,
        SUBGRUPRINCIPAL.CDMANCIA,
        SUBGRUPRINCIPAL.NRSERVICOCIAFILI,
        SUBGRUPRINCIPAL.CDCLASSE,
        SUBGRUPRINCIPAL.CDSUBGRU,
        SUBGRUPRINCIPAL.NRSEQSUBGRUSEMAM,
        SUBGRUPRINCIPAL.CDCICLO,
        SUBGRUPRINCIPAL.CDSUBGRUPRIN,
        V_NOVOSEQUENCIAL,
        SUBGRUPRINCIPAL.NRORG,
        SUBGRUPRINCIPAL.DTINCLUSAO,
        SUBGRUPRINCIPAL.CDOPERINCLUSAO,
        SUBGRUPRINCIPAL.DTULTATU,
        SUBGRUPRINCIPAL.NRORGINCLUSAO,
        SUBGRUPRINCIPAL.NRORGULTATU,
        SUBGRUPRINCIPAL.CDOPERULTATU,
        SUBGRUPRINCIPAL.IDATIVO
      );
  END LOOP;
  FOR SUBGRUSECUNDARIO IN
  (SELECT               *
    FROM SUBGRUPRINCIPALMENU
    WHERE CDFILIAL       = P_CDFILIAL
    AND CDCIA            = P_CDCIA
    AND CDMANCIA         = P_CDMANCIA
    AND NRSERVICOCIAFILI = P_NRSERVICOCIAFILI
    AND CDCLASSE         = P_CDCLASSE
    AND CDSUBGRU         = P_CDSUBGRU
    AND NRSEQSUBGRUSEMAM = P_NRSEQSUBGRUSEMAM
    AND NRORG            = P_NRORG
  )
  LOOP
    INSERT
    INTO SUBGRUPRINCIPALMENU
      (
        CDFILIAL,
        CDCIA,
        CDMANCIA,
        NRSERVICOCIAFILI,
        CDCLASSE,
        CDSUBGRU,
        NRSEQSUBGRUSEMAM,
        CDCICLO,
        CDSUBGRUPRIN,
        NRSEQSUBGRUSEMAMPRIN,
        NRORG,
        DTINCLUSAO,
        CDOPERINCLUSAO,
        DTULTATU,
        NRORGINCLUSAO,
        NRORGULTATU,
        CDOPERULTATU,
        IDATIVO
      )
      VALUES
      (
        SUBGRUSECUNDARIO.CDFILIAL,
        SUBGRUSECUNDARIO.CDCIA,
        SUBGRUSECUNDARIO.CDMANCIA,
        SUBGRUSECUNDARIO.NRSERVICOCIAFILI,
        SUBGRUSECUNDARIO.CDCLASSE,
        SUBGRUSECUNDARIO.CDSUBGRU,
        V_NOVOSEQUENCIAL,
        SUBGRUSECUNDARIO.CDCICLO,
        SUBGRUSECUNDARIO.CDSUBGRUPRIN,
        SUBGRUSECUNDARIO.NRSEQSUBGRUSEMAMPRIN,
        SUBGRUSECUNDARIO.NRORG,
        SUBGRUSECUNDARIO.DTINCLUSAO,
        SUBGRUSECUNDARIO.CDOPERINCLUSAO,
        SUBGRUSECUNDARIO.DTULTATU,
        SUBGRUSECUNDARIO.NRORGINCLUSAO,
        SUBGRUSECUNDARIO.NRORGULTATU,
        SUBGRUSECUNDARIO.CDOPERULTATU,
        SUBGRUSECUNDARIO.IDATIVO
      );
  END LOOP;
  RETURN V_NOVOSEQUENCIAL;
END;
FUNCTION TRATA_SUBGRUPO
  (
    P_CDFILIAL         VARCHAR2,
    P_CDCIA            VARCHAR2,
    P_CDMANCIA         VARCHAR2,
    P_NRSERVICOCIAFILI NUMBER,
    P_CDCLASSE         VARCHAR2,
    P_CDSUBGRU         VARCHAR2,
    P_NRSEQSUBGRUSEMAM NUMBER,
    P_NRORG            NUMBER,
    P_DTINIVGSUMA      DATE,
    P_DTFIMVGSUMA      DATE,
    P_IDSITUAPROPCIA   VARCHAR2,
    P_DTEFETIVACAO     DATE,
    P_CDOPERADOR       VARCHAR2
  )
  RETURN NUMBER
AS
BEGIN
  IF (P_IDSITUAPROPCIA = 'E') THEN
    RETURN P_NRSEQSUBGRUSEMAM;
  END IF;
  IF (P_IDSITUAPROPCIA = 'R') THEN
    RETURN NULL;
  END IF;
  IF (TRUNC(P_DTEFETIVACAO) < TRUNC(P_DTINIVGSUMA)) THEN
    RETURN P_NRSEQSUBGRUSEMAM;
  END IF;
  IF (TRUNC(P_DTEFETIVACAO) > TRUNC(P_DTFIMVGSUMA)) THEN
    RETURN NULL;
  END IF;
  IF (P_IDSITUAPROPCIA IN ('A', 'P') AND TRUNC(P_DTEFETIVACAO) BETWEEN TRUNC(P_DTINIVGSUMA) AND TRUNC(P_DTFIMVGSUMA)) THEN
    RETURN DUPLICA_SUBGRUPO( P_CDFILIAL, P_CDCIA, P_CDMANCIA, P_NRSERVICOCIAFILI, P_CDCLASSE, P_CDSUBGRU, P_NRSEQSUBGRUSEMAM, P_NRORG, P_DTEFETIVACAO, P_CDOPERADOR );
  END IF;
  RETURN NULL;
END;
BEGIN
  SELECT DTEFETIVACAO
  INTO V_DTEFETIVACAO
  FROM SUBSCOMPMENU
  WHERE NRSUBSCOMPMENU = P_NRSUBSCOMPMENU
  AND NRORG            = P_NRORG;
  FOR SUBGRUPO IN
  (SELECT SSM.CDFILIAL,
    SSM.CDCIA,
    SSM.CDMANCIA,
    SSM.NRSERVICOCIAFILI,
    SSM.CDCLASSE,
    SSM.CDSUBGRU,
    SSM.NRSEQSUBGRUSEMAM,
    LISTAGG(CSM.CDCICLO, '-') WITHIN GROUP (
  ORDER BY CSM.CDCICLO) AS CICLOS,
    SSM.NRORG,
    SSM.DTINIVGSUMA,
    SSM.DTFIMVGSUMA,
    MAN.IDSITUAPROPCIA
  FROM SUBSCOMPMENU SBS
  LEFT JOIN ESCOPOSUBSCOMPMENU ESC1
  ON ESC1.IDNIVELESCOPO   = 1
  AND ESC1.NRSUBSCOMPMENU = SBS.NRSUBSCOMPMENU
  AND ESC1.NRORG          = SBS.NRORG
  LEFT JOIN ESCOPOSUBSCOMPMENU ESC2
  ON ESC2.IDNIVELESCOPO   = 2
  AND ESC2.NRSUBSCOMPMENU = SBS.NRSUBSCOMPMENU
  AND ESC2.NRORG          = SBS.NRORG
  LEFT JOIN ESCOPOSUBSCOMPMENU ESC3
  ON ESC3.IDNIVELESCOPO   = 3
  AND ESC3.NRSUBSCOMPMENU = SBS.NRSUBSCOMPMENU
  AND ESC3.NRORG          = SBS.NRORG
  LEFT JOIN ESCOPOSUBSCOMPMENU ESC4
  ON ESC4.IDNIVELESCOPO   = 4
  AND ESC4.NRSUBSCOMPMENU = SBS.NRSUBSCOMPMENU
  AND ESC4.NRORG          = SBS.NRORG
  LEFT JOIN ESCOPOSUBSCOMPMENU ESC5
  ON ESC5.IDNIVELESCOPO   = 5
  AND ESC5.NRSUBSCOMPMENU = SBS.NRSUBSCOMPMENU
  AND ESC5.NRORG          = SBS.NRORG
  LEFT JOIN ESCOPOSUBSCOMPMENU ESC6
  ON ESC6.IDNIVELESCOPO   = 6
  AND ESC6.NRSUBSCOMPMENU = SBS.NRSUBSCOMPMENU
  AND ESC6.NRORG          = SBS.NRORG
  CROSS JOIN CICLOSUBGRUSEMAM CSM
  JOIN SUBGRUSEMAM SSM
  ON CSM.CDFILIAL          = SSM.CDFILIAL
  AND CSM.CDCIA            = SSM.CDCIA
  AND CSM.CDMANCIA         = SSM.CDMANCIA
  AND CSM.NRSERVICOCIAFILI = SSM.NRSERVICOCIAFILI
  AND CSM.CDCLASSE         = SSM.CDCLASSE
  AND CSM.CDSUBGRU         = SSM.CDSUBGRU
  AND CSM.NRSEQSUBGRUSEMAM = SSM.NRSEQSUBGRUSEMAM
  AND CSM.NRORG            = SSM.NRORG
  JOIN MANUALCIAM MAN
  ON CSM.CDFILIAL           = MAN.CDFILIAL
  AND CSM.CDCIA             = MAN.CDCIA
  AND CSM.CDMANCIA          = MAN.CDMANCIA
  AND CSM.NRORG             = MAN.NRORG
  JOIN EDITSUBSCOMPMENU EDIT
  ON EDIT.NRSUBSCOMPMENU = SBS.NRSUBSCOMPMENU
  AND (EDIT.IDTIPOACAO = 'A'
  OR (EDIT.CDFILIAL = CSM.CDFILIAL
  AND EDIT.CDCIA = CSM.CDCIA
  AND EDIT.CDMANCIA = CSM.CDMANCIA
  AND EDIT.NRSERVICOCIAFILI = CSM.NRSERVICOCIAFILI
  AND EDIT.CDCLASSE = CSM.CDCLASSE
  AND EDIT.CDSUBGRU = CSM.CDSUBGRU
  AND EDIT.NRSEQSUBGRUSEMAM = CSM.NRSEQSUBGRUSEMAM
  AND EDIT.NRORG = CSM.NRORG
  ))
  WHERE SBS.NRSUBSCOMPMENU  = P_NRSUBSCOMPMENU
  AND SBS.NRORG             = P_NRORG
  AND SBS.NRORG             = CSM.NRORG
  AND (SBS.TCDFILIAL        = 'T'
  OR ESC1.CDFILIAL          = CSM.CDFILIAL)
  AND (SBS.TCIAFILI         = 'T'
  OR ESC2.CDFILIAL          = CSM.CDFILIAL
  AND ESC2.CDCIA            = CSM.CDCIA)
  AND (SBS.TMANCIA          = 'T'
  OR ESC3.CDFILIAL          = CSM.CDFILIAL
  AND ESC3.CDCIA            = CSM.CDCIA
  AND ESC3.CDMANCIA         = CSM.CDMANCIA)
  AND (SBS.TSERVCLASSE      = 'T'
  OR ESC4.CDFILIAL          = CSM.CDFILIAL
  AND ESC4.CDCIA            = CSM.CDCIA
  AND ESC4.CDMANCIA         = CSM.CDMANCIA
  AND ESC4.NRSERVICOCIAFILI = CSM.NRSERVICOCIAFILI
  AND ESC4.CDCLASSE         = CSM.CDCLASSE)
  AND (SBS.TSUBGRUPO        = 'T'
  OR ESC5.CDFILIAL          = CSM.CDFILIAL
  AND ESC5.CDCIA            = CSM.CDCIA
  AND ESC5.CDMANCIA         = CSM.CDMANCIA
  AND ESC5.NRSERVICOCIAFILI = CSM.NRSERVICOCIAFILI
  AND ESC5.CDCLASSE         = CSM.CDCLASSE
  AND ESC5.CDSUBGRU         = CSM.CDSUBGRU
  AND ESC5.NRSEQSUBGRUSEMAM = CSM.NRSEQSUBGRUSEMAM)
  AND (SBS.TCICLO           = 'T'
  OR ESC6.CDFILIAL          = CSM.CDFILIAL
  AND ESC6.CDCIA            = CSM.CDCIA
  AND ESC6.CDMANCIA         = CSM.CDMANCIA
  AND ESC6.NRSERVICOCIAFILI = CSM.NRSERVICOCIAFILI
  AND ESC6.CDCLASSE         = CSM.CDCLASSE
  AND ESC6.CDSUBGRU         = CSM.CDSUBGRU
  AND ESC6.NRSEQSUBGRUSEMAM = CSM.NRSEQSUBGRUSEMAM
  AND ESC6.CDCICLO          = CSM.CDCICLO)
  GROUP BY SSM.CDFILIAL,
    SSM.CDCIA,
    SSM.CDMANCIA,
    SSM.NRSERVICOCIAFILI,
    SSM.CDCLASSE,
    SSM.CDSUBGRU,
    SSM.NRSEQSUBGRUSEMAM,
    SSM.NRORG,
    SSM.DTINIVGSUMA,
    SSM.DTFIMVGSUMA,
    MAN.IDSITUAPROPCIA
  )
  LOOP
    V_NRSEQSUBGRUSEMAM := TRATA_SUBGRUPO( SUBGRUPO.CDFILIAL, SUBGRUPO.CDCIA, SUBGRUPO.CDMANCIA, SUBGRUPO.NRSERVICOCIAFILI, SUBGRUPO.CDCLASSE, SUBGRUPO.CDSUBGRU, SUBGRUPO.NRSEQSUBGRUSEMAM, SUBGRUPO.NRORG, SUBGRUPO.DTINIVGSUMA, SUBGRUPO.DTFIMVGSUMA, SUBGRUPO.IDSITUAPROPCIA, V_DTEFETIVACAO, P_CDOPERADOR );
    IF (V_NRSEQSUBGRUSEMAM IS NOT NULL) THEN
      FOR ITEM_TO_DELETE IN
      (SELECT             *
      FROM EDITSUBSCOMPMENU
      WHERE NRSUBSCOMPMENU = P_NRSUBSCOMPMENU
      AND IDTIPOACAO       = 'D'
      AND CDFILIAL         = SUBGRUPO.CDFILIAL
      AND CDCIA            = SUBGRUPO.CDCIA
      AND CDMANCIA         = SUBGRUPO.CDMANCIA
      AND NRSERVICOCIAFILI = SUBGRUPO.NRSERVICOCIAFILI
      AND CDCLASSE         = SUBGRUPO.CDCLASSE
      AND CDSUBGRU         = SUBGRUPO.CDSUBGRU
      AND NRSEQSUBGRUSEMAM = SUBGRUPO.NRSEQSUBGRUSEMAM
      AND NRORG            = SUBGRUPO.NRORG
      )
      LOOP
        IF (ITEM_TO_DELETE.IDPRODRECNOVO = 'P') THEN
          DELETE
          FROM PRODSUBSEMAM
          WHERE CDFILIAL       = ITEM_TO_DELETE.CDFILIAL
          AND CDCIA            = ITEM_TO_DELETE.CDCIA
          AND CDMANCIA         = ITEM_TO_DELETE.CDMANCIA
          AND NRSERVICOCIAFILI = ITEM_TO_DELETE.NRSERVICOCIAFILI
          AND CDCLASSE         = ITEM_TO_DELETE.CDCLASSE
          AND CDSUBGRU         = ITEM_TO_DELETE.CDSUBGRU
          AND NRSEQSUBGRUSEMAM = V_NRSEQSUBGRUSEMAM
          AND CDCICLO          = ITEM_TO_DELETE.CDCICLO
          AND CDPRODUTO        = ITEM_TO_DELETE.CDPRODUTONOVO
          AND NRORG            = ITEM_TO_DELETE.NRORG;
        END IF;
      IF (ITEM_TO_DELETE.IDPRODRECNOVO = 'R') THEN
        DELETE
        FROM RECSUBSEMAM
        WHERE CDFILIAL       = ITEM_TO_DELETE.CDFILIAL
        AND CDCIA            = ITEM_TO_DELETE.CDCIA
        AND CDMANCIA         = ITEM_TO_DELETE.CDMANCIA
        AND NRSERVICOCIAFILI = ITEM_TO_DELETE.NRSERVICOCIAFILI
        AND CDCLASSE         = ITEM_TO_DELETE.CDCLASSE
        AND CDSUBGRU         = ITEM_TO_DELETE.CDSUBGRU
        AND NRSEQSUBGRUSEMAM = V_NRSEQSUBGRUSEMAM
        AND CDCICLO          = ITEM_TO_DELETE.CDCICLO
        AND CDRECEITA        = ITEM_TO_DELETE.CDRECEITANOVO
        AND NRORG            = ITEM_TO_DELETE.NRORG;
      END IF;
  END LOOP;
  FOR ITEM_TO_EDIT IN
  (SELECT           *
  FROM EDITSUBSCOMPMENU
  WHERE NRSUBSCOMPMENU = P_NRSUBSCOMPMENU
  AND IDTIPOACAO       = 'E'
  AND CDFILIAL         = SUBGRUPO.CDFILIAL
  AND CDCIA            = SUBGRUPO.CDCIA
  AND CDMANCIA         = SUBGRUPO.CDMANCIA
  AND NRSERVICOCIAFILI = SUBGRUPO.NRSERVICOCIAFILI
  AND CDCLASSE         = SUBGRUPO.CDCLASSE
  AND CDSUBGRU         = SUBGRUPO.CDSUBGRU
  AND NRSEQSUBGRUSEMAM = SUBGRUPO.NRSEQSUBGRUSEMAM
  AND NRORG            = SUBGRUPO.NRORG
  )
  LOOP
    IF (ITEM_TO_EDIT.IDPRODRECNOVO = 'P') THEN
      UPDATE PRODSUBSEMAM
      SET VRPESOGRAMATURA  = ITEM_TO_EDIT.VRPESOGRAMATURA ,
        QTGRAMATURA        = ITEM_TO_EDIT.QTGRAMATURA ,
        QTPRODSUMA         = ITEM_TO_EDIT.QTPRODREC ,
        DTULTATU           = SYSDATE ,
        NRORGULTATU        = P_NRORG ,
        CDOPERULTATU       = P_CDOPERADOR
      WHERE CDFILIAL       = ITEM_TO_EDIT.CDFILIAL
      AND CDCIA            = ITEM_TO_EDIT.CDCIA
      AND CDMANCIA         = ITEM_TO_EDIT.CDMANCIA
      AND NRSERVICOCIAFILI = ITEM_TO_EDIT.NRSERVICOCIAFILI
      AND CDCLASSE         = ITEM_TO_EDIT.CDCLASSE
      AND CDSUBGRU         = ITEM_TO_EDIT.CDSUBGRU
      AND NRSEQSUBGRUSEMAM = V_NRSEQSUBGRUSEMAM
      AND CDCICLO          = ITEM_TO_EDIT.CDCICLO
      AND CDPRODUTO        = ITEM_TO_EDIT.CDPRODUTONOVO
      AND NRORG            = ITEM_TO_EDIT.NRORG;
    END IF;
  IF (ITEM_TO_EDIT.IDPRODRECNOVO = 'R') THEN
    UPDATE RECSUBSEMAM
    SET VRPESOGRAMATURA  = ITEM_TO_EDIT.VRPESOGRAMATURA ,
      QTGRAMATURA        = ITEM_TO_EDIT.QTGRAMATURA ,
      QTRECSUMA          = ITEM_TO_EDIT.QTPRODREC ,
      DTULTATU           = SYSDATE ,
      NRORGULTATU        = P_NRORG ,
      CDOPERULTATU       = P_CDOPERADOR
    WHERE CDFILIAL       = ITEM_TO_EDIT.CDFILIAL
    AND CDCIA            = ITEM_TO_EDIT.CDCIA
    AND CDMANCIA         = ITEM_TO_EDIT.CDMANCIA
    AND NRSERVICOCIAFILI = ITEM_TO_EDIT.NRSERVICOCIAFILI
    AND CDCLASSE         = ITEM_TO_EDIT.CDCLASSE
    AND CDSUBGRU         = ITEM_TO_EDIT.CDSUBGRU
    AND NRSEQSUBGRUSEMAM = V_NRSEQSUBGRUSEMAM
    AND CDCICLO          = ITEM_TO_EDIT.CDCICLO
    AND CDRECEITA        = ITEM_TO_EDIT.CDRECEITANOVO
    AND NRORG            = ITEM_TO_EDIT.NRORG;
  END IF;
END LOOP;
FOR ITEM_TO_SUBS IN
(SELECT           *
FROM EDITSUBSCOMPMENU
WHERE NRSUBSCOMPMENU = P_NRSUBSCOMPMENU
AND IDTIPOACAO       = 'S'
AND CDFILIAL         = SUBGRUPO.CDFILIAL
AND CDCIA            = SUBGRUPO.CDCIA
AND CDMANCIA         = SUBGRUPO.CDMANCIA
AND NRSERVICOCIAFILI = SUBGRUPO.NRSERVICOCIAFILI
AND CDCLASSE         = SUBGRUPO.CDCLASSE
AND CDSUBGRU         = SUBGRUPO.CDSUBGRU
AND NRSEQSUBGRUSEMAM = SUBGRUPO.NRSEQSUBGRUSEMAM
AND NRORG            = SUBGRUPO.NRORG
)
LOOP
  IF (ITEM_TO_SUBS.IDPRODREC = 'P') THEN
    DELETE
    FROM PRODSUBSEMAM
    WHERE CDFILIAL       = ITEM_TO_SUBS.CDFILIAL
    AND CDCIA            = ITEM_TO_SUBS.CDCIA
    AND CDMANCIA         = ITEM_TO_SUBS.CDMANCIA
    AND NRSERVICOCIAFILI = ITEM_TO_SUBS.NRSERVICOCIAFILI
    AND CDCLASSE         = ITEM_TO_SUBS.CDCLASSE
    AND CDSUBGRU         = ITEM_TO_SUBS.CDSUBGRU
    AND NRSEQSUBGRUSEMAM = V_NRSEQSUBGRUSEMAM
    AND CDCICLO          = ITEM_TO_SUBS.CDCICLO
    AND CDPRODUTO        = ITEM_TO_SUBS.CDPRODUTO
    AND NRORG            = ITEM_TO_SUBS.NRORG;
  END IF;
IF (ITEM_TO_SUBS.IDPRODREC = 'R') THEN
  DELETE
  FROM RECSUBSEMAM
  WHERE CDFILIAL       = ITEM_TO_SUBS.CDFILIAL
  AND CDCIA            = ITEM_TO_SUBS.CDCIA
  AND CDMANCIA         = ITEM_TO_SUBS.CDMANCIA
  AND NRSERVICOCIAFILI = ITEM_TO_SUBS.NRSERVICOCIAFILI
  AND CDCLASSE         = ITEM_TO_SUBS.CDCLASSE
  AND CDSUBGRU         = ITEM_TO_SUBS.CDSUBGRU
  AND NRSEQSUBGRUSEMAM = V_NRSEQSUBGRUSEMAM
  AND CDCICLO          = ITEM_TO_SUBS.CDCICLO
  AND CDRECEITA        = ITEM_TO_SUBS.CDRECEITA
  AND NRORG            = ITEM_TO_SUBS.NRORG;
END IF;
IF (ITEM_TO_SUBS.IDPRODRECNOVO = 'P') THEN
  INSERT
  INTO PRODSUBSEMAM
    (
      CDFILIAL,
      CDCIA,
      CDMANCIA,
      NRSERVICOCIAFILI,
      CDCLASSE,
      CDSUBGRU,
      NRSEQSUBGRUSEMAM,
      CDCICLO,
      CDPRODUTO,
      VRPESOGRAMATURA,
      QTGRAMATURA,
      QTPRODSUMA,
      TXCOPRODSUMA,
      VRCUSPRSUBSEMAM,
      NRITAIRVISION,
      NRORDEM,
      IDANALIZEATIV,
      NRORG,
      DTINCLUSAO,
      CDOPERINCLUSAO,
      DTULTATU,
      NRORGINCLUSAO,
      NRORGULTATU,
      CDOPERULTATU,
      IDATIVO
    )
    VALUES
    (
      ITEM_TO_SUBS.CDFILIAL,
      ITEM_TO_SUBS.CDCIA,
      ITEM_TO_SUBS.CDMANCIA,
      ITEM_TO_SUBS.NRSERVICOCIAFILI,
      ITEM_TO_SUBS.CDCLASSE,
      ITEM_TO_SUBS.CDSUBGRU,
      V_NRSEQSUBGRUSEMAM,
      ITEM_TO_SUBS.CDCICLO,
      ITEM_TO_SUBS.CDPRODUTONOVO,
      ITEM_TO_SUBS.VRPESOGRAMATURA,
      ITEM_TO_SUBS.QTGRAMATURA,
      ITEM_TO_SUBS.QTPRODREC,
      NULL,
      NULL,
      NULL,
      NULL,
      NULL,
      P_NRORG,
      SYSDATE,
      P_CDOPERADOR,
      SYSDATE,
      P_NRORG,
      P_NRORG,
      P_CDOPERADOR,
      'S'
    );
END IF;
IF (ITEM_TO_SUBS.IDPRODRECNOVO = 'R') THEN
  INSERT
  INTO RECSUBSEMAM
    (
      CDFILIAL,
      CDCIA,
      CDMANCIA,
      NRSERVICOCIAFILI,
      CDCLASSE,
      CDSUBGRU,
      NRSEQSUBGRUSEMAM,
      CDCICLO,
      CDRECEITA,
      QTRECSUMA,
      VRCUSRESUBSEMAM,
      TXCOMRECSUMA,
      NRITAIRVISION,
      IDCUSTOPRODZERO,
      NRORDEM,
      QTGRAMATURA,
      VRPESOGRAMATURA,
      IDANALIZEATIV,
      NRORG,
      DTINCLUSAO,
      CDOPERINCLUSAO,
      DTULTATU,
      NRORGINCLUSAO,
      NRORGULTATU,
      CDOPERULTATU,
      IDATIVO
    )
    VALUES
    (
      ITEM_TO_SUBS.CDFILIAL,
      ITEM_TO_SUBS.CDCIA,
      ITEM_TO_SUBS.CDMANCIA,
      ITEM_TO_SUBS.NRSERVICOCIAFILI,
      ITEM_TO_SUBS.CDCLASSE,
      ITEM_TO_SUBS.CDSUBGRU,
      V_NRSEQSUBGRUSEMAM,
      ITEM_TO_SUBS.CDCICLO,
      ITEM_TO_SUBS.CDRECEITANOVO,
      ITEM_TO_SUBS.QTPRODREC,
      NULL,
      NULL,
      NULL,
      NULL,
      NULL,
      ITEM_TO_SUBS.QTGRAMATURA,
      ITEM_TO_SUBS.VRPESOGRAMATURA,
      NULL,
      P_NRORG,
      SYSDATE,
      P_CDOPERADOR,
      SYSDATE,
      P_NRORG,
      P_NRORG,
      P_CDOPERADOR,
      'S'
    );
END IF;
END LOOP;
FOR ITEM_TO_ADD IN
(SELECT          *
  FROM EDITSUBSCOMPMENU
  WHERE NRSUBSCOMPMENU = P_NRSUBSCOMPMENU
  AND IDTIPOACAO       = 'A'
  AND NRORG            = SUBGRUPO.NRORG
)
LOOP
  FOR CICLO IN
  (SELECT REGEXP_SUBSTR(SUBGRUPO.CICLOS, '[^-]+', 1, LEVEL) AS CDCICLO
    FROM DUAL
      CONNECT BY REGEXP_SUBSTR(SUBGRUPO.CICLOS, '[^-]+', 1, LEVEL) IS NOT NULL
  )
  LOOP
    IF
      (
        ITEM_TO_ADD.IDPRODRECNOVO = 'P'
      )
      THEN
      INSERT
      INTO PRODSUBSEMAM
        (
          CDFILIAL,
          CDCIA,
          CDMANCIA,
          NRSERVICOCIAFILI,
          CDCLASSE,
          CDSUBGRU,
          NRSEQSUBGRUSEMAM,
          CDCICLO,
          CDPRODUTO,
          VRPESOGRAMATURA,
          QTGRAMATURA,
          QTPRODSUMA,
          TXCOPRODSUMA,
          VRCUSPRSUBSEMAM,
          NRITAIRVISION,
          NRORDEM,
          IDANALIZEATIV,
          NRORG,
          DTINCLUSAO,
          CDOPERINCLUSAO,
          DTULTATU,
          NRORGINCLUSAO,
          NRORGULTATU,
          CDOPERULTATU,
          IDATIVO
        )
        VALUES
        (
          SUBGRUPO.CDFILIAL,
          SUBGRUPO.CDCIA,
          SUBGRUPO.CDMANCIA,
          SUBGRUPO.NRSERVICOCIAFILI,
          SUBGRUPO.CDCLASSE,
          SUBGRUPO.CDSUBGRU,
          V_NRSEQSUBGRUSEMAM,
          CICLO.CDCICLO,
          ITEM_TO_ADD.CDPRODUTONOVO,
          ITEM_TO_ADD.VRPESOGRAMATURA,
          ITEM_TO_ADD.QTGRAMATURA,
          ITEM_TO_ADD.QTPRODREC,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          P_NRORG,
          SYSDATE,
          P_CDOPERADOR,
          SYSDATE,
          P_NRORG,
          P_NRORG,
          P_CDOPERADOR,
          'S'
        );
    END IF;
  IF (ITEM_TO_ADD.IDPRODRECNOVO = 'R') THEN
    INSERT
    INTO RECSUBSEMAM
      (
        CDFILIAL,
        CDCIA,
        CDMANCIA,
        NRSERVICOCIAFILI,
        CDCLASSE,
        CDSUBGRU,
        NRSEQSUBGRUSEMAM,
        CDCICLO,
        CDRECEITA,
        QTRECSUMA,
        VRCUSRESUBSEMAM,
        TXCOMRECSUMA,
        NRITAIRVISION,
        IDCUSTOPRODZERO,
        NRORDEM,
        QTGRAMATURA,
        VRPESOGRAMATURA,
        IDANALIZEATIV,
        NRORG,
        DTINCLUSAO,
        CDOPERINCLUSAO,
        DTULTATU,
        NRORGINCLUSAO,
        NRORGULTATU,
        CDOPERULTATU,
        IDATIVO
      )
      VALUES
      (
        SUBGRUPO.CDFILIAL,
        SUBGRUPO.CDCIA,
        SUBGRUPO.CDMANCIA,
        SUBGRUPO.NRSERVICOCIAFILI,
        SUBGRUPO.CDCLASSE,
        SUBGRUPO.CDSUBGRU,
        V_NRSEQSUBGRUSEMAM,
        CICLO.CDCICLO,
        ITEM_TO_ADD.CDRECEITANOVO,
        ITEM_TO_ADD.QTPRODREC,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        ITEM_TO_ADD.QTGRAMATURA,
        ITEM_TO_ADD.VRPESOGRAMATURA,
        NULL,
        P_NRORG,
        SYSDATE,
        P_CDOPERADOR,
        SYSDATE,
        P_NRORG,
        P_NRORG,
        P_CDOPERADOR,
        'S'
      );
  END IF;
END LOOP;
END LOOP;
END IF;
END LOOP;
  UPDATE SUBSCOMPMENU SET IDSITUACAO = 'A', DTAPROVA = SYSDATE, CDOPERAPROVA = CASE WHEN CDOPERAPROVA IS NULL THEN P_CDOPERADOR ELSE CDOPERAPROVA END, DTULTATU = SYSDATE, CDOPERULTATU = P_CDOPERADOR, NRORGULTATU = P_NRORG WHERE NRSUBSCOMPMENU = P_NRSUBSCOMPMENU AND NRORG = P_NRORG;
END;
/