From 4061e117d2eaffaf1fdd6b7a165814c11198048b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Wed, 24 Dec 2014 00:23:39 +0100 Subject: [PATCH 01/10] [IMP] account_chart_update: search on code first to match tax codes The tax code code should be more discriminant that the tax code name. --- .../wizard/wizard_chart_update.py | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/account_chart_update/wizard/wizard_chart_update.py b/account_chart_update/wizard/wizard_chart_update.py index 891faf0db..7f73695f0 100644 --- a/account_chart_update/wizard/wizard_chart_update.py +++ b/account_chart_update/wizard/wizard_chart_update.py @@ -356,21 +356,19 @@ class wizard_update_charts_accounts(orm.TransientModel): # In other case tax_code_obj = self.pool['account.tax.code'] root_tax_code_id = wizard.chart_template_id.tax_code_root_id.id - tax_code_name = ((tax_code_template.id == root_tax_code_id) and - wizard.company_id.name or tax_code_template.name) - tax_code_ids = tax_code_obj.search(cr, uid, [ - ('name', '=', tax_code_name), - ('company_id', '=', wizard.company_id.id) - ], context=context) - if not tax_code_ids: - # if we could not match no tax code template name, - # try to match on tax code template code, if any - tax_code_code = tax_code_template.code - if tax_code_code: - tax_code_ids = tax_code_obj.search(cr, uid, [ - ('code', '=', tax_code_code), - ('company_id', '=', wizard.company_id.id) - ], context=context) + tax_code_code = tax_code_template.code + if tax_code_code: + tax_code_ids = tax_code_obj.search(cr, uid, [ + ('code', '=', tax_code_code), + ('company_id', '=', wizard.company_id.id) + ], context=context) + if not tax_code_code or not tax_code_ids: + tax_code_name = ((tax_code_template.id == root_tax_code_id) and + wizard.company_id.name or tax_code_template.name) + tax_code_ids = tax_code_obj.search(cr, uid, [ + ('name', '=', tax_code_name), + ('company_id', '=', wizard.company_id.id) + ], context=context) tax_code_templ_mapping[tax_code_template.id] = (tax_code_ids and tax_code_ids[0] or False) From 1899ce18077742940742f872263578872890ce7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Wed, 24 Dec 2014 00:39:11 +0100 Subject: [PATCH 02/10] [IMP] account_chart_update: compare more fields in tax codes --- .../wizard/wizard_chart_update.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/account_chart_update/wizard/wizard_chart_update.py b/account_chart_update/wizard/wizard_chart_update.py index 7f73695f0..59d4bc2db 100644 --- a/account_chart_update/wizard/wizard_chart_update.py +++ b/account_chart_update/wizard/wizard_chart_update.py @@ -461,6 +461,9 @@ class wizard_update_charts_accounts(orm.TransientModel): notes = "" tax_code = tax_code_obj.browse( cr, uid, tax_code_id, context=context) + if tax_code.name != tax_code_template.name: + notes += _("The name field is different.\n") + modified = True if tax_code.code != tax_code_template.code: notes += _("The code field is different.\n") modified = True @@ -470,7 +473,18 @@ class wizard_update_charts_accounts(orm.TransientModel): if tax_code.sign != tax_code_template.sign: notes += _("The sign field is different.\n") modified = True - # TODO: We could check other account fields for changes... + if tax_code.notprintable != tax_code_template.notprintable: + notes += _("The notprintable field is different.\n") + modified = True + if tax_code.sequence != tax_code_template.sequence: + notes += _("The sequence field is different.\n") + modified = True + if tax_code.parent_id.id != self._map_tax_code_template( + cr, uid, wizard, + tax_code_template_mapping, + tax_code_template.parent_id, context=context): + notes += _("The parent field is different.\n") + modified = True if modified: # Tax code to update. updated_tax_codes += 1 @@ -865,6 +879,8 @@ class wizard_update_charts_accounts(orm.TransientModel): tax_code_template_mapping.get(p_id)), 'company_id': wizard.company_id.id, 'sign': tax_code_template.sign, + 'notprintable': tax_code_template.notprintable, + 'sequence': tax_code_template.sequence, } tax_code_id = None modified = False From 64817c2eba56d3745a7ac1678ffd7dcaf3b5672b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Wed, 24 Dec 2014 08:19:53 +0100 Subject: [PATCH 03/10] [IMP] account_chart_update: deletion of tax codes removed from templates For tax codes, it make sense to propose deletion, since tax codes are normally defined by the tax authority and the user does not customize them. --- .../wizard/wizard_chart_update.py | 71 ++++++++++++++++++- .../wizard/wizard_chart_update_view.xml | 4 ++ 2 files changed, 72 insertions(+), 3 deletions(-) diff --git a/account_chart_update/wizard/wizard_chart_update.py b/account_chart_update/wizard/wizard_chart_update.py index 59d4bc2db..369ef6fc1 100644 --- a/account_chart_update/wizard/wizard_chart_update.py +++ b/account_chart_update/wizard/wizard_chart_update.py @@ -194,6 +194,10 @@ class wizard_update_charts_accounts(orm.TransientModel): 'Updated fiscal positions', readonly=True ), + 'deleted_tax_codes': fields.integer( + 'Deleted tax codes', + readonly=True + ), 'log': fields.text('Messages and Errors', readonly=True) } @@ -495,6 +499,54 @@ class wizard_update_charts_accounts(orm.TransientModel): 'update_tax_code_id': tax_code_id, 'notes': notes, }, context) + # search for tax codes not in the template + # and propose them for deletion + tax_code_ids = tax_code_obj.\ + search(cr, uid, [('company_id', '=', wizard.company_id.id)], + context=context) + tax_code_ids = set(tax_code_ids) + template_tax_code_ids = tax_code_template_mapping.values() + template_tax_code_ids = set(template_tax_code_ids) + tax_code_ids_to_delete = tax_code_ids - template_tax_code_ids + if tax_code_ids_to_delete: + cr.execute("SELECT DISTINCT(tax_code_id) " + "FROM account_move_line " + "WHERE tax_code_id in %(tctd)s " + "UNION " + "SELECT DISTINCT(base_code_id) " + "FROM account_tax " + "WHERE base_code_id in %(tctd)s " + "UNION " + "SELECT DISTINCT(tax_code_id) " + "FROM account_tax " + "WHERE tax_code_id in %(tctd)s " + "UNION " + "SELECT DISTINCT(ref_base_code_id) " + "FROM account_tax " + "WHERE ref_base_code_id in %(tctd)s " + "UNION " + "SELECT DISTINCT(ref_tax_code_id) " + "FROM account_tax " + "WHERE ref_tax_code_id in %(tctd)s ", + {'tctd': tuple(tax_code_ids_to_delete)}) + # do not delete tax codes which are in use, and their parents + tax_code_ids_to_keep = set([row[0] for row in cr.fetchall()]) + for tax_code in tax_code_obj.\ + browse(cr, uid, list(tax_code_ids_to_keep), + context=context): + while tax_code.parent_id: + tax_code_ids_to_keep.add(tax_code.parent_id.id) + tax_code = tax_code.parent_id + for tax_code_id in tax_code_ids_to_delete - tax_code_ids_to_keep: + updated_tax_codes += 1 + wiz_tax_code_obj.create(cr, uid, { + 'tax_code_id': False, + 'update_chart_wizard_id': wizard.id, + 'type': 'deleted', + 'update_tax_code_id': tax_code_id, + 'notes': "To delete: not in the template and unused", + }, context) + return { 'new': new_tax_codes, 'updated': updated_tax_codes, @@ -859,8 +911,12 @@ class wizard_update_charts_accounts(orm.TransientModel): root_tax_code_id = wizard.chart_template_id.tax_code_root_id.id new_tax_codes = 0 updated_tax_codes = 0 + deleted_tax_codes = 0 tax_code_template_mapping = {} + # process new/updated for wiz_tax_code in wizard.tax_code_ids: + if wiz_tax_code.type == 'deleted': + continue tax_code_template = wiz_tax_code.tax_code_id tax_code_name = ((root_tax_code_id == tax_code_template.id) and wizard.company_id.name or tax_code_template.name) @@ -914,9 +970,17 @@ class wizard_update_charts_accounts(orm.TransientModel): tax_code_name, tax_code_template.parent_id.name), True ) + # process deleted + tax_code_ids_to_delete = [wtc.update_tax_code_id.id + for wtc in wizard.tax_code_ids + if wtc.type == 'deleted'] + taxcodes.unlink(cr, uid, tax_code_ids_to_delete, context=context) + log.add(_("Deleted %d tax codes\n" % len(tax_code_ids_to_delete))) + deleted_tax_codes = len(tax_code_ids_to_delete) return { 'new': new_tax_codes, 'updated': updated_tax_codes, + 'deleted': deleted_tax_codes, 'mapping': tax_code_template_mapping } @@ -1475,6 +1539,7 @@ class wizard_update_charts_accounts(orm.TransientModel): 'updated_taxes': taxes_res.get('updated', 0), 'updated_accounts': accounts_res.get('updated', 0), 'updated_fps': fps_res.get('updated', 0), + 'deleted_tax_codes': tax_codes_res.get('deleted', 0), 'log': log(), }, context=context) return _reopen(self, wizard.id, 'wizard.update.chart.accounts') @@ -1491,7 +1556,6 @@ class wizard_update_charts_accounts_tax_code(orm.TransientModel): 'tax_code_id': fields.many2one( 'account.tax.code.template', 'Tax code template', - required=True, ondelete='set null' ), 'update_chart_wizard_id': fields.many2one( @@ -1501,8 +1565,9 @@ class wizard_update_charts_accounts_tax_code(orm.TransientModel): ondelete='cascade' ), 'type': fields.selection([ - ('new', 'New template'), - ('updated', 'Updated template'), + ('new', 'New tax code'), + ('updated', 'Updated tax code'), + ('deleted', 'Tax code to delete'), ], 'Type'), 'update_tax_code_id': fields.many2one( 'account.tax.code', diff --git a/account_chart_update/wizard/wizard_chart_update_view.xml b/account_chart_update/wizard/wizard_chart_update_view.xml index 91c7927d8..f4f1586e6 100644 --- a/account_chart_update/wizard/wizard_chart_update_view.xml +++ b/account_chart_update/wizard/wizard_chart_update_view.xml @@ -113,6 +113,10 @@ + + + +