From b970caef2291e71fdcf938a5c49b9f711b0c7823 Mon Sep 17 00:00:00 2001 From: Yolann Sabaux Date: Thu, 29 Jun 2023 12:13:54 +0000 Subject: [PATCH] [FIX] hr_expense: set correct account_id on upload Steps to reproduce: - On the expense product by default, set an account_id - Upload an expense Issue: The account_id that we set is not the one on the newly created expense Cause: We don't give any account_id during creation. Therefore, the account_id by default is set: https://github.com/odoo/odoo/blob/7bcd64c51b2e87ece462c75a528c9ad6e1601633/addons/hr_expense/models/hr_expense.py#L31-L32 opw-3343007 closes odoo/odoo#127891 X-original-commit: c6b048196be92392ee34e50cc7ab13107632b87e Signed-off-by: John Laterre (jol) --- addons/hr_expense/models/hr_expense.py | 7 +++++-- addons/hr_expense/tests/test_expenses.py | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/addons/hr_expense/models/hr_expense.py b/addons/hr_expense/models/hr_expense.py index 08352d0467840..dee9373942d3b 100644 --- a/addons/hr_expense/models/hr_expense.py +++ b/addons/hr_expense/models/hr_expense.py @@ -393,11 +393,14 @@ def create_expense_from_attachments(self, attachment_ids=None, view_type='list') for attachment in attachments: attachment_name = '.'.join(attachment.name.split('.')[:-1]) - expense = self.env['hr.expense'].create({ + vals = { 'name': attachment_name, 'unit_amount': 0, 'product_id': product.id, - }) + } + if product.property_account_expense_id: + vals['account_id'] = product.property_account_expense_id.id + expense = self.env['hr.expense'].create(vals) attachment.write({ 'res_model': 'hr.expense', 'res_id': expense.id, diff --git a/addons/hr_expense/tests/test_expenses.py b/addons/hr_expense/tests/test_expenses.py index b478b111458a1..87e1bb9c13426 100644 --- a/addons/hr_expense/tests/test_expenses.py +++ b/addons/hr_expense/tests/test_expenses.py @@ -1075,3 +1075,23 @@ def test_inverse_total_amount(self): expense.total_amount = 90 self.assertEqual(expense.unit_amount, 90, 'Unit amount should be the same as total amount was written to') + + def test_expense_from_attachments(self): + # avoid passing through extraction when installed + if 'hr.expense.extract.words' in self.env: + self.env.company.expense_extract_show_ocr_option_selection = 'no_send' + self.env.user.employee_id = self.expense_employee.id + attachment = self.env['ir.attachment'].create({ + 'datas': b"R0lGODdhAQABAIAAAP///////ywAAAAAAQABAAACAkQBADs=", + 'name': 'file.png', + 'res_model': 'hr.expense', + }) + product = self.env['product.product'].search([('can_be_expensed', '=', True)]) + # reproduce the same way we get the product by default + if product: + product = product.filtered(lambda p: p.default_code == "EXP_GEN") or product[0] + product.property_account_expense_id = self.company_data['default_account_payable'] + + self.env['hr.expense'].create_expense_from_attachments(attachment.id) + expense = self.env['hr.expense'].search([], order='id desc', limit=1) + self.assertEqual(expense.account_id, product.property_account_expense_id, "The expense account should be the default one of the product")