From 68aa40de06d35a781650c4331c601462123c2aa7 Mon Sep 17 00:00:00 2001 From: Laurent Smet Date: Mon, 11 Jan 2021 08:26:28 +0000 Subject: [PATCH] [IMP] account: introduce a new field on move lines to avoid inverting tax tags The sign of tax tags sometimes could get inverted explicitly. It typically happened for misc operations, so : - manual entries - CABA entries - writeoffs created from the reconciliation widget We had to do things this way in stable to fix those use cases, but it's not ideal, as the tags shown to the user were inverted, so it was a bit confusing, as one would have expected to see the exact same tags as on tax configuration. We fix that by using a boolean field on account.move.line telling whether or not the sign must be inverted. This allows keeping the same tags as in the tax config, and simplifies the computation of the tax report's multiplicator, making the overall model more intuitive for both user and developper. Task 2363287 closes #62536 closes odoo/odoo#64627 Related: odoo/enterprise#15031 Related: odoo/upgrade#1983 X-original-commit: 4e8581b6846ef8ee4601c994b0d586734a4fe117 Signed-off-by: Laurent Smet Signed-off-by: oco-odoo --- addons/account/models/account_move.py | 22 ++++++++++------- addons/hr_expense/tests/test_expenses.py | 30 ++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py index 1cc71ad27cd..1fb8d05613c 100644 --- a/addons/account/models/account_move.py +++ b/addons/account/models/account_move.py @@ -617,19 +617,22 @@ class AccountMove(models.Model): 'tax_base_amount': 0.0, 'grouping_dict': False, } - self.line_ids -= to_remove + if not recompute_tax_base_amount: + self.line_ids -= to_remove # ==== Mount base lines ==== for line in self.line_ids.filtered(lambda line: not line.tax_repartition_line_id): # Don't call compute_all if there is no tax. if not line.tax_ids: - line.tax_tag_ids = [(5, 0, 0)] + if not recompute_tax_base_amount: + line.tax_tag_ids = [(5, 0, 0)] continue compute_all_vals = _compute_base_line_taxes(line) # Assign tags on base line - line.tax_tag_ids = compute_all_vals['base_tags'] or [(5, 0, 0)] + if not recompute_tax_base_amount: + line.tax_tag_ids = compute_all_vals['base_tags'] or [(5, 0, 0)] tax_exigible = True for tax_vals in compute_all_vals['taxes']: @@ -651,20 +654,22 @@ class AccountMove(models.Model): taxes_map_entry['amount'] += tax_vals['amount'] taxes_map_entry['tax_base_amount'] += self._get_base_amount_to_display(tax_vals['base'], tax_repartition_line, tax_vals['group']) taxes_map_entry['grouping_dict'] = grouping_dict - line.tax_exigible = tax_exigible + if not recompute_tax_base_amount: + line.tax_exigible = tax_exigible # ==== Process taxes_map ==== for taxes_map_entry in taxes_map.values(): # The tax line is no longer used in any base lines, drop it. if taxes_map_entry['tax_line'] and not taxes_map_entry['grouping_dict']: - self.line_ids -= taxes_map_entry['tax_line'] + if not recompute_tax_base_amount: + self.line_ids -= taxes_map_entry['tax_line'] continue currency = self.env['res.currency'].browse(taxes_map_entry['grouping_dict']['currency_id']) # Don't create tax lines with zero balance. if currency.is_zero(taxes_map_entry['amount']): - if taxes_map_entry['tax_line']: + if taxes_map_entry['tax_line'] and not recompute_tax_base_amount: self.line_ids -= taxes_map_entry['tax_line'] continue @@ -672,8 +677,9 @@ class AccountMove(models.Model): tax_base_amount = currency._convert(taxes_map_entry['tax_base_amount'], self.company_currency_id, self.company_id, self.date or fields.Date.context_today(self)) # Recompute only the tax_base_amount. - if taxes_map_entry['tax_line'] and recompute_tax_base_amount: - taxes_map_entry['tax_line'].tax_base_amount = tax_base_amount + if recompute_tax_base_amount: + if taxes_map_entry['tax_line']: + taxes_map_entry['tax_line'].tax_base_amount = tax_base_amount continue balance = currency._convert( diff --git a/addons/hr_expense/tests/test_expenses.py b/addons/hr_expense/tests/test_expenses.py index f2d1fd86676..9cf10b651bf 100644 --- a/addons/hr_expense/tests/test_expenses.py +++ b/addons/hr_expense/tests/test_expenses.py @@ -138,3 +138,33 @@ class TestExpenses(TestExpenseCommon): 'currency_id': self.company_data['currency'].id, }, ]) + + def test_expenses_with_tax_and_lockdate(self): + ''' Test creating a journal entry for multiple expenses using taxes. A lock date is set in order to trigger + the recomputation of the taxes base amount. + ''' + self.env.company.tax_lock_date = '2020-02-01' + + expense = self.env['hr.expense.sheet'].create({ + 'name': 'Expense for John Smith', + 'employee_id': self.expense_employee.id, + 'accounting_date': '2020-01-01' + }) + + for i in range(2): + expense_line = self.env['hr.expense'].create({ + 'name': 'Car Travel Expenses', + 'employee_id': self.expense_employee.id, + 'product_id': self.product_a.id, + 'unit_amount': 350.00, + 'tax_ids': [(6, 0, [self.tax_purchase_a.id])], + 'sheet_id': expense.id, + 'analytic_account_id': self.analytic_account_1.id, + }) + expense_line._onchange_product_id_date_account_id() + + expense.action_submit_sheet() + expense.approve_expense_sheets() + + # Assert not "Cannot create unbalanced journal entry" error. + expense.action_sheet_move_create()