mirror of
https://github.com/OCA/pms.git
synced 2025-01-29 00:17:45 +02:00
@@ -82,7 +82,11 @@ class PmsAvailabilityPlan(models.Model):
|
|||||||
|
|
||||||
@api.model
|
@api.model
|
||||||
def update_quota(
|
def update_quota(
|
||||||
self, pricelist_id, room_type_id, date, pms_property_id, impacts_quota_id=False
|
self,
|
||||||
|
pricelist_id,
|
||||||
|
room_type_id,
|
||||||
|
date,
|
||||||
|
pms_property_id,
|
||||||
):
|
):
|
||||||
if pricelist_id and room_type_id and date:
|
if pricelist_id and room_type_id and date:
|
||||||
rule = self.env["pms.availability.plan.rule"].search(
|
rule = self.env["pms.availability.plan.rule"].search(
|
||||||
@@ -97,38 +101,8 @@ class PmsAvailabilityPlan(models.Model):
|
|||||||
if rule:
|
if rule:
|
||||||
rule.ensure_one()
|
rule.ensure_one()
|
||||||
if rule and rule.quota != -1 and rule.quota > 0:
|
if rule and rule.quota != -1 and rule.quota > 0:
|
||||||
|
rule.quota -= 1
|
||||||
# the line has no rule item applied before
|
return True
|
||||||
if not impacts_quota_id:
|
|
||||||
rule.quota -= 1
|
|
||||||
return rule.id
|
|
||||||
|
|
||||||
# the line has a rule item applied before
|
|
||||||
elif impacts_quota_id != rule.id:
|
|
||||||
|
|
||||||
# decrement quota on current rule item
|
|
||||||
rule.quota -= 1
|
|
||||||
|
|
||||||
# check old rule item
|
|
||||||
old_rule = self.env["pms.availability.plan.rule"].search(
|
|
||||||
[("id", "=", impacts_quota_id)]
|
|
||||||
)
|
|
||||||
|
|
||||||
# restore quota in old rule item
|
|
||||||
if old_rule:
|
|
||||||
old_rule.quota += 1
|
|
||||||
|
|
||||||
return rule.id
|
|
||||||
|
|
||||||
# in any case, check old rule item
|
|
||||||
if impacts_quota_id:
|
|
||||||
old_rule = self.env["pms.availability.plan.rule"].search(
|
|
||||||
[("id", "=", impacts_quota_id)]
|
|
||||||
)
|
|
||||||
# and restore quota in old rule item
|
|
||||||
if old_rule and (not rule or rule.id != old_rule.id):
|
|
||||||
old_rule.quota += 1
|
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Action methods
|
# Action methods
|
||||||
|
|||||||
@@ -102,13 +102,6 @@ class PmsReservationLine(models.Model):
|
|||||||
store=True,
|
store=True,
|
||||||
compute="_compute_occupies_availability",
|
compute="_compute_occupies_availability",
|
||||||
)
|
)
|
||||||
impacts_quota = fields.Integer(
|
|
||||||
string="Impacts quota",
|
|
||||||
help="This line has been taken into account in the avail quota",
|
|
||||||
readonly=True,
|
|
||||||
store=True,
|
|
||||||
compute="_compute_impacts_quota",
|
|
||||||
)
|
|
||||||
overnight_room = fields.Boolean(
|
overnight_room = fields.Boolean(
|
||||||
related="reservation_id.overnight_room",
|
related="reservation_id.overnight_room",
|
||||||
store=True,
|
store=True,
|
||||||
@@ -341,19 +334,6 @@ class PmsReservationLine(models.Model):
|
|||||||
# no matter what it is
|
# no matter what it is
|
||||||
line.room_id = list(bests.keys())[0]
|
line.room_id = list(bests.keys())[0]
|
||||||
|
|
||||||
@api.depends("reservation_id.room_type_id", "reservation_id.pricelist_id")
|
|
||||||
def _compute_impacts_quota(self):
|
|
||||||
for line in self:
|
|
||||||
reservation = line.reservation_id
|
|
||||||
impacts_quota_id = line.impacts_quota
|
|
||||||
line.impacts_quota = self.env["pms.availability.plan"].update_quota(
|
|
||||||
pricelist_id=reservation.pricelist_id.id,
|
|
||||||
room_type_id=reservation.room_type_id.id,
|
|
||||||
date=line.date,
|
|
||||||
pms_property_id=reservation.pms_property_id.id,
|
|
||||||
impacts_quota_id=impacts_quota_id,
|
|
||||||
)
|
|
||||||
|
|
||||||
@api.depends(
|
@api.depends(
|
||||||
"reservation_id",
|
"reservation_id",
|
||||||
"reservation_id.room_type_id",
|
"reservation_id.room_type_id",
|
||||||
@@ -498,6 +478,19 @@ class PmsReservationLine(models.Model):
|
|||||||
else:
|
else:
|
||||||
record.overbooking = False
|
record.overbooking = False
|
||||||
|
|
||||||
|
@api.model_create_multi
|
||||||
|
def create(self, vals_list):
|
||||||
|
records = super().create(vals_list)
|
||||||
|
for line in records:
|
||||||
|
reservation = line.reservation_id
|
||||||
|
self.env["pms.availability.plan"].update_quota(
|
||||||
|
pricelist_id=reservation.pricelist_id.id,
|
||||||
|
room_type_id=reservation.room_type_id.id,
|
||||||
|
date=line.date,
|
||||||
|
pms_property_id=reservation.pms_property_id.id,
|
||||||
|
)
|
||||||
|
return records
|
||||||
|
|
||||||
# Constraints and onchanges
|
# Constraints and onchanges
|
||||||
@api.constrains("date")
|
@api.constrains("date")
|
||||||
def constrains_duplicated_date(self):
|
def constrains_duplicated_date(self):
|
||||||
|
|||||||
@@ -604,53 +604,3 @@ class TestPmsRoomTypeAvailabilityRules(TestPms):
|
|||||||
"partner_id": self.partner1.id,
|
"partner_id": self.partner1.id,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_rule_update_quota_on_update_reservation(self):
|
|
||||||
"""
|
|
||||||
Checks that an availability rule is maintained if its pricelist is modified.
|
|
||||||
---------------------
|
|
||||||
Quota rule is restored after creating a reservation with pricelist linked
|
|
||||||
to an availability rule that applies and then modify the pricelist of the
|
|
||||||
reservation and no rules applies
|
|
||||||
"""
|
|
||||||
# ARRANGE
|
|
||||||
test_quota = 2
|
|
||||||
test_pricelist3 = self.env["product.pricelist"].create(
|
|
||||||
{
|
|
||||||
"name": "test pricelist 2",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
self.pricelist2.pms_property_ids = [
|
|
||||||
(4, self.pms_property1.id),
|
|
||||||
(4, self.pms_property2.id),
|
|
||||||
(4, self.pms_property3.id),
|
|
||||||
]
|
|
||||||
rule = self.env["pms.availability.plan.rule"].create(
|
|
||||||
{
|
|
||||||
"availability_plan_id": self.test_room_type_availability1.id,
|
|
||||||
"room_type_id": self.test_room_type_double.id,
|
|
||||||
"date": datetime.date.today(),
|
|
||||||
"quota": test_quota,
|
|
||||||
"pms_property_id": self.pms_property3.id,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
reservation = self.env["pms.reservation"].create(
|
|
||||||
{
|
|
||||||
"pms_property_id": self.pms_property3.id,
|
|
||||||
"checkin": datetime.date.today(),
|
|
||||||
"checkout": datetime.date.today() + datetime.timedelta(days=1),
|
|
||||||
"adults": 2,
|
|
||||||
"room_type_id": self.test_room_type_double.id,
|
|
||||||
"pricelist_id": self.pricelist2.id,
|
|
||||||
"partner_id": self.partner1.id,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
# ACT
|
|
||||||
reservation.pricelist_id = test_pricelist3.id
|
|
||||||
reservation.flush()
|
|
||||||
self.assertEqual(
|
|
||||||
test_quota,
|
|
||||||
rule.quota,
|
|
||||||
"The quota should be restored after changing the reservation's pricelist",
|
|
||||||
)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user