如果选中表单视图中的特殊复选框(field.Boolean ),我想添加一个模块,该模块将更改税收计算(class account.tax中的def _compute_all )。我使用复选框,因为我认为它为用户提供了更清晰的信息,它是否处于活动状态。
这样做的正确方法是什么(将来不会破坏东西)并且可以与其他模块一起使用(基本上使用与原始odoo制作的帐户模块相同的数据字段)?
看法:
<record id="view_tax_form_inherited" model="ir.ui.view">
<field name="name">account.tax.form.inherited</field>
<field name="model">account.tax</field>
<field name="inherit_id" ref="account.view_tax_form"/>
<field name="arch" type="xml">
<xpath expr="//page/group" position="after">
<!-- my custom boolean selection field -->
<field name="custom_tax" string="Custom tax computation"/>
<!-- my custom float fields to put tax rates in -->
<field name="custom_tax_field1" string="Tax 1"/>
<field name="custom_tax_field2" string="Tax 2"/>
</xpath>
</field>
</record>
感兴趣的原始帐户模块方法(python):
def _compute_amount(self, base_amount, price_unit, quantity=1.0, product=None, partner=None):
self.ensure_one()
if self.amount_type == 'fixed':
return math.copysign(self.amount, base_amount) * quantity
if (self.amount_type == 'percent' and not self.price_include) or (self.amount_type == 'division' and self.price_include):
return base_amount * self.amount / 100
if self.amount_type == 'percent' and self.price_include:
return base_amount - (base_amount / (1 + self.amount / 100))
if self.amount_type == 'division' and not self.price_include:
return base_amount / (1 - self.amount / 100) - base_amount
我的模块(python):
class MyTaxCompute(models.Model):
_inherit = 'account.tax'
custom_tax = fields.Boolean(string='Custom tax computation')
custom_tax_field1 = fields.Float(string='Tax 1')
custom_tax_field2 = fields.Float(string='Tax 2')
在此之后,我有点失落。想到了这种方法,但这很容易在将来破坏一些东西:
def newCompute (self, base_amount, price_unit, quantity=1.0, product=None, partner=None):
# custom computation code that uses custom_tax_field 1 and 2, and all of the data fields as original
def checkCustomTaxSelection (self):
if self.custom_tax =='1':
return self.newCompute()
else:
return self._compute_amount()
不好看,感觉不对。有一个更好的方法吗?
@api.v8 和 @api.v7 是否只是旧 Odoo 版本的遗留代码,还是我也应该注意它们(我的模块只有 odoo 9)?