CREATE OR REPLACE FUNCTION ARQUIVO_GIA_MS_08(
   P_CDEMPRESA   IN EMPRESA.CDEMPRESA%TYPE,
   P_CDINSCESTA  IN NOTAFISCAL.CDINSCESTA%TYPE,
   P_CDIMPOSTO   IN IMPOSTO.CDIMPOSTO%TYPE,
   P_DTINICIAL   IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_DTFINAL     IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_TPDEC       IN VARCHAR,
   P_PDEC        IN VARCHAR,
   P_CONTADOR    IN VARCHAR,
   P_SEQMAGNET   IN VARCHAR,
   P_QTDEFUNC    IN VARCHAR,
   P_FOLHAPGTO   IN VARCHAR,
   P_ENCARGOS    IN VARCHAR ) RETURN VARCHAR
AS
   V_CONTADOR            NUMBER;
   V_INSERT              VARCHAR2(100);
   V_MESINI              VARCHAR2(2);
BEGIN
   V_CONTADOR := NVL(P_CONTADOR,0);
   IF P_PDEC = '2' THEN
      V_MESINI := '00';
   ELSE
      V_MESINI := TO_CHAR(P_DTINICIAL,'MM');
   END IF;
   V_INSERT := SUBSTR(RPAD(REPLACE(REPLACE(REPLACE(NVL(P_CDINSCESTA, ' '),'.',''),'-',''),'ISENT','ISENTO'),9,' '),1,9)
               ||TO_CHAR(P_DTINICIAL,'YYYY')
               ||V_MESINI
               ||TO_CHAR(P_DTFINAL,'MM')
               ||P_TPDEC
               ||'08'
               ||LPAD(NVL(P_QTDEFUNC,'0'), 7, '0')
               ||LPAD(REPLACE(REPLACE(NVL(P_FOLHAPGTO,'0'),'.',''),',',''),13,'0')
               ||LPAD(REPLACE(REPLACE(NVL(P_ENCARGOS,'0'),'.',''),',',''),13,'0');

   V_CONTADOR := NVL(V_CONTADOR,0)+1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
    VALUES
     (LPAD(V_CONTADOR, 10, '0'),V_INSERT,P_SEQMAGNET);

   RETURN (TO_CHAR(LPAD(V_CONTADOR, 10, '0')));
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('ARQUIVO_GIA_MS_08','5.33.000'); END;
/
