diff --git a/addons/account/account.py b/addons/account/account.py index 32fc8ca335a..22c1f64518c 100644 --- a/addons/account/account.py +++ b/addons/account/account.py @@ -2376,6 +2376,55 @@ class account_account_template(osv.osv): res.append((record['id'],name )) return res + def generate_account(self, cr, uid, account_root_id, tax_template_ref, code_digits, company_id, context=None): + """ + This method for generating accounts from templates. + @param cr: A database cursor. + @param uid: ID of the user currently logged in. + @param account_root_id: Root account id getting from current template. + @param tax_template_ref: Taxes templates reference for write taxes_id in account_account. + @param code_digits: Digit getting from wizard.multi.charts.accounts.,this is use for account code. + @param company_id: company_id selected from wizard.multi.charts.accounts. + @return : return acc_template_ref for reference purpose. + + """ + if context is None: + context = {} + obj_acc = self.pool.get('account.account') + acc_template_ref = {} + #deactivate the parent_store functionnality on account_account for rapidity purpose + ctx = context.copy() + ctx.update({'defer_parent_store_computation': True}) + children_acc_template = self.search(cr, uid, [('parent_id','child_of', [account_root_id]),('nocreate','!=',True)], order='id') + for account_template in self.browse(cr, uid, children_acc_template, context=context): + tax_ids = [] + for tax in account_template.tax_ids: + tax_ids.append(tax_template_ref[tax.id]) + + code_main = account_template.code and len(account_template.code) or 0 + code_acc = account_template.code or '' + if code_main > 0 and code_main <= code_digits and account_template.type != 'view': + code_acc = str(code_acc) + (str('0'*(code_digits-code_main))) + vals={ + 'name': (account_root_id == account_template.id) and company_id.name or account_template.name, + 'currency_id': account_template.currency_id and account_template.currency_id.id or False, + 'code': code_acc, + 'type': account_template.type, + 'user_type': account_template.user_type and account_template.user_type.id or False, + 'reconcile': account_template.reconcile, + 'shortcut': account_template.shortcut, + 'note': account_template.note, + 'parent_id': account_template.parent_id and ((account_template.parent_id.id in acc_template_ref) and acc_template_ref[account_template.parent_id.id]) or False, + 'tax_ids': [(6,0,tax_ids)], + 'company_id': company_id.id, + } + new_account = obj_acc.create(cr, uid, vals, context=ctx) + acc_template_ref[account_template.id] = new_account + + #reactivate the parent_store functionnality on account_account + obj_acc._parent_store_compute(cr) + return acc_template_ref + account_account_template() class account_add_tmpl_wizard(osv.osv_memory): @@ -2832,8 +2881,6 @@ class wizard_multi_charts_accounts(osv.osv_memory): company_id = obj_multi.company_id.id chart_temp_id = obj_multi.chart_template_id.id - #new code - acc_template_ref = {} tax_code_template_ref = {} # create tax templates and real taxes from purchase_tax_rate,sale_tax_rate fields @@ -2876,46 +2923,13 @@ class wizard_multi_charts_accounts(osv.osv_memory): #recording the new tax code to do the mapping tax_code_template_ref[tax_code_template.id] = new_tax_code - #create all the tax + #Generate taxes from templates. tax_template_to_tax = {} tax_templates = [x for x in obj_multi.chart_template_id.tax_template_ids if x.installable] taxes_ids = obj_tax_temp.generate_tax(cr, uid, tax_templates, tax_code_template_ref, company_id, context=context) - #deactivate the parent_store functionnality on account_account for rapidity purpose - ctx = context and context.copy() or {} - ctx['defer_parent_store_computation'] = True - children_acc_template = obj_acc_template.search(cr, uid, [('parent_id','child_of',[obj_acc_root.id]),('nocreate','!=',True)]) - children_acc_template.sort() - for account_template in obj_acc_template.browse(cr, uid, children_acc_template, context=context): - tax_ids = [] - for tax in account_template.tax_ids: - tax_ids.append(tax_template_ref[tax.id]) - #create the account_account - - dig = obj_multi.code_digits - code_main = account_template.code and len(account_template.code) or 0 - code_acc = account_template.code or '' - if code_main>0 and code_main<=dig and account_template.type != 'view': - code_acc=str(code_acc) + (str('0'*(dig-code_main))) - vals={ - 'name': (obj_acc_root.id == account_template.id) and obj_multi.company_id.name or account_template.name, - 'currency_id': account_template.currency_id and account_template.currency_id.id or False, - 'code': code_acc, - 'type': account_template.type, - 'user_type': account_template.user_type and account_template.user_type.id or False, - 'reconcile': account_template.reconcile, - 'shortcut': account_template.shortcut, - 'note': account_template.note, - 'parent_id': account_template.parent_id and ((account_template.parent_id.id in acc_template_ref) and acc_template_ref[account_template.parent_id.id]) or False, - 'tax_ids': [(6,0,tax_ids)], - 'company_id': company_id, - } - new_account = obj_acc.create(cr, uid, vals, context=ctx) - acc_template_ref[account_template.id] = new_account - - - #reactivate the parent_store functionnality on account_account - obj_acc._parent_store_compute(cr) + #Generating Accounts from templates. + acc_template_ref = obj_acc_template.generate_account(cr, uid, obj_acc_root.id, taxes_ids['tax_template_ref'], obj_multi.code_digits, obj_multi.company_id, context=context) for key,value in taxes_ids['account_dict'].items(): if value['account_collected_id'] or value['account_paid_id']: