CREATE OR REPLACE PROCEDURE GERA_RATEIO_CONTA(
     P_IDENTRSAID  IN NOTAFISCAL.IDENTRSAIDOP%TYPE,
   P_CDFILIAL    IN RATCTPAGAR.CDFILIAL%TYPE,
   P_LANCAMENTO  IN RATCTPAGAR.NRLANCTONF%TYPE,
   P_NRSEQVENC   IN RATCTPAGAR.NRSEQVENPAG%TYPE,
   P_NRPARCELA   IN RATCTPAGAR.NRPARCELAPAG%TYPE,
   P_VRCONTA     IN CONTAPAGAR.VRATUAPAG%TYPE,
   P_CDTIPOCONTA IN CONTAPAGAR.CDTPCTPAGAR%TYPE,
   P_CDFILIRAT   IN RATCTPAGAR.CDFIRATCTPAG%TYPE)
AS
   V_CDCENTCUST   FILIAL.CDCENTCUST%TYPE;
   V_VALORRAT     RATCTPAGAR.VRRATCCCTPAG%TYPE;
   V_RATTPCT      NUMBER;
   V_CDFILIAL     FILIAL.CDFILIAL%TYPE;

   CURSOR C_RATTPCTPAG IS
      SELECT CDFILIAL, CDCENTCUST, CDFIRATTPCTP, VRPERATTPCTP
      FROM  RATTPCTP
      WHERE CDTPCTPAGAR = P_CDTIPOCONTA;

   CURSOR C_RATTPCTREC IS
      SELECT CDFILIAL, CDCENTCUST, CDFIRATTPCTR, VRPERATTPCTR
      FROM  RATTPCTR
      WHERE CDTPCTRECEB = P_CDTIPOCONTA;


BEGIN
   SELECT DECODE(P_IDENTRSAID, 'E', CDFILIRECE, CDFILIEMIT)
     INTO V_CDFILIAL
     FROM NOTAFISCAL
    WHERE CDFILIAL   = P_CDFILIAL
      AND NRLANCTONF = P_LANCAMENTO;
   V_CDFILIAL := NVL(P_CDFILIRAT,V_CDFILIAL);
  
   SELECT MAX(CDCENTCUST)
   INTO V_CDCENTCUST
   FROM FILIAL
   WHERE CDFILIAL = V_CDFILIAL;
   
   IF P_IDENTRSAID = 'E' THEN
      /* Para a entrada gera rateio de Contas a pagar*/
      SELECT COUNT(*)
      INTO V_RATTPCT
      FROM  RATTPCTP
      WHERE CDTPCTPAGAR = P_CDTIPOCONTA;

      IF V_RATTPCT > 0 THEN
         FOR C_RATTPCT IN C_RATTPCTPAG LOOP
            V_VALORRAT := (P_VRCONTA * C_RATTPCT.VRPERATTPCTP)/100;
            IF C_RATTPCT.CDFILIAL IS NULL THEN
                V_CDFILIAL := V_CDFILIAL;
            ELSE
                V_CDFILIAL := C_RATTPCT.CDFIRATTPCTP;
            END IF;
            INSERT INTO RATCTPAGAR
               (CDFILIAL, NRLANCTONF, NRSEQVENPAG,
                NRPARCELAPAG, CDCENTCUST, CDFIRATCTPAG,
                VRRATCCCTPAG)
            VALUES
               (P_CDFILIAL, P_LANCAMENTO, P_NRSEQVENC,
                P_NRPARCELA, C_RATTPCT.CDCENTCUST, V_CDFILIAL,
                V_VALORRAT);
         END LOOP;
      ELSE
         IF V_CDCENTCUST IS NOT NULL THEN      
            INSERT INTO RATCTPAGAR
               (CDFILIAL, NRLANCTONF, NRSEQVENPAG,
                NRPARCELAPAG, CDCENTCUST, CDFIRATCTPAG,
                VRRATCCCTPAG)
            VALUES
               (P_CDFILIAL, P_LANCAMENTO, P_NRSEQVENC,
                P_NRPARCELA, V_CDCENTCUST, V_CDFILIAL,
                P_VRCONTA);
          END IF;    
      END IF;
   ELSE
      /* Para a entrada gera rateio de Contas a Receber*/
      SELECT COUNT(*)
      INTO V_RATTPCT
      FROM  RATTPCTR
      WHERE CDTPCTRECEB = P_CDTIPOCONTA;

      IF V_RATTPCT > 0 THEN
         FOR C_RATTPCT IN C_RATTPCTREC LOOP
            V_VALORRAT := (P_VRCONTA * C_RATTPCT.VRPERATTPCTR)/100;
            IF C_RATTPCT.CDFILIAL IS NULL THEN
                V_CDFILIAL := P_CDFILIRAT;
            ELSE
                V_CDFILIAL := C_RATTPCT.CDFIRATTPCTR;
            END IF;
            --raise_application_error(-20501, P_CDFILIRAT, True);
            INSERT INTO RATCTRECEBER
               (CDFILIAL, NRLANCTONF, NRSEQVENREC,
                NRPARCELAREC, CDCENTCUST, CDFIRATCTREC,
                VRRATCCCTREC)
            VALUES
               (P_CDFILIAL, P_LANCAMENTO, P_NRSEQVENC,
                P_NRPARCELA, C_RATTPCT.CDCENTCUST, V_CDFILIAL,
                V_VALORRAT);

         END LOOP;
      ELSE
         IF V_CDCENTCUST IS NOT NULL THEN      
            INSERT INTO RATCTRECEBER
               (CDFILIAL, NRLANCTONF, NRSEQVENREC,
                NRPARCELAREC, CDCENTCUST, CDFIRATCTREC,
                VRRATCCCTREC)
            VALUES
               (P_CDFILIAL, P_LANCAMENTO, P_NRSEQVENC,
                P_NRPARCELA, V_CDCENTCUST, P_CDFILIRAT,
                P_VRCONTA);
          END IF;
      END IF;
   END IF;
END;
/

