| 1 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 2 |
from odoo import api, SUPERUSER_ID |
| 3 |
|
| 4 |
|
| 5 |
def migrate(cr, version): |
| 6 |
env = api.Environment(cr, SUPERUSER_ID, {"lang": "en_US"}) |
| 7 |
|
| 8 |
companies = env["res.company"].search([("chart_template", "=", "id"), ("parent_id", "=", False)]) |
| 9 |
|
| 10 |
new_tax_groups = ["l10n_id_tax_group_stlg", "l10n_id_tax_group_non_luxury_goods", "l10n_id_tax_group_luxury_goods", "l10n_id_tax_group_0"] |
| 11 |
new_taxes = [ |
| 12 |
"tax_ST4", "tax_PT4", |
| 13 |
"tax_ST5", "tax_PT5", |
| 14 |
"tax_ST6", "tax_ST7", |
| 15 |
"tax_luxury_sales_pemungut_ppn", |
| 16 |
"tax_PT6", "tax_PT7", |
| 17 |
] |
| 18 |
|
| 19 |
for company in companies: |
| 20 |
ChartTemplate = env["account.chart.template"].with_company(company) |
| 21 |
# ============================= |
| 22 |
# Load new tax data |
| 23 |
tax_group_data = { |
| 24 |
xmlid: data |
| 25 |
for xmlid, data in ChartTemplate._get_account_tax_group("id").items() |
| 26 |
if xmlid in new_tax_groups |
| 27 |
} |
| 28 |
tax_data = { |
| 29 |
xmlid: data |
| 30 |
for xmlid, data in ChartTemplate._get_account_tax("id").items() |
| 31 |
if xmlid in new_taxes |
| 32 |
} |
| 33 |
new_tax_group_data = {} |
| 34 |
if (tax_group_data): |
| 35 |
new_tax_group_data = { |
| 36 |
g: data |
| 37 |
for g, data in tax_group_data.items() |
| 38 |
if not ChartTemplate.ref(g, raise_if_not_found=False) |
| 39 |
} |
| 40 |
|
| 41 |
if new_tax_group_data: |
| 42 |
data = {"account.tax.group": new_tax_group_data} |
| 43 |
ChartTemplate._pre_reload_data(company, {}, data) |
| 44 |
ChartTemplate._load_data(data) |
| 45 |
if tax_data: |
| 46 |
data = {"account.tax": tax_data} |
| 47 |
ChartTemplate._pre_reload_data(company, {}, data) |
| 48 |
ChartTemplate._load_data(data) |
| 49 |
# ============================= |
| 50 |
# Update existing tax description |
| 51 |
tax_map = { |
| 52 |
"tax_ST0": "VAT Not Collected", |
| 53 |
"tax_PT0": "Zero-Rated", |
| 54 |
"tax_ST2": "Exempt", |
| 55 |
"tax_PT2": "Exempt", |
| 56 |
"tax_ST3": "Taxable Luxury Goods", |
| 57 |
"tax_PT3": "Standard Rate for Luxury Goods & Services", |
| 58 |
} |
| 59 |
for xmlid, new_description in tax_map.items(): |
| 60 |
tax = ChartTemplate.ref(xmlid, raise_if_not_found=False) |
| 61 |
if not tax: |
| 62 |
continue |
| 63 |
# Only update description if it hasn't been manually changed |
| 64 |
if tax.description == xmlid.replace("tax_", "").upper(): |
| 65 |
tax.description = new_description |
| 66 |
|
| 67 |
# ============================= |
| 68 |
# Archive ST1 and PT1 |
| 69 |
for xmlid in ["tax_ST1", "tax_PT1"]: |
| 70 |
tax = ChartTemplate.ref(xmlid, raise_if_not_found=False) |
| 71 |
if tax and not tax.active: |
| 72 |
continue |
| 73 |
if tax: |
| 74 |
tax.active = False |
| 75 |
|
| 76 |
# ============================= |
| 77 |
# Remove l10n_id.ppn_tag from specific taxes |
| 78 |
taxes_to_clean = [ |
| 79 |
"tax_ST1", "tax_PT1", |
| 80 |
"tax_ST3", "tax_PT3", |
| 81 |
"tax_luxury_sales", |
| 82 |
] |
| 83 |
ppn_tag = env.ref("l10n_id.ppn_tag", raise_if_not_found=False) |
| 84 |
if ppn_tag: |
| 85 |
tax_records = env["account.tax"].browse([]) |
| 86 |
for xmlid in taxes_to_clean: |
| 87 |
rec = ChartTemplate.ref(xmlid, raise_if_not_found=False) |
| 88 |
if rec: |
| 89 |
tax_records |= rec |
| 90 |
|
| 91 |
if tax_records: |
| 92 |
repartition_lines = env["account.tax.repartition.line"].sudo().search([ |
| 93 |
("tax_id", "in", tax_records.ids), |
| 94 |
("tag_ids", "in", [ppn_tag.id]), |
| 95 |
]) |
| 96 |
if repartition_lines: |
| 97 |
repartition_lines.write({"tag_ids": [(3, ppn_tag.id)]}) |
| 98 |
|
| 99 |
# ============================= |
| 100 |
# Update tax_luxury_sales group, description and invoice_label |
| 101 |
old_group = ChartTemplate.ref("l10n_id_tax_group_luxury_goods", raise_if_not_found=False) |
| 102 |
new_group = ChartTemplate.ref("l10n_id_tax_group_stlg", raise_if_not_found=False) |
| 103 |
tax_luxury_sales = ChartTemplate.ref("tax_luxury_sales", raise_if_not_found=False) |
| 104 |
if not (old_group and new_group and tax_luxury_sales): |
| 105 |
continue |
| 106 |
tax_luxury_sales_vals = {} |
| 107 |
if tax_luxury_sales.tax_group_id == old_group: |
| 108 |
tax_luxury_sales_vals['tax_group_id'] = new_group.id |
| 109 |
if tax_luxury_sales.description == "Luxury": |
| 110 |
tax_luxury_sales_vals['description'] = "Sales Tax on Luxury Goods (STLG)" |
| 111 |
if tax_luxury_sales.invoice_label == "Luxury Goods (ID)": |
| 112 |
tax_luxury_sales_vals['invoice_label'] = "20%" |
| 113 |
if tax_luxury_sales.name == "20%": |
| 114 |
tax_luxury_sales_vals['name'] = "20% (STLG)" |
| 115 |
if tax_luxury_sales.is_base_affected: |
| 116 |
tax_luxury_sales['is_base_affected'] = False |
| 117 |
tax_luxury_sales.write(tax_luxury_sales_vals) |