[FIX] hr_expense: correct bank account in payment register wizard

To reproduce:
* Add a bank account to Employee, and a bank account to your company.
* Set the company on the employee's contact to yours.
* Create an expense to be reimbursed to the employee, submit it and try
to "Register Payment".

Current behaviour: the recipient bank account in the wizard is set to
the company's.
Expected behaviour: the bank account in the wizard should be set to the
employee's bank account.

This commit solves this.

task-3837305

closes odoo/odoo#160749

Signed-off-by: William André (wan) <wan@odoo.com>
This commit is contained in:
Ali Alfie (alal)
2024-04-19 16:58:41 +00:00
parent a586ece3b0
commit 1ed71ba1fa
3 changed files with 46 additions and 8 deletions
@@ -711,6 +711,7 @@ class HrExpenseSheet(models.Model):
'ref': self.name, 'ref': self.name,
'move_type': 'in_invoice', 'move_type': 'in_invoice',
'partner_id': self.employee_id.sudo().work_contact_id.id, 'partner_id': self.employee_id.sudo().work_contact_id.id,
'partner_bank_id': self.employee_id.sudo().bank_account_id.id,
'currency_id': self.currency_id.id, 'currency_id': self.currency_id.id,
'line_ids': [Command.create(expense._prepare_move_lines_vals()) for expense in self.expense_line_ids], 'line_ids': [Command.create(expense._prepare_move_lines_vals()) for expense in self.expense_line_ids],
'attachment_ids': [ 'attachment_ids': [
+36
View File
@@ -1031,3 +1031,39 @@ class TestExpenses(TestExpenseCommon):
}]) }])
expense_state = Expense.get_expense_dashboard() expense_state = Expense.get_expense_dashboard()
self.assertEqual(expense_state['to_submit']['amount'], 3000.00) self.assertEqual(expense_state['to_submit']['amount'], 3000.00)
def test_payment_register_bank_from_expense_reimbursed_to_employee(self):
"""
Test that creating an expense to be paid to an employee having a commercial partner (the company listed in the
employee's contact) will have the employee's bank account in the register payment wizard.
"""
# Set bank account in employee.
self.expense_employee.bank_account_id = self.env['res.partner.bank'].create({
'acc_number': 'BE32707171912447',
'partner_id': self.expense_employee.work_contact_id.id,
'acc_type': 'bank',
})
# Set bank account in company.
self.env.company.partner_id.bank_ids = self.env['res.partner.bank'].create({
'acc_number': 'BE457268179587463',
'partner_id': self.env.company.id,
'acc_type': 'bank',
})
# Set commercial partner in employee's contact.
self.expense_employee.work_contact_id.commercial_partner_id = self.env.company.partner_id
expense = self.env['hr.expense'].create({
'name': 'expense_1',
'total_amount': 10.0,
'product_id': self.product_c.id,
'payment_mode': 'own_account',
'employee_id': self.expense_employee.id
})
sheet = self.env['hr.expense.sheet'].create(expense._get_default_expense_sheet_values())
sheet.action_submit_sheet()
sheet.action_approve_expense_sheets()
sheet.action_sheet_move_create()
action_data = sheet.action_register_payment()
with Form(self.env[action_data['res_model']].with_context(action_data['context'])) as wiz_form:
self.assertEqual(wiz_form.amount, 10)
self.assertEqual(wiz_form.partner_bank_id, self.expense_employee.bank_account_id)
@@ -11,15 +11,16 @@ class AccountPaymentRegister(models.TransientModel):
# ------------------------------------------------------------------------- # -------------------------------------------------------------------------
@api.model @api.model
def _get_line_batch_key(self, line): def _get_batch_available_partner_banks(self, batch_result, journal):
# OVERRIDE to set the bank account defined on the employee # OVERRIDE to set the bank account defined on the employee
res = super()._get_line_batch_key(line) expense_sheet = batch_result['lines'].move_id.expense_sheet_id.filtered(lambda sheet: sheet and sheet.payment_mode == 'own_account')
expense_sheet = line.move_id.expense_sheet_id.filtered(lambda sheet: sheet and sheet.payment_mode == 'own_account') if expense_sheet and batch_result['payment_values']['payment_type'] == 'outbound':
if expense_sheet and not line.move_id.partner_bank_id: # We use sudo since we may not have access to the employee_id record. If the env wasn't already in sudo,
res['partner_bank_id'] = expense_sheet.employee_id.sudo().bank_account_id.id \ # we should un-sudo the record before returning it.
or line.partner_id.bank_ids \ sudo_bank_account_id = expense_sheet.employee_id.sudo().bank_account_id
and line.partner_id.bank_ids.ids[0] return sudo_bank_account_id.sudo(self.env.su)
return res else:
return super()._get_batch_available_partner_banks(batch_result, journal)
def _init_payments(self, to_process, edit_mode=False): def _init_payments(self, to_process, edit_mode=False):
# OVERRIDE # OVERRIDE