[IMP] Precommit improvements

This commit is contained in:
Darío Lodeiros
2020-11-16 13:02:43 +01:00
parent b7fd5cfdfb
commit 447de5f8f3
5 changed files with 140 additions and 92 deletions

View File

@@ -259,12 +259,7 @@ class PmsReservation(models.Model):
store=True, store=True,
) )
rooms = fields.Char( rooms = fields.Char(string="Room/s", compute="_compute_rooms", store=True)
string="Room/s",
compute="_compute_rooms",
store=True
)
credit_card_details = fields.Text(related="folio_id.credit_card_details") credit_card_details = fields.Text(related="folio_id.credit_card_details")
cancelled_reason = fields.Selection( cancelled_reason = fields.Selection(
@@ -431,7 +426,9 @@ class PmsReservation(models.Model):
elif not reservation.room_type_id: elif not reservation.room_type_id:
reservation.room_type_id = False 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): def _compute_allowed_room_ids(self):
for reservation in self: for reservation in self:
if reservation.checkin and reservation.checkout: if reservation.checkin and reservation.checkout:
@@ -554,8 +551,9 @@ class PmsReservation(models.Model):
reservation.splitted = True reservation.splitted = True
else: else:
reservation.splitted = False 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") @api.depends("state", "qty_to_invoice", "qty_invoiced")
def _compute_invoice_status(self): def _compute_invoice_status(self):
@@ -792,20 +790,21 @@ class PmsReservation(models.Model):
def open_reservation_wizard(self): def open_reservation_wizard(self):
rooms_available = self.env["pms.room.type.availability"].rooms_available( rooms_available = self.env["pms.room.type.availability"].rooms_available(
checkin=self.checkin, checkin=self.checkin,
checkout=self.checkout, checkout=self.checkout,
current_lines=self.reservation_line_ids.ids, current_lines=self.reservation_line_ids.ids,
) )
# REVIEW: check capacity room # REVIEW: check capacity room
return { return {
'view_type': 'form', "view_type": "form",
'view_mode': 'form', "view_mode": "form",
'name': 'Unify the reservation', "name": "Unify the reservation",
'res_model': 'pms.reservation.wizard', "res_model": "pms.reservation.wizard",
'target': 'new', "target": "new",
'type': 'ir.actions.act_window', "type": "ir.actions.act_window",
'context': {'rooms_available': rooms_available.ids, "context": {
} "rooms_available": rooms_available.ids,
},
} }
# ORM Overrides # ORM Overrides
@@ -826,7 +825,9 @@ class PmsReservation(models.Model):
def name_get(self): def name_get(self):
result = [] result = []
for res in self: 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)) result.append((res.id, name))
return result return result
@@ -1017,8 +1018,8 @@ class PmsReservation(models.Model):
if record.reservation_type != "out": if record.reservation_type != "out":
record.checkin_partner_count = len(record.checkin_partner_ids) record.checkin_partner_count = len(record.checkin_partner_ids)
record.checkin_partner_pending_count = ( record.checkin_partner_pending_count = (
record.adults + record.children record.adults + record.children
) - len(record.checkin_partner_ids) ) - len(record.checkin_partner_ids)
else: else:
record.checkin_partner_count = 0 record.checkin_partner_count = 0
record.checkin_partner_pending_count = 0 record.checkin_partner_pending_count = 0
@@ -1082,7 +1083,9 @@ class PmsReservation(models.Model):
for reservation in self: for reservation in self:
if reservation.splitted: 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 reservation.preferred_room_id = False
else: else:
reservation.rooms = reservation.preferred_room_id.name reservation.rooms = reservation.preferred_room_id.name

View File

