CREATE OR REPLACE FUNCTION VALOR_IMPOSTO_ITEM_NFE_CAIXA(
   P_CDFILIAL        IN IMPBAIXAREC.CDFILIAL%TYPE,
   P_NRLANCTONF      IN IMPBAIXAREC.NRLANCTONF%TYPE,
   P_CDIMPOSTONFE    IN IMPBAIXAREC.CDIMPOSTO%TYPE,
   P_IDVALOR         IN ITEMPROIMPOS.IDVALORINFIT%TYPE,
   P_DTBAIXAREC      IN IMPBAIXAREC.DTBAIXAREC%TYPE DEFAULT NULL,
   P_CDFISCAL        IN IMPBAIXAREC.CDFISCALNF%TYPE DEFAULT NULL,
   P_CDFISCOPERNF    IN IMPBAIXAREC.CDFISCOPERNF%TYPE DEFAULT NULL,
   P_CDSITUTRIBITNFE IN IMPBAIXAREC.CDSITUTRIBITNFE%TYPE DEFAULT NULL,
   P_NRSEQUENCIAL    IN IMPBAIXAREC.NRSEQUENCIAL%TYPE DEFAULT NULL) RETURN NUMBER
AS
   V_VRBASECALCNF IMPOSTONF.VRBASECALCNF%TYPE;
   V_VRALIQNF     IMPOSTONF.VRALIQNF%TYPE;
   V_VRIMPOSTONF  IMPOSTONF.VRIMPOSTONF%TYPE;
   V_VRCONTABILNF IMPOSTONF.VRCONTABILNF%TYPE;
BEGIN
   SELECT NVL(SUM(DECODE(IT.CDFISCALNF,'1',ROUND(VRPROPRECBC,2),0)),0),
          NVL(MAX(VRALIQNF),0),
          NVL(SUM(DECODE(IT.CDFISCALNF,'1',ROUND(VRPROPRECBC*VRALIQNF/100,2))),0),
          NVL(SUM(ROUND(VRPROPREC,2)),0)
     INTO V_VRBASECALCNF,
          V_VRALIQNF,
          V_VRIMPOSTONF,
          V_VRCONTABILNF
     FROM IMPBAIXAREC IT, IMPOSTO IM
    WHERE IT.CDFILIAL        = P_CDFILIAL
      AND IT.NRLANCTONF      = P_NRLANCTONF
      AND IT.CDIMPOSTO       = IM.CDIMPOSTO
      AND IM.CDIMPOSTONFE    = P_CDIMPOSTONFE
      AND IT.DTBAIXAREC      = NVL(P_DTBAIXAREC, IT.DTBAIXAREC)
      AND IT.CDFISCALNF      = NVL(P_CDFISCAL, IT.CDFISCALNF)
      AND NVL(IT.CDFISCOPERNF,'!') = NVL(NVL(P_CDFISCOPERNF, IT.CDFISCOPERNF),'!')
      AND IT.CDSITUTRIBITNFE = NVL(P_CDSITUTRIBITNFE, IT.CDSITUTRIBITNFE)
      AND IT.NRSEQUENCIAL    = NVL(P_NRSEQUENCIAL, IT.NRSEQUENCIAL);

   IF P_IDVALOR = 'A' THEN
      RETURN(V_VRALIQNF);
   ELSIF P_IDVALOR = 'B' THEN
      RETURN(V_VRBASECALCNF);
   ELSIF P_IDVALOR = 'I' THEN
      RETURN(V_VRIMPOSTONF);
   ELSIF P_IDVALOR = 'C' THEN
      RETURN(V_VRCONTABILNF);
   END IF;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('VALOR_IMPOSTO_ITEM_NFE_CAIXA','5.75.000'); END;
/

