【问题标题】:Odoo 11 How to override compute_all() method?Odoo 11 如何覆盖 compute_all() 方法?
【发布时间】:2018-06-22 16:47:20
【问题描述】:

我正在开发一个模块,以便在发票和销售订单中使用自定义公式来计算金额。 行金额公式应为:nbJours * price_unit * quantity,而不是默认公式:price_unit * quantity

我通过继承向 AccountInvoiceLine 类添加了一个自定义字段,如下所示:

class AccountInvoiceLine(models.Model):
_inherit = "account.invoice.line"
# Nombre de jours de location
nombreJours = fields.Integer("Nombre de jours",default=1,required=True)

@api.multi
@api.depends('nombreJours','price_unit', 'discount', 'invoice_line_tax_ids', 'quantity', 'product_id', 'invoice_id.partner_id', 'invoice_id.currency_id', 'invoice_id.company_id', 'invoice_id.date_invoice', 'invoice_id.date')
def _compute_price(self):
    ...
    ...
    if self.invoice_line_tax_ids:
        taxes = self.invoice_line_tax_ids.compute_all(self.nombreJours, price, currency, self.quantity, product=self.product_id, partner=self.invoice_id.partner_id)

    # Calcul du sous-total de la ligne
    self.price_subtotal = price_subtotal_signed = taxes['total_excluded'] if taxes else self.quantity * price * self.nombreJours
    self.price_total = taxes['total_included'] if taxes else self.price_subtotal
    ...
    ...

而且我还需要在 AccountTax 类中设置自定义公式。我试图覆盖 compute_all() 方法:

# Modification du modèle de Taxes
class AccountTax(models.Model):
_inherit = 'account.tax'

@api.multi
def compute_all(self, nbJrs=1, price_unit=1, currency=None, quantity=1.0, product=None, partner=None):
    ...
    ...
    if not base_values:
            odooAmount = price_unit * quantity
            customAmount = nbJrs * odooAmount
            total_excluded = total_included = base = round( customAmount , prec)
        else:
            total_excluded, total_included, base = base_values
    ...
    ...

    return {
        'taxes': sorted(taxes, key=lambda k: k['sequence']),
        'total_excluded': currency.round(total_excluded) if round_total else total_excluded,
        'total_included': currency.round(total_included) if round_total else total_included,
        'base': base,
    }

我已成功更新我的模块,但当我尝试在新发票中添加产品时,我收到此错误:

... ... ... 文件“/OdooERP/Odoo 11.0/addons/account/models/account_invoice.py”,第 618 行,在 _onchange_invoice_line_ids tax_grouped = self.get_taxes_values()
文件“/OdooERP/Odoo 11.0/addons/account/models/account_invoice.py”,第 889 行,在 get_taxes_values tax = line.invoice_line_tax_ids.compute_all(price_unit, self.currency_id, line.quantity, line.product_id, self.partner_id) ['税']

文件“/OdooERP/Instances/xaymalab/addons/sunu_location_event/models/accountinvoice.py”,第 82 行,compute_all odooAmount = price_unit * quantity

TypeError: *: 'res.currency' 和 'product.product' 的操作数类型不受支持

当我在定义中删除“nbJrs”参数时,它起作用了! 这些值将一个参数向右移动。 有人可以帮我解决这个问题吗?

【问题讨论】:

  • 我的意思是......错误很简单,你试图乘以不支持该操作的东西。只需在错误行之前添加以下打印语句即可查看问题所在: print("price_unit type %s value %s quantity type: %s value %s" % (type(price_unit), price_unit, type(quantity), quantity )。可能是当您从函数 def 中删除参数时,您的调用者正在为输入变量分配不同的值(我猜)。您只能将支持它的东西相乘,从代码的外观来看,您似乎期待那里的数字。
  • 我的回答之外还有一件事:幸运的是方法调用甚至起作用了。

标签: python python-3.x odoo odoo-11


【解决方案1】:

您在覆盖 compute_all()account.tax 时犯了一个错误。似乎您添加了一个新参数nbJrsoriginal Method

def compute_all(self, price_unit, currency=None,
    quantity=1.0, product=None, partner=None)

还有你的:

def compute_all(self, nbJrs=1, price_unit=1, currency=None,
    quantity=1.0, product=None, partner=None):

这种方式行不通。错误是:您将currency 的值与product 的值相乘,因为这些值向右移动了一个参数。

【讨论】:

  • 嗨@CZoellner。谢谢您的答复。你是对的。但有一点我不明白:当我用 PyCharm 调试代码时,我可以看到计算做得很好。我错过了什么?覆盖 compute_all() 方法的正确方法是什么?谢谢。
  • nbJrs 是我添加的自定义字段。它代表天数。我需要将发票/销售订单行金额的计算设置如下:“nbJrs * price_unit * quantity”而不是默认公式:“price_unit * quantity”
  • 为什么需要它作为现有方法的参数?价值从何而来?也许可以在方法中获取值,或者您可以使用上下文将其放入其中。如果是“全局”值,只需创建系统参数 (ir,config,parameter) 或公司设置。
  • 我更新了我的问题以便更好地理解。我需要确定一些事情:当我覆盖一个方法时,我可以添加新参数吗?
  • 取决于方法。在这种情况下,以及 odoo 中的大多数情况:没有。
【解决方案2】:

我是如何解决这个问题的: 我试图通过添加一个新参数来覆盖 compute_all()。这是错误的方式。 我通过添加一个新变量“rentalprice”并将其传递给 compute_all() 来更新 _compute_price():

def _compute_price(self):
    currency = self.invoice_id and self.invoice_id.currency_id or None
    price = self.price_unit * (1 - (self.discount or 0.0) / 100.0)
    # Prix unitaire d'un article pendant la durée totale de location:
    rentalprice = self.nombrejours * price
    taxes = False
    if self.invoice_line_tax_ids:
        taxes = self.invoice_line_tax_ids.compute_all(rentalprice, currency, self.quantity, product=self.product_id, partner=self.invoice_id.partner_id)

我删除了 AccountTax 继承代码,因为我不再需要它,更新了我的模块,现在它可以工作了。

感谢 @CZoellner 帮助我更好地了解我所缺少的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多