mirror of
https://github.com/OCA/pms.git
synced 2025-01-29 00:17:45 +02:00
* [ADD]pms: avail real * [ADD] Wizards adaptation multiproperty & new real avail model * [ADD] Tests Wizard avail
147 lines
5.1 KiB
Python
147 lines
5.1 KiB
Python
import datetime
|
|
|
|
from odoo import api, fields, models
|
|
|
|
|
|
class NumRoomsSelectionModel(models.TransientModel):
|
|
_name = "pms.num.rooms.selection"
|
|
_rec_name = "value"
|
|
value = fields.Integer()
|
|
room_type_id = fields.Char()
|
|
folio_wizard_id = fields.One2many(
|
|
comodel_name="pms.folio.availability.wizard",
|
|
inverse_name="id",
|
|
)
|
|
|
|
|
|
class AvailabilityWizard(models.TransientModel):
|
|
_name = "pms.folio.availability.wizard"
|
|
|
|
# Fields declarations
|
|
folio_wizard_id = fields.Many2one(
|
|
comodel_name="pms.folio.wizard",
|
|
)
|
|
checkin = fields.Date(
|
|
string="From:",
|
|
required=True,
|
|
)
|
|
checkout = fields.Date(
|
|
string="To:",
|
|
required=True,
|
|
)
|
|
room_type_id = fields.Many2one(comodel_name="pms.room.type")
|
|
|
|
num_rooms_available = fields.Integer(
|
|
string="Available rooms",
|
|
compute="_compute_num_rooms_available",
|
|
store="true",
|
|
)
|
|
price_per_room = fields.Float(
|
|
string="Price per room",
|
|
default=0,
|
|
)
|
|
num_rooms_selected = fields.Many2one(
|
|
comodel_name="pms.num.rooms.selection",
|
|
inverse_name="folio_wizard_id",
|
|
string="Selected rooms",
|
|
compute="_compute_dynamic_selection",
|
|
store=True,
|
|
readonly=False,
|
|
domain="[('value', '<=', num_rooms_available), "
|
|
"('room_type_id', '=', room_type_id)]",
|
|
)
|
|
value_num_rooms_selected = fields.Integer(default=0)
|
|
price_total = fields.Float(
|
|
string="Total price", default=0, compute="_compute_price_total"
|
|
)
|
|
pms_property_id = fields.Many2one(
|
|
related="folio_wizard_id.pms_property_id",
|
|
string="Property",
|
|
)
|
|
|
|
@api.depends("num_rooms_selected", "checkin", "checkout")
|
|
def _compute_price_total(self):
|
|
for record in self:
|
|
record.price_total = 0
|
|
|
|
# this field refresh is just to update it and take into account @ xml
|
|
record.value_num_rooms_selected = record.num_rooms_selected.value
|
|
|
|
room_type_total_price_per_room = 0
|
|
|
|
for date_iterator in [
|
|
record.checkin + datetime.timedelta(days=x)
|
|
for x in range(0, (record.checkout - record.checkin).days)
|
|
]:
|
|
|
|
partner = record.folio_wizard_id.partner_id
|
|
product = record.room_type_id.product_id
|
|
product = product.with_context(
|
|
lang=partner.lang,
|
|
partner=partner.id,
|
|
quantity=1,
|
|
date=fields.Date.today(),
|
|
date_overnight=date_iterator,
|
|
pricelist=record.folio_wizard_id.pricelist_id.id,
|
|
uom=product.uom_id.id,
|
|
property=record.folio_wizard_id.pms_property_id.id,
|
|
)
|
|
room_type_total_price_per_room += product.price
|
|
|
|
# udpate the price per room
|
|
record.price_per_room = room_type_total_price_per_room
|
|
|
|
# if there's no rooms available
|
|
if record.num_rooms_available == 0:
|
|
# change the selector num_rooms_availabe to 0
|
|
value_selected = self.env["pms.num.rooms.selection"].search(
|
|
[
|
|
("room_type_id", "=", record.room_type_id.id),
|
|
("value", "=", 0),
|
|
]
|
|
)
|
|
if value_selected:
|
|
record.num_rooms_selected = value_selected
|
|
record.value_num_rooms_selected = 0
|
|
|
|
# change the price per room to 0
|
|
record.price_per_room = 0
|
|
|
|
record.price_total = record.price_per_room * record.num_rooms_selected.value
|
|
|
|
@api.depends("room_type_id", "checkin", "checkout")
|
|
def _compute_num_rooms_available(self):
|
|
for record in self:
|
|
record.num_rooms_available = self.env[
|
|
"pms.room.type.availability.plan"
|
|
].get_count_rooms_available(
|
|
record.checkin,
|
|
record.checkout,
|
|
room_type_id=record.room_type_id.id,
|
|
pricelist_id=record.folio_wizard_id.pricelist_id.id,
|
|
pms_property_id=record.folio_wizard_id.pms_property_id.id,
|
|
)
|
|
|
|
def _compute_dynamic_selection(self):
|
|
for record in self:
|
|
for elem_to_insert in range(0, record.num_rooms_available + 1):
|
|
if (
|
|
self.env["pms.num.rooms.selection"].search_count(
|
|
[
|
|
("value", "=", elem_to_insert),
|
|
("room_type_id", "=", record.room_type_id.id),
|
|
]
|
|
)
|
|
== 0
|
|
):
|
|
self.env["pms.num.rooms.selection"].create(
|
|
{
|
|
"value": elem_to_insert,
|
|
"room_type_id": record.room_type_id.id,
|
|
}
|
|
)
|
|
default = self.env["pms.num.rooms.selection"].search(
|
|
[("value", "=", 0), ("room_type_id", "=", record.room_type_id.id)]
|
|
)
|
|
record.num_rooms_selected = default
|