From c9c5a4583754160fa7c96d6cbc680c71390f1255 Mon Sep 17 00:00:00 2001 From: Suherdy Yacob Date: Mon, 6 Apr 2026 10:23:44 +0700 Subject: [PATCH] fix: bypass hr_expense synchronization restriction for draft payments to allow account move updates --- models/account_payment.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/models/account_payment.py b/models/account_payment.py index f0828af..c9327b4 100644 --- a/models/account_payment.py +++ b/models/account_payment.py @@ -1,6 +1,36 @@ -from odoo import fields, models +from odoo import fields, models, _ +from odoo.exceptions import UserError class AccountPayment(models.Model): _inherit = 'account.payment' realization_id = fields.Many2one('hr.expense.realization', string='Originating Realization', readonly=True) + + def _synchronize_to_moves(self, changed_fields): + """ + Bypass standard Odoo restriction for payments linked to expense reports + if the payment is still in Draft state. This allows applying deductions + or refining clearing accounts. + """ + # If we have draft payments linked to a sheet, we want to skip the hr_expense UserError + draft_payouts = self.filtered(lambda p: p.state == 'draft' and p.expense_sheet_id) + if draft_payouts: + # Store the original links + links = {p.id: p.expense_sheet_id.id for p in draft_payouts} + + # Temporarily clear the field in the instance cache to fool the hr_expense method + for p in draft_payouts: + p.env.cache.set(p, p._fields['expense_sheet_id'], False) + + try: + # This will call the chain, including hr_expense (which will now see no sheet) + # and finally reach account module to perform the move sync. + res = super()._synchronize_to_moves(changed_fields) + finally: + # Restore the links in the cache + for p in draft_payouts: + if p.id in links: + p.env.cache.set(p, p._fields['expense_sheet_id'], self.env['hr.expense.sheet'].browse(links[p.id])) + return res + + return super()._synchronize_to_moves(changed_fields)