forked from OCA/l10n-brazil
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhooks.py
86 lines (73 loc) · 3.14 KB
/
hooks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Copyright (C) 2019 - Raphaël Valyi Akretion
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
from odoo import SUPERUSER_ID, api
from odoo.tools.sql import column_exists, create_column
def pre_init_hook(cr):
"""
account.move and account.move.line inherits from
l10n_br_account.fiscal_document and l10n_br_account.fiscal_document.line
respectively.
But the problem is that you may have existing invoice and lines (like demo
data or because you were using Odoo before installing this module or because
you use your Odoo instance for other countries than Brazil) so we should
make the Odoo ORM happy for these records and we do that with dummy records
that we use to fill these new foreign keys.
"""
env = api.Environment(cr, SUPERUSER_ID, {})
# Create fiscal_document_id fields
if not column_exists(cr, "account_move", "fiscal_document_id"):
create_column(cr, "account_move", "fiscal_document_id", "INTEGER")
# Create fiscal_document_line_id fields
if not column_exists(cr, "account_move_line", "fiscal_document_line_id"):
create_column(cr, "account_move_line", "fiscal_document_line_id", "INTEGER")
companies = env["res.company"].search([])
for company in companies:
cr.execute(
"""
UPDATE
account_move
SET fiscal_document_id=%s
WHERE
company_id=%s
AND
fiscal_document_id IS NULL;""",
(company.fiscal_dummy_id.id, company.id),
)
cr.execute(
"""
UPDATE
account_move_line
SET
fiscal_document_line_id=%s
WHERE
company_id=%s
AND
fiscal_document_line_id IS NULL;""",
(company.fiscal_dummy_id.fiscal_line_ids[0].id, company.id),
)
def load_fiscal_taxes(env, l10n_br_coa_chart):
companies = env["res.company"].search(
[("chart_template_id", "=", l10n_br_coa_chart.id)]
)
for company in companies:
taxes = env["account.tax"].search([("company_id", "=", company.id)])
for tax in taxes:
if tax.get_external_id():
tax_ref = tax.get_external_id().get(tax.id)
ref_module, ref_name = tax_ref.split(".")
ref_name = ref_name.replace(str(company.id) + "_", "")
template_source_ref = ".".join(["l10n_br_coa", ref_name])
template_source = env.ref(template_source_ref)
tax_source_ref = ".".join([ref_module, ref_name])
tax_template = env.ref(tax_source_ref)
tax.fiscal_tax_ids = (
tax_template.fiscal_tax_ids
) = template_source.fiscal_tax_ids
def post_init_hook(cr, registry):
"""Relate fiscal taxes to account taxes."""
env = api.Environment(cr, SUPERUSER_ID, {})
l10n_br_coa_charts = env["account.chart.template"].search(
[("parent_id", "=", env.ref("l10n_br_coa.l10n_br_coa_template").id)]
)
for l10n_br_coa_chart in l10n_br_coa_charts:
load_fiscal_taxes(env, l10n_br_coa_chart)