CREATE OR REPLACE PROCEDURE FCT_CALC_MODPAG (
      P_NRORG IN NUMBER, 
      P_DTREFERENCIA IN DATE,
      P_CDFILIAL IN VARCHAR2,
      P_CDMODPGTO IN VARCHAR2,
      P_CDCLIENTE IN VARCHAR2,
      P_IDFATURACONTR IN VARCHAR2,
      P_INDIAS OUT NUMBER,
      P_DTBASE OUT DATE,
      P_IDFORMAPGTO OUT VARCHAR2,
      P_IDTIPODTPGT OUT VARCHAR2,
      P_IDTPPERIODO OUT VARCHAR2,
      P_NRPARCELAS OUT VARCHAR2,
      P_NRPARCPGTO OUT VARCHAR2,
      P_CDFORMPGTO OUT VARCHAR2,
      P_IDVALORPARCELAS OUT VARCHAR2,
      P_BOCONSFERIBANC OUT VARCHAR2)
   IS
      V_IDTPFORPGT VARCHAR2(1);
      V_IDUTDIAPGT VARCHAR2(1);
      V_NRDIAFIXO VARCHAR2(2);
      V_DTAUX DATE;
      V_IDCONSDIAUTIL CHAR;

      CURSOR C_PARAMETRIZACAO_PGTO (P_CDMODPGTO IN MODPGTO.CDMODPGTO%TYPE, P_NRORG IN NUMBER) IS
         SELECT M.IDTPFORPGTOMOD IDTPFORPGT, M.IDUTDIAPAGMOD IDUTDIAPGT, M.NRDIAPGFIXMOD NRDIAPGFIX,
                FP.IDTIPODTPGTO IDTIPODTPGT, M.CDFORMPGTO CDFORMPGT, FP.IDTPPERIODO IDTPPERIODO,
                M.NRPARCPGTOMOD NRPARCPGTO, M.IDVALORPARCELAS, FP.IDCONSDIAUTIL
           FROM FORMPGTO FP, MODPGTO M
          WHERE M.CDMODPGTO = P_CDMODPGTO
            AND FP.CDFORMPGTO(+) = M.CDFORMPGTO;
            
      CURSOR C_QTPARCELAS (P_CDMODPGTO IN MODPGTO.CDMODPGTO%TYPE, P_NRORG IN NUMBER) IS
         SELECT COUNT(*) AS QTPARCELAS
           FROM PRAZOMODPGTO
          WHERE CDMODPGTO = P_CDMODPGTO;
          
      CURSOR C_PARAMETRIZACAO_PGTO_CLI (P_CDCLIENTE IN VARCHAR2, P_NRORG IN NUMBER) IS
         SELECT C.IDTPFORPGTCL IDTPFORPGT, C.IDUTDIAPGTCL IDUTDIAPGT , C.NRDIAPGFIXCL NRDIAPGFIX ,
                FP.IDTIPODTPGTO IDTIPODTPGT, C.CDFORMPGTO CDFORMPGT, FP.IDTPPERIODO IDTPPERIODO,
                C.NRPARCPGTOCL NRPARCPGTO, FP.IDREFECALCPARC, FP.IDCONSDIAUTIL
           FROM CLIENTE C, FORMPGTO FP
          WHERE (C.CDCLIENTE = P_CDCLIENTE)
            AND (C.CDFORMPGTO = FP.CDFORMPGTO(+));
    
       CURSOR C_QTPARCELAS_CLI (P_CDCLIENTE IN VARCHAR2, P_NRORG IN NUMBER) IS
         SELECT COUNT(*) AS QTPARCELAS
           FROM PRAZOPGTCLIE
          WHERE CDCLIENTE = P_CDCLIENTE;

      PROCEDURE FORMAPGTO
         (P_INDIAS OUT NUMBER,
          P_DTBASE OUT DATE,
          P_DTREFERNCIA IN DATE,
          P_IDTPFORPGT IN VARCHAR2,
          P_IDTIPODTPGT IN VARCHAR2,
          P_IDUTDIAPGT IN VARCHAR2,
          P_NRDIAFIXO IN VARCHAR2)
      IS
       V_DTAUX DATE;
    
        
       FUNCTION FORMAPGTOFIXA
         (P_DTBASE IN DATE,
          P_IDUTDIAPGTO IN VARCHAR2,
          P_NRDIAFIXO IN VARCHAR2)
       RETURN DATE IS
          V_DATEVENCI DATE;
          V_INDIAS NUMBER;
       BEGIN
    
           IF (P_IDUTDIAPGTO = 'I') THEN
              IF (P_NRDIAFIXO >= TO_NUMBER(TO_CHAR(P_DTBASE, 'DD'))) THEN
                 V_INDIAS :=  TO_NUMBER(TO_CHAR(LAST_DAY(P_DTBASE), 'DD'));
                 IF P_NRDIAFIXO > V_INDIAS THEN
                    V_DATEVENCI := LAST_DAY(P_DTBASE);
                 ELSE
                    V_DATEVENCI := TO_DATE(TO_CHAR(P_NRDIAFIXO)||'/'||TO_CHAR(P_DTBASE, 'MM/YYYY'), 'DD/MM/YYYY');
                 END IF;
              ELSE
                 V_DATEVENCI := ADD_MONTHS(P_DTBASE, 1);
                 V_DATEVENCI := TO_DATE(TO_CHAR(P_NRDIAFIXO)||'/'||TO_CHAR(V_DATEVENCI, 'MM/YYYY'), 'DD/MM/YYYY');
              END IF;
           ELSE
              V_DATEVENCI := ADD_MONTHS(P_DTBASE, 1);
              V_DATEVENCI := TO_DATE(TO_CHAR(P_NRDIAFIXO)||'/'||TO_CHAR(V_DATEVENCI, 'MM/YYYY'), 'DD/MM/YYYY');
           END IF;
           RETURN(V_DATEVENCI);
    
       END;
    
       BEGIN
          IF NOT P_IDTPFORPGT IS NULL THEN
             IF P_IDTPFORPGT = 'V' THEN -- DATA VARIÁVEL
                IF P_IDTIPODTPGT = 'EMIN' THEN
                   P_INDIAS := 0;
                   P_DTBASE := P_DTREFERNCIA;
                ELSIF P_IDTIPODTPGT = 'EMEX' THEN
                   P_INDIAS := 1;
                   P_DTBASE := P_DTREFERNCIA;
                ELSIF P_IDTIPODTPGT = 'ENIN' THEN
                   P_INDIAS := 0;
                   P_DTBASE := P_DTREFERNCIA;
                ELSIF P_IDTIPODTPGT = 'ENEX' THEN
                   P_INDIAS := 1;
                   P_DTBASE := P_DTREFERNCIA;
                ELSIF P_IDTIPODTPGT = 'UEMI' THEN
                   P_INDIAS := 0;
                   P_DTBASE := P_DTREFERNCIA;
                ELSIF P_IDTIPODTPGT = 'UEME' THEN
                   P_INDIAS := 1;
                   P_DTBASE := P_DTREFERNCIA;
                END IF;
                V_DTAUX := P_DTBASE + P_INDIAS;
                V_DTAUX := V_DTAUX - 1;
             ELSE -- "F" DATA FIXA
                P_DTBASE := P_DTREFERNCIA;
                V_DTAUX := FORMAPGTOFIXA(P_DTREFERNCIA ,P_IDUTDIAPGT, P_NRDIAFIXO);
             END IF;
             P_INDIAS := TRUNC(V_DTAUX - P_DTBASE);
             IF (P_INDIAS IS NULL) THEN
                P_INDIAS := 0;
             END IF;
          END IF;
       END;
    
  BEGIN
      
      IF P_IDFATURACONTR = 'O' THEN 
         OPEN C_PARAMETRIZACAO_PGTO (P_CDMODPGTO, P_NRORG);
         FETCH C_PARAMETRIZACAO_PGTO INTO
            V_IDTPFORPGT,
            V_IDUTDIAPGT,
            V_NRDIAFIXO,
            P_IDTIPODTPGT,
            P_CDFORMPGTO,
            P_IDTPPERIODO,
            P_NRPARCPGTO,
            P_IDVALORPARCELAS,
            V_IDCONSDIAUTIL;
      ELSE
         OPEN C_PARAMETRIZACAO_PGTO_CLI (P_CDCLIENTE, P_NRORG);
         FETCH C_PARAMETRIZACAO_PGTO_CLI INTO
            V_IDTPFORPGT,
            V_IDUTDIAPGT,
            V_NRDIAFIXO,
            P_IDTIPODTPGT,
            P_CDFORMPGTO,
            P_IDTPPERIODO,
            P_NRPARCPGTO,
            P_IDVALORPARCELAS,
            V_IDCONSDIAUTIL;
        
      END IF;
      P_IDFORMAPGTO := V_IDTPFORPGT;
      
      IF P_IDFATURACONTR = 'O' THEN
         IF NOT (V_IDTPFORPGT IS NULL) AND C_PARAMETRIZACAO_PGTO%FOUND THEN
            IF V_IDTPFORPGT = 'V' THEN
               OPEN C_QTPARCELAS(P_CDMODPGTO, P_NRORG);
               FETCH C_QTPARCELAS INTO P_NRPARCELAS;
            ELSE
               P_NRPARCELAS := P_NRPARCPGTO;
            END IF;
            FORMAPGTO(P_INDIAS,
                      P_DTBASE,
                      P_DTREFERENCIA,                   
                      V_IDTPFORPGT,
                      P_IDTIPODTPGT,
                      V_IDUTDIAPGT,
                      V_NRDIAFIXO);
         ELSE
            P_INDIAS := 0;
            P_DTBASE := P_DTREFERENCIA;
            P_IDFORMAPGTO := NULL;
            P_IDTIPODTPGT := NULL;
            P_IDTPPERIODO := NULL;
            P_NRPARCELAS := 1;
            P_NRPARCPGTO := 1;
         END IF;
      ELSE 
         IF NOT (V_IDTPFORPGT IS NULL) AND C_PARAMETRIZACAO_PGTO_CLI%FOUND THEN
            IF V_IDTPFORPGT = 'V' THEN
               OPEN C_QTPARCELAS_CLI(P_CDMODPGTO, P_NRORG);
               FETCH C_QTPARCELAS_CLI INTO P_NRPARCELAS;
            ELSE
               P_NRPARCELAS := P_NRPARCPGTO;
            END IF;
            FORMAPGTO(P_INDIAS,
                      P_DTBASE,
                      P_DTREFERENCIA,                   
                      V_IDTPFORPGT,
                      P_IDTIPODTPGT,
                      V_IDUTDIAPGT,
                      V_NRDIAFIXO);
         ELSE
            P_INDIAS := 0;
            P_DTBASE := P_DTREFERENCIA;
            P_IDFORMAPGTO := NULL;
            P_IDTIPODTPGT := NULL;
            P_IDTPPERIODO := NULL;
            P_NRPARCELAS := 1;
            P_NRPARCPGTO := 1;
         END IF;
      END IF;
      
      IF(V_IDCONSDIAUTIL = 'S')THEN
         P_BOCONSFERIBANC := 'TRUE';
      ELSE
         P_BOCONSFERIBANC := 'FALSE';
      END IF;
      
      IF P_IDFATURACONTR = 'O' THEN 
         CLOSE C_PARAMETRIZACAO_PGTO;
         IF C_QTPARCELAS%ISOPEN THEN
            CLOSE C_QTPARCELAS;
      END IF;
      ELSE
         CLOSE C_PARAMETRIZACAO_PGTO_CLI;
         IF C_QTPARCELAS_CLI%ISOPEN THEN
            CLOSE C_QTPARCELAS_CLI;
      END IF;
      END IF;
   END;
/

-- VERSAO --
BEGIN ATUVEROBJ('FCT_CALC_MODPAG','6.10.xxx'); END;
/
