test_purchase_dashboard.py 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from datetime import timedelta
  4. from odoo.addons.account.tests.common import AccountTestInvoicingCommon
  5. from odoo.addons.mail.tests.common import MailCase
  6. from odoo.tests import tagged, Form, new_test_user
  7. from odoo.tools import mute_logger, format_amount
  8. from odoo import fields
  9. @tagged('-at_install', 'post_install')
  10. class TestPurchaseDashboard(AccountTestInvoicingCommon, MailCase):
  11. @classmethod
  12. def setUpClass(cls, chart_template_ref=None):
  13. super().setUpClass(chart_template_ref=chart_template_ref)
  14. # Create two new users
  15. cls.user_a = new_test_user(cls.env, login='purchaseusera', groups='purchase.group_purchase_user')
  16. cls.user_b = new_test_user(cls.env, login='purchaseuserb', groups='purchase.group_purchase_user')
  17. # Create two products.
  18. product_data = {
  19. 'name': 'SuperProduct',
  20. 'type': 'consu',
  21. }
  22. cls.product_100 = cls.env['product.product'].create({**product_data, 'standard_price': 100})
  23. cls.product_250 = cls.env['product.product'].create({**product_data, 'standard_price': 250})
  24. @mute_logger('odoo.addons.mail.models.mail_mail')
  25. def test_purchase_dashboard(self):
  26. '''
  27. Test purchase dashboard values with multiple users.
  28. '''
  29. # Create 3 Request for Quotations with lines.
  30. rfqs = self.env['purchase.order'].create([{
  31. 'partner_id': self.partner_a.id,
  32. 'company_id': self.user_a.company_id.id,
  33. 'currency_id': self.user_a.company_id.currency_id.id,
  34. 'date_order': fields.Date.today(),
  35. } for i in range(3)])
  36. for rfq, qty in zip(rfqs, [1, 2, 3]):
  37. rfq_form = Form(rfq)
  38. with rfq_form.order_line.new() as line_1:
  39. line_1.product_id = self.product_100
  40. line_1.product_qty = qty
  41. with rfq_form.order_line.new() as line_2:
  42. line_2.product_id = self.product_250
  43. line_2.product_qty = qty
  44. rfq_form.save()
  45. # Create 1 late RFQ without line.
  46. self.env['purchase.order'].create([{
  47. 'partner_id': self.partner_a.id,
  48. 'company_id': self.user_a.company_id.id,
  49. 'currency_id': self.user_a.company_id.currency_id.id,
  50. 'date_order': fields.Date.today() - timedelta(days=7)
  51. }])
  52. # Create 1 draft RFQ for user A.
  53. self.env['purchase.order'].with_user(self.user_a).create([{
  54. 'partner_id': self.partner_a.id,
  55. 'company_id': self.user_a.company_id.id,
  56. 'currency_id': self.user_a.company_id.currency_id.id,
  57. 'date_order': fields.Date().today() + timedelta(days=7)
  58. }])
  59. self.flush_tracking()
  60. with self.mock_mail_gateway():
  61. rfqs[0].with_user(self.user_a).write({'state': 'sent'})
  62. self.flush_tracking()
  63. # Sanity checks for rfq state.
  64. self.assertEqual(rfqs[0].state, 'sent')
  65. with self.mock_mail_gateway():
  66. rfqs[1].with_user(self.user_b).write({'state': 'sent'})
  67. self.flush_tracking()
  68. self.assertEqual(rfqs[1].state, 'sent')
  69. # Confirm Orders with lines.
  70. rfqs.button_confirm()
  71. # Retrieve dashboard as User A to check 'my_{to_send, waiting, late}' values.
  72. dashboard_result = rfqs.with_user(self.user_a).retrieve_dashboard()
  73. # Check dashboard values
  74. currency_id = self.env.company.currency_id
  75. zero_value_keys = ['all_waiting', 'my_waiting', 'my_late']
  76. self.assertListEqual([dashboard_result[key] for key in zero_value_keys], [0]*len(zero_value_keys))
  77. self.assertEqual(dashboard_result['all_to_send'], 2)
  78. self.assertEqual(dashboard_result['my_to_send'], 1)
  79. self.assertEqual(dashboard_result['all_late'], 1)
  80. self.assertEqual(dashboard_result['all_avg_order_value'], format_amount(self.env, self.tax_purchase_a.compute_all(700.0)['total_included'], currency_id))
  81. self.assertEqual(dashboard_result['all_avg_days_to_purchase'], 0)
  82. self.assertEqual(dashboard_result['all_total_last_7_days'], format_amount(self.env, self.tax_purchase_a.compute_all(2100.0)['total_included'], currency_id))
  83. self.assertEqual(dashboard_result['all_sent_rfqs'], 2)