CREATE OR REPLACE PROCEDURE ARQUIVO_SPED(
   P_CDIMPOSTO        IN IMPOSTO.CDIMPOSTO%TYPE,
   P_CDEMPRESA        IN EMPRESA.CDEMPRESA%TYPE,
   P_CDINSCESTA       IN EMPRESA.CDINSCESTA%TYPE,
   P_CDINSCMUNI       IN EMPRESA.CDINSCMUNI%TYPE,
   P_DTINICIAL        IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_DTFINAL          IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_LIVROMAG         IN LIVROMAGNETICO.CDLIVROMAG%TYPE,
   P_TIPO             IN NOTAFISCAL.IDSTATUS%TYPE,
   P_FINALIDADE       IN NOTAFISCAL.CDTIPOOPER%TYPE,
   P_INDESCRITU       IN NOTAFISCAL.IDSTATUS%TYPE,
   P_PERFIL           IN VARCHAR2,
   P_ATIVIDADE        IN   VARCHAR2,
   P_VERSAO           IN VARCHAR2,
   P_NRSEQARQMAG      IN ARQMAGNETICO.NRSEQARQMAG%TYPE,
   P_BLOCOH           IN VARCHAR2,
   P_H020             IN VARCHAR2,
   P_DTINIPER         IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_DTFINPER         IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_IDDIFALIQ        IN VARCHAR2,
   P_GERAFORSALE      IN VARCHAR2 DEFAULT 'N',
   P_MOTINV           IN VARCHAR2,
   P_OBRIGREGBL1      IN VARCHAR2,
   P_CDLIVROMAGADIC   IN LIVROMAGADIC.CDLIVROMAGADIC%TYPE,
   P_IDORIGLIVROADIC  IN NOTAFISCAL.IDSTATUS%TYPE,
   P_DTINIVAF         IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_DTFINVAF         IN NOTAFISCAL.DTENTRSAID%TYPE,
   P_GERABLOCOK       IN VARCHAR2,
   P_ORIGBLOCOK       IN VARCHAR2,
   P_AGRUPPROD        IN GRUPPROD.CDGRUPPROD%TYPE,
   P_CDINSCENTR       IN VARCHAR2,
   P_IDCLASSIFICESTAB IN DADOSGERSPED.IDCLASSIFICESTAB%TYPE,
   P_IDINFCOMPLST     IN DADOSGERSPED.IDINFCOMPLST%TYPE,
   P_NRDIAMEDIAEST    IN DADOSGERSPED.NRDIAMEDIAEST%TYPE,
   P_IDGERACAMPO11ST  IN DADOSGERSPED.IDGERACAMPO11ST%TYPE,
   P_IDCONSPRODEST    IN DADOSGERSPED.IDCONSPRODEST%TYPE DEFAULT 'S',
   P_IDCONSPRODEXT    IN VARCHAR2)
AS
   V_CONTGERA     NUMBER;
   V_CONTREGA     NUMBER;
   V_CONTREGB_AUX VARCHAR2(100);
   V_CONTREGB     NUMBER;
   V_CONTREGC     NUMBER;
   V_CONTREGC_AUX VARCHAR2(100);
   V_CONTREGD_AUX VARCHAR2(100);
   V_CONTREGD     NUMBER;
   V_CONTREGE     NUMBER;
   V_CONTREGH_AUX VARCHAR2(50);
   V_CONTREGH     NUMBER;
   V_CONTREG1     NUMBER;
   V_CONTREG9     NUMBER;
   V_CONTREGK_AUX VARCHAR2(50);
   V_CONTREGK     NUMBER;
   V_TOTSPED      NUMBER;
   V_TOTFORPER_S  NUMBER;
   V_TOTFORPER_E  NUMBER;
   V_TOTALIZA     NUMBER;
   V_VL_DEB_EXTE  NUMBER;
   V_CONTREGG     NUMBER;
   V_CONTREGG_AUX VARCHAR2(50);
   V_REG          VARCHAR2(100);
   V_IDORCLVER10  VARCHAR2(01);
