from odoo.tools import SQL lines = env['account.bank.statement.line'].browse([20262]) models = env['account.reconcile.model'].search([('trigger', '=', 'auto_reconcile'), ('can_be_proposed', '=', True)]) env['account.reconcile.model'].flush_model() lines.flush_recordset() # Run a simplified version of the _apply_reconcile_models SQL env.cr.execute(""" WITH matching_journal_ids AS ( SELECT account_reconcile_model_id, ARRAY_AGG(account_journal_id) AS ids FROM account_journal_account_reconcile_model_rel GROUP BY account_reconcile_model_id ), matching_partner_ids AS ( SELECT account_reconcile_model_id, ARRAY_AGG(res_partner_id) AS ids FROM account_reconcile_model_res_partner_rel GROUP BY account_reconcile_model_id ) SELECT st_line.id AS st_line_id, reco_model.id AS reco_model_id, reco_model.trigger FROM account_bank_statement_line st_line JOIN account_move move ON st_line.move_id = move.id LEFT JOIN LATERAL ( SELECT reco_model.id, reco_model.trigger FROM account_reconcile_model reco_model LEFT JOIN matching_journal_ids ON reco_model.id = matching_journal_ids.account_reconcile_model_id LEFT JOIN matching_partner_ids ON reco_model.id = matching_partner_ids.account_reconcile_model_id WHERE (matching_journal_ids.ids IS NULL OR st_line.journal_id = ANY(matching_journal_ids.ids)) AND (matching_partner_ids.ids IS NULL OR st_line.partner_id = ANY(matching_partner_ids.ids)) AND (reco_model.match_label IS NULL OR ( reco_model.match_label = 'contains' AND (st_line.payment_ref ILIKE '%%' || reco_model.match_label_param || '%%' OR move.narration::TEXT ILIKE '%%' || reco_model.match_label_param || '%%') )) AND reco_model.id IN %s AND reco_model.can_be_proposed IS TRUE AND reco_model.company_id = st_line.company_id ORDER BY reco_model.sequence ASC, reco_model.id ASC LIMIT 1 ) AS reco_model ON TRUE WHERE st_line.id IN %s """, (tuple(models.ids), tuple(lines.ids))) results = env.cr.fetchall() print(f'SQL results: {results}', flush=True) # Now check what the full _apply_reconcile_models method SQL has that's different # The key is that the method joins with model_fees and account_reconcile_model_line # Let me check if the model 47 has an account_reconcile_model_line with account_id set model47 = env['account.reconcile.model'].browse(47) print(f'Model 47 lines: {[(l.id, l.account_id.id, l.account_id.name) for l in model47.line_ids]}', flush=True) # Check the full method result print('Calling _apply_reconcile_models...', flush=True) lines2 = env['account.bank.statement.line'].browse([20266]) # FACEBK line print(f'Line 20266 before: reconciled={lines2.is_reconciled}', flush=True) models._apply_reconcile_models(lines2) env.cr.commit() lines2.invalidate_recordset() print(f'Line 20266 after: reconciled={lines2.is_reconciled}', flush=True)