Last active
September 22, 2020 03:16
-
-
Save maxcelos/8d415559449755bf3d85e078256da68c to your computer and use it in GitHub Desktop.
Converte extrato NuConta de OFX para CSV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.