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