Last active
October 14, 2023 15:15
-
-
Save cupertinobr/68756c47f0837127be8c743fbdf156e6 to your computer and use it in GitHub Desktop.
IMPORTAR OFX
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 | |
# 1 - CRIE O ARQUIVO PHP "PHP File" NA PASTA RAIZ DO PROJETO E COLE ESSE CÓDIGO: | |
class OFX | |
{ | |
private $arquivo; | |
public function __construct($arquivo) | |
{ | |
$this->arquivo = json_decode(urldecode($arquivo))->fileName; | |
} | |
public function ConverterOFX_XML() | |
{ | |
$arquivo = file_get_contents($this->arquivo); | |
$line = strpos($arquivo, "<OFX>"); | |
$ofx = substr($arquivo, $line - 1); | |
$buffer = $ofx; | |
$count = 0; | |
$als = []; | |
$sla = []; | |
while ($pos = strpos($buffer, '<')) | |
{ | |
$count++; | |
$pos2 = strpos($buffer, '>'); | |
$element = substr($buffer, $pos + 1, $pos2 - $pos - 1); | |
if (substr($element, 0, 1) == '/') | |
$sla[] = substr($element, 1); | |
else | |
$als[] = $element; | |
$buffer = substr($buffer, $pos2 + 1); | |
} | |
$adif = array_diff($als, $sla); | |
$adif = array_unique($adif); | |
$ofxy = $ofx; | |
foreach ($adif as $dif) | |
{ | |
$dpos = 0; | |
while ($dpos = strpos($ofxy, $dif, $dpos + 1)) { | |
$npos = strpos($ofxy, '<', $dpos + 1); | |
$ofxy = substr_replace($ofxy, "</$dif>\n<", $npos, 1); | |
$dpos = $npos + strlen($element) + 3; | |
} | |
} | |
$ofxy = str_replace('&', '&', $ofxy); | |
return $ofxy; | |
} | |
public function Saldo() | |
{ | |
$xml = new SimpleXMLElement($this->ConverterOFX_XML()); | |
$balance = $xml->BANKMSGSRSV1->STMTTRNRS->STMTRS->LEDGERBAL->BALAMT; | |
$dateOfBalance = $xml->BANKMSGSRSV1->STMTTRNRS->STMTRS->LEDGERBAL->DTASOF; | |
$date = strtotime(substr($dateOfBalance, 0, 8)); | |
$dateToReturn = date('Y-m-d', $date); | |
return Array( | |
'date' => $dateToReturn, | |
'balance' => $balance | |
); | |
} | |
public function Transacoes() | |
{ | |
$xml = new SimpleXMLElement($this->ConverterOFX_XML()); | |
$transacoes = $xml->BANKMSGSRSV1->STMTTRNRS->STMTRS->BANKTRANLIST->STMTTRN; | |
return $transacoes; | |
} | |
} | |
# 2 - NA JANELA DE IMPORTAÇÃO DO OFX | |
require_once('OFX.php'); | |
class ImportarArquivoOFX extends TWindow | |
{ | |
public function __construct( $param = null) | |
{ | |
parent::__construct(); | |
parent::setSize(800, null); | |
parent::setTitle("EXTRATO BANCÁRIO - OFX"); | |
parent::setProperty('class', 'window_modal'); | |
$this->form = new BootstrapFormBuilder(self::$formName); | |
$arquivo = new TFile('arquivo'); | |
$arquivo->setCompleteAction(new TAction([$this,'SelecionarOFX'])); | |
$arquivo->setSize('100%'); | |
$arquivo->enableFileHandling(); | |
$arquivo->setLimitUploadSize(100); | |
$arquivo->setAllowedExtensions(["ofx"]); | |
$row1 = $this->form->addFields([new TLabel("SELECIONE O ARQUIVO", null, '14px', null, '100%'),$arquivo]); | |
$row1->layout = [' col-sm-12']; | |
parent::add($this->form); | |
} | |
} | |
# MÉTODO DE UPLOAD DO OFX | |
public static function SelecionarOFX($param = null) | |
{ | |
try | |
{ | |
if (isset($param['arquivo'])) | |
{ | |
TTransaction::open(self::$database); | |
$ofx = new OFX($param['arquivo']); | |
$transacoes = $ofx->Transacoes(); | |
$saldo_geral = $ofx->Saldo(); | |
$saldo_data = date("d/m/Y", strtotime($saldo_geral['date'])); | |
$saldo_valor = str_replace('.','', $saldo_geral['balance']); | |
$contador = 0; | |
$saldo = 0; | |
foreach ($transacoes as $transacao) | |
{ | |
$tipo = (string) $transacao->TRNTYPE; | |
$descricao = (string) $transacao->MEMO; | |
$valor = (double) $transacao->TRNAMT; | |
$numero = (string) $transacao->CHECKNUM; | |
$data_transacao = date('Y-m-d', strtotime(substr((string)$transacao->DTPOSTED, 0, 8))); | |
$dtposted = new DateTime($data_transacao); | |
$lancamento = new Lancamento; | |
$lancamento->numero = $numero; | |
$lancamento->descricao = $descricao; | |
$lancamento->pagamento = $data_transacao; | |
$lancamento->emissao = $data_transacao; | |
$lancamento->mes_pagamento = $dtposted->format('m'); | |
$lancamento->ano_pagamento = $dtposted->format('Y'); | |
if ($tipo == 'CREDIT') | |
{ | |
$lancamento->entrada = $valor; | |
$lancamento->saida = 0; | |
} elseif ($tipo == 'DEBIT') | |
{ | |
$lancamento->saida = $valor; | |
$lancamento->entrada = 0; | |
} | |
$lancamento->store(); | |
$saldo += $valor; | |
$contador++; | |
} | |
TTransaction::close(); | |
if ($contador > 0) {TApplication::loadPage('LancamentoList','onShow');} | |
} | |
} | |
catch (Exception $e) | |
{ | |
new TMessage('error', $e->getMessage()); | |
} | |
} | |
# FONTE: https://forum.imasters.com.br/topic/490216-leitura-de-arquivos-ofx-com-php |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment