diff --git a/invoice_az/hooks.py b/invoice_az/hooks.py index 8761efa..7af745f 100644 --- a/invoice_az/hooks.py +++ b/invoice_az/hooks.py @@ -73,18 +73,22 @@ def after_install(): """Run installation tasks""" from invoice_az.auth import setup_token_renewal from invoice_az.master_data.sync import sync_all + from invoice_az.setup_defaults import ensure_settings_defaults setup_token_renewal() sync_all() + ensure_settings_defaults() def after_migrate(): """Run migration tasks""" from invoice_az.auth import setup_token_renewal from invoice_az.master_data.sync import sync_all + from invoice_az.setup_defaults import ensure_settings_defaults setup_token_renewal() sync_all() + ensure_settings_defaults() fixtures = [] diff --git a/invoice_az/setup_defaults.py b/invoice_az/setup_defaults.py new file mode 100644 index 0000000..d7dbf00 --- /dev/null +++ b/invoice_az/setup_defaults.py @@ -0,0 +1,33 @@ +import frappe + +# Sensible out-of-the-box defaults for E-Taxes Settings on a fresh base. +# These are required Link fields with no field-level default, so without this +# they start empty on every new install and have to be filled by hand. +SETTINGS_DEFAULTS = { + "default_item_group": ("Item Group", "All Item Groups"), + "default_uom": ("UOM", "Nos"), +} + + +def ensure_settings_defaults(): + """Populate E-Taxes Settings defaults if they are empty. + + Idempotent: only writes a field when it is currently blank AND the target + record exists. Safe to run on every after_install / after_migrate — it + never overwrites a value an admin has already chosen. + """ + settings = frappe.get_single("E-Taxes Settings") + + changed = False + for fieldname, (doctype, value) in SETTINGS_DEFAULTS.items(): + if settings.get(fieldname): + continue + if not frappe.db.exists(doctype, value): + continue + settings.set(fieldname, value) + changed = True + + if changed: + settings.flags.ignore_permissions = True + settings.save(ignore_permissions=True) + frappe.db.commit()