forked from OCA/l10n-spain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks.py
87 lines (81 loc) · 3.62 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
87
# Copyright 2020 Binovo IT Human Project SL
# Copyright 2021 Landoo Sistemas de Informacion SL
# Copyright 2021 Digital5, S.L.
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import api, SUPERUSER_ID
from odoo.addons.l10n_es_ticketbai_api.models.ticketbai_invoice import RefundCode, \
RefundType
def post_init_hook(cr, registry):
env = api.Environment(cr, SUPERUSER_ID, {})
fiscal_positions = env['account.fiscal.position'].search([])
for position in fiscal_positions:
if not position.company_id.chart_template_id:
continue
fiscal_position_template = env['account.fiscal.position.template'].search([
('chart_template_id', '=',
env.ref('l10n_es.account_chart_template_common').id),
('name', '=', position.name)
])
if 1 == len(fiscal_position_template):
vals = {}
if fiscal_position_template.tbai_vat_regime_key:
vals['tbai_vat_regime_key'] = \
fiscal_position_template.tbai_vat_regime_key.id
if fiscal_position_template.tbai_vat_regime_key2:
vals['tbai_vat_regime_key2'] = \
fiscal_position_template.tbai_vat_regime_key2.id
if fiscal_position_template.tbai_vat_regime_key3:
vals['tbai_vat_regime_key3'] = \
fiscal_position_template.tbai_vat_regime_key3.id
if 0 < len(fiscal_position_template.tbai_vat_exemption_ids):
tbai_vat_exemptions = []
for exemption in fiscal_position_template.tbai_vat_exemption_ids:
tax = position.company_id.get_taxes_from_templates(exemption.tax_id)
if 1 == len(tax):
tbai_vat_exemptions.append((0, 0, {
'tax_id': tax.id,
'tbai_vat_exemption_key':
exemption.tbai_vat_exemption_key.id
}))
if tbai_vat_exemptions:
vals['tbai_vat_exemption_ids'] = tbai_vat_exemptions
if vals:
position.write(vals)
companies = env['res.company'].search([])
for company in companies:
if company.tbai_enabled:
journals = env['account.journal'].search(
[('company_id', '=', company.id)]
)
for journal in journals:
if 'sale' == journal.type:
if env['ir.module.module'].search([
('name', '=', 'l10n_es_account_invoice_sequence'),
('state', '=', 'installed')
]):
journal.invoice_sequence_id.suffix = ''
else:
journal.sequence_id.suffix = ''
journal.refund_sequence = True
tbai_vat_regime_key_01 = env['tbai.vat.regime.key'].search(
[('code', '=', '01')], limit=1
)
cr.execute("""
UPDATE account_invoice ai
SET tbai_vat_regime_key = afp.tbai_vat_regime_key
FROM account_fiscal_position afp
WHERE afp.id = ai.fiscal_position_id
AND ai.type IN ('out_invoice', 'out_refund');
""")
cr.execute("""
UPDATE account_invoice
SET tbai_vat_regime_key = %s
WHERE fiscal_position_id IS NULL
AND type IN ('out_invoice', 'out_refund');
""", (tbai_vat_regime_key_01.id,))
cr.execute("""
UPDATE account_invoice
SET tbai_refund_key = %s,
tbai_refund_type = %s
WHERE type = 'out_refund';
""", (RefundCode.R1.value, RefundType.differences.value, ))