Skip to content

Commit

Permalink
notas fiscais
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardokum committed Aug 13, 2024
1 parent 891a3aa commit 77be20d
Show file tree
Hide file tree
Showing 10 changed files with 287 additions and 23 deletions.
2 changes: 2 additions & 0 deletions exemplos/abc_remessa.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
'operacao' => 1234567,
'agencia' => '0001',
'conta' => '7654321',
'chaveNfe' => '35240826589893000146550010000263841557593869',
]);

$remessa = new Eduardokum\LaravelBoleto\Cnab\Remessa\Cnab400\Banco\Abc([
Expand All @@ -49,4 +50,5 @@
]);
$remessa->addBoleto($boleto);

echo '<pre>';
echo $remessa->gerar();
66 changes: 66 additions & 0 deletions src/Boleto/AbstractBoleto.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
use Exception;
use Throwable;
use Carbon\Carbon;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use chillerlan\QRCode\QRCode;
use chillerlan\QRCode\QROptions;
use Eduardokum\LaravelBoleto\Util;
use chillerlan\QRCode\Data\QRMatrix;
use Eduardokum\LaravelBoleto\MagicTrait;
use Eduardokum\LaravelBoleto\NotaFiscal;
use chillerlan\QRCode\Output\QROutputInterface;
use Eduardokum\LaravelBoleto\Boleto\Render\Pdf;
use Eduardokum\LaravelBoleto\Boleto\Render\Html;
Expand All @@ -19,6 +21,7 @@
use Eduardokum\LaravelBoleto\Exception\ValidationException;
use Eduardokum\LaravelBoleto\Contracts\Pessoa as PessoaContract;
use Eduardokum\LaravelBoleto\Contracts\Boleto\Boleto as BoletoContract;
use Eduardokum\LaravelBoleto\Contracts\NotaFiscal as NotaFiscalContract;

/**
* Class AbstractBoleto
Expand Down Expand Up @@ -292,6 +295,13 @@ abstract class AbstractBoleto implements BoletoContract
*/
public $pagador;

/**
* Notas fiscais vinculadas ao Boleto
*
* @var NotaFiscalContract[]
*/
public $notasFiscais = [];

/**
* Entidade sacadora avalista
*
Expand Down Expand Up @@ -628,6 +638,58 @@ public function getBeneficiario()
return $this->beneficiario;
}

/**
* Add notas fiscais
*
* @param $notasFiscais
*
* @return AbstractBoleto
* @throws ValidationException
*/
public function setNotasFiscais($notasFiscais)
{
$notasFiscais = Arr::get($notasFiscais, '0') ? $notasFiscais : [$notasFiscais];
foreach ($notasFiscais as $notaFiscal) {
Util::addNotaFiscal($this->notasFiscais, $notaFiscal);
}

return $this;
}

/**
* Retorna as notas fiscais
*
* @return NotaFiscalContract[]
*/
public function getNotasFiscais()
{
if (count($this->notasFiscais) == 0 && $this->chaveNfe) {
return [
new NotaFiscal([
'chave' => $this->chaveNfe,
]),
];
}

return $this->notasFiscais;
}

/**
* Retorna a notas fiscal
*
* @return NotaFiscalContract
*/
public function getNotaFiscal($indice)
{
if ($indice == 0 && count($this->notasFiscais) == 0 && $this->chaveNfe) {
return new NotaFiscal([
'chave' => $this->chaveNfe,
]);
}

return optional(Arr::get($this->notasFiscais, $indice));
}

