forked from layerfsd/Roomer-PMS-1
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuFrmChannelCopyFrom.pas
419 lines (366 loc) · 13.6 KB
/
uFrmChannelCopyFrom.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
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
unit uFrmChannelCopyFrom;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Mask, sMaskEdit, sCustomComboEdit, sTooledit, sCheckBox, sComboBox, sLabel, sPanel,
sListBox, sGroupBox, sButton, cmpRoomerDataSet, System.Generics.Collections, sScrollBox;
type
TIntValue = class
value : Integer;
public
constructor create(_value : Integer);
end;
TValueType = (vtPrice,
vtAvailability,
vtMinStay,
vtMaxStay,
vtClosedOnArrival,
vtClosedOnDeparture,
vtStop,
vtLengthOfStayArrivalDateBased,
vtSingleUsePrice);
TChannelValueEnity = class
ValueType : TValueType;
ADate : TDateTime;
roomClassID : Integer;
value : Variant;
public
constructor create(_ValueType : TValueType;
_ADate : TDateTime;
_roomClassID : Integer;
_value : Variant);
end;
TChannelValueEnityList = TObjectList<TChannelValueEnity>;
TFrmChannelCopyFrom = class(TForm)
sPanel1: TsPanel;
lblChannel: TsLabel;
cbxSourceChannel: TsComboBox;
pnlParameters: TsPanel;
Label3: TsLabel;
dtBulkFrom: TsDateEdit;
Label4: TsLabel;
dtBulkTo: TsDateEdit;
sGroupBox1: TsGroupBox;
cbxRestrictions: TsCheckBox;
cbxAvailabilities: TsCheckBox;
cbxRate: TsCheckBox;
grpRoomClasses: TsGroupBox;
sPanel3: TsPanel;
BtnOk: TsButton;
btnCancel: TsButton;
sbRoomClasses: TsScrollBox;
cbxRoomClass_: TsCheckBox;
sLabel1: TsLabel;
cbxDestChannel: TsComboBox;
procedure FormCreate(Sender: TObject);
procedure BtnOkClick(Sender: TObject);
procedure cbxSourceChannelChange(Sender: TObject);
procedure cbxRateClick(Sender: TObject);
procedure dtBulkFromAcceptDate(Sender: TObject; var aDate: TDateTime; var CanAccept: Boolean);
procedure cbxRoomClass_Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure dtBulkToCloseUp(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
procedure ListChannels;
procedure ListRoomClasses(channel: Integer);
procedure RemoveRoomClassList;
procedure AddRoomClassToList(chClassRel: TRoomerDataSet);
procedure CheckOkButton;
function AnyRoomClassSelected: Boolean;
function GetRoomClassList: String;
procedure BuildResultList;
function LocateChannelManagerByChannelId(chId: Integer): Boolean;
{ Private declarations }
public
{ Public declarations }
toChannel,
planId,
channelManager : Integer;
directConnection : Boolean;
copyPerformed : Boolean;
channelManagers : TRoomerDataset;
end;
// TODO: thsi global var is Only used in TfrmChannelAvailabilityManager.AssignChanges, should be made a resuling object from CopyChannelSettings(
var ValueList : TChannelValueEnityList;
function CopyChannelSettings(var _toChannel : Integer; _channelManager, _planId : Integer) : Boolean;
implementation
{$R *.dfm}
uses uAppGlobal
, uD
, hData
, uDateUtils
, uStringUtils
;
function CopyChannelSettings(var _toChannel : Integer; _channelManager, _planId : Integer) : Boolean;
var
lFrmChannelCopyFrom: TFrmChannelCopyFrom;
begin
lFrmChannelCopyFrom := TFrmChannelCopyFrom.Create(nil);
try
lFrmChannelCopyFrom.toChannel := _toChannel;
lFrmChannelCopyFrom.channelManager := _channelManager;
lFrmChannelCopyFrom.planId := _planId;
lFrmChannelCopyFrom.ShowModal;
result := lFrmChannelCopyFrom.copyPerformed;
_toChannel := lFrmChannelCopyFrom.toChannel;
finally
lFrmChannelCopyFrom.Free;
end;
end;
function TFrmChannelCopyFrom.GetRoomClassList : String;
var i : Integer;
begin
result := '';
for i := ComponentCount - 1 downto 0 do
if Components[i] IS TsCheckBox then // 1234567890123
if copy((Components[i] AS TsCheckBox).Name, 1, 13) = 'cbxRoomClass_' then
if (Components[i] AS TsCheckBox).Checked then
begin
if result = '' then
result := inttostr((Components[i] AS TsCheckBox).Tag)
else
result := result + ',' + inttostr((Components[i] AS TsCheckBox).Tag);
end;
end;
procedure TFrmChannelCopyFrom.BtnOkClick(Sender: TObject);
begin
BuildResultList;
toChannel := TIntValue(cbxDestChannel.Items.Objects[cbxDestChannel.ItemIndex]).value;
CopyPerformed := True;
Close;
end;
procedure TFrmChannelCopyFrom.BuildResultList;
var roomClassList : String;
rSet : TRoomerDataSet;
begin
roomClassList := GetRoomClassList;
rSet := createNewDataSet;
rSet_bySQL(rSet,
format('SELECT ' +
'cr1.date, ' +
'cr1.roomClassId, ' +
'cr1.price, ' +
'cr1.availability, ' +
'cr1.minStay, ' +
'cr1.maxStay, ' +
'cr1.closedOnArrival, ' +
'cr1.closedOnDeparture, ' +
'cr1.stop, ' +
'cr1.lengthOfStayArrivalDateBased, ' +
'cr1.singleUsePrice ' +
'FROM channelrates cr, ' +
' (SELECT * FROM channelrates ' +
' WHERE channelId=%d ' +
' AND (roomClassId IN (%s)) AND (date>=''%s'' AND date<=''%s'')) cr1 ' +
'WHERE cr.channelId=%d ' +
'AND (cr.date>=''%s'' AND cr.date<=''%s'') ' +
'AND cr.channelManager=%d ' +
'AND cr.planCodeId=%d ' +
'AND (cr.roomClassId IN (%s)) ' +
'AND cr1.roomClassId=cr.roomClassID ' +
'AND cr1.date=cr.date ' +
'AND cr1.planCodeId=cr.planCodeId',
[TIntValue(cbxSourceChannel.Items.Objects[cbxSourceChannel.ItemIndex]).value,
roomClassList, uDateUtils.dateToSqlString(dtBulkFrom.Date), uDateUtils.dateToSqlString(dtBulkTo.Date),
toChannel, uDateUtils.dateToSqlString(dtBulkFrom.Date), uDateUtils.dateToSqlString(dtBulkTo.Date),
channelManager, planId,
roomClassList]));
ValueList.Clear;
rSet.First;
while NOT rSet.Eof do
begin
if cbxRate.Checked then
ValueList.Add(TChannelValueEnity.create(vtPrice, rSet['date'], rSet['roomClassId'], rSet['price']));
if cbxAvailabilities.Checked then
ValueList.Add(TChannelValueEnity.create(vtAvailability, rSet['date'], rSet['roomClassId'], rSet['availability']));
if cbxRestrictions.Checked then
begin
ValueList.Add(TChannelValueEnity.create(vtMinStay, rSet['date'], rSet['roomClassId'], rSet['minStay']));
ValueList.Add(TChannelValueEnity.create(vtMaxStay, rSet['date'], rSet['roomClassId'], rSet['maxStay']));
ValueList.Add(TChannelValueEnity.create(vtClosedOnArrival, rSet['date'], rSet['roomClassId'], rSet['closedOnArrival']));
ValueList.Add(TChannelValueEnity.create(vtClosedOnDeparture, rSet['date'], rSet['roomClassId'], rSet['closedOnDeparture']));
ValueList.Add(TChannelValueEnity.create(vtStop, rSet['date'], rSet['roomClassId'], rSet['stop']));
ValueList.Add(TChannelValueEnity.create(vtLengthOfStayArrivalDateBased, rSet['date'], rSet['roomClassId'], rSet['lengthOfStayArrivalDateBased']));
ValueList.Add(TChannelValueEnity.create(vtSingleUsePrice, rSet['date'], rSet['roomClassId'], rSet['singleUsePrice']));
end;
rSet.Next;
end;
end;
procedure TFrmChannelCopyFrom.cbxSourceChannelChange(Sender: TObject);
begin
if (cbxSourceChannel.ItemIndex < 0) OR
(cbxDestChannel.ItemIndex < 0) then
exit;
pnlParameters.Visible := assigned(cbxSourceChannel.Items.Objects[cbxSourceChannel.ItemIndex]) AND
assigned(cbxDestChannel.Items.Objects[cbxDestChannel.ItemIndex]) AND
(TIntValue(cbxSourceChannel.Items.Objects[cbxSourceChannel.ItemIndex]).value <>
TIntValue(cbxDestChannel.Items.Objects[cbxDestChannel.ItemIndex]).value);
if pnlParameters.Visible then
begin
cbxRate.Checked := False;
cbxAvailabilities.Checked := False;
cbxRestrictions.Checked := False;
toChannel := TIntValue(cbxDestChannel.Items.Objects[cbxDestChannel.ItemIndex]).value;
if LocateChannelManagerByChannelId(toChannel) then
begin
channelManager := channelManagers['id'];
directConnection := channelManagers['directConnection'];
end;
cbxAvailabilities.Visible := directConnection;
ListRoomClasses(TIntValue(cbxSourceChannel.Items.Objects[cbxSourceChannel.ItemIndex]).value);
end;
end;
procedure TFrmChannelCopyFrom.cbxRateClick(Sender: TObject);
begin
CheckOkButton;
end;
procedure TFrmChannelCopyFrom.cbxRoomClass_Click(Sender: TObject);
begin
CheckOkButton;
end;
procedure TFrmChannelCopyFrom.CheckOkButton;
begin
BtnOk.Enabled := AnyRoomClassSelected AND
(cbxRate.Checked OR cbxAvailabilities.Checked OR cbxRestrictions.Checked) AND
((Trunc(dtBulkFrom.Date) < Trunc(dtBulkTo.Date)) AND (Trunc(dtBulkFrom.Date) >= Trunc(Now)));
end;
procedure TFrmChannelCopyFrom.dtBulkFromAcceptDate(Sender: TObject; var aDate: TDateTime; var CanAccept: Boolean);
begin
CheckOkButton;
end;
procedure TFrmChannelCopyFrom.dtBulkToCloseUp(Sender: TObject);
begin
CheckOkButton;
end;
function TFrmChannelCopyFrom.AnyRoomClassSelected : Boolean;
var i : Integer;
begin
result := False;
for i := ComponentCount - 1 downto 0 do
if Components[i] IS TsCheckBox then // 1234567890123
if copy((Components[i] AS TsCheckBox).Name, 1, 13) = 'cbxRoomClass_' then
if (Components[i] AS TsCheckBox).Checked then
begin
result := True;
Break;
end;
end;
procedure TFrmChannelCopyFrom.RemoveRoomClassList;
var i : Integer;
begin
for i := ComponentCount - 1 downto 0 do
if Components[i] IS TsCheckBox then // 1234567890123
if copy((Components[i] AS TsCheckBox).Name, 1, 13) = 'cbxRoomClass_' then
(Components[i] AS TsCheckBox).Free;
end;
procedure TFrmChannelCopyFrom.AddRoomClassToList(chClassRel : TRoomerDataSet);
var box : TsCheckBox;
begin
if glb.LocateSpecificRecord('roomtypegroups', 'id', chClassRel.fieldByName('roomClassId').AsInteger) then
if glb.RoomTypeGroups['Active'] then
begin
box := TsCheckBox.Create(self);
box.ParentFont := False;
box.Parent := sbRoomClasses;
box.Align := alTop;
box.Top := 1000;
box.Tag := chClassRel['roomClassId'];
box.Name := 'cbxRoomClass_' + inttostr(chClassRel['roomClassId']);
box.Caption := format('[%s] %s', [glb.RoomTypeGroups['Code'], glb.RoomTypeGroups['Description']]);
box.OnClick := cbxRoomClass_Click;
end;
end;
procedure TFrmChannelCopyFrom.ListRoomClasses(channel : Integer);
var chClassRel : TRoomerDataSet;
begin
RemoveRoomClassList;
chClassRel := createNewDataSet;
rSet_bySQL(chClassRel,
format('SELECT * FROM channelclassrelations cr WHERE cr.channelId=%d ' +
'AND NOT ISNULL((SELECT id FROM channelclassrelations cr1 WHERE cr1.channelId=%d AND cr1.roomClassId=cr.roomClassId))',
[channel, toChannel]));
chClassRel.First;
while NOT chClassRel.Eof do
begin
AddRoomClassToList(chClassRel);
chClassRel.Next;
end;
end;
procedure TFrmChannelCopyFrom.FormCreate(Sender: TObject);
begin
copyPerformed := False;
channelManagers := createNewDataSet;
rSet_bySQL(channelManagers, 'SELECT * FROM channelmanagers WHERE active');
end;
procedure TFrmChannelCopyFrom.FormDestroy(Sender: TObject);
begin
RemoveRoomClassList;
end;
procedure TFrmChannelCopyFrom.FormShow(Sender: TObject);
begin
ListChannels;
end;
function TFrmChannelCopyFrom.LocateChannelManagerByChannelId(chId : Integer) : Boolean;
var
lSplit: TStrings;
begin
result := False;
channelManagers.First;
while (NOT channelManagers.Eof) do
begin
lSPlit := SplitStringToTStrings(',', channelManagers['channels']);
try
if lSplit.IndexOf(inttostr(chId)) >= 0 then
begin
result := True;
Break;
end;
finally
lSplit.Free;
end;
channelManagers.Next;
end;
end;
procedure TFrmChannelCopyFrom.ListChannels;
begin
cbxSourceChannel.Clear;
cbxSourceChannel.Items.AddObject('...', nil);
cbxDestChannel.Clear;
cbxDestChannel.Items.AddObject('...', nil);
glb.ChannelsSet.First;
while NOT glb.ChannelsSet.Eof do
begin
if glb.ChannelsSet['active'] then // AND (glb.ChannelsSet['id'] <> toChannel) then
begin
cbxSourceChannel.Items.AddObject(format('[%s] %s', [glb.ChannelsSet['channelManagerId'], glb.ChannelsSet['name']]),
TIntValue.Create(glb.ChannelsSet['id']));
if LocateChannelManagerByChannelId(glb.ChannelsSet['id']) then
begin
if channelManager = channelManagers['id'] then
cbxDestChannel.Items.AddObject(format('[%s] %s', [glb.ChannelsSet['channelManagerId'], glb.ChannelsSet['name']]),
TIntValue.Create(glb.ChannelsSet['id']));
end;
end;
glb.ChannelsSet.Next;
end;
end;
{ TIntValue }
constructor TIntValue.create(_value: Integer);
begin
inherited Create;
value := _value;
end;
{ TChannelValueEnity }
constructor TChannelValueEnity.create(_ValueType: TValueType; _ADate: TDateTime; _roomClassID: Integer; _value: Variant);
begin
ValueType := _ValueType;
ADate := _Adate;
roomClassID := _roomClassID;
value := _value;
end;
initialization
ValueList := TChannelValueEnityList.Create(True);
finalization
ValueList.Free;
end.