create or replace PROCEDURE LANC_DIARIOGERAL_411(
   P_CDEMPRESA    IN NOTAFISCAL.CDEMPRESA%TYPE,
   P_DTINICIAL    IN NOTAFISCAL.DTEMISSAO%TYPE,
   P_DTFINAL      IN NOTAFISCAL.DTEMISSAO%TYPE,
   P_NRSEQARQMAG  IN ARQMAGNETICO.NRSEQARQMAG%TYPE)
AS
   V_SEQUENCIAL      NUMBER;
   V_CONTRAPARTIDA   VARCHAR2(400);
BEGIN
   FOR LANC_DIARIOGERAL IN (SELECT TO_CHAR(L.DTLANCCONT,'DDMMYYYY')||
                                   RPAD(L.CDCONTCTBL,28,' ')||
                                   RPAD(NVL(LC.CDCENTCUST,'1001'),28)||
                                   RPAD(' ',28,' ')||
                                   SUBSTR(LPAD(LTRIM(REPLACE(TO_CHAR(ROUND(ABS(NVL(LC.VRLANCCUST,L.VRLANCAMENT)),2),'000000000000000.00'),'.','')),17,0),1,17)||
                                   DECODE(ABS(NVL(LC.VRLANCCUST,L.VRLANCAMENT)),NVL(LC.VRLANCCUST,L.VRLANCAMENT),'C','D')||
                                   RPAD(NVL(L.CDDOCUCONT,' '),12,' ')||
                                   RPAD(NVL(L.NRLANCCONT,' '),12,' ')||
                                   RPAD(REPLACE(NVL(DSHISTLANC,0),'
',''),150,' ') AS PARTE_1
		              FROM LANCCONT L,LANCCCUSTO LC
                             WHERE L.CDEMPRESA  = P_CDEMPRESA
                               AND L.DTLANCCONT BETWEEN P_DTINICIAL AND P_DTFINAL
			       AND L.CDEMPRESA = LC.CDEMPRESA(+)
                               AND L.NRLANCCONT = LC.NRLANCCONT(+)
                               AND L.NRSEQLANC  = LC.NRSEQLANC(+)                            
                             ORDER BY L.DTLANCCONT) LOOP     
V_SEQUENCIAL := NVL(V_SEQUENCIAL,0) +1;
     INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
        VALUES
        ('08'||LPAD(V_SEQUENCIAL,8,0),LANC_DIARIOGERAL.PARTE_1,  P_NRSEQARQMAG);
     V_CONTRAPARTIDA := '';
   END LOOP;
END;
/
-- VERSAO --
BEGIN ATUVEROBJ('LANC_DIARIOGERAL_411','5.33.xxx'); END;
/