diff --git a/nexa_coa_setup/hooks.py b/nexa_coa_setup/hooks.py index babbb238..59fbcd13 100644 --- a/nexa_coa_setup/hooks.py +++ b/nexa_coa_setup/hooks.py @@ -79,10 +79,52 @@ def post_init_hook(env): _normalize_company_hst_number(env) _archive_unused_l10n_ca_accounts(env) _rename_legacy_accounts(env) + _archive_unused_taxes(env) _lock_fiscal_year_2025(env) _logger.info("nexa_coa_setup: post_init_hook complete") +# Tax names to keep ACTIVE (covers GST/HST/QST/PST across provinces + zero-rated +# export + exempt). Everything else gets archived if it has zero usage on +# existing journal entries. +_KEEP_TAX_NAMES = { + "5% GST", "13% HST", "14% HST", "15% HST", + "11% GST+PST SK", "12% GST+PST BC", "12% GST+PST MB", "14.975% GST+QST", + "9.975% QST", "7% PST BC", "8% PST MB", "6% PST SK", "5% PST SK", + "0% GST", "0% Exempt", "0% Int", +} + + +def _archive_unused_taxes(env): + """Archive active taxes whose name is NOT in _KEEP_TAX_NAMES AND that + have no usage on existing move lines. Preserves audit trail for historical + moves; just hides duplicates and unused defaults from the active set. + """ + keep_names = list(_KEEP_TAX_NAMES) + env.cr.execute( + """ + SELECT t.id + FROM account_tax t + WHERE t.active = true + AND COALESCE(t.name->>'en_US', '') != ALL(%s) + AND NOT EXISTS ( + SELECT 1 FROM account_move_line_account_tax_rel r + WHERE r.account_tax_id = t.id + ) + """, + (keep_names,), + ) + ids = [r[0] for r in env.cr.fetchall()] + if not ids: + _logger.info("nexa_coa_setup: no unused taxes to archive") + return + env["account.tax"].browse(ids).write({"active": False}) + _logger.info( + "nexa_coa_setup: archived %d unused taxes (kept set: %d names)", + len(ids), len(keep_names), + ) + + def _normalize_company_hst_number(env): """Convert '741224877' to '741224877 RT0001' if not already in full form.""" company = env.ref("base.main_company", raise_if_not_found=False)