/**
* Retorna o código do banco
*
Expand Down Expand Up @@ -991,6 +1053,10 @@ public function setChaveNfe($chaveNfe)
*/
public function getChaveNfe()
{
if (count($this->notasFiscais) > 0) {
return Arr::first($this->getNotasFiscais())->getChave();
}

if (strlen($this->chaveNfe) != 44) {
return null;
}
Expand Down
33 changes: 18 additions & 15 deletions src/Cnab/Remessa/Cnab400/Banco/Abc.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,24 +200,27 @@ public function addBoleto(BoletoContract $boleto)
$this->add(394, 394, $boleto->getMoeda());
$this->add(395, 400, Util::formatCnab('9', $this->iRegistros + 1, 6));

if ($chaveNfe = $boleto->getChaveNfe()) {
if (count($boleto->getNotasFiscais()) > 0) {
$this->iniciaDetalhe();
$this->add(1, 1, '4');

$this->add(2, 16, Util::formatCnab('X', ltrim(substr($chaveNfe, 22, 9), 0), 15)); // Numero da nota 1
$this->add(17, 29, Util::formatCnab('9', 0, 11, 2)); // valor da nota 1
$this->add(30, 37, Util::formatCnab('9', 0, 8)); // data nota 1
$this->add(38, 81, Util::formatCnab('9', $chaveNfe, 44)); // Chave da nota 1

$this->add(82, 96, Util::formatCnab('X', ltrim(substr($chaveNfe, 22, 9), 0), 15)); // Numero da nota 2
$this->add(97, 109, Util::formatCnab('9', 0, 11, 2)); // valor da nota 2
$this->add(110, 117, Util::formatCnab('9', 0, 8)); // data nota 2
$this->add(118, 161, Util::formatCnab('9', 0, 44)); // Chave da nota 2

$this->add(162, 176, Util::formatCnab('X', ltrim(substr($chaveNfe, 22, 9), 0), 15)); // Numero da nota 3
$this->add(177, 189, Util::formatCnab('9', 0, 11, 2)); // valor da nota 3
$this->add(190, 197, Util::formatCnab('9', 0, 8)); // data nota 3
$this->add(198, 241, Util::formatCnab('9', 0, 44)); // Chave da nota 3
$nota1 = $boleto->getNotaFiscal(0);
$this->add(2, 16, Util::formatCnab('X', $nota1->getNumero(), 15)); // Numero da nota 1
$this->add(17, 29, Util::formatCnab('9', $nota1->getValor(), 11, 2)); // valor da nota 1
$this->add(30, 37, Util::formatCnab('9', $nota1->getData('dmY'), 8)); // data nota 1
$this->add(38, 81, Util::formatCnab('9', $nota1->getChave(), 44)); // Chave da nota 1

$nota2 = $boleto->getNotaFiscal(1);
$this->add(82, 96, Util::formatCnab('X', $nota2->getNumero(), 15)); // Numero da nota 2
$this->add(97, 109, Util::formatCnab('9', $nota2->getValor(), 11, 2)); // valor da nota 2
$this->add(110, 117, Util::formatCnab('9', $nota2->getData('dmY'), 8)); // data nota 2
$this->add(118, 161, Util::formatCnab('9', $nota2->getChave(), 44)); // Chave da nota 2

$nota3 = $boleto->getNotaFiscal(2);
$this->add(162, 176, Util::formatCnab('X', $nota3->getNumero(), 15)); // Numero da nota 3
$this->add(177, 189, Util::formatCnab('9', $nota3->getValor(), 11, 2)); // valor da nota 3
$this->add(190, 197, Util::formatCnab('9', $nota3->getData('dmY'), 8)); // data nota 3
$this->add(198, 241, Util::formatCnab('9', $nota3->getChave(), 44)); // Chave da nota 3

$this->add(395, 400, Util::formatCnab('9', $this->iRegistros + 1, 6));
}
Expand Down
4 changes: 2 additions & 2 deletions src/Cnab/Remessa/Cnab400/Banco/Fibra.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,10 @@ public function addBoleto(BoletoContract $boleto)
$this->add(394, 394, $boleto->getMoeda());
$this->add(395, 400, Util::formatCnab('9', $this->iRegistros + 1, 6));

if ($chaveNfe = $boleto->getChaveNfe()) {
if (count($boleto->getNotasFiscais()) > 0) {
$this->iniciaDetalhe();
$this->add(1, 1, '4');
$this->add(38, 81, Util::formatCnab('9', $chaveNfe, 44));
$this->add(38, 81, Util::formatCnab('9', $boleto->getNotaFiscal(0)->getChave(), 44));
$this->add(395, 400, Util::formatCnab('9', $this->iRegistros + 1, 6));
}

Expand Down
4 changes: 2 additions & 2 deletions src/Cnab/Remessa/Cnab400/Banco/Ourinvest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ public function addBoleto(BoletoContract $boleto)
$this->add(351, 394, Util::formatCnab('X', $boleto->getSacadorAvalista()->getNome(), 44));
}
$this->add(395, 400, Util::formatCnab('9', $this->iRegistros + 1, 6));
if ($chaveNfe = $boleto->getChaveNfe()) {
if (count($boleto->getNotasFiscais()) > 0) {
$this->iniciaDetalhe();
$this->add(1, 1, '2');
$this->add(82, 125, Util::formatCnab('9', $chaveNfe, 44));
$this->add(82, 125, Util::formatCnab('9', $boleto->getNotaFiscal(0)->getChave(), 44));
$this->add(242, 321, Util::formatCnab('X', $boleto->getPagador()->getEmail(), 80));
$this->add(395, 400, Util::formatCnab('9', $this->iRegistros + 1, 6));
}
Expand Down
4 changes: 2 additions & 2 deletions src/Cnab/Remessa/Cnab400/Banco/Pine.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,10 @@ public function addBoleto(BoletoContract $boleto)
$this->add(392, 393, Util::formatCnab('9', $boleto->getDiasProtesto('0'), 2));
$this->add(394, 394, $boleto->getMoeda());
$this->add(395, 400, Util::formatCnab('9', $this->iRegistros + 1, 6));
if ($chaveNfe = $boleto->getChaveNfe()) {
if (count($boleto->getNotasFiscais()) > 0) {
$this->iniciaDetalhe();
$this->add(1, 1, '4');
$this->add(38, 81, Util::formatCnab('9', $chaveNfe, 44));
$this->add(38, 81, Util::formatCnab('9', $boleto->getNotaFiscal(0)->getChave(), 44));
$this->add(395, 400, Util::formatCnab('9', $this->iRegistros + 1, 6));
}

Expand Down
4 changes: 2 additions & 2 deletions src/Cnab/Remessa/Cnab400/Banco/Rendimento.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,10 @@ public function addBoleto(BoletoContract $boleto)
$this->add(392, 393, Util::formatCnab('9', $boleto->getDiasProtesto('0'), 2));
$this->add(394, 394, $boleto->getMoeda());
$this->add(395, 400, Util::formatCnab('9', $this->iRegistros + 1, 6));
if ($chaveNfe = $boleto->getChaveNfe()) {
if (count($boleto->getNotasFiscais()) > 0) {
$this->iniciaDetalhe();
$this->add(1, 1, '4');
$this->add(38, 81, Util::formatCnab('9', $chaveNfe, 44));
$this->add(38, 81, Util::formatCnab('9', $boleto->getNotaFiscal(0)->getChave(), 44));
$this->add(395, 400, Util::formatCnab('9', $this->iRegistros + 1, 6));
}

Expand Down
14 changes: 14 additions & 0 deletions src/Contracts/NotaFiscal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Eduardokum\LaravelBoleto\Contracts;

interface NotaFiscal
{
public function getChave();

public function getData($format = 'dmy');

public function getValor();

public function getNumero();
}
157 changes: 157 additions & 0 deletions src/NotaFiscal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php

namespace Eduardokum\LaravelBoleto;

use Carbon\Carbon;
use Eduardokum\LaravelBoleto\Exception\ValidationException;
use Eduardokum\LaravelBoleto\Contracts\NotaFiscal as NotaFiscalContract;

class NotaFiscal implements NotaFiscalContract
{
/**
* @var string
*/
protected $chave;

/**
* @var string|null
*/
protected $valor = null;

/**
* @var Carbon|null
*/
protected $data = null;

/**
* @var string|null
*/
protected $numero = null;

/**
* @param $chave
* @param null $numero
* @param null $data
* @param null $valor
* @return NotaFiscal
*/
public static function create($chave, $numero = null, $data = null, $valor = null)
{
return new static([
'chave' => $chave,
'numero' => $numero,
'data' => $data,
'valor' => $valor,
]);
}

/**
* Construtor
*
* @param array $params
*/
public function __construct($params = [])
{
Util::fillClass($this, $params);
}

/**
* @return string
*/
public function getChave()
{
if (strlen($this->chave) != 44) {
return null;
}

return $this->chave;
}

/**
* @param $chave
* @return NotaFiscal
* @throws ValidationException
*/
public function setChave($chave)
{
if (strlen($chave) != 44) {
throw new ValidationException('Chave da nfe não é válida: ' . $chave);
}
$this->chave = $chave;

return $this;
}

/**
* @return string|null
*/
public function getValor()
{
return $this->valor;
}

/**
* @param string|null $valor
*/
public function setValor($valor): NotaFiscal
{
$this->valor = $valor;

return $this;
}

/**
* @return string|null
*/
public function getData($format = 'dmy')
{
return $this->data ? $this->data->format($format) : null;
}

/**
* @param Carbon $data
* @return NotaFiscal
*/
public function setData(Carbon $data)
{
$this->data = $data;

return $this;
}

/**
* @return string|null
*/
public function getNumero()
{
if (is_null($this->numero) && ! is_null($this->chave)) {
return ltrim(substr($this->chave, 25, 9), 0);
}

return $this->numero;
}

/**
* @param string|null $numero
* @return NotaFiscal
*/
public function setNumero($numero)
{
$this->numero = $numero;

return $this;
}

/**
* @return array
*/
public function toArray()
{
return [
'chave' => $this->getChave(),
'numero' => $this->getNumero(),
'data' => $this->getData(),
'valor' => $this->getValor(),
];
}
}
Loading

0 comments on commit 77be20d

Please sign in to comment.