CREATE OR REPLACE PROCEDURE APROVA_PLANAPREINF
(
P_CDFILIAL IN VARCHAR2,
P_NRPLANAPREINF 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 PLANAPREINF
    WHERE CDFILIAL = P_CDFILIAL
      AND NRPLANAPREINF = P_NRPLANAPREINF;

   IF V_IDSITUACAO IS NULL THEN
      RETURN;
   END IF;

   IF V_IDSITUACAO <> 'PENDENTE' THEN
      /*Operacao bloqueada. Planejamento [NRPLANAPREINF] ja aprovado.*/
      RAISE_APPLICATION_ERROR(-20501, 'APPERRMSGSTR' || 'APP-00131' || '$$' || 
                                       TO_CHAR(P_NRPLANAPREINF)
                                       || 'APPERRMSGEND');
   END IF;

   GERA_PLANAPREINF_CKLISTS(P_CDFILIAL, P_NRPLANAPREINF, P_CDOPERADOR, P_NRORG, P_NRLISTAERROS);


   /*Aprova o planejamento de apresentacao/cheftable*/
   UPDATE PLANAPREINF
      SET IDSITUACAO = 'APROVADO',
          NRORGULTATU = P_NRORG,
          CDOPERULTATU = P_CDOPERADOR,
          DTULTATU = SYSDATE
    WHERE CDFILIAL = P_CDFILIAL
      AND NRPLANAPREINF = P_NRPLANAPREINF;
END;
/