[FIX] pms: fix check adults w. extra beds, fix day_qty @ pms.service.line, change literal code spain & fix tests

This commit is contained in:
miguelpadin
2021-07-06 20:32:08 +02:00
parent 34bf8eaea6
commit 3b0bf39215
8 changed files with 122 additions and 85 deletions

View File

@@ -605,6 +605,29 @@ class PmsReservation(models.Model):
readonly=False,
)
check_adults = fields.Boolean(
help="Internal field to force room capacity validations",
compute="_compute_check_adults",
readonly=False,
store=True,
)
def _compute_date_order(self):
for record in self:
record.date_order = datetime.datetime.today()
@api.depends(
"service_ids",
"service_ids.service_line_ids",
"service_ids.service_line_ids.product_id",
"service_ids.service_line_ids.day_qty",
"reservation_line_ids",
"reservation_line_ids.room_id",
)
def _compute_check_adults(self):
for record in self:
record.check_adults = True
@api.depends(
"checkin",
"checkout",
@@ -1423,23 +1446,6 @@ class PmsReservation(models.Model):
# _("The room already is completed (%s)", record.name)
# )
@api.constrains("adults")
def _check_adults(self):
for record in self:
for line in record.reservation_line_ids:
extra_beds = record.service_ids.service_line_ids.filtered(
lambda x: x.date == line.date and x.product_id.is_extra_bed is True
)
if (
record.adults + record.children_occupying
) > line.room_id.get_capacity(len(extra_beds)):
raise ValidationError(
_(
"Persons can't be higher than room capacity (%s)",
record.name,
)
)
@api.constrains("state")
def _check_onboard_reservation(self):
for record in self:
@@ -1484,6 +1490,13 @@ class PmsReservation(models.Model):
if record.agency_id and not record.agency_id.is_agency:
raise ValidationError(_("booking agency with wrong configuration: "))
@api.constrains("check_adults")
def _check_capacity(self):
for record in self:
self.env["pms.room"]._check_adults(
record, record.service_ids.service_line_ids
)
# Action methods
def open_partner(self):
""" Utility method used to add an "View Customer" button in reservation views """

View File

@@ -470,17 +470,3 @@ class PmsReservationLine(models.Model):
)
if duplicated:
raise ValidationError(_("Duplicated reservation line date"))
@api.constrains("room_id")
def _check_adults(self):
for record in self.filtered("room_id"):
extra_bed = record.reservation_id.service_ids.filtered(
lambda r: r.product_id.is_extra_bed is True
)
if (
record.reservation_id.adults + record.reservation_id.children_occupying
> record.room_id.get_capacity(len(extra_bed))
):
raise ValidationError(_("Persons can't be higher than room capacity"))
# if record.reservation_id.adults == 0:
# raise ValidationError(_("Reservation has no adults"))

View File

@@ -118,6 +118,26 @@ class PmsRoom(models.Model):
)
)
@api.model
def _check_adults(self, reservation, service_line_ids=False):
for line in reservation.reservation_line_ids:
num_extra_beds = 0
if service_line_ids:
extra_beds = service_line_ids.filtered(
lambda x: x.date == line.date and x.product_id.is_extra_bed is True
)
num_extra_beds = sum(extra_beds.mapped("day_qty")) if extra_beds else 0
if line.room_id:
if (
reservation.adults + reservation.children_occupying
) > line.room_id.get_capacity(num_extra_beds):
raise ValidationError(
_(
"Persons can't be higher than room capacity (%s)",
reservation.name,
)
)
# Business methods
def get_capacity(self, extra_bed=0):

View File

@@ -340,23 +340,24 @@ class PmsService(models.Model):
move_day = 0
if consumed_on == "after":
move_day = 1
service.service_line_ids -= (
service.service_line_ids.filtered_domain(
[
"|",
(
"date",
"<",
reservation.checkin + timedelta(move_day),
),
(
"date",
">=",
reservation.checkout + timedelta(move_day),
),
]
)
)
for del_service_id in service.service_line_ids.filtered_domain(
[
"|",
(
"date",
"<",
reservation.checkin + timedelta(move_day),
),
(
"date",
">=",
reservation.checkout + timedelta(move_day),
),
]
).ids:
lines.append((2, del_service_id))
# TODO: check intermediate states in check_adults restriction
# when lines are removed
service.service_line_ids = lines
else:
if not service.service_line_ids:
@@ -535,10 +536,11 @@ class PmsService(models.Model):
# Businness Methods
def _service_day_qty(self):
self.ensure_one()
qty = self.product_qty if len(self.service_line_ids) == 1 else 0
qty = self.product_qty if len(self.service_line_ids) == 1 else 1
if not self.reservation_id:
return qty
# TODO: Pass per_person to service line from product default_per_person
# When the user modifies the quantity avoid overwriting
if self.product_id.per_person:
qty = self.reservation_id.adults
return qty

