--------------------------------------------------------
--  DDL for Function VALOR_VENDA
--------------------------------------------------------

  CREATE OR REPLACE FUNCTION VALOR_VENDA (
    P_NRORG        IN ITEMVENDA.NRORG%TYPE,
    P_CDFILIAL     IN ITEMVENDA.CDFILIAL%TYPE,
    P_CDCAIXA      IN ITEMVENDA.CDCAIXA%TYPE,
    P_NRSEQVENDA   IN ITEMVENDA.NRSEQVENDA%TYPE
) RETURN NUMBER IS
    V_VRVENDA   NUMBER;
    V_VRITEM    NUMBER;
BEGIN
    V_VRVENDA := 0;
    SELECT
        SUM(ROUND(I.QTPRODVEND * (I.VRUNITVEND + NVL(I.VRUNITVENDCL,0) ) - NVL(I.VRDESITVEND,0) + NVL(I.VRACRITVEND,0),2) ) AS VRITEM
    INTO
        V_VRITEM
    FROM
        ITEMVENDA I
    WHERE
        ( I.NRORG = P_NRORG )
        AND   ( I.CDFILIAL = P_CDFILIAL )
        AND   ( I.CDCAIXA = P_CDCAIXA )
        AND   ( I.NRSEQVENDA = P_NRSEQVENDA );

    V_VRVENDA := V_VRITEM;
    RETURN(V_VRVENDA);
END;
