Skip to content

Commit

Permalink
[FIX] hr_expense: set correct account_id on upload
Browse files Browse the repository at this point in the history
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#127891

X-original-commit: c6b0481
Signed-off-by: John Laterre (jol) <jol@odoo.com>
  • Loading branch information
yosa-odoo authored and epictete committed Jul 10, 2023
1 parent 3d2f766 commit b970cae
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
7 changes: 5 additions & 2 deletions addons/hr_expense/models/hr_expense.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 20 additions & 0 deletions addons/hr_expense/tests/test_expenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit b970cae

Please sign in to comment.