From 1d25e75241d034fbf473833d51cafe0f1647ed59 Mon Sep 17 00:00:00 2001 From: "Ali Alfie (alal)" Date: Tue, 22 Aug 2023 10:09:46 +0200 Subject: [PATCH] [IMP] hr_expense: add attachments from expense to journal entries This commit adds a copy of the attachments from the expense report to the created journal entry. task-3443042 closes odoo/odoo#132955 Signed-off-by: Quentin De Paoli --- addons/hr_expense/models/hr_expense.py | 9 +- addons/hr_expense/tests/test_expenses.py | 113 +++++++++++++++++++++++ 2 files changed, 121 insertions(+), 1 deletion(-) diff --git a/addons/hr_expense/models/hr_expense.py b/addons/hr_expense/models/hr_expense.py index bde446849f4..4caee5c7b61 100644 --- a/addons/hr_expense/models/hr_expense.py +++ b/addons/hr_expense/models/hr_expense.py @@ -713,6 +713,9 @@ class HrExpense(models.Model): 'payment_method_line_id': payment_method_line.id, 'currency_id': self.currency_id.id, 'line_ids': [Command.create(line) for line in move_lines], + 'attachment_ids': [ + Command.create(attachment.copy_data({'res_model': 'account.move', 'res_id': False, 'raw': attachment.raw})[0]) + for attachment in self.message_main_attachment_id] } def _prepare_move_lines_vals(self): @@ -1507,7 +1510,11 @@ class HrExpenseSheet(models.Model): 'move_type': 'in_invoice', 'partner_id': self.employee_id.sudo().work_contact_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': [ + Command.create(attachment.copy_data({'res_model': 'account.move', 'res_id': False, 'raw': attachment.raw})[0]) + for attachment in self.expense_line_ids.message_main_attachment_id + ], } def _prepare_move_vals(self): diff --git a/addons/hr_expense/tests/test_expenses.py b/addons/hr_expense/tests/test_expenses.py index 34a64a1f968..6d519472a6a 100644 --- a/addons/hr_expense/tests/test_expenses.py +++ b/addons/hr_expense/tests/test_expenses.py @@ -1475,3 +1475,116 @@ class TestExpenses(TestExpenseCommon): {'amount_total_in_currency_signed': 1000.00, 'amount_total_signed': 2000.00, 'currency_id': foreign_currency.id}, {'amount_total_in_currency_signed': 1000.00, 'amount_total_signed': 1520.00, 'currency_id': foreign_currency.id}, ]) + + def test_attachments_in_move_from_own_expense(self): + """ Checks that journal entries created form expense reports paid by employee have a copy of the attachments in the expense. """ + expense = self.env['hr.expense'].create({ + 'name': 'Employee expense', + 'date': '2022-11-16', + 'payment_mode': 'own_account', + 'total_amount': 1000.00, + 'employee_id': self.expense_employee.id, + }) + expense_2 = self.env['hr.expense'].create({ + 'name': 'Employee expense 2', + 'date': '2022-11-16', + 'payment_mode': 'own_account', + 'total_amount': 1000.00, + 'employee_id': self.expense_employee.id, + }) + attachment = self.env['ir.attachment'].create({ + 'raw': b"R0lGODdhAQABAIAAAP///////ywAAAAAAQABAAACAkQBADs=", + 'name': 'file1.png', + 'res_model': 'hr.expense', + 'res_id': expense.id, + }) + attachment_2 = self.env['ir.attachment'].create({ + 'raw': b"R0lGODdhAQABAIAAAP///////ywAAAAAAQABAAACAkQBADs=", + 'name': 'file2.png', + 'res_model': 'hr.expense', + 'res_id': expense_2.id, + }) + + expense.message_main_attachment_id = attachment + expense_2.message_main_attachment_id = attachment_2 + expenses = expense | expense_2 + + expense_sheet = self.env['hr.expense.sheet'].create({ + 'name': 'Expenses paid by employee', + 'employee_id': self.expense_employee.id, + 'expense_line_ids': expenses, + }) + expense_sheet.action_submit_sheet() + expense_sheet.action_approve_expense_sheets() + expense_sheet.action_sheet_move_create() + + self.assertRecordValues(expense_sheet.account_move_ids.attachment_ids, [ + { + 'raw': b"R0lGODdhAQABAIAAAP///////ywAAAAAAQABAAACAkQBADs=", + 'name': 'file1.png', + 'res_model': 'account.move', + 'res_id': expense_sheet.account_move_ids.id + }, + { + 'raw': b"R0lGODdhAQABAIAAAP///////ywAAAAAAQABAAACAkQBADs=", + 'name': 'file2.png', + 'res_model': 'account.move', + 'res_id': expense_sheet.account_move_ids.id + } + ]) + + def test_attachments_in_move_from_company_expense(self): + """ Checks that journal entries created form expense reports paid by company have a copy of the attachments in the expense. """ + expense = self.env['hr.expense'].create({ + 'name': 'Company expense', + 'date': '2022-11-16', + 'payment_mode': 'company_account', + 'total_amount': 1000.00, + 'employee_id': self.expense_employee.id, + }) + expense_2 = self.env['hr.expense'].create({ + 'name': 'Company expense 2', + 'date': '2022-11-16', + 'payment_mode': 'company_account', + 'total_amount': 1000.00, + 'employee_id': self.expense_employee.id, + }) + attachment = self.env['ir.attachment'].create({ + 'raw': b"R0lGODdhAQABAIAAAP///////ywAAAAAAQABAAACAkQBADs=", + 'name': 'file1.png', + 'res_model': 'hr.expense', + 'res_id': expense.id, + }) + attachment_2 = self.env['ir.attachment'].create({ + 'raw': b"R0lGODdhAQABAIAAAP///////ywAAAAAAQABAAACAkQBADs=", + 'name': 'file2.png', + 'res_model': 'hr.expense', + 'res_id': expense_2.id, + }) + + expense.message_main_attachment_id = attachment + expense_2.message_main_attachment_id = attachment_2 + expenses = expense | expense_2 + + expense_sheet = self.env['hr.expense.sheet'].create({ + 'name': 'Expenses paid by company', + 'employee_id': self.expense_employee.id, + 'expense_line_ids': expenses, + }) + expense_sheet.action_submit_sheet() + expense_sheet.action_approve_expense_sheets() + expense_sheet.action_sheet_move_create() + + self.assertRecordValues(expense_sheet.account_move_ids[0].attachment_ids, [{ + 'raw': b"R0lGODdhAQABAIAAAP///////ywAAAAAAQABAAACAkQBADs=", + 'name': 'file1.png', + 'res_model': 'account.move', + 'res_id': expense_sheet.account_move_ids[0].id + }]) + + self.assertRecordValues(expense_sheet.account_move_ids[1].attachment_ids, [{ + 'raw': b"R0lGODdhAQABAIAAAP///////ywAAAAAAQABAAACAkQBADs=", + 'name': 'file2.png', + 'res_model': 'account.move', + 'res_id': expense_sheet.account_move_ids[1].id + }])