Skip to content

Commit

Permalink
[IMP]pms: Minimal adaptation to new pricelist items price compute met…
Browse files Browse the repository at this point in the history
…hods
  • Loading branch information
DarioLodeiros committed Oct 17, 2024
1 parent 863c1be commit 315b1b3
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 117 deletions.
2 changes: 1 addition & 1 deletion pms/models/folio_sale_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -1147,7 +1147,7 @@ def _get_real_price_currency(self, product, rule_id, qty, uom, pricelist_id):
elif (
pricelist_item.base == "pricelist" and pricelist_item.base_pricelist_id
):
field_name = "price"
field_name = "lst_price"
product = product.with_context(
pricelist=pricelist_item.base_pricelist_id.id
)
Expand Down
8 changes: 3 additions & 5 deletions pms/models/pms_folio.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,10 @@ class PmsFolio(models.Model):
comodel_name="account.analytic.account",
)
currency_id = fields.Many2one(
string="Currency",
help="The currency of the property location",
readonly=True,
required=True,
index=True,
related="pricelist_id.currency_id",
depends=["pricelist_id"],
store=True,
precompute=True,
ondelete="restrict",
)
pricelist_id = fields.Many2one(
Expand Down
8 changes: 3 additions & 5 deletions pms/models/pms_reservation.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,11 @@ class PmsReservation(models.Model):
domain="[('is_used_in_checkin', '=', True)]",
)
currency_id = fields.Many2one(
string="Currency",
help="The currency used in relation to the pricelist",
readonly=True,
store=True,
index=True,
related="pricelist_id.currency_id",
depends=["pricelist_id"],
store=True,
precompute=True,
ondelete="restrict",
)
tax_ids = fields.Many2many(
string="Taxes",
Expand Down
164 changes: 137 additions & 27 deletions pms/models/pms_reservation_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ class PmsReservationLine(models.Model):
index=True,
check_pms_properties=True,
)
currency_id = fields.Many2one(
related="reservation_id.currency_id",
depends=["reservation_id.currency_id"],
store=True,
precompute=True,
)
room_id = fields.Many2one(
string="Room",
help="The room of a reservation. ",
Expand Down Expand Up @@ -65,6 +71,10 @@ class PmsReservationLine(models.Model):
related="reservation_id.state",
store=True,
)
# Tech field caching pricelist rule used for price & discount computation
pricelist_item_id = fields.Many2one(
comodel_name="product.pricelist.item", compute="_compute_pricelist_item_id"
)
price = fields.Float(
string="Price",
help="The price in a reservation line",
Expand Down Expand Up @@ -156,34 +166,134 @@ def name_get(self):
result.append((res.id, name))
return result

def _get_display_price(self, product):
# NEW

@api.depends(
"reservation_id",
"reservation_id.room_type_id",
"reservation_id.reservation_type",
"reservation_id.pms_property_id",
)
def _compute_pricelist_item_id(self):
for line in self:
if (
not line.reservation_id.room_type_id
or not line.reservation_id.pricelist_id
):
line.pricelist_item_id = False
else:
product = line.reservation_id.room_type_id.product_id
line.pricelist_item_id = (
line.reservation_id.pricelist_id._get_product_rule(
product,
1.0,
uom=product.uom_id,
date=line.reservation_id.date_order or fields.Date.now(),
consumption_date=line.date,
)
)

def _get_display_price(self):
"""Compute the displayed unit price for a given line.
Overridden in custom flows:
* where the price is not specified by the pricelist
* where the discount is not specified by the pricelist
Note: self.ensure_one()
"""
self.ensure_one()

pricelist_price = self._get_pricelist_price()

if self.reservation_id.pricelist_id.discount_policy == "with_discount":
return product.with_context(
pricelist=self.reservation_id.pricelist_id.id
).standard_price
product_context = dict(
self.env.context,
partner_id=self.reservation_id.partner_id.id,
date=self.date,
uom=product.uom_id.id,
)
final_price, rule_id = self.reservation_id.pricelist_id.with_context(
product_context
).get_product_price_rule(product, 1.0, self.reservation_id.partner_id)
base_price, currency = self.with_context(
product_context
)._get_real_price_currency(
product, rule_id, 1, product.uom_id, self.reservation_id.pricelist_id.id
)
if currency != self.reservation_id.pricelist_id.currency_id:
base_price = currency._convert(
base_price,
self.reservation_id.pricelist_id.currency_id,
self.reservation_id.company_id or self.env.company,
fields.Date.today(),
)
return pricelist_price

if not self.pricelist_item_id:
# No pricelist rule found => no discount from pricelist
return pricelist_price

base_price = self._get_pricelist_price_before_discount()

# negative discounts (= surcharge) are included in the display price
return max(base_price, final_price)
return max(base_price, pricelist_price)

def _get_pricelist_price(self):
"""Compute the price given by the pricelist for the given line information.
:return: the product sales price in the order currency (without taxes)
:rtype: float
"""
self.ensure_one()
product = self.reservation_id.room_type_id.product_id
product.ensure_one()

pricelist_rule = self.pricelist_item_id
order_date = self.reservation_id.date_order or fields.Date.today()
# product = product.with_context(**self._get_product_price_context())
qty = 1.0
uom = product.uom_id
currency = self.currency_id or self.order_id.company_id.currency_id

price = pricelist_rule._compute_price(
product, qty, uom, order_date, currency=currency
)

return price

# def _get_product_price_context(self):
# """Gives the context for product price computation.

# :return: additional context to consider extra prices from attributes in the base product price.
# :rtype: dict
# """
# self.ensure_one()
# res = {}

# # It is possible that a no_variant attribute is still in a variant if
# # the type of the attribute has been changed after creation.
# no_variant_attributes_price_extra = [
# ptav.price_extra for ptav in self.product_no_variant_attribute_value_ids.filtered(
# lambda ptav:
# ptav.price_extra and
# ptav not in self.product_id.product_template_attribute_value_ids
# )
# ]
# if no_variant_attributes_price_extra:
# res['no_variant_attributes_price_extra'] = tuple(no_variant_attributes_price_extra)

# return res

# OLD

# def _get_display_price(self, product):
# if self.reservation_id.pricelist_id.discount_policy == "with_discount":
# return product.with_context(
# pricelist=self.reservation_id.pricelist_id.id
# ).lst_price
# product_context = dict(
# self.env.context,
# partner_id=self.reservation_id.partner_id.id,
# date=self.date,
# uom=product.uom_id.id,
# )
# final_price, rule_id = self.reservation_id.pricelist_id.with_context(
# product_context
# ).get_product_price_rule(product, 1.0, self.reservation_id.partner_id)
# base_price, currency = self.with_context(
# product_context
# )._get_real_price_currency(
# product, rule_id, 1, product.uom_id, self.reservation_id.pricelist_id.id
# )
# if currency != self.reservation_id.pricelist_id.currency_id:
# base_price = currency._convert(
# base_price,
# self.reservation_id.pricelist_id.currency_id,
# self.reservation_id.company_id or self.env.company,
# fields.Date.today(),
# )
# # negative discounts (= surcharge) are included in the display price
# return max(base_price, final_price)

# flake8: noqa=C901
@api.depends("reservation_id.room_type_id", "reservation_id.preferred_room_id")
Expand Down Expand Up @@ -395,7 +505,7 @@ def _compute_price(self):
property=reservation.pms_property_id.id,
)
line.price = self.env["account.tax"]._fix_tax_included_price_company(
line._get_display_price(product),
line._get_display_price(),
product.taxes_id,
reservation.tax_ids,
reservation.pms_property_id.company_id,
Expand Down
14 changes: 14 additions & 0 deletions pms/models/pms_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ class PmsService(models.Model):
related="folio_id.pms_property_id",
check_pms_properties=True,
)
pricelist_id = fields.Many2one(
string="Pricelist",
help="Pricelist used in the service",
comodel_name="product.pricelist",
compute="_compute_pricelist_id",
store=True,
index=True,
)
tax_ids = fields.Many2many(
string="Taxes",
help="Taxes applied in the service",
Expand Down Expand Up @@ -225,6 +233,12 @@ class PmsService(models.Model):
)

# Compute and Search methods
@api.depends("folio_id", "reservation_id")
def _compute_pricelist_id(self):
for record in self:
origin = record.reservation_id if record.reservation_id else record.folio_id
record.pricelist_id = origin.pricelist_id

@api.depends("product_id")
def _compute_tax_ids(self):
for service in self:
Expand Down
16 changes: 11 additions & 5 deletions pms/models/pms_service_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,19 @@ class PmsServiceLine(models.Model):
store=True,
compute="_compute_day_amount_service",
)
pricelist_id = fields.Many2one(
related="service_id.pricelist_id",
depends=["service_id"],
store=True,
precompute=True,
ondelete="restrict",
)
currency_id = fields.Many2one(
string="Currency",
help="The currency used in relation to the service where it's included",
readonly=True,
related="pricelist_id.currency_id",
depends=["pricelist_id"],
store=True,
index=True,
related="service_id.currency_id",
precompute=True,
ondelete="restrict",
)
reservation_id = fields.Many2one(
string="Reservation",
Expand Down
Loading

0 comments on commit 315b1b3

Please sign in to comment.