Debian Codegraph @

addons/sale_project/tests/test_project_profitability.py

raw
1 # -*- coding: utf-8 -*-
2 # Part of Odoo. See LICENSE file for full copyright and licensing details.
3
4 from datetime import datetime
5
6 from odoo import Command
7 from odoo.tests import tagged
8 from odoo.tools.float_utils import float_compare
9
10 from odoo.addons.sale.tests.common import TestSaleCommon
11 from odoo.addons.project.tests.test_project_profitability import TestProjectProfitabilityCommon as Common
12
13
14 class TestProjectProfitabilityCommon(Common):
15 @classmethod
16 def setUpClass(cls):
17 super().setUpClass()
18 cls.env.user.group_ids += cls.env.ref('sales_team.group_sale_manager')
19 uom_unit_id = cls.env.ref('uom.product_uom_unit').id
20
21 # Create material product
22 cls.material_product = cls.env['product.product'].create({
23 'name': 'Material',
24 'type': 'consu',
25 'standard_price': 5,
26 'list_price': 10,
27 'invoice_policy': 'order',
28 'uom_id': uom_unit_id,
29 })
30
31 # Create service products
32 cls.uom_hour = cls.env.ref('uom.product_uom_hour')
33 cls.product_delivery_service = cls.env['product.product'].create({
34 'name': "Service Delivery, create task in global project",
35 'standard_price': 30,
36 'list_price': 90,
37 'type': 'service',
38 'invoice_policy': 'delivery',
39 'service_type': 'manual',
40 'uom_id': cls.uom_hour.id,
41 'default_code': 'SERV-ORDERED2',
42 'service_tracking': 'task_global_project',
43 'project_id': cls.project.id,
44 })
45 cls.down_payment_product = cls.env['product.product'].create({
46 'name': "downpayment product, used to simulate down payments",
47 'standard_price': 30,
48 'type': 'service',
49 'service_policy': 'ordered_prepaid',
50 })
51 cls.sale_order = cls.env['sale.order'].with_context(tracking_disable=True).create({
52 'partner_id': cls.partner.id,
53 'partner_invoice_id': cls.partner.id,
54 'partner_shipping_id': cls.partner.id,
55 })
56 SaleOrderLine = cls.env['sale.order.line'].with_context(tracking_disable=True, default_order_id=cls.sale_order.id)
57 cls.delivery_service_order_line = SaleOrderLine.create({
58 'product_id': cls.product_delivery_service.id,
59 'product_uom_qty': 10,
60 })
61 cls.sale_order.action_confirm()
62
63 cls.analytic_account_nb = cls.env['account.analytic.account'].create({
64 'name': 'Project non billable AA',
65 'code': 'AA-123456',
66 'plan_id': cls.analytic_plan.id,
67 })
68
69 cls.project_non_billable = cls.env['project.project'].with_context(tracking_disable=True).create({
70 'name': "Non Billable Project",
71 'account_id': cls.analytic_account_nb.id,
72 'allow_billable': False,
73 'partner_id': False,
74 })
75 cls.project_billable_no_company = cls.env['project.project'].create({'name': 'project billable', 'allow_billable': True})
76 cls.project_billable_no_company._create_analytic_account()
77
78
79 @tagged('-at_install', 'post_install')
80 class TestSaleProjectProfitability(TestProjectProfitabilityCommon, TestSaleCommon):
81
82 @classmethod
83 def setUpClass(cls):
84 super().setUpClass()
85 cls.company_data_2 = cls.setup_other_company()
86
87 def test_profitability_of_non_billable_project(self):
88 """ Test no data is found for the project profitability since the project is not billable
89 even if it is linked to a sale order items.
90 """
91 # Adding an extra cost/revenue to ensure those are not computed either.
92 self.env['account.analytic.line'].create([{
93 'name': 'other revenues line',
94 'account_id': self.project_non_billable.account_id.id,
95 'amount': 100,
96 }, {
97 'name': 'other costs line',
98 'account_id': self.project_non_billable.account_id.id,
99 'amount': -100,
100 }])
101 self.assertFalse(self.project_non_billable.allow_billable)
102 panel_data = self.project_non_billable.get_panel_data()
103 self.assertFalse(panel_data.get('profitability_items'))
104 self.assertFalse(panel_data.get('profitability_labels'))
105 self.project_non_billable.write({'sale_line_id': self.sale_order.order_line[0].id})
106 panel_data = self.project_non_billable.get_panel_data()
107 self.assertFalse(panel_data.get('profitability_items'),
108 "Even if the project has a sale order item linked, the project profitability should not be computed since it is not billable.")
109 self.assertFalse(panel_data.get('profitability_labels'),
110 "Even if the project has a sale order item linked, the project profitability should not be computed since it is not billable.")
111
112 def test_project_profitability(self):
113 foreign_company = self.company_data_2['company']
114 foreign_company.currency_id = self.foreign_currency
115 companies = foreign_company | self.sale_order.company_id
116 self.project.company_id = False
117 self.assertFalse(self.project.allow_billable, 'The project should be non billable.')
118 self.assertDictEqual(
119 self.project._get_profitability_items(False),
120 self.project_profitability_items_empty,
121 'No data for the project profitability should be found since the project is not billable, so no SOL is linked to the project.'
122 )
123 self.project.write({'allow_billable': True})
124 self.assertTrue(self.project.allow_billable, 'The project should be billable.')
125 self.project.sale_line_id = self.delivery_service_order_line
126 self.assertDictEqual(
127 self.project._get_profitability_items(False),
128 self.project_profitability_items_empty,
129 'No data for the project profitability should be found since no product is delivered in the SO linked.'
130 )
131
132 # Add extra cost and extra revenues to the analytic account.
133 self.env['account.analytic.line'].create([{
134 'name': 'other revenues line',
135 'account_id': self.project.account_id.id,
136 'amount': 100,
137 }, {
138 'name': 'other costs line',
139 'account_id': self.project.account_id.id,
140 'amount': -100,
141 }])
142
143 # Create and confirm a SO in a foreign company.
144 product_delivery_service_foreign = self.env['product.product'].with_company(foreign_company).create({
145 'name': "Service Delivery, create task in global project",
146 'standard_price': 30,
147 'list_price': 90,
148 'type': 'service',
149 'invoice_policy': 'delivery',
150 'service_type': 'manual',
151 'uom_id': self.uom_hour.id,
152 'default_code': 'SERV-ORDERED2',
153 'service_tracking': 'task_global_project',
154 'project_id': self.project.id,
155 })
156 sale_order_foreign = self.env['sale.order'].with_context(tracking_disable=True).create({
157 'partner_id': self.partner.id,
158 'partner_invoice_id': self.partner.id,
159 'partner_shipping_id': self.partner.id,
160 'company_id': foreign_company.id,
161 })
162 sale_order_foreign.currency_id = self.foreign_currency.id
163 sol_foreign = self.env['sale.order.line'].with_context(tracking_disable=True, default_order_id=sale_order_foreign.id).create({
164 'product_id': product_delivery_service_foreign.id,
165 'product_uom_qty': 10,
166 'company_id': foreign_company.id,
167 })
168 sale_order_foreign.action_confirm()
169 sol_foreign.qty_delivered = 1
170 service_policy_to_invoice_type = self.project._get_service_policy_to_invoice_type()
171 invoice_type = service_policy_to_invoice_type[self.delivery_service_order_line.product_id.service_policy]
172 self.assertIn(
173 invoice_type,
174 ['billable_manual', 'service_revenues'],
175 'invoice_type="billable_manual" if sale_timesheet is installed otherwise it is equal to "service_revenues"')
176 sequence_per_invoice_type = self.project._get_profitability_sequence_per_invoice_type()
177 # Ensures that when the only SO linked to the project is a foreign SO, the currency used is the default one, and not the currency of the SO.
178 self.assertDictEqual(
179 self.project._get_profitability_items(False),
180 {
181 'revenues': {
182 'data': [
183 {
184 'id': 'other_revenues_aal',
185 'sequence': sequence_per_invoice_type['other_revenues_aal'],
186 'invoiced': 100.0,
187 'to_invoice': 0.0,
188 },
189 {
190 # id should be equal to "billable_manual" if "sale_timesheet" module is installed otherwise "service_revenues"
191 'id': invoice_type,
192 'sequence': sequence_per_invoice_type[invoice_type],
193 'to_invoice': sol_foreign.untaxed_amount_to_invoice * 0.2,
194 'invoiced': 0.0,
195 },
196 ],
197 'total': {
198 'to_invoice': sol_foreign.untaxed_amount_to_invoice * 0.2,
199 'invoiced': 100.0,
200 },
201 },
202 'costs': {
203 'data': [{'id': 'other_costs_aal', 'sequence': sequence_per_invoice_type['other_costs_aal'], 'billed': -100.0, 'to_bill': 0.0}],
204 'total': {'billed': -100.0, 'to_bill': 0.0},
205 },
206 }
207 )
208 self.assertNotEqual(sol_foreign.untaxed_amount_to_invoice, 0.0)
209 self.assertEqual(sol_foreign.untaxed_amount_invoiced, 0.0)
210
211 # Set the qty_delivered of the sol of the main so to 1, this sol should now be computed for the project_profitability.
212 self.delivery_service_order_line.qty_delivered = 1
213 self.assertIn('service_revenues', sequence_per_invoice_type)
214 self.assertDictEqual(
215 self.project._get_profitability_items(False),
216 {
217 'revenues': {
218 'data': [
219 {
220 'id': 'other_revenues_aal',
221 'sequence': sequence_per_invoice_type['other_revenues_aal'],
222 'invoiced': 100.0,
223 'to_invoice': 0.0,
224 },
225 {
226 # id should be equal to "billable_manual" if "sale_timesheet" module is installed otherwise "service_revenues"
227 'id': invoice_type,
228 'sequence': sequence_per_invoice_type[invoice_type],
229 'to_invoice': self.delivery_service_order_line.untaxed_amount_to_invoice + sol_foreign.untaxed_amount_to_invoice * 0.2,
230 'invoiced': 0.0,
231 },
232 ],
233 'total': {
234 'to_invoice': self.delivery_service_order_line.untaxed_amount_to_invoice + sol_foreign.untaxed_amount_to_invoice * 0.2,
235 'invoiced': 100,
236 },
237 },
238 'costs': {
239 'data': [{'id': 'other_costs_aal', 'sequence': sequence_per_invoice_type['other_costs_aal'], 'billed': -100.0, 'to_bill': 0.0}],
240 'total': {'billed': -100.0, 'to_bill': 0.0},
241 },
242 }
243 )
244 self.assertNotEqual(self.delivery_service_order_line.untaxed_amount_to_invoice, 0.0)
245 self.assertEqual(self.delivery_service_order_line.untaxed_amount_invoiced, 0.0)
246
247 # Create and post an invoice for the foreign SO.
248 context = {
249 'active_model': 'sale.order',
250 'active_ids': sale_order_foreign.ids,
251 'active_id': sale_order_foreign.id,
252 'allowed_company_ids': companies.ids,
253 }
254 invoices_foreign = self.env['sale.advance.payment.inv'].with_context(context).create({
255 'advance_payment_method': 'delivered',
256 })._create_invoices(sale_order_foreign)
257 invoices_foreign.action_post()
258 # Ensures the foreign SO sols are now computed for the 'invoiced' section, while the sol's from the main SO are still in the 'to_invoice' section
259 self.assertDictEqual(
260 self.project._get_profitability_items(False),
261 {
262 'revenues': {
263 'data': [
264 {
265 'id': 'other_revenues_aal',
266 'sequence': sequence_per_invoice_type['other_revenues_aal'],
267 'invoiced': 100.0,
268 'to_invoice': 0.0,
269 },
270 {
271 # id should be equal to "billable_manual" if "sale_timesheet" module is installed otherwise "service_revenues"
272 'id': invoice_type,
273 'sequence': sequence_per_invoice_type[invoice_type],
274 'to_invoice': self.delivery_service_order_line.untaxed_amount_to_invoice,
275 'invoiced': sol_foreign.untaxed_amount_invoiced * 0.2,
276 },
277 ],
278 'total': {
279 'to_invoice': self.delivery_service_order_line.untaxed_amount_to_invoice,
280 'invoiced': 100 + sol_foreign.untaxed_amount_invoiced * 0.2,
281 },
282 },
283 'costs': {
284 'data': [
285 {'id': 'other_costs_aal', 'sequence': sequence_per_invoice_type['other_costs_aal'], 'billed': -100.0,
286 'to_bill': 0.0}],
287 'total': {'billed': -100.0, 'to_bill': 0.0},
288 },
289 }
290 )
291 self.assertEqual(sol_foreign.qty_invoiced, 1)
292 self.assertEqual(sol_foreign.untaxed_amount_to_invoice, 0.0)
293 self.assertNotEqual(sol_foreign.untaxed_amount_invoiced, 0.0)
294
295 # Create and post an invoice for the main SO.
296 context = {
297 'active_model': 'sale.order',
298 'active_ids': self.sale_order.ids,
299 'active_id': self.sale_order.id,
300 }
301 invoices = self.env['sale.advance.payment.inv'].with_context(context).create({
302 'advance_payment_method': 'delivered',
303 })._create_invoices(self.sale_order)
304 invoices.action_post()
305 invoice_type = service_policy_to_invoice_type[self.delivery_service_order_line.product_id.service_policy]
306 self.assertIn(
307 invoice_type,
308 ['billable_manual', 'service_revenues'],
309 'invoice_type="billable_manual" if sale_timesheet is installed otherwise it is equal to "service_revenues"')
310 # Ensures that the 'to_invoice' section is now empty, and the 'invoiced' section contains the amount from all the sol's.
311 self.assertDictEqual(
312 self.project._get_profitability_items(False),
313 {
314 'revenues': {
315 'data': [
316 {
317 'id': 'other_revenues_aal',
318 'sequence': sequence_per_invoice_type['other_revenues_aal'],
319 'invoiced': 100.0,
320 'to_invoice': 0.0,
321 },
322 {
323 'id': invoice_type,
324 'sequence': sequence_per_invoice_type[invoice_type],
325 'to_invoice': 0.0,
326 'invoiced': self.delivery_service_order_line.untaxed_amount_invoiced + sol_foreign.untaxed_amount_invoiced * 0.2,
327 },
328 ],
329 'total': {
330 'to_invoice': 0.0,
331 'invoiced': self.delivery_service_order_line.untaxed_amount_invoiced + 100 + sol_foreign.untaxed_amount_invoiced * 0.2,
332 },
333 },
334 'costs': {
335 'data': [{'id': 'other_costs_aal', 'sequence': sequence_per_invoice_type['other_costs_aal'], 'billed': -100.0, 'to_bill': 0.0}],
336 'total': {'billed': -100.0, 'to_bill': 0.0},
337 },
338 }
339 )
340 self.assertEqual(self.delivery_service_order_line.qty_invoiced, 1)
341 self.assertEqual(self.delivery_service_order_line.untaxed_amount_to_invoice, 0.0)
342 self.assertNotEqual(self.delivery_service_order_line.untaxed_amount_invoiced, 0.0)
343
344 # Add 2 sale order item to the foreign SO.
345 SaleOrderLineForeign = self.env['sale.order.line'].with_context(tracking_disable=True, default_order_id=sale_order_foreign.id)
346 manual_service_sol_foreign, material_sol_foreign = SaleOrderLineForeign.create([{
347 'product_id': self.product_delivery_service.id,
348 'product_uom_qty': 5,
349 'qty_delivered': 5,
350 }, {
351 'product_id': self.material_product.id,
352 'product_uom_qty': 1,
353 'qty_delivered': 1,
354 }])
355 service_sols_foreign = sol_foreign + manual_service_sol_foreign
356 # Ensures that the 'materials' section is now present, and that the new manual sol is computed in the 'to_invoice' section.
357 self.assertDictEqual(
358 self.project._get_profitability_items(False),
359 {
360 'revenues': {
361 'data': [
362 {
363 'id': 'other_revenues_aal',
364 'sequence': sequence_per_invoice_type['other_revenues_aal'],
365 'invoiced': 100.0,
366 'to_invoice': 0.0,
367 },
368 {
369 'id': invoice_type,
370 'sequence': sequence_per_invoice_type[invoice_type],
371 'to_invoice': sum(service_sols_foreign.mapped('untaxed_amount_to_invoice')) * 0.2,
372 'invoiced': self.delivery_service_order_line.untaxed_amount_invoiced + sum(service_sols_foreign.mapped('untaxed_amount_invoiced')) * 0.2,
373 },
374 {
375 'id': 'materials',
376 'sequence': sequence_per_invoice_type['materials'],
377 'to_invoice': material_sol_foreign.untaxed_amount_to_invoice * 0.2,
378 'invoiced': material_sol_foreign.untaxed_amount_invoiced * 0.2,
379 },
380 ],
381 'total': {
382 'to_invoice': (sum(service_sols_foreign.mapped('untaxed_amount_to_invoice')) + material_sol_foreign.untaxed_amount_to_invoice) * 0.2,
383 'invoiced': self.delivery_service_order_line.untaxed_amount_invoiced + (sum(service_sols_foreign.mapped('untaxed_amount_invoiced')) + material_sol_foreign.untaxed_amount_invoiced) * 0.2 + 100,
384 },
385 },
386 'costs': {
387 'data': [{'id': 'other_costs_aal', 'sequence': sequence_per_invoice_type['other_costs_aal'], 'billed': -100.0, 'to_bill': 0.0}],
388 'total': {'billed': -100.0, 'to_bill': 0.0},
389 },
390 },
391 )
392 self.assertNotEqual(manual_service_sol_foreign.untaxed_amount_to_invoice, 0.0)
393 self.assertEqual(manual_service_sol_foreign.untaxed_amount_invoiced, 0.0)
394 self.assertNotEqual(material_sol_foreign.untaxed_amount_to_invoice, 0.0)
395 self.assertEqual(material_sol_foreign.untaxed_amount_invoiced, 0.0)
396 # Add 2 sales order items in the main SO.
397 SaleOrderLine = self.env['sale.order.line'].with_context(tracking_disable=True, default_order_id=self.sale_order.id)
398 manual_service_order_line = SaleOrderLine.create({
399 'product_id': self.product_delivery_service.id,
400 'product_uom_qty': 5,
401 'qty_delivered': 5,
402 })
403 material_order_line = SaleOrderLine.create({
404 'product_id': self.material_product.id,
405 'product_uom_qty': 1,
406 'qty_delivered': 1,
407 })
408 service_sols = self.delivery_service_order_line + manual_service_order_line
409 invoice_type = service_policy_to_invoice_type[manual_service_order_line.product_id.service_policy]
410 self.assertIn(
411 invoice_type,
412 ['billable_manual', 'service_revenues'],
413 'invoice_type="billable_manual" if sale_timesheet is installed otherwise it is equal to "service_revenues"')
414 # Ensures that the 'materials' section contains the material sol from the main company, and that the new manual sol is computed in the 'to_invoice' section.
415 self.assertDictEqual(
416 self.project._get_profitability_items(False),
417 {
418 'revenues': {
419 'data': [
420 {
421 'id': 'other_revenues_aal',
422 'sequence': sequence_per_invoice_type['other_revenues_aal'],
423 'invoiced': 100.0,
424 'to_invoice': 0.0,
425 },
426 {
427 'id': invoice_type,
428 'sequence': sequence_per_invoice_type[invoice_type],
429 'to_invoice': sum(service_sols.mapped('untaxed_amount_to_invoice')) + sum(service_sols_foreign.mapped('untaxed_amount_to_invoice')) * 0.2,
430 'invoiced': sum(service_sols.mapped('untaxed_amount_invoiced')) + sum(service_sols_foreign.mapped('untaxed_amount_invoiced')) * 0.2,
431 },
432 {
433 'id': 'materials',
434 'sequence': sequence_per_invoice_type['materials'],
435 'to_invoice': material_order_line.untaxed_amount_to_invoice + material_sol_foreign.untaxed_amount_to_invoice * 0.2,
436 'invoiced': material_order_line.untaxed_amount_invoiced + material_sol_foreign.untaxed_amount_invoiced * 0.2,
437 },
438 ],
439 'total': {
440 'to_invoice': sum(service_sols.mapped('untaxed_amount_to_invoice')) + material_order_line.untaxed_amount_to_invoice +
441 (sum(service_sols_foreign.mapped('untaxed_amount_to_invoice')) + material_sol_foreign.untaxed_amount_to_invoice) * 0.2,
442 'invoiced': sum(service_sols.mapped('untaxed_amount_invoiced')) + material_order_line.untaxed_amount_invoiced +
443 (sum(service_sols_foreign.mapped('untaxed_amount_invoiced')) + material_sol_foreign.untaxed_amount_invoiced) * 0.2 + 100,
444 },
445 },
446 'costs': {
447 'data': [{'id': 'other_costs_aal', 'sequence': sequence_per_invoice_type['other_costs_aal'], 'billed': -100.0, 'to_bill': 0.0}],
448 'total': {'billed': -100.0, 'to_bill': 0.0},
449 },
450 },
451 )
452 self.assertNotEqual(manual_service_order_line.untaxed_amount_to_invoice, 0.0)
453 self.assertEqual(manual_service_order_line.untaxed_amount_invoiced, 0.0)
454 self.assertNotEqual(material_order_line.untaxed_amount_to_invoice, 0.0)
455 self.assertEqual(material_order_line.untaxed_amount_invoiced, 0.0)
456
457 # Revert the invoice from the foreign SO.
458 credit_notes = invoices_foreign._reverse_moves()
459 credit_notes.action_post()
460 # Ensures that the sols that were invoiced are computed in the 'to_invoice' section again.
461 self.assertDictEqual(
462 self.project._get_profitability_items(False),
463 {
464 'revenues': {
465 'data': [
466 {
467 'id': 'other_revenues_aal',
468 'sequence': sequence_per_invoice_type['other_revenues_aal'],
469 'invoiced': 100.0,
470 'to_invoice': 0.0,
471 },
472 {
473 'id': invoice_type,
474 'sequence': sequence_per_invoice_type[invoice_type],
475 'to_invoice': sum(service_sols.mapped('untaxed_amount_to_invoice')) + sum(service_sols_foreign.mapped('untaxed_amount_to_invoice')) * 0.2,
476 'invoiced': sum(service_sols.mapped('untaxed_amount_invoiced')) + sum(service_sols_foreign.mapped('untaxed_amount_invoiced')) * 0.2,
477 },
478 {
479 'id': 'materials',
480 'sequence': sequence_per_invoice_type['materials'],
481 'to_invoice': material_order_line.untaxed_amount_to_invoice + material_sol_foreign.untaxed_amount_to_invoice * 0.2,
482 'invoiced': material_order_line.untaxed_amount_invoiced + material_sol_foreign.untaxed_amount_invoiced * 0.2,
483 },
484 ],
485 'total': {
486 'to_invoice': sum(service_sols.mapped('untaxed_amount_to_invoice')) + material_order_line.untaxed_amount_to_invoice +
487 (sum(service_sols_foreign.mapped('untaxed_amount_to_invoice')) + material_sol_foreign.untaxed_amount_to_invoice) * 0.2,
488 'invoiced': sum(service_sols.mapped('untaxed_amount_invoiced')) + material_order_line.untaxed_amount_invoiced +
489 (sum(service_sols_foreign.mapped('untaxed_amount_invoiced')) + material_sol_foreign.untaxed_amount_invoiced) * 0.2 + 100,
490 },
491 },
492 'costs': {
493 'data': [{'id': 'other_costs_aal', 'sequence': sequence_per_invoice_type['other_costs_aal'], 'billed': -100.0, 'to_bill': 0.0}],
494 'total': {'billed': -100.0, 'to_bill': 0.0},
495 },
496 },
497 )
498 self.assertEqual(sol_foreign.qty_invoiced, 0.0)
499 self.assertNotEqual(sol_foreign.untaxed_amount_to_invoice, 0.0)
500 self.assertEqual(sol_foreign.untaxed_amount_invoiced, 0.0)
501
502 # Revert the invoice from the main SO.
503 credit_notes = invoices._reverse_moves()
504 credit_notes.action_post()
505 # Ensures that the sols that were invoiced are computed in the 'to_invoice' section again.
506 self.assertDictEqual(
507 self.project._get_profitability_items(False),
508 {
509 'revenues': {
510 'data': [
511 {
512 'id': 'other_revenues_aal',
513 'sequence': sequence_per_invoice_type['other_revenues_aal'],
514 'invoiced': 100.0,
515 'to_invoice': 0.0,
516 },
517 {
518 'id': invoice_type,
519 'sequence': sequence_per_invoice_type[invoice_type],
520 'to_invoice': sum(service_sols.mapped('untaxed_amount_to_invoice')) + sum(service_sols_foreign.mapped('untaxed_amount_to_invoice')) * 0.2,
521 'invoiced': sum(service_sols.mapped('untaxed_amount_invoiced')) + sum(service_sols_foreign.mapped('untaxed_amount_invoiced')) * 0.2,
522 },
523 {
524 'id': 'materials',
525 'sequence': sequence_per_invoice_type['materials'],
526 'to_invoice': material_order_line.untaxed_amount_to_invoice + material_sol_foreign.untaxed_amount_to_invoice * 0.2,
527 'invoiced': material_order_line.untaxed_amount_invoiced + material_sol_foreign.untaxed_amount_invoiced * 0.2,
528 },
529 ],
530 'total': {
531 'to_invoice': sum(service_sols.mapped('untaxed_amount_to_invoice')) + material_order_line.untaxed_amount_to_invoice +
532 (sum(service_sols_foreign.mapped('untaxed_amount_to_invoice')) + material_sol_foreign.untaxed_amount_to_invoice) * 0.2,
533 'invoiced': sum(service_sols.mapped('untaxed_amount_invoiced')) + material_order_line.untaxed_amount_invoiced +
534 (sum(service_sols_foreign.mapped('untaxed_amount_invoiced')) + material_sol_foreign.untaxed_amount_invoiced) * 0.2 + 100,
535 },
536 },
537 'costs': {
538 'data': [{'id': 'other_costs_aal', 'sequence': sequence_per_invoice_type['other_costs_aal'], 'billed': -100.0, 'to_bill': 0.0}],
539 'total': {'billed': -100.0, 'to_bill': 0.0},
540 },
541 },
542 )
543 self.assertEqual(self.delivery_service_order_line.qty_invoiced, 0.0)
544 self.assertNotEqual(self.delivery_service_order_line.untaxed_amount_to_invoice, 0.0)
545 self.assertEqual(self.delivery_service_order_line.untaxed_amount_invoiced, 0.0)
546
547 # Cancel the foreign SO.
548 sale_order_foreign._action_cancel()
549 # Ensures that the panel now contains only the sols from the main SO.
550 self.assertDictEqual(
551 self.project._get_profitability_items(False),
552 {
553 'revenues': {
554 'data': [
555 {
556 'id': 'other_revenues_aal',
557 'sequence': sequence_per_invoice_type['other_revenues_aal'],
558 'invoiced': 100.0,
559 'to_invoice': 0.0,
560 },
561 {
562 'id': invoice_type,
563 'sequence': sequence_per_invoice_type[invoice_type],
564 'to_invoice': sum(service_sols.mapped('untaxed_amount_to_invoice')),
565 'invoiced': sum(service_sols.mapped('untaxed_amount_invoiced')),
566 },
567 {
568 'id': 'materials',
569 'sequence': sequence_per_invoice_type['materials'],
570 'to_invoice': material_order_line.untaxed_amount_to_invoice,
571 'invoiced': material_order_line.untaxed_amount_invoiced,
572 },
573 ],
574 'total': {
575 'to_invoice': sum(service_sols.mapped('untaxed_amount_to_invoice')) + material_order_line.untaxed_amount_to_invoice,
576 'invoiced': sum(service_sols.mapped('untaxed_amount_invoiced')) + material_order_line.untaxed_amount_invoiced + 100,
577 },
578 },
579 'costs': {
580 'data': [{'id': 'other_costs_aal', 'sequence': sequence_per_invoice_type['other_costs_aal'], 'billed': -100.0, 'to_bill': 0.0}],
581 'total': {'billed': -100.0, 'to_bill': 0.0},
582 },
583 },
584 )
585 # Create a down payment for a fixed amount of 115.
586 Downpayment = {
587 'active_model': 'sale.order',
588 'active_ids': self.sale_order.ids,
589 'active_id': self.sale_order.id,
590 'default_journal_id': self.company_data['default_journal_sale'].id,
591 }
592 downpayment = self.env['sale.advance.payment.inv'].with_context(Downpayment).create({
593 'advance_payment_method': 'fixed',
594 'fixed_amount': 115,
595 })
596 # When a down payment is created, the default 15% tax is included. The SOL associated it then created by removing the taxed amount.
597 # Therefore, the amount of the dp is higher than the amount of the sol created.
598 down_payment_invoiced = 100.00
599 downpayment.create_invoices()
600 self.sale_order.invoice_ids[2].action_post()
601 # Ensures the down payment is correctly computed for the project profitability.
602 self._assert_dict_equal(invoice_type, sequence_per_invoice_type, material_order_line, service_sols, manual_service_order_line, down_payment_invoiced)
603
604 # Create a second down payment for a fixed amount of 115.
605 downpayment = self.env['sale.advance.payment.inv'].with_context(Downpayment).create({
606 'advance_payment_method': 'fixed',
607 'fixed_amount': 115,
608 })
609 down_payment_invoiced = 2 * down_payment_invoiced
610 downpayment.create_invoices()
611 self.sale_order.invoice_ids[3].action_post()
612 # Ensures the 2 down payments are correctly computed for the project profitability.
613 self._assert_dict_equal(invoice_type, sequence_per_invoice_type, material_order_line, service_sols, manual_service_order_line, down_payment_invoiced)
614
615 for sol in sale_order_foreign.order_line:
616 self.assertEqual(sol.untaxed_amount_to_invoice, 0.0)
617 self.assertEqual(sol.untaxed_amount_invoiced, 0.0)
618
619 # Cancel the main SO.
620 self.sale_order._action_cancel()
621 # Ensures that the panel no longer contains any SOL related section
622 self.assertDictEqual(
623 self.project._get_profitability_items(False),
624 {
625 # even if the sale order is canceled, if some expenses/revenues were added manually to the account, those lines must appear in the project profitabilty panel
626 'revenues': {
627 'data': [
628 {'id': 'other_revenues_aal', 'sequence': sequence_per_invoice_type['other_revenues_aal'], 'invoiced': 100.0, 'to_invoice': 0.0}],
629 'total': {'to_invoice': 0.0, 'invoiced': 100},
630 },
631 'costs': {
632 'data': [{'id': 'other_costs_aal', 'sequence': sequence_per_invoice_type['other_costs_aal'], 'billed': -100.0, 'to_bill': 0.0}],
633 'total': {'billed': -100.0, 'to_bill': 0.0},
634 },
635 },
636 )
637 #downpayment invoiced amount are not updated when the SO is canceled.
638 for sol in self.sale_order.order_line:
639 if sol.is_downpayment:
640 continue
641 self.assertEqual(sol.untaxed_amount_to_invoice, 0.0)
642 self.assertEqual(sol.untaxed_amount_invoiced, 0.0)
643
644 def _assert_dict_equal(self, invoice_type, sequence_per_invoice_type, material_order_line, service_sols, manual_service_order_line, down_payment_invoiced):
645 self.assertDictEqual(
646 self.project._get_profitability_items(False),
647 {
648 'revenues': {
649 'data': [
650 {
651 'id': 'other_revenues_aal',
652 'sequence': sequence_per_invoice_type['other_revenues_aal'],
653 'invoiced': 100.0,
654 'to_invoice': 0.0,
655 },
656 {
657 'id': 'downpayments', 'sequence': 20, 'invoiced': down_payment_invoiced,
658 'to_invoice': -down_payment_invoiced,
659 },
660 {
661 'id': invoice_type,
662 'sequence': sequence_per_invoice_type[invoice_type],
663 'invoiced': manual_service_order_line.untaxed_amount_invoiced,
664 'to_invoice': sum(service_sols.mapped('untaxed_amount_to_invoice')),
665 },
666 {
667 'id': 'materials',
668 'sequence': sequence_per_invoice_type['materials'],
669 'invoiced': material_order_line.untaxed_amount_invoiced,
670 'to_invoice': material_order_line.untaxed_amount_to_invoice,
671 },
672 ],
673 'total': {
674 'invoiced': manual_service_order_line.untaxed_amount_invoiced + material_order_line.untaxed_amount_invoiced + down_payment_invoiced + 100,
675 'to_invoice': sum(service_sols.mapped(
676 'untaxed_amount_to_invoice')) + material_order_line.untaxed_amount_to_invoice - down_payment_invoiced,
677 },
678 },
679 'costs': {
680 'data': [{'id': 'other_costs_aal', 'sequence': sequence_per_invoice_type['other_costs_aal'], 'billed': -100.0, 'to_bill': 0.0}],
681 'total': {'billed': -100.0, 'to_bill': 0.0},
682 },
683 },
684 )
685
686 def test_invoices_without_sale_order_are_accounted_in_profitability(self):
687 """
688 An invoice that has an AAL on one of its line should be taken into account
689 for the profitability of the project.
690 The contribution of the line should only be dependent
691 on the project's analytic account % that was set on the line
692 """
693 foreign_company = self.company_data_2['company']
694 foreign_company.currency_id = self.foreign_currency
695 # a custom analytic contribution (number between 1 -> 100 included)
696 analytic_distribution = 50
697 analytic_contribution = analytic_distribution / 100.
698 # Create an invoice with a foreign company with the AAL linked to the project account.
699 invoice_1_foreign = self.env['account.move'].create({
700 "name": "Invoice_1",
701 "move_type": "out_invoice",
702 "state": "draft",
703 "partner_id": self.partner.id,
704 "invoice_date": datetime.today(),
705 "company_id": foreign_company.id,
706 "invoice_line_ids": [Command.create({
707 "analytic_distribution": {self.project_billable_no_company.account_id.id: analytic_distribution},
708 "product_id": self.product_a.id,
709 "quantity": 1,
710 "product_uom_id": self.product_a.uom_id.id,
711 "price_unit": self.product_a.standard_price,
712 "currency_id": self.foreign_currency.id,
713 })],
714 })
715 # The invoice with foreign company is in draft, therefore its total is in the 'to invoice' section. The total should be update by the choas orb/dollar rate (0.2)
716 self.assertDictEqual(
717 self.project_billable_no_company._get_profitability_items(False)['revenues'],
718 {
719 'data': [{
720 'id': 'other_invoice_revenues',
721 'sequence': self.project_billable_no_company._get_profitability_sequence_per_invoice_type()['other_invoice_revenues'],
722 'to_invoice': self.product_a.standard_price * analytic_contribution * 0.2,
723 'invoiced': 0.0,
724 }],
725 'total': {'to_invoice': self.product_a.standard_price * analytic_contribution * 0.2, 'invoiced': 0.0},
726 },
727 )
728 # Create an invoice_1 with the AAL linked to the project account.
729 invoice_1 = self.env['account.move'].create({
730 "name": "Invoice_1",
731 "move_type": "out_invoice",
732 "state": "draft",
733 "partner_id": self.partner.id,
734 "invoice_date": datetime.today(),
735 "invoice_line_ids": [Command.create({
736 "analytic_distribution": {self.project_billable_no_company.account_id.id: analytic_distribution},
737 "product_id": self.product_a.id,
738 "quantity": 1,
739 "product_uom_id": self.product_a.uom_id.id,
740 "price_unit": self.product_a.standard_price,
741 })],
742 })
743 # The invoice_1 is in draft, therefore its total should be added to the 'to_invoice' section.
744 self.assertDictEqual(
745 self.project_billable_no_company._get_profitability_items(False)['revenues'],
746 {
747 'data': [{
748 'id': 'other_invoice_revenues',
749 'sequence': self.project_billable_no_company._get_profitability_sequence_per_invoice_type()['other_invoice_revenues'],
750 'to_invoice': self.product_a.standard_price * analytic_contribution * 1.2,
751 'invoiced': 0.0,
752 }],
753 'total': {'to_invoice': self.product_a.standard_price * analytic_contribution * 1.2, 'invoiced': 0.0},
754 },
755 )
756 # post invoice_1
757 invoice_1.action_post()
758 # We posted the invoice_1, therefore its total should be in the 'invoiced' section. The 'to_invoice' section should now contain only the foreign invoice.
759 self.assertDictEqual(
760 self.project_billable_no_company._get_profitability_items(False)['revenues'],
761 {
762 'data': [{
763 'id': 'other_invoice_revenues',
764 'sequence': self.project_billable_no_company._get_profitability_sequence_per_invoice_type()['other_invoice_revenues'],
765 'to_invoice': self.product_a.standard_price * analytic_contribution * 0.2,
766 'invoiced': self.product_a.standard_price * analytic_contribution,
767 }],
768 'total': {'to_invoice': self.product_a.standard_price * analytic_contribution * 0.2, 'invoiced': self.product_a.standard_price * analytic_contribution},
769 },
770 )
771 invoice_1_foreign.action_post()
772 # We posted the foreign invoice 1. Its total should now be in the 'invoiced' section. The 'to_invoice' section should be 0.
773 self.assertDictEqual(
774 self.project_billable_no_company._get_profitability_items(False)['revenues'],
775 {
776 'data': [{
777 'id': 'other_invoice_revenues',
778 'sequence': self.project_billable_no_company._get_profitability_sequence_per_invoice_type()['other_invoice_revenues'],
779 'to_invoice': 0.0,
780 'invoiced': self.product_a.standard_price * analytic_contribution * 1.2,
781 }],
782 'total': {'to_invoice': 0.0, 'invoiced': self.product_a.standard_price * analytic_contribution * 1.2},
783 },
784 )
785
786 # Ensures the sale_line_ids from multiple invoices from the same company are correctly computed.
787 # Create another invoice, with 2 lines, 2 diff products, the second line has a quantity of 2, the third line has a negative amount
788 NEG_AMOUNT = -42
789 invoice_2 = self.env['account.move'].create({
790 "name": "I have 2 lines",
791 "move_type": "out_invoice",
792 "state": "draft",
793 "partner_id": self.partner.id,
794 "invoice_date": datetime.today(),
795 "invoice_line_ids": [Command.create({
796 "analytic_distribution": {self.project_billable_no_company.account_id.id: analytic_distribution},
797 "product_id": self.product_a.id,
798 "quantity": 1,
799 "product_uom_id": self.product_a.uom_id.id,
800 "price_unit": self.product_a.standard_price,
801 }), Command.create({
802 "analytic_distribution": {self.project_billable_no_company.account_id.id: analytic_distribution},
803 "product_id": self.product_b.id,
804 "quantity": 2,
805 "product_uom_id": self.product_b.uom_id.id,
806 "price_unit": self.product_b.standard_price,
807 }), Command.create({
808 "analytic_distribution": {self.project_billable_no_company.account_id.id: analytic_distribution},
809 "product_id": self.product_b.id,
810 "quantity": 1,
811 "product_uom_id": self.product_b.uom_id.id,
812 "price_unit": NEG_AMOUNT,
813 })],
814 })
815 # The invoice_2 is not posted, therefore its cost should be in the "to_invoice" section
816 self.assertDictEqual(
817 self.project_billable_no_company._get_profitability_items(False)['revenues'],
818 {
819 'data': [{
820 'id': 'other_invoice_revenues',
821 'sequence': self.project_billable_no_company._get_profitability_sequence_per_invoice_type()['other_invoice_revenues'],
822 'to_invoice': (self.product_a.standard_price + 2 * self.product_b.standard_price + NEG_AMOUNT) * analytic_contribution,
823 'invoiced': self.product_a.standard_price * analytic_contribution * 1.2,
824 }],
825 'total': {
826 'to_invoice': (self.product_a.standard_price + 2 * self.product_b.standard_price + NEG_AMOUNT) * analytic_contribution,
827 'invoiced': self.product_a.standard_price * analytic_contribution * 1.2,
828 },
829 },
830 )
831 # post invoice_2
832 invoice_2.action_post()
833 # The invoice_2 is posted, therefore its cost should be in the "invoiced" section
834 self.assertDictEqual(
835 self.project_billable_no_company._get_profitability_items(False)['revenues'],
836 {
837 'data': [{
838 'id': 'other_invoice_revenues',
839 'sequence': self.project_billable_no_company._get_profitability_sequence_per_invoice_type()['other_invoice_revenues'],
840 'to_invoice': 0.0,
841 'invoiced': (2.2 * self.product_a.standard_price + 2 * self.product_b.standard_price + NEG_AMOUNT) * analytic_contribution,
842 }],
843 'total': {
844 'to_invoice': 0.0,
845 'invoiced': (2.2 * self.product_a.standard_price + 2 * self.product_b.standard_price + NEG_AMOUNT) * analytic_contribution,
846 },
847 },
848 )
849 # Create another invoice, with 2 lines, 2 diff products, the second line has a quantity of 2 with a foreign company.
850 invoice_2_foreign = self.env['account.move'].create({
851 "name": "I have 2 lines",
852 "move_type": "out_invoice",
853 "state": "draft",
854 "partner_id": self.partner.id,
855 "invoice_date": datetime.today(),
856 "company_id": foreign_company.id,
857 "invoice_line_ids": [Command.create({
858 "analytic_distribution": {self.project_billable_no_company.account_id.id: analytic_distribution},
859 "product_id": self.product_a.id,
860 "quantity": 1,
861 "product_uom_id": self.product_a.uom_id.id,
862 "price_unit": self.product_a.standard_price,
863 "currency_id": self.foreign_currency.id,
864 }), Command.create({
865 "analytic_distribution": {self.project_billable_no_company.account_id.id: analytic_distribution},
866 "product_id": self.product_b.id,
867 "quantity": 2,
868 "product_uom_id": self.product_b.uom_id.id,
869 "price_unit": self.product_b.standard_price,
870 "currency_id": self.foreign_currency.id,
871 })],
872 })
873 self.assertDictEqual(
874 self.project_billable_no_company._get_profitability_items(False)['revenues'],
875 {
876 'data': [{
877 'id': 'other_invoice_revenues',
878 'sequence': self.project_billable_no_company._get_profitability_sequence_per_invoice_type()['other_invoice_revenues'],
879 'to_invoice': (self.product_a.standard_price + 2 * self.product_b.standard_price) * analytic_contribution * 0.2,
880 'invoiced': (2.2 * self.product_a.standard_price + 2 * self.product_b.standard_price + NEG_AMOUNT) * analytic_contribution,
881 }],
882 'total': {
883 'to_invoice': (self.product_a.standard_price + 2 * self.product_b.standard_price) * analytic_contribution * 0.2,
884 'invoiced': (2.2 * self.product_a.standard_price + 2 * self.product_b.standard_price + NEG_AMOUNT) * analytic_contribution,
885 },
886 },
887 )
888 invoice_2_foreign.action_post()
889 # Note : for some reason, the method to round the amount to the rounding of the currency is not 100% reliable.
890 # We use a float_compare in order to ensure the value is close enough to the expected result. This problem has no repercusion on the client side, since
891 # there is also a rounding method on this side to ensure the amount is correctly displayed.
892 items = self.project_billable_no_company._get_profitability_items(False)['revenues']
893 self.assertEqual(float_compare(((self.product_a.standard_price + self.product_b.standard_price) * 2.4 + NEG_AMOUNT) * analytic_contribution, items['data'][0]['invoiced'], 2), 0)
894 self.assertEqual(float_compare(((self.product_a.standard_price + self.product_b.standard_price) * 2.4 + NEG_AMOUNT) * analytic_contribution, items['total']['invoiced'], 2), 0)
895 self.assertEqual(items['data'][0]['id'], 'other_invoice_revenues')
896 self.assertEqual(items['data'][0]['sequence'], self.project_billable_no_company._get_profitability_sequence_per_invoice_type()['other_invoice_revenues'])
897 self.assertEqual(items['data'][0]['to_invoice'], 0.0)
898 self.assertEqual(items['total']['to_invoice'], 0.0)
899
900 def test_bills_without_purchase_order_are_accounted_in_profitability_sale_project(self):
901 """
902 A bill that has an AAL on one of its line should be taken into account
903 for the profitability of the project.
904 """
905 foreign_company = self.company_data_2['company']
906 foreign_company.currency_id = self.foreign_currency
907
908 # Create a bill with its purchase line linked to the AA of the project, and a foreign company.
909 bill_1_foreign = self.env['account.move'].create({
910 "name": "Bill_1 name",
911 "move_type": "in_invoice",
912 "state": "draft",
913 "partner_id": self.partner.id,
914 "invoice_date": datetime.today(),
915 "company_id": foreign_company.id,
916 "invoice_line_ids": [Command.create({
917 "analytic_distribution": {self.project_billable_no_company.account_id.id: 100},
918 "product_id": self.product_a.id,
919 "quantity": 1,
920 "product_uom_id": self.product_a.uom_id.id,
921 "price_unit": self.product_a.standard_price,
922 "currency_id": self.foreign_currency.id
923 })],
924 })
925 # Add 2 new AAL to the analytic account. Those costs must be present in the 'other_cost' section
926 self.env['account.analytic.line'].create([{
927 'name': 'extra costs 1',
928 'account_id': self.project_billable_no_company.account_id.id,
929 'amount': -50,
930 }, {
931 'name': 'extra costs 2',
932 'account_id': self.project_billable_no_company.account_id.id,
933 'amount': -100,
934 }])
935 # Ensures that the amount of the 'other_purchase_cost' is correctly scale to the currency of the main company.
936 # Ensures that the 'other_cost' is not mixed within the 'other_purchase_costs' section and vice-versa
937 self.assertDictEqual(
938 self.project_billable_no_company._get_profitability_items(False)['costs'],
939 {
940 'data': [{
941 'id': 'other_costs_aal',
942 'sequence': self.project_billable_no_company._get_profitability_sequence_per_invoice_type()['other_costs_aal'],
943 'to_bill': 0.0,
944 'billed': -150.0,
945 }, {
946 'id': 'other_purchase_costs',
947 'sequence': self.project_billable_no_company._get_profitability_sequence_per_invoice_type()['other_purchase_costs'],
948 'to_bill': -self.product_a.standard_price * 0.2,
949 'billed': 0.0,
950 }],
951 'total': {'to_bill': -self.product_a.standard_price * 0.2, 'billed': -150.0},
952 },
953 )
954 # Create a bill with its purchase line linked to the AA of the project, and the main company.
955 bill_1 = self.env['account.move'].create({
956 "name": "Bill_1 name",
957 "move_type": "in_invoice",
958 "state": "draft",
959 "partner_id": self.partner.id,
960 "invoice_date": datetime.today(),
961 "invoice_line_ids": [Command.create({
962 "analytic_distribution": {self.project_billable_no_company.account_id.id: 100},
963 "product_id": self.product_a.id,
964 "quantity": 1,
965 "product_uom_id": self.product_a.uom_id.id,
966 "price_unit": self.product_a.standard_price,
967 })],
968 })
969 # Ensures that the amount from the bill_1 is in the 'to_bill' section of the 'other_purchase_cost'
970 self.assertDictEqual(
971 self.project_billable_no_company._get_profitability_items(False)['costs'],
972 {
973 'data': [{
974 'id': 'other_costs_aal',
975 'sequence': self.project_billable_no_company._get_profitability_sequence_per_invoice_type()['other_costs_aal'],
976 'to_bill': 0.0,
977 'billed': -150.0,
978 }, {
979 'id': 'other_purchase_costs',
980 'sequence': self.project_billable_no_company._get_profitability_sequence_per_invoice_type()['other_purchase_costs'],
981 'to_bill': -self.product_a.standard_price * 1.2,
982 'billed': 0.0,
983 }],
984 'total': {'to_bill': -self.product_a.standard_price * 1.2, 'billed': -150.0},
985 },
986 )
987 # post bill_1
988 bill_1.action_post()
989 # We posted the bill_1, therefore its cost should now be in the 'billed' section.
990 self.assertDictEqual(
991 self.project_billable_no_company._get_profitability_items(False)['costs'],
992 {
993 'data': [{
994 'id': 'other_costs_aal',
995 'sequence': self.project_billable_no_company._get_profitability_sequence_per_invoice_type()['other_costs_aal'],
996 'to_bill': 0.0,
997 'billed': -150.0,
998 }, {
999 'id': 'other_purchase_costs',
1000 'sequence': self.project_billable_no_company._get_profitability_sequence_per_invoice_type()['other_purchase_costs'],
1001 'to_bill': -self.product_a.standard_price * 0.2,
1002 'billed': -self.product_a.standard_price,
1003 }],
1004 'total': {'to_bill': -self.product_a.standard_price * 0.2, 'billed': -self.product_a.standard_price - 150},
1005 },
1006 )
1007 bill_1_foreign.action_post()
1008 # We posted the bill_1_foreign, therefore its cost should now be in the 'billed' section.
1009 self.assertDictEqual(
1010 self.project_billable_no_company._get_profitability_items(False)['costs'],
1011 {
1012 'data': [{
1013 'id': 'other_costs_aal',
1014 'sequence': self.project_billable_no_company._get_profitability_sequence_per_invoice_type()['other_costs_aal'],
1015 'to_bill': 0.0,
1016 'billed': -150.0,
1017 }, {
1018 'id': 'other_purchase_costs',
1019 'sequence': self.project_billable_no_company._get_profitability_sequence_per_invoice_type()['other_purchase_costs'],
1020 'to_bill': 0.0,
1021 'billed': -self.product_a.standard_price * 1.2,
1022 }],
1023 'total': {'to_bill': 0.0, 'billed': -self.product_a.standard_price * 1.2 - 150},
1024 },
1025 )
1026 # Create another bill, with 2 lines, 2 different products and different quantities
1027 bill_2 = self.env['account.move'].create({
1028 "name": "I have 2 lines",
1029 "move_type": "in_invoice",
1030 "state": "draft",
1031 "partner_id": self.partner.id,
1032 "invoice_date": datetime.today(),
1033 "invoice_line_ids": [Command.create({
1034 "analytic_distribution": {self.project_billable_no_company.account_id.id: 100},
1035 "product_id": self.product_a.id,
1036 "quantity": 1,
1037 "product_uom_id": self.product_a.uom_id.id,
1038 "price_unit": self.product_a.standard_price,
1039 }), Command.create({
1040 "analytic_distribution": {self.project_billable_no_company.account_id.id: 100},
1041 "product_id": self.product_b.id,
1042 "quantity": 2,
1043 "product_uom_id": self.product_b.uom_id.id,
1044 "price_unit": self.product_b.standard_price,
1045 })],
1046 })
1047 # Ensures that when there are more than one bill/move_line from one company, all the lines are computed.
1048 self.assertDictEqual(
1049 self.project_billable_no_company._get_profitability_items(False)['costs'],
1050 {
1051 'data': [{
1052 'id': 'other_costs_aal',
1053 'sequence': self.project_billable_no_company._get_profitability_sequence_per_invoice_type()['other_costs_aal'],
1054 'to_bill': 0.0,
1055 'billed': -150.0,
1056 }, {
1057 'id': 'other_purchase_costs',
1058 'sequence': self.project_billable_no_company._get_profitability_sequence_per_invoice_type()['other_purchase_costs'],
1059 'to_bill': -(self.product_a.standard_price + 2 * self.product_b.standard_price),
1060 'billed': -self.product_a.standard_price * 1.2,
1061 }],
1062 'total': {
1063 'to_bill': -(self.product_a.standard_price + 2 * self.product_b.standard_price),
1064 'billed': -self.product_a.standard_price * 1.2 - 150,
1065 },
1066 },
1067 )
1068 # post bill_2
1069 bill_2.action_post()
1070 # The bill_2 is posted, therefore its cost should now be in the 'billed' section.
1071 self.assertDictEqual(
1072 self.project_billable_no_company._get_profitability_items(False)['costs'],
1073 {
1074 'data': [{
1075 'id': 'other_costs_aal',
1076 'sequence': self.project_billable_no_company._get_profitability_sequence_per_invoice_type()['other_costs_aal'],
1077 'to_bill': 0.0,
1078 'billed': -150.0,
1079 }, {
1080 'id': 'other_purchase_costs',
1081 'sequence': self.project_billable_no_company._get_profitability_sequence_per_invoice_type()['other_purchase_costs'],
1082 'to_bill': 0.0,
1083 'billed': -(2.2 * self.product_a.standard_price + 2 * self.product_b.standard_price),
1084 }],
1085 'total': {
1086 'to_bill': 0.0,
1087 'billed': -(2.2 * self.product_a.standard_price + 2 * self.product_b.standard_price) - 150,
1088 },
1089 },
1090 )
1091 # Create another bill, with 2 lines, 2 different products, different quantities and a foreign company.
1092 bill_2_foreign = self.env['account.move'].create({
1093 "name": "I have 2 lines",
1094 "move_type": "in_invoice",
1095 "state": "draft",
1096 "partner_id": self.partner.id,
1097 "invoice_date": datetime.today(),
1098 "company_id": foreign_company.id,
1099 "invoice_line_ids": [Command.create({
1100 "analytic_distribution": {self.project_billable_no_company.account_id.id: 100},
1101 "product_id": self.product_a.id,
1102 "quantity": 1,
1103 "product_uom_id": self.product_a.uom_id.id,
1104 "price_unit": self.product_a.standard_price,
1105 "currency_id": self.foreign_currency.id,
1106 }), Command.create({
1107 "analytic_distribution": {self.project_billable_no_company.account_id.id: 100},
1108 "product_id": self.product_b.id,
1109 "quantity": 2,
1110 "product_uom_id": self.product_b.uom_id.id,
1111 "price_unit": self.product_b.standard_price,
1112 "currency_id": self.foreign_currency.id,
1113 })],
1114 })
1115 # Ensures that when there are more than one bill/move_line from one company, all the lines are computed and correctly scaled with the currency of the main company.
1116 self.assertDictEqual(
1117 self.project_billable_no_company._get_profitability_items(False)['costs'],
1118 {
1119 'data': [{
1120 'id': 'other_costs_aal',
1121 'sequence': self.project_billable_no_company._get_profitability_sequence_per_invoice_type()['other_costs_aal'],
1122 'to_bill': 0.0,
1123 'billed': -150.0,
1124 }, {
1125 'id': 'other_purchase_costs',
1126 'sequence': self.project_billable_no_company._get_profitability_sequence_per_invoice_type()['other_purchase_costs'],
1127 'to_bill': -(self.product_a.standard_price + 2 * self.product_b.standard_price) * 0.2,
1128 'billed': -(2.2 * self.product_a.standard_price + 2 * self.product_b.standard_price),
1129 }],
1130 'total': {
1131 'to_bill': -(self.product_a.standard_price + 2 * self.product_b.standard_price) * 0.2,
1132 'billed': -(2.2 * self.product_a.standard_price + 2 * self.product_b.standard_price) - 150,
1133 },
1134 },
1135 )
1136 # post bill_2_foreign
1137 bill_2_foreign.action_post()
1138 # The bill_2_foreign is posted, therefore its cost should now be in the 'billed' section.
1139 self.assertDictEqual(
1140 self.project_billable_no_company._get_profitability_items(False)['costs'],
1141 {
1142 'data': [{
1143 'id': 'other_costs_aal',
1144 'sequence': self.project_billable_no_company._get_profitability_sequence_per_invoice_type()[
1145 'other_costs_aal'],
1146 'to_bill': 0.0,
1147 'billed': -150.0,
1148 }, {
1149 'id': 'other_purchase_costs',
1150 'sequence': self.project_billable_no_company._get_profitability_sequence_per_invoice_type()[
1151 'other_purchase_costs'],
1152 'to_bill': 0.0,
1153 'billed': -2.4 * (self.product_a.standard_price + self.product_b.standard_price),
1154 }],
1155 'total': {
1156 'to_bill': 0.0,
1157 'billed': -2.4 * (self.product_a.standard_price + self.product_b.standard_price) - 150,
1158 },
1159 },
1160 )