diff --git a/pms/models/pms_reservation.py b/pms/models/pms_reservation.py index 6eecc4195..c123e95a5 100644 --- a/pms/models/pms_reservation.py +++ b/pms/models/pms_reservation.py @@ -1255,6 +1255,20 @@ class PmsReservation(models.Model): ) ) + @api.constrains("reservation_line_ids") + def check_consecutive_dates(self): + """ + simply convert date objects to integers using the .toordinal() method + of datetime objects. The difference between the maximum and minimum value + of the set of ordinal dates is one more than the length of the set + """ + for record in self: + if record.reservation_line_ids and len(record.reservation_line_ids) > 1: + dates = record.reservation_line_ids.mapped("date") + date_ints = {d.toordinal() for d in dates} + if not (max(date_ints) - min(date_ints) == len(date_ints) - 1): + raise ValidationError(_("Reservation dates should be consecutives")) + # @api.constrains("checkin_partner_ids", "adults") # def _max_checkin_partner_ids(self): # for record in self: diff --git a/pms/tests/test_pms_reservation.py b/pms/tests/test_pms_reservation.py index c54c509da..42466fc05 100644 --- a/pms/tests/test_pms_reservation.py +++ b/pms/tests/test_pms_reservation.py @@ -150,6 +150,84 @@ class TestPmsReservations(common.SavepointCase): } ) + @freeze_time("1980-11-01") + def test_reservation_dates_not_consecutive(self): + """ + Check the constrain if not consecutive dates + ---------------- + Create correct reservation set 3 reservation lines consecutives (nights) + """ + # ARRANGE + self.create_common_scenario() + customer = self.env.ref("base.res_partner_12") + today = fields.date.today() + tomorrow = fields.date.today() + datetime.timedelta(days=1) + three_days_later = fields.date.today() + datetime.timedelta(days=3) + + # ACT & ASSERT + with self.assertRaises( + ValidationError, + msg="Error, it has been allowed to create a reservation with non-consecutive days", + ): + self.env["pms.reservation"].create( + { + "room_type_id": self.room_type_double.id, + "partner_id": customer.id, + "pms_property_id": self.property.id, + "reservation_line_ids": [ + (0, False, {"date": today}), + (0, False, {"date": tomorrow}), + (0, False, {"date": three_days_later}), + ], + } + ) + + @freeze_time("1980-11-01") + def test_reservation_dates_compute_checkin_out(self): + """ + Check the reservation creation with specific reservation lines + anc compute checkin checkout + ---------------- + Create reservation with correct reservation lines and check + the checkin and checkout fields. Take into account that the + checkout of the reservation must be the day after the last night + (view checkout assertEqual) + """ + # ARRANGE + self.create_common_scenario() + customer = self.env.ref("base.res_partner_12") + today = fields.date.today() + tomorrow = fields.date.today() + datetime.timedelta(days=1) + two_days_later = fields.date.today() + datetime.timedelta(days=2) + + # ACT + reservation = self.env["pms.reservation"].create( + { + "room_type_id": self.room_type_double.id, + "partner_id": customer.id, + "pms_property_id": self.property.id, + "reservation_line_ids": [ + (0, False, {"date": today}), + (0, False, {"date": tomorrow}), + (0, False, {"date": two_days_later}), + ], + } + ) + + # ASSERT + self.assertEqual( + reservation.checkin, + today, + "The calculated checkin of the reservation does \ + not correspond to the first day indicated in the dates", + ) + self.assertEqual( + reservation.checkout, + two_days_later + datetime.timedelta(days=1), + "The calculated checkout of the reservation does \ + not correspond to the last day indicated in the dates", + ) + @freeze_time("1980-11-01") def test_create_reservation_start_date(self): # TEST CASE