feat(nexa_coa_setup): archive unused taxes

Adds _archive_unused_taxes hook that archives all active taxes whose
name is not in the curated keep-set (GST/HST/QST/PST per province + zero
rated + exempt) AND that have zero usage on existing move lines.

Reduces active taxes from 49 to 30 on staging. The 'HST for sales/
purchases - 13%' pair is kept active because of historical postings
(215 sales lines + 1 purchase line) — new invoicing routes to the
cleaner '13% HST' via fiscal positions.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
gsinghpal
2026-05-12 19:09:37 -04:00
parent 9f28dce160
commit 3559eb1fd5

View File

@@ -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)