e-taxes settings is single doctype now
This commit is contained in:
parent
639d580c19
commit
61ec366149
|
|
@ -1,90 +1,92 @@
|
|||
"""
|
||||
Migrate E-Taxes Settings from multi-record to Single doctype.
|
||||
|
||||
Before this patch the doctype had multiple records (with is_active/is_default flags).
|
||||
After converting to issingle=1 the data must live in tabSingles and child tables
|
||||
must have parent = "E-Taxes Settings".
|
||||
IMPORTANT: patches run after schema sync, so Frappe's ORM already treats
|
||||
E-Taxes Settings as a Single. All reads from the old table use raw SQL.
|
||||
"""
|
||||
|
||||
import frappe
|
||||
|
||||
|
||||
SCALAR_FIELDS = [
|
||||
"similarity_threshold",
|
||||
"consider_azeri_chars",
|
||||
"default_item_group",
|
||||
"default_uom",
|
||||
"default_item_tax_template",
|
||||
"default_supplier_group",
|
||||
"default_customer_group",
|
||||
"default_payment_terms",
|
||||
]
|
||||
|
||||
CHILD_TABLES = [
|
||||
"tabE-Taxes Item Mapping",
|
||||
"tabE-Taxes Customer Mappings",
|
||||
"tabE-Taxes Supplier Mappings",
|
||||
"tabE-Taxes Unit Mapping",
|
||||
"tabE-Taxes VAT Account Mapping",
|
||||
]
|
||||
|
||||
|
||||
def execute():
|
||||
# Nothing to do if the old table doesn't exist (fresh install)
|
||||
if not frappe.db.table_exists("tabE-Taxes Settings"):
|
||||
return
|
||||
|
||||
# Find the best old record: prefer is_default=1, then is_active=1, then any
|
||||
old_name = (
|
||||
frappe.db.get_value("E-Taxes Settings", {"is_default": 1}, "name", ignore=True)
|
||||
or frappe.db.get_value("E-Taxes Settings", {"is_active": 1}, "name", ignore=True)
|
||||
or frappe.db.get_value("E-Taxes Settings", {}, "name", ignore=True)
|
||||
)
|
||||
# Find the best old record via raw SQL.
|
||||
# Frappe ORM treats the doctype as Single by now, so we must bypass it.
|
||||
# Prefer is_default=1, then is_active=1, then any record with a non-standard name.
|
||||
# Wrap each attempt in try/except in case old columns were already dropped.
|
||||
old_row = None
|
||||
|
||||
if not old_name or old_name == "E-Taxes Settings":
|
||||
# Already migrated or nothing to migrate
|
||||
_migrate_child_tables(old_name)
|
||||
return
|
||||
|
||||
# Read the old record as raw dict
|
||||
old_row = frappe.db.get_value(
|
||||
"E-Taxes Settings",
|
||||
old_name,
|
||||
[
|
||||
"similarity_threshold",
|
||||
"consider_azeri_chars",
|
||||
"default_item_group",
|
||||
"default_uom",
|
||||
"default_item_tax_template",
|
||||
"default_supplier_group",
|
||||
"default_customer_group",
|
||||
"default_payment_terms",
|
||||
],
|
||||
as_dict=True,
|
||||
ignore=True,
|
||||
)
|
||||
for query in [
|
||||
"SELECT * FROM `tabE-Taxes Settings` WHERE `is_default` = 1 LIMIT 1",
|
||||
"SELECT * FROM `tabE-Taxes Settings` WHERE `is_active` = 1 LIMIT 1",
|
||||
"SELECT * FROM `tabE-Taxes Settings` WHERE `name` != 'E-Taxes Settings' LIMIT 1",
|
||||
]:
|
||||
try:
|
||||
old_row = _raw_get(query)
|
||||
except Exception:
|
||||
pass
|
||||
if old_row:
|
||||
break
|
||||
|
||||
if not old_row:
|
||||
return
|
||||
return # Fresh install or already migrated
|
||||
|
||||
# Write scalar fields into the Single record
|
||||
settings = frappe.get_single("E-Taxes Settings")
|
||||
for field, value in old_row.items():
|
||||
if value is not None:
|
||||
setattr(settings, field, value)
|
||||
old_name = old_row.get("name")
|
||||
|
||||
# Migrate child tables: update parent to "E-Taxes Settings"
|
||||
_migrate_child_tables(old_name)
|
||||
# Write scalar fields into tabSingles via raw SQL
|
||||
for field in SCALAR_FIELDS:
|
||||
value = old_row.get(field)
|
||||
if value is None:
|
||||
continue
|
||||
frappe.db.sql(
|
||||
"""
|
||||
INSERT INTO `tabSingles` (doctype, field, value)
|
||||
VALUES ('E-Taxes Settings', %s, %s)
|
||||
ON DUPLICATE KEY UPDATE value = VALUES(value)
|
||||
""",
|
||||
(field, str(value)),
|
||||
)
|
||||
|
||||
# Save the single record (scalar fields only — child rows are already re-parented)
|
||||
settings.flags.ignore_validate = True
|
||||
settings.save(ignore_permissions=True)
|
||||
# Re-parent child table rows from old name → "E-Taxes Settings"
|
||||
for table in CHILD_TABLES:
|
||||
if not frappe.db.table_exists(table):
|
||||
continue
|
||||
frappe.db.sql(
|
||||
f"""
|
||||
UPDATE `{table}`
|
||||
SET parent = 'E-Taxes Settings',
|
||||
parenttype = 'E-Taxes Settings'
|
||||
WHERE parent = %s
|
||||
""",
|
||||
(old_name,),
|
||||
)
|
||||
|
||||
frappe.db.commit()
|
||||
frappe.clear_cache(doctype="E-Taxes Settings")
|
||||
|
||||
|
||||
def _migrate_child_tables(old_name):
|
||||
"""Re-parent child table rows from the old record name to 'E-Taxes Settings'."""
|
||||
if not old_name or old_name == "E-Taxes Settings":
|
||||
return
|
||||
|
||||
child_tables = [
|
||||
"tabE-Taxes Item Mapping",
|
||||
"tabE-Taxes Customer Mappings",
|
||||
"tabE-Taxes Supplier Mappings",
|
||||
"tabE-Taxes Unit Mapping",
|
||||
"tabE-Taxes VAT Account Mapping",
|
||||
]
|
||||
|
||||
for table in child_tables:
|
||||
if frappe.db.table_exists(table):
|
||||
frappe.db.sql(
|
||||
f"""
|
||||
UPDATE `{table}`
|
||||
SET parent = 'E-Taxes Settings',
|
||||
parenttype = 'E-Taxes Settings'
|
||||
WHERE parent = %s
|
||||
AND parenttype = 'E-Taxes Settings'
|
||||
""",
|
||||
(old_name,),
|
||||
)
|
||||
def _raw_get(query):
|
||||
rows = frappe.db.sql(query, as_dict=True)
|
||||
return rows[0] if rows else None
|
||||
|
|
|
|||
Loading…
Reference in New Issue