From 447de5f8f330ba69472e4d00a6cf873768a56f84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Lodeiros?= Date: Mon, 16 Nov 2020 13:02:43 +0100 Subject: [PATCH] [IMP] Precommit improvements --- pms/models/pms_reservation.py | 53 +++++++-------- pms/models/pms_reservation_line.py | 99 +++++++++++++++++++---------- pms/views/pms_reservation_views.xml | 15 +++-- pms/wizards/wizard_reservation.py | 20 +++--- pms/wizards/wizard_reservation.xml | 45 +++++++------ 5 files changed, 140 insertions(+), 92 deletions(-) diff --git a/pms/models/pms_reservation.py b/pms/models/pms_reservation.py index 7ba04783f..cedccf2bd 100644 --- a/pms/models/pms_reservation.py +++ b/pms/models/pms_reservation.py @@ -259,12 +259,7 @@ class PmsReservation(models.Model): store=True, ) - rooms = fields.Char( - string="Room/s", - compute="_compute_rooms", - store=True - ) - + rooms = fields.Char(string="Room/s", compute="_compute_rooms", store=True) credit_card_details = fields.Text(related="folio_id.credit_card_details") cancelled_reason = fields.Selection( @@ -431,7 +426,9 @@ class PmsReservation(models.Model): elif not reservation.room_type_id: reservation.room_type_id = False - @api.depends("reservation_line_ids.date", "overbooking", "state", "preferred_room_id") + @api.depends( + "reservation_line_ids.date", "overbooking", "state", "preferred_room_id" + ) def _compute_allowed_room_ids(self): for reservation in self: if reservation.checkin and reservation.checkout: @@ -554,8 +551,9 @@ class PmsReservation(models.Model): reservation.splitted = True else: reservation.splitted = False - reservation.preferred_room_id = reservation.reservation_line_ids[0].room_id - + reservation.preferred_room_id = reservation.reservation_line_ids[ + 0 + ].room_id @api.depends("state", "qty_to_invoice", "qty_invoiced") def _compute_invoice_status(self): @@ -792,20 +790,21 @@ class PmsReservation(models.Model): def open_reservation_wizard(self): rooms_available = self.env["pms.room.type.availability"].rooms_available( - checkin=self.checkin, - checkout=self.checkout, - current_lines=self.reservation_line_ids.ids, - ) + checkin=self.checkin, + checkout=self.checkout, + current_lines=self.reservation_line_ids.ids, + ) # REVIEW: check capacity room return { - 'view_type': 'form', - 'view_mode': 'form', - 'name': 'Unify the reservation', - 'res_model': 'pms.reservation.wizard', - 'target': 'new', - 'type': 'ir.actions.act_window', - 'context': {'rooms_available': rooms_available.ids, - } + "view_type": "form", + "view_mode": "form", + "name": "Unify the reservation", + "res_model": "pms.reservation.wizard", + "target": "new", + "type": "ir.actions.act_window", + "context": { + "rooms_available": rooms_available.ids, + }, } # ORM Overrides @@ -826,7 +825,9 @@ class PmsReservation(models.Model): def name_get(self): result = [] for res in self: - name = u"{} ({})".format(res.folio_id.name, res.rooms if res.rooms else 'No room') + name = u"{} ({})".format( + res.folio_id.name, res.rooms if res.rooms else "No room" + ) result.append((res.id, name)) return result @@ -1017,8 +1018,8 @@ class PmsReservation(models.Model): if record.reservation_type != "out": record.checkin_partner_count = len(record.checkin_partner_ids) record.checkin_partner_pending_count = ( - record.adults + record.children - ) - len(record.checkin_partner_ids) + record.adults + record.children + ) - len(record.checkin_partner_ids) else: record.checkin_partner_count = 0 record.checkin_partner_pending_count = 0 @@ -1082,7 +1083,9 @@ class PmsReservation(models.Model): for reservation in self: if reservation.splitted: - reservation.rooms = ", ".join([r for r in reservation.reservation_line_ids.mapped('room_id.name')]) + reservation.rooms = ", ".join( + [r for r in reservation.reservation_line_ids.mapped("room_id.name")] + ) reservation.preferred_room_id = False else: reservation.rooms = reservation.preferred_room_id.name diff --git a/pms/models/pms_reservation_line.py b/pms/models/pms_reservation_line.py index 0247e891d..8391191c4 100644 --- a/pms/models/pms_reservation_line.py +++ b/pms/models/pms_reservation_line.py @@ -1,15 +1,15 @@ # Copyright 2017-2018 Alexandre Díaz # Copyright 2017 Dario Lodeiros # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -import logging import datetime -import operator +import logging from odoo import _, api, fields, models from odoo.exceptions import ValidationError _logger = logging.getLogger(__name__) + class PmsReservationLine(models.Model): _name = "pms.reservation.line" _description = "Reservations by day" @@ -99,7 +99,9 @@ class PmsReservationLine(models.Model): if line.reservation_id.room_type_id and not line.room_id: # we get the rooms available for the entire stay - rooms_available = self.env[ "pms.room.type.availability"].rooms_available( + rooms_available = self.env[ + "pms.room.type.availability" + ].rooms_available( checkin=line.reservation_id.checkin, checkout=line.reservation_id.checkout, room_type_id=line.reservation_id.room_type_id.id, @@ -118,70 +120,101 @@ class PmsReservationLine(models.Model): # if the preferred room is NOT available else: - raise ValidationError(_("%s: No room available.")% (line.reservation_id.preferred_room_id.name)) + raise ValidationError( + _("%s: No room available.") + % (line.reservation_id.preferred_room_id.name) + ) - # otherwise we assign the first of those available for the entire stay + # otherwise we assign the first of those + # available for the entire stay else: line.room_id = rooms_available[0] - # if there is no availability for the entire stay without changing rooms (we assume a split reservation) + # if there is no availability for the entire stay without + # changing rooms (we assume a split reservation) else: rooms_ranking = dict() # we go through the rooms of the type - for room in self.env['pms.room'].search([('room_type_id', '=', line.reservation_id.room_type_id.id)]): + for room in self.env["pms.room"].search( + [("room_type_id", "=", line.reservation_id.room_type_id.id)] + ): # we iterate the dates from the date of the line to the checkout - for date_iterator in \ - [line.date + datetime.timedelta(days=x) for x in range(0, (line.reservation_id.checkout - line.date).days)]: - - # if the room is already assigned for a date we go to the next room - if self.env['pms.reservation.line'].search_count([ - ('date', '=', date_iterator), - ('room_id', '=', room.id), - ('id', 'not in', line.reservation_id.reservation_line_ids.ids), - ("occupies_availability", "=", True), - ]) > 0: + for date_iterator in [ + line.date + datetime.timedelta(days=x) + for x in range( + 0, (line.reservation_id.checkout - line.date).days + ) + ]: + # if the room is already assigned for + # a date we go to the next room + ids = line.reservation_id.reservation_line_ids.ids + if ( + self.env["pms.reservation.line"].search_count( + [ + ("date", "=", date_iterator), + ("room_id", "=", room.id), + ("id", "not in", ids), + ("occupies_availability", "=", True), + ] + ) + > 0 + ): break - - # if the room is not assigned for a date we add it to the ranking / update its ranking + # if the room is not assigned for a date we + # add it to the ranking / update its ranking else: - rooms_ranking[room.id] = 1 if room.id not in rooms_ranking else rooms_ranking[room.id] + 1 + rooms_ranking[room.id] = ( + 1 + if room.id not in rooms_ranking + else rooms_ranking[room.id] + 1 + ) if len(rooms_ranking) == 0: - raise ValidationError(_("%s: No room type available") % (line.reservation_id.room_type_id.name)) + raise ValidationError( + _("%s: No room type available") + % (line.reservation_id.room_type_id.name) + ) else: # we get the best score in the ranking best = max(rooms_ranking.values()) # we keep the rooms with the best ranking - bests = {key: value for (key, value) in rooms_ranking.items() if value == best} + bests = { + key: value + for (key, value) in rooms_ranking.items() + if value == best + } # if there is a tie in the rankings if len(bests) > 1: # we get the line from last night date_last_night = line.date + datetime.timedelta(days=-1) - line_past_night = self.env['pms.reservation.line'].search([ - ('date', '=', date_last_night), - ('reservation_id', '=', line.reservation_id.id) - ]) - - # if there is the night before and if the room from the night before is in the ranking + line_past_night = self.env["pms.reservation.line"].search( + [ + ("date", "=", date_last_night), + ("reservation_id", "=", line.reservation_id.id), + ] + ) + # if there is the night before and if the room + # from the night before is in the ranking if line_past_night and line_past_night.room_id.id in bests: line.room_id = line_past_night.room_id.id - # if the room from the night before is not in the ranking or there is no night before + # if the room from the night before is not in the ranking + # or there is no night before else: - # At this point we set the room with the best ranking, no matter what it is + # At this point we set the room with the best ranking, + # no matter what it is line.room_id = list(bests.keys())[0] # if there is no tie in the rankings else: - # At this point we set the room with the best ranking, no matter what it is + # At this point we set the room with the best ranking, + # no matter what it is line.room_id = list(bests.keys())[0] - - @api.depends( "reservation_id", "reservation_id.pricelist_id", diff --git a/pms/views/pms_reservation_views.xml b/pms/views/pms_reservation_views.xml index caa73059f..928d8659f 100644 --- a/pms/views/pms_reservation_views.xml +++ b/pms/views/pms_reservation_views.xml @@ -66,7 +66,8 @@ style="margin-bottom:0px;" attrs="{'invisible': [('splitted','=',False)]}" > - This reservation is part of splitted reservation, you can try to unify the reservation here + This reservation is part of splitted reservation, you can try to + unify the reservation here