View File

@@ -36,6 +36,7 @@ class TestPmsReservations(TestPms):
"name": "Double 101",
"room_type_id": self.room_type_double.id,
"capacity": 2,
"extra_beds_allowed": 1,
}
)
@@ -626,19 +627,20 @@ class TestPmsReservations(TestPms):
# ACT & ASSERT
with self.assertRaises(
ValidationError,
msg="The number of people is lower than the capacity of the room",
msg="The number of people is greater than the capacity of the room",
):
self.env["pms.reservation"].create(
reservation = self.env["pms.reservation"].create(
{
"adults": 2,
"children_occupying": 1,
"checkin": datetime.datetime.now(),
"checkout": datetime.datetime.now() + datetime.timedelta(days=1),
"room_type_id": self.room_type_double.id,
"preferred_room_id": self.room1.id,
"partner_id": self.partner1.id,
"pms_property_id": self.pms_property1.id,
}
)
reservation.flush()
def test_to_assign_priority_reservation(self):
"""
@@ -1930,16 +1932,17 @@ class TestPmsReservations(TestPms):
}
)
with self.assertRaises(ValidationError):
self.env["pms.reservation"].create(
reservation = self.env["pms.reservation"].create(
{
"checkin": fields.date.today(),
"checkout": fields.date.today() + datetime.timedelta(days=3),
"pms_property_id": self.pms_property1.id,
"partner_id": self.host1.id,
"room_type_id": self.room_type_double.id,
"preferred_room_id": self.room1.id,
"adults": 4,
}
)
reservation.flush()
def test_check_format_arrival_hour(self):
"""
@@ -2616,7 +2619,7 @@ class TestPmsReservations(TestPms):
{
"checkin": fields.date.today() + datetime.timedelta(days=-3),
"checkout": fields.date.today() + datetime.timedelta(days=3),
"room_type_id": self.room_type_double.id,
"preferred_room_id": self.room1.id,
"partner_id": self.partner1.id,
"pms_property_id": self.pms_property1.id,
"pricelist_id": self.pricelist1.id,

View File

@@ -16,6 +16,7 @@
<field name="tax_ids" invisible="1" />
<field name="checkin_partner_count" invisible="1" />
<field name="to_assign" invisible="1" />
<field name="check_adults" invisible="1" />
<button
name="confirm"
string="Confirm"

View File

@@ -72,6 +72,7 @@ class TestWizardINE(TestPms):
{
"name": "partner1",
"country_id": self.country_italy.id,
"nationality_id": self.country_italy.id,
"birthdate_date": "2000-06-25",
"gender": "male",
}
@@ -90,6 +91,7 @@ class TestWizardINE(TestPms):
{
"name": "partner2",
"country_id": self.country_russia.id,
"nationality_id": self.country_russia.id,
"birthdate_date": "2000-06-25",
"gender": "male",
}
@@ -107,6 +109,7 @@ class TestWizardINE(TestPms):
{
"name": "partner3",
"country_id": self.country_italy.id,
"nationality_id": self.country_italy.id,
"birthdate_date": "2000-06-25",
"gender": "male",
}
@@ -124,6 +127,7 @@ class TestWizardINE(TestPms):
{
"name": "partner4",
"country_id": self.country_italy.id,
"nationality_id": self.country_italy.id,
"birthdate_date": "2000-06-25",
"gender": "male",
}
@@ -141,6 +145,7 @@ class TestWizardINE(TestPms):
{
"name": "partner5",
"country_id": self.country_afghanistan.id,
"nationality_id": self.country_afghanistan.id,
"birthdate_date": "2000-06-25",
"gender": "male",
}
@@ -158,6 +163,7 @@ class TestWizardINE(TestPms):
{
"name": "partner6",
"country_id": self.country_afghanistan.id,
"nationality_id": self.country_afghanistan.id,
"birthdate_date": "2000-06-25",
"gender": "male",
}
@@ -175,6 +181,7 @@ class TestWizardINE(TestPms):
{
"name": "partner7",
"country_id": self.country_afghanistan.id,
"nationality_id": self.country_afghanistan.id,
"birthdate_date": "2000-06-25",
"gender": "male",
}
@@ -257,15 +264,14 @@ class TestWizardINE(TestPms):
{
"name": "Product test",
"is_extra_bed": True,
"consumed_on": "before",
"per_day": True,
}
)
service_extra_bed = self.env["pms.service"].create(
{
"is_board_service": False,
"product_id": product_extra_bed.id,
}
)
vals_service_extra_bed = {
"is_board_service": False,
"product_id": product_extra_bed.id,
}
# Create reservation 4
self.reservation_4 = self.env["pms.reservation"].create(
{
@@ -275,7 +281,7 @@ class TestWizardINE(TestPms):
"partner_id": self.partner_6.id,
"adults": 2,
"pms_property_id": self.pms_property1.id,
"service_ids": [(6, 0, [service_extra_bed.id])],
"service_ids": [(0, 0, vals_service_extra_bed)],
}
)
self.checkin6 = self.env["pms.checkin.partner"].create(
@@ -395,7 +401,7 @@ class TestWizardINE(TestPms):
end_date = datetime.date(2021, 2, 4)
expected_result = {
self.country_afghanistan.id: {
self.country_afghanistan.code: {
second_date: {
"arrivals": 3,
"pernoctations": 3,
@@ -407,7 +413,7 @@ class TestWizardINE(TestPms):
"departures": 3,
},
},
self.country_italy.id: {
self.country_italy.code: {
start_date: {
"arrivals": 1,
"pernoctations": 1,
@@ -421,7 +427,7 @@ class TestWizardINE(TestPms):
"departures": 2,
},
},
self.country_russia.id: {
self.country_russia.code: {
start_date: {
"arrivals": 1,
"pernoctations": 1,

View File

@@ -6,7 +6,8 @@ import xml.etree.cElementTree as ET
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
CODE_SPAIN = 68
# TODO: Review code (code iso ?)
CODE_SPAIN = "ES"
class WizardIne(models.TransientModel):
@@ -197,21 +198,24 @@ class WizardIne(models.TransientModel):
"""
for entry in read_group_result:
# get country_id from group set read_group results
country_id_key = entry["country_id"][0]
# get nationality_id from group set read_group results
nationality_id_code = (
self.env["res.country"]
.search([("id", "=", entry["nationality_id"][0])])
.code
)
# all countries except Spain
if country_id_key != CODE_SPAIN:
if nationality_id_code != CODE_SPAIN:
# get count of each result
num = entry["__count"]
# update/create dicts for countries & dates and set num. arrivals
if not nationalities.get(country_id_key):
nationalities[country_id_key] = dict()
if not nationalities[country_id_key].get(date):
nationalities[country_id_key][date] = dict()
nationalities[country_id_key][date][type_of_entry] = num
if not nationalities.get(nationality_id_code):
nationalities[nationality_id_code] = dict()
if not nationalities[nationality_id_code].get(date):
nationalities[nationality_id_code][date] = dict()
nationalities[nationality_id_code][date][type_of_entry] = num
else:
# arrivals grouped by state_id (Spain "provincias")
read_by_arrivals_spain = self.env["res.partner"].read_group(
@@ -270,33 +274,33 @@ class WizardIne(models.TransientModel):
# arrivals
arrivals = hosts.filtered(lambda x: x.checkin == p_date)
# arrivals grouped by country_id
# arrivals grouped by nationality_id
read_by_arrivals = self.env["res.partner"].read_group(
[("id", "in", arrivals.mapped("partner_id").ids)],
["country_id"],
["country_id"],
["nationality_id"],
["nationality_id"],
lazy=False,
)
# departures
departures = hosts.filtered(lambda x: x.checkout == p_date)
# departures grouped by country_id
# departures grouped by nationality_id
read_by_departures = self.env["res.partner"].read_group(
[("id", "in", departures.mapped("partner_id").ids)],
["country_id"],
["country_id"],
["nationality_id"],
["nationality_id"],
lazy=False,
)
# pernoctations
pernoctations = hosts - departures
# pernoctations grouped by country_id
# pernoctations grouped by nationality_id
read_by_pernoctations = self.env["res.partner"].read_group(
[("id", "in", pernoctations.mapped("partner_id").ids)],
["country_id"],
["country_id"],
["nationality_id"],
["nationality_id"],
lazy=False,
)
@@ -457,7 +461,9 @@ class WizardIne(models.TransientModel):
self.start_date, self.end_date, self.pms_property_id.id
)
for key_country, value_country in nationalities.items():
country = self.env["res.country"].browse(key_country)
country = self.env["res.country"].search([("code", "=", key_country)])
if key_country != CODE_SPAIN:
residency_tag = ET.SubElement(accommodation_tag, "RESIDENCIA")
ET.SubElement(residency_tag, "ID_PAIS").text = country.code_alpha3