Skip to content

Commit

Permalink
[FIX] hr_expense: multi-currency
Browse files Browse the repository at this point in the history
- Set your company to USD
- Create an expense in EUR:
  Amount: 100
  Tax: 15% Excluded
- Validate, post the journal entries

It crashes because of a missing `.id`, but on top of that... not a
single AML is correct.

opw-1938570

closes #30987
  • Loading branch information
nim-odoo committed Feb 11, 2019
1 parent 728bb2a commit 44b791f
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 14 deletions.
35 changes: 21 additions & 14 deletions addons/hr_expense/models/hr_expense.py
Expand Up @@ -253,7 +253,7 @@ def _get_account_move_line_values(self):
account_date = expense.sheet_id.accounting_date or expense.date or fields.Date.context_today(expense)

company_currency = expense.company_id.currency_id
different_currency = expense.currency_id != company_currency
different_currency = expense.currency_id and expense.currency_id != company_currency

move_line_values = []
taxes = expense.tax_ids.with_context(round=True).compute_all(expense.unit_amount, expense.currency_id, expense.quantity, expense.product_id)
Expand All @@ -262,13 +262,17 @@ def _get_account_move_line_values(self):
partner_id = expense.employee_id.address_home_id.commercial_partner_id.id

# source move line
amount_currency = expense.total_amount if different_currency else False
amount = taxes['total_excluded']
amount_currency = False
if different_currency:
amount = expense.currency_id.with_context(date=account_date, company_id=expense.company_id.id).compute(amount, company_currency)
amount_currency = taxes['total_excluded']
move_line_src = {
'name': move_line_name,
'quantity': expense.quantity or 1,
'debit': taxes['total_excluded'] > 0 and taxes['total_excluded'],
'credit': taxes['total_excluded'] < 0 and -taxes['total_excluded'],
'amount_currency': taxes['total_excluded'] > 0 and abs(amount_currency) or -abs(amount_currency),
'debit': amount if amount > 0 else 0,
'credit': -amount if amount < 0 else 0,
'amount_currency': amount_currency if different_currency else 0.0,
'account_id': account_src.id,
'product_id': expense.product_id.id,
'product_uom_id': expense.product_uom_id.id,
Expand All @@ -285,22 +289,25 @@ def _get_account_move_line_values(self):

# taxes move lines
for tax in taxes['taxes']:
price = expense.currency_id.with_context(date=account_date).compute(tax['amount'], company_currency)
amount_currency = price if different_currency else False
amount = tax['amount']
amount_currency = False
if different_currency:
amount = expense.currency_id.with_context(date=account_date, company_id=expense.company_id.id).compute(amount, company_currency)
amount_currency = tax['amount']
move_line_tax_values = {
'name': tax['name'],
'quantity': 1,
'debit': price > 0 and price,
'credit': price < 0 and -price,
'amount_currency': price > 0 and abs(amount_currency) or -abs(amount_currency),
'debit': amount if amount > 0 else 0,
'credit': -amount if amount < 0 else 0,
'amount_currency': amount_currency if different_currency else 0.0,
'account_id': tax['account_id'] or move_line_src['account_id'],
'tax_line_id': tax['id'],
'expense_id': expense.id,
'partner_id': partner_id,
'currency_id': expense.currency_id if different_currency else False,
'currency_id': expense.currency_id.id if different_currency else False,
}
total_amount -= price
total_amount_currency -= move_line_tax_values['amount_currency'] or price
total_amount -= amount
total_amount_currency -= move_line_tax_values['amount_currency'] or amount
move_line_values.append(move_line_tax_values)

# destination move line
Expand All @@ -310,7 +317,7 @@ def _get_account_move_line_values(self):
'credit': total_amount < 0 and -total_amount,
'account_id': account_dst,
'date_maturity': account_date,
'amount_currency': total_amount > 0 and abs(amount_currency) or -abs(amount_currency),
'amount_currency': total_amount_currency if different_currency else 0.0,
'currency_id': expense.currency_id.id if different_currency else False,
'expense_id': expense.id,
'partner_id': partner_id,
Expand Down
64 changes: 64 additions & 0 deletions addons/hr_expense/tests/test_expenses.py
Expand Up @@ -73,6 +73,70 @@ def test_account_entry(self):
self.assertAlmostEquals(self.analytic_account.line_ids[0].amount, -636.36, "Amount on the only AAL is wrong")
self.assertEquals(self.analytic_account.line_ids[0].product_id, self.product_expense, "Product of AAL should be the one from the expense")

def test_account_entry_multi_currency(self):
""" Checking accounting move entries and analytic entries when submitting expense. With
multi-currency. And taxes. """

# Clean-up the rates
self.cr.execute("UPDATE res_company SET currency_id = %s WHERE id = %s", [self.env.ref('base.USD').id, self.env.user.company_id.id])
self.env['res.currency.rate'].search([]).unlink()
self.env['res.currency.rate'].create({
'currency_id': self.env.ref('base.EUR').id,
'company_id': self.env.user.company_id.id,
'rate': 2.0,
'name': '2010-01-01',
})

expense = self.env['hr.expense.sheet'].create({
'name': 'Expense for Dick Tracy',
'employee_id': self.employee.id,
})
expense_line = self.env['hr.expense'].create({
'name': 'Choucroute Saucisse',
'employee_id': self.employee.id,
'product_id': self.product_expense.id,
'unit_amount': 700.00,
'tax_ids': [(6, 0, [self.tax.id])],
'sheet_id': expense.id,
'analytic_account_id': self.analytic_account.id,
'currency_id': self.env.ref('base.EUR').id,
})
expense_line._onchange_product_id()
# Submitted to Manager
self.assertEquals(expense.state, 'submit', 'Expense is not in Reported state')
# Approve
expense.approve_expense_sheets()
self.assertEquals(expense.state, 'approve', 'Expense is not in Approved state')
# Create Expense Entries
expense.action_sheet_move_create()
self.assertEquals(expense.state, 'post', 'Expense is not in Waiting Payment state')
self.assertTrue(expense.account_move_id.id, 'Expense Journal Entry is not created')

# Should get this result [(0.0, 350.0, -700.0), (318.18, 0.0, 636.36), (31.82, 0.0, 63.64)]
for line in expense.account_move_id.line_ids:
if line.credit:
self.assertAlmostEquals(line.credit, 350.0)
self.assertAlmostEquals(line.amount_currency, -700.0)
self.assertEquals(len(line.analytic_line_ids), 0, "The credit move line should not have analytic lines")
self.assertFalse(line.product_id, "Product of credit move line should be false")
else:
if not line.tax_line_id == self.tax:
self.assertAlmostEquals(line.debit, 318.18)
self.assertAlmostEquals(line.amount_currency, 636.36)
self.assertEquals(len(line.analytic_line_ids), 1, "The debit move line should have 1 analytic lines")
self.assertEquals(line.product_id, self.product_expense, "Product of debit move line should be the one from the expense")
else:
self.assertAlmostEquals(line.debit, 31.82)
self.assertAlmostEquals(line.amount_currency, 63.64)
self.assertEquals(len(line.analytic_line_ids), 0, "The tax move line should not have analytic lines")
self.assertFalse(line.product_id, "Product of tax move line should be false")

self.assertEquals(self.analytic_account.line_ids, expense.account_move_id.mapped('line_ids.analytic_line_ids'))
self.assertEquals(len(self.analytic_account.line_ids), 1, "Analytic Account should have only one line")
self.assertAlmostEquals(self.analytic_account.line_ids[0].amount, -318.18, "Amount on the only AAL is wrong")
self.assertAlmostEquals(self.analytic_account.line_ids[0].currency_id, self.env.user.company_id.currency_id, "Currency on the only AAL is wrong")
self.assertEquals(self.analytic_account.line_ids[0].product_id, self.product_expense, "Product of AAL should be the one from the expense")

def test_expense_from_email(self):
user_demo = self.env.ref('base.user_demo')
self.tax.price_include = False
Expand Down

0 comments on commit 44b791f

Please sign in to comment.