第四章 价格表
价格表模型包括(product.pricelist)和价格表明细模型(product.pricelist.item)
价格表明细
价格
价格表明细模型中并没有直接字段可以显示该明细使用的价格表方法所对应的价格,它的价格字段(price)是一个文本字段,用来显示计算后的结果。
price = fields.Char(
string="Price",
compute='_compute_name_and_price',
help="Explicit rule name for this pricelist line.")
@api.depends('applied_on', 'categ_id', 'product_tmpl_id', 'product_id', 'compute_price', 'fixed_price', \
'pricelist_id', 'percent_price', 'price_discount', 'price_surcharge')
def _compute_name_and_price(self):
for item in self:
if item.categ_id and item.applied_on == '2_product_category':
item.name = _("Category: %s") % (item.categ_id.display_name)
elif item.product_tmpl_id and item.applied_on == '1_product':
item.name = _("Product: %s") % (item.product_tmpl_id.display_name)
elif item.product_id and item.applied_on == '0_product_variant':
item.name = _("Variant: %s") % (item.product_id.display_name)
else:
item.name = _("All Products")
if item.compute_price == 'fixed':
item.price = formatLang(
item.env, item.fixed_price, monetary=True, dp="Product Price", currency_obj=item.currency_id)
elif item.compute_price == 'percentage':
item.price = _("%s %% discount", item.percent_price)
else:
item.price = _("%(percentage)s %% discount and %(price)s surcharge", percentage=item.price_discount, price=item.price_surcharge)
我们从其方法定义可以看出来,其价格字段是依据价格表明细所使用的规则(固定/折扣/百分比)所计算出来的文本值,因此我们在使用这个字段的时候不能像数值或货币类型一样使用。
但是为了给我们使用方便,odoo同时在价格表明细模型中定义了一个_compute_price方法用来给我们进行计算使用:
def _compute_price(self, product, quantity, uom, date, currency=None):
"""Compute the unit price of a product in the context of a pricelist application.
:param product: recordset of product (product.product/product.template)
:param float qty: quantity of products requested (in given uom)
:param uom: unit of measure (uom.uom record)
:param datetime date: date to use for price computation and currency conversions
:param currency: pricelist currency (for the specific case where self is empty)
:returns: price according to pricelist rule, expressed in pricelist currency
:rtype: float
"""
product.ensure_one()
uom.ensure_one()
currency = currency or self.currency_id
currency.ensure_one()
# Pricelist specific values are specified according to product UoM
# and must be multiplied according to the factor between uoms
product_uom = product.uom_id
if product_uom != uom:
convert = lambda p: product_uom._compute_price(p, uom)
else:
convert = lambda p: p
if self.compute_price == 'fixed':
price = convert(self.fixed_price)
elif self.compute_price == 'percentage':
base_price = self._compute_base_price(product, quantity, uom, date, currency)
price = (base_price - (base_price * (self.percent_price / 100))) or 0.0
elif self.compute_price == 'formula':
base_price = self._compute_base_price(product, quantity, uom, date, currency)
# complete formula
price_limit = base_price
price = (base_price - (base_price * (self.price_discount / 100))) or 0.0
if self.price_round:
price = tools.float_round(price, precision_rounding=self.price_round)
if self.price_surcharge:
price += convert(self.price_surcharge)
if self.price_min_margin:
price = max(price, price_limit + convert(self.price_min_margin))
if self.price_max_margin:
price = min(price, price_limit + convert(self.price_max_margin))
else: # empty self, or extended pricelist price computation logic
price = self._compute_base_price(product, quantity, uom, date, currency)
return price
因此如果我们需要使用,直接使用此方法获取计算后的结果即可。