-
-
Notifications
You must be signed in to change notification settings - Fork 716
/
Copy pathpre-migration.py
444 lines (420 loc) · 15.7 KB
/
pre-migration.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# Copyright 2023 Viindoo - Trịnh Ngọc Hưng
# Copyright 2023 Viindoo - Nguyễn Đại Dương
# Copyright 2023 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from openupgradelib import openupgrade
_xmlids_renames = [
(
"sale.group_delivery_invoice_address",
"account.group_delivery_invoice_address",
),
]
_column_renames = {
"account_move": [
("auto_post", None),
],
}
_fields_renames = [
(
"account.analytic.line",
"account_analytic_line",
"move_id",
"move_line_id",
),
(
"account.tax.repartition.line.template",
"account_tax_repartition_line_template",
"minus_report_line_ids",
"minus_report_expression_ids",
),
(
"account.tax.repartition.line.template",
"account_tax_repartition_line_template",
"plus_report_line_ids",
"plus_report_expression_ids",
),
]
_models_renames = [
("account.tax.report", "account.report"),
("account.tax.carryover.line", "account.report.external.value"),
("account.tax.report.line", "account.report.line"),
]
_tables_renames = [
("account_tax_report", "account_report"),
("account_tax_carryover_line", "account_report_external_value"),
("account_tax_report_line", "account_report_line"),
]
def _fill_account_account_type(env, table):
"""Fill the account type deducing it from old types.
Used for both account.account and account.account.template.
"""
openupgrade.logged_query(
env.cr,
f"""
ALTER TABLE {table}
ADD COLUMN IF NOT EXISTS account_type VARCHAR
""",
)
openupgrade.logged_query(
env.cr,
f"""
WITH account_type_map AS (
SELECT
res_id AS user_type_id,
CASE
WHEN name = 'data_account_type_receivable' THEN 'asset_receivable'
WHEN name = 'data_account_type_payable' THEN 'liability_payable'
WHEN name = 'data_account_type_liquidity' THEN 'asset_cash'
WHEN name = 'data_account_type_credit_card' THEN 'liability_credit_card'
WHEN name = 'data_account_type_current_assets' THEN 'asset_current'
WHEN name = 'data_account_type_non_current_assets' THEN 'asset_non_current'
WHEN name = 'data_account_type_prepayments' THEN 'asset_prepayments'
WHEN name = 'data_account_type_fixed_assets' THEN 'asset_fixed'
WHEN name = 'data_account_type_current_liabilities'
THEN 'liability_current'
WHEN name = 'data_account_type_non_current_liabilities'
THEN 'liability_non_current'
WHEN name = 'data_account_type_equity' THEN 'equity'
WHEN name = 'data_unaffected_earnings' THEN 'equity_unaffected'
WHEN name = 'data_account_type_revenue' THEN 'income'
WHEN name = 'data_account_type_other_income' THEN 'income_other'
WHEN name = 'data_account_type_expenses' THEN 'expense'
WHEN name = 'data_account_type_depreciation' THEN 'expense_depreciation'
WHEN name = 'data_account_type_direct_costs' THEN 'expense_direct_cost'
ELSE 'off_balance'
END AS account_type
FROM ir_model_data
WHERE module='account' AND model = 'account.account.type'
)
UPDATE {table} aa
SET account_type = atm.account_type
FROM account_type_map atm
WHERE atm.user_type_id = aa.user_type_id
""",
)
# Fill the rest of "custom types" with `off_balance` type
openupgrade.logged_query(
env.cr,
f"""
UPDATE {table}
SET account_type='off_balance'
WHERE account_type IS NULL
""",
)
def _fill_account_account_include_initial_balance(env):
"""Fill include_initial_balance from the old account type."""
openupgrade.logged_query(
env.cr,
"""
ALTER TABLE account_account
ADD COLUMN IF NOT EXISTS include_initial_balance BOOL
""",
)
openupgrade.logged_query(
env.cr,
"""
UPDATE account_account aa
SET include_initial_balance = aat.include_initial_balance
FROM account_account_type aat
WHERE aat.id = aa.user_type_id
""",
)
def _delete_sql_constraints(env):
# Delete constraints to recreate it
openupgrade.delete_sql_constraint_safely(
env, "account", "account_journal", "code_company_uniq"
)
openupgrade.delete_sql_constraint_safely(
env, "account", "account_move_line", "check_accountable_required_fields"
)
openupgrade.delete_sql_constraint_safely(
env, "account", "account_move_line", "check_amount_currency_balance_sign"
)
openupgrade.delete_sql_constraint_safely(
env, "account", "account_move_line", "check_credit_debit"
)
def _account_bank_statement_line_fast_fill_internal_index(env):
if not openupgrade.column_exists(
env.cr, "account_bank_statement_line", "internal_index"
):
openupgrade.add_fields(
env,
[
(
"internal_index",
"account.bank.statement.line",
"account_bank_statement_line",
"char",
False,
"account",
)
],
)
openupgrade.logged_query(
env.cr,
"""
UPDATE account_bank_statement_line stmt
SET internal_index = concat(
to_char(am.date, 'YYYYMMDD'),
lpad((2147483647 - stmt.sequence)::text, 10, '0'),
lpad(am.id::text, 10, '0')
)
FROM account_move am
WHERE stmt.move_id = am.id;
""",
)
def _account_move_fast_fill_display_type(env):
"""
Respectively Fill display type is Null AND
Case 1: with am is not invoice
set display type is 'product'
Case 2: with am is invoice AND aml line tax
set display type is 'tax'
Case 3: with am is invoice AND aml line receivable or payable,
set display type is 'payment_term'
Case 4: with am is invoice
set display type is 'product'
Case 5: with aml is an accounting transaction occurring
set display type is 'product'
"""
openupgrade.logged_query(
env.cr,
"""
WITH sub AS (
SELECT
aml.id,
CASE
WHEN am.move_type NOT IN
('out_invoice', 'out_refund', 'in_invoice', 'in_refund')
THEN 'product'
WHEN aml.tax_line_id IS NOT NULL THEN 'tax'
WHEN aa.account_type IN
('asset_receivable', 'liability_payable') THEN 'payment_term'
ELSE 'product'
END AS display_type
FROM account_move_line AS aml
LEFT JOIN account_move AS am ON am.id = aml.move_id
LEFT JOIN account_account AS aa ON aa.id = aml.account_id
WHERE aml.display_type IS NULL AND am.id = aml.move_id
)
UPDATE account_move_line AS aml
SET display_type = sub.display_type
FROM sub
WHERE aml.id = sub.id;
""",
)
def _account_move_auto_post_boolean_to_selection(env):
openupgrade.logged_query(
env.cr,
"ALTER TABLE account_move ADD COLUMN auto_post VARCHAR",
)
openupgrade.logged_query(
env.cr,
f"""
UPDATE account_move AS am
SET auto_post =
CASE
WHEN {openupgrade.get_legacy_name('auto_post')}
THEN 'at_date'
ELSE 'no'
END;
""",
)
def _account_analytic_distribution_model_generate(env):
openupgrade.logged_query(
env.cr,
"ALTER TABLE account_analytic_distribution_model "
"ADD COLUMN IF NOT EXISTS partner_id INT4",
)
openupgrade.logged_query(
env.cr,
"ALTER TABLE account_analytic_distribution_model "
"ADD COLUMN IF NOT EXISTS product_id INT4",
)
openupgrade.logged_query(
env.cr,
"ALTER TABLE account_analytic_distribution_model "
"ADD COLUMN IF NOT EXISTS company_id INT4",
)
openupgrade.logged_query(
env.cr,
"ALTER TABLE account_analytic_distribution_model "
"ADD COLUMN IF NOT EXISTS account_prefix VARCHAR",
)
openupgrade.logged_query(
env.cr,
"""
WITH distribution_data AS (
WITH sub AS (
SELECT
all_line_data.analytic_default_id,
all_line_data.analytic_account_id,
SUM(all_line_data.percentage) AS percentage
FROM (
SELECT
analytic_default.id AS analytic_default_id,
account.id AS analytic_account_id,
100 AS percentage
FROM account_analytic_default analytic_default
JOIN account_analytic_account account
ON account.id = analytic_default.analytic_id
WHERE analytic_default.analytic_id IS NOT NULL
UNION ALL
SELECT
analytic_default.id AS analytic_default_id,
dist.account_id AS analytic_account_id,
dist.percentage AS percentage
FROM account_analytic_default analytic_default
JOIN account_analytic_default_account_analytic_tag_rel tag_rel
ON tag_rel.account_analytic_default_id = analytic_default.id
JOIN account_analytic_distribution dist
ON dist.tag_id = tag_rel.account_analytic_tag_id
JOIN account_analytic_tag aat
ON aat.id = tag_rel.account_analytic_tag_id
WHERE aat.active_analytic_distribution = true
) AS all_line_data
GROUP BY all_line_data.analytic_default_id, all_line_data.analytic_account_id
)
SELECT sub.analytic_default_id AS analytic_default_id,
jsonb_object_agg(sub.analytic_account_id::text, sub.percentage)
AS analytic_distribution
FROM sub
GROUP BY sub.analytic_default_id
)
INSERT INTO account_analytic_distribution_model (
account_prefix,
partner_id,
product_id,
company_id,
create_date,
write_date,
create_uid,
write_uid,
analytic_distribution)
SELECT
aa.code,
aad.partner_id,
aad.product_id,
COALESCE(aad.company_id, aaa.company_id, aa.company_id),
aad.create_date,
aad.write_date,
aad.create_uid,
aad.write_uid,
dist.analytic_distribution
FROM
distribution_data dist
JOIN account_analytic_default aad ON aad.id = dist.analytic_default_id
LEFT JOIN account_analytic_account aaa ON aaa.id = aad.analytic_id
LEFT JOIN account_account aa ON aa.id = aad.account_id
""",
)
def _fill_analytic_distribution(env, table, m2m_rel, m2m_column1):
"""Take all the lines, if have an analytic accounting account, it's 100%
combined with the analytic distribution of account analytic tag
then sum them together by analytic account.
Used for both account.move.line and account.reconcile.model.line.
"""
openupgrade.logged_query(
env.cr,
f"ALTER TABLE {table} ADD COLUMN IF NOT EXISTS analytic_distribution jsonb",
)
openupgrade.logged_query(
env.cr,
f"""
WITH distribution_data AS (
WITH sub AS (
SELECT
all_line_data.line_id,
all_line_data.analytic_account_id,
SUM(all_line_data.percentage) AS percentage
FROM (
SELECT
line.id AS line_id,
account.id AS analytic_account_id,
100 AS percentage
FROM {table} line
JOIN account_analytic_account account
ON account.id = line.analytic_account_id
WHERE line.analytic_account_id IS NOT NULL
UNION ALL
SELECT
line.id AS line_id,
dist.account_id AS analytic_account_id,
dist.percentage AS percentage
FROM {table} line
JOIN {m2m_rel} tag_rel
ON tag_rel.{m2m_column1} = line.id
JOIN account_analytic_distribution dist
ON dist.tag_id = tag_rel.account_analytic_tag_id
JOIN account_analytic_tag aat
ON aat.id = tag_rel.account_analytic_tag_id
WHERE aat.active_analytic_distribution = true
) AS all_line_data
GROUP BY all_line_data.line_id, all_line_data.analytic_account_id
)
SELECT sub.line_id,
jsonb_object_agg(sub.analytic_account_id::text, sub.percentage)
AS analytic_distribution
FROM sub
GROUP BY sub.line_id
)
UPDATE {table} line
SET analytic_distribution = dist.analytic_distribution
FROM distribution_data dist WHERE line.id = dist.line_id
""",
)
def _fast_fill_account_payment_amount_company_currency_signed(env):
"""Avoid the heavy recomputation of this field precreating the column and
filling it for the simple case. The rest will be done on post-migration.
"""
openupgrade.logged_query(
env.cr,
"ALTER TABLE account_payment "
"ADD COLUMN IF NOT EXISTS amount_company_currency_signed numeric",
)
openupgrade.logged_query(
env.cr,
"""
UPDATE account_payment ap
SET amount_company_currency_signed = (
CASE
WHEN payment_type = 'inbound'
THEN amount
ELSE -amount
END
)
FROM res_company rc,
account_move am
WHERE ap.currency_id = rc.currency_id
AND am.id = ap.move_id
AND rc.id = am.company_id
""",
)
@openupgrade.migrate()
def migrate(env, version):
openupgrade.rename_xmlids(env.cr, _xmlids_renames)
openupgrade.rename_columns(env.cr, _column_renames)
openupgrade.rename_fields(env, _fields_renames)
openupgrade.rename_models(env.cr, _models_renames)
openupgrade.rename_tables(env.cr, _tables_renames)
_fill_account_account_type(env, "account_account")
_fill_account_account_type(env, "account_account_template")
_fill_account_account_include_initial_balance(env)
_delete_sql_constraints(env)
_account_bank_statement_line_fast_fill_internal_index(env)
_account_move_fast_fill_display_type(env)
_account_move_auto_post_boolean_to_selection(env)
_account_analytic_distribution_model_generate(env)
_fill_analytic_distribution(
env,
"account_move_line",
"account_analytic_tag_account_move_line_rel",
"account_move_line_id",
)
_fill_analytic_distribution(
env,
"account_reconcile_model_line",
"account_reconcile_model_analytic_tag_rel",
"account_reconcile_model_line_id",
)
_fast_fill_account_payment_amount_company_currency_signed(env)