CREATE OR REPLACE FUNCTION MMPM_SUBGRU_COC(
  P_CDFILIAL         IN VARCHAR2,
  P_CDCIA            IN VARCHAR2,
  P_CDMANCIA         IN VARCHAR2,
  P_NRSERVICOCIAFILI IN NUMBER,
  P_CDCLASSE         IN VARCHAR2,
  P_CDSUBGRU         IN VARCHAR2,
  P_NRSEQSUBGRUSEMAM IN NUMBER,
  P_CDCICLO          IN VARCHAR2,
  P_NRORG            IN NUMBER)
RETURN FLOAT
AS
  V_SOMAREC FLOAT;
BEGIN
  SELECT SUM((MMPM_RECEITAM_COC(RSM.CDFILIAL, REC.CDRECEITA,
    (SELECT NRVARIACAOREC
    FROM FILIRECEITA
    WHERE CDRECEITA = RSM.CDRECEITA
    AND CDFILIAL    = RSM.CDFILIAL
    ))) * RSM.QTRECSUMA)
  INTO V_SOMAREC
  FROM RECSUBSEMAM RSM
  INNER JOIN RECEITAM REC
  ON REC.CDRECEITA         = RSM.CDRECEITA
  AND REC.NRORG            = RSM.NRORG
  WHERE RSM.CDFILIAL       = P_CDFILIAL
  AND RSM.CDCIA            = P_CDCIA
  AND RSM.CDMANCIA         = P_CDMANCIA
  AND RSM.NRSERVICOCIAFILI = P_NRSERVICOCIAFILI
  AND RSM.CDCLASSE         = P_CDCLASSE
  AND RSM.CDSUBGRU         = P_CDSUBGRU
  AND RSM.NRSEQSUBGRUSEMAM = P_NRSEQSUBGRUSEMAM
  AND RSM.CDCICLO          = P_CDCICLO
  AND RSM.NRORG            = P_NRORG;
  RETURN NVL(V_SOMAREC, 0);
END;
/