| 1 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 2 |
from odoo import api, SUPERUSER_ID |
| 3 |
from odoo.tools import SQL |
| 4 |
|
| 5 |
|
| 6 |
def _get_tax_ids_for_xml_id(env, xml_id): |
| 7 |
rows = env.execute_query(SQL( |
| 8 |
""" |
| 9 |
SELECT res_id |
| 10 |
FROM ir_model_data |
| 11 |
WHERE model = 'account.tax' |
| 12 |
AND name LIKE %s |
| 13 |
""", f"%{xml_id}" |
| 14 |
)) |
| 15 |
return [res_id for res_id, in rows] |
| 16 |
|
| 17 |
|
| 18 |
def migrate(cr, version): |
| 19 |
env = api.Environment(cr, SUPERUSER_ID, {}) |
| 20 |
|
| 21 |
goods_tax_ids = [tax_id for xml_id in ['btw_X0_producten', 'btw_X0'] |
| 22 |
for tax_id in _get_tax_ids_for_xml_id(env, xml_id)] |
| 23 |
|
| 24 |
goods_taxes = env['account.tax'].browse(goods_tax_ids) |
| 25 |
services_taxes = env['account.tax'].browse(_get_tax_ids_for_xml_id(env, 'btw_X0_diensten')) |
| 26 |
|
| 27 |
old_3bl_tax_tags = env['account.account.tag']._get_tax_tags('3bl (omzet)', 'nl') |
| 28 |
old_3b_tax_tags = env['account.account.tag']._get_tax_tags('3b (omzet)', 'nl') |
| 29 |
if not old_3bl_tax_tags and not old_3b_tax_tags: |
| 30 |
return |
| 31 |
|
| 32 |
goods_tax_tags = env['account.account.tag']._get_tax_tags('3bg (omzet)', 'nl') |
| 33 |
services_tax_tags = env['account.account.tag']._get_tax_tags('3bs (omzet)', 'nl') |
| 34 |
|
| 35 |
old_3bl_tax_tags_plus = old_3bl_tax_tags.filtered(lambda tag: not tag.tax_negate).id |
| 36 |
old_3b_tax_tags_plus = old_3b_tax_tags.filtered(lambda tag: not tag.tax_negate).id |
| 37 |
old_plus_tax_tag_ids = [] |
| 38 |
if old_3bl_tax_tags_plus: |
| 39 |
old_plus_tax_tag_ids.append(old_3bl_tax_tags_plus) |
| 40 |
if old_3b_tax_tags_plus: |
| 41 |
old_plus_tax_tag_ids.append(old_3b_tax_tags_plus) |
| 42 |
|
| 43 |
old_3bl_tax_tags_minus = old_3bl_tax_tags.filtered(lambda tag: tag.tax_negate).id |
| 44 |
old_3b_tax_tags_minus = old_3b_tax_tags.filtered(lambda tag: tag.tax_negate).id |
| 45 |
old_minus_tax_tag_ids = [] |
| 46 |
if old_3bl_tax_tags_minus: |
| 47 |
old_minus_tax_tag_ids.append(old_3bl_tax_tags_minus) |
| 48 |
if old_3b_tax_tags_minus: |
| 49 |
old_minus_tax_tag_ids.append(old_3b_tax_tags_minus) |
| 50 |
|
| 51 |
goods_plus_tax_tag_id = goods_tax_tags.filtered(lambda tag: not tag.tax_negate).id |
| 52 |
goods_minus_tax_tag_id = goods_tax_tags.filtered(lambda tag: tag.tax_negate).id |
| 53 |
services_plus_tax_tag_id = services_tax_tags.filtered(lambda tag: not tag.tax_negate).id |
| 54 |
services_minus_tax_tag_id = services_tax_tags.filtered(lambda tag: tag.tax_negate).id |
| 55 |
|
| 56 |
insert_query_params = [ |
| 57 |
(goods_plus_tax_tag_id, goods_taxes.ids, old_plus_tax_tag_ids, goods_taxes.invoice_repartition_line_ids.ids), |
| 58 |
(services_plus_tax_tag_id, services_taxes.ids, old_plus_tax_tag_ids, services_taxes.invoice_repartition_line_ids.ids), |
| 59 |
(goods_minus_tax_tag_id, goods_taxes.ids, old_minus_tax_tag_ids, goods_taxes.refund_repartition_line_ids.ids), |
| 60 |
(services_minus_tax_tag_id, services_taxes.ids, old_minus_tax_tag_ids, services_taxes.refund_repartition_line_ids.ids), |
| 61 |
] |
| 62 |
insert_query_parts = [] |
| 63 |
|
| 64 |
for new_tax_tag_id, tax_ids, old_tax_tag_ids, repartition_line_ids in insert_query_params: |
| 65 |
insert_query_parts.append( |
| 66 |
SQL(""" |
| 67 |
SELECT tag_aml_rel.account_move_line_id, %s |
| 68 |
FROM account_account_tag_account_move_line_rel tag_aml_rel |
| 69 |
JOIN account_move_line_account_tax_rel aml_at_rel ON aml_at_rel.account_move_line_id = tag_aml_rel.account_move_line_id |
| 70 |
WHERE aml_at_rel.account_tax_id = ANY(%s) |
| 71 |
AND tag_aml_rel.account_account_tag_id = ANY(%s) |
| 72 |
""", |
| 73 |
new_tax_tag_id, tax_ids, old_tax_tag_ids |
| 74 |
) |
| 75 |
) |
| 76 |
|
| 77 |
if len(old_tax_tag_ids) > 1: |
| 78 |
cr.execute(SQL( |
| 79 |
""" |
| 80 |
DELETE FROM account_account_tag_account_tax_repartition_line_rel tag_aml_rel |
| 81 |
WHERE tag_aml_rel.account_account_tag_id = %s |
| 82 |
AND ( |
| 83 |
SELECT COUNT(*) |
| 84 |
FROM account_account_tag_account_tax_repartition_line_rel sub_tag_aml_rel |
| 85 |
WHERE sub_tag_aml_rel.account_tax_repartition_line_id = tag_aml_rel.account_tax_repartition_line_id |
| 86 |
AND sub_tag_aml_rel.account_account_tag_id = %s |
| 87 |
) >= 1 |
| 88 |
""", |
| 89 |
old_tax_tag_ids[0], old_tax_tag_ids[1] |
| 90 |
)) |
| 91 |
|
| 92 |
cr.execute(SQL( |
| 93 |
""" |
| 94 |
UPDATE account_account_tag_account_tax_repartition_line_rel |
| 95 |
SET account_account_tag_id = %s |
| 96 |
WHERE account_tax_repartition_line_id = ANY(%s) |
| 97 |
AND account_account_tag_id = ANY(%s) |
| 98 |
""", |
| 99 |
new_tax_tag_id, repartition_line_ids, old_tax_tag_ids |
| 100 |
)) |
| 101 |
|
| 102 |
cr.execute(SQL( |
| 103 |
""" |
| 104 |
INSERT INTO account_account_tag_account_move_line_rel (account_move_line_id, account_account_tag_id) |
| 105 |
%s |
| 106 |
ON CONFLICT DO NOTHING |
| 107 |
""", |
| 108 |
SQL(" UNION ").join(insert_query_parts) |
| 109 |
)) |