Skip to content

Instantly share code, notes, and snippets.

@maxcelos
Last active September 22, 2020 03:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxcelos/8d415559449755bf3d85e078256da68c to your computer and use it in GitHub Desktop.
Save maxcelos/8d415559449755bf3d85e078256da68c to your computer and use it in GitHub Desktop.
Converte extrato NuConta de OFX para CSV
<?php
$ofx = $argv[1] ?? null;
if (! $ofx)
echo 'Origin file missing';
$output = $argv[2] ?? 'extrato.csv';
$ofx = @file_get_contents($ofx);
$startDate = makeDate(getVal($ofx, 'DTSTART'));
$endDate = makeDate(getVal($ofx, 'DTEND'));
$importDate = makeDate(getVal($ofx, 'DTSERVER'));
$bankID = getVal($ofx, 'BRANCHID');
$accountID = getVal($ofx, 'ACCTID');
$accountType = getVal($ofx, 'ACCTTYPE');
$orgName = getVal($ofx, 'ORG');
$orgID = getVal($ofx, 'FID');
$result = getVal($ofx, 'BALAMT');
$interest = getVal(getVal($ofx, 'BAL'), 'VALUE');
$transactions = getVal($ofx, 'STMTTRN');
$sum = getSaldoPeriodo($ofx);
$inicial = $result - $interest - $sum;
$inicial = round($inicial, 2);
$result = str_replace('.', ',', $result);
$interest = str_replace('.', ',', $interest);
$inicialStr = str_replace('.', ',', $inicial);
$content = "";
$content = "EXTRATO BANCARIO\n";
$content .= "Cod. Banco;$orgID\n";
$content .= "Banco;$orgName\n";
$content .= "Agencia;$bankID\n";
$content .= "Num Conta;$accountID\n";
$content .= "Tipo de Conta;$accountType\n";
$content .= "De;$startDate\n";
$content .= "Até;$endDate\n";
$content .= "Saldo Inicial;$inicialStr\n";
$content .= "Fechamento;$result\n";
$content .= "Rendimento;$interest\n";
$content .= "\n\n";
$content .= "OPERACOES\n";
$content .= "Data;Tipo;Valor;Saldo;Descricao\n";
$sum = $inicial;
foreach ($transactions as $transaction) {
$date = getVal($transaction, 'DTPOSTED');
$date = makeDate($date);
$type = getVal($transaction, 'TRNTYPE');
$desc = getVal($transaction, 'MEMO');
$encoding = mb_detect_encoding( $desc, array(
"UTF-8",
"UTF-32",
"UTF-32BE",
"UTF-32LE",
"UTF-16",
"UTF-16BE",
"UTF-16LE"
), TRUE );
$desc = mb_convert_encoding($desc, 'ISO-8859-1', $encoding);;
$amount = getVal($transaction, 'TRNAMT');
$sum = $sum + (float) $amount;
$sum = round($sum, 2);
$sumStr = str_replace('.', ',', $sum);
$amount = str_replace('.', ',', $amount);
$content .= "$date;$type;$amount;$sumStr;$desc\n";
}
$file = fopen($output, "w");
fwrite($file, $content);
fclose($file);
function getSaldoPeriodo($ofx) {
$sum = 0;
$transactions = getVal($ofx, 'STMTTRN');
foreach ($transactions as $transaction) {
$amount = getVal($transaction, 'TRNAMT');
$sum = $sum + (float) $amount;
}
return $sum;
}
function getVal($string, $tagName, $simbol = '<>') {
$openTag = $simbol[0] . $tagName . $simbol[1];
$closeTag = $simbol[0] . '/' . $tagName . $simbol[1];
$items = explode($openTag, $string);
unset($items[0]);
$values = [];
foreach ($items as $item) {
$values[] = explode($closeTag, $item)[0];
}
return count($values) === 1 ? $values[0] : $values;
}
function makeDate($date) {
return (new DateTime(substr($date, 0, 8)))->format('Y-m-d');
}
@maxcelos
Copy link
Author

Como usar

Ao receber seu extrato no email, baixe para uma pasta e execute o script passando o path para o arquivo original e um path para a saída em csv.

php nubankOfx2Csv.php NU_xxxxxx_01JUN2020_17SET2020.ofx extrato.csv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment