From 950b7846d30abe7e079886cf4b50e9c9bfb6de23 Mon Sep 17 00:00:00 2001 From: Dario Lodeiros Date: Tue, 15 Jun 2021 23:12:14 +0200 Subject: [PATCH] [FIX] compute commission percent --- pms/models/pms_reservation.py | 81 +++++++++++++++++++++++------------ pms/tests/test_pms_folio.py | 6 +-- 2 files changed, 57 insertions(+), 30 deletions(-) diff --git a/pms/models/pms_reservation.py b/pms/models/pms_reservation.py index d423b0ed1..6eecc4195 100644 --- a/pms/models/pms_reservation.py +++ b/pms/models/pms_reservation.py @@ -376,16 +376,18 @@ class PmsReservation(models.Model): checkin = fields.Date( string="Check In", help="It is the checkin date of the reservation, ", - required=True, - default=lambda self: self._get_default_checkin(), + compute="_compute_checkin", + readonly=False, + store=True, copy=False, tracking=True, ) checkout = fields.Date( string="Check Out", help="It is the checkout date of the reservation, ", - required=True, - default=lambda self: self._get_default_checkout(), + compute="_compute_checkout", + readonly=False, + store=True, copy=False, tracking=True, ) @@ -918,6 +920,53 @@ class PmsReservation(models.Model): for reservation in self: reservation.access_url = "/my/reservations/%s" % (reservation.id) + @api.depends("reservation_line_ids") + def _compute_checkin(self): + """ + Allows to calculate the checkin by default or when the create + specifically indicates the lines of the reservation + """ + for record in self: + if record.reservation_line_ids: + checkin_line_date = min(record.reservation_line_ids.mapped("date")) + # check if the checkin was created directly as reservation_line_id: + if checkin_line_date != record.checkin: + record.checkin = checkin_line_date + elif not record.checkin: + # default checkout other folio reservations or today + if len(record.folio_id.reservation_ids) > 1: + record.checkin = record.folio_id.reservation_ids[0].checkin + else: + record.checkin = fields.date.today() + + @api.depends("reservation_line_ids", "checkin") + def _compute_checkout(self): + """ + Allows to calculate the checkout by default or when the create + specifically indicates the lines of the reservation + """ + for record in self: + if record.reservation_line_ids: + checkout_line_date = max( + record.reservation_line_ids.mapped("date") + ) + datetime.timedelta(days=1) + # check if the checkout was created directly as reservation_line_id: + if checkout_line_date != record.checkout: + record.checkout = checkout_line_date + # default checkout if checkin is set + elif record.checkin and not record.checkout: + if len(record.folio_id.reservation_ids) > 1: + record.checkin = record.folio_id.reservation_ids[0].checkout + else: + record.checkout = record.checkin + datetime.timedelta(days=1) + elif not record.checkout: + record.checkout = False + # date checking + if record.checkin and record.checkout and record.checkin >= record.checkout: + raise UserError( + _("The checkout date must be greater than the checkin date") + ) + @api.depends("pms_property_id", "folio_id") def _compute_arrival_hour(self): for record in self: @@ -969,7 +1018,7 @@ class PmsReservation(models.Model): for reservation in self: if reservation.commission_percent > 0: reservation.commission_amount = ( - reservation.price_total * reservation.commission_percent + reservation.price_total * reservation.commission_percent / 100 ) else: reservation.commission_amount = 0 @@ -1178,28 +1227,6 @@ class PmsReservation(models.Model): recs = self.search([]).filtered(lambda x: x.checkin_partner_pending_count > 0) return [("id", "in", [x.id for x in recs])] if recs else [] - def _get_default_checkin(self): - folio = False - if "folio_id" in self._context: - folio = self.env["pms.folio"].search( - [("id", "=", self._context["folio_id"])] - ) - if folio and folio.reservation_ids: - return folio.reservation_ids[0].checkin - else: - return fields.Date.today() - - def _get_default_checkout(self): - folio = False - if "folio_id" in self._context: - folio = self.env["pms.folio"].search( - [("id", "=", self._context["folio_id"])] - ) - if folio and folio.reservation_ids: - return folio.reservation_ids[0].checkout - else: - return fields.Date.today() + datetime.timedelta(1) - def _get_default_segmentation(self): folio = False segmentation_ids = False diff --git a/pms/tests/test_pms_folio.py b/pms/tests/test_pms_folio.py index 9fd2b3307..a2a68af27 100644 --- a/pms/tests/test_pms_folio.py +++ b/pms/tests/test_pms_folio.py @@ -158,8 +158,8 @@ class TestPmsFolio(TestPms): self.env["pms.reservation"].create( { - "checkin": fields.date.today(), - "checkout": fields.date.today() + datetime.timedelta(days=3), + "folio_id": folio1.id, + "room_type_id": self.room_type_double.id, "reservation_line_ids": [ ( 0, @@ -186,7 +186,6 @@ class TestPmsFolio(TestPms): }, ), ], - "folio_id": folio1.id, } ) # ASSERT @@ -215,6 +214,7 @@ class TestPmsFolio(TestPms): reservation1 = self.env["pms.reservation"].create( { + "room_type_id": self.demo_room_type_double.id, "checkin": fields.date.today(), "checkout": fields.date.today() + datetime.timedelta(days=1), "folio_id": folio1.id,