-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCNAB240.pas
241 lines (208 loc) · 7.22 KB
/
CNAB240.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
{* Unit CNAB240.pas
*
* Criação: Ricardo Nastás Acras ([email protected])
* Data: 15/10/2005
*
* Objetivo: Tratar arquivos CNAB 240 posições.
}
unit CNAB240;
interface
uses
Classes, SysUtils, DBClient, DB;
type
TTipoArquivo = (taRemessa, taRetorno);
TCNAB240 = class
private
FDataHoraGeracao: TDateTime;
FSequencialArquivo: integer;
FTipoArquivo: TTipoArquivo;
FClientDataSetTitulos: TClientDataSet;
protected
public
constructor create;
destructor destroy;
property dataGeracao: TDateTime read FDataHoraGeracao write FDataHoraGeracao;
property sequencialArquivo: integer read FSequencialArquivo write FSequencialArquivo;
property tipoArquivo: TTipoArquivo read FTipoArquivo write FTipoArquivo;
property dataSet: TClientDataSet read FClientDataSetTitulos;
procedure abrirArquivo(sNomeArquivo: string);
end;
implementation
uses TypInfo, DateUtils;
{ TCNAB240 }
procedure TCNAB240.abrirArquivo(sNomeArquivo: string);
var
conteudo: TStringList;
sHeader, sSegmentoT, sSegmentoU: string;
iLinhaAtual: integer;
begin
conteudo := TStringList.Create;
try
conteudo.LoadFromFile(sNomeArquivo);
//Interpretar a primeira linha do arquivo
//A primeira linha do arquivo é o Header de Arquivo
//Adicionados apenas campos com uso prático detectado, caso necessite
//mais campos basta consultar a documentação do CNAB40 e adiciona-los
sHeader := conteudo[0];
if copy(sHeader, 143,1) = '1' then
FTipoArquivo := taRemessa
else
FTipoArquivo := taRetorno;
FDataHoraGeracao := EncodeDateTime(
StrToInt(copy(sHeader,148,4)),
StrToInt(copy(sHeader,146,2)),
StrToInt(copy(sHeader,144,2)),
StrToInt(copy(sHeader,152,2)),
StrToInt(copy(sHeader,154,2)),
StrToInt(copy(sHeader,156,2)),
0
);
FSequencialArquivo := StrToInt(copy(sHeader, 158, 6));
//pulando o header do lote por que não foi encontrado uso prático para
//o arquivo de retorno, caso encontre pode adicionar aqui o código de
//interpretação desta linha
//ler os segmentos T's e U's
//Cada par de segmentos T's e U's correspondem a um título
iLinhaAtual := 2;
while iLinhaAtual < conteudo.Count do
begin
//se é segmento T deverá conter um segmento T e um segmento U
if copy(conteudo[iLinhaAtual], 14, 1) = 'T' then
begin
sSegmentoT := conteudo[iLinhaAtual];
sSegmentoU := conteudo[iLinhaAtual+1];
iLinhaAtual := iLinhaAtual + 2;
FClientDataSetTitulos.Append;
FClientDataSetTitulos.FieldByName('NossoNumero').AsString :=
trim(copy(sSegmentoT, 106, 25));
FClientDataSetTitulos.FieldByName('ValorTarifa').AsFloat :=
StrToFloat(copy(sSegmentoT, 199, 13)+','+copy(sSegmentoT, 212, 2));
FClientDataSetTitulos.FieldByName('ValorJuros').AsFloat :=
StrToFloat(copy(sSegmentoU, 18, 13)+','+copy(sSegmentoU, 31, 2));
FClientDataSetTitulos.FieldByName('ValorDesconto').AsFloat :=
StrToFloat(copy(sSegmentoU, 33, 13)+','+copy(sSegmentoU, 46, 2));
FClientDataSetTitulos.FieldByName('ValorAbatimento').AsFloat :=
StrToFloat(copy(sSegmentoU, 48, 13)+','+copy(sSegmentoU, 61, 2));
FClientDataSetTitulos.FieldByName('ValorIOF').AsFloat :=
StrToFloat(copy(sSegmentoU, 63, 13)+','+copy(sSegmentoU, 76, 2));
FClientDataSetTitulos.FieldByName('ValorPago').AsFloat :=
StrToFloat(copy(sSegmentoU, 78, 13)+','+copy(sSegmentoU, 91, 2));
FClientDataSetTitulos.FieldByName('ValorLiquido').AsFloat :=
StrToFloat(copy(sSegmentoU, 93, 13)+','+copy(sSegmentoU, 106, 2));
FClientDataSetTitulos.FieldByName('ValorOutrasDespesas').AsFloat :=
StrToFloat(copy(sSegmentoU, 108, 13)+','+copy(sSegmentoU, 121, 2));
FClientDataSetTitulos.FieldByName('ValorOutrosCreditos').AsFloat :=
StrToFloat(copy(sSegmentoU, 123, 13)+','+copy(sSegmentoU, 136, 2));
FClientDataSetTitulos.FieldByName('DataOcorrencia').AsDateTime :=
EncodeDate(
StrToInt(copy(sSegmentoU,142,4)),
StrToInt(copy(sSegmentoU,140,2)),
StrToInt(copy(sSegmentoU,138,2))
);
FClientDataSetTitulos.FieldByName('DataCredito').AsDateTime :=
EncodeDate(
StrToInt(copy(sSegmentoU,150,4)),
StrToInt(copy(sSegmentoU,148,2)),
StrToInt(copy(sSegmentoU,146,2))
);
FClientDataSetTitulos.FieldByName('DataTarifa').AsDateTime :=
EncodeDate(
StrToInt(copy(sSegmentoU,158,4)),
StrToInt(copy(sSegmentoU,156,2)),
StrToInt(copy(sSegmentoU,154,2))
);
FClientDataSetTitulos.Post;
end
else
inc(iLinhaAtual);
end;
finally
FreeAndNil(conteudo);
end;
end;
constructor TCNAB240.create;
begin
inherited;
FClientDataSetTitulos := TClientDataSet.Create(nil);
//criar os campos para o ClientDataSet
with TStringField.create(FClientDataSetTitulos) do
begin
FieldName := 'NossoNumero';
DataSet := FClientDataSetTitulos;
end;
with TFloatField.Create(FClientDataSetTitulos) do
begin
FieldName := 'ValorTarifa';
currency := true;
DataSet := FClientDataSetTitulos;
end;
with TFloatField.Create(FClientDataSetTitulos) do
begin
FieldName := 'ValorJuros';
currency := true;
DataSet := FClientDataSetTitulos;
end;
with TFloatField.Create(FClientDataSetTitulos) do
begin
FieldName := 'ValorDesconto';
currency := true;
DataSet := FClientDataSetTitulos;
end;
with TFloatField.Create(FClientDataSetTitulos) do
begin
FieldName := 'ValorAbatimento';
currency := true;
DataSet := FClientDataSetTitulos;
end;
with TFloatField.Create(FClientDataSetTitulos) do
begin
FieldName := 'ValorIOF';
currency := true;
DataSet := FClientDataSetTitulos;
end;
with TFloatField.Create(FClientDataSetTitulos) do
begin
FieldName := 'ValorPago';
currency := true;
DataSet := FClientDataSetTitulos;
end;
with TFloatField.Create(FClientDataSetTitulos) do
begin
FieldName := 'ValorLiquido';
currency := true;
DataSet := FClientDataSetTitulos;
end;
with TFloatField.Create(FClientDataSetTitulos) do
begin
FieldName := 'ValorOutrasDespesas';
currency := true;
DataSet := FClientDataSetTitulos;
end;
with TFloatField.Create(FClientDataSetTitulos) do
begin
FieldName := 'ValorOutrosCreditos';
currency := true;
DataSet := FClientDataSetTitulos;
end;
with TDateTimeField.Create(FClientDataSetTitulos) do
begin
FieldName := 'DataOcorrencia';
DataSet := FClientDataSetTitulos;
end;
with TDateTimeField.Create(FClientDataSetTitulos) do
begin
FieldName := 'DataCredito';
DataSet := FClientDataSetTitulos;
end;
with TDateTimeField.Create(FClientDataSetTitulos) do
begin
FieldName := 'DataTarifa';
DataSet := FClientDataSetTitulos;
end;
FClientDataSetTitulos.CreateDataSet;
end;
destructor TCNAB240.destroy;
begin
FClientDataSetTitulos.Free;
end;
end.