You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
3.5 KiB

# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
from odoo import api, fields, models
class ProductTemplate(models.Model):
_inherit = "product.template"
fix_variant_prices = fields.Boolean(
"Set variant prices manually",
default=False,
help="If checked, the extra values from attributes will be ignored and you'll have to define each variant price.",
)
def _update_fix_price(self, vals):
if "list_price" in vals:
self.mapped("product_variant_ids").write({"fix_price": vals["list_price"]})
@api.model
def create(self, vals):
product_tmpl = super(ProductTemplate, self).create(vals)
if product_tmpl.fix_variant_prices:
product_tmpl._update_fix_price(vals)
return product_tmpl
def write(self, vals):
res = super(ProductTemplate, self).write(vals)
if self.env.context.get("skip_update_fix_price", False):
return res
for template in self:
if template.fix_variant_prices:
template._update_fix_price(vals)
return res
class ProductProduct(models.Model):
_inherit = "product.product"
list_price = fields.Float(
compute="_compute_list_price",
)
fix_price = fields.Float(string="Fixed price")
@api.depends("list_price", "price_extra", "fix_variant_prices", "fix_price")
@api.depends_context("uom")
def _compute_product_lst_price(self):
to_uom = None
if "uom" in self._context:
to_uom = self.env["uom.uom"].browse(self._context["uom"])
for product in self:
if not product.fix_variant_prices:
super(ProductProduct, product)._compute_product_lst_price()
else:
price = product.fix_price or product.list_price
if to_uom:
price = product.uom_id._compute_price(price, to_uom)
product.lst_price = price
def _compute_list_price(self):
for product in self:
price = (
product.fix_variant_prices
and product.fix_price
or product.product_tmpl_id.list_price
)
if self._context.get("uom"):
price = (
self.env["uom.uom"]
.browse(self._context["uom"])
._compute_price(price, product.uom_id)
)
product.list_price = price
def _set_product_lst_price(self):
for product in self:
if not product.fix_variant_prices:
super(ProductProduct, product)._set_product_lst_price()
else:
if self._context.get("uom"):
price = (
self.env["uom.uom"]
.browse(self._context["uom"])
._compute_price(product.lst_price, product.uom_id)
)
else:
price = product.lst_price
product.write({"fix_price": price})
if product.product_variant_count == 1:
product.product_tmpl_id.list_price = price
else:
fix_prices = product.product_tmpl_id.mapped(
"product_variant_ids.fix_price"
)
# for consistency with price shown in the shop
product.product_tmpl_id.with_context(
skip_update_fix_price=True
).list_price = min(fix_prices)