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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
| #include <vcl.h>
#pragma hdrstop
//---------------------------------------------------------------------------
USEFORM("DateTime.cpp", Form1);
USEFORM("UsersUnit.cpp", UsersForm);
//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
try
{
Application->Initialize();
Application->CreateForm(__classid(TForm1), &Form1);
Application->CreateForm(__classid(TUsersForm), &UsersForm);
Application->Run();
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
catch (...)
{
try
{
throw Exception("");
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
}
return 0;
}
//------------
/*
2. отключение сбора при переходе
*/
#include <vcl.h>
#pragma hdrstop
#include "DateTime.h"
#include "UsersUnit.h"
#include "ping1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "trayicon"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{ //Коннектимся к базе данных
if(FileExists(ExtractFilePath(Application->ExeName)+"work.mdb")) //Проверяем наличие файла в рабочем каталоге
{ AnsiString Con = "Provider=Microsoft.Jet.OLEDB.4.0;\
User ID=Admin;Data Source="+ExtractFilePath(Application->ExeName)+"work.mdb;\
Mode=Share Deny None;\
Extended Properties="";\
Jet OLEDB:System database="";\
Jet OLEDB:Registry Path="";\
Jet OLEDB:Database Password="";\
Jet OLEDB:Engine Type=5;\
Jet OLEDB:Database Locking Mode=1;\
Jet OLEDB:Global Partial Bulk Ops=2;\
Jet OLEDB:Global Bulk Transactions=1;\
Jet OLEDB:New Database Password="";\
Jet OLEDB:Create System Database=False;\
Jet OLEDB:Encrypt Database=False;\
Jet OLEDB:Don't Copy Locale on Compact=False;\
Jet OLEDB:Compact Without Replica Repair=False;\
Jet OLEDB:SFP=False";
ADOConnectionBAZA->ConnectionString=Con;
ADOConnectionBAZA->Connected=true;
ADOQueryBAZA->Close();
ADOQueryBAZA->SQL->Clear();
ADOQueryBAZA->SQL->Add("SELECT * FROM TimesTable"); //Выгребаем всё из таблицы;
ADOQueryBAZA->Open();
}
DBGrid1->Align = alClient;
Panel1->Align = alClient;
Panel3->Align = alClient;
StringGrid1->Align = alClient;
PageControl1->Align = alClient;
StringGrid1->ColCount = 8;
StringGrid1->Cols[0]->SetText("No");
StringGrid1->Cols[1]->SetText("Ф.И.О.");
StringGrid1->Cols[2]->SetText("пн");
StringGrid1->Cols[3]->SetText("вт");
StringGrid1->Cols[4]->SetText("ср");
StringGrid1->Cols[5]->SetText("чт");
StringGrid1->Cols[6]->SetText("пц");
StringGrid1->Cols[7]->SetText("Баланс");
ReportTime = Now();
BeginReport = Now();
Users = NULL;
UsercCount = 0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{ Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::StartTimerTimer(TObject *Sender)
{ StartTimer->Enabled = false;
SetLabel();
SetUsers();
Update();
TrayIcon1->Minimize();
BitBtn6Click(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn3Click(TObject *Sender)
{ if(UsersForm->ShowModal() == mrOk)
{ SetLabel();
SetUsers();
}
}
//---------------------------------------------------------------------------
AnsiString IntToStr2(int p)
{ AnsiString r = IntToStr(p);
if(r.Length() < 2)r = "0"+r;
return r;
}
//---------------------------------------------------------------------------
AnsiString GetDay(TDateTime D)
{ unsigned short year, month, day;
D.DecodeDate(&year, &month, &day);
return IntToStr(day);
}
//---------------------------------------------------------------------------
AnsiString GetTime(TDateTime D)
{ unsigned short hour, min, sec, msec;
D.DecodeTime(&hour, &min, &sec, &msec);
return IntToStr2(hour)+":"+IntToStr2(min);
}
//---------------------------------------------------------------------------
AnsiString GetDate(TDateTime D)
{ unsigned short year, month, day;
D.DecodeDate(&year, &month, &day);
return IntToStr2(day)+"-"+IntToStr2(month)+"-"+IntToStr(year);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn4Click(TObject *Sender)
{ TADOQuery *A = UsersForm->ADOQueryNames;
AnsiString SQLIns;
AnsiString Note;
TDateTime D;
ADOQueryBAZA->Close();
ADOQueryBAZA->SQL->Clear();
SQLIns = "DELETE FROM TimesTable";
ADOQueryBAZA->SQL->Add(SQLIns);
ADOQueryBAZA->ExecSQL();
A->First();
for(int u = 0; u < A->RecordCount; u++)
{ D = Now()-100;
for(int i = 0; i < 100; i++)
{ ADOQueryBAZA->Close();
ADOQueryBAZA->SQL->Clear();
Note = "";
SQLIns = "INSERT INTO TimesTable VALUES ('"+A->FieldByName("Name")->AsString+"', '"+GetDate(D++)+"', '"+IntToStr2(8+random(2))+":"+IntToStr2(random(60))+"', '"+IntToStr2(17+random(2))+":"+IntToStr2(random(60))+"', '"+Note+"')";
ADOQueryBAZA->SQL->Add(SQLIns);
ADOQueryBAZA->ExecSQL();
}
A->Next();
}
ADOQueryBAZA->Close();
ADOQueryBAZA->SQL->Clear();
ADOQueryBAZA->SQL->Add("SELECT * FROM TimesTable"); //Выгребаем всё из таблицы;
ADOQueryBAZA->Open();
}
//---------------------------------------------------------------------------
int GetHour(AnsiString Begin)
{ int r, B;
AnsiString Buff = "";
if(Begin != "")
{ try
{ B = Begin.Pos(":");
for(int i = 0; i < B-1; i++)Buff += Begin.c_str()[i];
r = StrToInt(Buff);
}
catch(...)
{ r = 0;
}
}
else r = 0;
return r;
}
//---------------------------------------------------------------------------
int GetMinute(AnsiString Begin)
{ int r, B;
AnsiString Buff = "";
if(Begin != "")
{ try
{ B = Begin.Pos(":");
for(int i = B; i < Begin.Length(); i++)Buff += Begin.c_str()[i];
r = StrToInt(Buff);
}
catch(...)
{ r = 0;
}
}
else r = 0;
return r;
}
//---------------------------------------------------------------------------
int GetWorkHour(AnsiString Begin, AnsiString End)
{ int r, BH, BM, EH, EM, B;
AnsiString Buff = "";
if(Begin != "" && End != "")
{ try
{ B = Begin.Pos(":");
for(int i = 0; i < B-1; i++)Buff += Begin.c_str()[i];
BH = StrToInt(Buff);
Buff = "";
for(int i = B; i < Begin.Length(); i++)Buff += Begin.c_str()[i];
BM = StrToInt(Buff);
B = End.Pos(":");
Buff = "";
for(int i = 0; i < B-1; i++)Buff += End.c_str()[i];
EH = StrToInt(Buff);
Buff = "";
for(int i = B; i < End.Length(); i++)Buff += End.c_str()[i];
EM = StrToInt(Buff);
r = (EH*60+EM - BH*60-BM)/60;
}
catch(...)
{ r = 0;
}
}
else r = 0;
return r;
}
//---------------------------------------------------------------------------
void TForm1::SetUsers()
{ StringGrid1->Cursor = crSQLWait;
WorkTimer->Enabled = false;
TADOQuery *A = UsersForm->ADOQueryNames;
if(Users)
{ for(int i = 0; i < UsercCount; i++)delete Users[i];
delete [] Users;
}
UsercCount = A->RecordCount;
Users = new CUsers* [UsercCount];
for(int i = 0; i < UsercCount; i++)Users[i] = new CUsers();
A->First();
for(int u = 0; u < A->RecordCount; u++)
{ Users[u]->Name = A->FieldByName("Name")->AsString;
Users[u]->IP = A->FieldByName("IP")->AsString;
A->Next();
}
WorkTimer->Enabled = true;
StringGrid1->Cursor = crDefault;
}
//---------------------------------------------------------------------------
void TForm1::SetLabel()
{ StringGrid1->Cursor = crSQLWait;
WorkTimer->Enabled = false;
TADOQuery *A = UsersForm->ADOQueryNames;
AnsiString SQLIns;
int WorkHour, Buff;
while(BeginReport.DayOfWeek() != 2)BeginReport--;
Label1->Caption = GetDay(BeginReport)+"..."+GetDate(BeginReport+6);
StringGrid1->Cols[0]->SetText("No"); StringGrid1->ColWidths[0] = 60;
StringGrid1->Cols[1]->SetText("Ф.И.О."); StringGrid1->ColWidths[1] = 130;
StringGrid1->Cols[2]->SetText(GetDate(BeginReport).c_str()); StringGrid1->ColWidths[2] = 100;
StringGrid1->Cols[3]->SetText(GetDate(BeginReport+1).c_str()); StringGrid1->ColWidths[3] = 100;
StringGrid1->Cols[4]->SetText(GetDate(BeginReport+2).c_str()); StringGrid1->ColWidths[4] = 100;
StringGrid1->Cols[5]->SetText(GetDate(BeginReport+3).c_str()); StringGrid1->ColWidths[5] = 100;
StringGrid1->Cols[6]->SetText(GetDate(BeginReport+4).c_str()); StringGrid1->ColWidths[6] = 100;
StringGrid1->Cols[7]->SetText("Баланс"); StringGrid1->ColWidths[7] = 60;
StringGrid1->RowCount = A->RecordCount+1;
A->First();
for(int u = 0; u < A->RecordCount; u++)
{ WorkHour = 0;
if(Users && Users[u] && Users[u]->ONline)StringGrid1->Cells[0][u+1] = IntToStr(u+1)+" ON";
else StringGrid1->Cells[0][u+1] = u+1;
StringGrid1->Cells[1][u+1] = A->FieldByName("Name")->AsString;
for(int d = 0; d < 5; d++)
{ ADOQueryBAZA->Close();
ADOQueryBAZA->SQL->Clear();
SQLIns = "SELECT * FROM TimesTable WHERE Name = '"+A->FieldByName("Name")->AsString+"' AND Day = '"+GetDate(BeginReport+d)+"'";
ADOQueryBAZA->SQL->Add(SQLIns);
ADOQueryBAZA->Open();
if(ADOQueryBAZA->RecordCount > 0)
{ if(ADOQueryBAZA->FieldByName("Note1")->AsString == "")
{ StringGrid1->Cells[2+d][u+1] = ADOQueryBAZA->FieldByName("TimeON")->AsString+" ... "+ADOQueryBAZA->FieldByName("TimeOFF")->AsString;
WorkHour += GetWorkHour(ADOQueryBAZA->FieldByName("TimeON")->AsString, ADOQueryBAZA->FieldByName("TimeOFF")->AsString);
}
else StringGrid1->Cells[2+d][u+1] = ADOQueryBAZA->FieldByName("Note1")->AsString; // выводим коментарий;
}
else StringGrid1->Cells[2+d][u+1] = " ..."; // нет мзаписи;
}
StringGrid1->Cells[7][u+1] = IntToStr(WorkHour);
A->Next();
}
WorkTimer->Enabled = true;
StringGrid1->Cursor = crDefault;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{ // Время на неделю назад;
BeginReport--;
SetLabel();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn5Click(TObject *Sender)
{ // Время на неделю вперед;
BeginReport += 8;
if(BeginReport > Now())BeginReport = Now();
SetLabel();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TabSheet1Show(TObject *Sender)
{ SetLabel();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TabSheet2Show(TObject *Sender)
{ ADOQueryBAZA->Close();
ADOQueryBAZA->SQL->Clear();
ADOQueryBAZA->SQL->Add("SELECT * FROM TimesTable"); //Выгребаем всё из таблицы;
ADOQueryBAZA->Open();
}
//---------------------------------------------------------------------------
bool Ping(AnsiString IP)
{ if(PingHost(IP, NULL) != -1)return true;
else return false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::WorkTimerTimer(TObject *Sender)
{ WorkTimer->Enabled = false;
if(BitBtn6->Caption == "Вкл.")
{ bool Action = false;
StringGrid1->Cursor = crHourGlass;
BitBtn6->Font->Color = clLime;
BitBtn6->Update();
TrayIcon1->IconIndex = 2;
TrayIcon1->Hint = "Опрос";
ExitFlag = true;
for(int i = 0; i < UsercCount && ExitFlag; i++)
{ if(Users[i]->Day != GetDate(Now()))
{ Users[i]->Day = GetDate(Now());
Users[i]->CreateRecord();
Action = true;
}
if(Users[i]->IP.LowerCase() == "null" || Users[i]->IP == "") // Для тех у кого нет IP;
{ if(Users[i]->BTime == "" || Users[i]->ETime == "")
{ Users[i]->BTime = IntToStr2(8+random(3))+":"+IntToStr2(random(60));
Users[i]->ETime = IntToStr2(17+random(2))+":"+IntToStr2(random(60));
Users[i]->UpdateRecord();
Action = true;
}
}
else
{ if(Ping(Users[i]->IP))
{ if(!Users[i]->ONline)Action = true;
Users[i]->ONline = true;
if(Users[i]->BTime == "")
{ Users[i]->BTime = GetTime(Now());
Users[i]->UpdateRecord();
Action = true;
}
Users[i]->ETime = ""; // Еще не ущел;
}
else
{ if(Users[i]->ONline)Action = true;
Users[i]->ONline = false;
if(Users[i]->BTime != "" && Users[i]->ETime == "")
{ Users[i]->ETime = GetTime(Now());
Users[i]->UpdateRecord();
Action = true;
}
}
}
}
if(Action)
{ TabSheet2Show(Sender);
SetLabel();
}
BitBtn6->Font->Color = clGreen;
BitBtn6->Update();
TrayIcon1->IconIndex = 1;
TrayIcon1->Hint = "Включен";
StringGrid1->Cursor = crDefault;
}
if(BitBtn6->Caption == "Откл.") // Если отключили во время опроса;
{ TrayIcon1->IconIndex = 0;
TrayIcon1->Hint = "Отключен";
}
WorkTimer->Enabled = true;
}
//---------------------------------------------------------------------------
void CUsers::CreateRecord()
{ AnsiString SQLIns;
BTime = "";
ETime = "";
Form1->ADOQueryBAZA->Close();
Form1->ADOQueryBAZA->SQL->Clear();
SQLIns = "SELECT * FROM TimesTable WHERE Name = '"+Name+"' AND Day = '"+Day+"'";
Form1->ADOQueryBAZA->SQL->Add(SQLIns);
Form1->ADOQueryBAZA->Open();
if(Form1->ADOQueryBAZA->RecordCount == 0)
{ Form1->ADOQueryBAZA->Close();
Form1->ADOQueryBAZA->SQL->Clear();
SQLIns = "INSERT INTO TimesTable VALUES ('"+Name+"', '"+Day+"', '"+BTime+"', '"+ETime+"', 'команд.')";
Form1->ADOQueryBAZA->SQL->Add(SQLIns);
Form1->ADOQueryBAZA->ExecSQL();
}
else BTime = Form1->ADOQueryBAZA->FieldByName("TimeON")->AsString;
}
//---------------------------------------------------------------------------
void CUsers::UpdateRecord()
{ AnsiString SQLIns;
Form1->ADOQueryBAZA->Close();
Form1->ADOQueryBAZA->SQL->Clear();
SQLIns = "UPDATE TimesTable SET Note1 = '' WHERE Name = '"+Name+"' AND Day = '"+Day+"'";
Form1->ADOQueryBAZA->SQL->Add(SQLIns);
Form1->ADOQueryBAZA->ExecSQL();
Form1->ADOQueryBAZA->Close();
Form1->ADOQueryBAZA->SQL->Clear();
SQLIns = "UPDATE TimesTable SET TimeON = '"+BTime+"' WHERE Name = '"+Name+"' AND Day = '"+Day+"'";
Form1->ADOQueryBAZA->SQL->Add(SQLIns);
Form1->ADOQueryBAZA->ExecSQL();
Form1->ADOQueryBAZA->Close();
Form1->ADOQueryBAZA->SQL->Clear();
SQLIns = "UPDATE TimesTable SET TimeOFF = '"+ETime+"' WHERE Name = '"+Name+"' AND Day = '"+Day+"'";
Form1->ADOQueryBAZA->SQL->Add(SQLIns);
Form1->ADOQueryBAZA->ExecSQL();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn6Click(TObject *Sender)
{ if(BitBtn6->Caption == "Вкл.")
{ BitBtn6->Caption = "Откл.";
BitBtn6->Font->Color = clBlack;
TrayIcon1->IconIndex = 0;
TrayIcon1->Hint = "Отключен";
ExitFlag = false;
}
else
{ BitBtn6->Caption = "Вкл.";
BitBtn6->Font->Color = clGreen;
WorkTimerTimer(Sender);
TrayIcon1->IconIndex = 1;
TrayIcon1->Hint = "Включен";
}
BitBtn6->Update();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::StringGrid1DblClick(TObject *Sender)
{ // Двойной клик - переход к событию;
AnsiString SearchName;
AnsiString SearchDate;
PageControl1->ActivePageIndex = 1;
SearchName = StringGrid1->Cells[1][StringGrid1->Row];
SearchDate = StringGrid1->Cells[StringGrid1->Col][0];
if(SearchName != "" && SearchDate != "")
{ DBGrid1->Visible = false;
ADOQueryBAZA->First();
for(int i = 0; i < ADOQueryBAZA->RecordCount; i++)
{ if(ADOQueryBAZA->FieldByName("Name")->AsString == SearchName && ADOQueryBAZA->FieldByName("Day")->AsString == SearchDate)break;
ADOQueryBAZA->Next();
}
DBGrid1->Visible = true;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormKeyUp(TObject *Sender, WORD &Key,
TShiftState Shift)
{ if(Key == 120 && MessageBox(NULL, "Выполнить проверку?", "Проверка БД", MB_YESNO) == mrYes)
{ AnsiString SQLIns;
AnsiString BTime;
AnsiString ETime;
float PerCent;
Caption = "Проверка БД";
StringGrid1->Cursor = crSQLWait;
WorkTimer->Enabled = false;
ADOQueryBAZA->Close();
ADOQueryBAZA->SQL->Clear();
SQLIns = "SELECT * FROM TimesTable WHERE Note1 = ''";
ADOQueryBAZA->SQL->Add(SQLIns);
ADOQueryBAZA->Open();
ADOQueryBAZA->First();
if(ADOQueryBAZA->RecordCount > 0)PerCent = 100./(float)ADOQueryBAZA->RecordCount;
for(int i = 0; i < ADOQueryBAZA->RecordCount; i++)
{ Caption = "Проверка БД "+FormatFloat("0.0%", PerCent*(float)i);
BTime = ADOQueryBAZA->FieldByName("TimeON")->AsString;
ETime = ADOQueryBAZA->FieldByName("TimeOFF")->AsString;
if(GetWorkHour(BTime, ETime) < 8)
{ if(GetHour(BTime) > 10)BTime = "09:"+IntToStr2(GetMinute(BTime));
while(GetWorkHour(BTime, ETime) < 8)
{ ETime = IntToStr2(GetHour(ETime)+1)+":"+IntToStr2(GetMinute(ETime));
}
ADOQueryBAZA->Edit();
ADOQueryBAZA->FieldByName("TimeON")->AsString = BTime;
ADOQueryBAZA->FieldByName("TimeOFF")->AsString = ETime;
ADOQueryBAZA->Post();
}
ADOQueryBAZA->Next();
}
Caption = "Учет рабочего времени сотрудников";
TabSheet2Show(Sender);
SetLabel();
WorkTimer->Enabled = true;
StringGrid1->Cursor = crDefault;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N2Click(TObject *Sender)
{
Application->HelpFile="ERER.hlp";
Application->HelpCommand(HELP_CONTEXT, 1);
}
//--------------------------------------------------------------------------- |