@ -43,6 +43,7 @@ class RestaurantBooking(models.Model):
datetime_start = fields . Datetime ( string = " Date start " , compute = " _get_datetimes " , store = True )
datetime_start = fields . Datetime ( string = " Date start " , compute = " _get_datetimes " , store = True )
datetime_stop = fields . Datetime ( string = " Date stop " , compute = " _get_datetimes " , store = True )
datetime_stop = fields . Datetime ( string = " Date stop " , compute = " _get_datetimes " , store = True )
count = fields . Integer ( string = " People count " , required = True )
count = fields . Integer ( string = " People count " , required = True )
can_overlap = fields . Boolean ( string = " Table bookings can overlap " )
available_table_ids = fields . Many2many ( comodel_name = ' restaurant.table ' , string = " Available tables " ,
available_table_ids = fields . Many2many ( comodel_name = ' restaurant.table ' , string = " Available tables " ,
column1 = " booking_id " , column2 = " table_id " , relation = " booking_available_table_rel " ,
column1 = " booking_id " , column2 = " table_id " , relation = " booking_available_table_rel " ,
compute = " _get_available_tables " )
compute = " _get_available_tables " )
@ -101,22 +102,22 @@ class RestaurantBooking(models.Model):
for booking in self :
for booking in self :
booking . table_capacity_ok = bool ( booking . table_capacity > = booking . count )
booking . table_capacity_ok = bool ( booking . table_capacity > = booking . count )
@api.depends ( ' datetime_start ' , ' datetime_stop ' , ' table_ids ' )
@api.depends ( ' datetime_start ' , ' datetime_stop ' , ' can_overlap ' )
def _get_available_tables ( self ) :
def _get_available_tables ( self ) :
Table = self . env [ ' restaurant.table ' ]
Table = self . env [ ' restaurant.table ' ]
TableBooking = self . env [ ' restaurant.table_booking ' ]
TableBooking = self . env [ ' restaurant.table_booking ' ]
for booking in self :
for booking in self :
if booking . datetime_start and booking . datetime_stop :
if booking . datetime_start and booking . datetime_stop :
domain = [ ( ' bookable ' , ' = ' , True ) ]
if not self . can_overlap :
tbookings = TableBooking . search ( [
tbookings = TableBooking . search ( [
' | ' , ' & ' , ( ' datetime_start ' , ' < ' , booking . datetime_stop ) ,
' | ' , ' & ' , ( ' datetime_start ' , ' < ' , booking . datetime_stop ) ,
( ' datetime_start ' , ' >= ' , booking . datetime_start ) ,
( ' datetime_start ' , ' >= ' , booking . datetime_start ) ,
' & ' , ( ' datetime_stop ' , ' > ' , booking . datetime_start ) ,
' & ' , ( ' datetime_stop ' , ' > ' , booking . datetime_start ) ,
( ' datetime_stop ' , ' <= ' , booking . datetime_stop )
( ' datetime_stop ' , ' <= ' , booking . datetime_stop )
] )
] )
booking . available_table_ids = Table . search ( [
( ' bookable ' , ' = ' , True ) ,
( ' id ' , ' not in ' , tbookings . mapped ( ' table_id ' ) . ids ) ,
] ) - booking . table_ids
domain . append ( ( ' id ' , ' not in ' , tbookings . mapped ( ' table_id ' ) . ids ) )
booking . available_table_ids = Table . search ( domain )
else :
else :
booking . available_table_ids = [ ( 5 , 0 , 0 ) ]
booking . available_table_ids = [ ( 5 , 0 , 0 ) ]