# -*- coding: utf-8 -*- import logging _logger = logging.getLogger(__name__) def post_init_hook(env): """Imperative one-shot operations after module data is loaded. Each helper is idempotent — safe to re-run on -u. """ _logger.info("nexa_coa_setup: post_init_hook starting") _normalize_company_hst_number(env) _archive_unused_l10n_ca_accounts(env) _rename_legacy_accounts(env) _lock_fiscal_year_2025(env) _logger.info("nexa_coa_setup: post_init_hook complete") 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) if not company: return vat = (company.partner_id.vat or "").strip() if vat == "741224877": company.partner_id.vat = "741224877 RT0001" _logger.info("nexa_coa_setup: normalized HST# to '741224877 RT0001'") def _archive_unused_l10n_ca_accounts(env): """Stub — filled in Phase 4. Archives ~370 unused accounts.""" pass def _rename_legacy_accounts(env): """Stub — filled in Phase 4. Renames the 14xx/15xx legacy accounts.""" pass def _lock_fiscal_year_2025(env): """Try to set fiscalyear_lock_date = 2025-12-31 on main company. If Odoo blocks the lock because unreconciled bank statement lines or other open items exist in the period, log a clear warning and continue. The user can set the lock manually via Accounting > Configuration > Settings > Lock Dates once those items are cleaned up. """ from datetime import date from odoo.exceptions import RedirectWarning, UserError, ValidationError company = env.ref("base.main_company", raise_if_not_found=False) if not company: return target = date(2025, 12, 31) if company.fiscalyear_lock_date and company.fiscalyear_lock_date >= target: _logger.info("nexa_coa_setup: fiscalyear_lock_date already at or after 2025-12-31") return try: company.fiscalyear_lock_date = target _logger.info("nexa_coa_setup: fiscalyear_lock_date set to 2025-12-31") except (RedirectWarning, UserError, ValidationError) as exc: _logger.warning( "nexa_coa_setup: could not auto-lock fiscal year 2025-12-31. " "Reason: %s. Set the lock manually via Accounting > Configuration > " "Settings > Lock Dates after the unreconciled items in the period " "are cleaned up.", exc, )