CREATE OR REPLACE PROCEDURE EXCLUI_MENUGRID
(
  P_CDFILIAL IN VARCHAR2,
  P_CDCIA IN VARCHAR2,
  P_CDMANCIA 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 <> 'E' THEN     
     RAISE_APPLICATION_ERROR(-20501, 'APPERRMSGSTR' || 'APP-00046' || 'APPERRMSGEND' , TRUE);
  END IF;

   FOR CUR_SERVICOS IN
   (SELECT NRSERVICOCIAFILI, CDCLASSE
      FROM SERVMANUALM
     WHERE CDFILIAL = P_CDFILIAL
       AND CDCIA = P_CDCIA
       AND CDMANCIA = P_CDMANCIA) LOOP
      EXCLUI_SERVICO_MENUGRID(P_CDFILIAL, P_CDCIA, P_CDMANCIA, CUR_SERVICOS.NRSERVICOCIAFILI, CUR_SERVICOS.CDCLASSE);
   END LOOP;
         
   DELETE FROM MANUALCIAM
    WHERE CDFILIAL = P_CDFILIAL
      AND CDCIA = P_CDCIA
      AND CDMANCIA = P_CDMANCIA;
END;
/