Newer
Older
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
##############################################################################
#
# Accounting Periods, for Odoo
# Copyright © 2024 XCG Consulting <https://xcg-consulting.fr/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import json
from odoo.tools.sql import SQL, column_exists, create_column, set_not_null, table_exists
def migrate(cr, version):
# Migrate table data from <15.0
if table_exists(cr, "account_fiscalyear"):
dict_migrated_fiscal_years = {}
# Migrate fiscal years (account.fiscalyear to account.fiscal_year)
cr.execute(
SQL(
"SELECT id, name, company_id, date_start, date_stop, create_date, "
"create_uid, write_date, write_uid from account_fiscalyear"
)
)
fiscalyears = {}
for (
id,
name,
company_id,
date_start,
date_stop,
create_date,
create_uid,
write_date,
write_uid,
) in cr.fetchall():
fiscalyears[id] = SQL(
"INSERT INTO account_fiscal_year (name, company_id, date_from, date_to,"
"create_date, create_uid, write_date, write_uid) VALUES "
"(%s, %s, %s, %s, %s, %s, %s, %s) RETURNING id",
name,
company_id,
date_start,
date_stop,
create_date,
create_uid,
write_date,
write_uid,
)
for id, query in fiscalyears.items():
cr.execute(query)
dict_migrated_fiscal_years[id] = cr.fetchall()[0][0]
# Migrate analytic_id in fiscal year if it exists
if column_exists(cr, "account_fiscalyear", "analytic_id"):
# create the column to avoid losing the value when dropping data
if not column_exists(cr, "account_fiscal_year", "analytic_id"):
create_column(cr, "account_fiscal_year", "analytic_id", "int4")
cr.execute(SQL("SELECT id, analytic_id FROM account_fiscalyear"))
update_data = [row for row in cr.fetchall()]
for id, analytic_id in update_data:
cr.execute(
SQL(
"UPDATE account_fiscal_year SET analytic_id = %s WHERE id = %s",
analytic_id,
dict_migrated_fiscal_years[id],
)
)
# clean up the now useless table
cr.execute(SQL("DROP TABLE %s;", SQL.identifier("account_fiscalyear")))
# search the date range type to use for periods (from this module data)
cr.execute(
SQL(
"SELECT res_id from ir_model_data WHERE name='period_type' AND "
"module='account_period'"
)
)
date_range_type_id = cr.fetchall()[0][0]
# search for installed languages
cr.execute(SQL("SELECT code from res_lang WHERE active"))
languages_code = [row[0] for row in cr.fetchall()]
cr.execute(
SQL(
"SELECT id, company_id, code, name, date_start, date_stop, create_date"
", create_uid, write_date, write_uid, fiscalyear_id FROM account_period"
" WHERE date_range_id is null;"
)
)
creation = {}
for row in cr.fetchall():
jsonb_name = json.dumps({lang: row[2] for lang in languages_code})
jsonb_type_name = json.dumps({})
creation[row[0]] = (
SQL(
"INSERT INTO date_range (company_id, type_id, date_start, date_end,"
" create_date, create_uid, write_date, write_uid, name, type_name, "
"active) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, TRUE) "
"RETURNING id",
row[1],
date_range_type_id,
row[4],
row[5],
row[6],
row[7],
row[8],
row[9],
jsonb_name,
jsonb_type_name,
),
row[10],
)
update_query = (
"UPDATE account_period SET date_range_id = %s, fiscal_year_id = %s "
"WHERE id = %s"
)
for period_id, (query, fiscalyear_id) in creation.items():
cr.execute(query)
created_date_range_id = cr.fetchall()[0][0]
cr.execute(
SQL(
update_query,
created_date_range_id,
dict_migrated_fiscal_years[fiscalyear_id],
period_id,
)
)
for column in ("date_range_id", "fiscal_year_id"):
set_not_null(cr, "account_period", column)
# clean up the period table
for column in (
"fiscalyear_id",
"date_start",
"date_stop",
"company_id",
"name",
"code",
):
cr.execute(
SQL("ALTER TABLE account_period DROP COLUMN %s", SQL.identifier(column))
)