-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathuLancarContas.pas
499 lines (430 loc) · 18.3 KB
/
uLancarContas.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
unit uLancarContas;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Graphics, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls,
uCadastros, FMX.Objects, FMX.Edit, FMX.Controls.Presentation, FMX.Layouts,
Data.DB, FMX.Effects, FMX.TabControl, FMX.ListBox, System.Actions,
FMX.ActnList, FMX.DateTimeCtrls, FMX.ComboEdit;
type
TfLancarContas = class(TfCadastros)
btCancelar: TSpeedButton;
edtID: TEdit;
TabControl1: TTabControl;
tbDadosGerais: TTabItem;
tbDadosAdicionais: TTabItem;
edtCodigoBarra: TEdit;
edtParcela: TEdit;
edtDocumento: TEdit;
edtNome: TEdit;
ShadowEffect2: TShadowEffect;
edtAnotacoes: TEdit;
actContatos: TActionList;
tbDadosGeraisChange: TChangeTabAction;
tbDadosAdicionaisChange: TChangeTabAction;
btnLancar: TSpeedButton;
ShadowEffect6: TShadowEffect;
Rectangle10: TRectangle;
Layout1: TLayout;
lblLancar: TLabel;
Layout2: TLayout;
Label2: TLabel;
Rectangle6: TRectangle;
ListBox1: TListBox;
StyleBook1: TStyleBook;
edtValorConta: TEdit;
edtEmissao: TDateEdit;
edtVencimento: TDateEdit;
cbxCategoria: TComboEdit;
cbxRecorrencia: TComboBox;
Layout3: TLayout;
Rectangle1: TRectangle;
procedure btnLancarClick(Sender: TObject);
procedure edtDocumentoChangeTracking(Sender: TObject);
procedure edtIDChangeTracking(Sender: TObject);
procedure EditEnter(Sender: TObject);
procedure EditExit(Sender: TObject);
procedure ListBoxItem1Click(Sender: TObject);
procedure ListBoxItem2Click(Sender: TObject);
procedure Rectangle10Click(Sender: TObject);
procedure retMovimentosClick(Sender: TObject);
procedure TabControl1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Single);
procedure edtValorContaKeyDown(Sender: TObject; var Key: Word;
var KeyChar: Char; Shift: TShiftState);
procedure btCancelarClick(Sender: TObject);
procedure TestarContas;
procedure CarregaCategorias;
function ArredondaParcela(total: real; parc, num_parcelas: integer): real;
private
{ Private declarations }
public
{ Public declarations }
iIDAtendimento :Integer;
lbl :TLabel;
end;
var
fLancarContas: TfLancarContas;
implementation
{$R *.fmx}
uses dPrincipal, uPrincipal, uFuncoes, uBuscaAvancada, uContas;
{ TfCadastrosContatos }
procedure TfLancarContas.edtDocumentoChangeTracking(Sender: TObject);
begin
inherited;
{ lbl := TLabel((Sender as TEdit).FindComponent('Label'+(Sender as TEdit).Name));
edtDocumento.StyledSettings := [];
if (edtDocumento.Text <> '') and (Length(edtDocumento.Text) <> 11) and (Length(edtDocumento.Text) <> 14) then begin
lbl.Text := 'Informe um CPF/CNPJ válido';
edtDocumento.TextSettings.Font.Style := [TFontStyle.fsBold];
edtDocumento.TextSettings.FontColor := TAlphaColors.Red;
end;
if (Length(edtDocumento.Text) = 11) then begin
if not ValidaCPF(edtDocumento.Text) then begin
edtDocumento.StyledSettings := [];
edtDocumento.TextSettings.Font.Style := [TFontStyle.fsBold];
edtDocumento.TextSettings.FontColor := TAlphaColors.Red;
lbl.Text := 'Informe um CPF válido';
// Abort;
end
else begin
edtDocumento.TextSettings.Font.Style := [];
edtDocumento.TextSettings.FontColor := AppColor;
lbl.Text := 'CPF';
end;
end;
if (Length(edtDocumento.Text) = 14) then begin
if not ValidaCNPJ(edtDocumento.Text) then begin
edtDocumento.TextSettings.Font.Style := [TFontStyle.fsBold];
edtDocumento.TextSettings.FontColor := TAlphaColors.Red;
lbl.Text := 'Informe um CNPJ válido';
end else begin
edtDocumento.TextSettings.Font.Style := [];
edtDocumento.TextSettings.FontColor := AppColor;
lbl.Text := 'CNPJ';
end;
end;
if (edtDocumento.Text = '') then lbl.Text := 'Documento';}
end;
procedure TfLancarContas.edtIDChangeTracking(Sender: TObject);
var I :Integer;
begin
inherited;
TabControl1.TabPosition := TTabPosition.None;
for I := 0 to ComponentCount -1 do begin
if (Components[I] is TEdit)then begin
TEdit(Components[I] as TEdit).OnEnter := EditEnter;
TEdit(Components[I] as TEdit).OnExit := EditExit;
TEdit(Components[I] as TEdit).StyledSettings := [];
TEdit(Components[I] as TEdit).TextSettings.Font.Size:= 14;
TEdit(Components[I] as TEdit).Caption(TEdit(Components[I] as TEdit).Hint);
end else if (Components[I] is TComboEdit)then begin
TComboEdit(Components[I] as TComboEdit).OnEnter := EditEnter;
TComboEdit(Components[I] as TComboEdit).OnExit := EditExit;
// TComboBox(Components[I] as TComboBox).StyledSettings := [];
// TComboBox(Components[I] as TComboBox).TextSettings.Font.Size:= 14;
TComboEdit(Components[I] as TComboEdit).Caption(TComboEdit(Components[I] as TComboEdit).Hint);
end else if (Components[I] is TComboBox)then begin
TComboBox(Components[I] as TComboBox).OnEnter := EditEnter;
TComboBox(Components[I] as TComboBox).OnExit := EditExit;
// TComboBox(Components[I] as TComboBox).StyledSettings := [];
// TComboBox(Components[I] as TComboBox).TextSettings.Font.Size:= 14;
TComboBox(Components[I] as TComboBox).Caption(TComboBox(Components[I] as TComboBox).Hint);
end else if (Components[I] is TDateEdit)then begin
TDateEdit(Components[I] as TDateEdit).OnEnter := EditEnter;
TDateEdit(Components[I] as TDateEdit).OnExit := EditExit;
TDateEdit(Components[I] as TDateEdit).StyledSettings := [];
TDateEdit(Components[I] as TDateEdit).TextSettings.Font.Size:= 14;
TDateEdit(Components[I] as TDateEdit).Caption(TEdit(Components[I] as TDateEdit).Hint);
end;
end;
CarregaCategorias;
end;
procedure TfLancarContas.edtValorContaKeyDown(Sender: TObject; var Key: Word;
var KeyChar: Char; Shift: TShiftState);
var
Texto, Texto2: string;
i: byte;
begin
inherited;
if (KeyChar in ['0'..'9',chr(vkback)]) then begin
// limito a 23 caracteres senão haverá um erro na função StrToInt64()
if (KeyChar in ['0'..'9']) and (Length(Trim(TEdit(Sender).Text))>23) then
KeyChar := #0;
// pego somente os caracteres de 0 a 9, ignorando a pontuação
Texto2 := '0';
Texto := Trim(TEdit(Sender).Text)+KeyChar;
for i := 1 to Length(Texto) do
if Texto[i] in ['0'..'9'] then
Texto2 := Texto2 + Texto[i];
// se foi pressionado BACKSPACE (única tecla válida, fora os números)
// apago o último caractere da string
if KeyChar = chr(vkback) then
Delete(Texto2,Length(Texto2),1);
// formato o texto que depois será colocado no Edit
Texto2 := FormatFloat('#,0.00',StrToInt64(Texto2)/100);
TEdit(Sender).Text := Texto2;
TEdit(Sender).SelStart := Length(Texto2);
end;
KeyChar := #0;
end;
procedure TfLancarContas.ListBoxItem1Click(Sender: TObject);
begin
inherited;
TListBoxItem(Sender).Free;
end;
procedure TfLancarContas.ListBoxItem2Click(Sender: TObject);
begin
inherited;
tbDadosAdicionaisChange.Execute;
end;
procedure TfLancarContas.Rectangle10Click(Sender: TObject);
begin
inherited;
fPrincipal.tbChangeMovimentos.Execute;
end;
procedure TfLancarContas.retMovimentosClick(Sender: TObject);
begin
inherited;
fPrincipal.tbChangeMovimentos.Execute;
end;
procedure TfLancarContas.btnLancarClick(Sender: TObject);
var Conta : TContas;
i,iChave,iParc :Integer;
sRecorrencia,sTipo :string;
dData :TDate;
lbl :TLabel;
sMsg :TStringBuilder;
rValor :Real;
begin
inherited;
if trim(edtValorConta.Text) = '' then begin
lbl := TLabel((edtValorConta).FindComponent('Label'+(edtValorConta).Name));
lbl.TextSettings.FontColor := TAlphaColors.Red;
edtValorConta.SetFocus;
Abort;
end else if strtofloat(StringReplace(edtValorConta.Text,'.','',[rfReplaceAll])) < 1 then begin
lbl := TLabel((edtValorConta).FindComponent('Label'+(edtValorConta).Name));
lbl.TextSettings.FontColor := TAlphaColors.Red;
edtValorConta.SetFocus;
Abort;
end else begin
lbl := TLabel((edtValorConta).FindComponent('Label'+(edtValorConta).Name));
lbl.TextSettings.FontColor := TAlphaColors.Steelblue;
end;
if trim(cbxCategoria.Text) = '' then begin
lbl := TLabel((cbxCategoria).FindComponent('Label'+(cbxCategoria).Name));
lbl.TextSettings.FontColor := TAlphaColors.Red;
cbxCategoria.SetFocus;
Abort;
end else begin
lbl := TLabel((cbxCategoria).FindComponent('Label'+(cbxCategoria).Name));
lbl.TextSettings.FontColor := TAlphaColors.Steelblue;
end;
if edtParcela.Text = '' then begin
edtParcela.SetFocus;
edtParcela.Text := '1';
end;
if (Trim(cbxCategoria.Text) <> '')and (cbxCategoria.ItemIndex = -1) then begin
dmPrincipal.QuerySelect.Open('SELECT ID,Descricao FROM contas_categorias WHERE Descricao ='+QuotedStr(Trim(cbxCategoria.Text)));
if dmPrincipal.QuerySelect.RecordCount = 0 then begin
dmPrincipal.sqliteConnection.ExecSQL('INSERT INTO contas_categorias '+
' (Descricao) VALUES ('+QuotedStr(Trim(cbxCategoria.Text))+')');
CarregaCategorias;
end;
end;
iChave := StrToInt(ChavePrimaria('contas'));
if lblLancar.Text = 'Lançar conta à receber' then
sTipo := 'R'
else
sTipo := 'P';
iParc := StrtoInt(edtParcela.Text);
rValor := strtofloat(StringReplace(edtValorConta.Text,'.','',[rfReplaceAll]));
Conta := TContas.Create;
sMsg := TStringBuilder.Create;
for I := 0 to IParc-1 do begin
if (cbxRecorrencia.ItemIndex = 0) or (cbxRecorrencia.ItemIndex = -1) then begin
sRecorrencia := 'M';
dData := IncMonth(edtVencimento.Date,+I);
end else if cbxRecorrencia.ItemIndex = 1 then begin
sRecorrencia := 'S';
dData := IncMonth(edtVencimento.Date,+(I*12));
end else if cbxRecorrencia.ItemIndex = 2 then begin
sRecorrencia := 'A';
dData := edtVencimento.Date+(I*7);
end;
Conta.Categoria := cbxCategoria.Text;
Conta.IDContato := edtNome.Tag;
Conta.Parcela := InttoStr(I+1)+'/'+InttoStr(IParc);
Conta.TipoCobranca := sRecorrencia;
Conta.NumeroDocumento := edtDocumento.Text;
Conta.CodigoBarra := edtCodigoBarra.Text;
Conta.ValorConta := ArredondaParcela(rValor,I, IParc);
Conta.DataLiquidacao := 0;
Conta.DataEmissao := edtVencimento.Date;
Conta.DataVencimento := dData;
Conta.CodigoChave := iChave;
Conta.TipoConta := sTipo;
Conta.Observacoes := edtAnotacoes.Text;
Conta.IDAtendimento := iIDAtendimento;
sMsg.Append(FormatDateTime('ddmmyyyy',dData)+#13);
Conta.Inserir;
// ShowMessage(inttostr(Conta.IDCategoria));
// Showmessage(datetostr(dData)+ ' '+
// Decimal(
// FloatToStr(
// ArredondaParcela(NumberBox1.value,
// ArredondaParcela(StrtoFloat(Desformata(edtValorConta.Text))/100,
// I, IParc))));
end;
Notificacoes('','Contas lançadas com sucesso!',edtNome.Text+#13+sMsg.ToString);
end;
procedure TfLancarContas.TabControl1MouseMove(Sender: TObject;
Shift: TShiftState; X, Y: Single);
begin
inherited;
ListBox1.Visible := False;
end;
procedure TfLancarContas.TestarContas;
begin
{if (edtRecorrencia.ItemIndex = -1) then begin
MostrarHintComboEdit(edtRecorrencia,'Qual a recorrência?');
Abort;
end;
if StrToFloatDef(Trim( Desformata(edtValorTotal.Text)),0) <= 0 then begin
MostrarHint(edtValorTotal,'Qual valor?');
Abort;
end;
Refresh := False;
CodigoConta := '';
if (cbxContas.ItemIndex <> -1) then begin
CodigoConta := (InttoStr(integer(cbxContas.Items.Objects[cbxContas.ItemIndex])));
end else begin
if Trim(cbxContas.Text) = '' then begin
MostrarHintComboEdit(cbxContas,'Qual a conta?');
Abort;
end else begin
retAvisos.Visible := False;
mPrincipal.QuerySelect.Open('SELECT Codigo,Descricao FROM contas_desc WHERE Descricao ='+QuotedStr(Trim(cbxContas.Text)));
if mPrincipal.QuerySelect.RecordCount = 0 then begin
CodigoConta := ChavePrimaria('contas_desc');
mPrincipal.sqliteConnection.ExecSQL('INSERT INTO contas_desc '+
' (Usuario,Descricao) VALUES ('+
InttoStr(fPrincipal.btnUsuario.Tag)+','+QuotedStr(Trim(cbxContas.Text))+')');
Refresh := True;
end else CodigoConta := mPrincipal.QuerySelect.Fields[0].AsString;
end;
end;
retAvisos.Visible := False; }
end;
function TfLancarContas.ArredondaParcela(total: real; parc,
num_parcelas: integer): real;
var
valor, n_centavos, total_parcelado, total_final: real;
parcelas: array[0..100] of real;
i: integer;
begin
total_final := 0;
valor := Round((total / num_parcelas)*100)/100;
total_parcelado := valor * num_parcelas;
n_centavos := 0;
if total_parcelado <> total then
n_centavos := round((total_parcelado - total) * 100);
for i:=0 to num_parcelas-1 do begin
parcelas[i] := valor;
if n_centavos > 0 then begin
parcelas[i] := parcelas[i] - 1/100;
n_centavos := n_centavos - 1;
end
else if n_centavos < 0 then begin
parcelas[i] := parcelas[i] + 1/100;
n_centavos := n_centavos + 1;
end;
if i = parc then
result := parcelas[parc];
total_final := total_final + parcelas[i];
end;
end;
procedure TfLancarContas.btCancelarClick(Sender: TObject);
begin
inherited;
fPrincipal.Rectangle6.Visible := False;
fPrincipal.Rectangle7.Visible := False;
Close;
end;
procedure TfLancarContas.CarregaCategorias;
begin
dmPrincipal.tabTarefas.Open('SELECT ID,Descricao FROM contas_categorias');
cbxCategoria.items.Clear;
while not dmPrincipal.tabTarefas.Eof do begin
cbxCategoria.Items.AddObject(dmPrincipal.tabTarefas.Fields[1].AsString , Tobject(Integer(dmPrincipal.tabTarefas.Fields[0].AsInteger)));
dmPrincipal.tabTarefas.Next;
end;
end;
procedure TfLancarContas.EditEnter(Sender: TObject);
var runlbl :TLabel;
begin
if (Sender is TEdit) then begin
if (Sender as TEdit).Text = '' then begin
runlbl := TLabel((Sender as TEdit).FindComponent('Label'+(Sender as TEdit).Name));
runlbl.AnimateFloat('Font.Size',9,0.1, TAnimationType.InOut,TInterpolationType.Linear);
runlbl.AnimateFloat('Position.Y',-11,0.1, TAnimationType.InOut,TInterpolationType.Linear);
runlbl.FontColor := AppColor;
end;
end else if (Sender is TComboEdit) then begin
if (Sender as TComboEdit).Text = '' then begin
runlbl := TLabel((Sender as TComboEdit).FindComponent('Label'+(Sender as TComboEdit).Name));
runlbl.AnimateFloat('Font.Size',9,0.1, TAnimationType.InOut,TInterpolationType.Linear);
runlbl.AnimateFloat('Position.Y',-11,0.1, TAnimationType.InOut,TInterpolationType.Linear);
runlbl.FontColor := AppColor;
end;
end else if (Sender is TComboBox) then begin
if (Sender as TComboBox).ItemIndex = -1 then begin
runlbl := TLabel((Sender as TComboBox).FindComponent('Label'+(Sender as TComboBox).Name));
runlbl.AnimateFloat('Font.Size',9,0.1, TAnimationType.InOut,TInterpolationType.Linear);
runlbl.AnimateFloat('Position.Y',-11,0.1, TAnimationType.InOut,TInterpolationType.Linear);
runlbl.FontColor := AppColor;
end;
end else if (Sender is TDateEdit) then begin
if (Sender as TDateEdit).Text = '' then begin
runlbl := TLabel((Sender as TDateEdit).FindComponent('Label'+(Sender as TDateEdit).Name));
runlbl.AnimateFloat('Font.Size',9,0.1, TAnimationType.InOut,TInterpolationType.Linear);
runlbl.AnimateFloat('Position.Y',-11,0.1, TAnimationType.InOut,TInterpolationType.Linear);
runlbl.FontColor := AppColor;
end;
end;
end;
procedure TfLancarContas.EditExit(Sender: TObject);
var runlbl :TLabel;
begin
if (Sender is TEdit) then begin
if (Sender as TEdit).Text = '' then begin
runlbl := TLabel((Sender as TEdit).FindComponent('Label'+(Sender as TEdit).Name));
runlbl.AnimateFloat('Font.Size',12,0.1, TAnimationType.InOut,TInterpolationType.Linear);
runlbl.AnimateFloat('Position.Y',-2,0.1, TAnimationType.InOut,TInterpolationType.Linear);
runlbl.FontColor := AppColor;
end;
end else if (Sender is TComboEdit) then begin
if (Sender as TComboEdit).Text = '' then begin
runlbl := TLabel((Sender as TComboEdit).FindComponent('Label'+(Sender as TComboEdit).Name));
runlbl.AnimateFloat('Font.Size',12,0.1, TAnimationType.InOut,TInterpolationType.Linear);
runlbl.AnimateFloat('Position.Y',-2,0.1, TAnimationType.InOut,TInterpolationType.Linear);
runlbl.FontColor := AppColor;
end;
end else if (Sender is TComboBox) then begin
if (Sender as TComboBox).ItemIndex = -1 then begin
runlbl := TLabel((Sender as TComboBox).FindComponent('Label'+(Sender as TComboBox).Name));
runlbl.AnimateFloat('Font.Size',12,0.1, TAnimationType.InOut,TInterpolationType.Linear);
runlbl.AnimateFloat('Position.Y',-2,0.1, TAnimationType.InOut,TInterpolationType.Linear);
runlbl.FontColor := AppColor;
end;
end else if (Sender is TDateEdit) then begin
if (Sender as TDateEdit).Text = '' then begin
runlbl := TLabel((Sender as TDateEdit).FindComponent('Label'+(Sender as TDateEdit).Name));
runlbl.AnimateFloat('Font.Size',12,0.1, TAnimationType.InOut,TInterpolationType.Linear);
runlbl.AnimateFloat('Position.Y',-2,0.1, TAnimationType.InOut,TInterpolationType.Linear);
runlbl.FontColor := AppColor;
end;
end;
end;
end.