-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUfrReportClientR.pas
224 lines (194 loc) · 5.98 KB
/
UfrReportClientR.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
unit UfrReportClientR;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, DBGridEhGrouping, ToolCtrlsEh,
DBGridEhToolCtrls, DynVarsEh, EhLibVCL, GridsEh, DBAxisGridsEh, DBGridEh, DataUnit,
Data.DB, DBAccess, MyAccess, MemDS, UtilsUnit, Vcl.ComCtrls, Vcl.Menus;
type
TfrReportClientR = class(TForm)
DBGridEh1: TDBGridEh;
q: TMyQuery;
ds: TMyDataSource;
lv: TListView;
PopupMenu1: TPopupMenu;
N1: TMenuItem;
SaveDialog1: TSaveDialog;
procedure FormShow(Sender: TObject);
procedure lvCustomDrawItem(Sender: TCustomListView; Item: TListItem;
State: TCustomDrawState; var DefaultDraw: Boolean);
procedure lvCompare(Sender: TObject; Item1, Item2: TListItem; Data: Integer;
var Compare: Integer);
procedure lvColumnClick(Sender: TObject; Column: TListColumn);
procedure N1Click(Sender: TObject);
private
{ Private declarations }
procedure createSqlReport;
public
{ Public declarations }
idDep, nameDep:string;
dt1, dt2:TDate;
userId:string;
end;
const
sqlReport:string=
'select tasks.id, tasks.id_client, tasks.id_work, tasks.time_work, tasks.dt, tasks.user_id,'+chr(13)+
'clients.`name` as `client`,'+chr(13)+
'works.`name` as `work`'+chr(13)+
'from tasks'+chr(13)+
'LEFT JOIN works ON (works.`id`=tasks.`id_work`)'+chr(13)+
'LEFT JOIN clients ON (clients.`id`=tasks.`id_client`)'+chr(13)+
// 'WHERE (tasks.dt BETWEEN ' + qs(':dt1') + ' AND ' + qs(':dt2') + ')';
'WHERE (tasks.dt BETWEEN :dt1 AND :dt2)';
sqlGroup:string=' GROUP BY tasks.id';
sqlOrder:string=' ORDER BY clients.name ASC';
var
frReportClientR: TfrReportClientR;
i:integer;
ColumnToSort: Integer;
sort:TSortState=ssNone;
implementation
{$R *.dfm}
procedure TfrReportClientR.createSqlReport;
var slUserIds:TStringList;
begin
if(idDep='-2') then
begin
q.SQL.Text:=sqlReport+chr(13)+
'AND (tasks.user_id in('+userId+'))'
+sqlGroup+sqlOrder;
q.ParamByName('dt1').Value:=toMySqlDate(dt1);
q.ParamByName('dt2').Value:=toMySqlDate(dt2);
end else
if(idDep='-1') then
begin
q.SQL.Text:=sqlReport+sqlGroup+sqlOrder;
q.ParamByName('dt1').Value:=toMySqlDate(dt1);
q.ParamByName('dt2').Value:=toMySqlDate(dt2);
end else
begin
slUserIds:=TStringList.Create;
q.SQL.Text:='select users.id, users.id_dep from users where users.id_dep='+idDep;
q.Open;
if(q.RecordCount=0) then
begin
ShowMessage('Íå íàéäåíî íè îäíîãî ïîëüçîâàòåëÿ â ýòîì ïîäðàçäåëåíèè');
q.Close;
slUserIds.Free;
exit;
end;
for I := 0 to q.RecordCount-1 do
begin
slUserIds.Add(q.FieldByName('id').AsString);
q.Next;
end;
q.Close;
slUserIds.Delimiter:=',';
//showmessage(slUserIds.DelimitedText);
q.SQL.Text:=sqlReport+chr(13)+
'AND (tasks.user_id in('+slUserIds.DelimitedText+'))'
+sqlGroup+sqlOrder;
q.ParamByName('dt1').Value:=toMySqlDate(dt1);
q.ParamByName('dt2').Value:=toMySqlDate(dt2);
slUserIds.Free;
end;
end;
procedure TfrReportClientR.FormShow(Sender: TObject);
var tm, currWork, nextWork, allTime:integer;
client:string;
function addItem(name:string; tm:integer):TListItem;
var item:TListItem;
begin
item:=lv.Items.Add;
item.Caption:=name;
item.SubItems.Add(inttostr(tm));
item.SubItems.Add(FormatFloat('#0.##', tm/60));
item.SubItems.Add(minToHour(tm));
Result:=item;
end;
begin
Caption:='Îò÷åò '+nameDep;
createSqlReport;
//ShowMessage(q.SQL.Text);
q.Open;
if(q.RecordCount=0) then exit;
tm:=0;
allTime:=0;
currWork:=q.FieldByName('id_client').AsInteger;
client:=q.FieldByName('client').AsString;
for I := 0 to q.RecordCount-1 do
begin
nextWork:= q.FieldByName('id_client').AsInteger;
if(currWork=nextWork) then
begin
tm:=tm+q.FieldByName('time_work').AsInteger;
if(i=q.RecordCount-1) then addItem(client, tm);
end else
begin
addItem(client, tm);
tm:=q.FieldByName('time_work').AsInteger;
currWork:=q.FieldByName('id_client').AsInteger;
client:=q.FieldByName('client').AsString;
if(i=q.RecordCount-1) then addItem(client, tm);
end;
allTime:=allTime+q.FieldByName('time_work').AsInteger;
if(i=q.RecordCount-1) then addItem('Èòîãî:', allTime);
q.Next;
end;
q.Close;
end;
procedure TfrReportClientR.lvColumnClick(Sender: TObject; Column: TListColumn);
begin
ColumnToSort := Column.Index;
//if(ColumnToSort>0) then
if(sort=ssNone) then sort:=ssUp
else if(sort=ssUp) then sort:=ssDown
else if(sort=ssDown) then sort:=ssUp;
(Sender as TCustomListView).AlphaSort;
end;
procedure TfrReportClientR.lvCompare(Sender: TObject; Item1, Item2: TListItem;
Data: Integer; var Compare: Integer);
var
ix: Integer;
f1, f2:real;
begin
case ColumnToSort of
0:begin
if(sort=ssUp) then
Compare := CompareText(Item1.Caption, Item2.Caption)
else Compare := CompareText(Item2.Caption, Item1.Caption);
//showmessage(inttostr(Compare));
end;
1,2:begin
ix := ColumnToSort - 1;
f1:=strtofloat(Item1.SubItems[ix]);
f2:=strtofloat(Item2.SubItems[ix]);
if(sort=ssUp) then
Compare:=CompareDoubleInc(f1, f2)
else Compare:=CompareDoubleInc(f2, f1);
end;
3:begin
ix := ColumnToSort - 2;
f1:=strtofloat(Item1.SubItems[ix]);
f2:=strtofloat(Item2.SubItems[ix]);
if(sort=ssUp) then
Compare:=CompareDoubleInc(f1, f2)
else Compare:=CompareDoubleInc(f2, f1);
end;
end;
end;
procedure TfrReportClientR.lvCustomDrawItem(Sender: TCustomListView;
Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
if(Item.Caption='Èòîãî:') then
begin
Sender.Canvas.Font.Style:= Sender.Canvas.Font.Style + [fsBold];
Sender.Canvas.Brush.Color:=cl3dLight;
end else if item.Index mod 2=0 then Sender.Canvas.Brush.Color:=$00EFEFEF;
end;
procedure TfrReportClientR.N1Click(Sender: TObject);
begin
if not SaveDialog1.Execute(handle) then exit;
UtilsUnit.saveListViewToExel(lv, saveDialog1.FileName);
end;
end.