Search code examples
python-2.7odoo-9

How to add fixed tax automatically in invoice


I'm using odoo 9 and i have created a custom module to add stamp tax in invoice automatically . I have created a new page to enter stamp value and account in res.company. My problem is stamp tax does not add automatically to invoice. Any help please ??

account_invoice.py

from openerp import tools
from openerp.osv import fields,osv

class res_partner(osv.osv):
_name="res.company"
_inherit="res.company"
_columns={
"default_stamp_tax_account":fields.many2one('account.account',"stamp tax account"),
"default_stamp_tax_value":fields.float('stamp tax value'),
}
class account_invoice(osv.osv):
_name="account.invoice"
_inherit="account.invoice"
def create(self,cr,uid,values,context=None):

    stamp_account=self.pool.get('res.company').browse(cr,uid,values.get('company_id')).default_stamp_tax_account.id
    tax_value=self.pool.get('res.company').browse(cr,uid,values.get('company_id')).default_stamp_tax_value
    #pos_account=self.pool.get('pos.order').browse(cr, uid, values.get('company_id')).partner_id.property_account_receivable.id
    print stamp_account
    print tax_value

    #print pos_account

    if stamp_account:
        tax_line=[]
        tax_line.append([0, False, {'base_amount': 0, 'amount': tax_value, 'tax_amount': tax_value, 'name': 'Timbre', 'account_id': stamp_account}])
        values.update({
        'tax_line':tax_line,

        })

        print values
    return super(account_invoice,self).create(cr,uid,values,context)

Solution

  • You will find in the log a line similar to the following:

    WARNING odoo9-demo openerp.models: account.invoice.create() includes unknown fields: tax_line

    which tell you that tax_line field in not defined inside the account.invoice model.

    If you did not define it in that model which is not according to the provided code above, you should use tax_line_ids to add the tax stamp to the invoice and be careful with invoice taxes added when calling:

    super(account_invoice,self).create(cr,uid,values,context)
    

    You can see that taxes are added to the invoice after calling super in create method after calling invoice.compute_taxes().

    So you can use the same logic (compute_taxes) to add your tax.

    • Edit
      Add you tax after calling super.