diff --git a/addons/sale_project/models/sale_order.py b/addons/sale_project/models/sale_order.py index bf03f42a516..91197cf3657 100644 --- a/addons/sale_project/models/sale_order.py +++ b/addons/sale_project/models/sale_order.py @@ -115,7 +115,6 @@ class SaleOrder(models.Model): projects = order.order_line.mapped('product_id.project_id') projects |= order.order_line.mapped('project_id') projects |= order.project_id - projects = projects.filtered('active') projects |= projects_per_so[order.id or order._origin.id] if not is_project_manager: projects = projects._filter_access_rules('read') @@ -201,7 +200,7 @@ class SaleOrder(models.Model): action = { 'type': 'ir.actions.act_window', 'name': _('Projects'), - 'domain': ['|', ('sale_order_id', '=', self.id), ('id', 'in', self.project_ids.ids)], + 'domain': ['|', ('sale_order_id', '=', self.id), ('id', 'in', self.with_context(active_test=False).project_ids.ids), ('active', 'in', [True, False])], 'res_model': 'project.project', 'views': [(False, 'kanban'), (False, 'tree'), (False, 'form')], 'view_mode': 'kanban,tree,form', @@ -212,7 +211,7 @@ class SaleOrder(models.Model): 'default_allow_billable': 1, } } - if len(self.project_ids) == 1: + if len(self.with_context(active_test=False).project_ids) == 1: action.update({'views': [(False, 'form')], 'res_id': self.project_ids.id}) return action diff --git a/addons/sale_project/tests/test_sale_project.py b/addons/sale_project/tests/test_sale_project.py index ea8b64f9922..6cfc5e8f6fc 100644 --- a/addons/sale_project/tests/test_sale_project.py +++ b/addons/sale_project/tests/test_sale_project.py @@ -495,3 +495,64 @@ class TestSaleProject(TestSaleProjectCommon): self.assertEqual(sale_order.analytic_account_id.company_id, sale_order.project_ids.company_id, "The company_id of the account created should be the company of the project.") self.assertEqual(sale_order.analytic_account_id.plan_id, project_plan, "The plan of the account created should be the default analytic plan of the setting") self.assertEqual(sale_order.analytic_account_id, sale_order.project_ids.analytic_account_id, "The project created for the SO and the SO should have the same account.") + + def test_include_archived_projects_in_stat_btn_related_view(self): + """Checks if the project stat-button action includes both archived and active projects.""" + # Setup + project_A = self.env['project.project'].create({'name': 'Project_A'}) + project_B = self.env['project.project'].create({'name': 'Project_B'}) + + product_A = self.env['product.product'].create({ + 'name': 'product A', + 'list_price': 1.0, + 'type': 'service', + 'service_tracking': 'task_global_project', + 'project_id':project_A.id, + }) + product_B = self.env['product.product'].create({ + 'name': 'product B', + 'list_price': 2.0, + 'type': 'service', + 'service_tracking': 'task_global_project', + 'project_id':project_B.id, + }) + + sale_order = self.env['sale.order'].with_context(tracking_disable=True).create({ + 'partner_id': self.partner.id, + 'partner_invoice_id': self.partner.id, + 'partner_shipping_id': self.partner.id, + }) + + SaleOrderLine = self.env['sale.order.line'].with_context(tracking_disable=True) + SaleOrderLine.create({ + 'name': product_A.name, + 'product_id': product_A.id, + 'product_uom_qty': 10, + 'price_unit': product_A.list_price, + 'order_id': sale_order.id, + }) + SaleOrderLine.create({ + 'name': product_B.name, + 'product_id': product_B.id, + 'product_uom_qty': 10, + 'price_unit': product_B.list_price, + 'order_id': sale_order.id, + }) + + def get_project_ids_from_action_domain(action): + for el in action['domain']: + if len(el) == 3 and el[0] == 'id' and el[1] == 'in': + domain_proj_ids = el[2] + break + else: + raise Exception(f"Couldn't find projects ids in the following action domain: {action['domain']}") + return domain_proj_ids + + # Check if button action includes both projects BEFORE archivization + action = sale_order.action_view_project_ids() + self.assertEqual(len(get_project_ids_from_action_domain(action)), 2, "Domain should contain 2 projects.") + + # Check if button action includes both projects AFTER archivization + project_B.write({'active': False}) + action = sale_order.action_view_project_ids() + self.assertEqual(len(get_project_ids_from_action_domain(action)), 2, "Domain should contain 2 projects. (one archived, one not)")