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.

137 lines
5.1 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 LasLabs Inc.
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from odoo.tests.common import TransactionCase
  5. from odoo.exceptions import ValidationError
  6. class TestSaleOrder(TransactionCase):
  7. def setUp(self):
  8. super(TestSaleOrder, self).setUp()
  9. self.product1 = self.env.ref('product.product_product_1')
  10. self.product2 = self.env.ref('product.product_product_2')
  11. self.sale = self.env.ref('sale.sale_order_2')
  12. self.contract_template1 = self.env['account.analytic.contract'].create(
  13. {'name': 'Template 1'}
  14. )
  15. self.contract_template2 = self.env['account.analytic.contract'].create(
  16. {'name': 'Template 2'}
  17. )
  18. self.product1.write(
  19. {
  20. 'is_contract': True,
  21. 'contract_template_id': self.contract_template1.id,
  22. }
  23. )
  24. self.product2.write(
  25. {
  26. 'is_contract': True,
  27. 'contract_template_id': self.contract_template2.id,
  28. }
  29. )
  30. self.order_line1 = self.sale.order_line.filtered(
  31. lambda l: l.product_id == self.product1
  32. )
  33. def test_compute_is_contract(self):
  34. """Sale Order should have is_contract true if one of its lines is
  35. contract"""
  36. self.assertTrue(self.sale.is_contract)
  37. def test_action_confirm(self):
  38. """ It should create a contract for each contract template used in
  39. order_line """
  40. self.sale.action_confirm()
  41. contracts = self.sale.order_line.mapped('contract_id')
  42. self.assertEqual(len(contracts), 2)
  43. self.assertEqual(
  44. self.order_line1.contract_id.contract_template_id,
  45. self.contract_template1,
  46. )
  47. def test_sale_contract_count(self):
  48. """It should count contracts as many different contract template used
  49. in order_line"""
  50. self.sale.action_confirm()
  51. self.assertEqual(self.sale.contract_count, 2)
  52. def test_onchange_product(self):
  53. """ It should get recurrence invoicing info to the sale line from
  54. its product """
  55. self.assertEqual(
  56. self.order_line1.recurring_rule_type,
  57. self.product1.recurring_rule_type,
  58. )
  59. self.assertEqual(
  60. self.order_line1.recurring_interval,
  61. self.product1.recurring_interval,
  62. )
  63. self.assertEqual(
  64. self.order_line1.recurring_invoicing_type,
  65. self.product1.recurring_invoicing_type,
  66. )
  67. def test_check_contract_sale_partner(self):
  68. """Can't link order line to a partner contract different then the
  69. order one"""
  70. contract2 = self.env['account.analytic.account'].create(
  71. {
  72. 'name': 'Contract',
  73. 'contract_template_id': self.contract_template2.id,
  74. 'partner_id': self.sale.partner_id.id,
  75. }
  76. )
  77. with self.assertRaises(ValidationError):
  78. self.order_line1.contract_id = contract2
  79. def test_check_contract_sale_contract_template(self):
  80. """Can't link order line to a contract with different contract
  81. template then the product one"""
  82. contract1 = self.env['account.analytic.account'].create(
  83. {
  84. 'name': 'Contract',
  85. 'contract_template_id': self.contract_template1.id,
  86. }
  87. )
  88. with self.assertRaises(ValidationError):
  89. self.order_line1.contract_id = contract1
  90. def test_no_contract_proudct(self):
  91. """it should create contract for only product contract"""
  92. self.product1.is_contract = False
  93. self.sale.action_confirm()
  94. self.assertFalse(self.order_line1.contract_id)
  95. def test_sale_order_line_invoice_status(self):
  96. """Sale order line for contract product should have nothing to
  97. invoice as status"""
  98. self.sale.action_confirm()
  99. self.assertEqual(self.order_line1.invoice_status, 'no')
  100. def test_sale_order_invoice_status(self):
  101. """Sale order with only contract product should have nothing to
  102. invoice status directtly"""
  103. self.sale.order_line.filtered(
  104. lambda line: not line.product_id.is_contract
  105. ).unlink()
  106. self.sale.action_confirm()
  107. self.assertEqual(self.sale.invoice_status, 'no')
  108. def test_sale_order_create_invoice(self):
  109. """Should not invoice contract product on sale order create invoice"""
  110. self.product2.is_contract = False
  111. self.product2.invoice_policy = 'order'
  112. self.sale.action_confirm()
  113. self.sale.action_invoice_create()
  114. self.assertEqual(len(self.sale.invoice_ids), 1)
  115. invoice_line = self.sale.invoice_ids.invoice_line_ids.filtered(
  116. lambda line: line.product_id.is_contract
  117. )
  118. self.assertEqual(len(invoice_line), 0)
  119. def test_link_contract_invoice_to_sale_order(self):
  120. """It should link contract invoice to sale order"""
  121. self.sale.action_confirm()
  122. invoice = self.order_line1.contract_id.recurring_create_invoice()
  123. self.assertTrue(invoice in self.sale.invoice_ids)