create or replace PROCEDURE ARQUIVO_DIF_TO_J(
   P_CDEMPRESA     IN MAPAAPURAC.CDEMPRESA%TYPE,  --Cod empresa referencia
   P_CDINSCESTA    IN MAPAAPURAC.CDINSCESTA%TYPE,  --I.E. da empresa referencia
   P_PERIODO       IN MAPAAPURAC.CDINSCESTA%TYPE,  --Periodo referEncia AAAA
   P_DTINICIO      IN MAPAAPURAC.DTINIPERIODO%TYPE, --Data inicio periodo
   P_DTFIM         IN MAPAAPURAC.DTFIMPERIODO%TYPE, --Data final periodo o ano sera usado para refencia
   P_NRSEQARQMAG   IN ARQMAGNETICO.NRSEQARQMAG%TYPE, --Sequencial controle tabela ARQMAGNETICO
   P_CONTREG       OUT NUMBER
   )
AS
   V_CONTREG   NUMBER;
BEGIN
   V_CONTREG := 0;

------------------------------/* SEGMENTO J  Demonstrativo do Estoque  */------------------------------
    INSERT INTO ARQMAGNETICO
         (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
    SELECT
        '10'||LPAD(1, 8, '0') ORDEM,
        'J'                                                                   || --J1 Segmento
        LPAD(P_CDINSCESTA,9,'0')                                              || --J2 Inscricao Estadual 
        P_PERIODO                                                             || --J3 Periodo de Referencia
		NVL(REPLACE(LTRIM(TO_CHAR(SUM(NVL(Ini_Tributado,0)),'000000000000.00')),'.'),LPAD(0,'14','0')) || --J4 A - Valor do Estoque Inicial: Tributados
		NVL(REPLACE(LTRIM(TO_CHAR(SUM(NVL(Ini_OutInsNTr,0)),'000000000000.00')),'.'),LPAD(0,'14','0'))     || --J5 B - Valor do Estoque Inicial: Outras, Isentas e/ou Nao Tributadas
		NVL(REPLACE(LTRIM(TO_CHAR(SUM(NVL(Ini_SubsTributaria,0)),'000000000000.00')),'.'),LPAD(0,'14','0'))|| --J6 C - Valor do Estoque Inicial: SubstituicaoTributaria
		NVL(REPLACE(LTRIM(TO_CHAR(SUM(NVL(Fim_Tributado,0)),'000000000000.00')),'.'),LPAD(0,'14','0'))     || --J7 A - Valor do Estoque Final: Tributados
		NVL(REPLACE(LTRIM(TO_CHAR(SUM(NVL(Fim_OutInsNTr,0)),'000000000000.00')),'.'),LPAD(0,'14','0'))     || --J8 B - Valor do Estoque Final: Outras, Isentas e/ou Nao Tributadas
		NVL(REPLACE(LTRIM(TO_CHAR(SUM(NVL(Fim_SubsTributaria,0)),'000000000000.00')),'.'),LPAD(0,'14','0'))   --J9 C - Valor do Estoque Final: SubstituicaoTributaria
		,P_NRSEQARQMAG
	FROM
	  (
      SELECT
        CASE
      	WHEN ((NVL(I.CDSITUTRIBIT,'090') IN ('000','020')) AND (TRUNC(I.DTINVEFISCAL) = TRUNC(P_DTINICIO-1)))
      	  THEN ROUND(I.VRTOTINVFISC,2)
      	  ELSE 0
      	  END Ini_Tributado,
        CASE
      	WHEN ((NVL(I.CDSITUTRIBIT,'090') IN ('040','041','050','051','090')) AND (TRUNC(I.DTINVEFISCAL) = TRUNC(P_DTINICIO-1)))
      	  THEN ROUND(I.VRTOTINVFISC,2)
      	  ELSE 0
      	  END Ini_OutInsNTr,
        CASE
      	WHEN ((NVL(I.CDSITUTRIBIT,'090') IN ('010','030','060','070')) AND (TRUNC(I.DTINVEFISCAL) = TRUNC(P_DTINICIO-1)))
      	  THEN ROUND(I.VRTOTINVFISC,2)
      	  ELSE 0
      	  END Ini_SubsTributaria,
        CASE
      	WHEN ((NVL(I.CDSITUTRIBIT,'090') IN ('000','020')) AND (TRUNC(I.DTINVEFISCAL) = TRUNC(P_DTFIM)))
      	  THEN ROUND(I.VRTOTINVFISC,2)
      	  ELSE 0
      	  END Fim_Tributado,
        CASE
      	WHEN ((NVL(I.CDSITUTRIBIT,'090') IN ('040','041','050','051','090')) AND (TRUNC(I.DTINVEFISCAL) = TRUNC(P_DTFIM)))
      	  THEN ROUND(I.VRTOTINVFISC,2)
      	  ELSE 0
      	  END Fim_OutInsNTr,
        CASE
      	WHEN ((NVL(I.CDSITUTRIBIT,'090') IN ('010','030','060','070')) AND (TRUNC(I.DTINVEFISCAL) = TRUNC(P_DTFIM)))
      	  THEN ROUND(I.VRTOTINVFISC,2)
      	  ELSE 0
      	  END Fim_SubsTributaria
      FROM INVENTFISC I
      WHERE I.CDEMPRESA    = P_CDEMPRESA
      AND I.CDINSCESTA   = P_CDINSCESTA
      AND TRUNC(I.DTINVEFISCAL) IN (TRUNC(P_DTINICIO-1),TRUNC(P_DTFIM))
      AND NVL(I.QTESTINVFISC,0) <> 0
	  )
   ;

   V_CONTREG := V_CONTREG + SQL%ROWCOUNT;

   P_CONTREG := V_CONTREG;
END;
/