1
votes

I want to add a module that will change tax computation (def _compute_all in class account.tax) if special checkbox (field.Boolean) in form view is checked. I use checkbox since I think it gives clearer information to the user is it active or not.

What is the proper way to do this (way that will not break stuff in the future) and that will work with other modules (basically use same data fields as original odoo-made account module)?

view:

<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>

original account module method of interest (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

my module (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')

After this I am a bit lost. Thought of this approach but this would easily break stuff in the future:

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()

Looks bad, feels wrong. Is there a better way to do this?

Are @api.v8 and @api.v7 only legacy code for old Odoo verions or should I pay attention to them too (my module is odoo 9 only)?

1

1 Answers

0
votes

I cannot answer about everything that goes around account.tax, but I can only suggest you that, instead of define a new method you should override the old one:

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