-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dlgDCodePicker.pas
290 lines (263 loc) · 7.77 KB
/
dlgDCodePicker.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
unit dlgDCodePicker;
{
23.08.31 : Routine works with Entrant and Team DataSets.
Normally I would create an Object list with the DataSet.
Instead I've used TListView.Items.SubItems to hold the DisqualifyCodeID
as a String params.
The caller must assign a valid fEntrantID or fTeamID value,
prior to executing TDCodePicker.ShowModal.
}
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, FireDAC.Stan.Intf, FireDAC.Stan.Option,
FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf,
FireDAC.DApt.Intf, FireDAC.Stan.Async, FireDAC.DApt, Data.DB,
FireDAC.Comp.DataSet, FireDAC.Comp.Client, dmSCM, Vcl.StdCtrls, Vcl.ExtCtrls,
Vcl.ComCtrls;
type
TDCodePicker = class(TForm)
qryDisqualifyCode: TFDQuery;
btnOk: TButton;
btnCancel: TButton;
btnClearCodeExit: TButton;
Panel1: TPanel;
qryStroke: TFDQuery;
lvCodes: TListView;
procedure btnCancelClick(Sender: TObject);
procedure btnClearCodeExitClick(Sender: TObject);
procedure btnOkClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure FormShow(Sender: TObject);
procedure lvCodesDblClick(Sender: TObject);
private
{ Private declarations }
fConnection: TFDConnection;
fEntrantID: integer;
fTeamID: integer;
fDoINDV: boolean;
fIsScratchedDCode: integer;
fIsDisqualifiedDCode: integer;
function AssertConnection: boolean;
public
{ Public declarations }
constructor CreateWithConnection(AOwner: TComponent;
aConnection: TFDConnection);
property EntrantID: integer read fEntrantID write fEntrantID;
property TeamID: integer read fTeamID write fTeamID;
end;
var
DCodePicker: TDCodePicker;
implementation
{$R *.dfm}
{ TDisqualifyCode }
constructor TDCodePicker.CreateWithConnection(AOwner: TComponent;
aConnection: TFDConnection);
begin
inherited Create(AOwner);
// INIT
fConnection := aConnection;
fEntrantID := 0;
fTeamID := 0;
if Assigned(fConnection) and fConnection.Connected then
BEGIN
qryStroke.Connection := fConnection;
qryDisqualifyCode.Connection := fConnection;
END;
end;
procedure TDCodePicker.btnCancelClick(Sender: TObject);
begin
ModalResult := mrCancel;
end;
procedure TDCodePicker.btnClearCodeExitClick(Sender: TObject);
var
SQL: string;
begin
if fDoINDV then
begin
// remove disqualification code and exit
SQL := 'UPDATE SwimClubMeet.dbo.Entrant SET' +
' [DisqualifyCodeID] = NULL, [IsScratched] = 0,' +
' [IsDisqualified] = 0 WHERE [Entrant].EntrantID = :ID1;';
fConnection.ExecSQL(SQL, [fEntrantID]);
end
else
begin
SQL := 'UPDATE SwimClubMeet.dbo.Team SET' +
' [DisqualifyCodeID] = NULL, [IsScratched] = 0,' +
' [IsDisqualified] = 0 WHERE [Team].TeamID = :ID1;';
fConnection.ExecSQL(SQL, [fTeamID]);
end;
ModalResult := mrOk;
end;
procedure TDCodePicker.btnOkClick(Sender: TObject);
var
SQL: String;
CodeID: integer;
IsScratched, IsDisqualified: boolean;
lvItem: TListItem;
begin
lvItem := lvCodes.Selected;
if Assigned(lvItem) then
begin
// "Simplified Disqualification Schema" :DEFAULT INIT
// SCMb - Unspecified disqualification.
IsScratched := false;
IsDisqualified := true;
// disqualification code ID
CodeID := StrToIntDef(lvItem.SubItems[1], 0);
// "Simplified Disqualification Schema"
// SCMa - Swimmer didn't show for event. Scratched.
if (CodeID = fIsScratchedDCode) then
begin
IsScratched := true;
IsDisqualified := false;
end;
if fDoINDV then
begin
SQL := 'UPDATE SwimClubMeet.dbo.Entrant SET' +
' [DisqualifyCodeID] = :ID1, [IsScratched] = :ID2,' +
' [IsDisqualified] = :ID3 WHERE [Entrant].EntrantID = :ID4;';
fConnection.ExecSQL(SQL, [CodeID, IsScratched, IsDisqualified,
fEntrantID], [ftInteger, ftBoolean, ftBoolean, ftInteger]);
end
else
begin
SQL := 'UPDATE SwimClubMeet.dbo.Team SET' +
' [DisqualifyCodeID] = :ID1, [IsScratched] = :ID2,' +
' [IsDisqualified] = :ID3 WHERE [Team].TeamID = :ID4;';
fConnection.ExecSQL(SQL, [CodeID, IsScratched, IsDisqualified, fTeamID],
[ftInteger, ftBoolean, ftBoolean, ftInteger]);
end;
ModalResult := mrOk;
end;
end;
procedure TDCodePicker.FormCreate(Sender: TObject);
begin
// Clear out the DesignTime doodles...
lvCodes.Items.Clear;
end;
procedure TDCodePicker.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key = VK_ESCAPE then
ModalResult := mrCancel;
end;
function TDCodePicker.AssertConnection: boolean;
begin
result := false;
if Assigned(SCM) then
begin
// IsActive if TFDConnection::scmConnection && FireDAC tables are active
if SCM.SCMActive then result := true;
end;
end;
procedure TDCodePicker.FormShow(Sender: TObject);
var
lv: TListItem;
lvg: TListGroup;
i, j: integer;
begin
if (fEntrantID > 0) then
fDoINDV := true
else if (fTeamID > 0) then
fDoINDV := false
else
begin
ModalResult := mrCancel;
{
Well do an abort here, because I've fogotten to assign a valid ID value!
}
Abort;
{ Alternative method....
PostMessage(Self.Handle, WM_CLOSE, 0,0);
Exit;
}
end;
j := 0;
fIsScratchedDCode := 0;
fIsDisqualifiedDCode := 0;
if AssertConnection then
begin
{ Find the DCode 'alias' for the simplified disqualification.
Returns zero if missing }
fIsScratchedDCode := SCM.GetIsScratchedDCode;
fIsDisqualifiedDCode := SCM.GetIsDisqualifiedDCode;
end;
if fDoINDV then
begin
qryStroke.ParamByName('EntrantID').AsInteger := fEntrantID;
qryStroke.Prepare;
qryStroke.Open;
// init
lvCodes.Items.BeginUpdate;
if qryStroke.Active and not qryStroke.IsEmpty then
begin
// init Group 1 SWIMMING STROKE
lvg := lvCodes.Groups.Items[1];
lvg.Header := UpperCase(qryStroke.FieldByName('Caption').AsString);
// TRANSPOSE SCM STROKE TO FINA'S STROKE TYPE ID
i := qryStroke.FieldByName('StrokeID').AsInteger;
case i of
1:
j := 2; // FS
2:
j := 4; // BS
3:
j := 3; // BK
4:
j := 5; // BF
5:
j := 6; // IM
end;
end;
end
else
begin
// init Group 1 SWIMMING STROKE
lvg := lvCodes.Groups.Items[1];
lvg.Header := 'TEAM RELAYS';
j := 7; // RELAY
end;
// I N I T L I S T I T E M S . . .
qryDisqualifyCode.ParamByName('DISQUALIFYTYPEID').AsInteger := j;
qryDisqualifyCode.Prepare;
qryDisqualifyCode.Open;
if qryDisqualifyCode.Active then
BEGIN
while not qryDisqualifyCode.eof do
Begin
// clear items
lv := lvCodes.Items.Add;
// Human readable string for the code.
lv.Caption := qryDisqualifyCode.FieldByName('ABREV').AsString;
// A description of the disqualification.
lv.SubItems.Add(qryDisqualifyCode.FieldByName('DCodeStr').AsString);
//
lv.SubItems.Add(qryDisqualifyCode.FieldByName('DisqualifyCodeID')
.AsString);
// General codes, SCM codes else swimming stroke.
lv.SubItems.Add(qryDisqualifyCode.FieldByName('DisqualifyTypeID')
.AsString);
// SET LISTVIEW GROUPS X 3
i := qryDisqualifyCode.FieldByName('DisqualifyTypeID').AsInteger;
case i of
1: // General
lv.GroupID := 0;
2, 3, 4, 5, 6, 7: // strokes FR,BK,BS,BF, IM,RELAY
lv.GroupID := 1;
8: // SCM simplified or missing disqualification codes.
lv.GroupID := 2;
end;
qryDisqualifyCode.next;
End;
END;
lvCodes.Items.EndUpdate;
end;
procedure TDCodePicker.lvCodesDblClick(Sender: TObject);
begin
btnOkClick(Sender);
end;
end.