From 2ed464bc6bf4fa35e1ad2704ed9db71789c39257 Mon Sep 17 00:00:00 2001
From: "Andrea Grazioso (agr-odoo)" <agr@odoo.com>
Date: Fri, 22 Sep 2023 18:03:56 +0200
Subject: [PATCH] [FIX] account,hr_expense: double base line amount

Create an expense sheet (report)
Add 2+ expenses with 10% included tax to the same employee
Approve expenses and post journal entries
Bill will be created
Go to the tax report
Group by "Tax > Account" or "Account > Tax"

Issue: Bill line will have the tax basis calculated incorrectly
This occurs because the bill will have 2 separate tax line and the query
 does not handle this case

opw-3510371

closes odoo/odoo#136339

Signed-off-by: Laurent Smet (las) <las@odoo.com>
---
 addons/account/models/account_move_line_tax_details.py | 8 ++++++++
 addons/hr_expense/models/account_move_line.py          | 3 +++
 2 files changed, 11 insertions(+)

diff --git a/addons/account/models/account_move_line_tax_details.py b/addons/account/models/account_move_line_tax_details.py
index 4024e426c405..7e162aa9f180 100644
--- a/addons/account/models/account_move_line_tax_details.py
+++ b/addons/account/models/account_move_line_tax_details.py
@@ -24,6 +24,11 @@ class AccountMoveLine(models.Model):
         tables, where_clause, where_params = query.get_sql()
         return self._get_query_tax_details(tables, where_clause, where_params, fallback=fallback)
 
+    @api.model
+    def _get_extra_query_base_tax_line_mapping(self):
+        #TO OVERRIDE
+        return ''
+
     @api.model
     def _get_query_tax_details(self, tables, where_clause, where_params, fallback=True):
         """ Create the tax details sub-query based on the orm domain passed as parameter.
@@ -81,6 +86,8 @@ class AccountMoveLine(models.Model):
             fallback_query = ''
             fallback_params = []
 
+        extra_query_base_tax_line_mapping = self._get_extra_query_base_tax_line_mapping()
+
         return f'''
             /*
             As example to explain the different parts of the query, we'll consider a move with the following lines:
@@ -185,6 +192,7 @@ class AccountMoveLine(models.Model):
                         OR (base_line.analytic_distribution IS NULL AND account_move_line.analytic_distribution IS NULL)
                         OR base_line.analytic_distribution = account_move_line.analytic_distribution
                     )
+                    {extra_query_base_tax_line_mapping}
                 LEFT JOIN affecting_base_tax_ids tax_line_tax_ids ON tax_line_tax_ids.id = account_move_line.id
                 JOIN affecting_base_tax_ids base_line_tax_ids ON base_line_tax_ids.id = base_line.id
                 WHERE account_move_line.tax_repartition_line_id IS NOT NULL
diff --git a/addons/hr_expense/models/account_move_line.py b/addons/hr_expense/models/account_move_line.py
index afd2cd6d4bfe..3ab4109cad54 100644
--- a/addons/hr_expense/models/account_move_line.py
+++ b/addons/hr_expense/models/account_move_line.py
@@ -57,3 +57,6 @@ class AccountMoveLine(models.Model):
             result.setdefault('extra_context', {})
             result['extra_context']['force_price_include'] = True
         return result
+
+    def _get_extra_query_base_tax_line_mapping(self):
+        return ' AND (base_line.expense_id IS NULL OR account_move_line.expense_id = base_line.expense_id)'
-- 
GitLab