diff --git a/hr_expense_invoice/models/hr_expense_sheet.py b/hr_expense_invoice/models/hr_expense_sheet.py index a74840d37..f47f90f8b 100644 --- a/hr_expense_invoice/models/hr_expense_sheet.py +++ b/hr_expense_invoice/models/hr_expense_sheet.py @@ -2,9 +2,9 @@ # Copyright 2015-2024 Tecnativa - Pedro M. Baeza # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -from odoo import _, api, fields, models +from odoo import Command, _, api, fields, models from odoo.exceptions import UserError -from odoo.tools import float_compare +from odoo.tools import config, float_compare class HrExpenseSheet(models.Model): @@ -70,6 +70,21 @@ def _compute_invoice_count(self): can_read and len(sheet.expense_line_ids.mapped("invoice_id")) or 0 ) + def _prepare_bills_vals(self): + res = super()._prepare_bills_vals() + test_condition = not config["test_enable"] or self.env.context.get( + "test_hr_expense_invoice" + ) + if test_condition: + expenses_without_invoice = self.expense_line_ids.filtered( + lambda r: not r.invoice_id + ) + res["line_ids"] = [ + Command.create(expense._prepare_move_lines_vals()) + for expense in expenses_without_invoice + ] + return res + @api.depends( "expense_line_ids.invoice_id.payment_state", "expense_line_ids.amount_residual", diff --git a/hr_expense_invoice/tests/test_hr_expense_invoice.py b/hr_expense_invoice/tests/test_hr_expense_invoice.py index 6ab27fb10..b93a10ecb 100644 --- a/hr_expense_invoice/tests/test_hr_expense_invoice.py +++ b/hr_expense_invoice/tests/test_hr_expense_invoice.py @@ -18,7 +18,11 @@ class TestHrExpenseInvoice(TestExpenseCommon): @classmethod def setUpClass(cls, chart_template_ref=None): super().setUpClass(chart_template_ref=chart_template_ref) - cls.env = cls.env(context=dict(cls.env.context, **DISABLED_MAIL_CONTEXT)) + cls.env = cls.env( + context=dict( + cls.env.context, **DISABLED_MAIL_CONTEXT, test_hr_expense_invoice=True + ) + ) cls.account_payment_register = cls.env["account.payment.register"] cls.payment_obj = cls.env["account.payment"] cls.cash_journal = cls.company_data["default_journal_cash"] @@ -246,3 +250,21 @@ def test_4_hr_expense_constraint(self): sheet._validate_expense_invoice() self.expense.total_amount_currency = 100.0 sheet._validate_expense_invoice() + + def test_5_hr_expense_invoice_no_duplicate_accounting_entries(self): + """Test that expenses linked to invoices don't create + duplicate accounting entries.""" + sheet = self._action_submit_expenses(self.expense + self.expense2) + + with Form(self.expense) as f: + f.invoice_id = self.invoice + sheet.action_approve_expense_sheets() + with self.assertRaises(UserError): + sheet.action_sheet_move_create() + self.invoice.action_post() + sheet.action_sheet_move_create() + self.assertEqual(len(sheet.account_move_ids.invoice_line_ids), 1) + self.assertEqual( + sheet.account_move_ids.invoice_line_ids.price_total, + self.expense2.total_amount, + )