[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) <jol@odoo.com>
This commit is contained in:
Yolann Sabaux
2023-07-10 16:59:33 +02:00
committed by John Laterre (jol)
parent 3d2f76671c
commit b970caef22
2 changed files with 25 additions and 2 deletions
+5 -2
View File
@@ -393,11 +393,14 @@ class HrExpense(models.Model):
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,
+20
View File
@@ -1075,3 +1075,23 @@ class TestExpenses(TestExpenseCommon):
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")