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.

100 lines
4.3 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.recurring_create_sale()
  55. self.sale_monthly = self.env['sale.order'].search(
  56. [('project_id', '=', self.contract.id),
  57. ('state', '=', 'draft')])
  58. self.assertTrue(self.sale_monthly)
  59. self.assertEqual(self.contract.recurring_next_date, '2017-02-28')
  60. self.sale_line = self.sale_monthly.order_line[0]
  61. self.assertAlmostEqual(self.sale_line.price_subtotal, 50.0)
  62. self.assertEqual(self.contract.partner_id.user_id,
  63. self.sale_monthly.user_id)
  64. def test_contract_autoconfirm(self):
  65. self.contract.sale_autoconfirm = True
  66. self.assertAlmostEqual(self.contract_line.price_subtotal, 50.0)
  67. res = self.contract_line._onchange_product_id()
  68. self.assertIn('uom_id', res['domain'])
  69. self.contract_line.price_unit = 100.0
  70. self.contract.recurring_create_sale()
  71. self.sale_monthly = self.env['sale.order'].search(
  72. [('project_id', '=', self.contract.id),
  73. ('state', '=', 'sale')])
  74. self.assertTrue(self.sale_monthly)
  75. self.assertEqual(self.contract.recurring_next_date, '2017-02-28')
  76. self.sale_line = self.sale_monthly.order_line[0]
  77. self.assertAlmostEqual(self.sale_line.price_subtotal, 50.0)
  78. self.assertEqual(self.contract.partner_id.user_id,
  79. self.sale_monthly.user_id)
  80. def test_onchange_contract_template_id(self):
  81. """ It should change the contract values to match the template. """
  82. self.contract.contract_template_id = self.template
  83. self.contract._onchange_contract_template_id()
  84. res = {
  85. 'recurring_rule_type': self.contract.recurring_rule_type,
  86. 'recurring_interval': self.contract.recurring_interval,
  87. 'type': 'sale',
  88. 'sale_autoconfirm': False
  89. }
  90. del self.template_vals['name']
  91. self.assertDictEqual(res, self.template_vals)