Debian Codegraph @

addons/sale_expense/tests/test_sale_expense.py

raw
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 tagged
6
7
8 @tagged('post_install', '-at_install')
9 class TestSaleExpense(TestExpenseCommon, TestSaleCommon):
10
11 def test_sale_expense(self):
12 """ Test the behaviour of sales orders when managing expenses """
13
14 # create a so with a product invoiced on delivery
15 so = self.env['sale.order'].create({
16 'partner_id': self.partner_a.id,
17 'partner_invoice_id': self.partner_a.id,
18 'partner_shipping_id': self.partner_a.id,
19 'order_line': [Command.create({
20 'name': self.company_data['product_delivery_no'].name,
21 'product_id': self.company_data['product_delivery_no'].id,
22 'product_uom_qty': 2,
23 'price_unit': self.company_data['product_delivery_no'].list_price,
24 })],
25 })
26 so.action_confirm()
27 analytic_account = self.env['account.analytic.account'].create(so._prepare_analytic_account_data())
28 init_price = so.amount_total
29
30 # create some expense and validate it (expense at cost)
31 expense = self.create_expenses({
32 'product_id': self.company_data['product_delivery_cost'].id,
33 'analytic_distribution': {analytic_account.id: 100},
34 'quantity': 11.30,
35 'sale_order_id': so.id,
36 })
37 expense.action_submit()
38 expense.action_approve()
39 self.post_expenses_with_wizard(expense)
40
41 # expense should now be in sales order
42 self.assertIn(self.company_data['product_delivery_cost'], so.mapped('order_line.product_id'), 'Sale Expense: expense product should be in so')
43 sol = so.order_line.filtered(lambda sol: sol.product_id.id == self.company_data['product_delivery_cost'].id)
44 self.assertEqual((sol.price_unit, sol.qty_delivered), (55.0, 11.3), 'Sale Expense: error when invoicing an expense at cost')
45 self.assertEqual(so.amount_total, init_price + expense.total_amount, 'Sale Expense: price of so should be updated after adding expense')
46 self.assertEqual(sol.analytic_distribution, {str(analytic_account.id): 100})
47
48 # create some expense and validate it (expense at sale price)
49 init_price = so.amount_total
50 prod_exp_2 = self.env['product.product'].create({
51 'name': 'Car Travel',
52 'expense_policy': 'sales_price',
53 'type': 'service',
54 'can_be_expensed': True,
55 'invoice_policy': 'delivery',
56 'list_price': 0.50,
57 'uom_id': self.env.ref('uom.product_uom_km').id,
58 'standard_price': 0.15,
59 })
60 expense_2 = self.env['hr.expense'].create({
61 'name': 'Car Travel',
62 'product_id': prod_exp_2.id,
63 'analytic_distribution': {analytic_account.id: 100},
64 'quantity': 100,
65 'employee_id': self.expense_employee.id,
66 'sale_order_id': so.id,
67 })
68 expense_2.action_submit()
69 expense_2.action_approve()
70 self.post_expenses_with_wizard(expense_2)
71
72 # expense should now be in sales order
73 self.assertIn(prod_exp_2, so.mapped('order_line.product_id'), 'Sale Expense: expense product should be in so')
74 sol = so.order_line.filtered(lambda sol: sol.product_id.id == prod_exp_2.id)
75 self.assertEqual((sol.price_unit, sol.qty_delivered), (prod_exp_2.list_price, 100.0), 'Sale Expense: error when invoicing an expense at cost')
76 self.assertEqual(so.amount_untaxed, init_price + (prod_exp_2.list_price * 100.0), 'Sale Expense: price of so should be updated after adding expense')
77
78 # both expenses should be invoiced
79 inv = so._create_invoices()
80 self.assertEqual(inv.amount_untaxed, 621.5 + (prod_exp_2.list_price * 100.0), 'Sale Expense: invoicing of expense is wrong')
81
82 def test_expense_multi_id_analytic_distribution(self):
83 """
84 Test conversion of analytic_distribution dict into account numbers when a hr.expense with an analytic_distribution
85 having 2+ account ids
86 """
87 expensed_product = self.env['product.product'].create({
88 'name': 'test product',
89 'can_be_expensed': True,
90 'type': 'service',
91 'invoice_policy': 'order',
92 'standard_price': 100,
93 'expense_policy': 'cost',
94 })
95
96 sale_order = self.env['sale.order'].create({
97 'partner_id': self.partner_a.id,
98 'partner_invoice_id': self.partner_a.id,
99 'partner_shipping_id': self.partner_a.id,
100 'order_line': [Command.create({'product_id': self.product_b.id})],
101 })
102 sale_order.action_confirm()
103 sale_order._create_invoices()
104
105 analytic_account_3 = self.env['account.analytic.account'].create({
106 'name': 'analytic_account_3',
107 'plan_id': self.analytic_plan.id,
108 })
109
110 expense = self.create_expenses({
111 'product_id': expensed_product.id,
112 'quantity': 1000.00,
113 'analytic_distribution': {
114 f'{self.analytic_account_1.id},{self.analytic_account_2.id}': 60,
115 f'{analytic_account_3.id}': 40,
116 },
117 'sale_order_id': sale_order.id,
118 })
119 expense.action_submit()
120 expense.action_approve()
121 self.post_expenses_with_wizard(expense)
122
123 self.assertTrue(self.env['account.move'].search([('expense_ids', '=', expense.id)], limit=1))