| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from odoo.addons.sale_timesheet.tests.common import TestCommonSaleTimesheet |
| 5 |
from odoo import Command |
| 6 |
from odoo.tests import tagged |
| 7 |
|
| 8 |
|
| 9 |
@tagged('-at_install', 'post_install') |
| 10 |
class TestSaleTimesheetMargin(TestCommonSaleTimesheet): |
| 11 |
|
| 12 |
def setUp(self): |
| 13 |
super().setUp() |
| 14 |
uom_day_id = self.ref('uom.product_uom_day') |
| 15 |
self.uom_day = self.env['uom.uom'].browse(uom_day_id) |
| 16 |
self.product_1 = self.env['product.product'].create({ |
| 17 |
'name': "Service Ordered, create no task, uom day", |
| 18 |
'list_price': 1.0, |
| 19 |
'type': 'service', |
| 20 |
'invoice_policy': 'order', |
| 21 |
'uom_id': uom_day_id, |
| 22 |
'default_code': 'SERV-ORDERED-DAY', |
| 23 |
'service_type': 'timesheet', |
| 24 |
'service_tracking': 'task_in_project', |
| 25 |
'project_id': False, |
| 26 |
'taxes_id': False, |
| 27 |
}) |
| 28 |
self.employee_manager.hourly_cost = 10 |
| 29 |
|
| 30 |
def test_sale_timesheet_margin(self): |
| 31 |
""" Test the timesheet cost is reported correctly in sale order line. """ |
| 32 |
sale_order = self.env['sale.order'].create({ |
| 33 |
'name': 'Test_SO0001', |
| 34 |
'order_line': [ |
| 35 |
Command.create({ |
| 36 |
'product_id': self.product_1.id, |
| 37 |
'price_unit': 1.0, |
| 38 |
'product_uom_id': self.uom_day.id, |
| 39 |
'product_uom_qty': 1.0, |
| 40 |
})], |
| 41 |
'partner_id': self.partner_b.id, |
| 42 |
'partner_invoice_id': self.partner_b.id, |
| 43 |
'partner_shipping_id': self.partner_b.id, |
| 44 |
}) |
| 45 |
|
| 46 |
sale_order.action_confirm() |
| 47 |
|
| 48 |
|
| 49 |
self.env['account.analytic.line'].create({ |
| 50 |
'name': 'Test Line', |
| 51 |
'unit_amount': 2, |
| 52 |
'employee_id': self.employee_manager.id, |
| 53 |
'project_id': sale_order.project_ids.id, |
| 54 |
'task_id': sale_order.order_line.task_id.id, |
| 55 |
'account_id': self.analytic_account_sale.id, |
| 56 |
'so_line': sale_order.order_line.id, |
| 57 |
}) |
| 58 |
sale_order.order_line._compute_purchase_price() |
| 59 |
|
| 60 |
expected_cost = self.uom_day._compute_quantity( |
| 61 |
self.employee_manager.hourly_cost, |
| 62 |
self.env.company.project_time_mode_id |
| 63 |
) |
| 64 |
self.assertEqual(sale_order.order_line.purchase_price, expected_cost, "Sale order line cost should be number of working hours on one day * timesheet cost of the employee set on the timesheet linked to the SOL.") |
| 65 |
|
| 66 |
def test_no_recompute_purchase_price_not_timesheet(self): |
| 67 |
""" |
| 68 |
check that if a sale order line is linked to a task but the service is ordered_prepaid, |
| 69 |
adding a timesheet line does not trigger a recomputation of purchase_price. |
| 70 |
We also check that the pruchase price of new sale order lines added after the confirmation |
| 71 |
is computed correctly. |
| 72 |
""" |
| 73 |
project = self.env['project.project'].create({ |
| 74 |
'name': "Test", |
| 75 |
}) |
| 76 |
self.product_1.write({ |
| 77 |
'uom_id': self.ref('uom.product_uom_unit'), |
| 78 |
'service_type': 'timesheet', |
| 79 |
'service_policy': 'ordered_prepaid', |
| 80 |
'service_tracking': 'task_global_project', |
| 81 |
'project_id': project.id, |
| 82 |
'standard_price': 2, |
| 83 |
}) |
| 84 |
simple_service = self.env['product.product'].create({ |
| 85 |
'name': "Simple service", |
| 86 |
'list_price': 1.0, |
| 87 |
'type': 'service', |
| 88 |
'invoice_policy': 'order', |
| 89 |
'service_type': 'timesheet', |
| 90 |
'standard_price': 5, |
| 91 |
}) |
| 92 |
sale_order = self.env['sale.order'].create({ |
| 93 |
'name': 'Test_SO0002', |
| 94 |
'order_line': [ |
| 95 |
Command.create({ |
| 96 |
'product_id': self.product_1.id, |
| 97 |
'price_unit': 1.0, |
| 98 |
'product_uom_qty': 1.0, |
| 99 |
})], |
| 100 |
'partner_id': self.partner_b.id, |
| 101 |
'partner_invoice_id': self.partner_b.id, |
| 102 |
'partner_shipping_id': self.partner_b.id, |
| 103 |
}) |
| 104 |
sale_order.order_line.purchase_price = 3 |
| 105 |
|
| 106 |
sale_order.action_confirm() |
| 107 |
|
| 108 |
self.env['account.analytic.line'].create({ |
| 109 |
'name': 'Test Line 222', |
| 110 |
'unit_amount': 2, |
| 111 |
'amount': 1, |
| 112 |
'employee_id': self.employee_manager.id, |
| 113 |
'project_id': project.id, |
| 114 |
'task_id': sale_order.order_line.task_id.id, |
| 115 |
'account_id': self.analytic_account_sale.id, |
| 116 |
'so_line': sale_order.order_line.id, |
| 117 |
}) |
| 118 |
self.env.flush_all() |
| 119 |
self.assertEqual(sale_order.order_line.filtered(lambda sol: sol.product_id == self.product_1).purchase_price, 3) |
| 120 |
sale_order.order_line = [Command.create({ |
| 121 |
'product_id': simple_service.id, |
| 122 |
'price_unit': 1.0, |
| 123 |
'product_uom_qty': 1.0, |
| 124 |
})] |
| 125 |
self.assertEqual(sale_order.order_line.filtered(lambda sol: sol.product_id == simple_service).purchase_price, 5) |