Skip to content

Commit

Permalink
Merge pull request #11 from kbca/master
Browse files Browse the repository at this point in the history
Correção para considerar os valores decimais, pois estavam indo com zero
  • Loading branch information
mikhailcavalcanti committed Oct 14, 2015
2 parents 5dad0df + 503b1d2 commit be78b8f
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ tmp/*
.~lock.*
.buildpath
.project
.idea
.settings
composer.lock
/resources/cache
Expand Down
Binary file added docs/manuais/Manual Cnab 240 bb.pdf
Binary file not shown.
16 changes: 14 additions & 2 deletions src/Cnab/Cnab240/Segmento/AbstractSegmento.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
*/
abstract class AbstractSegmento implements SegmentoInterface
{

const DECIMAL_POINTS = 2;

/**
* Formata uma string, contendo uma data sem o separador, no formato DDMMAA.
* @param string $date String contendo a data no formato DDMMAA.
Expand Down Expand Up @@ -41,9 +44,18 @@ public function createDateTime($dateTimeString, $format = "mdy His")
return DateTime::createFromFormat($format, $dateTimeString);
}

public function convertToFloat(Stringy $string)
/**
* Converte uma stringy para um float, de acordo com a quantidade de casas decimais passadas
* @param Stringy $string
* @param int $decimalPoints
* @return float
*/
public function convertToFloat(Stringy $string, $decimalPoints = self::DECIMAL_POINTS)
{
return (float)$string->__toString();
if (!is_int($decimalPoints)) {
$decimalPoints = self::DECIMAL_POINTS;
}
return (float) preg_replace('#(\d*)(\d{' . $decimalPoints . '})$#', '$1.$2', $string->__toString());
}

public function convertToInt(Stringy $string)
Expand Down
10 changes: 5 additions & 5 deletions src/Cnab/Cnab240/Segmento/SegmentoJ.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ public function buildDetail(Stringy $linha)
->setCodMovimento($linha->substr(16, 2))
->setCodBarras($linha->substr(18, 44))
->setDataVencimento($this->createDate($linha->substr(92, 8)))
->setValorTitulo($linha->substr(100, 13))
->setDesconto($linha->substr(115, 13))
->setAcrescimos($linha->substr(130, 13))
->setValorTitulo($this->convertToFloat($linha->substr(100, 15)))
->setDesconto($this->convertToFloat($linha->substr(115, 15)))
->setAcrescimos($this->convertToFloat($linha->substr(130, 15)))
->setDataPagamento($this->createDate($linha->substr(145, 8)))
->setValorPagamento($linha->substr(153, 13))
->setQuantidadeMoeda($linha->substr(168, 10))
->setValorPagamento($this->convertToFloat($linha->substr(153, 15)))
->setQuantidadeMoeda($this->convertToFloat($linha->substr(168, 15), 5))
->setReferenciaSacado($linha->substr(183, 20))
->setNossoNumero($linha->substr(203, 20))
->setCodMoeda($linha->substr(223, 2))
Expand Down
4 changes: 2 additions & 2 deletions src/Cnab/Cnab240/Segmento/SegmentoT.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function buildDetail(Stringy $linha)
->setCarteira($linha->substr(58, 1))
->setNumeroDocumento($linha->substr(59, 15))
->setDataVencimento($linha->substr(74, 8))
->setValorTitulo($linha->substr(82, 13));
->setValorTitulo($this->convertToFloat($linha->substr(82, 15)));

$banco->setCod($linha->substr(97, 3))
->setAgencia($linha->substr(100, 5))
Expand All @@ -53,7 +53,7 @@ public function buildDetail(Stringy $linha)
->setNome($linha->substr(149, 40));

$detail->setNumeroContrato($linha->substr(189, 10))
->setValorTarifa($linha->substr(199, 13))
->setValorTarifa($this->convertToFloat($linha->substr(199, 15)))
->addOcorrencia($linha->substr(214, 10))
->addCnab($linha->substr(224, 17));

Expand Down
18 changes: 9 additions & 9 deletions src/Cnab/Cnab240/Segmento/SegmentoU.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,22 @@ public function buildDetail(Stringy $linha)
->setCodMovimento($linha->substr(16, 2));

//Dados do Titulo
$dadosTitulo->setAcrescimos($this->convertToFloat($linha->substr(18, 13)))
->setValorDesconto($this->convertToFloat($linha->substr(33, 13)))
->setValorAbatimento($this->convertToFloat($linha->substr(48, 13)))
->setValorIOF($this->convertToFloat($linha->substr(63, 13)))
->setValorPago($this->convertToFloat($linha->substr(78, 13)))
->setValorLiquido($this->convertToFloat($linha->substr(93, 13)));
$dadosTitulo->setAcrescimos($this->convertToFloat($linha->substr(18, 15)))
->setValorDesconto($this->convertToFloat($linha->substr(33, 15)))
->setValorAbatimento($this->convertToFloat($linha->substr(48, 15)))
->setValorIOF($this->convertToFloat($linha->substr(63, 15)))
->setValorPago($this->convertToFloat($linha->substr(78, 15)))
->setValorLiquido($this->convertToFloat($linha->substr(93, 15)));

$detail->setDadosTitulo($dadosTitulo)
->setOutrasDespesas($this->convertToFloat($linha->substr(108, 13)))
->setOutrosCreditos($this->convertToFloat($linha->substr(123, 13)))
->setOutrasDespesas($this->convertToFloat($linha->substr(108, 15)))
->setOutrosCreditos($this->convertToFloat($linha->substr(123, 15)))
->setDataOcorrencia($this->createDate($linha->substr(138, 8), "dmY"))
->setDataCredito($this->createDate($linha->substr(146, 8), "dmY"));

$ocorrencia->setCod(($this->convertToInt($linha->substr(154, 4))))
->setData($this->createDate($linha->substr(158, 8)))
->setValor($this->convertToFloat($linha->substr(166, 13)))
->setValor($this->convertToFloat($linha->substr(166, 15)))
->setComplemento($linha->substr(181, 30));

$banco->setCod($linha->substr(211, 3));
Expand Down
4 changes: 2 additions & 2 deletions tests/Resources/ret/240/retorno_cnab240.ret
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
00100000 2026599530001760032948600014 0032860000000075367 SAPATARIA E CONFECSOES JIBA BANCO DO BRASIL 22103201102475000025703000000909214318PROCESSAMEN
00100011T0100020 20026599530001760032948600014 0032860000000075367 SAPATARIA E CONFECSOES JIBA 000000002103201100000000 000000000
0010001300001T 060032860000000075367 32948600000000196 1 23032011000000000004000237009880 0900000000000000000000000000000000000000000000000000000 000000000000000000000050000 180273294860
0010001300002U 060000000000000000000000000000000000000000000000000000000000000000000000040000000000000035000000000000000000000000000000002103201123032011 000000000000000 000
0010001300001T 060032860000000075367 32948600000000196 1 23032011000000000004285237009880 0900000000000000000000000000000000000000000000000000000 000000000000000000000050000 180273294860
0010001300002U 060000000000325000000000000000000000000000000000000000000000000000000000041250000000000035000000000000000000000000000000002103201123032011 000000000000000 000
0010001300003T 060032860000000075367 32948600000000236 1 25032011000000000003000033045680 0900000000000000000000000000000000000000000000000000000 000000000000000000000050000 180273294860
0010001300004U 060000000000000000000000000000000000000000000000000000000000000000000000030000000000000025000000000000000000000000000000002103201123032011 000000000000000 000
0010001300005T 060032860000000075367 32948600000000244 1 23032011000000000001500104004900 0900000000000000000000000000000000000000000000000000000 000000000000000000000050000 180273294860
Expand Down
39 changes: 39 additions & 0 deletions tests/SegmentosCNAB240/AbstractSegmentoTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Umbrella\Tests\Ya\RetornoBoleto\SegmentosCNAB240;

use Umbrella\Tests\Ya\RetornoBoleto\RetornoCNAB240Test;
use Umbrella\Ya\RetornoBoleto\ProcessFactory;
use Umbrella\Ya\RetornoBoleto\ProcessHandler;

abstract class AbstractSegmentoTestCase extends RetornoCNAB240Test
{
public function segmentoProvider($segmento)
{
$segmentos = array();
foreach ($this->cnabProvider() as $fileName) {
$cnab = ProcessFactory::getRetorno(current($fileName));

$processor = new ProcessHandler($cnab);
$retorno = $processor->processar();

$lotes = $retorno->getLotes();
foreach ($lotes->get(0)->getDetails() as $detail) {
if ($detail->getSegmento() == $segmento) {
$segmentos[] = array($detail);
}
}
}
return $segmentos;
}

public function segmentoTProvider()
{
return $this->segmentoProvider('T');
}

public function segmentoUProvider()
{
return $this->segmentoProvider('U');
}
}
16 changes: 16 additions & 0 deletions tests/SegmentosCNAB240/SegmentoTTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Umbrella\Tests\Ya\RetornoBoleto\SegmentosCNAB240;

class SegmentoTTest extends AbstractSegmentoTestCase
{
/**
* @dataProvider segmentoTProvider
* @param $fileName
*/
public function testMonetaryValues($segmentoTDetail)
{
$this->assertEquals('T', $segmentoTDetail->getSegmento());
$this->assertTrue(is_double($segmentoTDetail->getValorTarifa()));
}
}
16 changes: 16 additions & 0 deletions tests/SegmentosCNAB240/SegmentoUTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Umbrella\Tests\Ya\RetornoBoleto\SegmentosCNAB240;

class SegmentoUTest extends AbstractSegmentoTestCase
{
/**
* @dataProvider segmentoUProvider
* @param $fileName
*/
public function testMonetaryValues($segmentoUDetail)
{
$this->assertEquals('U', $segmentoUDetail->getSegmento());
$this->assertTrue(is_double($segmentoUDetail->getDadosTitulo()->getValorPago()));
}
}

0 comments on commit be78b8f

Please sign in to comment.