CREATE OR REPLACE PROCEDURE GERA_AJUSTE_FECP(
   P_NRSEQUMAPAPU    IN MAPAAPURAC.NRSEQUMAPAPU%TYPE,
   P_CDEMPRESA       IN MAPAAPURAC.CDEMPRESA%TYPE,
   P_CDINSCESTA      IN MAPAAPURAC.CDINSCESTA%TYPE,
   P_CDIMPOSTO       IN MAPAAPURAC.CDIMPOSTO%TYPE
)
AS
   V_ORIGEM           VARCHAR2(01);
   V_IDGERAFECPDEBESP VARCHAR2(01);
   V_VRADICFECP       FLOAT;
   V_NRSEQITMAPR      NUMBER;
   
   /* CAMPOS DA NOVA TABELA */
   V_TPAJUSTEFECP            VARCHAR2(3);  
   V_CDAJUSTEDDUE            VARCHAR2(10); 
   V_DSAJUSTEDDUE            VARCHAR2(400);
   V_CDAJUSTDEBESP           VARCHAR2(10); 
   V_DSAJUSTDEBESP           VARCHAR2(400);
   V_CDAJUSTOUTDEB           VARCHAR2(10); 
   V_DSAJUSTOUTDEB           VARCHAR2(400);
   V_IDGERAREGC191           VARCHAR2(1);
    
