diff options
Diffstat (limited to 'addons/account_tax_python')
| -rw-r--r-- | addons/account_tax_python/__init__.py | 2 | ||||
| -rw-r--r-- | addons/account_tax_python/__openerp__.py | 20 | ||||
| -rw-r--r-- | addons/account_tax_python/account_tax_python.py | 78 | ||||
| -rw-r--r-- | addons/account_tax_python/account_tax_python.xml | 22 |
4 files changed, 122 insertions, 0 deletions
diff --git a/addons/account_tax_python/__init__.py b/addons/account_tax_python/__init__.py new file mode 100644 index 00000000000..c13faa45ed3 --- /dev/null +++ b/addons/account_tax_python/__init__.py @@ -0,0 +1,2 @@ +# -*- coding: utf-8 -*- +import account_tax_python diff --git a/addons/account_tax_python/__openerp__.py b/addons/account_tax_python/__openerp__.py new file mode 100644 index 00000000000..ad9f6abef9d --- /dev/null +++ b/addons/account_tax_python/__openerp__.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +{ + 'name': "Define Taxes as Python Code", + 'summary': """ + Allows to use python code to define taxes""", + 'description': """ + A tax defined as python code consists in two snippets of python code which are executed in a local environment containing data such as the unit price, product or partner. + + "Applicable Code" defines if the tax is to be applied. + + "Python Code" defines the amount of the tax. + """, + 'author': "Odoo SA", + 'category': 'Accounting & Finance', + 'version': '1.0', + 'depends': ['account'], + 'data': [ + 'account_tax_python.xml', + ], +} diff --git a/addons/account_tax_python/account_tax_python.py b/addons/account_tax_python/account_tax_python.py new file mode 100644 index 00000000000..3ab2bba5407 --- /dev/null +++ b/addons/account_tax_python/account_tax_python.py @@ -0,0 +1,78 @@ +# -*- coding: utf-8 -*- + +from openerp import models, fields, api + +class AccountTaxPython(models.Model): + _inherit = "account.tax" + + amount_type = fields.Selection(selection_add=[('code', 'Python Code')]) + + python_compute = fields.Text(string='Python Code', default="result = price_unit * 0.10", + help="Compute the amount of the tax by setting the variable 'result'.\n\n" + ":param base_amount: float, actual amount on which the tax is applied\n" + ":param price_unit: float\n" + ":param quantity: float\n" + ":param product: product.product recordset singleton or None\n" + ":param partner: res.partner recordset singleton or None") + python_applicable = fields.Text(string='Applicable Code', default="result = True", + help="Determine if the tax will be applied by setting the variable 'result' to True or False.\n\n" + ":param price_unit: float\n" + ":param quantity: float\n" + ":param product: product.product recordset singleton or None\n" + ":param partner: res.partner recordset singleton or None") + + + def _compute_amount(self, base_amount, price_unit, quantity=1.0, product=None, partner=None): + self.ensure_one() + if self.amount_type == 'code': + localdict = {'base_amount': base_amount, 'price_unit':price_unit, 'quantity': quantity, 'product':product, 'partner':partner} + exec self.python_compute in localdict + return localdict['result'] + return super(AccountTaxPython, self)._compute_amount(base_amount, price_unit, quantity, product, partner) + + @api.v8 + def compute_all(self, price_unit, currency=None, quantity=1.0, product=None, partner=None): + taxes = self.env['account.tax'] + for tax in self: + localdict = {'price_unit':price_unit, 'quantity': quantity, 'product':product, 'partner':partner} + exec tax.python_applicable in localdict + if localdict.get('result', False): + taxes += tax + return super(AccountTaxPython, taxes).compute_all(price_unit, currency, quantity, product, partner) + + @api.v7 + def compute_all(self, cr, uid, ids, price_unit, currency_id=None, quantity=1.0, product_id=None, partner_id=None, context=None): + currency = currency_id and self.pool.get('res.currency').browse(cr, uid, currency_id, context=context) or None + product = product_id and self.pool.get('product.product').browse(cr, uid, product_id, context=context) or None + partner = partner_id and self.pool.get('res.partner').browse(cr, uid, partner_id, context=context) or None + ids = isinstance(ids, (int, long)) and [ids] or ids + recs = self.browse(cr, uid, ids, context=context) + return recs.compute_all(price_unit, currency, quantity, product, partner) + +class AccountTaxTemplatePython(models.Model): + _inherit = 'account.tax.template' + + amount_type = fields.Selection(selection_add=[('code', 'Python Code')]) + + python_compute = fields.Text(string='Python Code', default="result = price_unit * 0.10", + help="Compute the amount of the tax by setting the variable 'result'.\n\n" + ":param base_amount: float, actual amount on which the tax is applied\n" + ":param price_unit: float\n" + ":param quantity: float\n" + ":param product: product.product recordset singleton or None\n" + ":param partner: res.partner recordset singleton or None") + python_applicable = fields.Text(string='Applicable Code', default="result = True", + help="Determine if the tax will be applied by setting the variable 'result' to True or False.\n\n" + ":param price_unit: float\n" + ":param quantity: float\n" + ":param product: product.product recordset singleton or None\n" + ":param partner: res.partner recordset singleton or None") + + def _get_tax_vals(self, company): + """ This method generates a dictionnary of all the values for the tax that will be created. + """ + self.ensure_one() + res = super(AccountTaxTemplatePython, self)._get_tax_vals(company) + res['python_compute'] = self.python_compute + res['python_applicable'] = self.python_applicable + return res diff --git a/addons/account_tax_python/account_tax_python.xml b/addons/account_tax_python/account_tax_python.xml new file mode 100644 index 00000000000..eaeabedfb11 --- /dev/null +++ b/addons/account_tax_python/account_tax_python.xml @@ -0,0 +1,22 @@ +<?xml version="1.0" encoding="utf-8"?> +<openerp> + <data> + <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="//field[@name='children_tax_ids']" position="before"> + <group string="Tax Computation By Python Code" attrs="{'invisible': [('amount_type','!=','code')]}"> + <group> + <field name="python_compute" attrs="{'required':[('amount_type','=','code')]}" /> + </group> + <group> + <field name="python_applicable" attrs="{'required':[('amount_type','=','code')]}" /> + </group> + </group> + </xpath> + </field> + </record> + </data> +</openerp> |
