CREATE OR REPLACE PROCEDURE CALCULA_DESCONTOSM
   (P_CDFILIAL    IN  NOTAFISCAL.CDFILIAL%TYPE,
    P_CDFILIPROC  IN  FILIAL.CDFILIAL%TYPE,
    P_NRLANCTONF  IN  NOTAFISCAL.NRLANCTONF%TYPE,
    P_CDCIA       IN  COMPANHIA.CDCIA%TYPE,
    P_NRAPURACFATM IN  NOTAFISCAL.NRAPURACFATM%TYPE,
    P_GRUPPROD    IN GRUPPROD.CDGRUPPROD%TYPE DEFAULT NULL)
AS
   V_VRINDICE         MOVINDICE.VRINDICE%TYPE;
   V_IDTIPMANUALAP    TRAB_APURACM.IDTIPMANUALAP%TYPE;
   V_VRDESCONTO       ITEMENCARGO.VRITEMENCA%TYPE;
   V_VRDESCSERV       ITEMENCARGO.VRITEMENCA%TYPE;
   V_VRDESCONTOME     ITEMENCARGO.VRITEMENCA%TYPE;
   V_VRDESCSERVME     ITEMENCARGO.VRITEMENCA%TYPE;
   V_VRDESCDELAP      ITEMENCARGO.VRITEMENCA%TYPE;
   V_VRDESCSRVAP      ITEMENCARGO.VRITEMENCA%TYPE;
   V_VRLIQPROD        ITEMPRODUTO.VRTOTAITEM%TYPE;
   V_VRLIQSERV        ITEMPRODUTO.VRTOTAITEM%TYPE;
   V_VRENCARGO        ITEMENCARGO.VRITEMENCA%TYPE;
   V_VRENCASRV        ITEMENCARGO.VRITEMENCA%TYPE;
   V_VRLIQPRODME      ITEMPRODUTO.VRTOTAITEM%TYPE;
   V_VRLIQSERVME      ITEMPRODUTO.VRTOTAITEM%TYPE;
   V_VRENCARGOME      ITEMENCARGO.VRITEMENCA%TYPE;
   V_VRENCASRVME      ITEMENCARGO.VRITEMENCA%TYPE;
   V_CDCIA            COMPANHIA.CDCIA%TYPE;
   V_VRTAXAAERO       CIAFILI.VRALTXPORT%TYPE;
   V_VRTAXAAEROME     CIAFILI.VRALTXPORT%TYPE;
   V_IDNACCIA         COMPANHIA.IDNACCIA%TYPE;
   V_NRSEQITPROD      ITEMPRODUTO.NRSEQITPROD%TYPE;
   V_DTEMISDELAP      DELIVERYM.DTEMISDEL%TYPE;
   V_BCDESCSERV       DELIVERYM.VRDESCCOND%TYPE;
   V_BCDESCPROD       DELIVERYM.VRDESCCOND%TYPE;
   V_BCDESCSERVME     DELIVERYM.VRDESCCOND%TYPE;
   V_BCDESCPRODME     DELIVERYM.VRDESCCOND%TYPE;
   V_CDARVPROD        PRODUTO.CDARVPROD%TYPE;
   V_CDARVPRODINT     PRODUTO.CDARVPROD%TYPE;
   V_CDPRODUTO        PARAMCOMIS.CDPRODUTO%TYPE;
   V_NMPRODUTO        PRODUTO.NMPRODUTO%TYPE;
   V_IMPOISS          NUMBER;
   V_IDNACSERV        VARCHAR2(01);
   V_CDPRAEROIN       PARAMCOMIS.CDPRAEROIN%TYPE;
   V_NMPRAEROIN       PRODUTO.NMPRODUTO%TYPE;
   V_SEPPRDSERV       PARFATCOMIS.IDSEPPRDSERV%TYPE;
   V_COUNT            NUMBER;
   V_CDGRUPOFISC      VARCHAR2(03);
   V_CDCLIENTE        CLIENTE.CDCLIENTE%TYPE;
   V_VRDESCDESVAP     TRAB_APURACM.VRDESCDESVAP%TYPE;
   V_VRDESCDESVAPME   TRAB_APURACM.VRDESCDESVAP%TYPE;
   V_VRDESCDESVSEAP   TRAB_APURACM.VRDESCDESVSEAP%TYPE;
   V_VRDESCDESVSEAPME TRAB_APURACM.VRDESCDESVSEAP%TYPE;
   V_VRDESCFIXOAP     TRAB_APURACM.VRDESCFIXOAP%TYPE;
   V_VRDESCFIXOAPME   TRAB_APURACM.VRDESCFIXOAP%TYPE;
   V_VRDESCFIXOSEAP   TRAB_APURACM.VRDESCFIXOSEAP%TYPE;
   V_VRDESCFIXOSEAPME TRAB_APURACM.VRDESCFIXOSEAP%TYPE;
   V_VRDESCCONDAP     TRAB_APURACM.VRDESCCONDAP%TYPE;
   V_VRDESCCONDSEAP   TRAB_APURACM.VRDESCCONDSEAP%TYPE;
   V_VRDESCCONDAPME   TRAB_APURACM.VRDESCCONDAP%TYPE;
   V_VRDESCCONDSEAPME TRAB_APURACM.VRDESCCONDSEAP%TYPE;
   V_VRTXAERORAP      TRAB_APURACM.VRTXAERORAP%TYPE;
   V_VRTXAERODAP      TRAB_APURACM.VRTXAERODAP%TYPE;
   V_NRDECITOTANF     PARAMGERAL.NRDECITOTANF%TYPE;

