[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,
)
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

View File

@@ -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",

View File

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

View File

@@ -1,18 +1,20 @@
from odoo import fields, models
from odoo import models, fields
class ReservationWizard(models.TransientModel):
_name = 'pms.reservation.wizard'
allowed_rooms = fields.One2many('pms.room', compute="_compute_allowed_rooms")
options = fields.Many2one('pms.room', string='Room')
_name = "pms.reservation.wizard"
allowed_rooms = fields.One2many("pms.room", compute="_compute_allowed_rooms")
options = fields.Many2one("pms.room", string="Room")
def _compute_allowed_rooms(self):
for record in self:
record.allowed_rooms = self._context.get('rooms_available')
record.allowed_rooms = self._context.get("rooms_available")
def unify(self):
if self.options:
for record in self:
for line in self.env['pms.reservation'].search([('id', '=', self._context.get('active_id'))]).reservation_line_ids:
line.room_id = self.options
for line in (
self.env["pms.reservation"]
.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" ?>
<odoo>
<record id="reservation_wizard" model="ir.ui.view">
<field name="name">Reservation Wizard</field>
<field name="model">pms.reservation.wizard</field>
<field name="arch" type="xml">
<form string="Choose The Details">
<group>
<field name="options"
string="Suggested rooms to unify the reservation:"
options="{'no_create': True,'no_open': True}"
required="1"
domain="[('id', 'in', allowed_rooms)]"/>
<field name="allowed_rooms" invisible="1"/>
</group>
<footer>
<button name="unify" string="Unify" type="object" class="oe_highlight" />
or
<button string="Cancel" class="oe_link" special="cancel"/>
</footer>
</form>
<record id="reservation_wizard" model="ir.ui.view">
<field name="name">Reservation Wizard</field>
<field name="model">pms.reservation.wizard</field>
<field name="arch" type="xml">
<form string="Choose The Details">
<group>
<field
name="options"
string="Suggested rooms to unify the reservation:"
options="{'no_create': True,'no_open': True}"
required="1"
domain="[('id', 'in', allowed_rooms)]"
/>
<field name="allowed_rooms" invisible="1" />
</group>
<footer>
<button
name="unify"
string="Unify"
type="object"
class="oe_highlight"
/>
or
<button string="Cancel" class="oe_link" special="cancel" />
</footer>
</form>
</field>
</record>
</odoo>