/
openupgrade_130.py
238 lines (225 loc) · 7.29 KB
/
openupgrade_130.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
"""Tools specific for migrating from Odoo 12.0 to 13.0."""
import logging
from . import openupgrade
_logger = logging.getLogger("OpenUpgrade")
def convert_old_style_tax_tag_to_new(
env, report_line, old_tag_id, new_debit_tag_id, new_credit_tag_id
):
_logger.debug(
"Converting %s to +/- %s..." % (report_line.name, report_line.tag_name)
)
# First, update the tax repartition lines' tags
affected = openupgrade.logged_query(
env.cr,
"""
UPDATE account_account_tag_account_tax_repartition_line_rel r
SET account_account_tag_id = %s
FROM account_tax_repartition_line atrl
WHERE
r.account_tax_repartition_line_id = atrl.id AND
atrl.invoice_tax_id IS NOT NULL AND
atrl.refund_tax_id IS NULL AND
r.account_account_tag_id = %s
"""
% (new_debit_tag_id, old_tag_id),
)
if affected > 0:
_logger.info(
'Converted tag "%s" to "+%s" on repartition line.'
% (report_line.name, report_line.tag_name)
)
affected = openupgrade.logged_query(
env.cr,
"""
UPDATE account_tax_repartition_financial_tags r
SET account_account_tag_id = %s
FROM account_tax_repartition_line atrl
WHERE
r.account_tax_repartition_line_template_id = atrl.id AND
atrl.invoice_tax_id IS NOT NULL AND
atrl.refund_tax_id IS NULL AND
r.account_account_tag_id = %s
"""
% (new_debit_tag_id, old_tag_id),
)
if affected > 0:
_logger.info(
'Converted tag "%s" to "+%s" on repartition line '
"template." % (report_line.name, report_line.tag_name)
)
affected = openupgrade.logged_query(
env.cr,
"""
UPDATE account_account_tag_account_tax_repartition_line_rel r
SET account_account_tag_id = %s
FROM account_tax_repartition_line atrl
WHERE
r.account_tax_repartition_line_id = atrl.id AND
atrl.invoice_tax_id IS NULL AND
atrl.refund_tax_id IS NOT NULL AND
r.account_account_tag_id = %s
"""
% (new_credit_tag_id, old_tag_id),
)
if affected > 0:
_logger.info(
'Converted tag "%s" to "-%s" on repartition line.'
% (report_line.name, report_line.tag_name)
)
affected = openupgrade.logged_query(
env.cr,
"""
UPDATE account_tax_repartition_financial_tags r
SET account_account_tag_id = %s
FROM account_tax_repartition_line atrl
WHERE
r.account_tax_repartition_line_template_id = atrl.id AND
atrl.invoice_tax_id IS NULL AND
atrl.refund_tax_id IS NOT NULL AND
r.account_account_tag_id = %s
"""
% (new_credit_tag_id, old_tag_id),
)
if affected > 0:
_logger.info(
'Converted tag "%s" to "-%s" on repartition line '
"template." % (report_line.name, report_line.tag_name)
)
# Then, update the move line tags
openupgrade.logged_query(
env.cr,
"""
UPDATE account_account_tag_account_move_line_rel r
SET account_account_tag_id = %s
FROM account_move_line aml
WHERE
r.account_move_line_id = aml.id AND
aml.debit >= 0 AND
r.account_account_tag_id = %s
""",
(new_debit_tag_id, old_tag_id),
)
openupgrade.logged_query(
env.cr,
"""
UPDATE account_account_tag_account_move_line_rel r
SET account_account_tag_id = %s
FROM account_move_line aml
WHERE
r.account_move_line_id = aml.id AND
aml.credit > 0 AND
r.account_account_tag_id = %s
""",
(new_credit_tag_id, old_tag_id),
)
# The old tag should be deleted or deactivated, because the l10n VAT
# report would otherwise still use them. Besides, they are not
# necessary anymore anyway.
openupgrade.logged_query(
env.cr,
"""
UPDATE account_account_tag
SET active = FALSE
WHERE id = %s
""",
(old_tag_id,),
)
def unlink_invalid_tax_tags_from_move_lines(
env, module, base_tag_xmlids, tax_tag_xmlids
):
openupgrade.logged_query(
env.cr,
"""
DELETE FROM account_account_tag_account_move_line_rel r
WHERE
account_account_tag_id IN (
SELECT res_id FROM ir_model_data
WHERE
model = 'account.account.tag' AND
module = %s AND
name IN ('"""
+ "','".join(tax_tag_xmlids)
+ """')
) AND
account_move_line_id IN (
SELECT id FROM account_move_line
WHERE tax_base_amount = 0
)
""",
[module],
)
openupgrade.logged_query(
env.cr,
"""
DELETE FROM account_account_tag_account_move_line_rel r
WHERE
account_account_tag_id IN (
SELECT res_id FROM ir_model_data
WHERE
model = 'account.account.tag' AND
module = %s AND
name IN ('"""
+ "','".join(base_tag_xmlids)
+ """')
) AND
account_move_line_id IN (
SELECT id FROM account_move_line
WHERE tax_base_amount <> 0
)
""",
[module],
)
def unlink_invalid_tax_tags_from_repartition_lines(
env, module, base_tag_xmlids, tax_tag_xmlids
):
"""The migration script of the account module assigns all tags of
the account.tax's tag_ids field to the tag_ids field of the new
account.tax.repartition.line. However, because each repartition
line only needs a 'base' - or 'tax' tag, we clean up the other
tags.
"""
openupgrade.logged_query(
env.cr,
"""
DELETE FROM account_account_tag_account_tax_repartition_line_rel r
WHERE
account_tax_repartition_line_id IN (
SELECT id FROM account_tax_repartition_line
WHERE id = r.account_tax_repartition_line_id
AND repartition_type = 'base'
) AND
account_account_tag_id IN (
SELECT res_id FROM ir_model_data
WHERE
model = 'account.account.tag' AND
module = %s AND
name IN ('"""
+ "','".join(tax_tag_xmlids)
+ """')
)
""",
[module],
)
openupgrade.logged_query(
env.cr,
"""
DELETE FROM account_account_tag_account_tax_repartition_line_rel r
WHERE
account_tax_repartition_line_id IN (
SELECT id FROM account_tax_repartition_line
WHERE id = r.account_tax_repartition_line_id
AND repartition_type = 'tax'
) AND
account_account_tag_id IN (
SELECT res_id FROM ir_model_data
WHERE
model = 'account.account.tag' AND
module = %s AND
name IN ('"""
+ "','".join(base_tag_xmlids)
+ """')
)
""",
[module],
)