diff --git a/exemplos/abc_remessa.php b/exemplos/abc_remessa.php index 36f8a099..9b716bac 100644 --- a/exemplos/abc_remessa.php +++ b/exemplos/abc_remessa.php @@ -38,6 +38,7 @@ 'operacao' => 1234567, 'agencia' => '0001', 'conta' => '7654321', + 'chaveNfe' => '35240826589893000146550010000263841557593869', ]); $remessa = new Eduardokum\LaravelBoleto\Cnab\Remessa\Cnab400\Banco\Abc([ @@ -49,4 +50,5 @@ ]); $remessa->addBoleto($boleto); +echo '
'; echo $remessa->gerar(); diff --git a/src/Boleto/AbstractBoleto.php b/src/Boleto/AbstractBoleto.php index be339641..30dbf14f 100644 --- a/src/Boleto/AbstractBoleto.php +++ b/src/Boleto/AbstractBoleto.php @@ -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; @@ -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 @@ -292,6 +295,13 @@ abstract class AbstractBoleto implements BoletoContract */ public $pagador; + /** + * Notas fiscais vinculadas ao Boleto + * + * @var NotaFiscalContract[] + */ + public $notasFiscais = []; + /** * Entidade sacadora avalista * @@ -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 * @@ -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; } diff --git a/src/Cnab/Remessa/Cnab400/Banco/Abc.php b/src/Cnab/Remessa/Cnab400/Banco/Abc.php index e1dec28d..f87f4d0c 100644 --- a/src/Cnab/Remessa/Cnab400/Banco/Abc.php +++ b/src/Cnab/Remessa/Cnab400/Banco/Abc.php @@ -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)); } diff --git a/src/Cnab/Remessa/Cnab400/Banco/Fibra.php b/src/Cnab/Remessa/Cnab400/Banco/Fibra.php index cb348194..15417250 100644 --- a/src/Cnab/Remessa/Cnab400/Banco/Fibra.php +++ b/src/Cnab/Remessa/Cnab400/Banco/Fibra.php @@ -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)); } diff --git a/src/Cnab/Remessa/Cnab400/Banco/Ourinvest.php b/src/Cnab/Remessa/Cnab400/Banco/Ourinvest.php index 3b51ba5a..0f61cb0f 100644 --- a/src/Cnab/Remessa/Cnab400/Banco/Ourinvest.php +++ b/src/Cnab/Remessa/Cnab400/Banco/Ourinvest.php @@ -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)); } diff --git a/src/Cnab/Remessa/Cnab400/Banco/Pine.php b/src/Cnab/Remessa/Cnab400/Banco/Pine.php index 0f52b080..b1ab686d 100644 --- a/src/Cnab/Remessa/Cnab400/Banco/Pine.php +++ b/src/Cnab/Remessa/Cnab400/Banco/Pine.php @@ -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)); } diff --git a/src/Cnab/Remessa/Cnab400/Banco/Rendimento.php b/src/Cnab/Remessa/Cnab400/Banco/Rendimento.php index ac83929f..2fa9a32e 100644 --- a/src/Cnab/Remessa/Cnab400/Banco/Rendimento.php +++ b/src/Cnab/Remessa/Cnab400/Banco/Rendimento.php @@ -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)); } diff --git a/src/Contracts/NotaFiscal.php b/src/Contracts/NotaFiscal.php new file mode 100644 index 00000000..3a4a77a7 --- /dev/null +++ b/src/Contracts/NotaFiscal.php @@ -0,0 +1,14 @@ + $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(), + ]; + } +} diff --git a/src/Util.php b/src/Util.php index 5cedf4c3..083aea48 100644 --- a/src/Util.php +++ b/src/Util.php @@ -1113,6 +1113,28 @@ public static function addPessoa(&$property, $obj) throw new ValidationException('Objeto inválido, somente Pessoa e Array'); } + /** + * @param $property + * @param $obj + * + * @return NotaFiscal + * @throws ValidationException + */ + public static function addNotaFiscal(&$property, $obj) + { + if (is_subclass_of($obj, 'Eduardokum\\LaravelBoleto\\Contracts\\NotaFiscal')) { + $property[] = $obj; + + return $obj; + } elseif (is_array($obj)) { + $obj = new NotaFiscal($obj); + $property[] = $obj; + + return $obj; + } + throw new ValidationException('Objeto inválido, somente Pessoa e Array'); + } + /** * @return string */