Skip to content

Commit

Permalink
Geraçao de arquivo remessa
Browse files Browse the repository at this point in the history
  • Loading branch information
fhferreira committed Nov 21, 2016
1 parent 5a9c590 commit 11bb1b1
Show file tree
Hide file tree
Showing 21 changed files with 1,192 additions and 41 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
index.php
gera.php
vendor/
bin/
remessa.rem
remessa.*
remessa-passou-1-version.rem
13 changes: 11 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,17 @@
convertNoticesToExceptions="false"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="true"
bootstrap="tests/bootstrap.php">
bootstrap="tests/bootstrap.php"
backupGlobals="false"
backupStaticAttributes="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
strict="false"
syntaxCheck="false"
verbose="true"
>
<testsuites>
<testsuite>
<directory>tests</directory>
Expand Down
82 changes: 67 additions & 15 deletions src/Remessa/CNAB240/Arquivo.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,96 @@

class Arquivo {

public $lote;
//Header de Arquivo
public $header;

//Header de Lote
public $headerLote;
//Segmentos P,Q,R,S -> Boletos
public $boletos = [];
//Trailer de Lote

public $trailerLote;
//Trailer de Arquivo
public $trailer;

public $renderedData;

public function render($savePath = null)
public $lines = [];

public function __construct($attributes = [])
{
$rendered = array();
$this->fill($attributes);
}

array_push($rendered, $this->header->render());
array_push($rendered, $this->headerLote->render());
public function fill($attributes)
{
if (isset($attributes['lote'])) {
$this->lote = $attributes['lote'];
$attributes['header']['lote'] = 0;
$attributes['header_lote']['lote'] = $this->lote;
}
$this->header = new HeaderArquivo(isset($attributes['header'])?$attributes['header']:[]);
$this->headerLote = new HeaderLote(isset($attributes['header_lote'])?$attributes['header_lote']:[]);
$this->trailerLote = new TrailerLote([]);
$this->trailer = new TrailerArquivo([]);
}

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

public function render($join = true, $savePath = null, $returnPath = false)
{
array_push($this->lines, $this->header->render());
array_push($this->lines, $this->headerLote->render());

$sumBoletos = [];
foreach($this->boletos as $boleto) {
array_push($rendered, $boleto->render());
$sumBoletos[$boleto->count] = $boleto->valor;
$boletoLines = $boleto->render();
foreach($boletoLines as $line) {
array_push($this->lines, $line);
}
}

array_push($rendered, $this->trailerLote->render());
array_push($rendered, $this->trailer->render());
$calculatedSumBoletos = array_sum($sumBoletos);
$countBoletos = count($sumBoletos);

$dataLote = [
'lote' => $this->lote,
'quantidade_registros' => $countBoletos,
'quantidade_titulos_simples' => $countBoletos,
'valor_titulos_simples' => $calculatedSumBoletos
];
$dataArquivo = [
'quantidade_lotes' => 1,
'quantidade_registros' => $countBoletos
];
$this->trailerLote->fill($dataLote);
$this->trailer->fill($dataArquivo);

$this->renderedData = implode("\r\n", $rendered);
array_push($this->lines, $this->trailerLote->render());
array_push($this->lines, $this->trailer->render());

$this->renderedData = implode("\r\n", $this->lines);

if ($savePath) {
$fp = fopen($savePath, "w");
$path = $this->saveFile($savePath);
if ($returnPath) {
return $path;
}
}

return $join ? $this->renderedData : $this->lines;
}

public function saveFile($filename)
{
if ($filename) {
$fp = fopen($filename, "w");
fwrite($fp, $this->renderedData);
fclose($fp);
return $savePath;
return $filename;
}

return $this->renderedData;
}
}
9 changes: 7 additions & 2 deletions src/Remessa/CNAB240/Boleto.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public function __construct($attributes = [])
$this->fill($attributes);
}

/**
* @param $attributes
*/
public function fill($attributes)
{
if (isset($attributes['valor'])) {
Expand Down Expand Up @@ -109,8 +112,10 @@ public function getSegment($type)
return $this->{"segment" . $type};
}


/**
* @return array
* @param bool $joined
* @return array|string
*/
public function render($joined = false)
{
Expand All @@ -119,6 +124,6 @@ public function render($joined = false)
$rendered[] = $this->getSegment('Q')->render();
$rendered[] = $this->getSegment('R')->render();
$rendered[] = $this->getSegment('S')->render();
return $joined ? implode('\r\n', $rendered) : $rendered;
return $joined ? implode("\r\n", $rendered) : $rendered;
}
}
Loading

0 comments on commit 11bb1b1

Please sign in to comment.