BEGIN
   V_TOTSPED := 0;
   DELETE FROM TMP_REGDADOSSPED;
   COMMIT;

   BEGIN
      SELECT IDORCLVER10
        INTO V_IDORCLVER10
        FROM PARAMFISCAL;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_IDORCLVER10 := 'N';
   END;

   --IF V_IDORCLVER10 = 'S' THEN
   GRAVA_ENDPART_SPED_ORCLANT;
   --END IF;

----------------------------------------------------  /* ABERTURA */  -------------------------------------------------
   V_CONTREGA := 0;
   V_CONTREGA := SPED_INICIO(P_CDIMPOSTO,P_CDEMPRESA,P_CDINSCESTA,P_DTINICIAL,P_DTFINAL,P_LIVROMAG,P_TIPO,P_FINALIDADE,P_INDESCRITU,P_NRSEQARQMAG,P_PERFIL,P_ATIVIDADE, P_VERSAO, P_IDCLASSIFICESTAB);
------------------------------------------------------/* BLOCO B  */ -----------------------------------------------
   V_CONTREGB     := 0;
   V_CONTREGB_AUX := SPED_BLOCOB(P_CDEMPRESA,P_CDINSCESTA,P_CDINSCMUNI,P_DTINICIAL,P_DTFINAL,P_NRSEQARQMAG,P_VERSAO);
   V_CONTREGB     := TO_NUMBER(SUBSTR(V_CONTREGB_AUX,1 ,10));
   V_TOTALIZA     := TO_NUMBER(SUBSTR(V_CONTREGB_AUX,11,10));
----------------------------------------------------  /* BLOCO C */   ---------------------------------------------
   V_CONTREGC := 0;
   V_CONTREGC_AUX := SPED_BLOCOC(P_CDIMPOSTO, P_CDEMPRESA, P_CDINSCESTA, P_DTINICIAL, P_DTFINAL,P_LIVROMAG,P_CDLIVROMAGADIC, P_NRSEQARQMAG, P_PERFIL, P_VERSAO, P_IDDIFALIQ, V_TOTALIZA, P_IDINFCOMPLST, P_NRDIAMEDIAEST, P_IDGERACAMPO11ST, P_ATIVIDADE, P_IDCONSPRODEST, P_GERAFORSALE, P_IDCONSPRODEXT);
   V_CONTREGC     := TO_NUMBER(SUBSTR(V_CONTREGC_AUX,1 ,10));
   V_TOTFORPER_S  := TO_NUMBER(SUBSTR(V_CONTREGC_AUX,11,10));
   V_TOTFORPER_E  := TO_NUMBER(SUBSTR(V_CONTREGC_AUX,21,10));
   V_TOTALIZA     := TO_NUMBER(SUBSTR(V_CONTREGC_AUX,31,10));
   V_VL_DEB_EXTE  := TO_NUMBER(SUBSTR(V_CONTREGC_AUX,41,10));
----------------------------------------------------  /* BLOCO D */   ---------------------------------------------
   V_CONTREGD     := 0;
   V_CONTREGD_AUX := SPED_BLOCOD(P_CDIMPOSTO, P_CDEMPRESA, P_CDINSCESTA, P_DTINICIAL, P_DTFINAL, P_LIVROMAG,P_CDLIVROMAGADIC, P_NRSEQARQMAG, P_PERFIL, P_VERSAO, V_TOTALIZA, P_IDCONSPRODEST, P_IDCONSPRODEXT);
   V_CONTREGD     := TO_NUMBER(SUBSTR(V_CONTREGD_AUX,1 ,10));
   V_TOTALIZA     := TO_NUMBER(SUBSTR(V_CONTREGD_AUX,11,10));
----------------------------------------------------  /* BLOCO E */   ---------------------------------------------
   V_CONTREGE := 0;
   V_CONTREGE := SPED_BLOCOE(P_CDIMPOSTO, P_CDEMPRESA, P_CDINSCESTA, P_DTINICIAL, P_DTFINAL, P_LIVROMAG, P_NRSEQARQMAG, V_TOTFORPER_S, V_TOTFORPER_E, V_VL_DEB_EXTE,P_GERAFORSALE, P_IDDIFALIQ);
