Repository navigation
Expand file tree
/
Copy pathUChaveNFe.pas
More file actions
208 lines (177 loc) · 6.06 KB
/
Copy pathUChaveNFe.pas
File metadata and controls
208 lines (177 loc) · 6.06 KB
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
unit UChaveNFe;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ExtCtrls, DB, DBClient, Mask, DBCtrls, AtkDA,
rxToolEdit, rxCurrEdit, CellEditors;
type
TFChaveNfe = class(TForm)
Panel1: TPanel;
Label1: TLabel;
Panel2: TPanel;
Label9: TLabel;
Label8: TLabel;
Label7: TLabel;
Label6: TLabel;
Label5: TLabel;
Label4: TLabel;
Label3: TLabel;
Label2: TLabel;
BtnQuebraChave: TBitBtn;
BtnCalcDV: TBitBtn;
BtnGeraChaveNFe: TBitBtn;
CDSDados: TClientDataSet;
CDSDadosCodEstado: TStringField;
CDSDadosAnoMes: TStringField;
CDSDadosCNPJ: TStringField;
CDSDadosModeloNFe: TStringField;
CDSDadosSerieNFe: TStringField;
CDSDadosNumeroNFe: TStringField;
CDSDadosCodNFe: TStringField;
CDSDadosDV: TStringField;
DsDados: TDataSource;
CDSDadosChaveNFe: TStringField;
DBCBxUfs: TDBComboBoxValues;
EdtChaveNfe: TDBEdit;
EdtDV: TDBEdit;
EdtCodNfe: TDBEdit;
EdtNumDocto: TDBEdit;
EdtSerieNfe: TDBEdit;
EdtModDocto: TDBEdit;
EdtCNPJ: TDBEdit;
EdtMesAno: TDBEdit;
CDSDadosTPIMP: TStringField;
DBeTpImp: TDBEdit;
Label10: TLabel;
procedure BtnQuebraChaveClick(Sender: TObject);
procedure BtnCalcDVClick(Sender: TObject);
procedure BtnGeraChaveNFeClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure EdtSerieNfeExit(Sender: TObject);
procedure EdtNumDoctoExit(Sender: TObject);
procedure EdtCodNfeExit(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure DBeTpImpExit(Sender: TObject);
private
// procedure SetEditNumbers(HEdit: THandle);
private
{ Private declarations }
public
{ Public declarations }
end;
var
FChaveNfe: TFChaveNfe;
implementation
uses UPrincipal, UFunctions;
{$R *.dfm}
procedure TFChaveNfe.BtnQuebraChaveClick(Sender: TObject);
var ChaveNfe:String;
begin
ChaveNfe := CDSDadosChaveNfe.AsString;
if (Length(ChaveNfe) <> 44) or (ChaveNfe[44] <> CalcMod11(Copy(ChaveNfe,0,43))) then
begin
ShowMessage('Chave de Acesso invalida');
exit;
end;
if not (CDSDados.State in [dsinsert,dsedit]) then
CDSDados.Edit;
CDSDadosCodEstado.AsString := copy(ChaveNfe,0,2);
CDSDadosAnoMes.AsString := Copy(ChaveNfe,3,4);
CDSDadosCNPJ.AsString := Copy(ChaveNfe,7,14);
CDSDadosModeloNFe.AsString := Copy(ChaveNfe,21,2);
CDSDadosSerieNFe.AsString := Copy(ChaveNfe,23,3);
CDSDadosNumeroNFe.AsString := Copy(ChaveNfe,26,9);
CDSDadosTPIMP.AsString := Copy(ChaveNfe,35,1);
CDSDadosCodNFe.AsString := Copy(ChaveNfe,36,8);
CDSDadosDV.AsString := Copy(ChaveNfe,44,1);
end;
procedure TFChaveNfe.BtnCalcDVClick(Sender: TObject);
begin
if Length(CDSDadosChaveNfe.AsString) = 44 then
begin
ShowMessage('Chave de acesso ja possui 44 digitos');
exit;
end;
if Length(CDSDadosChaveNfe.AsString) <> 43 then
begin
ShowMessage('É necessario que a chave possua 43 digitos para calcular o dv');
exit;
end;
if not (CDSDados.State in [dsinsert,dsedit]) then
CDSDados.Edit;
CDSDadosChaveNfe.AsString := CDSDadosChaveNfe.AsString + CalcMod11(CDSDadosChaveNfe.AsString);
end;
procedure TFChaveNfe.BtnGeraChaveNFeClick(Sender: TObject);
var DV: String;
chave:String;
begin
if (CDSDadosCodEstado.AsString = '') or (CDSDadosAnoMes.AsString = '') or (CDSDadosCNPJ.AsString = '') or (CDSDadosModeloNFe.AsString = '') or (CDSDadosSerieNFe.AsString = '') or
(CDSDadosNumeroNFe.AsString = '') or (CDSDadosCodNFe.AsString = '') or (CDSDadosTPIMP.AsString = '') then
begin
ShowMessage('É necessario informar os campos acima antes, o unico que pode ficar vazio é o DV');
end;
chave := CDSDadosCodEstado.AsString + CDSDadosAnoMes.AsString + CDSDadosCNPJ.AsString + CDSDadosModeloNFe.AsString + CDSDadosSerieNFe.AsString + CDSDadosNumeroNFe.AsString +CDSDadosTPIMP.AsString+ CDSDadosCodNFe.AsString;
DV := CalcMod11 (chave);
if not (CDSDados.State in [dsinsert,dsedit]) then
CDSDados.Edit;
if CDSDadosDV.AsString <> '' then
begin
if CDSDadosDV.AsString <> DV then
begin
if messagedlg('DV Informado invalido, deseja recalcular ?',mtCustom,[mbYes,mbNo], 0) = mrYes then
CDSDadosDV.AsString := DV
else
exit;
end;
end
else
CDSDadosDV.AsString := DV;
CDSDadosChaveNfe.AsString := chave + DV;
end;
procedure TFChaveNfe.FormShow(Sender: TObject);
begin
if not (CDSDados.Active and (CDSDados.State in [dsinsert,dsedit])) then
CDSDados.Append;
end;
procedure TFChaveNfe.FormClose(Sender: TObject; var Action: TCloseAction);
begin
TFPrincipal(owner).FChaveNfe := nil;
TFPrincipal(owner).FChaveNfe.Free;
self.Release;
end;
procedure TFChaveNfe.EdtSerieNfeExit(Sender: TObject);
begin
CDSDadosSerieNFe.AsString := FormatFloat('000', CDSDadosSerieNFe.AsFloat);
end;
procedure TFChaveNfe.EdtNumDoctoExit(Sender: TObject);
begin
CDSDadosNumeroNFe.AsString := FormatFloat('000000000', CDSDadosNumeroNFe.AsFloat);
end;
procedure TFChaveNfe.EdtCodNfeExit(Sender: TObject);
begin
CDSDadosCodNFe.AsString := FormatFloat('000000000',CDSDadosCodNFe.AsFloat);
end;
{procedure TFChaveNfe.SetEditNumbers(HEdit: THandle);
var fstyle: DWord;
begin
fstyle := GetWindowLong(HEdit,GWL_STYLE);
SetWindowLong(HEdit, GWL_STYLE, fstyle or ES_NUMBER);
end;}
procedure TFChaveNfe.FormCreate(Sender: TObject);
begin
SetEditNumbers(EdtChaveNfe.Handle);
SetEditNumbers(EdtMesAno.Handle);
SetEditNumbers(EdtCNPJ.Handle);
SetEditNumbers(EdtModDocto.Handle);
SetEditNumbers(EdtSerieNfe.Handle);
SetEditNumbers(EdtNumDocto.Handle);
SetEditNumbers(EdtCodNfe.Handle);
SetEditNumbers(EdtDV.Handle);
SetEditNumbers(DBeTpImp.Handle);
end;
procedure TFChaveNfe.DBeTpImpExit(Sender: TObject);
begin
CDSDadosTPIMP.AsString := FormatFloat('0', CDSDadosTPIMP.AsFloat);
end;
end.