|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Umbrella\Ya\RetornoBoleto\Cnab\Cnab400\Convenio\Processor; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use Stringy\Stringy; |
|
7
|
|
|
use Umbrella\Ya\RetornoBoleto\AbstractProcessor; |
|
8
|
|
|
use Umbrella\Ya\RetornoBoleto\Cnab\Cnab400\Detail; |
|
9
|
|
|
use Umbrella\Ya\RetornoBoleto\Cnab\Cnab400\Header; |
|
10
|
|
|
use Umbrella\Ya\RetornoBoleto\Cnab\Cnab400\ITrailer; |
|
11
|
|
|
use Umbrella\Ya\RetornoBoleto\Cnab\Cnab400\Trailer; |
|
12
|
|
|
use Umbrella\Ya\RetornoBoleto\Cnab\ComposableInterface; |
|
13
|
|
|
use Umbrella\Ya\RetornoBoleto\Exception\EmptyLineException; |
|
14
|
|
|
use Umbrella\Ya\RetornoBoleto\Exception\InvalidPositionException; |
|
15
|
|
|
use Umbrella\Ya\RetornoBoleto\LoteInterface; |
|
16
|
|
|
use Umbrella\Ya\RetornoBoleto\Model\Banco; |
|
17
|
|
|
use Umbrella\Ya\RetornoBoleto\Model\Cedente; |
|
18
|
|
|
use Umbrella\Ya\RetornoBoleto\Model\Cobranca; |
|
19
|
|
|
use Umbrella\Ya\RetornoBoleto\RetornoInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Classe abstrata para leitura de arquivos de retorno de cobranças no padrão CNAB400/CBR643.<br/> |
|
23
|
|
|
* Layout Padrão CNAB/Febraban 400 posições<br/>. |
|
24
|
|
|
* Baseado na documentação para "Layout de Arquivo Retorno para Convênios |
|
25
|
|
|
* na faixa numérica entre 000.001 a 999.999 (Convênios de até 6 posições). Versão Set/09" e |
|
26
|
|
|
* "Layout de Arquivo Retorno para convênios na faixa numérica entre 1.000.000 a 9.999.999 |
|
27
|
|
|
* (Convênios de 7 posições). Versão Set/09" do Banco do Brasil |
|
28
|
|
|
* (arquivos Doc8826BR643Pos6.pdf e Doc2628CBR643Pos7.pdf) |
|
29
|
|
|
* @author Ítalo Lelis de Vietro <[email protected]> |
|
30
|
|
|
*/ |
|
31
|
|
|
abstract class AbstractCNAB400Processor extends AbstractProcessor |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @property int HEADER_ARQUIVO Define o valor que identifica uma coluna do tipo HEADER DE ARQUIVO |
|
35
|
|
|
*/ |
|
36
|
|
|
const HEADER_ARQUIVO = 0; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @property int TRAILER_ARQUIVO Define o valor que identifica uma coluna do tipo TRAILER DE ARQUIVO |
|
40
|
|
|
*/ |
|
41
|
|
|
const TRAILER_ARQUIVO = 9; |
|
42
|
|
|
|
|
43
|
|
|
public function createHeader() |
|
44
|
|
|
{ |
|
45
|
|
|
return new Header(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function createDetail() |
|
49
|
|
|
{ |
|
50
|
|
|
return new Detail(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function createTrailer() |
|
54
|
|
|
{ |
|
55
|
|
|
return new Trailer(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Processa a linha header do arquivo |
|
60
|
|
|
* @param string $linha Linha do header de arquivo processado |
|
61
|
|
|
* @return string Retorna um vetor contendo os dados dos campos do header do arquivo. |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function processarHeaderArquivo($linha) |
|
64
|
|
|
{ |
|
65
|
|
|
$header = $this->createHeader(); |
|
66
|
|
|
//X = ALFANUMÉRICO 9 = NUMÉRICO V = VÍRGULA DECIMAL ASSUMIDA |
|
67
|
|
|
$header->setRegistro($linha->substr(1, 1)->trim()); |
|
|
|
|
|
|
68
|
|
|
$header->setTipoOperacao($linha->substr(2, 1)->trim()); |
|
|
|
|
|
|
69
|
|
|
$header->setIdTipoOperacao($linha->substr(3, 7)->trim()); |
|
|
|
|
|
|
70
|
|
|
$header->setIdTipoServico($linha->substr(10, 2)->trim()); |
|
|
|
|
|
|
71
|
|
|
$header->setTipoServico($linha->substr(12, 8)->trim()); |
|
|
|
|
|
|
72
|
|
|
$header->addComplemento($linha->substr(20, 7)->trim()); |
|
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
$bancoArray = array(); |
|
75
|
|
|
if (!preg_match('#^([\d]{3})(.+)#', $linha->substr(77, 18)->trim(), $bancoArray)) { |
|
|
|
|
|
|
76
|
|
|
throw new InvalidArgumentException('Banco invalido'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$banco = new Banco(); |
|
80
|
|
|
$banco |
|
81
|
|
|
->setCod($bancoArray[1]) |
|
82
|
|
|
->setNome($bancoArray[2]) |
|
83
|
|
|
->setAgencia($linha->substr(27, 4)->trim()) |
|
|
|
|
|
|
84
|
|
|
->setDvAgencia($linha->substr(31, 1)->trim()) |
|
|
|
|
|
|
85
|
|
|
->setConta($linha->substr(32, 8)->trim()) |
|
|
|
|
|
|
86
|
|
|
->setDvConta($linha->substr(40, 1)->trim()); |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
$cedente = new Cedente(); |
|
90
|
|
|
$cedente->setBanco($banco) |
|
91
|
|
|
->setNome($linha->substr(47, 30)->trim()); |
|
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
$header->setCedente($cedente); |
|
94
|
|
|
$header->setDataGravacao($this->createDate($linha->substr(95, 6)->trim())); |
|
|
|
|
|
|
95
|
|
|
$header->setSequencialReg($linha->substr(395, 6)->trim()); |
|
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
return $header; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Processa uma linha detalhe do arquivo. |
|
102
|
|
|
* @param string $linha Linha detalhe do arquivo processado |
|
103
|
|
|
* @return string Retorna um vetor contendo os dados dos campos da linha detalhe. |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function processarDetalhe($linha) |
|
106
|
|
|
{ |
|
107
|
|
|
$detail = $this->createDetail(); |
|
108
|
|
|
|
|
109
|
|
|
$bancoEmissor = new Banco(); |
|
110
|
|
|
$bancoEmissor |
|
111
|
|
|
->setAgencia($linha->substr(22, 1)->trim()) |
|
|
|
|
|
|
112
|
|
|
->setDvAgencia($linha->substr(31, 1)->trim()) |
|
|
|
|
|
|
113
|
|
|
->setConta($linha->substr(23, 8)->trim()) |
|
|
|
|
|
|
114
|
|
|
->setDvConta($linha->substr(31, 1)->trim()); |
|
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
$bancoRecebedor = new Banco(); |
|
117
|
|
|
$bancoRecebedor |
|
118
|
|
|
->setCod($linha->substr(166, 3)->trim()) |
|
|
|
|
|
|
119
|
|
|
->setAgencia($linha->substr(169, 4)->trim()) |
|
|
|
|
|
|
120
|
|
|
->setDvAgencia($linha->substr(173, 1)->trim()); |
|
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
$detail |
|
123
|
|
|
->setBancoEmissor($bancoEmissor) |
|
124
|
|
|
->setBancoRecebedor($bancoRecebedor) |
|
125
|
|
|
->setRegistro($linha->substr(1, 1)->trim()) |
|
|
|
|
|
|
126
|
|
|
->setTaxaDesconto($this->formataNumero($linha->substr(96, 5)->trim())) |
|
|
|
|
|
|
127
|
|
|
->setTaxaIof($linha->substr(101, 5)->trim()) |
|
|
|
|
|
|
128
|
|
|
->setCateira($linha->substr(107, 2)->trim()) |
|
|
|
|
|
|
129
|
|
|
->setComando($linha->substr(109, 2)->trim()) |
|
|
|
|
|
|
130
|
|
|
->setDataOcorrencia($this->createDate($linha->substr(111, 6)->trim())) |
|
|
|
|
|
|
131
|
|
|
->setNumTitulo($linha->substr(117, 10)->trim()) |
|
|
|
|
|
|
132
|
|
|
->setDataVencimento($this->createDate($linha->substr(147, 6)->trim())) |
|
|
|
|
|
|
133
|
|
|
->setValor($this->formataNumero($linha->substr(153, 13)->trim())) |
|
|
|
|
|
|
134
|
|
|
->setEspecie($linha->substr(174, 2)->trim()) |
|
|
|
|
|
|
135
|
|
|
->setDataCredito($this->createDate($linha->substr(176, 6)->trim())) |
|
|
|
|
|
|
136
|
|
|
->setValorTarifa($this->formataNumero($linha->substr(182, 7)->trim())) |
|
|
|
|
|
|
137
|
|
|
->setOutrasDespesas($this->formataNumero($linha->substr(189, 13)->trim())) |
|
|
|
|
|
|
138
|
|
|
->setJurosDesconto($this->formataNumero($linha->substr(202, 13)->trim())) |
|
|
|
|
|
|
139
|
|
|
->setIofDesconto($this->formataNumero($linha->substr(215, 13)->trim())) |
|
|
|
|
|
|
140
|
|
|
->setValorAbatimento($this->formataNumero($linha->substr(228, 13)->trim())) |
|
|
|
|
|
|
141
|
|
|
->setDescontoConcedido($this->formataNumero($linha->substr(241, 13)->trim())) |
|
|
|
|
|
|
142
|
|
|
->setValorRecebido($this->formataNumero($linha->substr(254, 13)->trim())) |
|
|
|
|
|
|
143
|
|
|
->setJurosMora($this->formataNumero($linha->substr(267, 13)->trim())) |
|
|
|
|
|
|
144
|
|
|
->setOutrosRecebimentos($this->formataNumero($linha->substr(280, 13)->trim())) |
|
|
|
|
|
|
145
|
|
|
->setAbatimentoNaoAprovado($this->formataNumero($linha->substr(293, 13)->trim())) |
|
|
|
|
|
|
146
|
|
|
->setValorLancamento($this->formataNumero($linha->substr(306, 13)->trim())) |
|
|
|
|
|
|
147
|
|
|
->setIndicativoDc($linha->substr(319, 1)->trim()) |
|
|
|
|
|
|
148
|
|
|
->setIndicadorValor($linha->substr(320, 1)->trim()) |
|
|
|
|
|
|
149
|
|
|
->setValorAjuste($this->formataNumero($linha->substr(321, 12)->trim())) |
|
|
|
|
|
|
150
|
|
|
->setCanalPagTitulo($linha->substr(393, 2)->trim()) |
|
|
|
|
|
|
151
|
|
|
->setSequencial($linha->substr(395, 6)->trim()); |
|
|
|
|
|
|
152
|
|
|
|
|
153
|
|
|
return $detail; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Processa a linha trailer do arquivo. |
|
158
|
|
|
* @param string $linha Linha trailer do arquivo processado |
|
159
|
|
|
* @return ITrailer Retorna um vetor contendo os dados dos campos da linha trailer do arquivo. |
|
160
|
|
|
*/ |
|
161
|
|
|
protected function processarTrailerArquivo($linha) |
|
162
|
|
|
{ |
|
163
|
|
|
$trailer = $this->createTrailer(); |
|
164
|
|
|
|
|
165
|
|
|
$banco = new Banco(); |
|
166
|
|
|
$banco->setCod($linha->substr(5, 3)->trim()); |
|
|
|
|
|
|
167
|
|
|
|
|
168
|
|
|
$simples = new Cobranca(); |
|
169
|
|
|
$simples->setQtdTitulos($linha->substr(18, 8)->trim()) |
|
|
|
|
|
|
170
|
|
|
->setValorTotal($this->formataNumero($linha->substr(26, 14)->trim())) |
|
|
|
|
|
|
171
|
|
|
->setNumAviso($linha->substr(40, 8)->trim()); |
|
|
|
|
|
|
172
|
|
|
|
|
173
|
|
|
$vinculada = new Cobranca(); |
|
174
|
|
|
$vinculada->setQtdTitulos($linha->substr(58, 8)->trim()) |
|
|
|
|
|
|
175
|
|
|
->setValorTotal($this->formataNumero($linha->substr(66, 14)->trim())) |
|
|
|
|
|
|
176
|
|
|
->setNumAviso($linha->substr(80, 8)->trim()); |
|
|
|
|
|
|
177
|
|
|
|
|
178
|
|
|
$caucionada = new Cobranca(); |
|
179
|
|
|
$caucionada->setQtdTitulos($linha->substr(98, 8)->trim()) |
|
|
|
|
|
|
180
|
|
|
->setValorTotal($this->formataNumero($linha->substr(106, 14)->trim())) |
|
|
|
|
|
|
181
|
|
|
->setNumAviso($linha->substr(120, 8)->trim()); |
|
|
|
|
|
|
182
|
|
|
|
|
183
|
|
|
$descontada = new Cobranca(); |
|
184
|
|
|
$descontada->setQtdTitulos($linha->substr(138, 8)->trim()) |
|
|
|
|
|
|
185
|
|
|
->setValorTotal($this->formataNumero($linha->substr(146, 14)->trim())) |
|
|
|
|
|
|
186
|
|
|
->setNumAviso($linha->substr(160, 8)->trim()); |
|
|
|
|
|
|
187
|
|
|
|
|
188
|
|
|
$vendor = new Cobranca(); |
|
189
|
|
|
$vendor->setQtdTitulos($linha->substr(218, 8)->trim()) |
|
|
|
|
|
|
190
|
|
|
->setValorTotal($this->formataNumero($linha->substr(226, 14)->trim())) |
|
|
|
|
|
|
191
|
|
|
->setNumAviso($linha->substr(240, 8)->trim()); |
|
|
|
|
|
|
192
|
|
|
|
|
193
|
|
|
$trailer |
|
194
|
|
|
->setBanco($banco) |
|
195
|
|
|
->setRegistro($linha->substr(1, 1)->trim()) |
|
|
|
|
|
|
196
|
|
|
->setRetorno($linha->substr(2, 1)->trim()) |
|
|
|
|
|
|
197
|
|
|
->setTipoRegistro($linha->substr(3, 2)->trim()) |
|
|
|
|
|
|
198
|
|
|
->setSimples($simples) |
|
199
|
|
|
->setVinculada($vinculada) |
|
200
|
|
|
->setCaucionada($caucionada) |
|
201
|
|
|
->setDescontada($descontada) |
|
202
|
|
|
->setVendor($vendor) |
|
203
|
|
|
->setSequencial($linha->substr(395, 6)->trim()); |
|
|
|
|
|
|
204
|
|
|
return $trailer; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
public function processCnab(RetornoInterface $retorno, ComposableInterface $composable, LoteInterface $lote = null) |
|
208
|
|
|
{ |
|
209
|
|
|
switch ((int)$composable->getRegistro()->__toString()) { |
|
210
|
|
|
case self::HEADER_ARQUIVO: |
|
211
|
|
|
$retorno->setHeader($composable); |
|
|
|
|
|
|
212
|
|
|
break; |
|
213
|
|
|
|
|
214
|
|
|
case self::TRAILER_ARQUIVO: |
|
215
|
|
|
$retorno->setTrailer($composable); |
|
|
|
|
|
|
216
|
|
|
break; |
|
217
|
|
|
|
|
218
|
|
|
default: |
|
219
|
|
|
$lote->addDetail($composable); |
|
|
|
|
|
|
220
|
|
|
break; |
|
221
|
|
|
} |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Processa uma linha do arquivo de retorno. |
|
226
|
|
|
* @param int $numLn Número_linha a ser processada |
|
227
|
|
|
* @param string $linha String contendo a linha a ser processada |
|
228
|
|
|
* @return IComposable Retorna um vetor associativo contendo os valores_linha processada. |
|
229
|
|
|
*/ |
|
230
|
|
|
public function processarLinha($numLn, Stringy $linha) |
|
231
|
|
|
{ |
|
232
|
|
|
$tamLinha = 400; //total de caracteres das linhas do arquivo |
|
233
|
|
|
//o +2 é utilizado para contar o \r\n no final da linha |
|
234
|
|
|
if (strlen($linha) != $tamLinha && strlen($linha) != $tamLinha + 2) { |
|
235
|
|
|
throw new InvalidPositionException("A linha $numLn não tem $tamLinha posições. Possui " . strlen($linha)); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
if (trim($linha) == "") { |
|
239
|
|
|
throw new EmptyLineException("A linha $numLn está vazia."); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
//é adicionado um espaço vazio no início_linha para que |
|
243
|
|
|
//possamos trabalhar com índices iniciando_1, no lugar_zero, |
|
244
|
|
|
//e assim, ter os valores_posição_campos exatamente |
|
245
|
|
|
//como no manual CNAB400 |
|
246
|
|
|
$linha = $linha->insert(" ", 0); |
|
247
|
|
|
$tipoLn = $linha->substr(1, 1)->trim(); |
|
248
|
|
|
|
|
249
|
|
|
$this->needToCreateLote = false; |
|
250
|
|
|
if ((string)$tipoLn == static::HEADER_ARQUIVO) { |
|
251
|
|
|
$this->needToCreateLote = true; |
|
252
|
|
|
$vlinha = $this->processarHeaderArquivo($linha); |
|
253
|
|
|
} elseif ((string)$tipoLn == static::DETALHE) { |
|
254
|
|
|
$vlinha = $this->processarDetalhe($linha); |
|
255
|
|
|
} elseif ((string)$tipoLn == static::TRAILER_ARQUIVO) { |
|
256
|
|
|
$vlinha = $this->processarTrailerArquivo($linha); |
|
257
|
|
|
} else { |
|
258
|
|
|
$vlinha = null; |
|
259
|
|
|
} |
|
260
|
|
|
return $vlinha; |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.