-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathuAvailability.pas
386 lines (323 loc) · 8.7 KB
/
uAvailability.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
unit uAvailability;
interface
uses
Classes
, windows
, uAppGlobal
, ADODB
, dbTables
, DB
, SysUtils
, dialogs
, _glob
, ug
, ud
, cmpRoomerDataSet
, cmpRoomerConnection
, uUtils
;
Type
{$M+}
TDataCollection = class( TObject )
private
FRoomType : string;
FUsed : integer;
FNumber : integer;
public
constructor Create( RoomType : String );
destructor Destroy; override;
published
property RoomType : string read FRoomType;
property Used : integer read FUsed write FUsed;
property Number : integer read FNumber;
end;
{$M-}
TAvailability = class( TObject )
private
FDateFrom,
FDateTo : TDateTime;
FData : TList;
function GetNumItems : integer;
function GetDataByType( RoomType : string ) : TDataCollection;
function GetItems( idx : integer ) : TDataCollection;
procedure LoadData;
public
constructor Create( dtFrom, dtTo : TDateTime );
destructor Destroy; override;
procedure Clear;
function NumAvailable( RoomType : string ) : integer;
property NumItems : integer read GetNumItems;
property Items[ idx : integer ] : TDataCollection read GetItems;
property DataByType[ RoomType : string ] : TDataCollection read GetDataByType;
end;
procedure InitTypeAvailabilities( dtArrival, dtDeparture : TDateTime );
function GetTypeAvailabilities( sType : string ) : integer;
implementation
uses
uSqlDefinitions
, hData
, uRoomerDefinitions
, uReservationStateDefinitions
;
{ TDataCollection }
constructor TDataCollection.Create(RoomType : String);
begin
inherited Create;
FRoomType := RoomType;
end;
destructor TDataCollection.Destroy;
begin
inherited;
end;
{ TAvailability }
constructor TAvailability.Create( dtFrom, dtTo : TDateTime );
begin
inherited Create;
FDateFrom := dtFrom;
FDateTo := dtTo;
FData := TList.create;
LoadData;
end;
destructor TAvailability.Destroy;
begin
// --
Clear;
FData.free;
// --
inherited;
end;
procedure TAvailability.Clear;
var
i : integer;
begin
// --
for i := FData.count - 1 downto 0 do
begin
TDataCollection( FData[ i ] ).free;
FData.delete( i );
end;
end;
function TAvailability.GetNumItems : integer;
begin
// --
result := FData.count;
end;
function TAvailability.GetItems(idx : integer) : TDataCollection;
begin
// --
result := TDataCollection(FData[idx] );
end;
function TAvailability.GetDataByType( RoomType : string ) : TDataCollection;
var i : integer;
begin
// --
result := nil;
for i := 0 to NumItems - 1 do
begin
if Items[i].RoomType = RoomType then
begin
result := Items[ i ];
break;
end;
end;
end;
function TAvailability.NumAvailable( RoomType : string ) : integer;
var
aDataCollection : TDataCollection;
begin
// --
result := 0;
aDataCollection := DataByType[RoomType];
if aDataCollection = nil then exit;
result := aDataCollection.FNumber - aDataCollection.FUsed;
end;
procedure TAvailability.LoadData;
var
rSet : TRoomerDataSet;
aDataCollection : TDataCollection;
i : integer;
ALastDate,
ALastRoomType : string;
iCount : integer;
s : string;
sql : string;
begin
// --
for i := 0 to glb.RoomTypeCount - 1 do
begin
aDataCollection := TDataCollection.create(glb.GetRoomType(i));
aDataCollection.FNumber := glb.GetNumberOfItems(aDataCollection.FRoomType );
aDataCollection.FUsed := 0;
FData.add( aDataCollection );
end;
rSet := CreateNewDataSet;
try
rSet.CommandType := cmdText;
sql:=
'SELECT RoomType, ADate, ResFlag from roomsdate '+
' where (ADate >= %s '+
' and ADate < %s )'+
' AND (ResFlag <> '+_db(STATUS_DELETED)+' ) '+ //**zxhj bætt við
' ORDER BY ADate, RoomType ' ;
s := format(sql , [_DatetoDBDate(FDateFrom,true),_DatetoDBDate(FDateTo,true) ]);
hData.rSet_bySQL(rSet,s);
with rSet do
begin
first;
ALastDate := '';
ALastRoomType := '';
iCount := 0;
while not eof do
begin
//**zxhj Breytti ekkert hér ?????????
if ( trim( fieldByName('ResFlag').asString ) <> 'C' ) and
( trim( fieldByName( 'ResFlag' ).asString ) <> 'N' ) then
begin
// --
if ( ( ALastDate <> trim( fieldByName( 'ADate' ).asString ) ) or
( ALastRoomType <> trim( fieldByName( 'RoomType' ).asString ) ) ) and
( ( ALastDate <> '' ) and
( ALastRoomType <> '' ) ) then
begin
// --
aDataCollection := DataByType[ALastRoomType];
if aDataCollection <> nil then
begin
if aDataCollection.FUsed < iCount then
aDataCollection.FUsed := iCount;
end;
iCount := 0;
end;
// --
inc(iCount);
// --
ALastRoomType := trim( fieldByName( 'RoomType' ).asString );
ALastDate := trim( fieldByName( 'ADate' ).asString );
end;
// --
next;
// --
if eof then
begin
// --
aDataCollection := DataByType[ALastRoomType];
if aDataCollection <> nil then
begin
if aDataCollection.FUsed < iCount then
aDataCollection.FUsed := iCount;
end;
end;
end;
end;
finally
freeandnil(rSet);
end;
end;
function isNoRoom(s : string) : boolean;
begin
result := false;
if length(s) < 1 then exit;
result := copy(trim(s),1,1) = '<';
end;
procedure InitTypeAvailabilities( dtArrival, dtDeparture : TDateTime );
var
i : integer;
rSet : TRoomerDataSet;
sLastRoom : string;
function CheckNonRooms(SType : string ) : integer;
var
i,
l,
iMax : integer;
begin
// --
iMax := 0;
for i := trunc( dtArrival ) to trunc( dtDeparture ) - 1 do
begin
rSet.Filtered := false;
rSet.Filter := 'RoomType=' + quotedStr( sType ) + ' and ' +
'ADate=' + _DateToDBDate( i ,true );
rSet.Filtered := true;
try
with rSet do
begin
l := 0;
first;
while not eof do
begin
//**zxhj breytti ekki hér
if ((trim( fieldByName( 'ResFlag' ).asString ) <> 'N') and (trim( fieldByName( 'ResFlag' ).asString ) <> 'N')) then inc( l );
next;
end;
if l > iMax then iMax := l;
end;
finally
rSet.Filtered := false;
end;
end;
result := iMax;
end;
var
s : string;
sql : string;
begin
// Öll herbergin
glb.InitAvailability;
rSet := CreateNewDataSet;
try
rSet.CommandType := cmdText;
sql:=
'SELECT Room, RoomType, ADate, ResFlag from roomsdate '+
' WHERE (ADate >= %s '+
' and ADate < %s) '+
' AND (ResFlag <> '+_db(STATUS_DELETED)+' ) '+ //**zxhj bætti við
' ORDER BY Room, ADate, RoomType ' ;
s := format(sql, [_DatetoDBDate(dtArrival,true),_DatetoDBDate(dtDeparture,true)]);
hData.rSet_bySQL(rSet,s);
sLastRoom := '<None Yet>';
with rSet do
begin
first;
while not eof do
begin
if trim( fieldByName( 'ResFlag' ).asString ) <> 'N' then
if copy( trim( fieldByName( 'Room' ).asString ), 1, 1 ) <> '<' then
begin
if sLastRoom <> trim( fieldByName( 'Room' ).asString ) then
begin
sLastRoom := trim( fieldByName( 'Room' ).asString );
glb.NumAvailType.Values[ trim( fieldByName( 'RoomType' ).asString ) ] :=
inttostr(strtointdef(glb.NumAvailType.Values[trim(fieldByName('RoomType').asString)],0)-1);
end;
end;
next;
end;
end;
// //<Roomsdate>SELECT
// s := '';
// s := s+ 'select Room, RoomType, ADate, ResFlag from [RoomsDate]' ;
// s := s+ ' where ADate >= ' + _DatetoDBDate(dtArrival,true) ;
// s := s+ ' and ADate < ' + _DatetoDBDate(dtDeparture,true) ;
// s := s+ ' and Room >= ' + QuotedStr( '<' ) ;
// s := s+ ' and Room < ' + QuotedStr( '>' ) ;
// s := s+ ' ORDER BY Room, ADate, RoomType' ;
//
// s := format(select_InitTypeAvailabilities2 , [_DatetoDBDate(dtArrival,true),_DatetoDBDate(dtDeparture,true)]);
// CopyToClipboard(s);
// DebugMessage('select_InitTypeAvailabilities2'#10#10+s);
// hData.rSet_bySQL(rSet,s);
for i := 0 to glb.NumAvailType.count - 1 do
begin
glb.NumAvailType.Values[ glb.NumAvailType.Names[ i ] ] :=
inttostr(strtointdef(glb.NumAvailType.Values[glb.NumAvailType.Names[i]],0)-
CheckNonRooms(glb.NumAvailType.Names[i]));
end;
finally
freeandnil(rSet);
end;
end;
function GetTypeAvailabilities( sType : string ) : integer;
begin
result := strtointdef(glb.NumAvailType.Values[ sType ], 0 );
end;
end.