# -*- coding: utf-8 -*- from odoo import models, fields, api, _ class PaymentAmountFixWizard(models.TransientModel): _name = 'payment.amount.fix.wizard' _description = 'Fix Payment Amount Calculation' payment_ids = fields.Many2many( 'account.payment', string='Payments to Fix', domain=[('amount_substract', '>', 0), ('state', '=', 'posted')], help='Payments with deductions that may have incorrect amounts' ) @api.model def default_get(self, fields_list): """Pre-select payments that need fixing.""" res = super().default_get(fields_list) # Find payments that might need fixing payments = self.env['account.payment'].search([ ('amount_substract', '>', 0), ('state', '=', 'posted'), ]) payments_to_fix = [] for payment in payments: if payment.move_id: counterpart_lines = payment.move_id.line_ids.filtered( lambda l: l.debit > 0 and l.account_id.account_type in ('liability_payable', 'expense') ) if counterpart_lines: correct_amount = counterpart_lines[0].debit if abs(payment.amount - correct_amount) > 0.01: payments_to_fix.append(payment.id) res['payment_ids'] = [(6, 0, payments_to_fix)] return res def action_fix_amounts(self): """Fix the selected payments.""" fixed_count = 0 for payment in self.payment_ids: if payment.move_id: counterpart_lines = payment.move_id.line_ids.filtered( lambda l: l.debit > 0 and l.account_id.account_type in ('liability_payable', 'expense') ) if counterpart_lines: correct_amount = counterpart_lines[0].debit if abs(payment.amount - correct_amount) > 0.01: # Fix using SQL to avoid sync issues payment.env.cr.execute( "UPDATE account_payment SET amount = %s WHERE id = %s", (correct_amount, payment.id) ) payment.invalidate_recordset(['amount']) payment._compute_final_payment_amount() fixed_count += 1 return { 'type': 'ir.actions.client', 'tag': 'display_notification', 'params': { 'title': _('Fix Complete'), 'message': _('Fixed %d payments with incorrect amounts.') % fixed_count, 'type': 'success', } }