diff --git a/account_chart_update/wizard/wizard_chart_update.py b/account_chart_update/wizard/wizard_chart_update.py index 7389cbf2..be17b466 100644 --- a/account_chart_update/wizard/wizard_chart_update.py +++ b/account_chart_update/wizard/wizard_chart_update.py @@ -101,8 +101,10 @@ class wizard_update_charts_accounts(orm.TransientModel): ('done', 'Wizard completed') ], 'Status', readonly=True), 'company_id': fields.many2one('res.company', 'Company', required=True, ondelete='set null'), - 'chart_template_id': fields.many2one('account.chart.template', 'Chart Template', required=True, ondelete='set null'), - 'code_digits': fields.integer('# of Digits', required=True, help="No. of Digits to use for account code. Make sure it is the same number as existing accounts."), + 'chart_template_id': fields.many2one( + 'account.chart.template', 'Chart Template', ondelete='cascade', + required=True), + 'code_digits': fields.integer('# of digits', required=True, help="No. of digits to use for account code. Make sure it is the same number as existing accounts."), 'lang': fields.selection(_get_lang_selection_options, 'Language', size=5, help="For records searched by name (taxes, tax codes, fiscal positions), the template name will be matched against the record name on this language."), 'update_tax_code': fields.boolean('Update tax codes', help="Existing tax codes are updated. Tax codes are searched by name."), 'update_tax': fields.boolean('Update taxes', help="Existing taxes are updated. Taxes are searched by name."), @@ -242,108 +244,141 @@ class wizard_update_charts_accounts(orm.TransientModel): ########################################################################## # Helper methods ########################################################################## - """ + + def _map_tax_template(self, cr, uid, wizard, tax_templ_mapping, + tax_templ, context=None): + """ Adds a tax template -> tax id to the mapping. - """ - def _map_tax_template(self, cr, uid, wizard, tax_template_mapping, tax_template, context=None): - if tax_template and not tax_template_mapping.get(tax_template.id): - taxes = self.pool.get('account.tax') - tax_ids = taxes.search(cr, uid, [ - ('name', '=', tax_template.name), - ('company_id', '=', wizard.company_id.id) - ], context=context) - if tax_ids: - tax_template_mapping[tax_template.id] = tax_ids[0] + """ + if not tax_templ: + return False + if tax_templ_mapping.get(tax_templ.id): + return tax_templ_mapping[tax_templ.id] + # In other case + tax_obj = self.pool['account.tax'] + criteria = ['|', + ('name', '=', tax_templ.name), + ('description', '=', tax_templ.name)] + if tax_templ.description: + criteria = (['|', '|'] + criteria + + [('description', '=', tax_templ.description), + ('name', '=', tax_templ.description)]) + tax_ids = tax_obj.search(cr, uid, criteria, context=context) + tax_templ_mapping[tax_templ.id] = tax_ids and tax_ids[0] or False + return tax_templ_mapping[tax_templ.id] - """ - Adds a tax code template -> tax code id to the mapping. - """ - def _map_tax_code_template(self, cr, uid, wizard, tax_code_template_mapping, tax_code_template, context=None): - if tax_code_template and not tax_code_template_mapping.get(tax_code_template.id): - tax_codes = self.pool.get('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_codes.search(cr, uid, [ - ('name', '=', tax_code_name), - ('company_id', '=', wizard.company_id.id) - ]) - 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_codes.search(cr, uid, [ - ('code', '=', tax_code_code), - ('company_id', '=', wizard.company_id.id) - ]) - if tax_code_ids: - tax_code_template_mapping[ - tax_code_template.id] = tax_code_ids[0] + def _map_tax_code_template(self, cr, uid, wizard, + tax_code_templ_mapping, tax_code_template, + context=None): + """ + Adds a tax code template -> tax code id to the mapping. + """ + if not tax_code_template: + return False + if not wizard.chart_template_id: + return False + if tax_code_templ_mapping.get(tax_code_template.id): + return tax_code_templ_mapping[tax_code_template.id] + # 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_templ_mapping[tax_code_template.id] = (tax_code_ids and + tax_code_ids[0] or + False) + return tax_code_templ_mapping[tax_code_template.id] - """ - Adds an account template -> account id to the mapping - """ - def _map_account_template(self, cr, uid, wizard, account_template_mapping, account_template, context=None): - if account_template and not account_template_mapping.get(account_template.id): - accounts = self.pool.get('account.account') - code = account_template.code or '' - if account_template.type != 'view': - if len(code) > 0 and len(code) <= wizard.code_digits: - code = '%s%s' % ( - code, '0' * (wizard.code_digits - len(code))) - account_ids = accounts.search(cr, uid, [ - ('code', '=', code), - ('company_id', '=', wizard.company_id.id) - ], context=context) - if account_ids: - account_template_mapping[account_template.id] = account_ids[0] - """ - Adds a fiscal position template -> fiscal position id to the mapping. - """ - def _map_fp_template(self, cr, uid, wizard, fp_template_mapping, fp_template, context=None): - if fp_template and not fp_template_mapping.get(fp_template.id): - fiscalpositions = self.pool.get('account.fiscal.position') - fp_ids = fiscalpositions.search(cr, uid, [ - ('name', '=', fp_template.name), - ('company_id', '=', wizard.company_id.id) - ], context=context) - if fp_ids: - fp_template_mapping[fp_template.id] = fp_ids[0] + def _map_account_template(self, cr, uid, wizard, acc_templ_mapping, + acc_templ, context=None): + """ + Adds an account template -> account id to the mapping + """ + if not acc_templ: + return False + if acc_templ_mapping.get(acc_templ.id): + return acc_templ_mapping[acc_templ.id] + # In other case + acc_obj = self.pool['account.account'] + code = acc_templ.code or '' + if acc_templ.type != 'view': + if code and len(code) <= wizard.code_digits: + code = '%s%s' % (code, '0' * (wizard.code_digits - len(code))) + acc_ids = acc_obj.search(cr, uid, [ + ('code', '=', code), + ('company_id', '=', wizard.company_id.id) + ], context=context) + acc_templ_mapping[acc_templ.id] = acc_ids and acc_ids[0] or False + return acc_templ_mapping[acc_templ.id] - """ - Search for, and load, tax code templates to create/update. - """ - def _find_tax_codes(self, cr, uid, wizard, context=None): + def _map_fp_template(self, cr, uid, wizard, fp_templ_mapping, fp_template, context=None): + """ + Adds a fiscal position template -> fiscal position id to the mapping. + """ + if not fp_template: + return False + if fp_templ_mapping.get(fp_template.id): + return fp_templ_mapping[fp_template.id] + # In other case + fp_obj = self.pool['account.fiscal.position'] + fp_ids = fp_obj.search(cr, uid, + [('name', '=', fp_template.name), + ('company_id', '=', wizard.company_id.id)], + context=context) + fp_templ_mapping[fp_template.id] = fp_ids and fp_ids[0] or False + return fp_templ_mapping[fp_template.id] + + def _find_tax_codes(self, cr, uid, wizard, chart_template_ids, + context=None): + """ + Search for, and load, tax code templates to create/update. + + @param chart_template_ids: IDs of the chart templates to look on, + calculated once in the calling method. + """ + if not wizard.chart_template_id: + return {} new_tax_codes = 0 updated_tax_codes = 0 tax_code_template_mapping = {} - - taxes_codes_templates = self.pool.get('account.tax.code.template') - taxcodes = self.pool.get('account.tax.code') - wiz_taxcodes = self.pool.get('wizard.update.charts.accounts.tax.code') - + tax_code_templ_obj = self.pool['account.tax.code.template'] + tax_code_obj = self.pool['account.tax.code'] + wiz_tax_code_obj = self.pool['wizard.update.charts.accounts.tax.code'] # Remove previous tax codes - wiz_taxcodes.unlink( - cr, uid, wiz_taxcodes.search(cr, uid, [])) - - # + wiz_tax_code_obj.unlink(cr, uid, wiz_tax_code_obj.search(cr, uid, [])) # Search for new / updated tax codes # root_tax_code_id = wizard.chart_template_id.tax_code_root_id.id - children_tax_code_template = taxes_codes_templates.search(cr, uid, [( - 'parent_id', 'child_of', [root_tax_code_id])], order='id') - for tax_code_template in taxes_codes_templates.browse(cr, uid, children_tax_code_template): + children_tax_code_template = tax_code_templ_obj.search(cr, uid, [( + 'parent_id', 'child_of', [root_tax_code_id])], order='id', + context=context) + for tax_code_template in tax_code_templ_obj.browse(cr, uid, + children_tax_code_template, context=context): # Ensure the tax code template is on the map (search for the mapped # tax code id). - self._map_tax_code_template(cr, uid, wizard, tax_code_template_mapping, tax_code_template, context) - - tax_code_id = tax_code_template_mapping.get(tax_code_template.id) + tax_code_id = self._map_tax_code_template(cr, uid, wizard, + tax_code_template_mapping, + tax_code_template, context=context) if not tax_code_id: new_tax_codes += 1 - wiz_taxcodes.create(cr, uid, { + wiz_tax_code_obj.create(cr, uid, { 'tax_code_id': tax_code_template.id, 'update_chart_wizard_id': wizard.id, 'type': 'new', + 'notes': _('Name or code not found.'), }, context) elif wizard.update_tax_code: # @@ -351,9 +386,8 @@ class wizard_update_charts_accounts(orm.TransientModel): # modified = False notes = "" - tax_code = taxcodes.browse( + tax_code = tax_code_obj.browse( cr, uid, tax_code_id, context=context) - if tax_code.code != tax_code_template.code: notes += _("The code field is different.\n") modified = True @@ -363,59 +397,62 @@ 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 modified: # # Tax code to update. # updated_tax_codes += 1 - wiz_taxcodes.create(cr, uid, { + wiz_tax_code_obj.create(cr, uid, { 'tax_code_id': tax_code_template.id, 'update_chart_wizard_id': wizard.id, 'type': 'updated', 'update_tax_code_id': tax_code_id, 'notes': notes, }, context) - return { 'new': new_tax_codes, 'updated': updated_tax_codes, 'mapping': tax_code_template_mapping } - def _find_taxes(self, cr, uid, wizard, context=None): + def _find_taxes(self, cr, uid, wizard, chart_template_ids, context=None): + """ + Search for, and load, tax templates to create/update. + + @param chart_template_ids: IDs of the chart templates to look on, + calculated once in the calling method. + """ new_taxes = 0 updated_taxes = 0 - tax_template_mapping = {} - - taxes = self.pool.get('account.tax') - wiz_taxes = self.pool.get('wizard.update.charts.accounts.tax') - + tax_templ_mapping = {} + tax_obj = self.pool['account.tax'] + tax_templ_obj = self.pool['account.tax.template'] + wiz_taxes_obj = self.pool['wizard.update.charts.accounts.tax'] delay_wiz_tax = [] # Remove previous taxes - wiz_taxes.unlink(cr, uid, wiz_taxes.search(cr, uid, [])) - - # + wiz_taxes_ids = wiz_taxes_obj.search(cr, uid, [], context=context) + wiz_taxes_obj.unlink(cr, uid, wiz_taxes_ids, context=context) # Search for new / updated taxes - # - for tax_template in wizard.chart_template_id.tax_template_ids: - # Ensure the tax template is on the map (search for the mapped tax - # id). - self._map_tax_template( - cr, uid, wizard, tax_template_mapping, tax_template, context) - - tax_id = tax_template_mapping.get(tax_template.id) + tax_templ_ids = tax_templ_obj.search( + cr, uid, [('chart_template_id', 'in', chart_template_ids)], + context=context) + for tax_templ in tax_templ_obj.browse(cr, uid, tax_templ_ids, + context=context): + # Ensure tax template is on the map (search for the mapped tax id) + tax_id = self._map_tax_template(cr, uid, wizard, + tax_templ_mapping, + tax_templ, context=context) if not tax_id: new_taxes += 1 vals_wiz = { - 'tax_id': tax_template.id, + 'tax_id': tax_templ.id, 'update_chart_wizard_id': wizard.id, 'type': 'new', + 'notes': _('Name or description not found.'), } - if not tax_template.parent_id: - wiz_taxes.create(cr, uid, vals_wiz, context) + if not tax_templ.parent_id: + wiz_taxes_obj.create(cr, uid, vals_wiz, context) else: delay_wiz_tax.append(vals_wiz) elif wizard.update_tax: @@ -424,99 +461,107 @@ class wizard_update_charts_accounts(orm.TransientModel): # modified = False notes = "" - tax = taxes.browse(cr, uid, tax_id, context=context) - if tax.sequence != tax_template.sequence: + tax = tax_obj.browse(cr, uid, tax_id, context=context) + if tax.sequence != tax_templ.sequence: notes += _("The sequence field is different.\n") modified = True - if tax.amount != tax_template.amount: + if tax.amount != tax_templ.amount: notes += _("The amount field is different.\n") modified = True - if tax.type != tax_template.type: + if tax.type != tax_templ.type: notes += _("The type field is different.\n") modified = True - if tax.applicable_type != tax_template.applicable_type: + if tax.applicable_type != tax_templ.applicable_type: notes += _("The applicable type field is different.\n") modified = True - if tax.domain != tax_template.domain: + if tax.domain != tax_templ.domain: notes += _("The domain field is different.\n") modified = True - if tax.child_depend != tax_template.child_depend: + if tax.child_depend != tax_templ.child_depend: notes += _("The child depend field is different.\n") modified = True - if tax.python_compute != tax_template.python_compute: + if tax.python_compute != tax_templ.python_compute: notes += _("The python compute field is different.\n") modified = True - # if tax.tax_group != tax_template.tax_group: + # if tax.tax_group != tax_templ.tax_group: # notes += _("The tax group field is different.\n") # modified = True - if tax.base_sign != tax_template.base_sign: + if tax.base_sign != tax_templ.base_sign: notes += _("The base sign field is different.\n") modified = True - if tax.tax_sign != tax_template.tax_sign: + if tax.tax_sign != tax_templ.tax_sign: notes += _("The tax sign field is different.\n") modified = True - if tax.include_base_amount != tax_template.include_base_amount: + if tax.include_base_amount != tax_templ.include_base_amount: notes += _("The include base amount field is different.\n") modified = True - if tax.type_tax_use != tax_template.type_tax_use: + if tax.type_tax_use != tax_templ.type_tax_use: notes += _("The type tax use field is different.\n") modified = True # TODO: We could check other tax fields for changes... - if modified: # # Tax code to update. # updated_taxes += 1 - wiz_taxes.create(cr, uid, { - 'tax_id': tax_template.id, + wiz_taxes_obj.create(cr, uid, { + 'tax_id': tax_templ.id, 'update_chart_wizard_id': wizard.id, 'type': 'updated', 'update_tax_id': tax_id, 'notes': notes, }, context) - for delay_vals_wiz in delay_wiz_tax: - wiz_taxes.create(cr, uid, delay_vals_wiz, context) + wiz_taxes_obj.create(cr, uid, delay_vals_wiz, context) - return {'new': new_taxes, 'updated': updated_taxes, 'mapping': tax_template_mapping} + return {'new': new_taxes, 'updated': updated_taxes, 'mapping': tax_templ_mapping} """ Search for, and load, account templates to create/update. """ def _find_accounts(self, cr, uid, wizard, context=None): + """ + Search for, and load, account templates to create/update. + """ + if not wizard.chart_template_id: + return {} new_accounts = 0 updated_accounts = 0 - account_template_mapping = {} - - accounts = self.pool.get('account.account') - accounts_template = self.pool.get('account.account.template') - wiz_accounts = self.pool.get( - 'wizard.update.charts.accounts.account') - + acc_templ_mapping = {} + acc_obj = self.pool['account.account'] + acc_templ_obj = self.pool['account.account.template'] + wiz_accounts = self.pool['wizard.update.charts.accounts.account'] # Remove previous accounts - wiz_accounts.unlink( - cr, uid, wiz_accounts.search(cr, uid, [])) - - # + wiz_accounts_ids = wiz_accounts.search(cr, uid, [], context=context) + wiz_accounts.unlink(cr, uid, wiz_accounts_ids, context=context) # Search for new / updated accounts # root_account_id = wizard.chart_template_id.account_root_id.id - children_acc_template = accounts_template.search(cr, uid, [( - 'parent_id', 'child_of', [root_account_id])], context=context) - children_acc_template.sort() - for account_template in accounts_template.browse(cr, uid, children_acc_template, context=context): + acc_templ_criteria = [('chart_template_id', + '=', + wizard.chart_template_id.id)] + if root_account_id: + acc_templ_criteria = (['|'] + acc_templ_criteria + + ['&', ('parent_id', 'child_of', [root_account_id]), + ('chart_template_id', '=', False)]) + acc_ids = acc_templ_obj.search(cr, uid, acc_templ_criteria, + context=context) + acc_ids.sort() + for acc_templ in acc_templ_obj.browse(cr, uid, acc_ids, + context=context): # Ensure the account template is on the map (search for the mapped # account id). - self._map_account_template(cr, uid, wizard, account_template_mapping, account_template, context) - - account_id = account_template_mapping.get(account_template.id) + account_id = self._map_account_template(cr, uid, wizard, + acc_templ_mapping, + acc_templ, + context=context) if not account_id: new_accounts += 1 wiz_accounts.create(cr, uid, { - 'account_id': account_template.id, + 'account_id': acc_templ.id, 'update_chart_wizard_id': wizard.id, 'type': 'new', + 'notes': _('Code not found.'), }, context) elif wizard.update_account: # @@ -524,141 +569,150 @@ class wizard_update_charts_accounts(orm.TransientModel): # modified = False notes = "" - account = accounts.browse( - cr, uid, account_id, context=context) - - if account.name != account_template.name and account.name != wizard.company_id.name: + account = acc_obj.browse(cr, uid, account_id, context=context) + if account.name != acc_templ.name and account.name != wizard.company_id.name: notes += _("The name is different.\n") modified = True - if account.type != account_template.type: + if account.type != acc_templ.type: notes += _("The type is different.\n") modified = True - if account.user_type != account_template.user_type: + if account.user_type != acc_templ.user_type: notes += _("The user type is different.\n") modified = True - if account.reconcile != account_template.reconcile: + if account.reconcile != acc_templ.reconcile: notes += _("The reconcile is different.\n") modified = True - # TODO: We could check other account fields for changes... - if modified: # # Account to update. # updated_accounts += 1 wiz_accounts.create(cr, uid, { - 'account_id': account_template.id, + 'account_id': acc_templ.id, 'update_chart_wizard_id': wizard.id, 'type': 'updated', 'update_account_id': account_id, 'notes': notes, }, context) + return { + 'new': new_accounts, + 'updated': updated_accounts, + 'mapping': acc_templ_mapping + } - return {'new': new_accounts, 'updated': updated_accounts, 'mapping': account_template_mapping} - - """ - Search for, and load, fiscal position templates to create/update. - """ - def _find_fiscal_positions(self, cr, uid, wizard, context=None): + def _find_fiscal_positions(self, cr, uid, wizard, chart_template_ids, + context=None): + """ + Search for, and load, fiscal position templates to create/update. + + @param chart_template_ids: IDs of the chart templates to look on, + calculated once in the calling method. + """ new_fps = 0 updated_fps = 0 - fp_template_mapping = {} - - fiscalpostitions_template = self.pool.get('account.fiscal.position.template') - fiscalpositions = self.pool.get('account.fiscal.position') - wiz_fiscalpositions = self.pool.get( - 'wizard.update.charts.accounts.fiscal.position') - + fp_templ_mapping = {} + tax_templ_mapping = {} + acc_templ_mapping = {} + fp_templ_obj = self.pool['account.fiscal.position.template'] + fp_obj = self.pool['account.fiscal.position'] + wiz_fp = self.pool['wizard.update.charts.accounts.fiscal.position'] # Remove previous fiscal positions - wiz_fiscalpositions.unlink(cr, uid, wiz_fiscalpositions.search(cr, uid, [])) - - # + wiz_fp.unlink(cr, uid, wiz_fp.search(cr, uid, [])) # Search for new / updated fiscal positions - # - fp_template_ids = fiscalpostitions_template.search(cr, uid, [('chart_template_id', '=', wizard.chart_template_id.id)], context=context) - for fp_template in fiscalpostitions_template.browse(cr, uid, fp_template_ids, context=context): + fp_template_ids = fp_templ_obj.search(cr, uid, + [('chart_template_id', + 'in', + chart_template_ids)], + context=context) + for fp_templ in fp_templ_obj.browse(cr, uid, fp_template_ids, + context=context): # Ensure the fiscal position template is on the map (search for the # mapped fiscal position id). - self._map_fp_template( - cr, uid, wizard, fp_template_mapping, fp_template, context) - - fp_id = fp_template_mapping.get(fp_template.id) + fp_id = self._map_fp_template(cr, uid, wizard, fp_templ_mapping, + fp_templ, context=context) if not fp_id: # # New fiscal position template. # new_fps += 1 - wiz_fiscalpositions.create(cr, uid, { - 'fiscal_position_id': fp_template.id, + wiz_fp.create(cr, uid, { + 'fiscal_position_id': fp_templ.id, 'update_chart_wizard_id': wizard.id, 'type': 'new', - }, context) - elif wizard.update_fiscal_position: - # - # Check the fiscal position for changes. - # - modified = False - notes = "" - fp = fiscalpositions.browse(cr, uid, fp_id, context=context) - - # - # Check fiscal position taxes for changes. - # - if fp_template.tax_ids and fp.tax_ids: - for fp_tax_template in fp_template.tax_ids: - found = False - for fp_tax in fp.tax_ids: - if fp_tax.tax_src_id.name == fp_tax_template.tax_src_id.name: - if fp_tax_template.tax_dest_id and fp_tax.tax_dest_id: - if fp_tax.tax_dest_id.name == fp_tax_template.tax_dest_id.name: - found = True - break - elif not fp_tax_template.tax_dest_id and not fp_tax.tax_dest_id: + 'notes': _('Name not found.'), + }, context=context) + continue + # Check the fiscal position for changes + modified = False + notes = "" + fp = fp_obj.browse(cr, uid, fp_id, context=context) + # Check fiscal position taxes for changes. + if fp_templ.tax_ids and fp.tax_ids: + for fp_tax_templ in fp_templ.tax_ids: + found = False + tax_src_id = self._map_tax_template(cr, uid, wizard, + tax_templ_mapping, + fp_tax_templ.tax_src_id, + context=None) + tax_dest_id = self._map_tax_template(cr, uid, wizard, + tax_templ_mapping, + fp_tax_templ.tax_dest_id, + context=None) + for fp_tax in fp.tax_ids: + if fp_tax.tax_src_id.id == tax_src_id: + if not fp_tax.tax_dest_id: + if not tax_dest_id: found = True break - if not found: - if fp_tax_template.tax_dest_id: - notes += _("Tax mapping not found on the fiscal position instance: %s -> %s.\n") % (fp_tax_template.tax_src_id.name, fp_tax_template.tax_dest_id.name) else: - notes += _("Tax mapping not found on the fiscal position instance: %s -> None.\n") % fp_tax_template.tax_src_id.name - modified = True - elif fp_template.tax_ids and not fp.tax_ids: - notes += _("The template has taxes the fiscal position instance does not.\n") - modified = True - - # - # Check fiscal position accounts for changes. - # - if fp_template.account_ids and fp.account_ids: - for fp_account_template in fp_template.account_ids: - found = False - for fp_account in fp.account_ids: - if fp_account.account_src_id.name == fp_account_template.account_src_id.name: - if fp_account.account_dest_id.name == fp_account_template.account_dest_id.name: + if fp_tax.tax_dest_id.id == tax_dest_id: found = True break - if not found: - notes += _("Account mapping not found on the fiscal position instance: %s -> %s.\n") % (fp_account_template.account_src_id.name, fp_account_template.account_dest_id.name) - modified = True - elif fp_template.account_ids and not fp.account_ids: - notes += _("The template has accounts the fiscal position instance does not.\n") - modified = True - - if modified: - # - # Fiscal position template to update. - # - updated_fps += 1 - wiz_fiscalpositions.create(cr, uid, { - 'fiscal_position_id': fp_template.id, - 'update_chart_wizard_id': wizard.id, - 'type': 'updated', - 'update_fiscal_position_id': fp_id, - 'notes': notes, - }, context) - - return {'new': new_fps, 'updated': updated_fps, 'mapping': fp_template_mapping} + if not found: + notes += _("Tax mapping not found on the fiscal position instance: %s -> %s.\n") % ( + fp_tax_templ.tax_src_id.name, + fp_tax_templ.tax_dest_id and fp_tax_templ.tax_dest_id.name or _('None')) + modified = True + elif fp_templ.tax_ids and not fp.tax_ids: + notes += _("The template has taxes the fiscal position instance does not.\n") + modified = True + # Check fiscal position accounts for changes + if fp_templ.account_ids and fp.account_ids: + for fp_acc_templ in fp_templ.account_ids: + found = False + acc_src_id = self._map_account_template( + cr, uid, wizard, acc_templ_mapping, + fp_acc_templ.account_src_id, context=context) + acc_dest_id = self._map_account_template( + cr, uid, wizard, acc_templ_mapping, + fp_acc_templ.account_dest_id, context=context) + for fp_acc in fp.account_ids: + if (fp_acc.account_src_id.id == acc_src_id and + fp_acc.account_dest_id.id == acc_dest_id): + found = True + break + if not found: + notes += _("Account mapping not found on the fiscal " + "position instance: %s -> %s.\n") % \ + (fp_acc_templ.account_src_id.name, + fp_acc_templ.account_dest_id.name) + modified = True + elif fp_templ.account_ids and not fp.account_ids: + notes += _("The template has accounts the fiscal position " + "instance does not.\n") + modified = True + if modified: + # Fiscal position template to update + updated_fps += 1 + wiz_fp.create(cr, uid, { + 'fiscal_position_id': fp_templ.id, + 'update_chart_wizard_id': wizard.id, + 'type': 'updated', + 'update_fiscal_position_id': fp_id, + 'notes': notes, + }, context=context) + return {'new': new_fps, 'updated': updated_fps, 'mapping': fp_templ_mapping} """ Searchs for records to update/create and shows them @@ -667,21 +721,34 @@ class wizard_update_charts_accounts(orm.TransientModel): if context is None: context = {} wizard = self.browse(cr, uid, ids[0], context=context) - if wizard.lang: context['lang'] = wizard.lang elif context.get('lang'): del context['lang'] - - # + # Defaults when calculations are not done + tax_codes_res = {} + taxes_res = {} + accounts_res = {} + fps_res = {} + # Get all chart templates involved + wiz_obj = self.pool['wizard.multi.charts.accounts'] + chart_template_ids = wiz_obj._get_chart_parent_ids( + cr, uid, wizard.chart_template_id, context=context) # Search for, and load, the records to create/update. - # - tax_codes_res = self._find_tax_codes(cr, uid, wizard, context=context) - taxes_res = self._find_taxes(cr, uid, wizard, context=context) - accounts_res = self._find_accounts(cr, uid, wizard, context=context) - fps_res = self._find_fiscal_positions(cr, uid, wizard, context=context) - - # + if wizard.update_tax_code: + tax_codes_res = self._find_tax_codes(cr, uid, wizard, + chart_template_ids, + context=context) + if wizard.update_tax: + taxes_res = self._find_taxes(cr, uid, wizard, chart_template_ids, + context=context) + if wizard.update_account: + accounts_res = self._find_accounts(cr, uid, wizard, + context=context) + if wizard.update_fiscal_position: + fps_res = self._find_fiscal_positions(cr, uid, wizard, + chart_template_ids, + context=context) # Write the results, and go to the next step. # self.write(cr, uid, [wizard.id], { @@ -706,15 +773,14 @@ class wizard_update_charts_accounts(orm.TransientModel): new_tax_codes = 0 updated_tax_codes = 0 tax_code_template_mapping = {} - for wiz_tax_code in wizard.tax_code_ids: 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 - # Ensure the parent tax code template is on the map. - self._map_tax_code_template(cr, uid, wizard, tax_code_template_mapping, tax_code_template.parent_id, context) - - # + self._map_tax_code_template(cr, uid, wizard, + tax_code_template_mapping, + tax_code_template.parent_id, + context=context) # Values # vals = { @@ -725,10 +791,8 @@ class wizard_update_charts_accounts(orm.TransientModel): 'company_id': wizard.company_id.id, 'sign': tax_code_template.sign, } - tax_code_id = None modified = False - if wiz_tax_code.type == 'new': # # Create the tax code @@ -749,17 +813,14 @@ class wizard_update_charts_accounts(orm.TransientModel): else: tax_code_id = wiz_tax_code.update_tax_code_id and wiz_tax_code.update_tax_code_id.id modified = False - # Store the tax codes on the map tax_code_template_mapping[tax_code_template.id] = tax_code_id - if modified: # # Detect errors # if tax_code_template.parent_id and not tax_code_template_mapping.get(tax_code_template.parent_id.id): log.add(_("Tax code %s: The parent tax code %s can not be set.\n") % (tax_code_name, tax_code_template.parent_id.name), True) - return { 'new': new_tax_codes, 'updated': updated_tax_codes, @@ -770,21 +831,19 @@ class wizard_update_charts_accounts(orm.TransientModel): Search for, and load, tax templates to create/update. """ def _update_taxes(self, cr, uid, wizard, log, tax_code_template_mapping, context=None): - taxes = self.pool.get('account.tax') - + """ + Search for, and load, tax templates to create/update. + """ + taxes = self.pool['account.tax'] new_taxes = 0 updated_taxes = 0 tax_template_mapping = {} taxes_pending_for_accounts = {} - for wiz_tax in wizard.tax_ids: tax_template = wiz_tax.tax_id - # Ensure the parent tax template is on the map. self._map_tax_template(cr, uid, wizard, tax_template_mapping, tax_template.parent_id, context) - - # # Ensure the referenced tax codes are on the map. # tax_code_templates_to_find = [ @@ -794,9 +853,9 @@ class wizard_update_charts_accounts(orm.TransientModel): tax_template.ref_tax_code_id ] for tax_code_template in [tmpl for tmpl in tax_code_templates_to_find if tmpl]: - self._map_tax_code_template(cr, uid, wizard, tax_code_template_mapping, tax_code_template) - - # + self._map_tax_code_template(cr, uid, wizard, + tax_code_template_mapping, + tax_code_template, context=context) # Values # vals_tax = { @@ -825,10 +884,8 @@ class wizard_update_charts_accounts(orm.TransientModel): 'company_id': wizard.company_id.id, 'type_tax_use': tax_template.type_tax_use } - tax_id = None modified = False - if wiz_tax.type == 'new': # # Create a new tax. @@ -848,10 +905,8 @@ class wizard_update_charts_accounts(orm.TransientModel): modified = True else: tax_id = wiz_tax.update_tax_id and wiz_tax.update_tax_id.id - # Update the tax template map tax_template_mapping[tax_template.id] = tax_id - if modified: # # Add to the dict of taxes waiting for accounts. @@ -860,8 +915,6 @@ class wizard_update_charts_accounts(orm.TransientModel): 'account_collected_id': tax_template.account_collected_id and tax_template.account_collected_id.id or False, 'account_paid_id': tax_template.account_paid_id and tax_template.account_paid_id.id or False, } - - # # Detect errors # if tax_template.parent_id and not tax_template_mapping.get(tax_template.parent_id.id): @@ -874,7 +927,6 @@ class wizard_update_charts_accounts(orm.TransientModel): log.add(_("Tax %s: The tax code for the base refund %s can not be set.\n") % (tax_template.name, tax_template.ref_base_code_id.name), True) if tax_template.ref_tax_code_id and not tax_code_template_mapping.get(tax_template.ref_tax_code_id.id): log.add(_("Tax %s: The tax code for the tax refund %s can not be set.\n") % (tax_template.name, tax_template.ref_tax_code_id.name), True) - return { 'new': new_taxes, 'updated': updated_taxes, @@ -917,31 +969,23 @@ class wizard_update_charts_accounts(orm.TransientModel): def _update_accounts(self, cr, uid, wizard, log, tax_template_mapping, context=None): accounts = self.pool.get('account.account') root_account_id = wizard.chart_template_id.account_root_id.id - # Disable the parent_store computing on account_account during the batch # processing, we will force _parent_store_compute afterwards. self.pool._init = True new_accounts = 0 updated_accounts = 0 account_template_mapping = {} - for wiz_account in wizard.account_ids: account_template = wiz_account.account_id - # Ensure the parent account template is on the map. self._map_account_template(cr, uid, wizard, account_template_mapping, account_template.parent_id, context) - - # # Ensure the related tax templates are on the map. # for tax_template in account_template.tax_ids: self._map_tax_template(cr, uid, wizard, tax_template_mapping, tax_template, context) - # Get the tax ids tax_ids = [tax_template_mapping[tax_template.id] for tax_template in account_template.tax_ids if tax_template_mapping[tax_template.id]] - - # # Calculate the account code (we need to add zeros to non-view # account codes) # @@ -950,8 +994,6 @@ class wizard_update_charts_accounts(orm.TransientModel): if len(code) > 0 and len(code) <= wizard.code_digits: code = '%s%s' % ( code, '0' * (wizard.code_digits - len(code))) - - # # Values # vals = { @@ -968,10 +1010,8 @@ class wizard_update_charts_accounts(orm.TransientModel): 'tax_ids': [(6, 0, tax_ids)], 'company_id': wizard.company_id.id, } - account_id = None modified = False - if wiz_account.type == 'new': # # Create the account @@ -999,79 +1039,74 @@ class wizard_update_charts_accounts(orm.TransientModel): % (code, ex.name, ex.value), True) else: account_id = wiz_account.update_account_id and wiz_account.update_account_id.id - # Store the account on the map account_template_mapping[account_template.id] = account_id - if modified: # # Detect errors # if account_template.parent_id and not account_template_mapping.get(account_template.parent_id.id): log.add(_("Account %s: The parent account %s can not be set.\n") % (code, account_template.parent_id.code), True) - - # # Set this account as the parent of the accounts that seem to # be its children (brothers starting with the same code). # if wizard.update_children_accounts_parent: self._update_children_accounts_parent( cr, uid, wizard, log, account_id, context=context) - - # # Reenable the parent_store computing on account_account # and force the recomputation. # self.pool._init = False self.pool.get('account.account')._parent_store_compute(cr) - return { 'new': new_accounts, 'updated': updated_accounts, 'mapping': account_template_mapping } - """ - Updates the taxes (created or updated on previous steps) to set - the references to the accounts (the taxes where created/updated first, - when the referenced accounts where still not available). - """ - def _update_taxes_pending_for_accounts(self, cr, uid, wizard, log, taxes_pending_for_accounts, account_template_mapping, context=None): - taxes = self.pool.get('account.tax') - accounts_template = self.pool.get('account.account.template') - + def _update_taxes_pending_for_accounts(self, cr, uid, wizard, log, + taxes_pending_for_accounts, + acc_templ_mapping, context=None): + """ + Updates the taxes (created or updated on previous steps) to set + the references to the accounts (the taxes where created/updated first, + when the referenced accounts where still not available). + """ + taxes = self.pool['account.tax'] + accounts_template = self.pool['account.account.template'] for key, value in taxes_pending_for_accounts.items(): # # Ensure the related account templates are on the map. # if value['account_collected_id']: - account_template = accounts_template.browse( + acc_templ = accounts_template.browse( cr, uid, value['account_collected_id'], context=context) - self._map_account_template(cr, uid, wizard, account_template_mapping, account_template, context) + self._map_account_template(cr, uid, wizard, acc_templ_mapping, + acc_templ, context=context) if value['account_paid_id']: - account_template = accounts_template.browse( + acc_templ = accounts_template.browse( cr, uid, value['account_paid_id'], context=context) - self._map_account_template(cr, uid, wizard, account_template_mapping, account_template, context) - + self._map_account_template(cr, uid, wizard, acc_templ_mapping, + acc_templ, context=context) if value['account_collected_id'] or value['account_paid_id']: - if account_template_mapping.get(value['account_collected_id']) and account_template_mapping.get(value['account_paid_id']): + if acc_templ_mapping.get(value['account_collected_id']) and acc_templ_mapping.get(value['account_paid_id']): vals = { - 'account_collected_id': account_template_mapping[value['account_collected_id']], - 'account_paid_id': account_template_mapping[value['account_paid_id']], + 'account_collected_id': acc_templ_mapping[value['account_collected_id']], + 'account_paid_id': acc_templ_mapping[value['account_paid_id']], } taxes.write(cr, uid, [key], vals) else: tax = taxes.browse(cr, uid, key) - if not account_template_mapping.get(value['account_collected_id']): + if not acc_templ_mapping.get(value['account_collected_id']): log.add(_("Tax %s: The collected account can not be set.\n") % (tax.name), True) - if not account_template_mapping.get(value['account_paid_id']): + if not acc_templ_mapping.get(value['account_paid_id']): log.add(_("Tax %s: The paid account can not be set.\n") % (tax.name), True) - """ - Search for, and load, fiscal position templates to create/update. - """ - def _update_fiscal_positions(self, cr, uid, wizard, log, tax_template_mapping, account_template_mapping, context=None): + def _update_fiscal_positions(self, cr, uid, wizard, log, tax_template_mapping, acc_templ_mapping, context=None): + """ + Search for, and load, fiscal position templates to create/update. + """ fiscalpositions = self.pool.get('account.fiscal.position') fiscalpositions_taxes = self.pool.get('account.fiscal.position.tax') fiscalpositions_account = self.pool.get('account.fiscal.position.account') @@ -1148,27 +1183,29 @@ class wizard_update_charts_accounts(orm.TransientModel): for fp_account in fp_template.account_ids: # # Ensure the related account templates are on the map. - # - self._map_account_template(cr, uid, wizard, account_template_mapping, fp_account.account_src_id, context) + self._map_account_template(cr, uid, wizard, + acc_templ_mapping, + fp_account.account_src_id, + context=context) if fp_account.account_dest_id: - self._map_account_template(cr, uid, wizard, account_template_mapping, fp_account.account_dest_id, context) - - # + self._map_account_template(cr, uid, wizard, + acc_templ_mapping, + fp_account.account_dest_id, + context=context) # Create the fp account mapping # vals_account = { - 'account_src_id': account_template_mapping.get(fp_account.account_src_id.id), - 'account_dest_id': fp_account.account_dest_id and account_template_mapping.get(fp_account.account_dest_id.id), + 'account_src_id': acc_templ_mapping.get(fp_account.account_src_id.id), + 'account_dest_id': fp_account.account_dest_id and acc_templ_mapping.get(fp_account.account_dest_id.id), 'position_id': fp_id, } fiscalpositions_account.create(cr, uid, vals_account) # # Check for errors - # - if not account_template_mapping.get(fp_account.account_src_id.id): + if not acc_templ_mapping.get(fp_account.account_src_id.id): log.add(_("Fiscal position %s: The source account %s can not be set.\n") % (fp_template.name, fp_account.account_src_id.code), True) - if fp_account.account_dest_id and not account_template_mapping.get(fp_account.account_dest_id.id): + if fp_account.account_dest_id and not acc_templ_mapping.get(fp_account.account_dest_id.id): log.add(_("Fiscal position %s: The destination account %s can not be set.\n") % (fp_template.name, fp_account.account_dest_id.code), True) log.add(_("Created or updated fiscal position %s.\n") @@ -1182,34 +1219,43 @@ class wizard_update_charts_accounts(orm.TransientModel): if context is None: context = {} wizard = self.browse(cr, uid, ids[0], context=context) - if wizard.lang: context['lang'] = wizard.lang elif context.get('lang'): del context['lang'] - log = WizardLog() - - # + # Defaults when calculations are not done + tax_codes_res = {'mapping': {}} + taxes_res = {'mapping': {}, 'pending': {}} + accounts_res = {'mapping': {}} + fps_res = {} # Create or update the records. - # - tax_codes_res = self._update_tax_codes( - cr, uid, wizard, log, context=context) - taxes_res = self._update_taxes( - cr, uid, wizard, log, tax_codes_res['mapping'], context=context) - accounts_res = self._update_accounts( - cr, uid, wizard, log, taxes_res['mapping'], context=context) - self._update_taxes_pending_for_accounts(cr, uid, wizard, log, taxes_res['pending'], accounts_res['mapping'], context=context) - fps_res = self._update_fiscal_positions(cr, uid, wizard, log, taxes_res['mapping'], accounts_res['mapping'], context=context) - - # + if wizard.update_tax_code: + tax_codes_res = self._update_tax_codes(cr, uid, wizard, log, + context=context) + if wizard.update_tax: + taxes_res = self._update_taxes(cr, uid, wizard, log, + tax_codes_res['mapping'], + context=context) + if wizard.update_account: + accounts_res = self._update_accounts(cr, uid, wizard, log, + taxes_res['mapping'], + context=context) + if wizard.update_tax and wizard.update_account: + self._update_taxes_pending_for_accounts(cr, uid, wizard, log, + taxes_res['pending'], + accounts_res['mapping'], + context=context) + if wizard.update_fiscal_position: + fps_res = self._update_fiscal_positions(cr, uid, wizard, log, + taxes_res['mapping'], + accounts_res['mapping'], + context=context) # Check if errors where detected and wether we should stop. # if log.has_errors() and not wizard.continue_on_errors: raise orm.except_orm(_('Error'), _( "One or more errors detected!\n\n%s") % log.get_errors_str()) - - # # Store the data and go to the next step. # self.write(cr, uid, [wizard.id], { @@ -1223,8 +1269,7 @@ class wizard_update_charts_accounts(orm.TransientModel): 'updated_accounts': accounts_res.get('updated', 0), 'updated_fps': fps_res.get('updated', 0), 'log': log(), - }, context) - + }, context=context) return _reopen(self, wizard.id, 'wizard.update.chart.accounts') wizard_update_charts_accounts() diff --git a/account_chart_update/wizard/wizard_chart_update_view.xml b/account_chart_update/wizard/wizard_chart_update_view.xml index 1705eb45..b0b93d39 100644 --- a/account_chart_update/wizard/wizard_chart_update_view.xml +++ b/account_chart_update/wizard/wizard_chart_update_view.xml @@ -4,121 +4,93 @@ - - Update Chart of Accounts from a Chart Template - - wizard.update.charts.accounts - - -
-
- -
- -

-

This wizard will update your accounts, taxes and fiscal positions according to the selected chart template

- -

-
- - - - - - - - - - - - - - - - - - + + Update Chart of Accounts from a Chart Template + + wizard.update.charts.accounts + + +
+ +
+ +

+

This wizard will update your accounts, taxes and fiscal positions according to the selected chart template

+

+
+ + + + + + + + + + + + + + + + + + - -
-

If you leave these options set, the wizard will not just create new records, but also update records with changes (i.e. different tax amount)

-

Note:Not all the fields are tested for changes, just the main ones

-
-
- - - - - - - - - - - - - - - - - - - + +
+

If you leave these options set, the wizard will not just create new records, but also update records with changes (i.e. different tax amount)

+

Note: Not all the fields are tested for changes, just the main ones

+
+
+ + + + + + + + + + + + - - - - - - - -
- - - - - - -
-
-
- - - - - - - -
- - - - - - -
-
-
- - - - - - - -
- - - - - - -
-
-
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
@@ -143,22 +115,25 @@
-
-
-
-
-
-
- -
-
+ + +
+
Update Chart of Accounts from a Chart Template