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.

155 lines
5.8 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. {
  17. 'name': 'Template 2',
  18. 'recurring_invoice_line_ids': [
  19. (
  20. 0,
  21. 0,
  22. {
  23. 'product_id': self.product2.id,
  24. 'name': 'Services from #START# to #END#',
  25. 'quantity': 1,
  26. 'uom_id': self.product2.uom_id.id,
  27. 'price_unit': 100,
  28. 'discount': 50,
  29. 'recurring_rule_type': 'yearly',
  30. 'recurring_interval': 1,
  31. },
  32. )
  33. ],
  34. }
  35. )
  36. self.product1.write(
  37. {
  38. 'is_contract': True,
  39. 'contract_template_id': self.contract_template1.id,
  40. }
  41. )
  42. self.product2.write(
  43. {
  44. 'is_contract': True,
  45. 'contract_template_id': self.contract_template2.id,
  46. }
  47. )
  48. self.order_line1 = self.sale.order_line.filtered(
  49. lambda l: l.product_id == self.product1
  50. )
  51. def test_compute_is_contract(self):
  52. """Sale Order should have is_contract true if one of its lines is
  53. contract"""
  54. self.assertTrue(self.sale.is_contract)
  55. def test_action_confirm(self):
  56. """ It should create a contract for each contract template used in
  57. order_line """
  58. self.sale.action_confirm()
  59. contracts = self.sale.order_line.mapped('contract_id')
  60. self.assertEqual(len(contracts), 2)
  61. self.assertEqual(
  62. self.order_line1.contract_id.contract_template_id,
  63. self.contract_template1,
  64. )
  65. def test_sale_contract_count(self):
  66. """It should count contracts as many different contract template used
  67. in order_line"""
  68. self.sale.action_confirm()
  69. self.assertEqual(self.sale.contract_count, 2)
  70. def test_onchange_product(self):
  71. """ It should get recurrence invoicing info to the sale line from
  72. its product """
  73. self.assertEqual(
  74. self.order_line1.recurring_rule_type,
  75. self.product1.recurring_rule_type,
  76. )
  77. self.assertEqual(
  78. self.order_line1.recurring_interval,
  79. self.product1.recurring_interval,
  80. )
  81. self.assertEqual(
  82. self.order_line1.recurring_invoicing_type,
  83. self.product1.recurring_invoicing_type,
  84. )
  85. def test_check_contract_sale_partner(self):
  86. """Can't link order line to a partner contract different then the
  87. order one"""
  88. contract2 = self.env['account.analytic.account'].create(
  89. {
  90. 'name': 'Contract',
  91. 'contract_template_id': self.contract_template2.id,
  92. 'partner_id': self.sale.partner_id.id,
  93. }
  94. )
  95. with self.assertRaises(ValidationError):
  96. self.order_line1.contract_id = contract2
  97. def test_check_contract_sale_contract_template(self):
  98. """Can't link order line to a contract with different contract
  99. template then the product one"""
  100. contract1 = self.env['account.analytic.account'].create(
  101. {
  102. 'name': 'Contract',
  103. 'contract_template_id': self.contract_template1.id,
  104. }
  105. )
  106. with self.assertRaises(ValidationError):
  107. self.order_line1.contract_id = contract1
  108. def test_no_contract_proudct(self):
  109. """it should create contract for only product contract"""
  110. self.product1.is_contract = False
  111. self.sale.action_confirm()
  112. self.assertFalse(self.order_line1.contract_id)
  113. def test_sale_order_line_invoice_status(self):
  114. """Sale order line for contract product should have nothing to
  115. invoice as status"""
  116. self.sale.action_confirm()
  117. self.assertEqual(self.order_line1.invoice_status, 'no')
  118. def test_sale_order_invoice_status(self):
  119. """Sale order with only contract product should have nothing to
  120. invoice status directtly"""
  121. self.sale.order_line.filtered(
  122. lambda line: not line.product_id.is_contract
  123. ).unlink()
  124. self.sale.action_confirm()
  125. self.assertEqual(self.sale.invoice_status, 'no')
  126. def test_sale_order_create_invoice(self):
  127. """Should not invoice contract product on sale order create invoice"""
  128. self.product2.is_contract = False
  129. self.product2.invoice_policy = 'order'
  130. self.sale.action_confirm()
  131. self.sale.action_invoice_create()
  132. self.assertEqual(len(self.sale.invoice_ids), 1)
  133. invoice_line = self.sale.invoice_ids.invoice_line_ids.filtered(
  134. lambda line: line.product_id.is_contract
  135. )
  136. self.assertEqual(len(invoice_line), 0)
  137. def test_link_contract_invoice_to_sale_order(self):
  138. """It should link contract invoice to sale order"""
  139. self.sale.action_confirm()
  140. invoice = self.order_line1.contract_id.recurring_create_invoice()
  141. self.assertTrue(invoice in self.sale.invoice_ids)