BEGIN

   SELECT NVL(NRDECITOTANF, 2)
   INTO V_NRDECITOTANF
   FROM PARAMGERAL;
   SELECT MAX(DTEMISDELAP), MAX(IDTIPMANUALAP)
   INTO V_DTEMISDELAP, V_IDTIPMANUALAP
   FROM TRAB_APURACM
   WHERE CDCIAAP       = P_CDCIA        AND
         NRAPURACFATAP = P_NRAPURACFATM  AND
         CDFILIALAP    = P_CDFILIPROC   AND
         NRLANCTONFAP  = P_NRLANCTONF;

   SELECT NVL(MAX(IDSEPPRDSERV),'N')
   INTO V_SEPPRDSERV
   FROM PARFATCOMIS;

   SELECT MAX(C.CDCIA), NVL(MAX(CO.IDNACCIA),'1'),
          MAX(N.CDCLIENTE)
   INTO V_CDCIA, V_IDNACCIA, V_CDCLIENTE
   FROM CIAFILI C, COMPANHIA CO, NOTAFISCAL N
   WHERE N.CDFILIAL   = P_CDFILIPROC AND
         N.NRLANCTONF = P_NRLANCTONF AND
         N.CDCLIENTE  = CO.CDCLIENTE AND
         C.CDCIA      = CO.CDCIA     AND
         C.CDFILIAL   = P_CDFILIAL;

   SELECT NVL(MAX(VRINDICE),1)
   INTO V_VRINDICE
   FROM MOVINDICE M, CIAFILI C
   WHERE C.CDFILIAL   = P_CDFILIAL   AND
         C.CDCIA      = V_CDCIA      AND
         M.CDTPINDICE = C.CDTPINDICE AND
         M.CDINDICE   = C.CDINDICE   AND
         M.IDPERVALOR = '2'          AND
         DTREFIND     = V_DTEMISDELAP;

   SELECT NVL(SUM(VRDESCDELAP),0) VRDESCDELAP,
          NVL(SUM(VRDESCSRVAP),0) VRDESCSRVAP,
          NVL(SUM(VRDESCDESVAP),0), NVL(SUM(VRDESCFIXOAP),0), NVL(SUM(VRDESCCONDAP),0),
          NVL(SUM(VRDESCDESVSEAP),0), NVL(SUM(VRDESCFIXOSEAP),0), NVL(SUM(VRDESCCONDSEAP),0),
          NVL(SUM(VRTXAERORAP),0), NVL(SUM(VRTXAERODAP),0)
   INTO V_VRDESCDELAP, V_VRDESCSRVAP,
        V_VRDESCDESVAP, V_VRDESCFIXOAP, V_VRDESCCONDAP,
        V_VRDESCDESVSEAP, V_VRDESCFIXOSEAP, V_VRDESCCONDSEAP,
        V_VRTXAERORAP, V_VRTXAERODAP
   FROM TRAB_APURACM
   WHERE CDCIAAP       = P_CDCIA        AND
         NRAPURACFATAP = P_NRAPURACFATM  AND
         CDFILIALAP    = P_CDFILIPROC   AND
         NRLANCTONFAP  = P_NRLANCTONF;

   V_VRDESCONTO     := V_VRDESCDELAP;
   V_VRDESCSERV     := V_VRDESCSRVAP;
   V_VRDESCONTOME   := V_VRDESCDELAP/V_VRINDICE;
   V_VRDESCSERVME   := V_VRDESCSRVAP/V_VRINDICE;
   V_VRDESCDESVAP   := V_VRDESCDESVAP;
   V_VRDESCFIXOAP   := V_VRDESCFIXOAP;
   V_VRDESCCONDAP   := V_VRDESCCONDAP;
   V_VRDESCDESVSEAP := V_VRDESCDESVSEAP;
   V_VRDESCFIXOSEAP := V_VRDESCFIXOSEAP;
   V_VRDESCCONDSEAP := V_VRDESCCONDSEAP;
   V_VRDESCDESVAPME   := V_VRDESCDESVAP/V_VRINDICE;
   V_VRDESCFIXOAPME   := V_VRDESCFIXOAP/V_VRINDICE;
   V_VRDESCCONDAPME   := V_VRDESCCONDAP/V_VRINDICE;
   V_VRDESCDESVSEAPME := V_VRDESCDESVSEAP/V_VRINDICE;
   V_VRDESCFIXOSEAPME := V_VRDESCFIXOSEAP/V_VRINDICE;
   V_VRDESCCONDSEAPME := V_VRDESCCONDSEAP/V_VRINDICE;
   V_VRLIQSERV   := 0;
   V_VRLIQPROD   := 0;
   V_VRLIQSERVME := 0;
   V_VRLIQPRODME := 0;


   IF (V_NRDECITOTANF = 0) THEN
      V_VRDESCONTO     := ROUND(V_VRDESCONTO    , 0);
      V_VRDESCSERV     := ROUND(V_VRDESCSERV    , 0);
      V_VRDESCDESVAP   := ROUND(V_VRDESCDESVAP  , 0);
      V_VRDESCFIXOAP   := ROUND(V_VRDESCFIXOAP  , 0);
      V_VRDESCCONDAP   := ROUND(V_VRDESCCONDAP  , 0);
      V_VRDESCDESVSEAP := ROUND(V_VRDESCDESVSEAP, 0);
      V_VRDESCFIXOSEAP := ROUND(V_VRDESCFIXOSEAP, 0);
      V_VRDESCCONDSEAP := ROUND(V_VRDESCCONDSEAP, 0);
   END IF;

   CONSULTA_GRUPOFISCAL('C', V_CDCLIENTE, '', 'S', '01', V_CDGRUPOFISC);

   FOR CURS_BCDESCONTO IN
      (SELECT N.CDEMPRESA,  N.CDINSCESTA, N.CDTIPOOPER, P.CDPRODUTO,
              P.VRTOTAITEM, P.VRDESCITEM, SUBSTR(P.CDPRODEMIS,1,1) AS IDTIPMANUAL, P.VRITMOEDESTR,
              PR.CDARVPROD
       FROM PARAMCOMIS G, NOTAFISCAL N, ITEMPRODUTO P, PRODUTO PR
       WHERE N.CDFILIAL     = P_CDFILIPROC       AND
             N.NRLANCTONF   = P_NRLANCTONF       AND
             P.CDFILIAL     = N.CDFILIAL         AND
             P.NRLANCTONF   = N.NRLANCTONF       AND
             PR.CDPRODUTO   = P.CDPRODUTO        AND
             G.CDFILIAL     = P_CDFILIAL         AND
             (((G.CDPRODUTO  <> P.CDPRODUTO) OR (G.CDPRODUTO  IS NULL)) AND
              ((G.CDPRAEROIN <> P.CDPRODUTO) OR (G.CDPRAEROIN IS NULL)))) LOOP

       CONSULTA_INCIDENCIA('S', CURS_BCDESCONTO.CDEMPRESA, CURS_BCDESCONTO.CDARVPROD,
                           CURS_BCDESCONTO.CDINSCESTA, CURS_BCDESCONTO.CDTIPOOPER,
                           P_CDFILIPROC, P_NRLANCTONF, V_CDGRUPOFISC);

       SELECT COUNT(*) INTO V_IMPOISS
       FROM INCITEMP
       WHERE NRINCITEMP = P_CDFILIPROC||P_NRLANCTONF AND
             CDIMPOTEMP = '31';

       IF V_IMPOISS > 0 THEN
          V_VRLIQSERV   := V_VRLIQSERV   + NVL(CURS_BCDESCONTO.VRTOTAITEM - CURS_BCDESCONTO.VRDESCITEM,0);
          V_VRLIQSERVME := V_VRLIQSERVME + NVL(CURS_BCDESCONTO.VRITMOEDESTR,0);
       ELSE
          V_VRLIQPROD   := V_VRLIQPROD   + NVL(CURS_BCDESCONTO.VRTOTAITEM - CURS_BCDESCONTO.VRDESCITEM,0);
          V_VRLIQPRODME := V_VRLIQPRODME + NVL(CURS_BCDESCONTO.VRITMOEDESTR,0);
       END IF;

       DELETE INCITEMP
       WHERE NRINCITEMP = P_CDFILIPROC||P_NRLANCTONF;
   END LOOP;

   V_BCDESCSERV   := V_VRLIQSERV;
   V_BCDESCPROD   := V_VRLIQPROD;
   V_BCDESCSERVME := V_VRLIQSERVME;
   V_BCDESCPRODME := V_VRLIQPRODME;

   IF V_VRDESCONTO > 0 THEN
      V_VRENCARGO   := V_VRDESCONTO;
      V_VRENCARGOME := V_VRDESCONTOME;

      SELECT COUNT(*)
      INTO V_COUNT
      FROM ENCARGO
      WHERE CDENCARGO = '06';

      IF V_COUNT > 0 THEN
         INSERT INTO ITEMENCARGO
            (CDFILIAL, NRLANCTONF, CDENCARGO, VRITEMENCA, VRENMOEDESTR)
         VALUES
            (P_CDFILIPROC, P_NRLANCTONF, '06', NVL(V_VRENCARGO,0), NVL(V_VRENCARGOME,0));
      END IF;

      V_BCDESCPROD   := V_BCDESCPROD   - V_VRENCARGO;
      V_BCDESCPRODME := V_BCDESCPRODME - V_VRENCARGOME;
   END IF;

   IF V_VRDESCSERV > 0 THEN
      V_VRENCASRV   := V_VRDESCSERV;
      V_VRENCASRVME := V_VRDESCSERVME;

      SELECT COUNT(*)
      INTO V_COUNT
      FROM ENCARGO
      WHERE CDENCARGO = '07';

      IF V_COUNT > 0 THEN
         INSERT INTO ITEMENCARGO
            (CDFILIAL, NRLANCTONF, CDENCARGO, VRITEMENCA, VRENMOEDESTR)
         VALUES
            (P_CDFILIPROC, P_NRLANCTONF, '07', NVL(V_VRENCASRV,0), NVL(V_VRENCASRVME,0));
      END IF;

      V_BCDESCSERV   := V_BCDESCSERV   - V_VRENCASRV;
      V_BCDESCSERVME := V_BCDESCSERVME - V_VRENCASRVME;
   END IF;

   IF V_VRDESCDESVAP >= 0 THEN
      SELECT COUNT(*)
      INTO V_COUNT
      FROM ENCARGO
      WHERE CDENCARGO = '08';

      IF V_COUNT > 0 THEN
         INSERT INTO ITEMENCARGO
            (CDFILIAL, NRLANCTONF, CDENCARGO, VRITEMENCA, VRENMOEDESTR)
         VALUES
            (P_CDFILIPROC, P_NRLANCTONF, '08', NVL(V_VRDESCDESVAP,0), NVL(V_VRDESCDESVAPME,0));
      END IF;
   ELSE
      SELECT COUNT(*)
      INTO V_COUNT
      FROM ENCARGO
      WHERE CDENCARGO = '14';

      IF V_COUNT > 0 THEN
         INSERT INTO ITEMENCARGO
            (CDFILIAL, NRLANCTONF, CDENCARGO, VRITEMENCA, VRENMOEDESTR)
         VALUES
            (P_CDFILIPROC, P_NRLANCTONF, '14', NVL(V_VRDESCDESVAP,0) * (-1), NVL(V_VRDESCDESVAPME,0));
      END IF;
   END IF;

   IF V_VRDESCDESVSEAP >= 0 THEN
      SELECT COUNT(*)
      INTO V_COUNT
      FROM ENCARGO
      WHERE CDENCARGO = '09';

      IF V_COUNT > 0 THEN
         INSERT INTO ITEMENCARGO
            (CDFILIAL, NRLANCTONF, CDENCARGO, VRITEMENCA, VRENMOEDESTR)
         VALUES
            (P_CDFILIPROC, P_NRLANCTONF, '09', NVL(V_VRDESCDESVSEAP,0), NVL(V_VRDESCDESVSEAPME,0));
      END IF;
   ELSE
      SELECT COUNT(*)
      INTO V_COUNT
      FROM ENCARGO
      WHERE CDENCARGO = '15';

      IF V_COUNT > 0 THEN
         INSERT INTO ITEMENCARGO
            (CDFILIAL, NRLANCTONF, CDENCARGO, VRITEMENCA, VRENMOEDESTR)
         VALUES
            (P_CDFILIPROC, P_NRLANCTONF, '15', NVL(V_VRDESCDESVSEAP,0)  * (-1), NVL(V_VRDESCDESVSEAPME,0));
      END IF;
   END IF;

   V_BCDESCPROD   := V_BCDESCPROD   - V_VRDESCDESVAP;
   V_BCDESCPRODME := V_BCDESCPRODME - V_VRDESCDESVAPME;
   V_BCDESCSERV   := V_BCDESCSERV   - V_VRDESCDESVSEAP;
   V_BCDESCSERVME := V_BCDESCSERVME - V_VRDESCDESVSEAPME;

   IF V_VRDESCFIXOAP > 0 THEN
      SELECT COUNT(*)
      INTO V_COUNT
      FROM ENCARGO
      WHERE CDENCARGO = '10';

      IF V_COUNT > 0 THEN
         INSERT INTO ITEMENCARGO
            (CDFILIAL, NRLANCTONF, CDENCARGO, VRITEMENCA, VRENMOEDESTR)
         VALUES
            (P_CDFILIPROC, P_NRLANCTONF, '10', NVL(V_VRDESCFIXOAP,0), NVL(V_VRDESCFIXOAPME,0));
      END IF;
   END IF;

   IF V_VRDESCFIXOSEAP > 0 THEN
      SELECT COUNT(*)
      INTO V_COUNT
      FROM ENCARGO
      WHERE CDENCARGO = '11';

      IF V_COUNT > 0 THEN
         INSERT INTO ITEMENCARGO
            (CDFILIAL, NRLANCTONF, CDENCARGO, VRITEMENCA, VRENMOEDESTR)
         VALUES
            (P_CDFILIPROC, P_NRLANCTONF, '11', NVL(V_VRDESCFIXOSEAP,0), NVL(V_VRDESCFIXOSEAPME,0));
      END IF;
   END IF;

   V_BCDESCPROD   := V_BCDESCPROD   - V_VRDESCFIXOAP;
   V_BCDESCPRODME := V_BCDESCPRODME - V_VRDESCFIXOAPME;
   V_BCDESCSERV   := V_BCDESCSERV   - V_VRDESCFIXOSEAP;
   V_BCDESCSERVME := V_BCDESCSERVME - V_VRDESCFIXOSEAPME;

   IF V_VRDESCCONDAP > 0 THEN
      SELECT COUNT(*)
      INTO V_COUNT
      FROM ENCARGO
      WHERE CDENCARGO = '12';

      IF V_COUNT > 0 THEN
         INSERT INTO ITEMENCARGO
            (CDFILIAL, NRLANCTONF, CDENCARGO, VRITEMENCA, VRENMOEDESTR)
         VALUES
            (P_CDFILIPROC, P_NRLANCTONF, '12', NVL(V_VRDESCCONDAP,0), NVL(V_VRDESCCONDAPME,0));
      END IF;
   END IF;

   IF V_VRDESCCONDSEAP > 0 THEN
      SELECT COUNT(*)
      INTO V_COUNT
      FROM ENCARGO
      WHERE CDENCARGO = '13';

      IF V_COUNT > 0 THEN
         INSERT INTO ITEMENCARGO
            (CDFILIAL, NRLANCTONF, CDENCARGO, VRITEMENCA, VRENMOEDESTR)
         VALUES
            (P_CDFILIPROC, P_NRLANCTONF, '13', NVL(V_VRDESCCONDSEAP,0), NVL(V_VRDESCCONDSEAPME,0));
      END IF;
   END IF;

   V_BCDESCPROD   := V_BCDESCPROD   - V_VRDESCCONDAP;
   V_BCDESCPRODME := V_BCDESCPRODME - V_VRDESCCONDAPME;
   V_BCDESCSERV   := V_BCDESCSERV   - V_VRDESCCONDSEAP;
   V_BCDESCSERVME := V_BCDESCSERVME - V_VRDESCCONDSEAPME;
END;
/