You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
4.6 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 Carlos Dauden <carlos.dauden@tecnativa.com>
  3. # Copyright 2017 Pesol (<http://pesol.es>)
  4. # Copyright 2017 Angel Moya <angel.moya@pesol.es>
  5. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  6. from odoo.exceptions import ValidationError
  7. from odoo.tests.common import TransactionCase
  8. class TestContractSale(TransactionCase):
  9. # Use case : Prepare some data for current test case
  10. def setUp(self):
  11. super(TestContractSale, self).setUp()
  12. self.partner = self.env.ref('base.res_partner_2')
  13. self.product = self.env.ref('product.product_product_2')
  14. self.product.taxes_id += self.env['account.tax'].search(
  15. [('type_tax_use', '=', 'sale')], limit=1)
  16. self.product.description_sale = 'Test description sale'
  17. self.template_vals = {
  18. 'recurring_rule_type': 'yearly',
  19. 'recurring_interval': 1,
  20. 'name': 'Test Contract Template',
  21. 'type': 'sale',
  22. 'sale_autoconfirm': False
  23. }
  24. self.template = self.env['account.analytic.contract'].create(
  25. self.template_vals,
  26. )
  27. self.contract = self.env['account.analytic.account'].create({
  28. 'name': 'Test Contract',
  29. 'partner_id': self.partner.id,
  30. 'pricelist_id': self.partner.property_product_pricelist.id,
  31. 'recurring_invoices': True,
  32. 'date_start': '2016-02-15',
  33. 'recurring_next_date': '2016-02-29',
  34. })
  35. self.contract.contract_template_id = self.template
  36. self.contract._onchange_contract_template_id()
  37. self.contract_line = self.env['account.analytic.invoice.line'].create({
  38. 'analytic_account_id': self.contract.id,
  39. 'product_id': self.product.id,
  40. 'name': 'Services from #START# to #END#',
  41. 'quantity': 1,
  42. 'uom_id': self.product.uom_id.id,
  43. 'price_unit': 100,
  44. 'discount': 50,
  45. })
  46. def test_check_discount(self):
  47. with self.assertRaises(ValidationError):
  48. self.contract_line.write({'discount': 120})
  49. def test_contract(self):
  50. self.assertAlmostEqual(self.contract_line.price_subtotal, 50.0)
  51. res = self.contract_line._onchange_product_id()
  52. self.assertIn('uom_id', res['domain'])
  53. self.contract_line.price_unit = 100.0
  54. self.contract.partner_id = False
  55. with self.assertRaises(ValidationError):
  56. self.contract.recurring_create_sale()
  57. self.contract.partner_id = self.partner.id
  58. self.contract.recurring_create_sale()
  59. self.sale_monthly = self.env['sale.order'].search(
  60. [('project_id', '=', self.contract.id),
  61. ('state', '=', 'draft')])
  62. self.assertTrue(self.sale_monthly)
  63. self.assertEqual(self.contract.recurring_next_date, '2017-02-28')
  64. self.sale_line = self.sale_monthly.order_line[0]
  65. self.assertAlmostEqual(self.sale_line.price_subtotal, 50.0)
  66. self.assertEqual(self.contract.partner_id.user_id,
  67. self.sale_monthly.user_id)
  68. def test_contract_autoconfirm(self):
  69. self.contract.sale_autoconfirm = True
  70. self.assertAlmostEqual(self.contract_line.price_subtotal, 50.0)
  71. res = self.contract_line._onchange_product_id()
  72. self.assertIn('uom_id', res['domain'])
  73. self.contract_line.price_unit = 100.0
  74. self.contract.partner_id = False
  75. with self.assertRaises(ValidationError):
  76. self.contract.recurring_create_sale()
  77. self.contract.partner_id = self.partner.id
  78. self.contract.recurring_create_sale()
  79. self.sale_monthly = self.env['sale.order'].search(
  80. [('project_id', '=', self.contract.id),
  81. ('state', '=', 'sale')])
  82. self.assertTrue(self.sale_monthly)
  83. self.assertEqual(self.contract.recurring_next_date, '2017-02-28')
  84. self.sale_line = self.sale_monthly.order_line[0]
  85. self.assertAlmostEqual(self.sale_line.price_subtotal, 50.0)
  86. self.assertEqual(self.contract.partner_id.user_id,
  87. self.sale_monthly.user_id)
  88. def test_onchange_contract_template_id(self):
  89. """ It should change the contract values to match the template. """
  90. self.contract.contract_template_id = self.template
  91. self.contract._onchange_contract_template_id()
  92. res = {
  93. 'recurring_rule_type': self.contract.recurring_rule_type,
  94. 'recurring_interval': self.contract.recurring_interval,
  95. 'type': 'sale',
  96. 'sale_autoconfirm': False
  97. }
  98. del self.template_vals['name']
  99. self.assertDictEqual(res, self.template_vals)