test_project_profitability.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from datetime import datetime
  4. from odoo import Command
  5. from odoo.tests import tagged
  6. from odoo.addons.sale.tests.common import TestSaleCommon
  7. from odoo.addons.project.tests.test_project_profitability import TestProjectProfitabilityCommon as Common
  8. class TestProjectProfitabilityCommon(Common):
  9. @classmethod
  10. def setUpClass(cls):
  11. super().setUpClass()
  12. uom_unit_id = cls.env.ref('uom.product_uom_unit').id
  13. # Create material product
  14. cls.material_product = cls.env['product.product'].create({
  15. 'name': 'Material',
  16. 'type': 'consu',
  17. 'standard_price': 5,
  18. 'list_price': 10,
  19. 'invoice_policy': 'order',
  20. 'uom_id': uom_unit_id,
  21. 'uom_po_id': uom_unit_id,
  22. })
  23. # Create service products
  24. uom_hour = cls.env.ref('uom.product_uom_hour')
  25. cls.product_delivery_service = cls.env['product.product'].create({
  26. 'name': "Service Delivery, create task in global project",
  27. 'standard_price': 30,
  28. 'list_price': 90,
  29. 'type': 'service',
  30. 'invoice_policy': 'delivery',
  31. 'service_type': 'manual',
  32. 'uom_id': uom_hour.id,
  33. 'uom_po_id': uom_hour.id,
  34. 'default_code': 'SERV-ORDERED2',
  35. 'service_tracking': 'task_global_project',
  36. 'project_id': cls.project.id,
  37. })
  38. cls.sale_order = cls.env['sale.order'].with_context(tracking_disable=True).create({
  39. 'partner_id': cls.partner.id,
  40. 'partner_invoice_id': cls.partner.id,
  41. 'partner_shipping_id': cls.partner.id,
  42. })
  43. SaleOrderLine = cls.env['sale.order.line'].with_context(tracking_disable=True, default_order_id=cls.sale_order.id)
  44. cls.delivery_service_order_line = SaleOrderLine.create({
  45. 'product_id': cls.product_delivery_service.id,
  46. 'product_uom_qty': 10,
  47. })
  48. cls.sale_order.action_confirm()
  49. @tagged('-at_install', 'post_install')
  50. class TestSaleProjectProfitability(TestProjectProfitabilityCommon, TestSaleCommon):
  51. def test_project_profitability(self):
  52. self.assertFalse(self.project.allow_billable, 'The project should be non billable.')
  53. self.assertDictEqual(
  54. self.project._get_profitability_items(False),
  55. self.project_profitability_items_empty,
  56. 'No data for the project profitability should be found since the project is not billable, so no SOL is linked to the project.'
  57. )
  58. self.project.write({'allow_billable': True})
  59. self.assertTrue(self.project.allow_billable, 'The project should be billable.')
  60. self.project.sale_line_id = self.delivery_service_order_line
  61. self.assertDictEqual(
  62. self.project._get_profitability_items(False),
  63. self.project_profitability_items_empty,
  64. 'No data for the project profitability should be found since no product is delivered in the SO linked.'
  65. )
  66. self.delivery_service_order_line.qty_delivered = 1
  67. service_policy_to_invoice_type = self.project._get_service_policy_to_invoice_type()
  68. invoice_type = service_policy_to_invoice_type[self.delivery_service_order_line.product_id.service_policy]
  69. self.assertIn(
  70. invoice_type,
  71. ['billable_manual', 'service_revenues'],
  72. 'invoice_type="billable_manual" if sale_timesheet is installed otherwise it is equal to "service_revenues"')
  73. sequence_per_invoice_type = self.project._get_profitability_sequence_per_invoice_type()
  74. self.assertIn('service_revenues', sequence_per_invoice_type)
  75. self.assertDictEqual(
  76. self.project._get_profitability_items(False),
  77. {
  78. 'revenues': {
  79. 'data': [
  80. {
  81. # id should be equal to "billable_manual" if "sale_timesheet" module is installed otherwise "service_revenues"
  82. 'id': invoice_type,
  83. 'sequence': sequence_per_invoice_type[invoice_type],
  84. 'to_invoice': self.delivery_service_order_line.untaxed_amount_to_invoice,
  85. 'invoiced': self.delivery_service_order_line.untaxed_amount_invoiced,
  86. },
  87. ],
  88. 'total': {
  89. 'to_invoice': self.delivery_service_order_line.untaxed_amount_to_invoice,
  90. 'invoiced': self.delivery_service_order_line.untaxed_amount_invoiced,
  91. },
  92. },
  93. 'costs': {
  94. 'data': [],
  95. 'total': {'billed': 0.0, 'to_bill': 0.0},
  96. },
  97. }
  98. )
  99. self.assertNotEqual(self.delivery_service_order_line.untaxed_amount_to_invoice, 0.0)
  100. self.assertEqual(self.delivery_service_order_line.untaxed_amount_invoiced, 0.0)
  101. # create an invoice
  102. context = {
  103. 'active_model': 'sale.order',
  104. 'active_ids': self.sale_order.ids,
  105. 'active_id': self.sale_order.id,
  106. }
  107. invoices = self.env['sale.advance.payment.inv'].with_context(context).create({
  108. 'advance_payment_method': 'delivered',
  109. })._create_invoices(self.sale_order)
  110. invoices.action_post()
  111. self.assertEqual(self.delivery_service_order_line.qty_invoiced, 1)
  112. self.assertEqual(self.delivery_service_order_line.untaxed_amount_to_invoice, 0.0)
  113. self.assertNotEqual(self.delivery_service_order_line.untaxed_amount_invoiced, 0.0)
  114. invoice_type = service_policy_to_invoice_type[self.delivery_service_order_line.product_id.service_policy]
  115. self.assertIn(
  116. invoice_type,
  117. ['billable_manual', 'service_revenues'],
  118. 'invoice_type="billable_manual" if sale_timesheet is installed otherwise it is equal to "service_revenues"')
  119. self.assertDictEqual(
  120. self.project._get_profitability_items(False),
  121. {
  122. 'revenues': {
  123. 'data': [
  124. {
  125. 'id': invoice_type,
  126. 'sequence': sequence_per_invoice_type[invoice_type],
  127. 'to_invoice': 0.0,
  128. 'invoiced': self.delivery_service_order_line.untaxed_amount_invoiced,
  129. },
  130. ],
  131. 'total': {
  132. 'to_invoice': 0.0,
  133. 'invoiced': self.delivery_service_order_line.untaxed_amount_invoiced,
  134. },
  135. },
  136. 'costs': {
  137. 'data': [],
  138. 'total': {'billed': 0.0, 'to_bill': 0.0},
  139. },
  140. }
  141. )
  142. # Add 2 sales order items in the SO
  143. SaleOrderLine = self.env['sale.order.line'].with_context(tracking_disable=True, default_order_id=self.sale_order.id)
  144. manual_service_order_line = SaleOrderLine.create({
  145. 'product_id': self.product_delivery_service.id,
  146. 'product_uom_qty': 5,
  147. 'qty_delivered': 5,
  148. })
  149. material_order_line = SaleOrderLine.create({
  150. 'product_id': self.material_product.id,
  151. 'product_uom_qty': 1,
  152. 'qty_delivered': 1,
  153. })
  154. service_sols = self.delivery_service_order_line + manual_service_order_line
  155. invoice_type = service_policy_to_invoice_type[manual_service_order_line.product_id.service_policy]
  156. self.assertIn(
  157. invoice_type,
  158. ['billable_manual', 'service_revenues'],
  159. 'invoice_type="billable_manual" if sale_timesheet is installed otherwise it is equal to "service_revenues"')
  160. self.assertDictEqual(
  161. self.project._get_profitability_items(False),
  162. {
  163. 'revenues': {
  164. 'data': [
  165. {
  166. 'id': invoice_type,
  167. 'sequence': sequence_per_invoice_type[invoice_type],
  168. 'to_invoice': sum(service_sols.mapped('untaxed_amount_to_invoice')),
  169. 'invoiced': sum(service_sols.mapped('untaxed_amount_invoiced')),
  170. },
  171. {
  172. 'id': 'other_revenues',
  173. 'sequence': sequence_per_invoice_type['other_revenues'],
  174. 'to_invoice': material_order_line.untaxed_amount_to_invoice,
  175. 'invoiced': material_order_line.untaxed_amount_invoiced,
  176. },
  177. ],
  178. 'total': {
  179. 'to_invoice': sum(service_sols.mapped('untaxed_amount_to_invoice')) + material_order_line.untaxed_amount_to_invoice,
  180. 'invoiced': sum(service_sols.mapped('untaxed_amount_invoiced')) + material_order_line.untaxed_amount_invoiced,
  181. },
  182. },
  183. 'costs': { # no cost because we have no purchase orders.
  184. 'data': [],
  185. 'total': {'billed': 0.0, 'to_bill': 0.0},
  186. },
  187. },
  188. )
  189. self.assertNotEqual(manual_service_order_line.untaxed_amount_to_invoice, 0.0)
  190. self.assertEqual(manual_service_order_line.untaxed_amount_invoiced, 0.0)
  191. self.assertNotEqual(material_order_line.untaxed_amount_to_invoice, 0.0)
  192. self.assertEqual(material_order_line.untaxed_amount_invoiced, 0.0)
  193. credit_notes = invoices._reverse_moves()
  194. credit_notes.action_post()
  195. self.assertDictEqual(
  196. self.project._get_profitability_items(False),
  197. {
  198. 'revenues': {
  199. 'data': [
  200. {
  201. 'id': invoice_type,
  202. 'sequence': sequence_per_invoice_type[invoice_type],
  203. 'to_invoice': sum(service_sols.mapped('untaxed_amount_to_invoice')),
  204. 'invoiced': manual_service_order_line.untaxed_amount_invoiced,
  205. },
  206. {
  207. 'id': 'other_revenues',
  208. 'sequence': sequence_per_invoice_type['other_revenues'],
  209. 'to_invoice': material_order_line.untaxed_amount_to_invoice,
  210. 'invoiced': material_order_line.untaxed_amount_invoiced,
  211. },
  212. ],
  213. 'total': {
  214. 'to_invoice': sum(service_sols.mapped('untaxed_amount_to_invoice')) + material_order_line.untaxed_amount_to_invoice,
  215. 'invoiced': manual_service_order_line.untaxed_amount_invoiced + material_order_line.untaxed_amount_invoiced,
  216. },
  217. },
  218. 'costs': { # no cost because we have no purchase orders.
  219. 'data': [],
  220. 'total': {'billed': 0.0, 'to_bill': 0.0},
  221. },
  222. },
  223. )
  224. self.sale_order._action_cancel()
  225. self.assertDictEqual(
  226. self.project._get_profitability_items(False),
  227. self.project_profitability_items_empty,
  228. )
  229. def test_invoices_without_sale_order_are_accounted_in_profitability(self):
  230. """
  231. An invoice that has an AAL on one of its line should be taken into account
  232. for the profitability of the project.
  233. The contribution of the line should only be dependent
  234. on the project's analytic account % that was set on the line
  235. """
  236. self.project.allow_billable = True
  237. # a custom analytic contribution (number between 1 -> 100 included)
  238. analytic_distribution = 42
  239. analytic_contribution = analytic_distribution / 100.
  240. # create a invoice_1 with the AAL
  241. invoice_1 = self.env['account.move'].create({
  242. "name": "Invoice_1",
  243. "move_type": "out_invoice",
  244. "state": "draft",
  245. "partner_id": self.partner.id,
  246. "invoice_date": datetime.today(),
  247. "invoice_line_ids": [Command.create({
  248. "analytic_distribution": {self.analytic_account.id: analytic_distribution},
  249. "product_id": self.product_a.id,
  250. "quantity": 1,
  251. "product_uom_id": self.product_a.uom_id.id,
  252. "price_unit": self.product_a.standard_price,
  253. })],
  254. })
  255. # the bill_1 is in draft, therefor it should have the cost "to_invoice" same as the -product_price (untaxed)
  256. self.assertDictEqual(
  257. self.project._get_profitability_items(False)['revenues'],
  258. {
  259. 'data': [{
  260. 'id': 'other_invoice_revenues',
  261. 'sequence': self.project._get_profitability_sequence_per_invoice_type()['other_invoice_revenues'],
  262. 'to_invoice': self.product_a.standard_price * analytic_contribution,
  263. 'invoiced': 0.0,
  264. }],
  265. 'total': {'to_invoice': self.product_a.standard_price * analytic_contribution, 'invoiced': 0.0},
  266. },
  267. )
  268. # post invoice_1
  269. invoice_1.action_post()
  270. # we posted the invoice_1, therefore the revenue "invoiced" should be -product_price, to_invoice should be back to 0
  271. self.assertDictEqual(
  272. self.project._get_profitability_items(False)['revenues'],
  273. {
  274. 'data': [{
  275. 'id': 'other_invoice_revenues',
  276. 'sequence': self.project._get_profitability_sequence_per_invoice_type()['other_invoice_revenues'],
  277. 'to_invoice': 0.0,
  278. 'invoiced': self.product_a.standard_price * analytic_contribution,
  279. }],
  280. 'total': {'to_invoice': 0.0, 'invoiced': self.product_a.standard_price * analytic_contribution},
  281. },
  282. )
  283. # create another invoice, with 2 lines, 2 diff products, the second line has 2 as quantity
  284. invoice_2 = self.env['account.move'].create({
  285. "name": "I have 2 lines",
  286. "move_type": "out_invoice",
  287. "state": "draft",
  288. "partner_id": self.partner.id,
  289. "invoice_date": datetime.today(),
  290. "invoice_line_ids": [Command.create({
  291. "analytic_distribution": {self.analytic_account.id: analytic_distribution},
  292. "product_id": self.product_a.id,
  293. "quantity": 1,
  294. "product_uom_id": self.product_a.uom_id.id,
  295. "price_unit": self.product_a.standard_price,
  296. }), Command.create({
  297. "analytic_distribution": {self.analytic_account.id: analytic_distribution},
  298. "product_id": self.product_b.id,
  299. "quantity": 2,
  300. "product_uom_id": self.product_b.uom_id.id,
  301. "price_unit": self.product_b.standard_price,
  302. })],
  303. })
  304. # invoice_2 is not posted, therefor its cost should be "to_invoice" = - sum of all product_price * qty for each line
  305. self.assertDictEqual(
  306. self.project._get_profitability_items(False)['revenues'],
  307. {
  308. 'data': [{
  309. 'id': 'other_invoice_revenues',
  310. 'sequence': self.project._get_profitability_sequence_per_invoice_type()['other_invoice_revenues'],
  311. 'to_invoice': (self.product_a.standard_price + 2 * self.product_b.standard_price) * analytic_contribution,
  312. 'invoiced': self.product_a.standard_price * analytic_contribution,
  313. }],
  314. 'total': {
  315. 'to_invoice': (self.product_a.standard_price + 2 * self.product_b.standard_price) * analytic_contribution,
  316. 'invoiced': self.product_a.standard_price * analytic_contribution,
  317. },
  318. },
  319. )
  320. # post invoice_2
  321. invoice_2.action_post()
  322. # invoice_2 is posted, therefor its revenue should be counting in "invoiced", with the revenues from invoice_1
  323. self.assertDictEqual(
  324. self.project._get_profitability_items(False)['revenues'],
  325. {
  326. 'data': [{
  327. 'id': 'other_invoice_revenues',
  328. 'sequence': self.project._get_profitability_sequence_per_invoice_type()['other_invoice_revenues'],
  329. 'to_invoice': 0.0,
  330. 'invoiced': 2 * (self.product_a.standard_price + self.product_b.standard_price) * analytic_contribution,
  331. }],
  332. 'total': {
  333. 'to_invoice': 0.0,
  334. 'invoiced': 2 * (self.product_a.standard_price + self.product_b.standard_price) * analytic_contribution,
  335. },
  336. },
  337. )
  338. # invoice with negative subtotal on move line
  339. NEG_AMOUNT = -42
  340. invoice_3 = self.env['account.move'].create({
  341. "name": "I am negative",
  342. "move_type": "out_invoice",
  343. "state": "draft",
  344. "partner_id": self.partner.id,
  345. "invoice_date": datetime.today(),
  346. "invoice_line_ids": [Command.create({
  347. "analytic_distribution": {self.analytic_account.id: analytic_distribution},
  348. "product_id": self.product_a.id,
  349. "quantity": 1,
  350. "product_uom_id": self.product_a.uom_id.id,
  351. "price_unit": NEG_AMOUNT,
  352. }), Command.create({
  353. "product_id": self.product_a.id,
  354. "quantity": 1,
  355. "product_uom_id": self.product_a.uom_id.id,
  356. "price_unit": -NEG_AMOUNT, # so the invoice is not negative and we can post it
  357. })],
  358. })
  359. self.assertDictEqual(
  360. self.project._get_profitability_items(False)['revenues'],
  361. {
  362. 'data': [{
  363. 'id': 'other_invoice_revenues',
  364. 'sequence': self.project._get_profitability_sequence_per_invoice_type()['other_invoice_revenues'],
  365. 'to_invoice': NEG_AMOUNT * analytic_contribution,
  366. 'invoiced': 2 * (self.product_a.standard_price + self.product_b.standard_price) * analytic_contribution,
  367. }],
  368. 'total': {
  369. 'to_invoice': NEG_AMOUNT * analytic_contribution,
  370. 'invoiced': 2 * (self.product_a.standard_price + self.product_b.standard_price) * analytic_contribution,
  371. },
  372. },
  373. )
  374. invoice_3.action_post()
  375. self.assertDictEqual(
  376. self.project._get_profitability_items(False)['revenues'],
  377. {
  378. 'data': [{
  379. 'id': 'other_invoice_revenues',
  380. 'sequence': self.project._get_profitability_sequence_per_invoice_type()['other_invoice_revenues'],
  381. 'to_invoice': 0.0,
  382. 'invoiced': (2 * (self.product_a.standard_price + self.product_b.standard_price) + NEG_AMOUNT) * analytic_contribution,
  383. }],
  384. 'total': {
  385. 'to_invoice': 0.0,
  386. 'invoiced': (2 * (self.product_a.standard_price + self.product_b.standard_price) + NEG_AMOUNT) * analytic_contribution,
  387. },
  388. },
  389. )