CREATE OR REPLACE FUNCTION CUSTO_PERC_CARDTRAB
(
   P_NRCARDAPIOTRAB IN NUMBER,
   P_IDTIPO IN VARCHAR2 DEFAULT NULL
) RETURN FLOAT
AS
   V_VRCUSTO FLOAT := 0;
BEGIN
   FOR C_COLS IN
   (
      SELECT CUSTO_PERC_COL_CARDTRAB(NRCARDAPIOTRAB, NRCONFIGCARDTRAB) AS VRCUSTO
        FROM
      (
      SELECT NRCARDAPIOTRAB, NRCONFIGCARDTRAB
        FROM CONFIGCARDTRAB
       WHERE NRCARDAPIOTRAB = P_NRCARDAPIOTRAB
         AND (P_IDTIPO IS NULL OR IDCLASSIFICACAO = P_IDTIPO)
      ) S
   ) LOOP
      V_VRCUSTO := V_VRCUSTO + C_COLS.VRCUSTO;
   END LOOP;

   RETURN ROUND(V_VRCUSTO, 5);
END;
/