Skip to content

Commit a381b29

Browse files
committed
DateIsFree bugfixing
1 parent 33bc3e0 commit a381b29

File tree

5 files changed

+296
-145
lines changed

5 files changed

+296
-145
lines changed

contracts/contracts/Abab.sol

+60-45
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ contract Abab is Ownable,Claimable,StandardToken {
88
uint constant maxUInt = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
99
uint constant error = maxUInt;
1010

11-
// event log(string s);
12-
// event log2(string s, uint i);
11+
event log(string s);
12+
event logUint(string s, uint i);
1313

1414
struct Schedule {
1515
uint from;
@@ -20,14 +20,16 @@ contract Abab is Ownable,Claimable,StandardToken {
2020
uint currency; // see Currencies array
2121
}
2222

23+
enum BookingStatus { New, Agreed, Cancel, Complete } // TODO Complete?
24+
2325
struct BookingRecord {
2426
address guest;
2527
uint from;
2628
uint to;
2729
uint totalCost;
2830
uint currency;
2931
uint ababCoinTotalCost;
30-
uint status; //TODO change type to enum or short //status 0-new; 1-appruve ; 2 - cancel ; 3 - complete
32+
BookingStatus status;
3133
}
3234

3335
struct Room {
@@ -91,7 +93,7 @@ contract Abab is Ownable,Claimable,StandardToken {
9193
public
9294
returns (uint roomIndex)
9395
{
94-
return UpsertRoom(_roomIndex, _roomDescriptionHash, msg.sender, _partner, _partnerPPM, _minNightCount, _timeForBooking);
96+
return UpsertRoom(_roomIndex, _roomDescriptionHash, msg.sender, _partner, _partnerPPM, _minNightCount, _timeForBooking);
9597
}
9698

9799
function UpsertRoomFromPartner(
@@ -150,7 +152,7 @@ contract Abab is Ownable,Claimable,StandardToken {
150152
{
151153
return rooms[msg.sender][_roomIndex].roomDescriptionHash;
152154
}
153-
155+
154156
function RemoveRoomFromPartner(address _host, uint _roomIndex)
155157
public
156158
{
@@ -277,41 +279,41 @@ contract Abab is Ownable,Claimable,StandardToken {
277279

278280
event NewBooking (address indexed host, uint roomIndex, uint bookingIndex);
279281
event UpdateBooking (address indexed host, uint roomIndex, uint bookingIndex);
280-
281-
function Timestamp2Daystamp(uint timestamp)
282+
283+
function GetDate(uint timestamp)
282284
public constant
283285
returns(uint result)
284286
{
285287
return timestamp/1 days;
286288
}
287-
288-
function GetBlockDayStamp()
289-
public
289+
290+
function GetTime(uint datetime)
291+
public constant
290292
returns(uint result)
291293
{
292-
return Timestamp2Daystamp(now);
294+
uint dayCount = datetime / 1 days;
295+
return datetime - 1 days * dayCount;
293296
}
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)
296299
public constant
297300
returns(bool result)
298301
{
299-
require(_to>=_from);
300-
301302
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;
302308

303309
//check, that this date don't booking
304310
uint i = room.bookingLength;
305-
while(i>0){
311+
while(i>0) {
306312
--i;
307313
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;
315317
}
316318
return true;
317319
}
@@ -325,14 +327,15 @@ contract Abab is Ownable,Claimable,StandardToken {
325327
if((arg2<arg1)&&(arg2<arg3)) return arg2;
326328
return arg3;
327329
}
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)
333334
{
335+
totalCost = 0;
336+
334337
//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;
336339

337340
var room = rooms[_host][_roomIndex];
338341

@@ -341,7 +344,6 @@ contract Abab is Ownable,Claimable,StandardToken {
341344
uint needFrom = _from;
342345
uint nextFrom = maxUInt;
343346
uint daysCount = _to-_from;
344-
uint totalCost = 0;
345347

346348
// log2('needFrom=' ,needFrom);
347349
// log2('nextFrom=' ,nextFrom);
@@ -385,21 +387,34 @@ contract Abab is Ownable,Claimable,StandardToken {
385387

386388
function Booking(address _host, uint _roomIndex, uint _from, uint _to)
387389
public
388-
returns(bool result) // TODO или удалить или возвращать
389390
{
390-
if (_from<=GetBlockDayStamp()) return false;
391-
391+
if (_from < GetDate(now)) return;
392392
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;
399399
}
400400
}
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)
403418
public
404419
{
405420
if (_roomIndex >= rooms[_host].length)
@@ -408,8 +423,8 @@ contract Abab is Ownable,Claimable,StandardToken {
408423
if((room.partner != msg.sender) && (_host != msg.sender) )
409424
return;
410425

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;
413428
UpdateBooking(_host, _roomIndex, _bookingIndex);
414429
}
415430
}
@@ -423,8 +438,8 @@ contract Abab is Ownable,Claimable,StandardToken {
423438
if((room.partner != msg.sender) && (_host != msg.sender) && (room.booking[_bookingIndex].guest != msg.sender) )
424439
return;
425440

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;
428443
UpdateBooking(_host, _roomIndex, _bookingIndex);
429444
}
430445
}

contracts/contracts/zeppelin-solidity/ownership/Claimable.sol

-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
pragma solidity ^0.4.11;
22

3-
43
import './Ownable.sol';
54

6-
75
/**
86
* @title Claimable
97
* @dev Extension for the Ownable contract, where the ownership needs to be claimed.

contracts/test/TestAbab.Booking.js

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
var Abab = artifacts.require("Abab");
2+
3+
contract('TestAbab.Booking', function(accounts) {
4+
var abab;
5+
6+
it("check booking", function() {
7+
return Abab.deployed().then(function(instance) {
8+
abab = instance;
9+
10+
abab.UpsertRoomFromHost(0,0,0,0,1,0);
11+
abab.UpsertSchedule(0, 0, 8000000010, 8000000100, 10, 5, 1, 0);
12+
13+
abab.Booking(accounts[0], 0, 8000000010, 8000000100, {from: accounts[1]});
14+
15+
return abab.GetBookingLength.call(accounts[0], 0);
16+
}).then(function(result){
17+
assert.equal(result.toNumber(), 1, "check GetBookingLength");
18+
19+
return abab.GetBooking.call(accounts[0], 0, 0);
20+
}).then(function(result){
21+
assert.equal(result[0], accounts[1], "check guest");
22+
assert.equal(result[1].toNumber(), 8000000010, "check from");
23+
assert.equal(result[2].toNumber(), 8000000100, "check to");
24+
assert.equal(result[3].toNumber(), 90, "check totalCost");
25+
assert.equal(result[4].toNumber(), 0, "check currency");
26+
assert.equal(result[5].toNumber(), 90, "check ababCoinTotalCost");
27+
assert.equal(result[6].toNumber(), 0, "check status");
28+
29+
return abab.DateIsFree(accounts[0], 0, 8000000000, 8000000001, (8000000000-1000)*24*60*60);
30+
}).then(function(result){
31+
assert.equal(result, true, "check guest DateIsFree1");
32+
33+
return abab.DateIsFree(accounts[0], 0, 8000000001, 8000000000, (8000000000-1000)*24*60*60);
34+
}).then(function(result){
35+
assert.equal(result, false, "check guest DateIsFree2");
36+
37+
return abab.DateIsFree(accounts[0], 0, 8000000100, 8000000101, (8000000000-1000)*24*60*60);
38+
}).then(function(result){
39+
assert.equal(result, true, "check guest DateIsFree3");
40+
41+
// abab.DateIsFree(accounts[0], 0, 8000000009, 8000000011, (8000000000-1000)*24*60*60);
42+
return abab.DateIsFree(accounts[0], 0, 8000000009, 8000000011, (8000000000-1000)*24*60*60);
43+
}).then(function(result){
44+
assert.equal(result, false, "check guest DateIsFree4");
45+
46+
return abab.DateIsFree(accounts[0], 0, 8000000010, 8000000100, (8000000000-1000)*24*60*60);
47+
}).then(function(result){
48+
assert.equal(result, false, "check guest DateIsFree5");
49+
50+
return abab.DateIsFree(accounts[0], 0, 8000000099, 8000000100, (8000000000-1000)*24*60*60);
51+
}).then(function(result){
52+
assert.equal(result, false, "check guest DateIsFree6");
53+
54+
});
55+
});
56+
57+
it("check ApproveBooking CancelBooking", function() {
58+
return Abab.deployed().then(function(instance) {
59+
abab = instance;
60+
61+
abab.UpsertRoomFromHost(1, 0, 0, 0, 1, 0);
62+
abab.UpsertSchedule(1, 0, 10, 100, 10, 5, 1, 0);
63+
abab.Booking(accounts[0], 1, 10, 100, {from: accounts[1]});
64+
65+
return abab.GetBooking.call(accounts[0], 1, 0);
66+
}).then(function(result){
67+
assert.equal(result[6].toNumber(), 0, "check new status");
68+
69+
return abab.DateIsFree(accounts[0], 0, 10, 100, 0);
70+
}).then(function(result){
71+
assert.equal(result, false, "check agree DateIsFree == false");
72+
73+
abab.ApproveBooking(accounts[0], 1, 0);
74+
75+
return abab.GetBooking.call(accounts[0], 1, 0);
76+
}).then(function(result){
77+
assert.equal(result[6].toNumber(), 1, "check approve status");
78+
79+
return abab.DateIsFree(accounts[0], 0, 10, 100, 0);
80+
}).then(function(result){
81+
assert.equal(result, false, "check agree DateIsFree == false");
82+
83+
abab.CancelBooking(accounts[0], 1, 0);
84+
85+
return abab.GetBooking.call(accounts[0], 1, 0);
86+
}).then(function(result){
87+
assert.equal(result[6].toNumber(), 2, "check cancel status");
88+
89+
return abab.DateIsFree(accounts[0], 0, 10, 100, 0);
90+
}).then(function(result){
91+
assert.equal(result, true, "check canceled free DateIsFree");
92+
93+
});
94+
});
95+
});

0 commit comments

Comments
 (0)