CREATE OR REPLACE FUNCTION REGISTRO88_SINTEGRA_MS(
   P_CDEMPRESA    IN EMPRESA.CDEMPRESA%TYPE,
   P_CDINSCESTA   IN EMPRESA.CDINSCESTA%TYPE,
   P_DTINICIAL    IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_DTFINAL      IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_CDIMPOSTO   IN IMPOSTO.CDIMPOSTO%TYPE,
   P_NRSEQUENCIAL IN NOTAFISCAL.NRLANCTONF%TYPE) RETURN VARCHAR2
AS
   V_CNPJ           VARCHAR2(14);
   V_ENTRADA        VARCHAR2(10);
   V_SAIDA          VARCHAR2(10);
   V_LINHA_88SMS    VARCHAR2(126);
   V_LINHA_88SME    VARCHAR2(126);
   V_LINHA_88SEC    VARCHAR2(126);
   V_CONTADOR       NUMBER;
   V_LINHA_88SF     VARCHAR2(126);
BEGIN

   V_CONTADOR := 0;
   SELECT MAX(I.NRSEQUMAPAPU)
     INTO V_ENTRADA
     FROM IMPOSTONF I, NOTAFISCAL N,
          MAPAAPURAC M, MODDOCFISC MD
    WHERE M.CDEMPRESA  = P_CDEMPRESA
      AND M.CDINSCESTA = P_CDINSCESTA
      AND M.DTINIPERIODO = P_DTINICIAL
      AND M.DTFIMPERIODO = P_DTFINAL
      AND M.CDIMPOSTO = P_CDIMPOSTO
      AND I.CDIMPOSTO = P_CDIMPOSTO
      AND I.CDFILIAL = N.CDFILIAL
      AND I.NRLANCTONF = N.NRLANCTONF
      AND I.NRSEQUMAPAPU = M.NRSEQUMAPAPU
      AND N.IDENTRSAIDOP = 'E'
      AND N.CDMODDOCFISC = MD.CDMODDOCFISC
      AND N.IDSTATUS IN ('A','E')
      AND MD.CDTPREGFISC  = '50'
      AND NVL(I.IDIMPOSTONF, '1') <> '3';

   SELECT MAX(I.NRSEQUMAPAPU)
     INTO V_SAIDA
     FROM IMPOSTONF I, NOTAFISCAL N,
          MAPAAPURAC M, MODDOCFISC MD
    WHERE M.CDEMPRESA  = P_CDEMPRESA
      AND M.CDINSCESTA = P_CDINSCESTA
      AND M.DTINIPERIODO = P_DTINICIAL
      AND M.DTFIMPERIODO = P_DTFINAL
      AND M.CDIMPOSTO = P_CDIMPOSTO
      AND I.CDIMPOSTO = P_CDIMPOSTO
      AND I.CDFILIAL = N.CDFILIAL
      AND I.NRLANCTONF = N.NRLANCTONF
      AND I.NRSEQUMAPAPU = M.NRSEQUMAPAPU
      AND N.IDENTRSAIDOP = 'S'
      AND N.CDMODDOCFISC = MD.CDMODDOCFISC
      AND N.IDSTATUS IN ('A','E')
      AND MD.CDTPREGFISC  = '50'
      AND NVL(I.IDIMPOSTONF, '1') <> '3';
   
   SELECT MAX(LPAD(REPLACE(REPLACE(REPLACE(NRINSJURIEST,'-',''),'.',''),'/',''), 14, '0'))
     INTO V_CNPJ
     FROM INSCRESTAD
    WHERE CDEMPRESA = P_CDEMPRESA
      AND CDINSCESTA = P_CDINSCESTA;
/************************************************************************************************/
   /* REGISTRO 88SME */
   IF V_ENTRADA IS NULL THEN
      V_LINHA_88SME := '88SME'||
                       V_CNPJ||
                       RPAD(REPLACE(REPLACE(REPLACE(P_CDINSCESTA,'-',''),'.',''),'/',''), 14, ' ')||
                       '    SEM MOVIMENTO DE ENTRADAS     '||
                       RPAD(' ', 59, ' ');

      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        ('19'||LPAD(RTRIM(V_CONTADOR), 8, '0'), V_LINHA_88SME, P_NRSEQUENCIAL);
      V_CONTADOR  := NVL(V_CONTADOR,0)+1;
   END IF;
/************************************************************************************************/
   /* REGISTRO 88SMS */
   IF V_SAIDA IS NULL THEN
      V_LINHA_88SMS := '88SMS'||
                       V_CNPJ||
                       RPAD(REPLACE(REPLACE(REPLACE(P_CDINSCESTA,'-',''),'.',''),'/',''), 14, ' ')||
                       '      SEM MOVIMENTO DE SAIDAS     '||
                       RPAD(' ', 59, ' ');

      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        ('19'||LPAD(RTRIM(V_CONTADOR), 8, '0'), V_LINHA_88SMS, P_NRSEQUENCIAL);
      V_CONTADOR  := NVL(V_CONTADOR,0)+1;
   END IF;

/************************************************************************************************/
   /* REGISTRO 88SEC */
   SELECT MAX('88EC'||
          SUBSTR(LPAD(NVL(NMCONTADOR, ' '), 39, ' '),1,39)||
          LPAD(REPLACE(REPLACE(REPLACE(NVL(NRCPFRESPONS,'0'),'.',''),'-',''),'/',''), 11, '0')||
          LPAD(REPLACE(REPLACE(REPLACE(NVL(NRCRCCONT2,'0'),'.',''),'-',''),'/',''), 10, ' ')||
          LPAD(REPLACE(REPLACE(NVL(NRTELCONTATO,'0'),'-',''),'.',''),11,'0')||
          RPAD(' ',50,' ')||
          '1')
     INTO V_LINHA_88SEC
     FROM INSCRESTAD I, CONTADOR C
    WHERE CDEMPRESA = P_CDEMPRESA
      AND CDINSCESTA = P_CDINSCESTA
      AND I.CDCONTADOR2 = C.CDCONTADOR2(+);

   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     ('19'||LPAD(RTRIM(V_CONTADOR), 8, '0'), V_LINHA_88SEC, P_NRSEQUENCIAL);
   V_CONTADOR  := NVL(V_CONTADOR,0)+1;

/************************************************************************************************/
   /* REGISTRO 88SF */
   V_LINHA_88SF :=  '88SF'||
                    LPAD('AMA Software LTDA EPP', 35, ' ')||
                    '06240810000100'||
                    '06072167608'||
                    '03121222300'||
                    LPAD('fiscal@teknisa.com', 50, ' ')||
                    '1';

   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     ('19'||LPAD(RTRIM(V_CONTADOR), 8, '0'), V_LINHA_88SF, P_NRSEQUENCIAL);
   V_CONTADOR  := NVL(V_CONTADOR,0)+1;


   RETURN('');
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('REGISTRO88_SINTEGRA_MS','5.45.000'); END;
/

