如何在Odoo中更改模块方法并避免破坏事物?

时间:2015-12-18 10:15:07

标签: python python-2.7 openerp odoo-9

如果特殊复选框( field.Boolean def _compute_all class account.tax 中) >)在表单视图中进行检查。我使用复选框,因为我认为它为用户提供更清晰的信息是否有效。

这样做的正确方法是什么(未来不会破坏的方式)和其他模块(基本上使用与原始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)?

1 个答案:

答案 0 :(得分:0)

我无法回答有关account.tax的所有内容,但我只能建议您,而不是定义一个新方法,您应该覆盖旧方法:

def _compute_amount(self, base_amount, price_unit, quantity=1.0, product=None, partner=None):
    if self.custom_tax:
        #your code here
        #res = ...
    else:
        res = super(MyTaxCompute, self)._compute_amount(base_amount, price_unit, quantity, product, partner)
    return res