/**
 * Apaga um servi�o da proposta, com todos os detalhes e hist�rico
 * 
 * @param {Number} P_NRSERVICOPROPCOM N�mero do servi�o da proposta 
 *revisada
 */
CREATE OR REPLACE PROCEDURE DELETA_SERVICOPROPCOM
(
  P_NRSERVICOPROPCOM IN NUMBER
)
AS  
  V_IDSITUACAO VARCHAR2(30);
BEGIN
   V_IDSITUACAO := SIT_SERVICOPROPCOM(P_NRSERVICOPROPCOM);

   IF V_IDSITUACAO NOT IN ('PENDENTE', 'CALCULADO', 'FINALIZADOPLANEJAMENTO', 'RECUSADO') THEN
      RAISE_APPLICATION_ERROR(-20000, MSG('TCOPROC_SERVPROPCOMERCIAL_N_PODE_EXCLUIR'));
   END IF;

   -- Hist�rico de notifica��es
   DELETE FROM HISTNOTIFPROPCOM
    WHERE NRSERVICOPROPCOM = P_NRSERVICOPROPCOM;

   -- limpa detalhes do servi�o
   UPDATE SERVICOPROPCOM SET NRDETALHESERVPROPCOM = NULL, NRDETSERVPROPCOMPLAN = NULL
    WHERE NRSERVICOPROPCOM = P_NRSERVICOPROPCOM;

   -- Configura��o de servi�o
   DELETE FROM CONFIGSERVPROPCOM C
    WHERE EXISTS (
             SELECT 1
               FROM DETALHESERVPROPCOM D
              WHERE D.NRSERVICOPROPCOM = P_NRSERVICOPROPCOM
                AND D.NRDETALHESERVPROPCOM = C.NRDETALHESERVPROPCOM
          );
   
   -- Configura��o de grupo de tipo de prato
   DELETE FROM CONFGRUPOTPPROPCOM C
    WHERE EXISTS (
             SELECT 1
               FROM DETALHESERVPROPCOM D
              WHERE D.NRSERVICOPROPCOM = P_NRSERVICOPROPCOM
                AND D.NRDETALHESERVPROPCOM = C.NRDETALHESERVPROPCOM
          );
   
   -- Incid�ncias solicitadas
   DELETE FROM INCIDENCIAPROPCOM I
    WHERE EXISTS (
             SELECT 1
               FROM DETALHESERVPROPCOM D
              WHERE D.NRSERVICOPROPCOM = P_NRSERVICOPROPCOM
                AND D.NRDETALHESERVPROPCOM = I.NRDETALHESERVPROPCOM
          );

   -- Produto
   DELETE FROM PRODUTOPROPCOM P
    WHERE EXISTS (
             SELECT 1
               FROM DETALHESERVPROPCOM D
              WHERE D.NRSERVICOPROPCOM = P_NRSERVICOPROPCOM
                AND D.NRDETALHESERVPROPCOM = P.NRDETALHESERVPROPCOM
          );

   -- Detalhes do Servi�o
   DELETE FROM DETALHESERVPROPCOM
    WHERE NRSERVICOPROPCOM = P_NRSERVICOPROPCOM;

   -- Servi�o
   DELETE FROM SERVICOPROPCOM
    WHERE NRSERVICOPROPCOM = P_NRSERVICOPROPCOM;   
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('DELETA_SERVICOPROPCOM','6.35.009'); END;
/
