[IMP]pms: update price from pricelist change

This commit is contained in:
Darío Lodeiros
2021-03-05 19:56:32 +01:00
parent 5af70155d4
commit a14d248114
4 changed files with 67 additions and 45 deletions

View File

@@ -107,7 +107,6 @@ class PmsFolio(models.Model):
copy=False,
)
currency_id = fields.Many2one(
"res.currency",
related="pricelist_id.currency_id",
string="Currency",
readonly=True,
@@ -457,15 +456,14 @@ class PmsFolio(models.Model):
@api.depends("partner_id", "agency_id")
def _compute_pricelist_id(self):
for folio in self:
if folio.partner_id and folio.partner_id.property_product_pricelist:
pricelist_id = folio.partner_id.property_product_pricelist.id
else:
pricelist_id = self.env.user.pms_property_id.default_pricelist_id.id
if folio.pricelist_id.id != pricelist_id:
# TODO: Warning change de pricelist?
folio.pricelist_id = pricelist_id
if folio.agency_id and folio.agency_id.apply_pricelist:
pricelist_id = folio.agency_id.property_product_pricelist.id
folio.pricelist_id = folio.agency_id.property_product_pricelist.id
elif folio.partner_id and folio.partner_id.property_product_pricelist:
folio.pricelist_id = folio.partner_id.property_product_pricelist.id
elif not folio.pricelist_id.id:
folio.pricelist_id = (
self.env.user.pms_property_id.default_pricelist_id.id
)
@api.depends("agency_id")
def _compute_partner_id(self):

View File

@@ -184,6 +184,13 @@ class PmsReservation(models.Model):
store=True,
readonly=False,
)
show_update_pricelist = fields.Boolean(
string="Has Pricelist Changed",
help="Technical Field, True if the pricelist was changed;\n"
" this will then display a recomputation button",
compute="_compute_show_update_pricelist",
store=True,
)
commission_percent = fields.Float(
string="Commission percent (%)",
compute="_compute_commission_percent",
@@ -248,7 +255,7 @@ class PmsReservation(models.Model):
default=_get_default_segmentation,
)
currency_id = fields.Many2one(
"res.currency",
related="pricelist_id.currency_id",
depends=["pricelist_id"],
store=True,
readonly=True,
@@ -700,22 +707,36 @@ class PmsReservation(models.Model):
reservation.service_ids -= old_board_lines
reservation.service_ids = board_services
@api.depends("partner_id")
@api.depends("partner_id", "agency_id")
def _compute_pricelist_id(self):
for reservation in self:
# TODO: Review logic pricelist by partner
# and by allowed channel pricelist_ids
if reservation.folio_id:
pricelist_id = reservation.folio_id.pricelist_id.id
else:
pricelist_id = (
reservation.partner_id.property_product_pricelist
and reservation.partner_id.property_product_pricelist.id
or self.env.user.pms_property_id.default_pricelist_id.id
if reservation.agency_id and reservation.agency_id.apply_pricelist:
reservation.pricelist_id = (
reservation.agency_id.property_product_pricelist.id
)
if reservation.pricelist_id.id != pricelist_id:
# TODO: Warning change de pricelist?
reservation.pricelist_id = pricelist_id
elif (
reservation.partner_id
and reservation.partner_id.property_product_pricelist
):
reservation.pricelist_id = (
reservation.partner_id.property_product_pricelist.id
)
elif not reservation.pricelist_id.id:
reservation.pricelist_id = (
self.env.user.pms_property_id.default_pricelist_id.id
)
@api.depends("pricelist_id")
def _compute_show_update_pricelist(self):
for reservation in self:
if (
sum(reservation.reservation_line_ids.mapped("price")) > 0
and reservation.pricelist_id
and reservation._origin.pricelist_id != reservation.pricelist_id
):
reservation.show_update_pricelist = True
else:
reservation.show_update_pricelist = False
@api.depends("adults")
def _compute_checkin_partner_ids(self):
@@ -1397,6 +1418,18 @@ class PmsReservation(models.Model):
# Business methods
def update_prices(self):
self.ensure_one()
for line in self.reservation_line_ids:
line.with_context(force_recompute=True)._compute_price()
self.show_update_pricelist = False
self.message_post(
body=_(
"Prices have been recomputed according to pricelist <b>%s<b> ",
self.pricelist_id.display_name,
)
)
def _compute_shared(self):
# Has this reservation more charges associates in folio?,
# Yes?, then, this is share folio ;)

View File

@@ -265,7 +265,6 @@ class PmsReservationLine(models.Model):
@api.depends(
"reservation_id",
"reservation_id.pricelist_id",
"reservation_id.room_type_id",
"reservation_id.reservation_type",
"reservation_id.pms_property_id",
@@ -279,7 +278,7 @@ class PmsReservationLine(models.Model):
or not reservation.pms_property_id
):
line.price = 0
elif line._recompute_price():
elif not line.price or self._context.get("force_recompute"):
room_type_id = reservation.room_type_id.id
product = self.env["pms.room.type"].browse(room_type_id).product_id
partner = self.env["res.partner"].browse(reservation.partner_id.id)
@@ -312,25 +311,6 @@ class PmsReservationLine(models.Model):
else:
line.occupies_availability = True
def _recompute_price(self):
# REVIEW: Conditional to avoid overriding already calculated prices,
# I'm not sure it's the best way
self.ensure_one()
origin = self._origin.reservation_id
new = self.reservation_id
price_fields = [
"pricelist_id",
"room_type_id",
"reservation_type",
"pms_property_id",
]
if (
any(origin[field] != new[field] for field in price_fields)
or self._origin.price == 0
):
return True
return False
@api.depends("move_line_ids", "move_line_ids.move_id.state")
def _compute_invoiced(self):
for line in self:

View File

@@ -286,9 +286,20 @@
string="Reservation Details"
name="reservation_details"
>
<field name="show_update_pricelist" invisible="1" />
<field
name="pricelist_id"
attrs="{'invisible': [('reservation_type','in',('out'))]}"
options="{'no_open':True,'no_create': True}"
/>
<button
name="update_prices"
type="object"
string=" Update Prices"
help="Recompute all prices based on this pricelist"
class="btn-link mb-1 px-0"
icon="fa-refresh"
confirm="This will update all unit prices based on the currently set pricelist."
attrs="{'invisible': ['|', ('show_update_pricelist', '=', False), ('state', 'in', ['done'])]}"
/>
<!--<field
name="agency_id"