BEGIN
   V_VRADICFECP := 0;
   
   /******** Verifica a parametrizacao do tratamento do FECP no mapa de apuracao ********/
   BEGIN
      SELECT 'M', TPAJUSTEFECP, CDAJUSTEDDUE, DSAJUSTEDDUE, CDAJUSTDEBESP, 
             DSAJUSTDEBESP, CDAJUSTOUTDEB, DSAJUSTOUTDEB, IDGERAREGC191
        INTO V_ORIGEM, V_TPAJUSTEFECP, V_CDAJUSTEDDUE, V_DSAJUSTEDDUE, V_CDAJUSTDEBESP,
             V_DSAJUSTDEBESP, V_CDAJUSTOUTDEB, V_DSAJUSTOUTDEB, V_IDGERAREGC191
        FROM AJUSTEFECP
       WHERE CDEMPRESA    = P_CDEMPRESA
         AND CDINSCESTA   = P_CDINSCESTA
         AND CDIMPOSTO    = P_CDIMPOSTO;
    EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_ORIGEM := 'D';
		 V_TPAJUSTEFECP  := 'DDE';
		 V_CDAJUSTEDDUE  := '';
		 V_DSAJUSTEDDUE  := '';
		 V_CDAJUSTDEBESP := '';
         V_DSAJUSTDEBESP := '';
		 V_CDAJUSTOUTDEB := '';
		 V_DSAJUSTOUTDEB := '';
		 V_IDGERAREGC191 := 'S';
	END;
   
   /******** Se nao encontrar o cadastro no Mobile procura a parametrizacao do Delphi ********/
   IF NVL(V_ORIGEM,'D') = 'D' THEN 
      BEGIN
         SELECT 'D', IDGERAFECPDEBESP, CDAJUSTEFECPDD, DSAJUSTEFECPDD, CDAJUSTEFECPDE, DSAJUSTEFECPDE
           INTO V_ORIGEM, V_IDGERAFECPDEBESP, V_CDAJUSTEDDUE, V_DSAJUSTEDDUE, V_CDAJUSTDEBESP, V_DSAJUSTDEBESP
           FROM PARAMESTSPED
          WHERE CDEMPRESA    = P_CDEMPRESA
            AND CDINSCESTA   = P_CDINSCESTA;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_ORIGEM := 'D';
			V_IDGERAFECPDEBESP := 'N';
            V_CDAJUSTEDDUE     := '';
			V_DSAJUSTEDDUE     := '';
			V_CDAJUSTDEBESP    := '';
			V_DSAJUSTDEBESP    := '';
      END;
      IF V_TPAJUSTEFECP IS NULL THEN
         IF V_IDGERAFECPDEBESP = 'S' THEN
            V_TPAJUSTEFECP := 'DDE' ;
         ELSE
            V_TPAJUSTEFECP := 'N';
         END IF;
      END IF;  
   END IF;	

   /******** Calcula o valor do FECP presente na apuracao ********/
   BEGIN
      SELECT NVL(SUM(ROUND(I.VRADICFECP,2)),0.0) AS VRADICFECP
        INTO V_VRADICFECP
        FROM IMPOSTONF I, NOTAFISCAL N
       WHERE I.NRSEQUMAPAPU  = P_NRSEQUMAPAPU
         AND I.CDFILIAL      = N.CDFILIAL
         AND I.NRLANCTONF    = N.NRLANCTONF
         AND N.IDENTRSAIDOP  = 'S'
         AND I.CDFISCALNF    = '1';
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_VRADICFECP := 0;
   END;

   /******** Atualiza o valor do campo no Mapa de Apuracao ********/
   UPDATE MAPARECUP
      SET VRFECP = NVL(V_VRADICFECP,0)
    WHERE NRSEQUMAPAPU = P_NRSEQUMAPAPU;

   /******** Deleta os itens de ajuste referente ao valor do FECP gerado pela rotina, evitando erro na reapuracao de mapas ********/
    DELETE FROM ITMAPARECUP
    WHERE NRSEQUMAPAPU  = P_NRSEQUMAPAPU
      AND IDTIPOITMAPR  IN ('DD','DE')
      AND IDAJFECP      = 'S';

   /******** Verifica se o FECP deve ser gerado como Deducao e Debito Especial ********/
   IF (V_TPAJUSTEFECP = 'DDE') AND (V_VRADICFECP > 0) THEN
      /******** Busca o maior sequencial de Deducao ********/
      BEGIN
         SELECT NVL(MAX(NRSEQITMAPR),0) AS NRSEQITMAPR
           INTO V_NRSEQITMAPR
           FROM ITMAPARECUP
          WHERE NRSEQUMAPAPU = P_NRSEQUMAPAPU
            AND IDTIPOITMAPR = 'DD';
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_NRSEQITMAPR := NULL;
      END;

      V_NRSEQITMAPR := NVL(V_NRSEQITMAPR,0)+1;

      /******** Insere o item de deducao com o valor calculado ********/
      INSERT INTO ITMAPARECUP
         (NRSEQUMAPAPU, IDTIPOITMAPR, NRSEQITMAPR, DSITEMMAPR, VRITEMMAPR, CDAJUSTEITMAPA, IDAJFECP)
      VALUES
         (P_NRSEQUMAPAPU, 'DD', LPAD(V_NRSEQITMAPR,10,'0'), V_DSAJUSTEDDUE, V_VRADICFECP, V_CDAJUSTEDDUE, 'S');

      /******** Busca o maior sequencial de Debito Especial ********/
      BEGIN
         SELECT NVL(MAX(NRSEQITMAPR),0) AS NRSEQITMAPR
           INTO V_NRSEQITMAPR
           FROM ITMAPARECUP
          WHERE NRSEQUMAPAPU = P_NRSEQUMAPAPU
            AND IDTIPOITMAPR = 'DE';
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_NRSEQITMAPR := NULL;
      END;

      V_NRSEQITMAPR := NVL(V_NRSEQITMAPR,0)+1;

      /******** Insere o item de debito especial com o valor calculado ********/
      INSERT INTO ITMAPARECUP
         (NRSEQUMAPAPU, IDTIPOITMAPR, NRSEQITMAPR, DSITEMMAPR, VRITEMMAPR, CDAJUSTEITMAPA, IDAJFECP)
      VALUES
         (P_NRSEQUMAPAPU, 'DE', LPAD(V_NRSEQITMAPR,10,'0'), V_DSAJUSTDEBESP, V_VRADICFECP, V_CDAJUSTDEBESP, 'S');
   
   
   /******** Verifica se o FECP deve ser gerado como Debito Especial ********/
   ELSIF (V_TPAJUSTEFECP = 'DE') AND (V_VRADICFECP > 0) THEN 
   
   /******** Busca o maior sequencial de Debito Especial ********/
      BEGIN
         SELECT NVL(MAX(NRSEQITMAPR),0) AS NRSEQITMAPR
           INTO V_NRSEQITMAPR
           FROM ITMAPARECUP
          WHERE NRSEQUMAPAPU = P_NRSEQUMAPAPU
            AND IDTIPOITMAPR = 'DE';
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_NRSEQITMAPR := NULL;
      END;

      V_NRSEQITMAPR := NVL(V_NRSEQITMAPR,0)+1;

      /******** Insere o item de debito especial com o valor calculado ********/
      INSERT INTO ITMAPARECUP
         (NRSEQUMAPAPU, IDTIPOITMAPR, NRSEQITMAPR, DSITEMMAPR, VRITEMMAPR, CDAJUSTEITMAPA, IDAJFECP)
      VALUES
         (P_NRSEQUMAPAPU, 'DE', LPAD(V_NRSEQITMAPR,10,'0'), V_DSAJUSTDEBESP, V_VRADICFECP, V_CDAJUSTDEBESP, 'S');
   
   /******** Verifica se o FECP deve ser gerado como Outros Debitos ********/      
   ELSIF (V_TPAJUSTEFECP = 'OD') AND (V_VRADICFECP > 0) THEN 
    
    /******** Busca o maior sequencial de Outros Debitos ********/
      BEGIN
         SELECT NVL(MAX(NRSEQITMAPR),0) AS NRSEQITMAPR
           INTO V_NRSEQITMAPR
           FROM ITMAPARECUP
          WHERE NRSEQUMAPAPU = P_NRSEQUMAPAPU
            AND IDTIPOITMAPR = 'OD';
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_NRSEQITMAPR := NULL;
      END;

      V_NRSEQITMAPR := NVL(V_NRSEQITMAPR,0)+1;

      /******** Insere o item de debito especial com o valor calculado ********/
      INSERT INTO ITMAPARECUP
         (NRSEQUMAPAPU, IDTIPOITMAPR, NRSEQITMAPR, DSITEMMAPR, VRITEMMAPR, CDAJUSTEITMAPA, IDAJFECP)
      VALUES
         (P_NRSEQUMAPAPU, 'OD', LPAD(V_NRSEQITMAPR,10,'0'), V_DSAJUSTDEBESP, V_VRADICFECP, V_CDAJUSTDEBESP, 'S');
   
   END IF;
      
END;
/