CREATE OR REPLACE PROCEDURE SPED_CONTABIL_ENCERRAMENTO (
   P_NRSEQARQMAG  IN ARQMAGNETICO.NRSEQARQMAG%TYPE,
   P_CDEMPRESA    IN EMPRESA.CDEMPRESA%TYPE,
   P_DTINICIAL    IN DATE,
   P_DTFINAL      IN DATE,
   P_DSNATUREZA   IN VARCHAR2,
   P_NRINIORDENA  IN INTEGER,
   P_IDSPED       IN VARCHAR2, /* PARAMETRO QUE DEFINE SE SERA GERADO SPED OU FCONT */
   P_NRORDREGARQ  OUT INTEGER)

IS
   V_NRORDREGARQ INTEGER;
   V_AUXILIAR    VARCHAR2(200);
   V_CONTADOR    INTEGER;

BEGIN
   V_NRORDREGARQ := P_NRINIORDENA;

   V_AUXILIAR := '|9001|0|';
   V_NRORDREGARQ := V_NRORDREGARQ + 1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), V_AUXILIAR, P_NRSEQARQMAG);

   V_CONTADOR := 1;
   FOR C_9900 IN (
   SELECT '|9900|'||
          SUBSTR(DSREGARQMAG,2,4)||'|'||
          TO_CHAR(COUNT(*))||'|' AS DSLINHA
   FROM ARQMAGNETICO
   WHERE NRSEQARQMAG = P_NRSEQARQMAG
     AND SUBSTR(DSREGARQMAG,2,1) <> '9'
   GROUP BY SUBSTR(DSREGARQMAG,2,4)
   ORDER BY SUBSTR(DSREGARQMAG,2,4)) LOOP
      V_NRORDREGARQ := V_NRORDREGARQ + 1;
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), C_9900.DSLINHA, P_NRSEQARQMAG);

      V_CONTADOR := V_CONTADOR + 1;
   END LOOP;

   V_AUXILIAR := '|9900|9001|1|';
   V_NRORDREGARQ := V_NRORDREGARQ + 1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), V_AUXILIAR, P_NRSEQARQMAG);

   V_AUXILIAR := '|9900|9900|' || TO_CHAR(V_CONTADOR + 3)||'|';
   V_NRORDREGARQ := V_NRORDREGARQ + 1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), V_AUXILIAR, P_NRSEQARQMAG);

   V_AUXILIAR := '|9900|9990|1|';
   V_NRORDREGARQ := V_NRORDREGARQ + 1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), V_AUXILIAR, P_NRSEQARQMAG);

   V_AUXILIAR := '|9900|9999|1|';
   V_NRORDREGARQ := V_NRORDREGARQ + 1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), V_AUXILIAR, P_NRSEQARQMAG);

   SELECT '|9990|'||
          TO_CHAR(COUNT(*) + 2)||'|' AS DSLINHA
   INTO V_AUXILIAR
   FROM ARQMAGNETICO
   WHERE NRSEQARQMAG = P_NRSEQARQMAG
     AND SUBSTR(DSREGARQMAG,2,1) = '9';

   V_NRORDREGARQ := V_NRORDREGARQ + 1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), V_AUXILIAR, P_NRSEQARQMAG);

   SELECT '|9999|'||
          TO_CHAR(COUNT(*) + 1)||'|' AS DSLINHA,
          (COUNT(*) + 1) AS NRCONTADOR
   INTO V_AUXILIAR, V_CONTADOR
   FROM ARQMAGNETICO
   WHERE NRSEQARQMAG = P_NRSEQARQMAG;

   V_NRORDREGARQ := V_NRORDREGARQ + 1;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     (LPAD(TO_CHAR(V_NRORDREGARQ),10,'0'), V_AUXILIAR, P_NRSEQARQMAG);

   UPDATE ARQMAGNETICO
   SET DSREGARQMAG = REPLACE(DSREGARQMAG,'#QTDE_TOTAL#',TO_CHAR(V_CONTADOR))
   WHERE NRSEQARQMAG = P_NRSEQARQMAG
     AND SUBSTR(DSREGARQMAG,2,4) IN ('I030','J900');

   P_NRORDREGARQ := V_NRORDREGARQ;
END;
/


-- VERSAO --
BEGIN ATUVEROBJ('SPED_CONTABIL_ENCERRAMENTO','6.35.014'); END;
/

