Improve create/update chart of accounts
We create in one call all the records of the same tree level and we update all the record in one call. Side effect, as there is no more recursion, the depth of the chart can be higher than the maximum depth of the Python interpreter stack. issue5400 review23991002
This commit is contained in:
1 parent
f21e31d5b6
commit
b3cb087a84
3 files changed
+456
-372
No files matched your search
+171
-166
@@ -115,35 +115,42 @@ class TypeTemplate(ModelSQL, ModelView):
|
||||
res['template'] = self.id
|
||||
return res
|
||||
|
||||
def create_type(self, company_id, template2type=None, parent_id=None):
|
||||
def create_type(self, company_id, template2type=None):
|
||||
'''
|
||||
Create recursively types based on template.
|
||||
template2type is a dictionary with template id as key and type id as
|
||||
value, used to convert template id into type. The dictionary is filled
|
||||
with new types.
|
||||
Return the id of the type created
|
||||
'''
|
||||
pool = Pool()
|
||||
Type = pool.get('account.account.type')
|
||||
assert self.parent is None
|
||||
|
||||
if template2type is None:
|
||||
template2type = {}
|
||||
|
||||
if self.id not in template2type:
|
||||
vals = self._get_type_value()
|
||||
vals['company'] = company_id
|
||||
vals['parent'] = parent_id
|
||||
def create(templates):
|
||||
values = []
|
||||
created = []
|
||||
for template in templates:
|
||||
if template.id not in template2type:
|
||||
vals = template._get_type_value()
|
||||
vals['company'] = company_id
|
||||
if template.parent:
|
||||
vals['parent'] = template2type[template.parent.id]
|
||||
else:
|
||||
vals['parent'] = None
|
||||
values.append(vals)
|
||||
created.append(template)
|
||||
|
||||
new_type, = Type.create([vals])
|
||||
types = Type.create(values)
|
||||
for template, type_ in zip(created, types):
|
||||
template2type[template.id] = type_.id
|
||||
|
||||
template2type[self.id] = new_type.id
|
||||
new_id = template2type[self.id]
|
||||
|
||||
new_childs = []
|
||||
for child in self.childs:
|
||||
new_childs.append(child.create_type(company_id,
|
||||
template2type=template2type, parent_id=new_id))
|
||||
return new_id
|
||||
childs = [self]
|
||||
while childs:
|
||||
create(childs)
|
||||
childs = sum((c.childs for c in childs), ())
|
||||
|
||||
|
||||
class Type(ModelSQL, ModelView):
|
||||
@@ -269,19 +276,22 @@ class Type(ModelSQL, ModelView):
|
||||
value, used to convert template id into type. The dictionary is filled
|
||||
with new types
|
||||
'''
|
||||
|
||||
if template2type is None:
|
||||
template2type = {}
|
||||
|
||||
if self.template:
|
||||
vals = self.template._get_type_value(type=self)
|
||||
if vals:
|
||||
self.write([self], vals)
|
||||
|
||||
template2type[self.template.id] = self.id
|
||||
|
||||
for child in self.childs:
|
||||
child.update_type(template2type=template2type)
|
||||
values = []
|
||||
childs = [self]
|
||||
while childs:
|
||||
for child in childs:
|
||||
if child.template:
|
||||
vals = child.template._get_type_value(type=child)
|
||||
if vals:
|
||||
values.append([child])
|
||||
values.append(vals)
|
||||
template2type[child.template.id] = child.id
|
||||
childs = sum((c.childs for c in childs), ())
|
||||
if values:
|
||||
self.write(*values)
|
||||
|
||||
|
||||
class OpenType(Wizard):
|
||||
@@ -419,7 +429,7 @@ class AccountTemplate(ModelSQL, ModelView):
|
||||
return res
|
||||
|
||||
def create_account(self, company_id, template2account=None,
|
||||
template2type=None, parent_id=None):
|
||||
template2type=None):
|
||||
'''
|
||||
Create recursively accounts based on template.
|
||||
template2account is a dictionary with template id as key and account id
|
||||
@@ -427,10 +437,10 @@ class AccountTemplate(ModelSQL, ModelView):
|
||||
filled with new accounts
|
||||
template2type is a dictionary with type template id as key and type id
|
||||
as value, used to convert type template id into type.
|
||||
Return the id of the account created
|
||||
'''
|
||||
pool = Pool()
|
||||
Account = pool.get('account.account')
|
||||
assert self.parent is None
|
||||
|
||||
if template2account is None:
|
||||
template2account = {}
|
||||
@@ -438,24 +448,32 @@ class AccountTemplate(ModelSQL, ModelView):
|
||||
if template2type is None:
|
||||
template2type = {}
|
||||
|
||||
if self.id not in template2account:
|
||||
vals = self._get_account_value()
|
||||
vals['company'] = company_id
|
||||
vals['parent'] = parent_id
|
||||
vals['type'] = (template2type.get(self.type.id) if self.type
|
||||
else None)
|
||||
def create(templates):
|
||||
values = []
|
||||
created = []
|
||||
for template in templates:
|
||||
if template.id not in template2account:
|
||||
vals = template._get_account_value()
|
||||
vals['company'] = company_id
|
||||
if template.parent:
|
||||
vals['parent'] = template2account[template.parent.id]
|
||||
else:
|
||||
vals['parent'] = None
|
||||
if template.type:
|
||||
vals['type'] = template2type.get(template.type.id)
|
||||
else:
|
||||
vals['type'] = None
|
||||
values.append(vals)
|
||||
created.append(template)
|
||||
|
||||
new_account, = Account.create([vals])
|
||||
accounts = Account.create(values)
|
||||
for template, account in zip(created, accounts):
|
||||
template2account[template.id] = account.id
|
||||
|
||||
template2account[self.id] = new_account.id
|
||||
new_id = template2account[self.id]
|
||||
|
||||
new_childs = []
|
||||
for child in self.childs:
|
||||
new_childs.append(child.create_account(company_id,
|
||||
template2account=template2account, template2type=template2type,
|
||||
parent_id=new_id))
|
||||
return new_id
|
||||
childs = [self]
|
||||
while childs:
|
||||
create(childs)
|
||||
childs = sum((c.childs for c in childs), ())
|
||||
|
||||
def update_account_taxes(self, template2account, template2tax,
|
||||
template_done=None):
|
||||
@@ -477,17 +495,25 @@ class AccountTemplate(ModelSQL, ModelView):
|
||||
if template_done is None:
|
||||
template_done = []
|
||||
|
||||
if self.id not in template_done:
|
||||
if self.taxes:
|
||||
Account.write([Account(template2account[self.id])], {
|
||||
'taxes': [
|
||||
('add', [template2tax[x.id] for x in self.taxes])],
|
||||
})
|
||||
template_done.append(self.id)
|
||||
def update(templates):
|
||||
to_write = []
|
||||
for template in templates:
|
||||
if template.id not in template_done:
|
||||
if template.taxes:
|
||||
tax_ids = [template2tax[x.id] for x in template.taxes]
|
||||
to_write.append([Account(template2account[template.id])])
|
||||
to_write.append({
|
||||
'taxes': [
|
||||
('add', tax_ids)],
|
||||
})
|
||||
template_done.append(template.id)
|
||||
if to_write:
|
||||
Account.write(to_write)
|
||||
|
||||
for child in self.childs:
|
||||
child.update_account_taxes(template2account, template2tax,
|
||||
template_done=template_done)
|
||||
childs = [self]
|
||||
while childs:
|
||||
update(childs)
|
||||
childs = sum((c.childs for c in childs), ())
|
||||
|
||||
|
||||
class Account(ModelSQL, ModelView):
|
||||
@@ -846,28 +872,33 @@ class Account(ModelSQL, ModelView):
|
||||
template2type is a dictionary with type template id as key and type id
|
||||
as value, used to convert type template id into type.
|
||||
'''
|
||||
|
||||
if template2account is None:
|
||||
template2account = {}
|
||||
|
||||
if template2type is None:
|
||||
template2type = {}
|
||||
|
||||
if self.template:
|
||||
vals = self.template._get_account_value(account=self)
|
||||
current_type = self.type.id if self.type else None
|
||||
template_type = (template2type.get(self.template.type.id)
|
||||
if self.template.type else None)
|
||||
if current_type != template_type:
|
||||
vals['type'] = template_type
|
||||
if vals:
|
||||
self.write([self], vals)
|
||||
|
||||
template2account[self.template.id] = self.id
|
||||
|
||||
for child in self.childs:
|
||||
child.update_account(template2account=template2account,
|
||||
template2type=template2type)
|
||||
values = []
|
||||
childs = [self]
|
||||
while childs:
|
||||
for child in childs:
|
||||
if child.template:
|
||||
vals = child.template._get_account_value(account=child)
|
||||
current_type = child.type.id if child.type else None
|
||||
if child.template.type:
|
||||
template_type = template2type.get(
|
||||
child.template.type.id)
|
||||
else:
|
||||
template_type = None
|
||||
if current_type != template_type:
|
||||
vals['type'] = template_type
|
||||
if vals:
|
||||
values.append([child])
|
||||
values.append(vals)
|
||||
template2account[child.template.id] = child.id
|
||||
childs = sum((c.childs for c in childs), ())
|
||||
if values:
|
||||
self.write(*values)
|
||||
|
||||
def update_account_taxes(self, template2account, template2tax):
|
||||
'''
|
||||
@@ -883,23 +914,30 @@ class Account(ModelSQL, ModelView):
|
||||
if template2tax is None:
|
||||
template2tax = {}
|
||||
|
||||
if self.template:
|
||||
if self.template.taxes:
|
||||
tax_ids = [template2tax[x.id] for x in self.template.taxes
|
||||
if x.id in template2tax]
|
||||
old_tax_ids = [x.id for x in self.taxes]
|
||||
values = []
|
||||
childs = [self]
|
||||
while childs:
|
||||
for child in childs:
|
||||
if not child.template:
|
||||
continue
|
||||
if not child.template.taxes:
|
||||
continue
|
||||
tax_ids = [template2tax[x.id] for x in child.template.taxes
|
||||
if x.id in template2tax]
|
||||
old_tax_ids = [x.id for x in child.taxes]
|
||||
for tax_id in tax_ids:
|
||||
if tax_id not in old_tax_ids:
|
||||
self.write([self], {
|
||||
'taxes': [
|
||||
values.append([child])
|
||||
values.append({
|
||||
'taxes': [
|
||||
('add', template2tax[x.id])
|
||||
for x in self.template.taxes
|
||||
if x.id in template2tax],
|
||||
})
|
||||
break
|
||||
|
||||
for child in self.childs:
|
||||
child.update_account_taxes(template2account, template2tax)
|
||||
childs = sum((c.childs for c in childs), ())
|
||||
if values:
|
||||
self.write(*values)
|
||||
|
||||
|
||||
class AccountDeferral(ModelSQL, ModelView):
|
||||
@@ -1685,38 +1723,34 @@ class CreateChart(Wizard):
|
||||
with Transaction().set_context(language=Config.get_language(),
|
||||
company=self.account.company.id):
|
||||
account_template = self.account.account_template
|
||||
company = self.account.company
|
||||
|
||||
# Create account types
|
||||
template2type = {}
|
||||
account_template.type.create_type(self.account.company.id,
|
||||
account_template.type.create_type(
|
||||
company.id,
|
||||
template2type=template2type)
|
||||
|
||||
# Create accounts
|
||||
template2account = {}
|
||||
account_template.create_account(self.account.company.id,
|
||||
template2account=template2account, template2type=template2type)
|
||||
account_template.create_account(
|
||||
company.id,
|
||||
template2account=template2account,
|
||||
template2type=template2type)
|
||||
|
||||
# Create tax codes
|
||||
template2tax_code = {}
|
||||
tax_code_templates = TaxCodeTemplate.search([
|
||||
('account', '=', account_template.id),
|
||||
('parent', '=', None),
|
||||
])
|
||||
for tax_code_template in tax_code_templates:
|
||||
tax_code_template.create_tax_code(self.account.company.id,
|
||||
template2tax_code=template2tax_code)
|
||||
TaxCodeTemplate.create_tax_code(
|
||||
account_template.id, company.id,
|
||||
template2tax_code=template2tax_code)
|
||||
|
||||
# Create taxes
|
||||
template2tax = {}
|
||||
tax_templates = TaxTemplate.search([
|
||||
('account', '=', account_template.id),
|
||||
('parent', '=', None),
|
||||
])
|
||||
for tax_template in tax_templates:
|
||||
tax_template.create_tax(self.account.company.id,
|
||||
template2tax_code=template2tax_code,
|
||||
template2account=template2account,
|
||||
template2tax=template2tax)
|
||||
TaxTemplate.create_tax(
|
||||
account_template.id, company.id,
|
||||
template2tax_code=template2tax_code,
|
||||
template2account=template2account,
|
||||
template2tax=template2tax)
|
||||
|
||||
# Update taxes on accounts
|
||||
account_template.update_account_taxes(template2account,
|
||||
@@ -1724,21 +1758,15 @@ class CreateChart(Wizard):
|
||||
|
||||
# Create tax rules
|
||||
template2rule = {}
|
||||
tax_rule_templates = TaxRuleTemplate.search([
|
||||
('account', '=', account_template.id),
|
||||
])
|
||||
for tax_rule_template in tax_rule_templates:
|
||||
tax_rule_template.create_rule(self.account.company.id,
|
||||
template2rule=template2rule)
|
||||
TaxRuleTemplate.create_rule(
|
||||
account_template.id, company.id,
|
||||
template2rule=template2rule)
|
||||
|
||||
# Create tax rule lines
|
||||
template2rule_line = {}
|
||||
tax_rule_line_templates = TaxRuleLineTemplate.search([
|
||||
('rule.account', '=', account_template.id),
|
||||
])
|
||||
for tax_rule_line_template in tax_rule_line_templates:
|
||||
tax_rule_line_template.create_rule_line(template2tax,
|
||||
template2rule, template2rule_line=template2rule_line)
|
||||
TaxRuleLineTemplate.create_rule_line(
|
||||
account_template.id, template2tax, template2rule,
|
||||
template2rule_line=template2rule_line)
|
||||
return 'properties'
|
||||
|
||||
def default_properties(self, fields):
|
||||
@@ -1829,13 +1857,15 @@ class UpdateChart(Wizard):
|
||||
pool.get('account.tax.rule.line.template')
|
||||
|
||||
account = self.start.account
|
||||
company = account.company
|
||||
|
||||
# Update account types
|
||||
template2type = {}
|
||||
account.type.update_type(template2type=template2type)
|
||||
# Create missing account types
|
||||
if account.type.template:
|
||||
account.type.template.create_type(account.company.id,
|
||||
account.type.template.create_type(
|
||||
company.id,
|
||||
template2type=template2type)
|
||||
|
||||
# Update accounts
|
||||
@@ -1844,82 +1874,57 @@ class UpdateChart(Wizard):
|
||||
template2type=template2type)
|
||||
# Create missing accounts
|
||||
if account.template:
|
||||
account.template.create_account(account.company.id,
|
||||
template2account=template2account, template2type=template2type)
|
||||
account.template.create_account(
|
||||
company.id,
|
||||
template2account=template2account,
|
||||
template2type=template2type)
|
||||
|
||||
# Update tax codes
|
||||
template2tax_code = {}
|
||||
tax_codes = TaxCode.search([
|
||||
('company', '=', account.company.id),
|
||||
('parent', '=', None),
|
||||
])
|
||||
for tax_code in tax_codes:
|
||||
tax_code.update_tax_code(template2tax_code=template2tax_code)
|
||||
TaxCode.update_tax_code(
|
||||
company.id,
|
||||
template2tax_code=template2tax_code)
|
||||
# Create missing tax codes
|
||||
if account.template:
|
||||
tax_code_templates = TaxCodeTemplate.search([
|
||||
('account', '=', account.template.id),
|
||||
('parent', '=', None),
|
||||
])
|
||||
for tax_code_template in tax_code_templates:
|
||||
tax_code_template.create_tax_code(account.company.id,
|
||||
template2tax_code=template2tax_code)
|
||||
TaxCodeTemplate.create_tax_code(
|
||||
account.template.id, company.id,
|
||||
template2tax_code=template2tax_code)
|
||||
|
||||
# Update taxes
|
||||
template2tax = {}
|
||||
taxes = Tax.search([
|
||||
('company', '=', account.company.id),
|
||||
('parent', '=', None),
|
||||
])
|
||||
for tax in taxes:
|
||||
tax.update_tax(template2tax_code=template2tax_code,
|
||||
template2account=template2account,
|
||||
template2tax=template2tax)
|
||||
Tax.update_tax(
|
||||
company.id,
|
||||
template2tax_code=template2tax_code,
|
||||
template2account=template2account,
|
||||
template2tax=template2tax)
|
||||
# Create missing taxes
|
||||
if account.template:
|
||||
tax_templates = TaxTemplate.search([
|
||||
('account', '=', account.template.id),
|
||||
('parent', '=', None),
|
||||
])
|
||||
for tax_template in tax_templates:
|
||||
tax_template.create_tax(account.company.id,
|
||||
template2tax_code=template2tax_code,
|
||||
template2account=template2account,
|
||||
template2tax=template2tax)
|
||||
TaxTemplate.create_tax(
|
||||
account.template.id, account.company.id,
|
||||
template2tax_code=template2tax_code,
|
||||
template2account=template2account,
|
||||
template2tax=template2tax)
|
||||
|
||||
# Update taxes on accounts
|
||||
account.update_account_taxes(template2account, template2tax)
|
||||
|
||||
# Update tax rules
|
||||
template2rule = {}
|
||||
tax_rules = TaxRule.search([
|
||||
('company', '=', account.company.id),
|
||||
])
|
||||
for tax_rule in tax_rules:
|
||||
tax_rule.update_rule(template2rule=template2rule)
|
||||
TaxRule.update_rule(company.id, template2rule=template2rule)
|
||||
# Create missing tax rules
|
||||
if account.template:
|
||||
tax_rule_templates = TaxRuleTemplate.search([
|
||||
('account', '=', account.template.id),
|
||||
])
|
||||
for tax_rule_template in tax_rule_templates:
|
||||
tax_rule_template.create_rule(account.company.id,
|
||||
template2rule=template2rule)
|
||||
TaxRuleTemplate.create_rule(
|
||||
account.template.id, account.company.id,
|
||||
template2rule=template2rule)
|
||||
|
||||
# Update tax rule lines
|
||||
template2rule_line = {}
|
||||
tax_rule_lines = TaxRuleLine.search([
|
||||
('rule.company', '=', account.company.id),
|
||||
])
|
||||
for tax_rule_line in tax_rule_lines:
|
||||
tax_rule_line.update_rule_line(template2tax, template2rule,
|
||||
template2rule_line=template2rule_line)
|
||||
TaxRuleLine.update_rule_line(
|
||||
company.id, template2tax, template2rule,
|
||||
template2rule_line=template2rule_line)
|
||||
# Create missing tax rule lines
|
||||
if account.template:
|
||||
tax_rule_line_templates = TaxRuleLineTemplate.search([
|
||||
('rule.account', '=', account.template.id),
|
||||
])
|
||||
for tax_rule_line_template in tax_rule_line_templates:
|
||||
tax_rule_line_template.create_rule_line(template2tax,
|
||||
template2rule, template2rule_line=template2rule_line)
|
||||
TaxRuleLineTemplate.create_rule_line(
|
||||
account.template.id, template2tax, template2rule,
|
||||
template2rule_line=template2rule_line)
|
||||
return 'succeed'
|
||||
@@ -88,14 +88,13 @@ class TaxCodeTemplate(ModelSQL, ModelView):
|
||||
res['template'] = self.id
|
||||
return res
|
||||
|
||||
def create_tax_code(self, company_id, template2tax_code=None,
|
||||
parent_id=None):
|
||||
@classmethod
|
||||
def create_tax_code(cls, account_id, company_id, template2tax_code=None):
|
||||
'''
|
||||
Create recursively tax codes based on template.
|
||||
template2tax_code is a dictionary with tax code template id as key and
|
||||
tax code id as value, used to convert template id into tax code. The
|
||||
dictionary is filled with new tax codes.
|
||||
Return the id of the tax code created
|
||||
'''
|
||||
pool = Pool()
|
||||
TaxCode = pool.get('account.tax.code')
|
||||
@@ -103,24 +102,31 @@ class TaxCodeTemplate(ModelSQL, ModelView):
|
||||
if template2tax_code is None:
|
||||
template2tax_code = {}
|
||||
|
||||
if self.id not in template2tax_code:
|
||||
vals = self._get_tax_code_value()
|
||||
vals['company'] = company_id
|
||||
vals['parent'] = parent_id
|
||||
def create(templates):
|
||||
values = []
|
||||
created = []
|
||||
for template in templates:
|
||||
if template.id not in template2tax_code:
|
||||
vals = template._get_tax_code_value()
|
||||
vals['company'] = company_id
|
||||
if template.parent:
|
||||
vals['parent'] = template2tax_code[template.parent.id]
|
||||
else:
|
||||
vals['parent'] = None
|
||||
values.append(vals)
|
||||
created.append(template)
|
||||
|
||||
new_tax_code, = TaxCode.create([vals])
|
||||
tax_codes = TaxCode.create(values)
|
||||
for template, tax_code in zip(created, tax_codes):
|
||||
template2tax_code[template.id] = tax_code.id
|
||||
|
||||
prev_data = {}
|
||||
for field_name, field in self._fields.iteritems():
|
||||
prev_data[field_name] = getattr(self, field_name)
|
||||
template2tax_code[self.id] = new_tax_code.id
|
||||
new_id = template2tax_code[self.id]
|
||||
|
||||
new_childs = []
|
||||
for child in self.childs:
|
||||
new_childs.append(child.create_tax_code(company_id,
|
||||
template2tax_code=template2tax_code, parent_id=new_id))
|
||||
return new_id
|
||||
childs = cls.search([
|
||||
('account', '=', account_id),
|
||||
('parent', '=', None),
|
||||
])
|
||||
while childs:
|
||||
create(childs)
|
||||
childs = sum((c.childs for c in childs), ())
|
||||
|
||||
|
||||
class TaxCode(ModelSQL, ModelView):
|
||||
@@ -232,25 +238,33 @@ class TaxCode(ModelSQL, ModelView):
|
||||
])
|
||||
super(TaxCode, cls).delete(codes)
|
||||
|
||||
def update_tax_code(self, template2tax_code=None):
|
||||
@classmethod
|
||||
def update_tax_code(cls, company_id, template2tax_code=None):
|
||||
'''
|
||||
Update recursively tax code based on template.
|
||||
template2tax_code is a dictionary with tax code template id as key and
|
||||
tax code id as value, used to convert template id into tax code. The
|
||||
dictionary is filled with new tax codes
|
||||
'''
|
||||
|
||||
if template2tax_code is None:
|
||||
template2tax_code = {}
|
||||
|
||||
if self.template:
|
||||
vals = self.template._get_tax_code_value(code=self)
|
||||
if vals:
|
||||
self.write([self], vals)
|
||||
template2tax_code[self.template.id] = self.id
|
||||
|
||||
for child in self.childs:
|
||||
child.update_tax_code(template2tax_code=template2tax_code)
|
||||
values = []
|
||||
childs = cls.search([
|
||||
('company', '=', company_id),
|
||||
('parent', '=', None),
|
||||
])
|
||||
while childs:
|
||||
for child in childs:
|
||||
if child.template:
|
||||
vals = child.template._get_tax_code_value(code=child)
|
||||
if vals:
|
||||
values.append([child])
|
||||
values.append(vals)
|
||||
template2tax_code[child.template.id] = child.id
|
||||
childs = sum((c.childs for c in childs), ())
|
||||
if values:
|
||||
cls.write(*values)
|
||||
|
||||
|
||||
class OpenChartTaxCodeStart(ModelView):
|
||||
@@ -454,8 +468,9 @@ class TaxTemplate(ModelSQL, ModelView):
|
||||
res['template'] = self.id
|
||||
return res
|
||||
|
||||
def create_tax(self, company_id, template2tax_code, template2account,
|
||||
template2tax=None, parent_id=None):
|
||||
@classmethod
|
||||
def create_tax(cls, account_id, company_id, template2tax_code,
|
||||
template2account, template2tax=None):
|
||||
'''
|
||||
Create recursively taxes based on template.
|
||||
|
||||
@@ -467,7 +482,6 @@ class TaxTemplate(ModelSQL, ModelView):
|
||||
template2tax is a dictionary with tax template id as key and tax id as
|
||||
value, used to convert template id into tax. The dictionary is filled
|
||||
with new taxes.
|
||||
Return id of the tax created
|
||||
'''
|
||||
pool = Pool()
|
||||
Tax = pool.get('account.tax')
|
||||
@@ -475,52 +489,63 @@ class TaxTemplate(ModelSQL, ModelView):
|
||||
if template2tax is None:
|
||||
template2tax = {}
|
||||
|
||||
if self.id not in template2tax:
|
||||
vals = self._get_tax_value()
|
||||
vals['company'] = company_id
|
||||
vals['parent'] = parent_id
|
||||
if self.invoice_account:
|
||||
vals['invoice_account'] = \
|
||||
template2account[self.invoice_account.id]
|
||||
else:
|
||||
vals['invoice_account'] = None
|
||||
if self.credit_note_account:
|
||||
vals['credit_note_account'] = \
|
||||
template2account[self.credit_note_account.id]
|
||||
else:
|
||||
vals['credit_note_account'] = None
|
||||
if self.invoice_base_code:
|
||||
vals['invoice_base_code'] = \
|
||||
template2tax_code[self.invoice_base_code.id]
|
||||
else:
|
||||
vals['invoice_base_code'] = None
|
||||
if self.invoice_tax_code:
|
||||
vals['invoice_tax_code'] = \
|
||||
template2tax_code[self.invoice_tax_code.id]
|
||||
else:
|
||||
vals['invoice_tax_code'] = None
|
||||
if self.credit_note_base_code:
|
||||
vals['credit_note_base_code'] = \
|
||||
template2tax_code[self.credit_note_base_code.id]
|
||||
else:
|
||||
vals['credit_note_base_code'] = None
|
||||
if self.credit_note_tax_code:
|
||||
vals['credit_note_tax_code'] = \
|
||||
template2tax_code[self.credit_note_tax_code.id]
|
||||
else:
|
||||
vals['credit_note_tax_code'] = None
|
||||
def create(templates):
|
||||
values = []
|
||||
created = []
|
||||
for template in templates:
|
||||
if template.id not in template2tax:
|
||||
vals = template._get_tax_value()
|
||||
vals['company'] = company_id
|
||||
if template.parent:
|
||||
vals['parent'] = template2tax[template.parent.id]
|
||||
else:
|
||||
vals['parent'] = None
|
||||
if template.invoice_account:
|
||||
vals['invoice_account'] = \
|
||||
template2account[template.invoice_account.id]
|
||||
else:
|
||||
vals['invoice_account'] = None
|
||||
if template.credit_note_account:
|
||||
vals['credit_note_account'] = \
|
||||
template2account[template.credit_note_account.id]
|
||||
else:
|
||||
vals['credit_note_account'] = None
|
||||
if template.invoice_base_code:
|
||||
vals['invoice_base_code'] = \
|
||||
template2tax_code[template.invoice_base_code.id]
|
||||
else:
|
||||
vals['invoice_base_code'] = None
|
||||
if template.invoice_tax_code:
|
||||
vals['invoice_tax_code'] = \
|
||||
template2tax_code[template.invoice_tax_code.id]
|
||||
else:
|
||||
vals['invoice_tax_code'] = None
|
||||
if template.credit_note_base_code:
|
||||
vals['credit_note_base_code'] = \
|
||||
template2tax_code[
|
||||
template.credit_note_base_code.id]
|
||||
else:
|
||||
vals['credit_note_base_code'] = None
|
||||
if template.credit_note_tax_code:
|
||||
vals['credit_note_tax_code'] = \
|
||||
template2tax_code[template.credit_note_tax_code.id]
|
||||
else:
|
||||
vals['credit_note_tax_code'] = None
|
||||
|
||||
new_tax, = Tax.create([vals])
|
||||
values.append(vals)
|
||||
created.append(template)
|
||||
|
||||
template2tax[self.id] = new_tax.id
|
||||
new_id = template2tax[self.id]
|
||||
taxes = Tax.create(values)
|
||||
for template, tax in zip(created, taxes):
|
||||
template2tax[template.id] = tax.id
|
||||
|
||||
new_childs = []
|
||||
for child in self.childs:
|
||||
new_childs.append(child.create_tax(company_id, template2tax_code,
|
||||
template2account, template2tax=template2tax,
|
||||
parent_id=new_id))
|
||||
return new_id
|
||||
childs = cls.search([
|
||||
('account', '=', account_id),
|
||||
('parent', '=', None),
|
||||
])
|
||||
while childs:
|
||||
create(childs)
|
||||
childs = sum((c.childs for c in childs), ())
|
||||
|
||||
|
||||
class Tax(ModelSQL, ModelView):
|
||||
@@ -869,7 +894,8 @@ class Tax(ModelSQL, ModelView):
|
||||
taxes = cls.sort_taxes(taxes)
|
||||
return cls._reverse_unit_compute(price_unit, taxes, date)
|
||||
|
||||
def update_tax(self, template2tax_code, template2account,
|
||||
@classmethod
|
||||
def update_tax(cls, company_id, template2tax_code, template2account,
|
||||
template2tax=None):
|
||||
'''
|
||||
Update recursively taxes based on template.
|
||||
@@ -882,81 +908,88 @@ class Tax(ModelSQL, ModelView):
|
||||
value, used to convert template id into tax. The dictionary is filled
|
||||
with new taxes.
|
||||
'''
|
||||
|
||||
if template2tax is None:
|
||||
template2tax = {}
|
||||
|
||||
if self.template:
|
||||
vals = self.template._get_tax_value(tax=self)
|
||||
invoice_account_id = (self.invoice_account.id
|
||||
if self.invoice_account else None)
|
||||
if (self.template.invoice_account and
|
||||
invoice_account_id != template2account.get(
|
||||
self.template.invoice_account.id)):
|
||||
vals['invoice_account'] = template2account.get(
|
||||
self.template.invoice_account.id)
|
||||
elif (not self.template.invoice_account
|
||||
and self.invoice_account):
|
||||
vals['invoice_account'] = None
|
||||
credit_note_account_id = (self.credit_note_account.id
|
||||
if self.credit_note_account else None)
|
||||
if (self.template.credit_note_account and
|
||||
credit_note_account_id != template2account.get(
|
||||
self.template.credit_note_account.id)):
|
||||
vals['credit_note_account'] = template2account.get(
|
||||
self.template.credit_note_account.id)
|
||||
elif (not self.template.credit_note_account
|
||||
and self.credit_note_account):
|
||||
vals['credit_note_account'] = None
|
||||
invoice_base_code_id = (self.invoice_base_code.id
|
||||
if self.invoice_base_code else None)
|
||||
if (self.template.invoice_base_code and
|
||||
invoice_base_code_id != template2tax_code.get(
|
||||
self.template.invoice_base_code.id)):
|
||||
vals['invoice_base_code'] = template2tax_code.get(
|
||||
self.template.invoice_base_code.id)
|
||||
elif (not self.template.invoice_base_code
|
||||
and self.invoice_base_code):
|
||||
vals['invoice_base_code'] = None
|
||||
invoice_tax_code_id = (self.invoice_tax_code.id
|
||||
if self.invoice_tax_code else None)
|
||||
if (self.template.invoice_tax_code
|
||||
and invoice_tax_code_id != template2tax_code.get(
|
||||
self.template.invoice_tax_code.id)):
|
||||
vals['invoice_tax_code'] = template2tax_code.get(
|
||||
self.template.invoice_tax_code.id)
|
||||
elif (not self.template.invoice_tax_code
|
||||
and self.invoice_tax_code):
|
||||
vals['invoice_tax_code'] = None
|
||||
credit_note_base_code_id = (self.credit_note_base_code.id
|
||||
if self.credit_note_base_code else None)
|
||||
if (self.template.credit_note_base_code
|
||||
and credit_note_base_code_id != template2tax_code.get(
|
||||
self.template.credit_note_base_code.id)):
|
||||
vals['credit_note_base_code'] = template2tax_code.get(
|
||||
self.template.credit_note_base_code.id)
|
||||
elif (not self.template.credit_note_base_code
|
||||
and self.credit_note_base_code):
|
||||
vals['credit_note_base_code'] = None
|
||||
credit_note_tax_code_id = (self.credit_note_tax_code.id
|
||||
if self.credit_note_tax_code else None)
|
||||
if (self.template.credit_note_tax_code
|
||||
and credit_note_tax_code_id != template2tax_code.get(
|
||||
self.template.credit_note_tax_code.id)):
|
||||
vals['credit_note_tax_code'] = template2tax_code.get(
|
||||
self.template.credit_note_tax_code.id)
|
||||
elif (not self.template.credit_note_tax_code
|
||||
and self.credit_note_tax_code):
|
||||
vals['credit_note_tax_code'] = None
|
||||
values = []
|
||||
childs = cls.search([
|
||||
('company', '=', company_id),
|
||||
('parent', '=', None),
|
||||
])
|
||||
while childs:
|
||||
for child in childs:
|
||||
if child.template:
|
||||
vals = child.template._get_tax_value(tax=child)
|
||||
invoice_account_id = (child.invoice_account.id
|
||||
if child.invoice_account else None)
|
||||
if (child.template.invoice_account and
|
||||
invoice_account_id != template2account.get(
|
||||
child.template.invoice_account.id)):
|
||||
vals['invoice_account'] = template2account.get(
|
||||
child.template.invoice_account.id)
|
||||
elif (not child.template.invoice_account
|
||||
and child.invoice_account):
|
||||
vals['invoice_account'] = None
|
||||
credit_note_account_id = (child.credit_note_account.id
|
||||
if child.credit_note_account else None)
|
||||
if (child.template.credit_note_account and
|
||||
credit_note_account_id != template2account.get(
|
||||
child.template.credit_note_account.id)):
|
||||
vals['credit_note_account'] = template2account.get(
|
||||
child.template.credit_note_account.id)
|
||||
elif (not child.template.credit_note_account
|
||||
and child.credit_note_account):
|
||||
vals['credit_note_account'] = None
|
||||
invoice_base_code_id = (child.invoice_base_code.id
|
||||
if child.invoice_base_code else None)
|
||||
if (child.template.invoice_base_code and
|
||||
invoice_base_code_id != template2tax_code.get(
|
||||
child.template.invoice_base_code.id)):
|
||||
vals['invoice_base_code'] = template2tax_code.get(
|
||||
child.template.invoice_base_code.id)
|
||||
elif (not child.template.invoice_base_code
|
||||
and child.invoice_base_code):
|
||||
vals['invoice_base_code'] = None
|
||||
invoice_tax_code_id = (child.invoice_tax_code.id
|
||||
if child.invoice_tax_code else None)
|
||||
if (child.template.invoice_tax_code
|
||||
and invoice_tax_code_id != template2tax_code.get(
|
||||
child.template.invoice_tax_code.id)):
|
||||
vals['invoice_tax_code'] = template2tax_code.get(
|
||||
child.template.invoice_tax_code.id)
|
||||
elif (not child.template.invoice_tax_code
|
||||
and child.invoice_tax_code):
|
||||
vals['invoice_tax_code'] = None
|
||||
credit_note_base_code_id = (child.credit_note_base_code.id
|
||||
if child.credit_note_base_code else None)
|
||||
if (child.template.credit_note_base_code
|
||||
and (credit_note_base_code_id
|
||||
!= template2tax_code.get(
|
||||
child.template.credit_note_base_code.id))):
|
||||
vals['credit_note_base_code'] = template2tax_code.get(
|
||||
child.template.credit_note_base_code.id)
|
||||
elif (not child.template.credit_note_base_code
|
||||
and child.credit_note_base_code):
|
||||
vals['credit_note_base_code'] = None
|
||||
credit_note_tax_code_id = (child.credit_note_tax_code.id
|
||||
if child.credit_note_tax_code else None)
|
||||
if (child.template.credit_note_tax_code
|
||||
and (credit_note_tax_code_id
|
||||
!= template2tax_code.get(
|
||||
child.template.credit_note_tax_code.id))):
|
||||
vals['credit_note_tax_code'] = template2tax_code.get(
|
||||
child.template.credit_note_tax_code.id)
|
||||
elif (not child.template.credit_note_tax_code
|
||||
and child.credit_note_tax_code):
|
||||
vals['credit_note_tax_code'] = None
|
||||
|
||||
if vals:
|
||||
self.write([self], vals)
|
||||
|
||||
template2tax[self.template.id] = self.id
|
||||
|
||||
for child in self.childs:
|
||||
child.update_tax(template2tax_code, template2account,
|
||||
template2tax=template2tax)
|
||||
if vals:
|
||||
values.append([child])
|
||||
values.append(vals)
|
||||
template2tax[child.template.id] = child.id
|
||||
childs = sum((c.childs for c in childs), ())
|
||||
if values:
|
||||
cls.write(*values)
|
||||
|
||||
|
||||
class _TaxKey(dict):
|
||||
@@ -1132,13 +1165,13 @@ class TaxRuleTemplate(ModelSQL, ModelView):
|
||||
res['template'] = self.id
|
||||
return res
|
||||
|
||||
def create_rule(self, company_id, template2rule=None):
|
||||
@classmethod
|
||||
def create_rule(cls, account_id, company_id, template2rule=None):
|
||||
'''
|
||||
Create tax rule based on template.
|
||||
template2rule is a dictionary with tax rule template id as key and tax
|
||||
rule id as value, used to convert template id into tax rule. The
|
||||
dictionary is filled with new tax rules.
|
||||
Return id of the tax rule created
|
||||
'''
|
||||
pool = Pool()
|
||||
Rule = pool.get('account.tax.rule')
|
||||
@@ -1146,13 +1179,22 @@ class TaxRuleTemplate(ModelSQL, ModelView):
|
||||
if template2rule is None:
|
||||
template2rule = {}
|
||||
|
||||
if self.id not in template2rule:
|
||||
vals = self._get_tax_rule_value()
|
||||
vals['company'] = company_id
|
||||
new_rule, = Rule.create([vals])
|
||||
templates = cls.search([
|
||||
('account', '=', account_id),
|
||||
])
|
||||
|
||||
template2rule[self.id] = new_rule.id
|
||||
return template2rule[self.id]
|
||||
values = []
|
||||
created = []
|
||||
for template in templates:
|
||||
if template.id not in template2rule:
|
||||
vals = template._get_tax_rule_value()
|
||||
vals['company'] = company_id
|
||||
values.append(vals)
|
||||
created.append(template)
|
||||
|
||||
rules = Rule.create(values)
|
||||
for template, rule in zip(created, rules):
|
||||
template2rule[template.id] = rule.id
|
||||
|
||||
|
||||
class TaxRule(ModelSQL, ModelView):
|
||||
@@ -1192,23 +1234,30 @@ class TaxRule(ModelSQL, ModelView):
|
||||
return line.get_taxes()
|
||||
return tax and [tax.id] or None
|
||||
|
||||
def update_rule(self, template2rule=None):
|
||||
@classmethod
|
||||
def update_rule(cls, company_id, template2rule=None):
|
||||
'''
|
||||
Update tax rule based on template.
|
||||
template2rule is a dictionary with tax rule template id as key and tax
|
||||
rule id as value, used to convert template id into tax rule. The
|
||||
dictionary is filled with new tax rules.
|
||||
'''
|
||||
|
||||
if template2rule is None:
|
||||
template2rule = {}
|
||||
|
||||
if self.template:
|
||||
vals = self.template._get_tax_rule_value(rule=self)
|
||||
if vals:
|
||||
self.write([self], vals)
|
||||
|
||||
template2rule[self.template.id] = self.id
|
||||
values = []
|
||||
rules = cls.search([
|
||||
('company', '=', company_id),
|
||||
])
|
||||
for rule in rules:
|
||||
if rule.template:
|
||||
vals = rule.template._get_tax_rule_value(rule=rule)
|
||||
if vals:
|
||||
values.append([rule])
|
||||
values.append(vals)
|
||||
template2rule[rule.template.id] = rule.id
|
||||
if values:
|
||||
cls.write(*values)
|
||||
|
||||
|
||||
class TaxRuleLineTemplate(ModelSQL, ModelView):
|
||||
@@ -1288,7 +1337,8 @@ class TaxRuleLineTemplate(ModelSQL, ModelView):
|
||||
res['template'] = self.id
|
||||
return res
|
||||
|
||||
def create_rule_line(self, template2tax, template2rule,
|
||||
@classmethod
|
||||
def create_rule_line(cls, account_id, template2tax, template2rule,
|
||||
template2rule_line=None):
|
||||
'''
|
||||
Create tax rule line based on template.
|
||||
@@ -1299,27 +1349,36 @@ class TaxRuleLineTemplate(ModelSQL, ModelView):
|
||||
template2rule_line is a dictionary with tax rule line template id as
|
||||
key and tax rule line id as value, used to convert template id into tax
|
||||
rule line. The dictionary is filled with new tax rule lines.
|
||||
Return id of the tax rule line created
|
||||
'''
|
||||
RuleLine = Pool().get('account.tax.rule.line')
|
||||
|
||||
if template2rule_line is None:
|
||||
template2rule_line = {}
|
||||
|
||||
if self.id not in template2rule_line:
|
||||
vals = self._get_tax_rule_line_value()
|
||||
vals['rule'] = template2rule[self.rule.id]
|
||||
if self.origin_tax:
|
||||
vals['origin_tax'] = template2tax[self.origin_tax.id]
|
||||
else:
|
||||
vals['origin_tax'] = None
|
||||
if self.tax:
|
||||
vals['tax'] = template2tax[self.tax.id]
|
||||
else:
|
||||
vals['tax'] = None
|
||||
new_rule_line, = RuleLine.create([vals])
|
||||
template2rule_line[self.id] = new_rule_line.id
|
||||
return template2rule_line[self.id]
|
||||
templates = cls.search([
|
||||
('rule.account', '=', account_id),
|
||||
])
|
||||
|
||||
values = []
|
||||
created = []
|
||||
for template in templates:
|
||||
if template.id not in template2rule_line:
|
||||
vals = template._get_tax_rule_line_value()
|
||||
vals['rule'] = template2rule[template.rule.id]
|
||||
if template.origin_tax:
|
||||
vals['origin_tax'] = template2tax[template.origin_tax.id]
|
||||
else:
|
||||
vals['origin_tax'] = None
|
||||
if template.tax:
|
||||
vals['tax'] = template2tax[template.tax.id]
|
||||
else:
|
||||
vals['tax'] = None
|
||||
values.append(vals)
|
||||
created.append(template)
|
||||
|
||||
rule_lines = RuleLine.create(values)
|
||||
for template, rule_line in zip(created, rule_lines):
|
||||
template2rule_line[template.id] = rule_line.id
|
||||
|
||||
|
||||
class TaxRuleLine(ModelSQL, ModelView, MatchMixin):
|
||||
@@ -1402,7 +1461,8 @@ class TaxRuleLine(ModelSQL, ModelView, MatchMixin):
|
||||
return [self.tax.id]
|
||||
return None
|
||||
|
||||
def update_rule_line(self, template2tax, template2rule,
|
||||
@classmethod
|
||||
def update_rule_line(cls, company_id, template2tax, template2rule,
|
||||
template2rule_line=None):
|
||||
'''
|
||||
Update tax rule line based on template.
|
||||
@@ -1417,31 +1477,40 @@ class TaxRuleLine(ModelSQL, ModelView, MatchMixin):
|
||||
if template2rule_line is None:
|
||||
template2rule_line = {}
|
||||
|
||||
if self.template:
|
||||
vals = self.template._get_tax_rule_line_value(rule_line=self)
|
||||
if self.rule.id != template2rule[self.template.rule.id]:
|
||||
vals['rule'] = template2rule[self.template.rule.id]
|
||||
if self.origin_tax:
|
||||
if self.template.origin_tax:
|
||||
if (self.origin_tax.id !=
|
||||
template2tax[self.template.origin_tax.id]):
|
||||
vals['origin_tax'] = template2tax[
|
||||
self.template.origin_tax.id]
|
||||
else:
|
||||
vals['origin_tax'] = None
|
||||
elif self.template.origin_tax:
|
||||
vals['origin_tax'] = template2tax[self.template.origin_tax.id]
|
||||
if self.tax:
|
||||
if self.template.tax:
|
||||
if self.tax.id != template2tax[self.template.tax.id]:
|
||||
vals['tax'] = template2tax[self.template.tax.id]
|
||||
else:
|
||||
vals['tax'] = None
|
||||
elif self.template.tax:
|
||||
vals['tax'] = template2tax[self.template.tax.id]
|
||||
if vals:
|
||||
self.write([self], vals)
|
||||
template2rule_line[self.template.id] = self.id
|
||||
values = []
|
||||
lines = cls.search([
|
||||
('rule.company', '=', company_id),
|
||||
])
|
||||
for line in lines:
|
||||
if line.template:
|
||||
vals = line.template._get_tax_rule_line_value(rule_line=line)
|
||||
if line.rule.id != template2rule[line.template.rule.id]:
|
||||
vals['rule'] = template2rule[line.template.rule.id]
|
||||
if line.origin_tax:
|
||||
if line.template.origin_tax:
|
||||
if (line.origin_tax.id !=
|
||||
template2tax[line.template.origin_tax.id]):
|
||||
vals['origin_tax'] = template2tax[
|
||||
line.template.origin_tax.id]
|
||||
else:
|
||||
vals['origin_tax'] = None
|
||||
elif line.template.origin_tax:
|
||||
vals['origin_tax'] = template2tax[
|
||||
line.template.origin_tax.id]
|
||||
if line.tax:
|
||||
if line.template.tax:
|
||||
if line.tax.id != template2tax[line.template.tax.id]:
|
||||
vals['tax'] = template2tax[line.template.tax.id]
|
||||
else:
|
||||
vals['tax'] = None
|
||||
elif line.template.tax:
|
||||
vals['tax'] = template2tax[line.template.tax.id]
|
||||
if vals:
|
||||
values.append([line])
|
||||
values.append(vals)
|
||||
template2rule_line[line.template.id] = line.id
|
||||
if values:
|
||||
cls.write(*values)
|
||||
|
||||
|
||||
class OpenTaxCode(Wizard):
|
||||
|
||||
+11
-1
@@ -100,10 +100,20 @@ class AccountTestCase(ModuleTestCase):
|
||||
|
||||
@with_transaction()
|
||||
def test_account_chart(self):
|
||||
'Test creation of minimal chart of accounts'
|
||||
'Test creation and update of minimal chart of accounts'
|
||||
pool = Pool()
|
||||
Account = pool.get('account.account')
|
||||
UpdateChart = pool.get('account.update_chart', type='wizard')
|
||||
|
||||
company = create_company()
|
||||
with set_company(company):
|
||||
create_chart(company, tax=True)
|
||||
root, = Account.search([('parent', '=', None)])
|
||||
|
||||
session_id, _, _ = UpdateChart.create()
|
||||
update_chart = UpdateChart(session_id)
|
||||
update_chart.start.account = root
|
||||
update_chart.transition_update()
|
||||
|
||||
@with_transaction()
|
||||
def test_fiscalyear(self):
|
||||
|
||||
Reference in new issue
Block a user