Debian Codegraph @

addons/l10n_it/migrations/0.7/post-map_carryover_to_new_report.py

raw symbols SCIP
1 from odoo import api, SUPERUSER_ID
2 from odoo.tools import sql
3
4 def migrate(cr, version):
5 env = api.Environment(cr, SUPERUSER_ID, {})
6 vat_report_id = env.ref('l10n_it.tax_report_vat').id
7 monthly_vat_report_id = env.ref('l10n_it.tax_monthly_report_vat').id
8
9 external_value_cols = [
10 col
11 for col in sql.table_columns(env.cr, 'account_report_external_value')
12 if col not in ['id', 'carryover_origin_report_line_id', 'target_report_expression_id']
13 ]
14
15 cr.execute(f"""
16 SELECT report.id AS report_id,
17 expression.id AS expression_id,
18 report_line.code,
19 external_value.carryover_origin_report_line_id,
20 {', '.join(f'external_value.{col}' for col in external_value_cols)}
21 FROM account_report AS report
22 JOIN account_report_line AS report_line ON report.id = report_line.report_id
23 JOIN account_report_expression AS expression ON report_line.id = expression.report_line_id
24 AND expression.engine = 'external'
25 LEFT JOIN account_report_external_value AS external_value ON expression.id = external_value.target_report_expression_id
26 WHERE (
27 report.id = %s
28 AND external_value.company_id IS NOT NULL
29 )
30 OR report.id = %s
31 ORDER BY expression_id;
32 """, (vat_report_id, monthly_vat_report_id))
33 report_info = cr.fetchall()
34
35 code2expression_id = {
36 report_line_code: expression_id
37 for report_id, expression_id, report_line_code, *_ in report_info
38 if report_id == monthly_vat_report_id
39 }
40
41 cr.execute("""
42 SELECT DISTINCT old_report_line.id AS old_origin,
43 new_report_line.id AS new_origin
44 FROM account_report_external_value external_value
45 JOIN account_report_line AS old_report_line ON old_report_line.id = external_value.carryover_origin_report_line_id
46 AND old_report_line.report_id = %s
47 JOIN account_report_line AS new_report_line ON new_report_line.code = old_report_line.code
48 AND new_report_line.report_id = %s;
49 """, (vat_report_id, monthly_vat_report_id))
50 carryover_origin_info = cr.fetchall()
51 old2new_origin = {old_origin: new_origin for old_origin, new_origin in carryover_origin_info}
52 data_to_insert = [
53 (code2expression_id[report_line_code], old2new_origin.get(carryover_origin_report_line_id, carryover_origin_report_line_id), *other_external_vals)
54 for report_id, _, report_line_code, carryover_origin_report_line_id, *other_external_vals in report_info
55 if report_id == vat_report_id
56 ]
57
58 insert_query = f"""
59 INSERT INTO account_report_external_value (
60 target_report_expression_id,
61 carryover_origin_report_line_id,
62 {', '.join(col for col in external_value_cols)}
63 )
64 VALUES (%s, %s, {', '.join('%s' for _ in external_value_cols)})
65 ON CONFLICT DO NOTHING
66 """
67
68 if data_to_insert:
69 cr.executemany(insert_query, data_to_insert)
70
71 # Archive the old report
72 cr.execute("""
73 UPDATE account_report
74 SET active = FALSE
75 WHERE id = %s
76 """, (vat_report_id,))