CREATE OR REPLACE FUNCTION JUROS_SIMPLES
   (P_CDCLIENTE    IN CLIENTE.CDCLIENTE%TYPE,
    P_VRORIGREC    IN CONTARECEBER.VRORIGREC%TYPE,
    P_DTVENBOLREC  IN BOLETRECEBER.DTVENBOLREC%TYPE,
    P_CDEMPRESA    IN CONTARECEBER.CDEMPRESA%TYPE)  RETURN NUMBER
IS
   V_JUROS         NUMBER;
   S_ATRASO        VARCHAR2(10);
   AUX_MULTA       NUMBER;
   AUX_MORA        NUMBER;
   PERMORCLI       NUMBER;
   PERMOREMP       NUMBER;
   PERMULCLI       NUMBER;
   PERMULEMP       NUMBER;
BEGIN
   V_JUROS := 0;
   S_ATRASO       := TRUNC(SYSDATE - P_DTVENBOLREC);

   SELECT VRPEMORBCLI, VRPEMULBCLI INTO PERMORCLI, PERMULCLI
   FROM   CLIENTE
   WHERE
    (CDCLIENTE = P_CDCLIENTE);

   IF PERMORCLI IS NULL THEN
      SELECT VRPEMOREMPR, VRPEMULEMPR INTO PERMOREMP, PERMULEMP
      FROM
         EMPRESA
      WHERE
         (CDEMPRESA = P_CDEMPRESA);
   END IF;

   IF NOT (PERMORCLI IS NULL) THEN
      AUX_MORA := PERMORCLI;
   ELSE
      AUX_MORA := PERMOREMP;
   END IF;

   IF (AUX_MORA > 0) THEN
      V_JUROS := (ROUND((P_VRORIGREC*(AUX_MORA/100)*S_ATRASO),2));
   END IF;

   IF NOT (PERMULCLI IS NULL) THEN
      AUX_MULTA := PERMULCLI;
   ELSE
      AUX_MULTA := PERMULEMP;
   END IF;

   IF (AUX_MULTA > 0) THEN
      V_JUROS := V_JUROS + (ROUND((P_VRORIGREC*(AUX_MULTA/100)),2));
   END IF;

   RETURN(V_JUROS);
END;
/