@@ -1,15 +1,15 @@
# Copyright 2017-2018 Alexandre Díaz # Copyright 2017-2018 Alexandre Díaz
# Copyright 2017 Dario Lodeiros # Copyright 2017 Dario Lodeiros
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import logging
import datetime import datetime
import operator import logging
from odoo import _, api, fields, models from odoo import _, api, fields, models
from odoo.exceptions import ValidationError from odoo.exceptions import ValidationError
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
class PmsReservationLine(models.Model): class PmsReservationLine(models.Model):
_name = "pms.reservation.line" _name = "pms.reservation.line"
_description = "Reservations by day" _description = "Reservations by day"
@@ -99,7 +99,9 @@ class PmsReservationLine(models.Model):
if line.reservation_id.room_type_id and not line.room_id: if line.reservation_id.room_type_id and not line.room_id:
# we get the rooms available for the entire stay # 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, checkin=line.reservation_id.checkin,
checkout=line.reservation_id.checkout, checkout=line.reservation_id.checkout,
room_type_id=line.reservation_id.room_type_id.id, 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 # if the preferred room is NOT available
else: 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: else:
line.room_id = rooms_available[0] 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: else:
rooms_ranking = dict() rooms_ranking = dict()
# we go through the rooms of the type # 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 # we iterate the dates from the date of the line to the checkout
for date_iterator in \ for date_iterator in [
[line.date + datetime.timedelta(days=x) for x in range(0, (line.reservation_id.checkout - line.date).days)]: line.date + datetime.timedelta(days=x)
for x in range(
# if the room is already assigned for a date we go to the next room 0, (line.reservation_id.checkout - line.date).days
if self.env['pms.reservation.line'].search_count([ )
('date', '=', date_iterator), ]:
('room_id', '=', room.id), # if the room is already assigned for
('id', 'not in', line.reservation_id.reservation_line_ids.ids), # a date we go to the next room
("occupies_availability", "=", True), ids = line.reservation_id.reservation_line_ids.ids
]) > 0: if (
self.env["pms.reservation.line"].search_count(
[
("date", "=", date_iterator),
("room_id", "=", room.id),
("id", "not in", ids),
("occupies_availability", "=", True),
]
)
> 0
):
break break
# if the room is not assigned for a date we
# if the room is not assigned for a date we add it to the ranking / update its ranking # add it to the ranking / update its ranking
else: 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: 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: else:
# we get the best score in the ranking # we get the best score in the ranking
best = max(rooms_ranking.values()) best = max(rooms_ranking.values())
# we keep the rooms with the best ranking # 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 there is a tie in the rankings
if len(bests) > 1: if len(bests) > 1:
# we get the line from last night # we get the line from last night
date_last_night = line.date + datetime.timedelta(days=-1) date_last_night = line.date + datetime.timedelta(days=-1)
line_past_night = self.env['pms.reservation.line'].search([ line_past_night = self.env["pms.reservation.line"].search(
('date', '=', date_last_night), [
('reservation_id', '=', line.reservation_id.id) ("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 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: if line_past_night and line_past_night.room_id.id in bests:
line.room_id = line_past_night.room_id.id 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: 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] line.room_id = list(bests.keys())[0]
# if there is no tie in the rankings # if there is no tie in the rankings
else: 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] line.room_id = list(bests.keys())[0]
@api.depends( @api.depends(
"reservation_id", "reservation_id",
"reservation_id.pricelist_id", "reservation_id.pricelist_id",

View File

@@ -66,7 +66,8 @@
style="margin-bottom:0px;" style="margin-bottom:0px;"
attrs="{'invisible': [('splitted','=',False)]}" 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
<bold> <bold>
<button <button
class="alert-link" class="alert-link"
@@ -167,9 +168,7 @@
placeholder="Room" placeholder="Room"
style="margin-right: 30px;" style="margin-right: 30px;"
attrs="{'invisible': [('splitted','=',True)]}" attrs="{'invisible': [('splitted','=',True)]}"
/> />
<field <field
name="partner_id" name="partner_id"
default_focus="1" default_focus="1"
@@ -553,7 +552,6 @@
</calendar> </calendar>
</field> </field>
</record> </record>
<!-- Form view Checkin Partners from reservation --> <!-- Form view Checkin Partners from reservation -->
<record model="ir.ui.view" id="pms_reservation_checkin_view_form"> <record model="ir.ui.view" id="pms_reservation_checkin_view_form">
<field name="name">pms.reservation.checkin.form</field> <field name="name">pms.reservation.checkin.form</field>
@@ -660,9 +658,14 @@
<field name="name">pms.reservation.tree</field> <field name="name">pms.reservation.tree</field>
<field name="model">pms.reservation</field> <field name="model">pms.reservation</field>
<field name="arch" type="xml"> <field name="arch" type="xml">
<tree string="Reservation" multi_edit="1" sample="1" class="o_sale_order" <tree
string="Reservation"
multi_edit="1"
sample="1"
class="o_sale_order"
decoration-warning="splitted" decoration-warning="splitted"
decoration-bf="splitted"> decoration-bf="splitted"
>
<field name="splitted" invisible="1" /> <field name="splitted" invisible="1" />
<field name="pricelist_id" invisible="1" /> <field name="pricelist_id" invisible="1" />
<field name="rooms" /> <field name="rooms" />

View File

@@ -1,18 +1,20 @@
from odoo import fields, models
from odoo import models, fields
class ReservationWizard(models.TransientModel): class ReservationWizard(models.TransientModel):
_name = 'pms.reservation.wizard' _name = "pms.reservation.wizard"
allowed_rooms = fields.One2many('pms.room', compute="_compute_allowed_rooms") allowed_rooms = fields.One2many("pms.room", compute="_compute_allowed_rooms")
options = fields.Many2one('pms.room', string='Room') options = fields.Many2one("pms.room", string="Room")
def _compute_allowed_rooms(self): def _compute_allowed_rooms(self):
for record in self: for record in self:
record.allowed_rooms = self._context.get('rooms_available') record.allowed_rooms = self._context.get("rooms_available")
def unify(self): def unify(self):
if self.options: if self.options:
for record in self: for line in (
for line in self.env['pms.reservation'].search([('id', '=', self._context.get('active_id'))]).reservation_line_ids: self.env["pms.reservation"]
line.room_id = self.options .search([("id", "=", self._context.get("active_id"))])
.reservation_line_ids
):
line.room_id = self.options

View File

@@ -1,24 +1,31 @@
<?xml version="1.0" ?> <?xml version="1.0" ?>
<odoo> <odoo>
<record id="reservation_wizard" model="ir.ui.view"> <record id="reservation_wizard" model="ir.ui.view">
<field name="name">Reservation Wizard</field> <field name="name">Reservation Wizard</field>
<field name="model">pms.reservation.wizard</field> <field name="model">pms.reservation.wizard</field>
<field name="arch" type="xml"> <field name="arch" type="xml">
<form string="Choose The Details"> <form string="Choose The Details">
<group> <group>
<field name="options" <field
string="Suggested rooms to unify the reservation:" name="options"
options="{'no_create': True,'no_open': True}" string="Suggested rooms to unify the reservation:"
required="1" options="{'no_create': True,'no_open': True}"
domain="[('id', 'in', allowed_rooms)]"/> required="1"
<field name="allowed_rooms" invisible="1"/> domain="[('id', 'in', allowed_rooms)]"
</group> />
<footer> <field name="allowed_rooms" invisible="1" />
<button name="unify" string="Unify" type="object" class="oe_highlight" /> </group>
or <footer>
<button string="Cancel" class="oe_link" special="cancel"/> <button
</footer> name="unify"
</form> string="Unify"
type="object"
class="oe_highlight"
/>
or
<button string="Cancel" class="oe_link" special="cancel" />
</footer>
</form>
</field> </field>
</record> </record>
</odoo> </odoo>