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

时间:2018-06-22 16:47:20

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

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

我通过以下方式继承了 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,
    }

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

  

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

     

在“ compute_all”中的文件“ /OdooERP/Instances/xaymalab/addons/sunu_location_event/models/accountinvoice.py”第82行 odooAmount = price_unit *数量

     

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

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

2 个答案:

答案 0 :(得分:0)

您在覆盖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的值相乘,因为这些值将一个参数向右移动。

答案 1 :(得分:0)

我如何解决此问题: 我试图通过添加新参数来覆盖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帮助我更好地了解我所缺少的东西。