我目前在我的模块中实现了一项功能,该功能从订单行中总计税额。我的模块有一个订单行部分(非常类似于销售模块),它允许添加多个产品并将其价格显示在旁边。看看我如何添加此功能here。在底部,我有一个小计页脚,其中显示三个字段:amount_axaxed,amount_tax和amount_total。计税工作正常(存储在amount_tax),但我无法在该页脚中显示税额。下面是相关代码:Odoo 10 - Singleton错误 - 无法获得订单行中特定字段的总和
models.py
# -*- coding: utf-8 -*-
from odoo import models, fields, api
from odoo.addons import decimal_precision as dp
class mymodule(models.Model):
_name = 'mymodule.mymodule'
_description = 'My Module'
currency_id = fields.Many2one('res.currency', string='Currency')
operations = fields.One2many('mymodule.line', 'mymodule_id', 'Order Lines')
amount_untaxed = fields.Monetary(string='Untaxed Amount',
default=0,
compute='_compute_untaxed',
currency_field='currency_id')
amount_tax = fields.Monetary(string='Taxes',
default=0,
compute='_compute_taxes',
currency_field='currency_id')
amount_total = fields.Monetary(string='Total',
default=0,
compute='_compute_total',
currency_field='currency_id')
# Sum all prices into untaxed total
@api.depends('operations.price')
def _compute_untaxed(self):
for record in self:
record.amount_untaxed = sum(line.price for line in record.operations)
# Sum all prices into taxed total
@api.depends('operations.total')
def _compute_total(self):
for record in self:
record.amount_total = sum(line.total for line in record.operations)
# Sum all taxes
@api.depends('operations.taxes')
def _compute_taxes(self):
for record in self:
record.amount_tax = sum(line.taxes for line in record.operations)
class mymodule_line(models.Model):
_name = 'mymodule.line'
_description = 'Order Line'
mymodule_id = fields.Many2one('mymodule.mymodule')
product_id = fields.Many2one('product.template',
string='Product',
required=False) # Product name
tax_id = fields.Many2many('account.tax',
string='Taxes',
store=False) # Product default taxes
taxes = fields.Float(string='Taxes', readonly=False)
price = fields.Float(string='Price')
total = fields.Float(string='Total', readonly=False)
# Display default values based on product selected
@api.onchange('product_id')
def _onchange_product_id(self):
self.price = self.product_id.lst_price
self.tax_id = self.product_id.taxes_id
# Compute total and tax amount for selected product
@api.onchange('price', 'tax_id')
@api.depends('total', 'tax_id.amount', 'price', 'taxes')
def _compute_total(self):
self.taxes = (0.01 * self.tax_id.amount * self.price)
self.total = self.taxes + self.price
views.xml
<record id="mymodule.form" model="ir.ui.view">
<field name="name">My Module Form</field>
<field name="model">mymodule.mymodule</field>
<field name="arch" type="xml">
<form>
<sheet>
<notebook>
<!-- Order Lines -->
<page string="Operations">
<group class="oe_subtotal_footer oe_right" colspan="2" name="sale_total">
<field name="amount_untaxed"/>
<field name="amount_tax"/>
<div class="oe_subtotal_footer_separator oe_inline o_td_label">
</div>
<field name="amount_total" nolabel="1" class="oe_subtotal_footer_separator"/>
</group>
</page>
</notebook>
</sheet>
</form>
</field>
</record>
有趣的是,如果我更改以下行正因为如此,添加计算论点,税额将正确显示。
taxes = fields.Float(string='Taxes', readonly=False, compute='_compute_total')
但是,如果我在订单行中添加多个产品,然后尝试保存该记录,则会出现单例错误。
ValueError: Expected singleton: mymodule.line(346, 347)
添加@ api.one装饰上面我_compute_total功能则可以让我保存记录,但一旦它被保存计税消失。
这里是我的小计页脚应该是什么样子的参考图像:Subtotal Footer
我感谢您的帮助!
下面的链接将有助于你在未来。 https://odedrabhavesh.blogspot.in/2017/02/valueerror-expected-singleton-in-odoo.html –