From a14d248114c019de6162a518c79a24088b4b779e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Lodeiros?= Date: Fri, 5 Mar 2021 19:56:32 +0100 Subject: [PATCH] [IMP]pms: update price from pricelist change --- pms/models/pms_folio.py | 16 ++++---- pms/models/pms_reservation.py | 61 ++++++++++++++++++++++------- pms/models/pms_reservation_line.py | 22 +---------- pms/views/pms_reservation_views.xml | 13 +++++- 4 files changed, 67 insertions(+), 45 deletions(-) diff --git a/pms/models/pms_folio.py b/pms/models/pms_folio.py index 58829cd75..c8bcc5df7 100644 --- a/pms/models/pms_folio.py +++ b/pms/models/pms_folio.py @@ -107,7 +107,6 @@ class PmsFolio(models.Model): copy=False, ) currency_id = fields.Many2one( - "res.currency", related="pricelist_id.currency_id", string="Currency", readonly=True, @@ -457,15 +456,14 @@ class PmsFolio(models.Model): @api.depends("partner_id", "agency_id") def _compute_pricelist_id(self): for folio in self: - if folio.partner_id and folio.partner_id.property_product_pricelist: - pricelist_id = folio.partner_id.property_product_pricelist.id - else: - pricelist_id = self.env.user.pms_property_id.default_pricelist_id.id - if folio.pricelist_id.id != pricelist_id: - # TODO: Warning change de pricelist? - folio.pricelist_id = pricelist_id if folio.agency_id and folio.agency_id.apply_pricelist: - pricelist_id = folio.agency_id.property_product_pricelist.id + folio.pricelist_id = folio.agency_id.property_product_pricelist.id + elif folio.partner_id and folio.partner_id.property_product_pricelist: + folio.pricelist_id = folio.partner_id.property_product_pricelist.id + elif not folio.pricelist_id.id: + folio.pricelist_id = ( + self.env.user.pms_property_id.default_pricelist_id.id + ) @api.depends("agency_id") def _compute_partner_id(self): diff --git a/pms/models/pms_reservation.py b/pms/models/pms_reservation.py index 47b514d7d..31d4521bf 100644 --- a/pms/models/pms_reservation.py +++ b/pms/models/pms_reservation.py @@ -184,6 +184,13 @@ class PmsReservation(models.Model): store=True, readonly=False, ) + show_update_pricelist = fields.Boolean( + string="Has Pricelist Changed", + help="Technical Field, True if the pricelist was changed;\n" + " this will then display a recomputation button", + compute="_compute_show_update_pricelist", + store=True, + ) commission_percent = fields.Float( string="Commission percent (%)", compute="_compute_commission_percent", @@ -248,7 +255,7 @@ class PmsReservation(models.Model): default=_get_default_segmentation, ) currency_id = fields.Many2one( - "res.currency", + related="pricelist_id.currency_id", depends=["pricelist_id"], store=True, readonly=True, @@ -700,22 +707,36 @@ class PmsReservation(models.Model): reservation.service_ids -= old_board_lines reservation.service_ids = board_services - @api.depends("partner_id") + @api.depends("partner_id", "agency_id") def _compute_pricelist_id(self): for reservation in self: - # TODO: Review logic pricelist by partner - # and by allowed channel pricelist_ids - if reservation.folio_id: - pricelist_id = reservation.folio_id.pricelist_id.id - else: - pricelist_id = ( - reservation.partner_id.property_product_pricelist - and reservation.partner_id.property_product_pricelist.id - or self.env.user.pms_property_id.default_pricelist_id.id + if reservation.agency_id and reservation.agency_id.apply_pricelist: + reservation.pricelist_id = ( + reservation.agency_id.property_product_pricelist.id ) - if reservation.pricelist_id.id != pricelist_id: - # TODO: Warning change de pricelist? - reservation.pricelist_id = pricelist_id + elif ( + reservation.partner_id + and reservation.partner_id.property_product_pricelist + ): + reservation.pricelist_id = ( + reservation.partner_id.property_product_pricelist.id + ) + elif not reservation.pricelist_id.id: + reservation.pricelist_id = ( + self.env.user.pms_property_id.default_pricelist_id.id + ) + + @api.depends("pricelist_id") + def _compute_show_update_pricelist(self): + for reservation in self: + if ( + sum(reservation.reservation_line_ids.mapped("price")) > 0 + and reservation.pricelist_id + and reservation._origin.pricelist_id != reservation.pricelist_id + ): + reservation.show_update_pricelist = True + else: + reservation.show_update_pricelist = False @api.depends("adults") def _compute_checkin_partner_ids(self): @@ -1397,6 +1418,18 @@ class PmsReservation(models.Model): # Business methods + def update_prices(self): + self.ensure_one() + for line in self.reservation_line_ids: + line.with_context(force_recompute=True)._compute_price() + self.show_update_pricelist = False + self.message_post( + body=_( + "Prices have been recomputed according to pricelist %s ", + self.pricelist_id.display_name, + ) + ) + def _compute_shared(self): # Has this reservation more charges associates in folio?, # Yes?, then, this is share folio ;) diff --git a/pms/models/pms_reservation_line.py b/pms/models/pms_reservation_line.py index 77dc99bad..fd49aeb9b 100644 --- a/pms/models/pms_reservation_line.py +++ b/pms/models/pms_reservation_line.py @@ -265,7 +265,6 @@ class PmsReservationLine(models.Model): @api.depends( "reservation_id", - "reservation_id.pricelist_id", "reservation_id.room_type_id", "reservation_id.reservation_type", "reservation_id.pms_property_id", @@ -279,7 +278,7 @@ class PmsReservationLine(models.Model): or not reservation.pms_property_id ): line.price = 0 - elif line._recompute_price(): + elif not line.price or self._context.get("force_recompute"): room_type_id = reservation.room_type_id.id product = self.env["pms.room.type"].browse(room_type_id).product_id partner = self.env["res.partner"].browse(reservation.partner_id.id) @@ -312,25 +311,6 @@ class PmsReservationLine(models.Model): else: line.occupies_availability = True - def _recompute_price(self): - # REVIEW: Conditional to avoid overriding already calculated prices, - # I'm not sure it's the best way - self.ensure_one() - origin = self._origin.reservation_id - new = self.reservation_id - price_fields = [ - "pricelist_id", - "room_type_id", - "reservation_type", - "pms_property_id", - ] - if ( - any(origin[field] != new[field] for field in price_fields) - or self._origin.price == 0 - ): - return True - return False - @api.depends("move_line_ids", "move_line_ids.move_id.state") def _compute_invoiced(self): for line in self: diff --git a/pms/views/pms_reservation_views.xml b/pms/views/pms_reservation_views.xml index 352a17ca6..d839f58c3 100644 --- a/pms/views/pms_reservation_views.xml +++ b/pms/views/pms_reservation_views.xml @@ -286,9 +286,20 @@ string="Reservation Details" name="reservation_details" > + +