------------------------------------------------------/* BLOCO G  */ -----------------------------------------------
   V_CONTREGG := 0;
   V_CONTREGG_AUX := SPED_BLOCOG(P_CDEMPRESA,P_CDINSCESTA,P_DTINICIAL,P_DTFINAL,P_CDIMPOSTO, V_TOTALIZA,P_NRSEQARQMAG,P_VERSAO, P_IDCONSPRODEXT);
   V_CONTREGG     := TO_NUMBER(SUBSTR(V_CONTREGG_AUX,1 ,10));
   V_TOTALIZA     := TO_NUMBER(SUBSTR(V_CONTREGG_AUX,11,10));
---------------------------------------------------- /*  BLOCO H  */ -----------------------------------------------
   V_CONTREGH     := 0;
   V_CONTREGH_AUX := SPED_BLOCOH(P_CDIMPOSTO, P_CDEMPRESA, P_CDINSCESTA, P_DTINICIAL, P_DTFINAL, P_LIVROMAG, P_VERSAO, P_NRSEQARQMAG, P_BLOCOH, P_H020, P_DTINIPER, P_DTFINPER, P_MOTINV, V_TOTALIZA, P_IDCONSPRODEXT);
   V_CONTREGH     := TO_NUMBER(SUBSTR(V_CONTREGH_AUX,1 ,10));
   V_TOTALIZA     := TO_NUMBER(SUBSTR(V_CONTREGH_AUX,11,10));
----------------------------------------------------  /*  BLOCO K  */  --------------------------------------------
   V_CONTREGK     := 0;
   V_CONTREGK_AUX := SPED_BLOCOK(P_CDEMPRESA, P_CDINSCESTA, P_DTINICIAL, P_DTFINAL, P_NRSEQARQMAG, P_LIVROMAG, P_GERABLOCOK, P_ORIGBLOCOK, V_TOTALIZA, P_IDCONSPRODEXT);
   V_CONTREGK     := TO_NUMBER(SUBSTR(V_CONTREGK_AUX,1 ,10));
   V_TOTALIZA     := TO_NUMBER(SUBSTR(V_CONTREGK_AUX,11,10));
---------------------------------------------------- /*  BLOCO 1  */ -----------------------------------------------
   V_CONTREG1 := 0;
   V_CONTREG1 := SPED_BLOCO1(P_CDIMPOSTO, P_CDEMPRESA, P_CDINSCESTA, P_DTINICIAL, P_DTFINAL, P_VERSAO, P_NRSEQARQMAG,  P_OBRIGREGBL1, V_TOTALIZA, P_LIVROMAG, P_CDLIVROMAGADIC, P_IDORIGLIVROADIC, P_DTINIVAF, P_DTFINVAF, P_AGRUPPROD, P_CDINSCENTR, P_IDCONSPRODEXT);
----------------------------------------------------  /*  BLOCO 9  */  --------------------------------------------
   V_CONTREG9 := 0;
   V_CONTREG9 := SPED_BLOCO9(P_VERSAO,P_NRSEQARQMAG,P_LIVROMAG,P_CDEMPRESA,P_CDINSCESTA,P_ORIGBLOCOK,V_CONTREGA,P_DTINICIAL,P_DTFINAL, P_IDCONSPRODEXT);
-------------------------------------   /* ENCERRAMENTO TOTAL DO BLOCO */ ---------------------------------------------
   V_CONTGERA := NVL(V_CONTREGA,0) + NVL(V_CONTREGB,0) + NVL(V_CONTREGC,0) + NVL(TO_NUMBER(V_CONTREGD),0) + NVL(V_CONTREGE,0)+ NVL(V_CONTREGG,0) + NVL(V_CONTREGH,0) + NVL(V_CONTREG9,0)+2;

   /* total de linhas do arquivo somado mais um quando insere na tabela para que seja considerado o ultimo registro 9999 */
   BEGIN
      SELECT COUNT(*)
      INTO V_TOTSPED
      FROM ARQMAGNETICO
      WHERE NRSEQARQMAG = P_NRSEQARQMAG;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_TOTSPED := 0;
   END;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     ('99'||LPAD(RTRIM(V_CONTREG9), 8, '0'), '|9999'|| '|' ||RTRIM(V_TOTSPED+1)|| '|',  P_NRSEQARQMAG);
END;
/