Merge lp:~openerp-dev/openobject-addons/7.0-coa-tax-from-parent-odo into lp:openobject-addons/7.0

Proposed by Olivier Dony (Odoo)
Status: Merged
Merged at revision: 9499
Proposed branch: lp:~openerp-dev/openobject-addons/7.0-coa-tax-from-parent-odo
Merge into: lp:openobject-addons/7.0
Diff against target: 111 lines (+44/-24)
1 file modified
account/account.py (+44/-24)
To merge this branch: bzr merge lp:~openerp-dev/openobject-addons/7.0-coa-tax-from-parent-odo
Reviewer Review Type Date Requested Status
Pedro Manuel Baeza (community) code review and test Approve
OpenERP Core Team Pending
Review via email: mp+189115@code.launchpad.net

Description of the change

Attempt to handle hierachical Chart of Accounts with taxes belonging to the parent chart.

In relation with the questions on this MP: https://code.launchpad.net/~pedro.baeza/openobject-addons/7.0-updated_l10n_es/+merge/161654

To post a comment you must log in.
Revision history for this message
Pedro Manuel Baeza (pedro.baeza) wrote :

Hi, Olivier, I have check it and it works like a charm. Brilliant solution!

Regards.

review: Approve (code review and test)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'account/account.py'
2--- account/account.py 2013-09-18 14:31:46 +0000
3+++ account/account.py 2013-10-03 15:23:12 +0000
4@@ -3073,6 +3073,20 @@
5 'complete_tax_set': fields.boolean('Complete Set of Taxes', help='This boolean helps you to choose if you want to propose to the user to encode the sales and purchase rates or use the usual m2o fields. This last choice assumes that the set of tax defined for the chosen template is complete'),
6 }
7
8+
9+ def _get_chart_parent_ids(self, cr, uid, chart_template, context=None):
10+ """ Returns the IDs of all ancestor charts, including the chart itself.
11+ (inverse of child_of operator)
12+
13+ :param browse_record chart_template: the account.chart.template record
14+ :return: the IDS of all ancestor charts, including the chart itself.
15+ """
16+ result = [chart_template.id]
17+ while chart_template.parent_id:
18+ chart_template = chart_template.parent_id
19+ result.append(chart_template.id)
20+ return result
21+
22 def onchange_tax_rate(self, cr, uid, ids, rate=False, context=None):
23 return {'value': {'purchase_tax_rate': rate or False}}
24
25@@ -3092,12 +3106,17 @@
26 res['value'].update({'complete_tax_set': data.complete_tax_set, 'currency_id': currency_id})
27 if data.complete_tax_set:
28 # default tax is given by the lowest sequence. For same sequence we will take the latest created as it will be the case for tax created while isntalling the generic chart of account
29- sale_tax_ids = tax_templ_obj.search(cr, uid, [("chart_template_id"
30- , "=", chart_template_id), ('type_tax_use', 'in', ('sale','all'))], order="sequence, id desc")
31- purchase_tax_ids = tax_templ_obj.search(cr, uid, [("chart_template_id"
32- , "=", chart_template_id), ('type_tax_use', 'in', ('purchase','all'))], order="sequence, id desc")
33- res['value'].update({'sale_tax': sale_tax_ids and sale_tax_ids[0] or False, 'purchase_tax': purchase_tax_ids and purchase_tax_ids[0] or False})
34-
35+ chart_ids = self._get_chart_parent_ids(cr, uid, data, context=context)
36+ base_tax_domain = [("chart_template_id", "in", chart_ids), ('parent_id', '=', False)]
37+ sale_tax_domain = base_tax_domain + [('type_tax_use', 'in', ('sale','all'))]
38+ purchase_tax_domain = base_tax_domain + [('type_tax_use', 'in', ('purchase','all'))]
39+ sale_tax_ids = tax_templ_obj.search(cr, uid, sale_tax_domain, order="sequence, id desc")
40+ purchase_tax_ids = tax_templ_obj.search(cr, uid, purchase_tax_domain, order="sequence, id desc")
41+ res['value'].update({'sale_tax': sale_tax_ids and sale_tax_ids[0] or False,
42+ 'purchase_tax': purchase_tax_ids and purchase_tax_ids[0] or False})
43+ res.setdefault('domain', {})
44+ res['domain']['sale_tax'] = repr(sale_tax_domain)
45+ res['domain']['purchase_tax'] = repr(purchase_tax_domain)
46 if data.code_digits:
47 res['value'].update({'code_digits': data.code_digits})
48 return res
49@@ -3105,6 +3124,7 @@
50 def default_get(self, cr, uid, fields, context=None):
51 res = super(wizard_multi_charts_accounts, self).default_get(cr, uid, fields, context=context)
52 tax_templ_obj = self.pool.get('account.tax.template')
53+ account_chart_template = self.pool['account.chart.template']
54 data_obj = self.pool.get('ir.model.data')
55
56 if 'bank_accounts_id' in fields:
57@@ -3119,23 +3139,28 @@
58 currency_id = company_obj.on_change_country(cr, uid, company_id, country_id, context=context)['value']['currency_id']
59 res.update({'currency_id': currency_id})
60
61- ids = self.pool.get('account.chart.template').search(cr, uid, [('visible', '=', True)], context=context)
62+ ids = account_chart_template.search(cr, uid, [('visible', '=', True)], context=context)
63 if ids:
64+ #in order to set default chart which was last created set max of ids.
65+ chart_id = max(ids)
66+ if context.get("default_charts"):
67+ data_ids = data_obj.search(cr, uid, [('model', '=', 'account.chart.template'), ('module', '=', context.get("default_charts"))], limit=1, context=context)
68+ if data_ids:
69+ chart_id = data_obj.browse(cr, uid, data_ids[0], context=context).res_id
70+ chart = account_chart_template.browse(cr, uid, chart_id, context=context)
71+ chart_hierarchy_ids = self._get_chart_parent_ids(cr, uid, chart, context=context)
72 if 'chart_template_id' in fields:
73- #in order to set default chart which was last created set max of ids.
74- chart_id = max(ids)
75- if context.get("default_charts"):
76- data_ids = data_obj.search(cr, uid, [('model', '=', 'account.chart.template'), ('module', '=', context.get("default_charts"))], limit=1, context=context)
77- if data_ids:
78- chart_id = data_obj.browse(cr, uid, data_ids[0], context=context).res_id
79- res.update({'only_one_chart_template': len(ids) == 1, 'chart_template_id': chart_id})
80+ res.update({'only_one_chart_template': len(ids) == 1,
81+ 'chart_template_id': chart_id})
82 if 'sale_tax' in fields:
83- sale_tax_ids = tax_templ_obj.search(cr, uid, [("chart_template_id"
84- , "=", ids[0]), ('type_tax_use', 'in', ('sale','all'))], order="sequence")
85+ sale_tax_ids = tax_templ_obj.search(cr, uid, [("chart_template_id", "in", chart_hierarchy_ids),
86+ ('type_tax_use', 'in', ('sale','all'))],
87+ order="sequence")
88 res.update({'sale_tax': sale_tax_ids and sale_tax_ids[0] or False})
89 if 'purchase_tax' in fields:
90- purchase_tax_ids = tax_templ_obj.search(cr, uid, [("chart_template_id"
91- , "=", ids[0]), ('type_tax_use', 'in', ('purchase','all'))], order="sequence")
92+ purchase_tax_ids = tax_templ_obj.search(cr, uid, [("chart_template_id", "in", chart_hierarchy_ids),
93+ ('type_tax_use', 'in', ('purchase','all'))],
94+ order="sequence")
95 res.update({'purchase_tax': purchase_tax_ids and purchase_tax_ids[0] or False})
96 res.update({
97 'purchase_tax_rate': 15.0,
98@@ -3403,12 +3428,7 @@
99 obj_tax_temp = self.pool.get('account.tax.template')
100 chart_template = obj_wizard.chart_template_id
101 vals = {}
102- # get the ids of all the parents of the selected account chart template
103- current_chart_template = chart_template
104- all_parents = [current_chart_template.id]
105- while current_chart_template.parent_id:
106- current_chart_template = current_chart_template.parent_id
107- all_parents.append(current_chart_template.id)
108+ all_parents = self._get_chart_parent_ids(cr, uid, chart_template, context=context)
109 # create tax templates and tax code templates from purchase_tax_rate and sale_tax_rate fields
110 if not chart_template.complete_tax_set:
111 value = obj_wizard.sale_tax_rate