@@ -8,8 +8,8 @@ contract Abab is Ownable,Claimable,StandardToken {
8
8
uint constant maxUInt = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ;
9
9
uint constant error = maxUInt;
10
10
11
- // event log(string s);
12
- // event log2 (string s, uint i);
11
+ event log (string s );
12
+ event logUint (string s , uint i );
13
13
14
14
struct Schedule {
15
15
uint from;
@@ -20,14 +20,16 @@ contract Abab is Ownable,Claimable,StandardToken {
20
20
uint currency; // see Currencies array
21
21
}
22
22
23
+ enum BookingStatus { New, Agreed, Cancel, Complete } // TODO Complete?
24
+
23
25
struct BookingRecord {
24
26
address guest;
25
27
uint from;
26
28
uint to;
27
29
uint totalCost;
28
30
uint currency;
29
31
uint ababCoinTotalCost;
30
- uint status; //TODO change type to enum or short //status 0-new; 1-appruve ; 2 - cancel ; 3 - complete
32
+ BookingStatus status;
31
33
}
32
34
33
35
struct Room {
@@ -91,7 +93,7 @@ contract Abab is Ownable,Claimable,StandardToken {
91
93
public
92
94
returns (uint roomIndex )
93
95
{
94
- return UpsertRoom (_roomIndex, _roomDescriptionHash, msg .sender , _partner, _partnerPPM, _minNightCount, _timeForBooking);
96
+ return UpsertRoom (_roomIndex, _roomDescriptionHash, msg .sender , _partner, _partnerPPM, _minNightCount, _timeForBooking);
95
97
}
96
98
97
99
function UpsertRoomFromPartner (
@@ -150,7 +152,7 @@ contract Abab is Ownable,Claimable,StandardToken {
150
152
{
151
153
return rooms[msg .sender ][_roomIndex].roomDescriptionHash;
152
154
}
153
-
155
+
154
156
function RemoveRoomFromPartner (address _host , uint _roomIndex )
155
157
public
156
158
{
@@ -277,41 +279,41 @@ contract Abab is Ownable,Claimable,StandardToken {
277
279
278
280
event NewBooking (address indexed host , uint roomIndex , uint bookingIndex );
279
281
event UpdateBooking (address indexed host , uint roomIndex , uint bookingIndex );
280
-
281
- function Timestamp2Daystamp (uint timestamp )
282
+
283
+ function GetDate (uint timestamp )
282
284
public constant
283
285
returns (uint result )
284
286
{
285
287
return timestamp/ 1 days ;
286
288
}
287
-
288
- function GetBlockDayStamp ( )
289
- public
289
+
290
+ function GetTime ( uint datetime )
291
+ public constant
290
292
returns (uint result )
291
293
{
292
- return Timestamp2Daystamp (now );
294
+ uint dayCount = datetime / 1 days ;
295
+ return datetime - 1 days * dayCount;
293
296
}
294
-
295
- function DateIsFree (address _host , uint _roomIndex , uint _from , uint _to )
297
+
298
+ function DateIsFree (address _host , uint _roomIndex , uint _from , uint _to , uint nowDateTime )
296
299
public constant
297
300
returns (bool result )
298
301
{
299
- require (_to>= _from);
300
-
301
302
var room = rooms[_host][_roomIndex];
303
+ var nowDate = GetDate (nowDateTime);
304
+
305
+ if ((_from + room.minNightCount) > _to) return false ;
306
+ if (_from < nowDate) return false ;
307
+ if ((_from == nowDate) && (nowDate >= room.timeForBooking)) return false ;
302
308
303
309
//check, that this date don't booking
304
310
uint i = room.bookingLength;
305
- while (i> 0 ){
311
+ while (i> 0 ) {
306
312
-- i;
307
313
var booking_i = room.booking[i];
308
- if (booking_i.to > GetBlockDayStamp ())
309
- return true ;
310
- if (! (
311
- ((booking_i.to < _to) && (booking_i.from < _from))
312
- ||
313
- ((booking_i.to > _to) && (booking_i.from > _from))
314
- )) return false ;
314
+ if (booking_i.status > BookingStatus.Agreed) continue ;
315
+ if (booking_i.to < nowDate) continue ;
316
+ if (! ((booking_i.from > _to)|| (booking_i.to <= _from))) return false ;
315
317
}
316
318
return true ;
317
319
}
@@ -325,14 +327,15 @@ contract Abab is Ownable,Claimable,StandardToken {
325
327
if ((arg2< arg1)&& (arg2< arg3)) return arg2;
326
328
return arg3;
327
329
}
328
-
329
- function CalcTotalCost (address _host , uint _roomIndex , uint _from , uint _to )
330
- public
331
- constant
332
- returns (uint result )
330
+
331
+ function CalcTotalCost (address _host , uint _roomIndex , uint _from , uint _to , uint _nowDateTime )
332
+ public constant
333
+ returns (uint totalCost )
333
334
{
335
+ totalCost = 0 ;
336
+
334
337
//check, that this date don't booking
335
- if (! DateIsFree (_host, _roomIndex, _from, _to)) return 0 ;
338
+ if (! DateIsFree (_host, _roomIndex, _from, _to, _nowDateTime )) return 0 ;
336
339
337
340
var room = rooms[_host][_roomIndex];
338
341
@@ -341,7 +344,6 @@ contract Abab is Ownable,Claimable,StandardToken {
341
344
uint needFrom = _from;
342
345
uint nextFrom = maxUInt;
343
346
uint daysCount = _to- _from;
344
- uint totalCost = 0 ;
345
347
346
348
// log2('needFrom=' ,needFrom);
347
349
// log2('nextFrom=' ,nextFrom);
@@ -385,21 +387,34 @@ contract Abab is Ownable,Claimable,StandardToken {
385
387
386
388
function Booking (address _host , uint _roomIndex , uint _from , uint _to )
387
389
public
388
- returns (bool result ) // TODO или удалить или возвращать
389
390
{
390
- if (_from<= GetBlockDayStamp ()) return false ;
391
-
391
+ if (_from < GetDate (now )) return ;
392
392
var room = rooms[_host][_roomIndex];
393
- uint totalCost = CalcTotalCost (_host, _roomIndex, _from, _to);
394
- if ( totalCost> 0 ){
395
- uint _bookingIndex = room.bookingLength;
396
- room.booking[ _bookingIndex ] = BookingRecord ( msg . sender , _from, _to, totalCost, 0 , totalCost, 0 );
397
- room.bookingLength = _bookingIndex + 1 ;
398
- NewBooking (_host, _roomIndex, _bookingIndex); // TODO расчёт в AbabCoin
393
+
394
+ uint totalCost = CalcTotalCost (_host, _roomIndex, _from, _to, now );
395
+ if (totalCost > 0 ) {
396
+ NewBooking (_host, _roomIndex, room.bookingLength );
397
+ room.booking[ room. bookingLength ] = BookingRecord ( msg . sender , _from, _to, totalCost, 0 , totalCost, BookingStatus.New) ;
398
+ room.bookingLength = room.bookingLength + 1 ;
399
399
}
400
400
}
401
-
402
- function ApproveBooking (address _host , uint _roomIndex , uint _bookingIndex )
401
+
402
+ function GetBookingLength (address _host , uint _roomIndex )
403
+ public constant
404
+ returns (uint result )
405
+ {
406
+ return rooms[_host][_roomIndex].bookingLength;
407
+ }
408
+
409
+ function GetBooking (address _host , uint _roomIndex , uint _bookingIndex )
410
+ public constant
411
+ returns (address guest , uint from , uint to , uint totalCost , uint currency , uint ababCoinTotalCost , BookingStatus status )
412
+ {
413
+ var b = rooms[_host][_roomIndex].booking[_bookingIndex];
414
+ return (b.guest, b.from, b.to, b.totalCost, b.currency, b.ababCoinTotalCost, b.status);
415
+ }
416
+
417
+ function AgreeBooking (address _host , uint _roomIndex , uint _bookingIndex )
403
418
public
404
419
{
405
420
if (_roomIndex >= rooms[_host].length )
@@ -408,8 +423,8 @@ contract Abab is Ownable,Claimable,StandardToken {
408
423
if ((room.partner != msg .sender ) && (_host != msg .sender ) )
409
424
return ;
410
425
411
- if (room.booking[_bookingIndex].status == 0 ){
412
- room.booking[_bookingIndex].status = 1 ;
426
+ if (room.booking[_bookingIndex].status == BookingStatus.New ){
427
+ room.booking[_bookingIndex].status = BookingStatus.Agreed ;
413
428
UpdateBooking (_host, _roomIndex, _bookingIndex);
414
429
}
415
430
}
@@ -423,8 +438,8 @@ contract Abab is Ownable,Claimable,StandardToken {
423
438
if ((room.partner != msg .sender ) && (_host != msg .sender ) && (room.booking[_bookingIndex].guest != msg .sender ) )
424
439
return ;
425
440
426
- if ((room.booking[_bookingIndex].status == 0 ) || (room.booking[_bookingIndex].status == 1 )){
427
- room.booking[_bookingIndex].status = 1 ;
441
+ if ((room.booking[_bookingIndex].status == BookingStatus.New ) || (room.booking[_bookingIndex].status == BookingStatus.Agreed )){
442
+ room.booking[_bookingIndex].status = BookingStatus.Cancel ;
428
443
UpdateBooking (_host, _roomIndex, _bookingIndex);
429
444
}
430
445
}
0 commit comments