Odoo 11 如何覆盖 compute_all() 方法?

Odoo 11 How to override compute_all() method?

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

我通过如下继承将自定义字段添加到 AccountInvoiceLine class :

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 class 中设置自定义公式。我试图覆盖 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,
    }

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

... ... ... File "/OdooERP/Odoo 11.0/addons/account/models/account_invoice.py", line 618, in _onchange_invoice_line_ids taxes_grouped = self.get_taxes_values()
File "/OdooERP/Odoo 11.0/addons/account/models/account_invoice.py", line 889, in get_taxes_values taxes = line.invoice_line_tax_ids.compute_all(price_unit, self.currency_id, line.quantity, line.product_id, self.partner_id)['taxes']

File "/OdooERP/Instances/xaymalab/addons/sunu_location_event/models/accountinvoice.py",line 82, in compute_all odooAmount = price_unit * quantity

TypeError: unsupported operand type(s) for *: 'res.currency' and 'product.product'

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

您在覆盖 account.taxcompute_all() 时犯了一个错误。您似乎添加了一个新参数 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 的值,因为这些值向右移动了一个参数。

我是如何解决这个问题的: 我试图通过添加一个新参数来覆盖 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 帮助我更好地理解我遗漏了什么。