19.0 vanilla

This commit is contained in:
Ernad Husremovic 2026-03-09 09:32:12 +01:00
parent 79f83631d5
commit 73afc09215
6267 changed files with 1534193 additions and 1130106 deletions

View file

@ -2,3 +2,4 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from . import test_project_profitability
from . import test_project_sale_expense

View file

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo.tests import tagged
@ -10,67 +9,109 @@ from odoo.addons.sale_project.tests.test_project_profitability import TestProjec
@tagged('-at_install', 'post_install')
class TestProjectSaleExpenseProfitability(TestProjectProfitabilityCommon, TestProjectHrExpenseProfitabilityCommon, TestSaleCommon):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.product_c.write({
'expense_policy': 'sales_price',
})
def test_project_profitability(self):
project = self.env['project.project'].create({'name': 'new project'})
project._create_analytic_account()
account = project.account_id
# Create a new company with the foreign currency.
foreign_company = self.company_data_2['company']
foreign_company.currency_id = self.foreign_currency
foreign_partner = self.env['res.partner'].create({
'name': 'Foreign Employee address',
'company_id': foreign_company.id,
})
foreign_employee = self.env['hr.employee'].sudo().create({
'name': 'foreign_employee',
'company_id': foreign_company.id,
'expense_manager_id': self.expense_user_manager.id,
'work_contact_id': foreign_partner.id,
'work_email': 'email@email',
})
expense = self.env['hr.expense'].create({
'name': 'expense',
'product_id': self.company_data['product_order_sales_price'].id,
'unit_amount': self.company_data['product_order_sales_price'].list_price,
'total_amount_currency': self.company_data['product_order_sales_price'].list_price,
'employee_id': self.expense_employee.id,
'analytic_distribution': {self.project.analytic_account_id.id: 100},
'analytic_distribution': {account.id: 100},
'sale_order_id': self.sale_order.id,
})
# See method definition in `project_hr_expense.tests.test_project_profitability`
expense_sheet = self.check_project_profitability_before_creating_and_approving_expense_sheet(
expense = self.check_project_profitability_before_creating_and_approving_expense(
expense,
self.project,
project,
self.project_profitability_items_empty)
self.assertEqual(expense.state, 'approved')
expense_profitability = self.project._get_expenses_profitability_items(False)
sequence_per_invoice_type = self.project._get_profitability_sequence_per_invoice_type()
# Create an expense in a foreign company, the expense is linked to the AA of the project.
so_foreign = self.env['sale.order'].create({
'name': 'Sale order foreign',
'partner_id': self.partner_a.id,
'company_id': foreign_company.id,
})
so_foreign.currency_id = self.foreign_currency
so_foreign.action_confirm()
expense_foreign = self.create_expenses({
'name': 'Expense foreign',
'employee_id': foreign_employee.id,
'product_id': self.product_c.id, # Foreign currency product must have no cost
'total_amount_currency': 350.00 * 0.5, # 0.5 is the exchange rate
'company_id': foreign_company.id,
'analytic_distribution': {account.id: 100},
'currency_id': self.foreign_currency.id,
'sale_order_id': so_foreign.id,
})
expense_foreign.action_submit()
self.assertEqual(expense_foreign.state, 'submitted')
expense_foreign.action_approve()
self.assertEqual(expense_foreign.state, 'approved')
expense_profitability = project._get_expenses_profitability_items(False)
sequence_per_invoice_type = project._get_profitability_sequence_per_invoice_type()
self.assertIn('expenses', sequence_per_invoice_type)
expense_sequence = sequence_per_invoice_type['expenses']
billed = -expense.untaxed_amount_currency - expense_foreign.untaxed_amount_currency * 0.2 # -280.0 - 175.0 * 0.2 = -315.0
self.assertDictEqual(
expense_profitability.get('revenues', {}),
{},
)
self.assertDictEqual(
expense_profitability['costs'],
{'id': 'expenses', 'sequence': expense_sequence, 'billed': -280.0, 'to_bill': 0.0},
self.assertNotIn(
'costs',
expense_profitability,
'No costs should be found since the sheets are not posted or done.',
)
expense_sheet.action_sheet_move_create()
self.post_expenses_with_wizard(expense)
self.assertEqual(expense.state, 'posted')
self.assertRecordValues(self.sale_order.order_line, [
# Original SO line:
{
'product_id': self.product_delivery_service.id,
'qty_delivered': 0.0,
'product_uom_qty': 10,
'is_expense': False,
},
{
'product_id': self.company_data['product_order_sales_price'].id,
'qty_delivered': 1.0,
'product_uom_qty': 1.0,
'is_expense': True,
},
{'is_expense': False, 'product_uom_qty': 10.0, 'qty_delivered': 0.0, 'product_id': self.product_delivery_service.id},
{'is_expense': True, 'product_uom_qty': 1.0, 'qty_delivered': 1.0, 'product_id': self.company_data['product_order_sales_price'].id},
])
expense_sol = self.sale_order.order_line.filtered(lambda sol: sol.product_id == self.company_data['product_order_sales_price'])
expense_profitability = self.project._get_expenses_profitability_items(False)
expense_profitability = project._get_expenses_profitability_items(False)
self.assertDictEqual(
expense_profitability.get('revenues', {}),
{'id': 'expenses', 'sequence': expense_sequence, 'invoiced': 0.0, 'to_invoice': expense_sol.untaxed_amount_to_invoice},
)
self.assertDictEqual(
expense_profitability['costs'],
{'id': 'expenses', 'sequence': expense_sequence, 'billed': -280.0, 'to_bill': 0.0},
{'id': 'expenses', 'sequence': expense_sequence, 'billed': expense.currency_id.round(-expense.untaxed_amount_currency), 'to_bill': 0.0},
)
self.assertDictEqual(
self.project._get_profitability_items(False),
project._get_profitability_items(False),
{
'revenues': {
'data': [expense_profitability['revenues']],
@ -83,6 +124,32 @@ class TestProjectSaleExpenseProfitability(TestProjectProfitabilityCommon, TestPr
}
)
self.post_expenses_with_wizard(expense_foreign.with_company(expense_foreign.company_id))
self.assertEqual(expense_foreign.state, 'posted')
expense_sol_foreign = so_foreign.order_line[0]
expense_profitability = project._get_expenses_profitability_items(False)
self.assertDictEqual(
expense_profitability.get('revenues', {}),
{'id': 'expenses', 'sequence': expense_sequence, 'invoiced': 0.0, 'to_invoice': expense_sol.untaxed_amount_to_invoice + expense_sol_foreign.untaxed_amount_to_invoice * 0.2},
)
self.assertDictEqual(
expense_profitability['costs'],
{'id': 'expenses', 'sequence': expense_sequence, 'billed': expense.currency_id.round(billed), 'to_bill': 0.0},
)
self.assertDictEqual(
project._get_profitability_items(False),
{
'revenues': {
'data': [expense_profitability['revenues']],
'total': {k: v for k, v in expense_profitability['revenues'].items() if k in ['to_invoice', 'invoiced']},
},
'costs': {
'data': [expense_profitability['costs']],
'total': {k: v for k, v in expense_profitability['costs'].items() if k in ['to_bill', 'billed']},
},
}
)
invoice = self.env['sale.advance.payment.inv'] \
.with_context({
'active_model': 'sale.order',
@ -92,34 +159,80 @@ class TestProjectSaleExpenseProfitability(TestProjectProfitabilityCommon, TestPr
})._create_invoices(self.sale_order)
invoice.action_post()
expense_profitability = self.project._get_expenses_profitability_items(False)
expense_profitability = project._get_expenses_profitability_items(False)
self.assertDictEqual(
expense_profitability.get('revenues', {}),
{'id': 'expenses', 'sequence': expense_sequence, 'invoiced': expense_sol.untaxed_amount_invoiced, 'to_invoice': 0.0},
{'id': 'expenses', 'sequence': expense_sequence, 'invoiced': expense_sol.untaxed_amount_invoiced, 'to_invoice': expense_sol_foreign.untaxed_amount_to_invoice * 0.2},
)
credit_note = invoice._reverse_moves()
credit_note.action_post()
expense_profitability = self.project._get_expenses_profitability_items(False)
expense_profitability = project._get_expenses_profitability_items(False)
self.assertDictEqual(
expense_profitability.get('revenues', {}),
{'id': 'expenses', 'sequence': expense_sequence, 'invoiced': 0.0, 'to_invoice': expense_sol.untaxed_amount_to_invoice},
{'id': 'expenses', 'sequence': expense_sequence, 'invoiced': 0.0, 'to_invoice': expense_sol.untaxed_amount_to_invoice + expense_sol_foreign.untaxed_amount_to_invoice * 0.2},
)
self.sale_order._action_cancel()
expense_profitability = self.project._get_expenses_profitability_items(False)
expense_profitability = project._get_expenses_profitability_items(False)
self.assertDictEqual(
expense_profitability.get('revenues', {}),
{'id': 'expenses', 'sequence': expense_sequence, 'invoiced': 0.0, 'to_invoice': expense_sol_foreign.untaxed_amount_to_invoice * 0.2},
)
self.assertDictEqual(
expense_profitability['costs'],
{'id': 'expenses', 'sequence': expense_sequence, 'billed': expense.currency_id.round(billed), 'to_bill': 0.0},
)
expense.account_move_id.button_draft()
expense.account_move_id.unlink()
expense_profitability = project._get_expenses_profitability_items(False)
self.assertDictEqual(
expense_profitability.get('revenues', {}),
{'id': 'expenses', 'sequence': expense_sequence, 'invoiced': 0.0, 'to_invoice': expense_sol_foreign.untaxed_amount_to_invoice * 0.2},
)
self.assertDictEqual(
expense_profitability.get('costs', {}),
{'id': 'expenses', 'sequence': expense_sequence, 'billed': expense.currency_id.round(-expense_foreign.untaxed_amount_currency * 0.2), 'to_bill': 0.0},
)
invoice = self.env['sale.advance.payment.inv'].with_context({
'active_model': 'sale.order',
'active_id': so_foreign.id,
}).create({
'advance_payment_method': 'delivered',
})._create_invoices(so_foreign)
invoice.action_post()
expense_profitability = project._get_expenses_profitability_items(False)
self.assertDictEqual(
expense_profitability.get('revenues', {}),
{'id': 'expenses', 'sequence': expense_sequence, 'invoiced': expense_sol_foreign.untaxed_amount_invoiced * 0.2, 'to_invoice': 0.0},
)
credit_note = invoice._reverse_moves()
credit_note.action_post()
expense_profitability = project._get_expenses_profitability_items(False)
self.assertDictEqual(
expense_profitability.get('revenues', {}),
{'id': 'expenses', 'sequence': expense_sequence, 'invoiced': 0.0, 'to_invoice': expense_sol_foreign.untaxed_amount_to_invoice * 0.2},
)
so_foreign._action_cancel()
expense_profitability = project._get_expenses_profitability_items(False)
self.assertDictEqual(
expense_profitability.get('revenues', {}),
{},
)
self.assertDictEqual(
expense_profitability['costs'],
{'id': 'expenses', 'sequence': expense_sequence, 'billed': -280.0, 'to_bill': 0.0},
{'id': 'expenses', 'sequence': expense_sequence, 'billed': expense.currency_id.round(-expense_foreign.untaxed_amount_currency * 0.2), 'to_bill': 0.0},
)
expense_sheet.refuse_sheet('Test Cancel Expense')
expense_profitability = self.project._get_expenses_profitability_items(False)
expense_foreign.account_move_id.button_draft()
expense_foreign.account_move_id.unlink()
expense_profitability = project._get_expenses_profitability_items(False)
self.assertDictEqual(
expense_profitability.get('revenues', {}),
{},
@ -165,20 +278,16 @@ class TestProjectSaleExpenseProfitability(TestProjectProfitabilityCommon, TestPr
sale_order.action_confirm()
project = sale_order.order_line.project_id
expense = self.env['hr.expense'].create({
expense = self.create_expenses({
'name': 'expense',
'product_id': self.company_data['product_order_cost'].id,
'unit_amount': self.company_data['product_order_cost'].list_price,
'employee_id': self.expense_employee.id,
'analytic_distribution': {project.analytic_account_id.id: 100},
'quantity': 1,
'analytic_distribution': {project.account_id.id: 100},
'sale_order_id': sale_order.id,
})
expense_sheet_vals_list = expense._get_default_expense_sheet_values()
expense_sheet = self.env['hr.expense.sheet'].create(expense_sheet_vals_list)
expense_sheet.action_submit_sheet()
expense_sheet.approve_expense_sheets()
expense_sheet.action_sheet_move_create()
expense.action_submit()
expense.action_approve()
self.post_expenses_with_wizard(expense)
invoice = sale_order._create_invoices()
invoice.action_post()
@ -204,28 +313,3 @@ class TestProjectSaleExpenseProfitability(TestProjectProfitabilityCommon, TestPr
'total': {'invoiced': expense_profitability['revenues']['invoiced'] + revenue_items_from_sol['total']['invoiced'], 'to_invoice': expense_profitability['revenues']['to_invoice'] + revenue_items_from_sol['total']['to_invoice']},
},
)
def test_project_profitability_multi_currency(self):
currency_rate = 0.5
other_currency = self.env['res.currency'].create({
'name': 'TEST',
'symbol': 'T',
'rate_ids': [(0, 0, {
'name': '2020-01-01',
'rate': currency_rate,
})],
})
amount_in_other_currency = 100
expense = self.env['hr.expense'].create({
'name': 'Expense in another currency',
'product_id': self.company_data['product_order_sales_price'].id,
'total_amount': amount_in_other_currency,
'employee_id': self.expense_employee.id,
'analytic_distribution': {self.project.analytic_account_id.id: 100},
'sale_order_id': self.sale_order.id,
'currency_id': other_currency.id,
})
self.check_project_profitability_before_creating_and_approving_expense_sheet(expense, self.project, self.project_profitability_items_empty)
expense_profitability = self.project._get_expenses_profitability_items(False)
self.assertTrue(expense_profitability['costs']['billed'] == - amount_in_other_currency / currency_rate)

View file

@ -0,0 +1,153 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import Command
from odoo.addons.hr_expense.tests.common import TestExpenseCommon
from odoo.addons.sale.tests.common import TestSaleCommon
from odoo.tests import Form, tagged
@tagged('post_install', '-at_install')
class TestSaleExpense(TestExpenseCommon, TestSaleCommon):
def test_analytic_account_expense_policy(self):
product_form = Form(self.product_a.product_tmpl_id)
product_form.can_be_expensed = True
product_form.expense_policy = 'cost'
product_form.can_be_expensed = False
self.product_a.product_tmpl_id = product_form.save()
project = self.env['project.project'].sudo().create({'name': 'SO Project'})
# Remove the analytic account auto-generated when creating a timesheetable project if it exists
project.account_id = False
so = self.env['sale.order'].create({
'partner_id': self.partner_a.id,
'order_line': [Command.create({
'name': self.product_a.name,
'product_id': self.product_a.id,
'product_uom_qty': 2,
'price_unit': self.product_a.list_price,
})],
'project_id': project.id,
})
so.action_confirm()
self.assertFalse(so.project_account_id)
def test_compute_analytic_distribution_expense(self):
""" Test that the analytic distibution is well computed when we link a sale order to an expense """
# Make sure the user has access to analytic accounting, otherwise the 'analytic_distribution' field will not appear
# in the view and will not be computed
self.env.user.write({'group_ids': [Command.link(self.env.ref('analytic.group_analytic_accounting').id)]})
# Set the expense policy to 'sales_price' to make the 'sale_order_id' field visible on the form view
self.product_c.expense_policy = 'sales_price'
self.analytic_plan_2 = self.env['account.analytic.plan'].create({'name': 'Other Plan Test'})
self.analytic_account_3 = self.env['account.analytic.account'].create({
'name': 'analytic_account_3',
'plan_id': self.analytic_plan_2.id,
})
# Project Will use another analytic plan than the product
project = self.env['project.project'].sudo().create({'name': 'SO Project'})
project.account_id = self.analytic_account_3
# Set an analytic distribution using account_1 on the product that will be used on the expense
self.env['account.analytic.distribution.model'].create([{
'product_id': self.product_c.id,
'analytic_distribution': {str(self.analytic_account_1.id): 100}
}])
so_values = {
'partner_id': self.partner_a.id,
'order_line': [Command.create({
'name': self.product_c.name,
'product_id': self.product_c.id,
'product_uom_qty': 2,
'price_unit': self.product_c.list_price,
})],
'project_id': project.id,
}
so1 = self.env['sale.order'].create(so_values)
expense = self.create_expenses({
'name': 'Expense Test',
'sale_order_id': so1.id,
'product_id': self.product_c.id,
})
self.assertEqual(
expense.analytic_distribution,
{str(self.analytic_account_1.id): 100, str(self.analytic_account_3.id): 100},
"The analytic distribution of the expense should be set to the account of the project and the one from the sale order.",
)
# Check that it default to the one from the sale order if the project has no analytic distribution
project.account_id = False
so2 = self.env['sale.order'].create(so_values)
# We use the form to trigger the onchange on sale_order_id, which adds the 'analytic_distribution' field to the fields to recompute
with Form(expense) as exp_form:
exp_form.sale_order_id = so2
self.assertEqual(
expense.analytic_distribution,
{str(self.analytic_account_1.id): 100},
"The analytic distribution of the expense should be the one from the sale order only",
)
# The analytic_account_2 has the same plan as the one from the sale order
project.account_id = self.analytic_account_2
so3 = self.env['sale.order'].create(so_values)
with Form(expense) as exp_form:
exp_form.sale_order_id = so3
self.assertEqual(
expense.analytic_distribution,
{str(self.analytic_account_2.id): 100},
"The analytic distribution of the expense should keep only the one from the project when the so and project share the same plan",
)
def test_change_product_expense_policy_analytic_distribution(self):
""" Test that analytic distribution is not recomputed when changing the expense policy of the expense product """
analytic_account_2 = self.analytic_account_1.copy()
self.product_a.expense_policy = 'sales_price'
distribution_model = self.env['account.analytic.distribution.model'].create({
'account_prefix': self.company_data['default_account_expense'].code,
'analytic_distribution': {self.analytic_account_1.id: 100.0},
})
expenses = self.env['hr.expense'].create([
{
'name': f'Expense {i}',
'employee_id': self.expense_employee.id,
'product_id': self.product_a.id,
} for i in range(1, 3)
])
self.assertRecordValues(expenses, [
{
'account_id': self.company_data['default_account_expense'].id,
'analytic_distribution': {str(self.analytic_account_1.id): 100.0},
},
{
'account_id': self.company_data['default_account_expense'].id,
'analytic_distribution': {str(self.analytic_account_1.id): 100.0},
},
])
distribution_model.analytic_distribution = {analytic_account_2.id: 100.0}
expenses |= self.env['hr.expense'].create({
'name': 'Expense 3',
'employee_id': self.expense_employee.id,
'product_id': self.product_a.id,
})
self.product_a.expense_policy = 'cost'
self.assertRecordValues(expenses, [
{
'analytic_distribution': {str(self.analytic_account_1.id): 100.0},
},
{
'analytic_distribution': {str(self.analytic_account_1.id): 100.0},
},
{
'analytic_distribution': {str(analytic_account_2.id): 100.0},
},
])