create or replace PROCEDURE APURAC_RELGESDIRETA (
      P_CDFILIAL    IN FILIAL.CDFILIAL%TYPE,
      P_DATAINI     IN DATE,
      P_DATAFIN     IN DATE,
      P_NRAPURACGD  IN TRAB_APURAC_GD.NRAPURACGD%TYPE,
      P_CDCLIENTE   IN CLIENTE.CDCLIENTE%TYPE DEFAULT NULL,
      P_EQUIVALENCIA IN VARCHAR2 DEFAULT 'N',
      P_CENTRAL IN VARCHAR2 DEFAULT NULL,
      P_NRLISTA_AGREGADAS IN VARCHAR2 DEFAULT 'T')
   AS
   
   V_UTILIZA_EFET_PLAN VARCHAR2(1);
   V_GERAPREVIA VARCHAR2(1);

BEGIN   
   /* BUSCA OS PARAMETROS DA PARAMGERAL E PARAMFILIAL */
   SELECT MAX(SUBSTR(DSPARAMFATFI,15,1))
   INTO V_UTILIZA_EFET_PLAN
   FROM PARAMFILIAL
   WHERE CDFILIAL = P_CDFILIAL;

   IF (V_UTILIZA_EFET_PLAN IS NULL) OR (V_UTILIZA_EFET_PLAN = ' ') THEN
      V_UTILIZA_EFET_PLAN := 'N';      
   END IF;

   /*APAGA OS REGISTROS DOS DADOS ADICIONAIS PARA A APURACAO*/
   DELETE FROM DADOSADIC_APURAC
   WHERE CDFILIAL = P_CDFILIAL AND
         NRAPURACFAT = P_NRAPURACGD;

   /* APAGA OS REGISTROS DA TRAB_APURAC_GD DE APURACOES ANTERIORES */
   DELETE FROM TRAB_APURAC_GD
   WHERE CDFILIALAPGD  = P_CDFILIAL    AND
         NRAPURACGD    = P_NRAPURACGD;

   /* APAGA OS REGISTROS DA TRAB_APURAC_TU DE APURACOES ANTERIORES */
   DELETE FROM TRAB_APURAC_TU
   WHERE CDFILIALAPTU  = P_CDFILIAL    AND
         NRAPURACTU    = P_NRAPURACGD;

   V_GERAPREVIA := 'R';
   IF P_EQUIVALENCIA = 'S' THEN
      V_GERAPREVIA := 'RE';
   END IF;

   IF (P_CENTRAL = 'S') THEN
      FOR C_CLIENTES IN
      (
        SELECT DISTINCT S.CDCLIENTE
          FROM SUM_EFETIVOS S 
            LEFT JOIN PARAMFILIAL P 
               ON P.CDFILIAL = S.CDFILIAL 
            INNER JOIN CLIENFILIAL CL
               ON CL.CDFILIAL = P_CDFILIAL
               AND CL.CDCLIENTE = S.CDCLIENTE
         WHERE S.DTFATURAM BETWEEN P_DATAINI AND P_DATAFIN AND
              ((S.IDFATUPLAN = 'F') OR (CASE WHEN SUBSTR(DSPARAMFATFI,15,1) = ' ' THEN 'N' ELSE NVL(SUBSTR(DSPARAMFATFI,15,1),'N') END = 'S')) AND
              (S.CDCLIENTE  = P_CDCLIENTE OR P_CDCLIENTE IS NULL) AND
              (S.CDFILIAL IN (
                  SELECT P_CDFILIAL 
                     FROM DUAL
                  UNION ALL
                  SELECT CDFILIAL
                     FROM FILIAL 
                  WHERE 
                     CDFILCENTRALFATU  = P_CDFILIAL 
                     AND VALOR_NA_LISTA(CDFILIAL, P_NRLISTA_AGREGADAS) <> 0
              ))
      ) LOOP     
           APURAC_RELGESDIRETA_CENTRAL (P_CDFILIAL, C_CLIENTES.CDCLIENTE, P_DATAINI, P_DATAFIN,
                        P_NRAPURACGD, 'N', NULL,
                        '01', V_GERAPREVIA, P_NRLISTA_AGREGADAS);
      END LOOP;                        
   ELSE
      FOR C_CLIENTES IN
      (
         SELECT DISTINCT CDCLIENTE
           FROM SUM_EFETIVOS
            WHERE DTFATURAM BETWEEN P_DATAINI AND P_DATAFIN AND
                  ((IDFATUPLAN = 'F') OR (V_UTILIZA_EFET_PLAN = 'S')) AND
                  (CDFILIAL   = P_CDFILIAL) AND
                  (CDCLIENTE  = P_CDCLIENTE OR P_CDCLIENTE IS NULL)
      ) LOOP  
         APURAC_GESDIRETA (P_CDFILIAL, C_CLIENTES.CDCLIENTE, P_DATAINI, P_DATAFIN,
                           P_NRAPURACGD, 'N', NULL,
                           '01', NULL, V_GERAPREVIA,
                           P_CDFILIAL,P_NRAPURACGD);
      END LOOP;
   END IF;
END;
/