-
-
Save scriptdev/3281415004600d43cfbc6f9158a11580 to your computer and use it in GitHub Desktop.
IMPORTAR OFX
This file contains 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" OFX.php | |
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"); | |
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); | |
$Caixa = new Caixa; | |
$Caixa->numero = $numero; | |
$Caixa->descricao = $descricao; | |
$Caixa->pagamento = $data_transacao; | |
$Caixa->emissao = $data_transacao; | |
if ($tipo == 'CREDIT') | |
{ | |
$Caixa->entrada = $valor; | |
} elseif ($tipo == 'DEBIT') | |
{ | |
$Caixa->saida = $valor; | |
} | |
$Caixa->store(); | |
$saldo += $valor; | |
$contador++; | |
} | |
TTransaction::close(); | |
if ($contador > 0) {TApplication::loadPage('CaixaList','onShow');} | |
} | |
} | |
catch (Exception $e) | |
{ | |
new TMessage('error', $e->getMessage()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment