| 1 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 2 |
|
| 3 |
from odoo.tests import tagged |
| 4 |
|
| 5 |
from odoo.addons.project_hr_expense.tests.test_project_profitability import TestProjectHrExpenseProfitabilityCommon |
| 6 |
from odoo.addons.sale.tests.common import TestSaleCommon |
| 7 |
from odoo.addons.sale_project.tests.test_project_profitability import TestProjectProfitabilityCommon |
| 8 |
|
| 9 |
|
| 10 |
@tagged('-at_install', 'post_install') |
| 11 |
class TestProjectSaleExpenseProfitability(TestProjectProfitabilityCommon, TestProjectHrExpenseProfitabilityCommon, TestSaleCommon): |
| 12 |
|
| 13 |
@classmethod |
| 14 |
def setUpClass(cls): |
| 15 |
super().setUpClass() |
| 16 |
cls.product_c.write({ |
| 17 |
'expense_policy': 'sales_price', |
| 18 |
}) |
| 19 |
|
| 20 |
def test_project_profitability(self): |
| 21 |
project = self.env['project.project'].create({'name': 'new project'}) |
| 22 |
project._create_analytic_account() |
| 23 |
account = project.account_id |
| 24 |
# Create a new company with the foreign currency. |
| 25 |
foreign_company = self.company_data_2['company'] |
| 26 |
foreign_company.currency_id = self.foreign_currency |
| 27 |
foreign_partner = self.env['res.partner'].create({ |
| 28 |
'name': 'Foreign Employee address', |
| 29 |
'company_id': foreign_company.id, |
| 30 |
}) |
| 31 |
foreign_employee = self.env['hr.employee'].sudo().create({ |
| 32 |
'name': 'foreign_employee', |
| 33 |
'company_id': foreign_company.id, |
| 34 |
'expense_manager_id': self.expense_user_manager.id, |
| 35 |
'work_contact_id': foreign_partner.id, |
| 36 |
'work_email': 'email@email', |
| 37 |
}) |
| 38 |
|
| 39 |
expense = self.env['hr.expense'].create({ |
| 40 |
'name': 'expense', |
| 41 |
'product_id': self.company_data['product_order_sales_price'].id, |
| 42 |
'total_amount_currency': self.company_data['product_order_sales_price'].list_price, |
| 43 |
'employee_id': self.expense_employee.id, |
| 44 |
'analytic_distribution': {account.id: 100}, |
| 45 |
'sale_order_id': self.sale_order.id, |
| 46 |
}) |
| 47 |
|
| 48 |
# See method definition in `project_hr_expense.tests.test_project_profitability` |
| 49 |
expense = self.check_project_profitability_before_creating_and_approving_expense( |
| 50 |
expense, |
| 51 |
project, |
| 52 |
self.project_profitability_items_empty) |
| 53 |
self.assertEqual(expense.state, 'approved') |
| 54 |
|
| 55 |
# Create an expense in a foreign company, the expense is linked to the AA of the project. |
| 56 |
so_foreign = self.env['sale.order'].create({ |
| 57 |
'name': 'Sale order foreign', |
| 58 |
'partner_id': self.partner_a.id, |
| 59 |
'company_id': foreign_company.id, |
| 60 |
}) |
| 61 |
so_foreign.currency_id = self.foreign_currency |
| 62 |
so_foreign.action_confirm() |
| 63 |
expense_foreign = self.create_expenses({ |
| 64 |
'name': 'Expense foreign', |
| 65 |
'employee_id': foreign_employee.id, |
| 66 |
'product_id': self.product_c.id, # Foreign currency product must have no cost |
| 67 |
'total_amount_currency': 350.00 * 0.5, # 0.5 is the exchange rate |
| 68 |
'company_id': foreign_company.id, |
| 69 |
'analytic_distribution': {account.id: 100}, |
| 70 |
'currency_id': self.foreign_currency.id, |
| 71 |
'sale_order_id': so_foreign.id, |
| 72 |
}) |
| 73 |
expense_foreign.action_submit() |
| 74 |
self.assertEqual(expense_foreign.state, 'submitted') |
| 75 |
expense_foreign.action_approve() |
| 76 |
self.assertEqual(expense_foreign.state, 'approved') |
| 77 |
|
| 78 |
expense_profitability = project._get_expenses_profitability_items(False) |
| 79 |
sequence_per_invoice_type = project._get_profitability_sequence_per_invoice_type() |
| 80 |
self.assertIn('expenses', sequence_per_invoice_type) |
| 81 |
expense_sequence = sequence_per_invoice_type['expenses'] |
| 82 |
billed = -expense.untaxed_amount_currency - expense_foreign.untaxed_amount_currency * 0.2 # -280.0 - 175.0 * 0.2 = -315.0 |
| 83 |
|
| 84 |
self.assertDictEqual( |
| 85 |
expense_profitability.get('revenues', {}), |
| 86 |
{}, |
| 87 |
) |
| 88 |
self.assertNotIn( |
| 89 |
'costs', |
| 90 |
expense_profitability, |
| 91 |
'No costs should be found since the sheets are not posted or done.', |
| 92 |
) |
| 93 |
|
| 94 |
self.post_expenses_with_wizard(expense) |
| 95 |
self.assertEqual(expense.state, 'posted') |
| 96 |
self.assertRecordValues(self.sale_order.order_line, [ |
| 97 |
# Original SO line: |
| 98 |
{'is_expense': False, 'product_uom_qty': 10.0, 'qty_delivered': 0.0, 'product_id': self.product_delivery_service.id}, |
| 99 |
{'is_expense': True, 'product_uom_qty': 1.0, 'qty_delivered': 1.0, 'product_id': self.company_data['product_order_sales_price'].id}, |
| 100 |
]) |
| 101 |
expense_sol = self.sale_order.order_line.filtered(lambda sol: sol.product_id == self.company_data['product_order_sales_price']) |
| 102 |
|
| 103 |
expense_profitability = project._get_expenses_profitability_items(False) |
| 104 |
self.assertDictEqual( |
| 105 |
expense_profitability.get('revenues', {}), |
| 106 |
{'id': 'expenses', 'sequence': expense_sequence, 'invoiced': 0.0, 'to_invoice': expense_sol.untaxed_amount_to_invoice}, |
| 107 |
) |
| 108 |
self.assertDictEqual( |
| 109 |
expense_profitability['costs'], |
| 110 |
{'id': 'expenses', 'sequence': expense_sequence, 'billed': expense.currency_id.round(-expense.untaxed_amount_currency), 'to_bill': 0.0}, |
| 111 |
) |
| 112 |
|
| 113 |
self.assertDictEqual( |
| 114 |
project._get_profitability_items(False), |
| 115 |
{ |
| 116 |
'revenues': { |
| 117 |
'data': [expense_profitability['revenues']], |
| 118 |
'total': {k: v for k, v in expense_profitability['revenues'].items() if k in ['to_invoice', 'invoiced']}, |
| 119 |
}, |
| 120 |
'costs': { |
| 121 |
'data': [expense_profitability['costs']], |
| 122 |
'total': {k: v for k, v in expense_profitability['costs'].items() if k in ['to_bill', 'billed']}, |
| 123 |
}, |
| 124 |
} |
| 125 |
) |
| 126 |
|
| 127 |
self.post_expenses_with_wizard(expense_foreign.with_company(expense_foreign.company_id)) |
| 128 |
self.assertEqual(expense_foreign.state, 'posted') |
| 129 |
expense_sol_foreign = so_foreign.order_line[0] |
| 130 |
expense_profitability = project._get_expenses_profitability_items(False) |
| 131 |
self.assertDictEqual( |
| 132 |
expense_profitability.get('revenues', {}), |
| 133 |
{'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}, |
| 134 |
) |
| 135 |
self.assertDictEqual( |
| 136 |
expense_profitability['costs'], |
| 137 |
{'id': 'expenses', 'sequence': expense_sequence, 'billed': expense.currency_id.round(billed), 'to_bill': 0.0}, |
| 138 |
) |
| 139 |
|
| 140 |
self.assertDictEqual( |
| 141 |
project._get_profitability_items(False), |
| 142 |
{ |
| 143 |
'revenues': { |
| 144 |
'data': [expense_profitability['revenues']], |
| 145 |
'total': {k: v for k, v in expense_profitability['revenues'].items() if k in ['to_invoice', 'invoiced']}, |
| 146 |
}, |
| 147 |
'costs': { |
| 148 |
'data': [expense_profitability['costs']], |
| 149 |
'total': {k: v for k, v in expense_profitability['costs'].items() if k in ['to_bill', 'billed']}, |
| 150 |
}, |
| 151 |
} |
| 152 |
) |
| 153 |
invoice = self.env['sale.advance.payment.inv'] \ |
| 154 |
.with_context({ |
| 155 |
'active_model': 'sale.order', |
| 156 |
'active_id': self.sale_order.id, |
| 157 |
}).create({ |
| 158 |
'advance_payment_method': 'delivered', |
| 159 |
})._create_invoices(self.sale_order) |
| 160 |
invoice.action_post() |
| 161 |
|
| 162 |
expense_profitability = project._get_expenses_profitability_items(False) |
| 163 |
self.assertDictEqual( |
| 164 |
expense_profitability.get('revenues', {}), |
| 165 |
{'id': 'expenses', 'sequence': expense_sequence, 'invoiced': expense_sol.untaxed_amount_invoiced, 'to_invoice': expense_sol_foreign.untaxed_amount_to_invoice * 0.2}, |
| 166 |
) |
| 167 |
|
| 168 |
credit_note = invoice._reverse_moves() |
| 169 |
credit_note.action_post() |
| 170 |
|
| 171 |
expense_profitability = project._get_expenses_profitability_items(False) |
| 172 |
self.assertDictEqual( |
| 173 |
expense_profitability.get('revenues', {}), |
| 174 |
{'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}, |
| 175 |
) |
| 176 |
|
| 177 |
self.sale_order._action_cancel() |
| 178 |
expense_profitability = project._get_expenses_profitability_items(False) |
| 179 |
self.assertDictEqual( |
| 180 |
expense_profitability.get('revenues', {}), |
| 181 |
{'id': 'expenses', 'sequence': expense_sequence, 'invoiced': 0.0, 'to_invoice': expense_sol_foreign.untaxed_amount_to_invoice * 0.2}, |
| 182 |
) |
| 183 |
self.assertDictEqual( |
| 184 |
expense_profitability['costs'], |
| 185 |
{'id': 'expenses', 'sequence': expense_sequence, 'billed': expense.currency_id.round(billed), 'to_bill': 0.0}, |
| 186 |
) |
| 187 |
|
| 188 |
expense.account_move_id.button_draft() |
| 189 |
expense.account_move_id.unlink() |
| 190 |
expense_profitability = project._get_expenses_profitability_items(False) |
| 191 |
self.assertDictEqual( |
| 192 |
expense_profitability.get('revenues', {}), |
| 193 |
{'id': 'expenses', 'sequence': expense_sequence, 'invoiced': 0.0, 'to_invoice': expense_sol_foreign.untaxed_amount_to_invoice * 0.2}, |
| 194 |
) |
| 195 |
self.assertDictEqual( |
| 196 |
expense_profitability.get('costs', {}), |
| 197 |
{'id': 'expenses', 'sequence': expense_sequence, 'billed': expense.currency_id.round(-expense_foreign.untaxed_amount_currency * 0.2), 'to_bill': 0.0}, |
| 198 |
) |
| 199 |
|
| 200 |
invoice = self.env['sale.advance.payment.inv'].with_context({ |
| 201 |
'active_model': 'sale.order', |
| 202 |
'active_id': so_foreign.id, |
| 203 |
}).create({ |
| 204 |
'advance_payment_method': 'delivered', |
| 205 |
})._create_invoices(so_foreign) |
| 206 |
invoice.action_post() |
| 207 |
expense_profitability = project._get_expenses_profitability_items(False) |
| 208 |
self.assertDictEqual( |
| 209 |
expense_profitability.get('revenues', {}), |
| 210 |
{'id': 'expenses', 'sequence': expense_sequence, 'invoiced': expense_sol_foreign.untaxed_amount_invoiced * 0.2, 'to_invoice': 0.0}, |
| 211 |
) |
| 212 |
|
| 213 |
credit_note = invoice._reverse_moves() |
| 214 |
credit_note.action_post() |
| 215 |
|
| 216 |
expense_profitability = project._get_expenses_profitability_items(False) |
| 217 |
self.assertDictEqual( |
| 218 |
expense_profitability.get('revenues', {}), |
| 219 |
{'id': 'expenses', 'sequence': expense_sequence, 'invoiced': 0.0, 'to_invoice': expense_sol_foreign.untaxed_amount_to_invoice * 0.2}, |
| 220 |
) |
| 221 |
|
| 222 |
so_foreign._action_cancel() |
| 223 |
expense_profitability = project._get_expenses_profitability_items(False) |
| 224 |
self.assertDictEqual( |
| 225 |
expense_profitability.get('revenues', {}), |
| 226 |
{}, |
| 227 |
) |
| 228 |
self.assertDictEqual( |
| 229 |
expense_profitability['costs'], |
| 230 |
{'id': 'expenses', 'sequence': expense_sequence, 'billed': expense.currency_id.round(-expense_foreign.untaxed_amount_currency * 0.2), 'to_bill': 0.0}, |
| 231 |
) |
| 232 |
|
| 233 |
expense_foreign.account_move_id.button_draft() |
| 234 |
expense_foreign.account_move_id.unlink() |
| 235 |
expense_profitability = project._get_expenses_profitability_items(False) |
| 236 |
self.assertDictEqual( |
| 237 |
expense_profitability.get('revenues', {}), |
| 238 |
{}, |
| 239 |
) |
| 240 |
self.assertDictEqual( |
| 241 |
expense_profitability.get('costs', {}), |
| 242 |
{}, |
| 243 |
) |
| 244 |
|
| 245 |
def test_project_profitability_2(self): |
| 246 |
""" |
| 247 |
Test Case: |
| 248 |
========== |
| 249 |
- Create an expense for a project. |
| 250 |
- post it's entry moves |
| 251 |
- create an invoice for the sale order linked to the expense |
| 252 |
- post the invoice |
| 253 |
- the project profitability should not include the Customer invoice |
| 254 |
linked to the expense in the revenues, as the Expenses will be there. |
| 255 |
""" |
| 256 |
|
| 257 |
product_new_project_task = self.env['product.product'].create({ |
| 258 |
'name': "Service, create task in new project", |
| 259 |
'standard_price': 30, |
| 260 |
'list_price': 90, |
| 261 |
'type': 'service', |
| 262 |
'default_code': 'SERV-ORDERED2', |
| 263 |
'service_tracking': 'task_in_project', |
| 264 |
}) |
| 265 |
|
| 266 |
sale_order = self.env['sale.order'].with_context(tracking_disable=True).create({ |
| 267 |
'partner_id': self.partner.id, |
| 268 |
'partner_invoice_id': self.partner.id, |
| 269 |
'partner_shipping_id': self.partner.id, |
| 270 |
}) |
| 271 |
|
| 272 |
self.env['sale.order.line'].create({ |
| 273 |
'product_id': product_new_project_task.id, |
| 274 |
'product_uom_qty': 1, |
| 275 |
'order_id': sale_order.id, |
| 276 |
}) |
| 277 |
|
| 278 |
sale_order.action_confirm() |
| 279 |
project = sale_order.order_line.project_id |
| 280 |
|
| 281 |
expense = self.create_expenses({ |
| 282 |
'name': 'expense', |
| 283 |
'product_id': self.company_data['product_order_cost'].id, |
| 284 |
'quantity': 1, |
| 285 |
'analytic_distribution': {project.account_id.id: 100}, |
| 286 |
'sale_order_id': sale_order.id, |
| 287 |
}) |
| 288 |
expense.action_submit() |
| 289 |
expense.action_approve() |
| 290 |
self.post_expenses_with_wizard(expense) |
| 291 |
|
| 292 |
invoice = sale_order._create_invoices() |
| 293 |
invoice.action_post() |
| 294 |
|
| 295 |
sale_items = project.sudo()._get_sale_order_items() |
| 296 |
domain = [ |
| 297 |
('order_id', 'in', sale_items.order_id.ids), |
| 298 |
'|', |
| 299 |
'|', |
| 300 |
('project_id', 'in', project.ids), |
| 301 |
('project_id', '=', False), |
| 302 |
('id', 'in', sale_items.ids), |
| 303 |
] |
| 304 |
|
| 305 |
revenue_items_from_sol = project._get_revenues_items_from_sol(domain, False) |
| 306 |
expense_profitability = project._get_expenses_profitability_items(False) |
| 307 |
project_profitability = project._get_profitability_items(False) |
| 308 |
# invoice linked to the expense should not be included in the revenues |
| 309 |
self.assertDictEqual( |
| 310 |
project_profitability.get('revenues', {}), |
| 311 |
{ |
| 312 |
'data': [expense_profitability['revenues'], revenue_items_from_sol['data'][0]], |
| 313 |
'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']}, |
| 314 |
}, |
| 315 |
) |