UNPKG

cnab400-itau-parser

Version:

Extrai informações código de barras e linha digitável de arquivos de retorno CNAB 400 do Itaú

102 lines (84 loc) 3.75 kB
const fs = require('fs'); const path = require('path'); const { calculateDAC, compareInDays, calculateBarCodeDAC } = require('./utils'); /** * Extrai códigos de barras e linhas digitáveis a partir de um buffer de dados ou caminho de arquivo. * * @param {Buffer|string} input - Buffer contendo os dados do arquivo CNAB 400 ou caminho para o arquivo. * @param {Object} options - Opções para incluir o código de barras e a linha digitável. * @param {boolean} [options.barcode=true] - Se deve incluir o código de barras. * @param {boolean} [options.digitLine=true] - Se deve incluir a linha digitável. * @returns {Array} Array contendo códigos de barras e linhas digitáveis. */ async function extractData(input, { barcode = true, digitLine = true }) { let buffer; if (Buffer.isBuffer(input)) { buffer = input; } else if (typeof input === 'string') { buffer = readFileSync(input); } else { throw new Error('Input must be a Buffer or a file path'); } const lines = buffer.toString().split('\n'); const barcodes = []; lines.forEach(line => { const recordType = line.substring(0, 1); if (recordType === '1') { const agency = line.substring(17, 21).trim(); const account = line.substring(23, 28).trim(); const wallet = line.substring(82, 85).trim(); const ourNumber = line.substring(62, 70).trim(); const bankCode = line.substring(165, 168).trim(); const coinCode = 9; const rawDate = line.substring(146, 152).trim(); const daysDiff = compareInDays(rawDate); const rawValue = parseFloat(line.substring(152, 165).trim()) / 100; const value = rawValue.toFixed(2).replace('.', '').padStart(10, '0'); const dacAgency = calculateDAC(agency + account + wallet + ourNumber); const dacAccount = calculateDAC(agency + account); const dacBarCode = calculateBarCodeDAC( `${bankCode}${coinCode}${daysDiff}${value}${wallet}${ourNumber}${dacAgency}${agency}${account}${dacAccount}000` ); const barCode = `${bankCode}${coinCode}${dacBarCode}${daysDiff}${value}${wallet}${ourNumber}${dacAgency}${agency}${account}${dacAccount}000`; const AAA = line.substring(165, 168).trim(); const B = 9; const C = line.substring(82, 83).trim(); const CC = line.substring(83, 85).trim(); const DD = line.substring(85, 87).trim(); const X = calculateDAC(AAA + B + C + CC + DD); const firstField = `${AAA}${B}${C}.${CC}${DD}${X}`; const D = line.substring(87, 92).trim(); const DDD = line.substring(92, 93).trim(); const E = calculateDAC(agency + account + wallet + ourNumber); const FFF = line.substring(17, 20).trim(); const Y = calculateDAC(D + DDD + E + FFF); const secondField = `${D}.${DDD}${E}${FFF}${Y}`; const F = line.substring(20, 21).trim(); const GGGG = line.substring(23, 27).trim(); const GG = line.substring(27, 29).trim(); const HHH = '000'; const Z = calculateDAC(`${F}${GGGG}${GG}${HHH}`); const thirdField = `${F}${GGGG}.${GG}${HHH}${Z}`; barcodes.push({ barcode: barCode, digitLine: `${firstField} ${secondField} ${thirdField} ${dacBarCode} ${daysDiff}${value}` }); } }); return barcodes.map(item => { const result = {}; if (barcode) result.barcode = item.barcode; if (digitLine) result.digitLine = item.digitLine; return result; }); } /** * Lê um arquivo e retorna seu conteúdo como buffer. * * @param {string} filePath - Caminho do arquivo. * @returns {Buffer} Conteúdo do arquivo como buffer. */ function readFileSync(filePath) { return fs.readFileSync(path.resolve(filePath)); } module.exports = extractData;