import logging from odoo import models, api _logger = logging.getLogger(__name__) class FusionAccountingScoring(models.AbstractModel): _name = 'fusion.accounting.scoring' _description = 'Fusion Accounting Confidence Scoring' def calculate_confidence(self, tool_name, scenario_key=None): domain = [('tool_name', '=', tool_name)] if scenario_key: domain.append(('tool_params', 'ilike', scenario_key)) history = self.env['fusion.accounting.match.history'].search(domain) if not history: return 0.0 decided = history.filtered(lambda h: h.decision in ('approved', 'rejected')) if not decided: return 0.0 approved = len(decided.filtered(lambda h: h.decision == 'approved')) return approved / len(decided) def check_promotions(self): ICP = self.env['ir.config_parameter'].sudo() threshold = float(ICP.get_param('fusion_accounting.tier3_threshold', '0.95')) min_sample = int(ICP.get_param('fusion_accounting.tier3_min_sample', '30')) rules = self.env['fusion.accounting.rule'].search([ ('active', '=', True), ('approval_tier', '=', 'needs_approval'), ]) promoted = self.env['fusion.accounting.rule'] for rule in rules: if rule.total_uses >= min_sample and rule.confidence_score >= threshold: rule.approval_tier = 'auto' promoted |= rule _logger.info( "Promoted rule '%s' to auto (confidence=%.2f, sample=%d)", rule.name, rule.confidence_score, rule.total_uses, ) return promoted def get_tool_stats(self, tool_name=None): domain = [] if tool_name: domain.append(('tool_name', '=', tool_name)) history = self.env['fusion.accounting.match.history'].search(domain) stats = {} for h in history: if h.tool_name not in stats: stats[h.tool_name] = { 'total': 0, 'approved': 0, 'rejected': 0, 'pending': 0, 'auto': 0, } stats[h.tool_name]['total'] += 1 if h.decision in stats[h.tool_name]: stats[h.tool_name][h.decision] += 1 return stats