| 1 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 2 |
from odoo import Command |
| 3 |
from odoo.addons.hr_expense.tests.common import TestExpenseCommon |
| 4 |
from odoo.addons.sale.tests.common import TestSaleCommon |
| 5 |
from odoo.tests import Form, tagged |
| 6 |
|
| 7 |
|
| 8 |
@tagged('post_install', '-at_install') |
| 9 |
class TestSaleExpense(TestExpenseCommon, TestSaleCommon): |
| 10 |
|
| 11 |
def test_analytic_account_expense_policy(self): |
| 12 |
product_form = Form(self.product_a.product_tmpl_id) |
| 13 |
product_form.can_be_expensed = True |
| 14 |
product_form.expense_policy = 'cost' |
| 15 |
product_form.can_be_expensed = False |
| 16 |
self.product_a.product_tmpl_id = product_form.save() |
| 17 |
|
| 18 |
project = self.env['project.project'].sudo().create({'name': 'SO Project'}) |
| 19 |
# Remove the analytic account auto-generated when creating a timesheetable project if it exists |
| 20 |
project.account_id = False |
| 21 |
|
| 22 |
so = self.env['sale.order'].create({ |
| 23 |
'partner_id': self.partner_a.id, |
| 24 |
'order_line': [Command.create({ |
| 25 |
'name': self.product_a.name, |
| 26 |
'product_id': self.product_a.id, |
| 27 |
'product_uom_qty': 2, |
| 28 |
'price_unit': self.product_a.list_price, |
| 29 |
})], |
| 30 |
'project_id': project.id, |
| 31 |
}) |
| 32 |
so.action_confirm() |
| 33 |
self.assertFalse(so.project_account_id) |
| 34 |
|
| 35 |
def test_compute_analytic_distribution_expense(self): |
| 36 |
""" Test that the analytic distibution is well computed when we link a sale order to an expense """ |
| 37 |
|
| 38 |
# Make sure the user has access to analytic accounting, otherwise the 'analytic_distribution' field will not appear |
| 39 |
# in the view and will not be computed |
| 40 |
self.env.user.write({'group_ids': [Command.link(self.env.ref('analytic.group_analytic_accounting').id)]}) |
| 41 |
# Set the expense policy to 'sales_price' to make the 'sale_order_id' field visible on the form view |
| 42 |
self.product_c.expense_policy = 'sales_price' |
| 43 |
|
| 44 |
self.analytic_plan_2 = self.env['account.analytic.plan'].create({'name': 'Other Plan Test'}) |
| 45 |
self.analytic_account_3 = self.env['account.analytic.account'].create({ |
| 46 |
'name': 'analytic_account_3', |
| 47 |
'plan_id': self.analytic_plan_2.id, |
| 48 |
}) |
| 49 |
|
| 50 |
# Project Will use another analytic plan than the product |
| 51 |
project = self.env['project.project'].sudo().create({'name': 'SO Project'}) |
| 52 |
project.account_id = self.analytic_account_3 |
| 53 |
|
| 54 |
# Set an analytic distribution using account_1 on the product that will be used on the expense |
| 55 |
self.env['account.analytic.distribution.model'].create([{ |
| 56 |
'product_id': self.product_c.id, |
| 57 |
'analytic_distribution': {str(self.analytic_account_1.id): 100} |
| 58 |
}]) |
| 59 |
|
| 60 |
so_values = { |
| 61 |
'partner_id': self.partner_a.id, |
| 62 |
'order_line': [Command.create({ |
| 63 |
'name': self.product_c.name, |
| 64 |
'product_id': self.product_c.id, |
| 65 |
'product_uom_qty': 2, |
| 66 |
'price_unit': self.product_c.list_price, |
| 67 |
})], |
| 68 |
'project_id': project.id, |
| 69 |
} |
| 70 |
so1 = self.env['sale.order'].create(so_values) |
| 71 |
expense = self.create_expenses({ |
| 72 |
'name': 'Expense Test', |
| 73 |
'sale_order_id': so1.id, |
| 74 |
'product_id': self.product_c.id, |
| 75 |
}) |
| 76 |
|
| 77 |
self.assertEqual( |
| 78 |
expense.analytic_distribution, |
| 79 |
{str(self.analytic_account_1.id): 100, str(self.analytic_account_3.id): 100}, |
| 80 |
"The analytic distribution of the expense should be set to the account of the project and the one from the sale order.", |
| 81 |
) |
| 82 |
|
| 83 |
# Check that it default to the one from the sale order if the project has no analytic distribution |
| 84 |
project.account_id = False |
| 85 |
so2 = self.env['sale.order'].create(so_values) |
| 86 |
|
| 87 |
# We use the form to trigger the onchange on sale_order_id, which adds the 'analytic_distribution' field to the fields to recompute |
| 88 |
with Form(expense) as exp_form: |
| 89 |
exp_form.sale_order_id = so2 |
| 90 |
|
| 91 |
self.assertEqual( |
| 92 |
expense.analytic_distribution, |
| 93 |
{str(self.analytic_account_1.id): 100}, |
| 94 |
"The analytic distribution of the expense should be the one from the sale order only", |
| 95 |
) |
| 96 |
|
| 97 |
# The analytic_account_2 has the same plan as the one from the sale order |
| 98 |
project.account_id = self.analytic_account_2 |
| 99 |
so3 = self.env['sale.order'].create(so_values) |
| 100 |
with Form(expense) as exp_form: |
| 101 |
exp_form.sale_order_id = so3 |
| 102 |
self.assertEqual( |
| 103 |
expense.analytic_distribution, |
| 104 |
{str(self.analytic_account_2.id): 100}, |
| 105 |
"The analytic distribution of the expense should keep only the one from the project when the so and project share the same plan", |
| 106 |
) |
| 107 |
|
| 108 |
def test_change_product_expense_policy_analytic_distribution(self): |
| 109 |
""" Test that analytic distribution is not recomputed when changing the expense policy of the expense product """ |
| 110 |
analytic_account_2 = self.analytic_account_1.copy() |
| 111 |
self.product_a.expense_policy = 'sales_price' |
| 112 |
distribution_model = self.env['account.analytic.distribution.model'].create({ |
| 113 |
'account_prefix': self.company_data['default_account_expense'].code, |
| 114 |
'analytic_distribution': {self.analytic_account_1.id: 100.0}, |
| 115 |
}) |
| 116 |
expenses = self.env['hr.expense'].create([ |
| 117 |
{ |
| 118 |
'name': f'Expense {i}', |
| 119 |
'employee_id': self.expense_employee.id, |
| 120 |
'product_id': self.product_a.id, |
| 121 |
} for i in range(1, 3) |
| 122 |
]) |
| 123 |
self.assertRecordValues(expenses, [ |
| 124 |
{ |
| 125 |
'account_id': self.company_data['default_account_expense'].id, |
| 126 |
'analytic_distribution': {str(self.analytic_account_1.id): 100.0}, |
| 127 |
}, |
| 128 |
{ |
| 129 |
'account_id': self.company_data['default_account_expense'].id, |
| 130 |
'analytic_distribution': {str(self.analytic_account_1.id): 100.0}, |
| 131 |
}, |
| 132 |
]) |
| 133 |
|
| 134 |
distribution_model.analytic_distribution = {analytic_account_2.id: 100.0} |
| 135 |
expenses |= self.env['hr.expense'].create({ |
| 136 |
'name': 'Expense 3', |
| 137 |
'employee_id': self.expense_employee.id, |
| 138 |
'product_id': self.product_a.id, |
| 139 |
}) |
| 140 |
|
| 141 |
self.product_a.expense_policy = 'cost' |
| 142 |
|
| 143 |
self.assertRecordValues(expenses, [ |
| 144 |
{ |
| 145 |
'analytic_distribution': {str(self.analytic_account_1.id): 100.0}, |
| 146 |
}, |
| 147 |
{ |
| 148 |
'analytic_distribution': {str(self.analytic_account_1.id): 100.0}, |
| 149 |
}, |
| 150 |
{ |
| 151 |
'analytic_distribution': {str(analytic_account_2.id): 100.0}, |
| 152 |
}, |
| 153 |
]) |