diff --git a/addons/hr_expense/data/hr_expense_demo.xml b/addons/hr_expense/data/hr_expense_demo.xml index ff9f5cd44e1..cd846406668 100644 --- a/addons/hr_expense/data/hr_expense_demo.xml +++ b/addons/hr_expense/data/hr_expense_demo.xml @@ -39,7 +39,6 @@ - @@ -49,7 +48,6 @@ - @@ -58,7 +56,7 @@ Travel by car - + @@ -67,7 +65,7 @@ BreakFast - + @@ -82,7 +80,7 @@ - + @@ -94,7 +92,7 @@ - + @@ -106,7 +104,7 @@ - + @@ -118,7 +116,7 @@ - + @@ -135,7 +133,7 @@ - + @@ -148,7 +146,6 @@ - @@ -166,7 +163,7 @@ - + @@ -177,7 +174,7 @@ - + @@ -188,7 +185,6 @@ - @@ -206,7 +202,6 @@ - @@ -217,7 +212,6 @@ - @@ -229,7 +223,6 @@ - diff --git a/addons/hr_expense/models/hr_expense.py b/addons/hr_expense/models/hr_expense.py index b807c4007d8..15edd84fde2 100644 --- a/addons/hr_expense/models/hr_expense.py +++ b/addons/hr_expense/models/hr_expense.py @@ -73,7 +73,7 @@ class HrExpense(models.Model): help="Both price-included and price-excluded taxes will behave as price-included taxes for expenses.") amount_tax = fields.Monetary(string='Tax amount in Currency', help="Tax amount in currency", compute='_compute_amount_tax', store=True, currency_field='currency_id') amount_tax_company = fields.Monetary('Tax amount', help="Tax amount in company currency", compute='_compute_amount_tax', store=True, currency_field='company_currency_id') - total_amount = fields.Monetary("Total In Currency", compute='_compute_amount', store=True, currency_field='currency_id', tracking=True, readonly=False) + total_amount = fields.Monetary("Total In Currency", compute='_compute_amount', store=True, currency_field='currency_id', tracking=True, readonly=False, inverse='_inverse_total_amount') untaxed_amount = fields.Monetary("Total Untaxed Amount In Currency", compute='_compute_amount_tax', store=True, currency_field='currency_id') company_currency_id = fields.Many2one('res.currency', string="Report Company Currency", related='company_id.currency_id', readonly=True) total_amount_company = fields.Monetary('Total', compute='_compute_amount_tax', store=True, currency_field='company_currency_id') @@ -173,7 +173,7 @@ class HrExpense(models.Model): @api.depends('quantity', 'unit_amount', 'tax_ids') def _compute_amount(self): for expense in self: - if expense.product_id and not expense.unit_amount: + if expense.product_id and not expense.product_has_cost: continue taxes = expense._get_taxes_results(expense.unit_amount, expense.quantity, expense.currency_id) expense.total_amount = taxes['total_included'] @@ -315,6 +315,11 @@ class HrExpense(models.Model): }) expense.analytic_distribution = distribution or expense.analytic_distribution + @api.onchange('total_amount') + def _inverse_total_amount(self): + for expense in self: + expense.unit_amount = expense.total_amount_company / expense.quantity + @api.constrains('payment_mode') def _check_payment_mode(self): self.sheet_id._check_payment_mode() @@ -670,7 +675,7 @@ class HrExpense(models.Model): vals = { 'employee_id': employee.id, 'name': expense_description, - 'unit_amount': price, + 'total_amount': price, 'product_id': product.id if product else None, 'product_uom_id': product.uom_id.id, 'tax_ids': [(4, tax.id, False) for tax in product.supplier_taxes_id.filtered(lambda r: r.company_id == company)], diff --git a/addons/hr_expense/tests/test_expenses.py b/addons/hr_expense/tests/test_expenses.py index e67b189e0dc..105700a3564 100644 --- a/addons/hr_expense/tests/test_expenses.py +++ b/addons/hr_expense/tests/test_expenses.py @@ -924,3 +924,23 @@ class TestExpenses(TestExpenseCommon): move = expense_sheet.account_move_id expected_date = fields.Date.from_string('2021-01-31') self.assertEqual(move.invoice_date_due, expected_date, 'Bill due date should follow employee payment terms') + + def test_inverse_total_amount(self): + """ Test if the inverse method works correctly """ + + expense = self.env['hr.expense'].create({ + 'name': 'Choucroute Saucisse', + 'employee_id': self.expense_employee.id, + 'product_id': self.product_c.id, + 'total_amount': 60, + 'unit_amount': 0, + 'tax_ids': [self.tax_purchase_a.id, self.tax_purchase_b.id], + 'analytic_distribution': { + self.analytic_account_1.id: 50, + self.analytic_account_2.id: 50, + }, + }) + + expense.total_amount = 90 + + self.assertEqual(expense.unit_amount, 90, 'Unit amount should be the same as total amount was written to')