CREATE OR REPLACE PROCEDURE APROVA_SOLEMERGPROD
(
P_CDFILIAL IN VARCHAR2,
P_NRSOLEMERGPROD IN NUMBER,
P_CDOPERADOR IN VARCHAR2,
P_NRORG IN NUMBER,
P_NRLISTAERROS OUT NUMBER
)
AS
  V_IDSITUACAO VARCHAR2(30);
BEGIN

   SELECT MAX(IDSITUACAO)
     INTO V_IDSITUACAO
     FROM SOLEMERGPROD
    WHERE CDFILIAL = P_CDFILIAL
      AND NRSOLEMERGPROD = P_NRSOLEMERGPROD;

   IF V_IDSITUACAO IS NULL THEN
      RETURN;
   END IF;

   IF V_IDSITUACAO <> 'PENDENTE' THEN
      /*Operacao bloqueada. Solicitacao [NRSOLEMERGPROD] ja aprovada.*/
      RAISE_APPLICATION_ERROR(-20501, 'APPERRMSGSTR' || 'APP-00138' || '$$' || 
                                       TO_CHAR(P_NRSOLEMERGPROD)
                                       || 'APPERRMSGEND');
   END IF;

   GERA_SOLEMERGPROD_CKLISTS(P_CDFILIAL, P_NRSOLEMERGPROD, P_CDOPERADOR, P_NRORG, P_NRLISTAERROS);


   /*Aprova a solicitacao emergencial*/
   UPDATE SOLEMERGPROD
      SET IDSITUACAO = 'APROVADA',
          NRORGULTATU = P_NRORG,
          CDOPERULTATU = P_CDOPERADOR,
          DTULTATU = SYSDATE
    WHERE CDFILIAL = P_CDFILIAL
      AND NRSOLEMERGPROD = P_NRSOLEMERGPROD;
END;
/