From d5de68e120d3dafb24e628846bcca9481176d7af Mon Sep 17 00:00:00 2001 From: Sara Lago Date: Wed, 11 Nov 2020 11:49:11 +0100 Subject: [PATCH] [IMP] pms.folio add sale channel constraint --- pms/models/pms_folio.py | 11 +++++++++++ pms/models/pms_reservation.py | 18 ++---------------- pms/tests/test_pms_sale_channel.py | 25 ++++++++++++++++++++++++- 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/pms/models/pms_folio.py b/pms/models/pms_folio.py index 71088d067..77c31ef18 100644 --- a/pms/models/pms_folio.py +++ b/pms/models/pms_folio.py @@ -442,6 +442,17 @@ class PmsFolio(models.Model): "amount_total": amount_untaxed + amount_tax, } ) + # Check channel type + @api.constrains("channel_type") + def check_channel_type(self): + for record in self: + if (record.channel_type == "indirect" and record.partner_id.is_agency != True + or record.channel_type == "direct" and record.partner_id.is_agency == True): + raise ValidationError( + _( + "Indirect Sale Channel must have an agency associated!" + ) + ) @api.depends("reservation_ids", "reservation_ids.state") def _compute_reservations_pending_arrival(self): diff --git a/pms/models/pms_reservation.py b/pms/models/pms_reservation.py index e909aa8b0..bf32504a3 100644 --- a/pms/models/pms_reservation.py +++ b/pms/models/pms_reservation.py @@ -344,13 +344,9 @@ class PmsReservation(models.Model): reselling = fields.Boolean("Is Reselling", default=False) nights = fields.Integer("Nights", compute="_compute_nights", store=True) channel_type = fields.Selection( - [ - ("direct","Direct"), - ("indirect","Indirect") - ], - string="Channel type", + related="folio_id.channel_type", required = True, - store=True + readonly = True, ) origin = fields.Char("Origin", compute="_compute_origin", store=True) detail_origin = fields.Char( @@ -1008,16 +1004,6 @@ class PmsReservation(models.Model): # self._compute_tax_ids() TODO: refact - @api.constrains("channel_type") - def check_channel_type(self): - for record in self: - if (record.channel_type == "indirect" and record.partner_id.is_agency != True): - raise ValidationError( - _( - "Indirect Sale Channel must have an agency associated!" - ) - ) - # Action methods def open_folio(self): diff --git a/pms/tests/test_pms_sale_channel.py b/pms/tests/test_pms_sale_channel.py index f58ac8c88..e54c03b3f 100644 --- a/pms/tests/test_pms_sale_channel.py +++ b/pms/tests/test_pms_sale_channel.py @@ -23,10 +23,33 @@ class TestPmsSaleChannel(TestHotel): "checkin": datetime.datetime.now(), "checkout":datetime.datetime.now() + datetime.timedelta(days=3), "channel_type":"indirect", - "partner_id":not_agency.id + "partner_id":not_agency.id, } ) + def test_reservation_direct_channel(self): + PmsReservation = self.env["pms.reservation"] + agency = self.env["res.partner"].create( + { + "name":"partner2", + "is_agency":True, + } + ) + #ACT & ASSERT + with self.assertRaises(ValidationError), self.cr.savepoint(): + PmsReservation.create( + { + "checkin": datetime.datetime.now() +datetime.timedelta(days=5), + "checkout":datetime.datetime.now() + datetime.timedelta(days=8), + "channel_type":"direct", + "partner_id":agency.id, + } + ) + + + + +