【发布时间】: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