BEGIN
   DECLARE V_FILEHANDLE  UTL_FILE.FILE_TYPE;
BEGIN
   FOR CUR_FIL IN (
   SELECT F.CDFILIAL, LPAD(TO_CHAR(TO_NUMBER(F.CDFILIAL)),2,'0') AS CDEXPORTEMPR
   FROM FILIAL F, PARAMEMPRESA P
   WHERE F.CDEMPRESA = P.CDEMPRESA
   ORDER BY F.CDFILIAL) LOOP

      V_FILEHANDLE := UTL_FILE.FOPEN('EXPORA_DIR', 'ExpNFSDC'||NVL(CUR_FIL.CDEXPORTEMPR,CUR_FIL.CDFILIAL)||'.txt', 'w');

      FOR CUR_DADOS IN (
      SELECT
	  'Sky Chefs Argentina'||' '||
          LPAD(substr(to_char(NRNOTAFISC),1,10),10, '0')||' '||
          LPAD(TO_CHAR((TO_NUMBER(NRSEQLANC) - 1)), 1, '0')||' '||
          RPAD(SUBSTR(NVL(CDCONTCTBL, ' '), 1, 10),10)||
          RPAD(NVL(NMCONTCTBL, ' '),40)||
         'LINE'||
          LPAD(TO_CHAR(NVL(VRLANCAMENT,0),'00000000000000.00'),18)||
             '1'  AS DSARQUIVO
      FROM (SELECT NF.CDFILIAL,   NF.NRLANCTONF, NF.NRNOTAFISC,
                   DECODE(NF.CDTIPOOPER, '01', LC.VRLANCAMENT, (LC.VRLANCAMENT * -1)) AS VRLANCAMENT,
                   LC.NRSEQLANC, LC.CDCONTCTBL, C.NMCONTCTBL
            FROM NOTAFISCAL NF, LANCCONT LC, PARAMEMPRESA PR, EXPORTCLIE E, EXPFILCLIE EC, CONTCTBL C,
                 (SELECT DISTINCT CDFILIAL, NRLANCTONF
                  FROM CONTARECEBER
                  WHERE NRLANCTODUP IS NOT NULL) C,
                 (SELECT CDFILIAL, NRLANCTONF, MAX(NRFORMULARNF) AS NRFORMULARNF
                  FROM FORMULARIONF
                  GROUP BY CDFILIAL, NRLANCTONF) F
            WHERE (NF.CDFILIEMIT     = CUR_FIL.CDFILIAL)                 AND
                  (NF.IDSTATUS       IN ('A','E'))                       AND
                  (NF.DTEMISSAO <= SYSDATE)                              AND
                  (NF.CDTIPOOPER     IN ('01','17','18'))                AND
                  (NF.IDENTRSAIDOP   = 'S')                              AND
                  (NF.CDCLIENTE      = E.CDCLIENTE(+))                   AND
                  (NF.CDCLIENTE      = EC.CDCLIENTE(+))                  AND
                  (NF.CDFILIEMIT     = EC.CDFILIAL(+))                   AND
                  (NF.CDFILIAL       = C.CDFILIAL)                       AND
                  (NF.NRLANCTONF     = C.NRLANCTONF)                     AND
                  (LC.CDEMPRESA(+)   = NF.CDEMPRESA)                     AND
                  (LC.NRLANCCONT(+)  = NF.NRLANCTBDOC)                   AND
                  NOT (LC.CDCONTCTBL LIKE '11111%')                      AND
                  (NF.DTEXPORTACAO IS NULL)                              AND
                  (NF.CDEMPRESA      = PR.CDEMPRESA(+))                  AND
                  (NF.CDFILIAL       = F.CDFILIAL)                       AND
                  (NF.NRLANCTONF     = F.NRLANCTONF)                     AND
                  (C.CDCONTCTBL      = LC.CDCONTCTBL))
      ORDER BY NRNOTAFISC, NRSEQLANC) LOOP

         UTL_FILE.PUT(V_FILEHANDLE, CUR_DADOS.DSARQUIVO||CHR(13)||CHR(10));
         UTL_FILE.FFLUSH(V_FILEHANDLE);
      END LOOP;

      UTL_FILE.FCLOSE(V_FILEHANDLE);
   END LOOP;
END;
END;
/
