create or replace PROCEDURE LANC_DIARIOAUXILIAR_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_LANC            NUMBER;
   V_CONTRAPARTIDA   VARCHAR2(30);
   V_DSREGISTRO      VARCHAR2(500);
BEGIN
   FOR LANC_DIARIOAUXILIAR IN (SELECT L.DTLANCCONT,
                                      L.CDCONTCTBL,
				      LC.CDCENTCUST AS CDCENTCUST,
 				      L.VRLANCAMENT AS VRLANCAMENT,
                                      LC.VRLANCCUST AS VRLANCCUST,
                                      DECODE(ABS(NVL(LC.VRLANCCUST,L.VRLANCAMENT)),NVL(LC.VRLANCCUST,L.VRLANCAMENT),'C','D') AS TIPOLANC,
                                      CDDOCUCONT,
                                      DSHISTLANC,
                                      L.CDEMPRESA,
                                      L.NRLANCCONT,
                                      REPLACE(REPLACE(REPLACE(L.CDCONTCOME,'/',''),'.',''),'-','') AS CDCONTCOME
                               FROM LANCCONT L, LANCCCUSTO LC
                               WHERE L.CDEMPRESA  = P_CDEMPRESA
                                 AND L.DTLANCCONT BETWEEN P_DTINICIAL AND P_DTFINAL 
                                 AND L.CDCONTCOME IS NOT NULL
                                 AND L.CDEMPRESA = LC.CDEMPRESA(+)
                                 AND L.NRLANCCONT = LC.NRLANCCONT(+)
                                 AND L.NRSEQLANC  = LC.NRSEQLANC(+) 
                                 ORDER BY DTLANCCONT, L.NRLANCCONT, CDCONTCTBL) LOOP
      V_DSREGISTRO := TO_CHAR(LANC_DIARIOAUXILIAR.DTLANCCONT,'DDMMYYYY')||
                      RPAD(NVL(SUBSTR(LANC_DIARIOAUXILIAR.CDCONTCOME,2,28),' '),28)  ||
                      RPAD(NVL(LANC_DIARIOAUXILIAR.CDCENTCUST,'1001'),28)  ||
                      RPAD(NVL(V_CONTRAPARTIDA,' '),28)                 ||
                      LTRIM(REPLACE(TO_CHAR(ABS(NVL(LANC_DIARIOAUXILIAR.VRLANCCUST,LANC_DIARIOAUXILIAR.VRLANCAMENT)),'000000000000000.00'),'.',''))||
                      LANC_DIARIOAUXILIAR.TIPOLANC||
                      RPAD(NVL(LANC_DIARIOAUXILIAR.CDDOCUCONT,' '),12)||
                      RPAD(NVL(LANC_DIARIOAUXILIAR.NRLANCCONT,' '),12)||
                      RPAD(NVL(LANC_DIARIOAUXILIAR.DSHISTLANC,' '),150);
      V_SEQUENCIAL := NVL(V_SEQUENCIAL,0) +1;
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        ('08'||LPAD(RTRIM(V_SEQUENCIAL), 8, '0'), V_DSREGISTRO,  P_NRSEQARQMAG);
   END LOOP;
END;
/
-- VERSAO --
BEGIN ATUVEROBJ('LANC_DIARIOAUXILIAR_411','5.33.xxx'); END;
/
