From 8be6287863dfe4e5f4dd8e4bd7a497aac3537c80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Lodeiros?= Date: Wed, 7 Dec 2022 11:29:36 +0100 Subject: [PATCH] [IMP]pms: unique board_service_room_type by property --- pms/demo/pms_master_data.xml | 18 ++++++ pms/models/pms_board_service_room_type.py | 62 ++++--------------- .../pms_board_service_room_type_line.py | 47 +++----------- pms/models/pms_reservation.py | 1 + pms/models/product_pricelist.py | 19 ------ pms/models/product_product.py | 5 ++ pms/security/pms_security.xml | 2 +- pms/tests/test_pms_booking_engine.py | 2 + pms/tests/test_pms_folio_invoice.py | 5 ++ pms/tests/test_pms_folio_sale_line.py | 1 + pms/tests/test_pms_multiproperty.py | 1 + .../pms_board_service_room_type_views.xml | 4 +- pms/views/pms_room_type_views.xml | 2 +- pms/wizards/pms_booking_engine.py | 7 ++- pms/wizards/pms_booking_engine_views.xml | 1 + pms/wizards/wizard_folio_changes.py | 3 +- 16 files changed, 66 insertions(+), 114 deletions(-) diff --git a/pms/demo/pms_master_data.xml b/pms/demo/pms_master_data.xml index 0cb4ddac0..9fe51b973 100644 --- a/pms/demo/pms_master_data.xml +++ b/pms/demo/pms_master_data.xml @@ -808,6 +808,7 @@ > + + + + + + + + + + + + + + + + + + diff --git a/pms/models/pms_board_service_room_type.py b/pms/models/pms_board_service_room_type.py index 8beefc95e..ef81ae648 100644 --- a/pms/models/pms_board_service_room_type.py +++ b/pms/models/pms_board_service_room_type.py @@ -21,18 +21,14 @@ class PmsBoardServiceRoomType(models.Model): ondelete="cascade", check_pms_properties=True, ) - pms_property_ids = fields.Many2many( - string="Properties", - help="Properties with access to the element;" - " if not set, all properties can access", + pms_property_id = fields.Many2one( + string="Property", + help="Property with access to the element;" + " if not set, all property can access", required=False, ondelete="restrict", comodel_name="pms.property", - relation="pms_board_service_room_type_pms_property_rel", - column1="pms_board_service_room_type_id", - column2="pms_property_id", check_pms_properties=True, - compute="_compute_pms_property_ids", store=True, ) pms_room_type_id = fields.Many2one( @@ -64,43 +60,6 @@ class PmsBoardServiceRoomType(models.Model): help="Indicates if this board service is applied by default in the room type", ) - @api.depends( - "pms_room_type_id", - "pms_room_type_id.pms_property_ids", - "pms_board_service_id", - "pms_board_service_id.pms_property_ids", - ) - def _compute_pms_property_ids(self): - for record in self: - if ( - record.pms_room_type_id.pms_property_ids - and record.pms_board_service_id.pms_property_ids - ): - record.pms_property_ids = self.env["pms.property"].search( - [ - ( - "id", - "in", - list( - set(record.pms_room_type_id.pms_property_ids.ids) - & set(record.pms_board_service_id.pms_property_ids.ids) - ), - ) - ] - ) - elif ( - record.pms_room_type_id.pms_property_ids - and not record.pms_board_service_id.pms_property_ids - ): - record.pms_property_ids = record.pms_room_type_id.pms_property_ids - elif ( - not record.pms_room_type_id.pms_property_ids - and record.pms_board_service_id.pms_property_ids - ): - record.pms_property_ids = record.pms_board_service_id.pms_property_ids - else: - record.pms_property_ids = False - @api.depends("board_service_line_ids.amount") def _compute_board_amount(self): for record in self: @@ -119,7 +78,7 @@ class PmsBoardServiceRoomType(models.Model): return res @api.constrains("by_default") - def constrains_duplicated_board_defaul(self): + def constrains_duplicated_board_default(self): for record in self: default_boards = ( record.pms_room_type_id.board_service_room_type_ids.filtered( @@ -127,7 +86,12 @@ class PmsBoardServiceRoomType(models.Model): ) ) # TODO Check properties (with different propertys is allowed) - if any(default_boards.filtered(lambda l: l.id != record.id)): + if any( + default_boards.filtered( + lambda l: l.id != record.id + and l.pms_property_id == record.pms_property_id + ) + ): raise UserError(_("""Only can set one default board service""")) def open_board_lines_form(self): @@ -156,14 +120,14 @@ class PmsBoardServiceRoomType(models.Model): @api.model def create(self, vals): # properties = False - if "pms_board_service_id" in vals: + if "pms_board_service_id" in vals and "board_service_line_ids" not in vals: vals.update( self.prepare_board_service_reservation_ids(vals["pms_board_service_id"]) ) return super(PmsBoardServiceRoomType, self).create(vals) def write(self, vals): - if "pms_board_service_id" in vals: + if "pms_board_service_id" in vals and "board_service_line_ids" not in vals: vals.update( self.prepare_board_service_reservation_ids(vals["pms_board_service_id"]) ) diff --git a/pms/models/pms_board_service_room_type_line.py b/pms/models/pms_board_service_room_type_line.py index 5b3b633ad..6b7c0c8a4 100644 --- a/pms/models/pms_board_service_room_type_line.py +++ b/pms/models/pms_board_service_room_type_line.py @@ -1,6 +1,6 @@ # Copyright 2017 Dario Lodeiros # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from odoo import api, fields, models +from odoo import fields, models class PmsBoardServiceRoomTypeLine(models.Model): @@ -16,15 +16,13 @@ class PmsBoardServiceRoomTypeLine(models.Model): comodel_name="pms.board.service.room.type", ondelete="cascade", ) - pms_property_ids = fields.Many2many( - string="Properties", - help="Properties with access to the element;" + pms_property_id = fields.Many2one( + string="Property", + help="Property with access to the element;" " if not set, all properties can access", comodel_name="pms.property", - relation="pms_board_service_room_type_line_pms_property_rel", - column1="pms_board_service_room_type_id", - column2="pms_property_id", check_pms_properties=True, + related="pms_board_service_room_type_id.pms_property_id", ) product_id = fields.Many2one( string="Product", @@ -34,41 +32,12 @@ class PmsBoardServiceRoomTypeLine(models.Model): check_pms_properties=True, domain="[('is_pms_available', '=', True)]", ) - # TODO def default_amount "amount of service" amount = fields.Float( string="Amount", help="Price for this Board Service Room Type Line/Product", - default=0.0, + default=lambda self: self._default_amount(), digits=("Product Price"), ) - @api.model - def create(self, vals): - properties = False - if "pms_board_service_room_type_id" in vals: - board_service = self.env["pms.board.service.room.type"].browse( - vals["pms_board_service_room_type_id"] - ) - properties = board_service.pms_property_ids - if properties: - vals.update( - { - "pms_property_ids": properties, - } - ) - return super(PmsBoardServiceRoomTypeLine, self).create(vals) - - def write(self, vals): - properties = False - if "pms_board_service_room_type_id" in vals: - board_service = self.env["pms.board.service.room.type"].browse( - vals["pms_board_service_room_type_id"] - ) - properties = board_service.pms_property_ids - if properties: - vals.update( - { - "pms_property_ids": properties, - } - ) - return super(PmsBoardServiceRoomTypeLine, self).write(vals) + def _default_amount(self): + return self.product_id.list_price diff --git a/pms/models/pms_reservation.py b/pms/models/pms_reservation.py index 8c40edba7..3dbbe2ca5 100644 --- a/pms/models/pms_reservation.py +++ b/pms/models/pms_reservation.py @@ -827,6 +827,7 @@ class PmsReservation(models.Model): [ ("pms_room_type_id", "=", reservation.room_type_id.id), ("by_default", "=", True), + ("pms_property_id", "=", reservation.pms_property_id.id), ] ) if ( diff --git a/pms/models/product_pricelist.py b/pms/models/product_pricelist.py index 95420426c..2e919e8bb 100644 --- a/pms/models/product_pricelist.py +++ b/pms/models/product_pricelist.py @@ -144,25 +144,6 @@ class ProductPricelist(models.Model): ) return items - @api.constrains("pricelist_type", "item_ids", "pms_property_ids") - def _check_pricelist_type(self): - for record in self: - if record.item_ids: - for item in record.item_ids: - if record.pricelist_type == "daily" and ( - item.compute_price != "fixed" - or len(item.pms_property_ids) != 1 - or not item.date_end_consumption - or not item.date_start_consumption - or item.date_end_consumption != item.date_start_consumption - ): - raise ValidationError( - _( - "Daily Plan must have fixed price, " - "only one property and its items must be daily" - ) - ) - @api.constrains("is_pms_available", "availability_plan_id") def _check_is_pms_available(self): for record in self: diff --git a/pms/models/product_product.py b/pms/models/product_product.py index 9ae008777..9b559287d 100644 --- a/pms/models/product_product.py +++ b/pms/models/product_product.py @@ -23,6 +23,10 @@ class ProductProduct(models.Model): super(ProductProduct, self)._compute_product_price() def _compute_board_price(self): + pms_property_id = ( + self.env.context.get("property") + or self.env.user.get_active_property_ids()[0] + ) for record in self: if self._context.get("board_service"): record.board_price = ( @@ -35,6 +39,7 @@ class ProductProduct(models.Model): self._context.get("board_service"), ), ("product_id", "=", record.id), + ("pms_property_id", "=", pms_property_id), ] ) .amount diff --git a/pms/security/pms_security.xml b/pms/security/pms_security.xml index d77c0e0ba..e5d0cfa2d 100644 --- a/pms/security/pms_security.xml +++ b/pms/security/pms_security.xml @@ -102,7 +102,7 @@ - ['|',('pms_property_ids','=',False),('pms_property_ids', 'in', + ['|',('pms_property_id','=',False),('pms_property_id', 'in', user.get_active_property_ids())] diff --git a/pms/tests/test_pms_booking_engine.py b/pms/tests/test_pms_booking_engine.py index fc298e516..1bd646dba 100644 --- a/pms/tests/test_pms_booking_engine.py +++ b/pms/tests/test_pms_booking_engine.py @@ -770,6 +770,7 @@ class TestPmsBookingEngine(TestPms): { "pms_room_type_id": self.test_room_type_double.id, "pms_board_service_id": self.board_service_test.id, + "pms_property_id": self.pms_property1.id, } ) # self.board_service_room_type.flush() @@ -853,6 +854,7 @@ class TestPmsBookingEngine(TestPms): { "pms_room_type_id": self.test_room_type_double.id, "pms_board_service_id": self.board_service_test.id, + "pms_property_id": self.pms_property1.id, } ) discount = 15 diff --git a/pms/tests/test_pms_folio_invoice.py b/pms/tests/test_pms_folio_invoice.py index 26f5426d5..6390e59e1 100644 --- a/pms/tests/test_pms_folio_invoice.py +++ b/pms/tests/test_pms_folio_invoice.py @@ -493,6 +493,7 @@ class TestPmsFolioInvoice(TestPms): { "pms_room_type_id": self.room_type_double.id, "pms_board_service_id": self.board_service1.id, + "pms_property_id": self.property.id, } ) self.reservation1 = self.env["pms.reservation"].create( @@ -549,6 +550,7 @@ class TestPmsFolioInvoice(TestPms): { "pms_room_type_id": self.room_type_double.id, "pms_board_service_id": self.board_service1.id, + "pms_property_id": self.property.id, } ) self.reservation1 = self.env["pms.reservation"].create( @@ -606,6 +608,7 @@ class TestPmsFolioInvoice(TestPms): { "pms_room_type_id": self.room_type_double.id, "pms_board_service_id": self.board_service1.id, + "pms_property_id": self.property.id, } ) self.reservation1 = self.env["pms.reservation"].create( @@ -761,6 +764,7 @@ class TestPmsFolioInvoice(TestPms): { "pms_room_type_id": self.demo_room_type_double.id, "pms_board_service_id": self.board_service1.id, + "pms_property_id": self.pms_property_demo.id, } ) # ACT @@ -863,6 +867,7 @@ class TestPmsFolioInvoice(TestPms): { "pms_room_type_id": self.room_type_double.id, "pms_board_service_id": self.board_service1.id, + "pms_property_id": self.property.id, } ) # ACT diff --git a/pms/tests/test_pms_folio_sale_line.py b/pms/tests/test_pms_folio_sale_line.py index 140e1c159..bc9ad7fa8 100644 --- a/pms/tests/test_pms_folio_sale_line.py +++ b/pms/tests/test_pms_folio_sale_line.py @@ -72,6 +72,7 @@ class TestPmsFolioSaleLine(TestPms): { "pms_room_type_id": self.room_type_double.id, "pms_board_service_id": self.board_service_test.id, + "pms_property_id": self.pms_property1.id, } ) self.extra_service = self.env["pms.service"].create( diff --git a/pms/tests/test_pms_multiproperty.py b/pms/tests/test_pms_multiproperty.py index 3be97791b..64af13a78 100644 --- a/pms/tests/test_pms_multiproperty.py +++ b/pms/tests/test_pms_multiproperty.py @@ -389,6 +389,7 @@ class TestPmsMultiproperty(TestPms): { "pms_room_type_id": room_type_double.id, "pms_board_service_id": board_service_test.id, + "pms_property_id": self.pms_property1.id, } ) # ASSERT diff --git a/pms/views/pms_board_service_room_type_views.xml b/pms/views/pms_board_service_room_type_views.xml index f78cd3d2d..b7e604c06 100644 --- a/pms/views/pms_board_service_room_type_views.xml +++ b/pms/views/pms_board_service_room_type_views.xml @@ -8,7 +8,7 @@ - + - + diff --git a/pms/views/pms_room_type_views.xml b/pms/views/pms_room_type_views.xml index 8411cd511..2dc62f12f 100644 --- a/pms/views/pms_room_type_views.xml +++ b/pms/views/pms_room_type_views.xml @@ -66,7 +66,7 @@ - +