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.

196 lines
7.5 KiB

  1. # Copyright 2017 LasLabs Inc.
  2. # Copyright 2018 ACSONE SA/NV
  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. from odoo.fields import Date
  7. class TestSaleOrder(TransactionCase):
  8. def setUp(self):
  9. super(TestSaleOrder, self).setUp()
  10. self.product1 = self.env.ref('product.product_product_1')
  11. self.product2 = self.env.ref('product.product_product_2')
  12. self.sale = self.env.ref('sale.sale_order_2')
  13. self.contract_template1 = self.env['account.analytic.contract'].create(
  14. {'name': 'Template 1'}
  15. )
  16. self.contract_template2 = self.env['account.analytic.contract'].create(
  17. {
  18. 'name': 'Template 2',
  19. 'recurring_invoice_line_ids': [
  20. (
  21. 0,
  22. 0,
  23. {
  24. 'product_id': self.product2.id,
  25. 'name': 'Services from #START# to #END#',
  26. 'quantity': 1,
  27. 'uom_id': self.product2.uom_id.id,
  28. 'price_unit': 100,
  29. 'discount': 50,
  30. 'recurring_rule_type': 'yearly',
  31. 'recurring_interval': 1,
  32. },
  33. )
  34. ],
  35. }
  36. )
  37. self.product1.write(
  38. {
  39. 'is_contract': True,
  40. 'contract_template_id': self.contract_template1.id,
  41. }
  42. )
  43. self.product2.write(
  44. {
  45. 'is_contract': True,
  46. 'contract_template_id': self.contract_template2.id,
  47. }
  48. )
  49. self.order_line1 = self.sale.order_line.filtered(
  50. lambda l: l.product_id == self.product1
  51. )
  52. self.contract = self.env["account.analytic.account"].create(
  53. {
  54. "name": "Test Contract 2",
  55. "partner_id": self.sale.partner_id.id,
  56. "pricelist_id":
  57. self.sale.partner_id.property_product_pricelist.id,
  58. "recurring_invoices": True,
  59. "contract_type": "purchase",
  60. "contract_template_id": self.contract_template1.id,
  61. "recurring_invoice_line_ids": [
  62. (
  63. 0,
  64. 0,
  65. {
  66. "product_id": self.product1.id,
  67. "name": "Services from #START# to #END#",
  68. "quantity": 1,
  69. "uom_id": self.product1.uom_id.id,
  70. "price_unit": 100,
  71. "discount": 50,
  72. "recurring_rule_type": "monthly",
  73. "recurring_interval": 1,
  74. "date_start": "2016-02-15",
  75. "recurring_next_date": "2016-02-29",
  76. },
  77. )
  78. ],
  79. }
  80. )
  81. self.contract_line = self.contract.recurring_invoice_line_ids[0]
  82. def test_compute_is_contract(self):
  83. """Sale Order should have is_contract true if one of its lines is
  84. contract"""
  85. self.assertTrue(self.sale.is_contract)
  86. def test_action_confirm(self):
  87. """ It should create a contract for each contract template used in
  88. order_line """
  89. self.sale.action_confirm()
  90. contracts = self.sale.order_line.mapped('contract_id')
  91. self.assertEqual(len(contracts), 2)
  92. self.assertEqual(
  93. self.order_line1.contract_id.contract_template_id,
  94. self.contract_template1,
  95. )
  96. def test_sale_contract_count(self):
  97. """It should count contracts as many different contract template used
  98. in order_line"""
  99. self.sale.action_confirm()
  100. self.assertEqual(self.sale.contract_count, 2)
  101. def test_onchange_product(self):
  102. """ It should get recurrence invoicing info to the sale line from
  103. its product """
  104. self.assertEqual(
  105. self.order_line1.recurring_rule_type,
  106. self.product1.recurring_rule_type,
  107. )
  108. self.assertEqual(
  109. self.order_line1.recurring_interval,
  110. self.product1.recurring_interval,
  111. )
  112. self.assertEqual(
  113. self.order_line1.recurring_invoicing_type,
  114. self.product1.recurring_invoicing_type,
  115. )
  116. def test_check_contract_sale_partner(self):
  117. """Can't link order line to a partner contract different then the
  118. order one"""
  119. contract2 = self.env['account.analytic.account'].create(
  120. {
  121. 'name': 'Contract',
  122. 'contract_template_id': self.contract_template2.id,
  123. 'partner_id': self.sale.partner_id.id,
  124. }
  125. )
  126. with self.assertRaises(ValidationError):
  127. self.order_line1.contract_id = contract2
  128. def test_check_contract_sale_contract_template(self):
  129. """Can't link order line to a contract with different contract
  130. template then the product one"""
  131. contract1 = self.env['account.analytic.account'].create(
  132. {
  133. 'name': 'Contract',
  134. 'contract_template_id': self.contract_template1.id,
  135. }
  136. )
  137. with self.assertRaises(ValidationError):
  138. self.order_line1.contract_id = contract1
  139. def test_no_contract_proudct(self):
  140. """it should create contract for only product contract"""
  141. self.product1.is_contract = False
  142. self.sale.action_confirm()
  143. self.assertFalse(self.order_line1.contract_id)
  144. def test_sale_order_line_invoice_status(self):
  145. """Sale order line for contract product should have nothing to
  146. invoice as status"""
  147. self.sale.action_confirm()
  148. self.assertEqual(self.order_line1.invoice_status, 'no')
  149. def test_sale_order_invoice_status(self):
  150. """Sale order with only contract product should have nothing to
  151. invoice status directtly"""
  152. self.sale.order_line.filtered(
  153. lambda line: not line.product_id.is_contract
  154. ).unlink()
  155. self.sale.action_confirm()
  156. self.assertEqual(self.sale.invoice_status, 'no')
  157. def test_sale_order_create_invoice(self):
  158. """Should not invoice contract product on sale order create invoice"""
  159. self.product2.is_contract = False
  160. self.product2.invoice_policy = 'order'
  161. self.sale.action_confirm()
  162. self.sale.action_invoice_create()
  163. self.assertEqual(len(self.sale.invoice_ids), 1)
  164. invoice_line = self.sale.invoice_ids.invoice_line_ids.filtered(
  165. lambda line: line.product_id.is_contract
  166. )
  167. self.assertEqual(len(invoice_line), 0)
  168. def test_link_contract_invoice_to_sale_order(self):
  169. """It should link contract invoice to sale order"""
  170. self.sale.action_confirm()
  171. invoice = self.order_line1.contract_id.recurring_create_invoice()
  172. self.assertTrue(invoice in self.sale.invoice_ids)
  173. def test_contract_upsell(self):
  174. """Should stop contract line at sale order line start date"""
  175. self.order_line1.contract_id = self.contract
  176. self.order_line1.contract_line_id = self.contract_line
  177. self.order_line1.date_start = "2018-01-01"
  178. self.sale.action_confirm()
  179. self.assertEqual(
  180. self.contract_line.date_end, Date.to_date("2018-01-01")
  181. )