Debian Codegraph @

addons/sale_project/tests/test_analytic_distribution.py

raw
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 from .common import TestSaleProjectCommon
4 from odoo.fields import Command, Domain
5 from odoo.tests import HttpCase
6 from odoo.tests.common import tagged
7
8
9 @tagged('post_install', '-at_install')
10 class TestAnalyticDistribution(HttpCase, TestSaleProjectCommon):
11
12 @classmethod
13 def setUpClass(cls):
14 super().setUpClass()
15
16 # Creating analytic plans within tests could cause some registry issues
17 # hence we are creating them in the setupClass instead.
18 # This is because creating a plan creates fields and columns on models inheriting
19 # from the mixin.
20 # The registry is reset on class cleanup.
21 cls.plan_b = cls.env['account.analytic.plan'].create({'name': 'Q'})
22
23 def test_project_transmits_analytic_plans_to_sol_distribution(self):
24 plan_a = self.analytic_plan
25 plan_b = self.plan_b
26 account_a, account_b = self.env['account.analytic.account'].create([{
27 'name': 'account',
28 'plan_id': plan.id,
29 } for plan in (plan_a, plan_b)])
30 project = self.env['project.project'].create({
31 'name': 'X',
32 plan_a._column_name(): account_a.id,
33 plan_b._column_name(): account_b.id,
34 })
35
36 sale_order = self.env['sale.order'].create({
37 'partner_id': self.partner.id,
38 'project_id': project.id,
39 })
40 sale_order_line = self.env['sale.order.line'].create({
41 'order_id': sale_order.id,
42 'product_id': self.product.id,
43 })
44 self.assertEqual(
45 sale_order_line.analytic_distribution,
46 {f'{account_a.id},{account_b.id}': 100},
47 "The sale order line's analytic distribution should have one line containing all the accounts of the project's plans"
48 )
49
50 def test_sol_analytic_distribution_project_template_service(self):
51 sale_order = self.env['sale.order'].create({'partner_id': self.partner.id})
52 sale_order_line = self.env['sale.order.line'].create({
53 'order_id': sale_order.id,
54 'product_id': self.product_delivery_manual5.id,
55 })
56 self.assertFalse(
57 sale_order_line.analytic_distribution,
58 "No default analytic distribution should be set on the SOL as no project is linked to the SO, and we do not "
59 "take the project template set on the product into account.",
60 )
61 sale_order.action_confirm()
62 self.assertEqual(
63 sale_order_line.analytic_distribution,
64 {str(sale_order.project_id.account_id.id): 100},
65 "The analytic distribution of the SOL should be set to the account of the generated project.",
66 )
67
68 def test_sol_analytic_distribution_task_in_project_service(self):
69 self.project_global.account_id = self.analytic_account_sale
70 sale_order = self.env['sale.order'].create({'partner_id': self.partner.id})
71 sale_order_line = self.env['sale.order.line'].create({
72 'order_id': sale_order.id,
73 'product_id': self.product_delivery_manual2.id,
74 })
75 self.assertEqual(
76 sale_order_line.analytic_distribution,
77 {str(self.project_global.account_id.id): 100},
78 "The analytic distribution of the SOL should be set to the account of the project set on the product.",
79 )
80
81 def test_project_analytic_distribution_on_invoice_lines(self):
82 """
83 Test that Analytic Distribution applies from Project to Invoice Lines (excluding payable/receivable lines).
84 Steps:
85 1. Create a project.
86 2. Create an invoice with the project in context.
87 3. Add an invoice line.
88 4. Verify analytic distribution is applied.
89 """
90
91 invoice = self.env['account.move'].with_context({
92 'default_move_type': 'out_invoice',
93 'default_partner_id': self.project_global.partner_id.id,
94 'project_id': self.project_global.id
95 }).create({
96 'invoice_line_ids': [Command.create({
97 'product_id': self.product_delivery_manual1.id,
98 'quantity': 1,
99 'price_unit': 10,
100 })]
101 })
102
103 filtered_lines = invoice.line_ids.filtered(lambda l: l.analytic_distribution)
104 self.assertEqual(
105 len(filtered_lines),
106 1,
107 "Analytic distribution is not set on the payable/receivable lines"
108 )
109
110 def test_get_so_mapping_domain_with_no_analytic_distribution(self):
111 """
112 Ensure _get_so_mapping_domain doesnt fail when analytic_distribution is not set
113 """
114
115 account = self.env['account.account'].create({
116 'name': 'Receivable test account',
117 'code': '00001',
118 'account_type': 'asset_receivable',
119 })
120
121 move = self.env['account.move'].create({
122 'move_type': 'out_invoice',
123 'partner_id': self.partner.id,
124 })
125
126 line = self.env['account.move.line'].create({
127 'move_id': move.id,
128 'name': 'Line without analytic',
129 'quantity': 1,
130 'price_unit': 100,
131 'account_id': account.id,
132 })
133 domain = line._get_so_mapping_domain()
134
135 self.assertEqual(
136 domain,
137 Domain.FALSE,
138 "Domain should be False when analytic_distribution is missing."
139 )