fix: correct state calculation and cleanup logic for canceled expense moves

This commit is contained in:
Suherdy Yacob 2026-04-06 11:18:06 +07:00
parent 6dd8d6a387
commit 7807a9f284

View File

@ -10,7 +10,7 @@ class HrExpenseSheet(models.Model):
('wait_post', 'Wait Post')
], ondelete={'wait_post': 'set default'})
@api.depends('account_move_ids.payment_state', 'account_move_ids.amount_residual', 'expense_line_ids.receipt_received', 'expense_line_ids.realization_ids.state')
@api.depends('account_move_ids.payment_state', 'account_move_ids.amount_residual', 'account_move_ids.state', 'expense_line_ids.receipt_received', 'expense_line_ids.realization_ids.state')
def _compute_state(self):
# Store original states to detect transition to 'done'
original_states = {sheet.id: sheet.state for sheet in self}
@ -18,6 +18,12 @@ class HrExpenseSheet(models.Model):
super()._compute_state()
for sheet in self:
# FIX: If we have moves but they are ALL canceled, Odoo super() incorrectly sets state='post'.
# We must force it back to approval_state or draft.
active_moves = sheet.account_move_ids.filtered(lambda m: m.state != 'cancel')
if not active_moves and sheet.state == 'post':
sheet.state = sheet.approval_state or 'draft'
# Check for Company Account expenses
company_paid = sheet.expense_line_ids.filtered(lambda e: e.payment_mode == 'company_account')
@ -212,8 +218,8 @@ class HrExpenseSheet(models.Model):
cancel=True
)
# Unlink draft moves (including payment moves that are now draft/cancel)
sheet.account_move_ids.filtered(lambda m: m.state == 'draft').unlink()
# Unlink draft/canceled moves (including payment moves that are now draft/cancel)
sheet.account_move_ids.filtered(lambda m: m.state in ('draft', 'cancel')).unlink()
def action_reset_expense_sheets(self):
"""