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.

80 lines
2.9 KiB

  1. # Copyright 2016 Tecnativa - Pedro M. Baeza
  2. # Copyright 2018 Tecnativa - Carlos Dauden
  3. # Copyright 2018 ACSONE SA/NV
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  5. import odoo.tests
  6. from odoo import exceptions
  7. @odoo.tests.at_install(False)
  8. @odoo.tests.post_install(True)
  9. class TestContractVariableQuantity(odoo.tests.HttpCase):
  10. def setUp(self):
  11. super(TestContractVariableQuantity, self).setUp()
  12. self.partner = self.env['res.partner'].create({'name': 'Test partner'})
  13. self.product = self.env['product.product'].create(
  14. {'name': 'Test product'}
  15. )
  16. self.contract = self.env['contract.contract'].create(
  17. {
  18. 'name': 'Test Contract',
  19. 'partner_id': self.partner.id,
  20. 'pricelist_id': self.partner.property_product_pricelist.id,
  21. }
  22. )
  23. self.formula = self.env['contract.line.qty.formula'].create(
  24. {
  25. 'name': 'Test formula',
  26. # For testing each of the possible variables
  27. 'code': 'env["res.users"]\n'
  28. 'context.get("lang")\n'
  29. 'user.id\n'
  30. 'line.qty_type\n'
  31. 'contract.id\n'
  32. 'quantity\n'
  33. 'period_first_date\n'
  34. 'period_last_date\n'
  35. 'invoice_date\n'
  36. 'result = 12',
  37. }
  38. )
  39. self.contract_line = self.env['contract.line'].create(
  40. {
  41. 'contract_id': self.contract.id,
  42. 'product_id': self.product.id,
  43. 'name': 'Test',
  44. 'qty_type': 'variable',
  45. 'qty_formula_id': self.formula.id,
  46. 'quantity': 1,
  47. 'uom_id': self.product.uom_id.id,
  48. 'price_unit': 100,
  49. 'discount': 50,
  50. 'recurring_rule_type': 'monthly',
  51. 'recurring_interval': 1,
  52. 'date_start': '2016-02-15',
  53. 'recurring_next_date': '2016-02-29',
  54. }
  55. )
  56. def test_check_invalid_code(self):
  57. with self.assertRaises(exceptions.ValidationError):
  58. self.formula.code = "sdsds"
  59. def test_check_no_return_value(self):
  60. with self.assertRaises(exceptions.ValidationError):
  61. self.formula.code = "user.id"
  62. def test_check_variable_quantity(self):
  63. self.contract.recurring_create_invoice()
  64. invoice = self.contract._get_related_invoices()
  65. self.assertEqual(invoice.invoice_line_ids[0].quantity, 12)
  66. def test_check_skip_zero_qty(self):
  67. self.formula.code = 'result=0'
  68. self.contract.skip_zero_qty = True
  69. invoice = self.contract.recurring_create_invoice()
  70. self.assertFalse(invoice.invoice_line_ids)
  71. self.contract.skip_zero_qty = False
  72. invoice = self.contract.recurring_create_invoice()
  73. self.assertAlmostEqual(invoice.invoice_line_ids[0].quantity, 0.0)