Skip to content

Commit

Permalink
Adicionado classes de leitura de arquivos CNAB 240
Browse files Browse the repository at this point in the history
  • Loading branch information
fhferreira committed Dec 2, 2016
1 parent 653cbfd commit 35fdfe3
Show file tree
Hide file tree
Showing 14 changed files with 809 additions and 204 deletions.
109 changes: 109 additions & 0 deletions src/Helpers/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,113 @@ public static function formatNossoNumero($value)
];
}

public static function unaccent($value)
{
return str_replace(array(
'à',
'á',
'â',
'ã',
'ä',
'ç',
'è',
'é',
'ê',
'ë',
'ì',
'í',
'î',
'ï',
'ñ',
'ò',
'ó',
'ô',
'õ',
'ö',
'ù',
'ú',
'û',
'ü',
'ý',
'ÿ',
'À',
'Á',
'Â',
'Ã',
'Ä',
'Ç',
'È',
'É',
'Ê',
'Ë',
'Ì',
'Í',
'Î',
'Ï',
'Ñ',
'Ò',
'Ó',
'Ô',
'Õ',
'Ö',
'Ù',
'Ú',
'Û',
'Ü',
'Ý'
), array(
'a',
'a',
'a',
'a',
'a',
'c',
'e',
'e',
'e',
'e',
'i',
'i',
'i',
'i',
'n',
'o',
'o',
'o',
'o',
'o',
'u',
'u',
'u',
'u',
'y',
'y',
'A',
'A',
'A',
'A',
'A',
'C',
'E',
'E',
'E',
'E',
'I',
'I',
'I',
'I',
'N',
'O',
'O',
'O',
'O',
'O',
'U',
'U',
'U',
'U',
'Y'
), $value);
}

}
106 changes: 1 addition & 105 deletions src/Remessa/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,111 +82,7 @@ protected function upper()

protected function unaccent()
{
$this->value = str_replace(array(
'à',
'á',
'â',
'ã',
'ä',
'ç',
'è',
'é',
'ê',
'ë',
'ì',
'í',
'î',
'ï',
'ñ',
'ò',
'ó',
'ô',
'õ',
'ö',
'ù',
'ú',
'û',
'ü',
'ý',
'ÿ',
'À',
'Á',
'Â',
'Ã',
'Ä',
'Ç',
'È',
'É',
'Ê',
'Ë',
'Ì',
'Í',
'Î',
'Ï',
'Ñ',
'Ò',
'Ó',
'Ô',
'Õ',
'Ö',
'Ù',
'Ú',
'Û',
'Ü',
'Ý'
), array(
'a',
'a',
'a',
'a',
'a',
'c',
'e',
'e',
'e',
'e',
'i',
'i',
'i',
'i',
'n',
'o',
'o',
'o',
'o',
'o',
'u',
'u',
'u',
'u',
'y',
'y',
'A',
'A',
'A',
'A',
'A',
'C',
'E',
'E',
'E',
'E',
'I',
'I',
'I',
'I',
'N',
'O',
'O',
'O',
'O',
'O',
'U',
'U',
'U',
'U',
'Y'
), $this->value);
$this->value = Helper::unaccent($this->value);
}

/**
Expand Down
73 changes: 70 additions & 3 deletions src/Retorno/CNAB240/Arquivo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Sicoob\Retorno\CNAB240;

use Exception;

class Arquivo
{
public $filename;
Expand All @@ -12,16 +14,81 @@ class Arquivo

public $lines = [];

public $boletos = [];

public $trailerLote;

public $trailer;

public function __construct($filename = null)
{
if ($filename) {
$this->fill($filename);
}
}

public function fill($filename)
{
if (!$filename || !file_exists($filename)) {
throw new Exception("O Arquivo não foi encontrado ou o caminho informado está inválido.");
}

$this->filename = $filename;
$this->parseFileToArray();
$this->parseHeaderAndTrailer();
$this->parseBoletos();
}

public function parseFileToArray()
{
$file = fopen($this->filename, "r");
$lines = [];
while (!feof($file)) {
$line = fgets($file);
$line = trim($line);

if ($line) {
$lines[] = $line;
}
}
fclose($file);
$this->lines = $lines;
}

public function parseHeaderAndTrailer()
{
//First Line Header
$header = array_shift($this->lines);
$this->header = new Header($header);

//Second Line Header Lote
$headerLote = array_shift($this->lines);
$this->headerLote = new HeaderLote($headerLote);

//Lines of File on Array
//Last Trailer
$trailer = array_pop($this->lines);
$this->trailer = new Trailer($trailer);

//Read the File
//Until Last Trailer Lote
$trailerLote = array_pop($this->lines);
$this->trailerLote = new TrailerLote($trailerLote);
}

//Parse Line to Specific type of Object
public function parseBoletos()
{
$total = count($this->lines);
for ($i = 0; $i < $total; $i++) {
$lineT = $this->lines[$i];
$i++;
$lineU = $this->lines[$i];
$boleto = new Boleto($lineT, $lineU);
$this->addBoleto($boleto);
}
}

public function addBoleto(Boleto $boleto)
{
array_push($this->boletos, $boleto);
}

}
14 changes: 14 additions & 0 deletions src/Retorno/CNAB240/Boleto.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,18 @@ class Boleto

public $segmentU;

public $valor;

public function __construct($segmentT = null, $segmentU = null)
{
if ($segmentT && $segmentU)
$this->fill($segmentT, $segmentU);
}

public function fill($segmentT, $segmentU)
{
$this->segmentT = new SegmentT($segmentT);
$this->segmentU = new SegmentU($segmentU);
$this->valor = $this->segmentU->fields['valor_liquido']->value;
}
}
1 change: 0 additions & 1 deletion src/Retorno/CNAB240/LineAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public function fill($line)
$field->start = $config['start'];
$field->end = $config['end'];
$field->length = $config['length'];
$field->type = $config['type'];
$field->values = isset($config['values']) ? $config['values'] : null;
$field->name = $configName;
$field->value = Helper::cutInterval($line, $config['start'], $config['end']);
Expand Down
2 changes: 1 addition & 1 deletion src/Retorno/CNAB240/SegmentT.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class SegmentT extends LineAbstract
'length' => 4,
'required' => '',
'type' => 'numeric',
'description' => 'Código do Banco na Compensação: "756"'
'description' => 'Código do Lote 0001'
],
'tipo_registro' => [
'seq' => '03.3T',
Expand Down
Loading

0 comments on commit 35fdfe3

Please sign in to comment.