forked from layerfsd/Roomer-PMS-1
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuActivityLogs.pas
357 lines (289 loc) · 15 KB
/
uActivityLogs.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
unit uActivityLogs;
interface
uses
SysUtils,
Classes,
uUtils,
TypInfo,
uRoomerThreadedRequest;
type
TActivityType = (ROOMER, INVOICE, RESERVATION, AVAILABILITY, RATE, TABLE_CHANGE, OFFLINEREPORT, ALLOTMENTTORES);
TRoomerAction = (LOGIN, LOGOUT, BUTTON_CLICK, ERROR);
TInvoiceAction = (ADD_LINE, DELETE_LINE, CHANGE_ITEM, ADD_PAYMENT, DELETE_PAYMENT, CHANGE_PAYMENT, PRINT_PROFORMA,
PAY_AND_PRINT);
TTableAction = (ADD_RECORD, DELETE_RECORD, CHANGE_FIELD);
TReservationAction = (NEW_RESERVATION, CHANGE_RESERVATION, DELETE_RESERVATION, NEW_ROOMRESERVATION,
CHANGE_ROOMRESERVATION, DELETE_ROOMRESERVATION, CHANGE_ROOMRESERVATION_STATUS, CHANGE_ARRIVAL_DATE,
CHANGE_DEPARTURE_DATE, CHANGE_NUMBER_OF_GUESTS, NEW_ROOM_NUMBER, NEW_ROOM_TYPE);
TAllotmentToResAction = (CREATE_RESERVATION_FROM_ALLOTMENT);
TAvailabilityAction = (EDIT, BULK);
TRateAction = (RATE_EDIT, STOP_EDIT, MIN_EDIT, MAX_EDIT);
TOfflineReportAction = (REPORTEXCEPTION);
procedure AddAllotmentToResActivityLog(const user: String; action: TAllotmentToResAction; const aAllotmentResId: integer;
const aNewReservationId: integer; const aRestAllotmentResId: integer;
const aNewReservationArrival: TDate);
procedure AddTableChangeActivityLog(const user: String; action: TTableAction; const TableName: String;
const RecId: Integer; const OldValue: String; const NewValue: String; const moreInfo: String);
procedure AddOfflineReportActivityLog(const user: String; action: TOfflineReportAction; const reportname: string;
const result: String; const moreInfo: String);
procedure AddRoomerActivityLog(const user: String; action: TRoomerAction; const result: String; const moreInfo: String;
const OldValue: String = ''; const NewValue: String = ''; const Code: String = ''; const RESERVATION: Integer = 0;
const RoomReservation: Integer = 0; const ID_1: Integer = 0; const ID_2: Integer = 0; const ID_3: Integer = 0;
const ADate: String = '');
procedure AddInvoiceActivityLog(const user: String; iReservation, iRoomReservation, invoiceIndex: Integer;
action: TInvoiceAction; const Code: String; value: Double; lineId: Integer; const moreInfo: String);
procedure AddReservationActivityLog(const user: String; iReservation, iRoomReservation: Integer;
action: TReservationAction; const OldValue: String; const NewValue: String; const moreInfo: String);
procedure AddAvailabilityActivityLog(const user: String; action: TAvailabilityAction; const roomClass: String;
iAvailability: Integer; date: TDate; const moreInfo: String);
procedure AddRateActivityLog(const user: String; action: TRateAction; const roomClass: String; RATE: Double;
stop: Boolean; min: Integer; max: Integer; date: TDate; const moreInfo: String);
procedure PushActivityLogs;
procedure UserClickedDxLargeButton(Sender: TObject);
function CreateInvoiceActivityLog(const user: String; iReservation, iRoomReservation, invoiceIndex: Integer;
action: TInvoiceAction; const Code: String; value: Double; lineId: Integer; const moreInfo: String): String;
function WriteInvoiceActivityLog(const sLine: String): String;
function CreateReservationActivityLog(const user: String; iReservation, iRoomReservation: Integer;
action: TReservationAction; const OldValue: String; const NewValue: String; const moreInfo: String): String;
function WriteReservationActivityLog(const sLine: String): String;
var
ActivityLogGetThreadedData: TGetThreadedData = nil;
implementation
uses
Menus,
sButton,
sLabel,
ud,
ioUtils,
uAppGlobal,
uDateUtils,
uStringUtils,
dxBar,
WinApi.Windows,
XmlUtils;
function GetDataFileLocationWithName(activity: TActivityType): String;
var
filename: String;
begin
filename := 'User_Logs_' + GetEnumName(TypeInfo(TActivityType), Ord(activity)) + '.log';
result := TPath.Combine(glb.GetDataCacheLocation, filename);
end;
function CreateXmlElement(const user: String; const When: String; const Category: String; const action: String;
const Description: String; const DetailedDescription: String; const OldValue: String; const NewValue: String;
const Code: String; RESERVATION: Integer; RoomReservation: Integer; ID_1: Integer; ID_2: Integer; ID_3: Integer;
const ADate: String): String;
begin
result := '<log user="%s" type="%s" action="%s" when="%s">' + '<description>%s</description>' +
'<detailedDescription>%s</detailedDescription>' + '<machineName>%s</machineName>' + '<oldValue>%s</oldValue>' +
'<newValue>%s</newValue>' + '<code>%s</code>' + '<reservation>%s</reservation>' +
'<roomReservation>%s</roomReservation>' + '<id1>%s</id1>' + '<id2>%s</id2>' + '<id3>%s</id3>' + '<date>%s</date>'
+ '</log>';
result := format(result, [XmlEncode_ex(user, ''), XmlEncode_ex(Category, ''), XmlEncode_ex(action, ''), When,
XmlEncode_ex(Description, ''), XmlEncode_ex(DetailedDescription, ''), XmlEncode_ex(ComputerName, ''), // From uUtils
XmlEncode_ex(OldValue, ''), XmlEncode_ex(NewValue, ''), XmlEncode_ex(Code, ''), inttostr(RESERVATION),
inttostr(RoomReservation), inttostr(ID_1), inttostr(ID_2), inttostr(ID_3), ADate]);
end;
function CreateInvoiceActivityLog(const user: String; iReservation, iRoomReservation, invoiceIndex: Integer;
action: TInvoiceAction; const Code: String; value: Double; lineId: Integer; const moreInfo: String): String;
var
categoryName, actionName: String;
begin
categoryName := GetEnumName(TypeInfo(TActivityType), Ord(INVOICE));
actionName := GetEnumName(TypeInfo(TInvoiceAction), Ord(action));
result := CreateXmlElement(user, uDateUtils.dateTimeToXmlString(now), categoryName, actionName, moreInfo, moreInfo,
'', FloatToXml(value, 2), Code, iReservation, iRoomReservation, lineId, invoiceIndex, 0, '');
end;
function WriteInvoiceActivityLog(const sLine: String): String;
begin
AddToTextFile(GetDataFileLocationWithName(INVOICE), sLine);
end;
procedure AddInvoiceActivityLog(const user: String; iReservation, iRoomReservation, invoiceIndex: Integer;
action: TInvoiceAction; const Code: String; value: Double; lineId: Integer; const moreInfo: String);
begin
WriteInvoiceActivityLog(CreateInvoiceActivityLog(user, iReservation, iRoomReservation, invoiceIndex, action, Code,
value, lineId, moreInfo));
end;
procedure AddAllotmentToResActivityLog(const user: String; action: TAllotmentToResAction; const aAllotmentResId: integer;
const aNewReservationId: integer; const aRestAllotmentResId: integer;
const aNewReservationArrival: TDate);
var
categoryName, actionName, sLine: String;
begin
categoryName := GetEnumName(TypeInfo(TActivityType), Ord(ALLOTMENTTORES));
actionName := GetEnumName(TypeInfo(TAllotmentToResAction), Ord(action));
sLine := CreateXmlElement(user, uDateUtils.dateTimeToXmlString(now), categoryName, actionName, '', '',
IntToSTr(aAllotmentResId), IntToStr(aRestAllotmentResId), '', aNewReservationId, 0, 0, 0, 0, dateTimeToXMLString(aNewReservationArrival));
AddToTextFile(GetDataFileLocationWithName(RESERVATION), sLine);
end;
procedure AddReservationActivityLog(const user: String; iReservation, iRoomReservation: Integer;
action: TReservationAction; const OldValue: String; const NewValue: String; const moreInfo: String);
var
categoryName, actionName, sLine: String;
begin
categoryName := GetEnumName(TypeInfo(TActivityType), Ord(RESERVATION));
actionName := GetEnumName(TypeInfo(TReservationAction), Ord(action));
sLine := CreateXmlElement(user, uDateUtils.dateTimeToXmlString(now), categoryName, actionName, moreInfo, moreInfo,
OldValue, NewValue, '', iReservation, iRoomReservation, 0, 0, 0, '');
AddToTextFile(GetDataFileLocationWithName(RESERVATION), sLine);
end;
procedure AddTableChangeActivityLog(const user: String; action: TTableAction; const TableName: String;
const RecId: Integer; const OldValue: String; const NewValue: String; const moreInfo: String);
var
categoryName, actionName, sLine: String;
begin
categoryName := GetEnumName(TypeInfo(TActivityType), Ord(TABLE_CHANGE));
actionName := GetEnumName(TypeInfo(TTableAction), Ord(action));
sLine := CreateXmlElement(user, uDateUtils.dateTimeToXmlString(now), categoryName, actionName, moreInfo, moreInfo,
OldValue, NewValue, TableName, 0, 0, RecId, 0, 0, '');
AddToTextFile(GetDataFileLocationWithName(TABLE_CHANGE), sLine);
end;
function CreateReservationActivityLog(const user: String; iReservation, iRoomReservation: Integer;
action: TReservationAction; const OldValue: String; const NewValue: String; const moreInfo: String): String;
var
categoryName, actionName: String;
begin
categoryName := GetEnumName(TypeInfo(TActivityType), Ord(RESERVATION));
actionName := GetEnumName(TypeInfo(TReservationAction), Ord(action));
result := CreateXmlElement(user, uDateUtils.dateTimeToXmlString(now), categoryName, actionName, moreInfo, moreInfo,
OldValue, NewValue, '', iReservation, iRoomReservation, 0, 0, 0, '');
end;
function WriteReservationActivityLog(const sLine: String): String;
begin
AddToTextFile(GetDataFileLocationWithName(RESERVATION), sLine);
end;
// ****************************************
procedure AddOfflineReportActivityLog(const user: String; action: TOfflineReportAction; const reportname: string;
const result: String; const moreInfo: String);
var
sLine: string;
categoryName: string;
actionName: string;
begin
categoryName := GetEnumName(TypeInfo(TActivityType), Ord(OFFLINEREPORT));
actionName := GetEnumName(TypeInfo(TRoomerAction), Ord(action));
sLine := CreateXmlElement(user, uDateUtils.dateTimeToXmlString(now), categoryName + ': ' + reportname, actionName,
result, moreInfo, '', '', '', 0, 0, 0, 0, 0, '');
AddToTextFile(GetDataFileLocationWithName(OFFLINEREPORT), sLine);
end;
procedure AddRoomerActivityLog(const user: String; action: TRoomerAction; const result: String; const moreInfo: String;
const OldValue: String = ''; const NewValue: String = ''; const Code: String = ''; const RESERVATION: Integer = 0;
const RoomReservation: Integer = 0; const ID_1: Integer = 0; const ID_2: Integer = 0; const ID_3: Integer = 0;
const ADate: String = '');
var
categoryName, actionName, sLine: String;
begin
try
if d.roomerMainDataSet.hotelId = '' then
exit;
except
exit;
end;
categoryName := GetEnumName(TypeInfo(TActivityType), Ord(ROOMER));
actionName := GetEnumName(TypeInfo(TRoomerAction), Ord(action));
sLine := CreateXmlElement(user, uDateUtils.dateTimeToXmlString(now), categoryName, actionName, moreInfo, moreInfo,
OldValue, NewValue, Code, RESERVATION, RoomReservation, ID_1, ID_2, ID_3, ADate);
AddToTextFile(GetDataFileLocationWithName(ROOMER), sLine);
end;
procedure AddAvailabilityActivityLog(const user: String; action: TAvailabilityAction; const roomClass: String;
iAvailability: Integer; date: TDate; const moreInfo: String);
var
categoryName, actionName, sLine: String;
begin
categoryName := GetEnumName(TypeInfo(TActivityType), Ord(AVAILABILITY));
actionName := GetEnumName(TypeInfo(TAvailabilityAction), Ord(action));
sLine := CreateXmlElement(user, uDateUtils.dateTimeToXmlString(now), categoryName, actionName, moreInfo, moreInfo, '',
inttostr(iAvailability), roomClass, 0, 0, 0, 0, 0, uDateUtils.dateTimeToXmlString(date));
AddToTextFile(GetDataFileLocationWithName(AVAILABILITY), sLine);
end;
procedure AddRateActivityLog(const user: String; action: TRateAction; const roomClass: String; RATE: Double;
stop: Boolean; min: Integer; max: Integer; date: TDate; const moreInfo: String);
var
categoryName, actionName, sLine: String;
begin
categoryName := GetEnumName(TypeInfo(TActivityType), Ord(AVAILABILITY));
actionName := GetEnumName(TypeInfo(TRateAction), Ord(action));
sLine := CreateXmlElement(user, uDateUtils.dateTimeToXmlString(now), categoryName, actionName, moreInfo, moreInfo, '',
FloatToXml(RATE, 2), roomClass, 0, 0, Ord(stop), min, max, uDateUtils.dateTimeToXmlString(date));
AddToTextFile(GetDataFileLocationWithName(AVAILABILITY), sLine);
end;
procedure PushActivityLogs;
var
activity: TActivityType;
filename: String;
content: TStringList;
i: Integer;
list: TStringList;
sql: String;
begin
try
list := TStringList.Create;
try
for activity := LOW(TActivityType) to HIGH(TActivityType) do
begin
filename := GetDataFileLocationWithName(activity);
if FileExists(filename) then
begin
if FileExists(filename + '.busy') then
SysUtils.DeleteFile(filename + '.busy');
if SysUtils.RenameFile(filename, filename + '.busy') then
begin
filename := filename + '.busy';
content := TStringList.Create;
try
content.LoadFromFile(filename);
for i := 0 to content.Count - 1 do
list.Add(content[i]);
// Then delete the file...
SysUtils.DeleteFile(filename);
finally
content.Free;
end;
end;
end;
end;
if list.Count > 0 then
begin
sql := '<?xml version="1.0" encoding="UTF-8"?>' + #10 + '<logs>' + #10 + list.Text + '</logs>' + #10;
if NOT Assigned(ActivityLogGetThreadedData) then
ActivityLogGetThreadedData := TGetThreadedData.Create;
ActivityLogGetThreadedData.Put('userlogs', 'logs=' + d.roomerMainDataSet.UrlEncode(sql), nil);
end;
finally
list.Free;
end;
except
on e: Exception do
begin
// TODO - Ignore for the time being
end;
end;
end;
procedure UserClickedDxLargeButton(Sender: TObject);
begin
if Assigned(Sender) then
if (Sender IS TdxBarLargeButton) then
AddRoomerActivityLog(d.roomerMainDataSet.username, uActivityLogs.BUTTON_CLICK, TdxBarLargeButton(Sender).Name,
'User ' + d.roomerMainDataSet.username + ' clicked ' + TdxBarLargeButton(Sender).Caption + ' (' +
TdxBarLargeButton(Sender).Name + ')')
else if (Sender IS TMenuItem) then
AddRoomerActivityLog(d.roomerMainDataSet.username, uActivityLogs.BUTTON_CLICK, TMenuItem(Sender).Name,
'User ' + d.roomerMainDataSet.username + ' clicked ' + TMenuItem(Sender).Caption + ' (' + TMenuItem(Sender)
.Name + ')')
else if (Sender IS TsButton) then
AddRoomerActivityLog(d.roomerMainDataSet.username, uActivityLogs.BUTTON_CLICK, TsButton(Sender).Name,
'User ' + d.roomerMainDataSet.username + ' clicked ' + TsButton(Sender).Caption + ' (' + TsButton(Sender)
.Name + ')')
else if (Sender IS TsLabel) then
AddRoomerActivityLog(d.roomerMainDataSet.username, uActivityLogs.BUTTON_CLICK, TsLabel(Sender).Name,
'User ' + d.roomerMainDataSet.username + ' clicked ' + TsLabel(Sender).Caption + ' (' + TsLabel(Sender)
.Name + ')')
else
AddRoomerActivityLog(d.roomerMainDataSet.username, uActivityLogs.BUTTON_CLICK, '<UNKNOWN>',
'User ' + d.roomerMainDataSet.username + ' clicked <UNKNOWN> (<UNKNOWN>)');
end;
initialization
finalization
// Destruction should be here, but generates an InvalidPointererror when destroying SQL TStringlist of TROomerdataset
// Destruction is now in the FormDestroy of uMain
// FreeAndNil(ActivityLogGetThreadedData);
end.