diff --git a/examples/examples.php b/examples/examples.php index 45ee313..74e8d0b 100644 --- a/examples/examples.php +++ b/examples/examples.php @@ -67,8 +67,7 @@ $cnab = new CNABPagamento(\Ewersonfc\CNABPagamento\Bancos::SAFRA); //$tipoRetorno = 'confirmacao_rejeicao'; //$data = $cnab->processarRetorno('/home/ewerson/Downloads/RETORNO_701EHTLRONL0042119206.txt', $tipoRetorno); -$tipoRetorno = 'liquidacao'; -$data = $cnab->processarRetorno('/home/ewerson/Downloads/RETORNO_701EHTLRONL0042123031.txt', $tipoRetorno); +$data = $cnab->processarRetorno('/home/ewerson/Downloads/RETORNO_701EHTLRONL0042119194.txt'); echo '
'; print_r($data); diff --git a/src/Bancos.php b/src/Bancos.php index 6599884..f68ccab 100644 --- a/src/Bancos.php +++ b/src/Bancos.php @@ -7,6 +7,7 @@ */ namespace Ewersonfc\CNABPagamento; +use Ewersonfc\CNABPagamento\Constants\TipoRetorno; use Ewersonfc\CNABPagamento\Exceptions\CNABPagamentoException; /** * Class Bancos @@ -38,4 +39,17 @@ public static function getBankData(int $banco) break; } } + + public static function getSafraDetailType($type) + { + switch ($type) + { + case 'C': + return TipoRetorno::CONFIRMACAO_REJEICAO; + case 'L': + return TipoRetorno::LIQUIDACAO; + default: + return TipoRetorno::CONFIRMACAO_REJEICAO; + } + } } \ No newline at end of file diff --git a/src/CNABPagamento.php b/src/CNABPagamento.php index c06bb12..d57e631 100644 --- a/src/CNABPagamento.php +++ b/src/CNABPagamento.php @@ -56,18 +56,13 @@ public function gerarArquivo(DataFile $dataFile) /** * @param $archivePath - * @throws \Exception + * @param $tipoRetorno + * @return Factories\DataFile + * @throws FileRetornoException */ - public function processarRetorno($archivePath, $tipoRetorno) + public function processarRetorno($archivePath) { - if(!in_array($tipoRetorno, [ - TipoRetorno::CONFIRMACAO_REJEICAO, - TipoRetorno::LIQUIDACAO, - TipoRetorno::DDA, - ])) { - throw new FileRetornoException("Tipo de retorno definido não existe."); - }; - $data = $this->serviceRetorno->readFile($archivePath, $tipoRetorno); + $data = $this->serviceRetorno->readFile($archivePath); return $data; } } \ No newline at end of file diff --git a/src/Factories/RetornoFactory.php b/src/Factories/RetornoFactory.php index 45c4b29..b86b4c8 100644 --- a/src/Factories/RetornoFactory.php +++ b/src/Factories/RetornoFactory.php @@ -9,6 +9,7 @@ namespace Ewersonfc\CNABPagamento\Factories; +use Ewersonfc\CNABPagamento\Responses\FileResponse; use Ewersonfc\CNABPagamento\Entities\DataFile; class RetornoFactory @@ -35,15 +36,51 @@ function __construct($header, $detail) $this->detail = $detail; } + private function makeRejeicao($dataRejeicao) + { + return str_split($dataRejeicao, 3); + } + /** * @return DataFile */ - public function generateResponse() + public function generateSafraResponse() { - $dataFile = new DataFile(); - $dataFile->header = $this->header; - $dataFile->detail = $this->detail; + $response = []; + foreach($this->detail as $detail) { + $approved = false; + $fileResponse = new FileResponse; + + if(($detail['codigo_operacao']['value'] == 'C' AND trim($detail['codigo_rejeicao']['value']) == null) OR $detail['codigo_operacao']['value'] == 'L') + $approved = true; - return $dataFile; + + $fileResponse->aprovado = $approved; + $fileResponse->tipoAprovacao = $detail['codigo_operacao']['value']; + if(!$approved) { + $fileResponse->rejeicao = $this->makeRejeicao($detail['codigo_motivo_rejeicao']['value']); + } + $fileResponse->registro = $detail['codigo_registro']['value']; + $fileResponse->inscricao = $detail['codigo_registro']['value']; + $fileResponse->agencia = $detail['codigo_agencia']['value']; + $fileResponse->fornecedor = $detail['codigo_fornecedor']['value']; + $fileResponse->documento = $detail['tipo_documento']['value']; + $fileResponse->compromisso = $detail['numero_compromisso']['value']; + $fileResponse->dataPagamento = $detail['data_pagamento']['value']; + $fileResponse->dataVencimento = $detail['data_vencimento']['value']; + $fileResponse->operacao = $detail['codigo_operacao']['value']; + $fileResponse->ocorrencia = $detail['codigo_ocorrencia']['value']; + $fileResponse->dataOcorrencia = $detail['data_ocorrencia']['value']; + $fileResponse->numeroCompromisso = $detail['negociacao_compromisso']['value']; + $fileResponse->valorCompromisso = $detail['valor_compromisso']['value']; + $fileResponse->agenciaDestino = $detail['agencia_destino']['value']; + $fileResponse->contaDestino = $detail['conta_destino']['value']; + + $response[] = $fileResponse; + } + print_r($response); + exit(); } -} \ No newline at end of file +} + + diff --git a/src/Responses/FileResponse.php b/src/Responses/FileResponse.php new file mode 100644 index 0000000..d2a24d9 --- /dev/null +++ b/src/Responses/FileResponse.php @@ -0,0 +1,193 @@ +aprovado; + } + + /** + * @return mixed + */ + public function getTipoAprovacao() + { + return $this->tipoAprovacao; + } + + /** + * @return mixed + */ + public function getRejeicao() + { + return $this->rejeicao; + } + + /** + * @return mixed + */ + public function getRegistro() + { + return $this->registro; + } + + /** + * @return mixed + */ + public function getInscricao() + { + return $this->inscricao; + } + + /** + * @return mixed + */ + public function getAgencia() + { + return $this->agencia; + } + + /** + * @return mixed + */ + public function getFornecedor() + { + return $this->fornecedor; + } + + /** + * @return mixed + */ + public function getDocumento() + { + return $this->documento; + } + + /** + * @return mixed + */ + public function getCompromisso() + { + return $this->compromisso; + } + + /** + * @return mixed + */ + public function getDataPagamento() + { + return $this->dataPagamento; + } + + /** + * @return mixed + */ + public function getDataVencimento() + { + return $this->dataVencimento; + } + + /** + * @return mixed + */ + public function getOperacao() + { + return $this->operacao; + } + + /** + * @return mixed + */ + public function getOcorrencia() + { + return $this->ocorrencia; + } + + /** + * @return mixed + */ + public function getDataOcorrencia() + { + return $this->dataOcorrencia; + } + + /** + * @return mixed + */ + public function getNegociacaoCompromisso() + { + return $this->negociacaoCompromisso; + } + + /** + * @return mixed + */ + public function getValorCompromisso() + { + return $this->valorCompromisso; + } + + /** + * @return mixed + */ + public function getAgenciaDestino() + { + return $this->agenciaDestino; + } + + /** + * @return mixed + */ + public function getContaDestino() + { + return $this->contaDestino; + } + +} \ No newline at end of file diff --git a/src/Services/ServiceRetorno.php b/src/Services/ServiceRetorno.php index 6ae5431..f460f2f 100644 --- a/src/Services/ServiceRetorno.php +++ b/src/Services/ServiceRetorno.php @@ -8,6 +8,7 @@ namespace Ewersonfc\CNABPagamento\Services; +use Ewersonfc\CNABPagamento\Bancos; use Ewersonfc\CNABPagamento\Exceptions\FileRetornoException; use Ewersonfc\CNABPagamento\Factories\RetornoFactory; use Ewersonfc\CNABPagamento\Format\Yaml; @@ -59,7 +60,8 @@ private function readHeaderYml() private function readDetailYml($tipoRetorno) { - return $this->yaml->readDetail($tipoRetorno); + $tipoArquivo = Bancos::getSafraDetailType($tipoRetorno); + return $this->yaml->readDetail($tipoArquivo); } /** @@ -76,8 +78,6 @@ private function readFileData() $this->dataFile = $data; } - - private function makefield($string, $field) { $fieldPosition = $field['pos'][0]-1; @@ -102,14 +102,24 @@ private function matchHeaderFileAndHeaderData() return $headerComplete; } + private function getTypeReturnByBank($detailCompletely) + { + switch ($this->banco['codigo_banco']) + { + case Bancos::SAFRA: + return $this->readDetailYml(substr($detailCompletely, 108, 108)); + break; + default: + throw new \Exception("Não foi possivel toma danada"); + } + } - private function matchDetailFileAndDetailData($tipoRetorno) + private function matchDetailFileAndDetailData() { $onlyDetails = array_slice($this->dataFile, 1, count(array_filter($this->dataFile)) - 2); - $detailYml = $this->readDetailYml($tipoRetorno); - $detailComplete = []; foreach ($onlyDetails as $keyDetail => $detail) { + $detailYml = $this->getTypeReturnByBank($detail); foreach ($detailYml as $key => $field) { $detailComplete[$keyDetail][$key] = $this->makefield($detail, $field); } @@ -123,14 +133,17 @@ private function matchDetailFileAndDetailData($tipoRetorno) * @param $tipoRetorno * @return \Ewersonfc\CNABPagamento\Factories\DataFile */ - final public function readFile($filePath, $tipoRetorno) + final public function readFile($filePath) { $this->filePath = $filePath; $header = $this->matchHeaderFileAndHeaderData(); - $detail = $this->matchDetailFileAndDetailData($tipoRetorno); + $detail = $this->matchDetailFileAndDetailData(); $retornoFactory = new RetornoFactory($header, $detail); - return $retornoFactory->generateResponse(); + if($this->banco = Bancos::SAFRA) + return $retornoFactory->generateSafraResponse(); + + return false; } } \ No newline at end of file