CREATE OR REPLACE PROCEDURE EXCLUI_SERVICO_MENUGRID
(
  P_CDFILIAL IN VARCHAR2,
  P_CDCIA IN VARCHAR2,
  P_CDMANCIA IN VARCHAR2,
  P_NRSERVICOCIAFILI IN VARCHAR2,
  P_CDCLASSE IN VARCHAR2,
  P_NRORG IN NUMBER DEFAULT 1
)
AS
   V_IDSITUAPROPCIA VARCHAR2(1);  
BEGIN
    /*
  Verifica se o status do manual de destino
  */
  SELECT MAX(IDSITUAPROPCIA)
    INTO V_IDSITUAPROPCIA
    FROM MANUALCIAM
   WHERE CDFILIAL = P_CDFILIAL
     AND CDCIA = P_CDCIA
     AND CDMANCIA = P_CDMANCIA;

  /*Operacao bloqueada. Manual de destino ja esta em proposta ou aprovado.*/
  IF V_IDSITUAPROPCIA NOT IN ('E', 'A') THEN
     RAISE_APPLICATION_ERROR(-20501, 'APPERRMSGSTR' || 'APP-00046' || 'APPERRMSGEND' , TRUE);
  END IF;

  FOR CUR_SUBGRU IN
   (SELECT CDSUBGRU, NRSEQSUBGRUSEMAM 
      FROM SUBGRUSEMAM
     WHERE CDFILIAL = P_CDFILIAL
       AND CDCIA = P_CDCIA
       AND CDMANCIA = P_CDMANCIA
       AND NRSERVICOCIAFILI = P_NRSERVICOCIAFILI
       AND CDCLASSE = P_CDCLASSE) LOOP
      EXCLUI_SUBGRUPO_MENUGRID(P_CDFILIAL, P_CDCIA, P_CDMANCIA, P_NRSERVICOCIAFILI, P_CDCLASSE, 
                               CUR_SUBGRU.CDSUBGRU, CUR_SUBGRU.NRSEQSUBGRUSEMAM);
      
  END LOOP;
  
  /*Apaga, se existir, manutencoes de menu grid*/
  DELETE FROM AUXRECSUBSEMAM
   WHERE CDFILIAL = P_CDFILIAL
     AND CDCIA = P_CDCIA
     AND CDMANCIA = P_CDMANCIA
     AND NRSERVICOCIAFILI = P_NRSERVICOCIAFILI
     AND CDCLASSE = P_CDCLASSE;
    
  DELETE FROM AUXPRODSUBSEMAM
   WHERE CDFILIAL = P_CDFILIAL
     AND CDCIA = P_CDCIA
     AND CDMANCIA = P_CDMANCIA
     AND NRSERVICOCIAFILI = P_NRSERVICOCIAFILI
     AND CDCLASSE = P_CDCLASSE;
    
  DELETE FROM AUXSUBGRUPRINCMENU
   WHERE CDFILIAL = P_CDFILIAL
     AND CDCIA = P_CDCIA
     AND CDMANCIA = P_CDMANCIA
     AND NRSERVICOCIAFILI = P_NRSERVICOCIAFILI
     AND CDCLASSE = P_CDCLASSE;
    
  DELETE FROM AUXMONTASUBSEMAM
   WHERE CDFILIAL = P_CDFILIAL
     AND CDCIA = P_CDCIA
     AND CDMANCIA = P_CDMANCIA
     AND NRSERVICOCIAFILI = P_NRSERVICOCIAFILI
     AND CDCLASSE = P_CDCLASSE;
    
  DELETE FROM AUXCICLOSUBGRUSEMAM
   WHERE CDFILIAL = P_CDFILIAL
     AND CDCIA = P_CDCIA
     AND CDMANCIA = P_CDMANCIA
     AND NRSERVICOCIAFILI = P_NRSERVICOCIAFILI
     AND CDCLASSE = P_CDCLASSE;
    
  DELETE FROM AUXPROCIASUBSEMAM
   WHERE CDFILIAL = P_CDFILIAL
     AND CDCIA = P_CDCIA
     AND CDMANCIA = P_CDMANCIA
     AND NRSERVICOCIAFILI = P_NRSERVICOCIAFILI
     AND CDCLASSE = P_CDCLASSE;
    
  DELETE FROM AUXPROPSUBGRUSE
   WHERE CDFILIAL = P_CDFILIAL
     AND CDCIA = P_CDCIA
     AND CDMANCIA = P_CDMANCIA
     AND NRSERVICOCIAFILI = P_NRSERVICOCIAFILI
     AND CDCLASSE = P_CDCLASSE;
    
  DELETE FROM AUXSUBGRUSEMAM
   WHERE CDFILIAL = P_CDFILIAL
     AND CDCIA = P_CDCIA
     AND CDMANCIA = P_CDMANCIA
     AND NRSERVICOCIAFILI = P_NRSERVICOCIAFILI
     AND CDCLASSE = P_CDCLASSE;
         
  DELETE FROM SERVMANUALM
   WHERE CDFILIAL = P_CDFILIAL
     AND CDCIA = P_CDCIA
     AND CDMANCIA = P_CDMANCIA
     AND NRSERVICOCIAFILI = P_NRSERVICOCIAFILI
     AND CDCLASSE = P_CDCLASSE;
END;
/