-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuProductInfo.pas
182 lines (155 loc) · 4.5 KB
/
uProductInfo.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
unit uProductInfo;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, FireDAC.Comp.Client, FireDAC.Stan.Param,
Vcl.Mask, Vcl.Buttons;
type
TfProductInfo = class(TForm)
Panel1: TPanel;
btn_save: TButton;
btn_close: TButton;
Label1: TLabel;
edit_name: TEdit;
edit_price: TEdit;
Label2: TLabel;
Label3: TLabel;
edit_stock: TEdit;
Label4: TLabel;
edit_id: TEdit;
btn_openStockControl: TSpeedButton;
procedure btn_closeClick(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure btn_saveClick(Sender: TObject);
procedure edit_nameExit(Sender: TObject);
procedure edit_priceChange(Sender: TObject);
procedure edit_priceKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure edit_priceKeyPress(Sender: TObject; var Key: Char);
private
{ Private declarations }
ComponentePro: TComponent;
procedure changeEditColor(Sender: TObject); //change edit color when it is focused
function fieldValidation: Boolean;
public
{ Public declarations }
end;
var
fProductInfo: TfProductInfo;
fAux1, fAux2: String;
implementation
{$R *.dfm}
uses uVariables, uDatabaseRelatedFunctions, uDM, uSystemRelatedFunctions;
procedure TfProductInfo.btn_saveClick(Sender: TObject);
var
vm_sql: String;
begin
//if fieldValidation then
//begin
if isConnected then
begin
try
if prod_isNew then
vm_sql := 'INSERT INTO products (name, price, stock) VALUES (:p_name, :p_price, :p_stock);'
else
vm_sql := 'UPDATE products SET (name = :p_name, price = :p_price, stock = :p_stock WHERE id = :p_id);';
db_table := TFDQuery.Create(Self);
db_table.Connection := DM.FDConnection1;
db_table.SQL.Add(vm_sql);
//db_table.ParamByName('p_id').AsInteger := StrToInt(edit_id.Text);
db_table.ParamByName('p_name').AsString := edit_name.Text;
db_table.ParamByName('p_price').Value := StrToFloat(edit_price.Text);
db_table.ParamByName('p_stock').Value := StrToFloat(edit_stock.Text);
try
DM.FDConnection1.StartTransaction;
db_table.ExecSQL;
DM.FDConnection1.Commit;
except
DM.FDConnection1.Rollback;
end;
finally
FreeAndNil(db_table);
end;
end;
DM.FDConnection1.Connected := False;
//end;
end;
procedure TfProductInfo.btn_closeClick(Sender: TObject);
begin
Close;
end;
procedure TfProductInfo.changeEditColor(Sender: TObject);
Var
color: TColor;
begin
color := clWindow;
if Assigned(ComponentePro) then
TEdit(ComponentePro).Color := color; //color when leaving
if (ActiveControl is TCustomEdit) or (ActiveControl is TComboBox) or (ActiveControl is TMemo) then
begin
TEdit(ActiveControl).Color := $FFFFE0; //color when focusing
ComponentePro := ActiveControl;
end;
end;
procedure TfProductInfo.edit_nameExit(Sender: TObject);
begin
edit_name.Color := clWindow;
end;
procedure TfProductInfo.edit_priceChange(Sender: TObject);
begin
fAux2 := fAux1;
edit_price.text := currencyMagic(fAux2, Length(fAux2));
end;
procedure TfProductInfo.edit_priceKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if key = vk_delete then
begin
edit_price.clear;
fAux1 := '';
end;
end;
procedure TfProductInfo.edit_priceKeyPress(Sender: TObject; var Key: Char);
begin
fAux1 := fAux1 + key;
end;
function TfProductInfo.fieldValidation: Boolean;
var
i: Integer;
begin
for I := 0 to ComponentCount - 1 do
begin
if Components[I].ClassType = TEdit then
if TEdit(Components[I]).Text = '' then
begin
Result := true;
//TEdit(Components[I]).TextHint := 'Campo Obrigatório';
TEdit(Components[I]).SetFocus;
Exit;
end;
end;
end;
procedure TfProductInfo.FormActivate(Sender: TObject);
begin
Screen.OnActiveControlChange := changeEditColor;
end;
procedure TfProductInfo.FormCreate(Sender: TObject);
begin
if prod_isNew then
begin
Caption := 'Products > New Product';
edit_id.Text := Format('%.*d', [4, getNextFreedId('products')]);
end
else
begin
Caption := 'Products > Editing Product';
end;
edit_name.Color := $FFFFE0;
end;
procedure TfProductInfo.FormDestroy(Sender: TObject);
begin
Screen.OnActiveControlChange := nil;
end;
end.