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.

219 lines
8.4 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. 'is_auto_renew': True,
  41. 'contract_template_id': self.contract_template1.id,
  42. }
  43. )
  44. self.product2.write(
  45. {
  46. 'is_contract': True,
  47. 'contract_template_id': self.contract_template2.id,
  48. }
  49. )
  50. self.order_line1 = self.sale.order_line.filtered(
  51. lambda l: l.product_id == self.product1
  52. )
  53. self.order_line1.date_start = '2018-01-01'
  54. self.contract = self.env["account.analytic.account"].create(
  55. {
  56. "name": "Test Contract 2",
  57. "partner_id": self.sale.partner_id.id,
  58. "pricelist_id":
  59. self.sale.partner_id.property_product_pricelist.id,
  60. "recurring_invoices": True,
  61. "contract_type": "purchase",
  62. "contract_template_id": self.contract_template1.id,
  63. "recurring_invoice_line_ids": [
  64. (
  65. 0,
  66. 0,
  67. {
  68. "product_id": self.product1.id,
  69. "name": "Services from #START# to #END#",
  70. "quantity": 1,
  71. "uom_id": self.product1.uom_id.id,
  72. "price_unit": 100,
  73. "discount": 50,
  74. "recurring_rule_type": "monthly",
  75. "recurring_interval": 1,
  76. "date_start": "2016-02-15",
  77. "recurring_next_date": "2016-02-29",
  78. },
  79. )
  80. ],
  81. }
  82. )
  83. self.contract_line = self.contract.recurring_invoice_line_ids[0]
  84. def test_compute_is_contract(self):
  85. """Sale Order should have is_contract true if one of its lines is
  86. contract"""
  87. self.assertTrue(self.sale.is_contract)
  88. def test_action_confirm_auto_renew_without_date_end(self):
  89. with self.assertRaises(ValidationError):
  90. self.sale.action_confirm()
  91. def test_action_confirm(self):
  92. """ It should create a contract for each contract template used in
  93. order_line """
  94. self.order_line1.onchange_product()
  95. self.sale.action_confirm()
  96. contracts = self.sale.order_line.mapped('contract_id')
  97. self.assertEqual(len(contracts), 2)
  98. self.assertEqual(
  99. self.order_line1.contract_id.contract_template_id,
  100. self.contract_template1,
  101. )
  102. def test_sale_contract_count(self):
  103. """It should count contracts as many different contract template used
  104. in order_line"""
  105. self.order_line1.onchange_product()
  106. self.sale.action_confirm()
  107. self.assertEqual(self.sale.contract_count, 2)
  108. def test_onchange_product(self):
  109. """ It should get recurrence invoicing info to the sale line from
  110. its product """
  111. self.order_line1.onchange_product()
  112. self.assertEqual(
  113. self.order_line1.recurring_rule_type,
  114. self.product1.recurring_rule_type,
  115. )
  116. self.assertEqual(
  117. self.order_line1.recurring_interval,
  118. self.product1.recurring_interval,
  119. )
  120. self.assertEqual(
  121. self.order_line1.recurring_invoicing_type,
  122. self.product1.recurring_invoicing_type,
  123. )
  124. self.assertEqual(
  125. self.order_line1.date_end,
  126. Date.to_date('2019-01-01'),
  127. )
  128. def test_check_contract_sale_partner(self):
  129. """Can't link order line to a partner contract different then the
  130. order one"""
  131. contract2 = self.env['account.analytic.account'].create(
  132. {
  133. 'name': 'Contract',
  134. 'contract_template_id': self.contract_template2.id,
  135. 'partner_id': self.sale.partner_id.id,
  136. }
  137. )
  138. with self.assertRaises(ValidationError):
  139. self.order_line1.contract_id = contract2
  140. def test_check_contract_sale_contract_template(self):
  141. """Can't link order line to a contract with different contract
  142. template then the product one"""
  143. contract1 = self.env['account.analytic.account'].create(
  144. {
  145. 'name': 'Contract',
  146. 'contract_template_id': self.contract_template1.id,
  147. }
  148. )
  149. with self.assertRaises(ValidationError):
  150. self.order_line1.contract_id = contract1
  151. def test_no_contract_proudct(self):
  152. """it should create contract for only product contract"""
  153. self.product1.is_contract = False
  154. self.sale.action_confirm()
  155. self.assertFalse(self.order_line1.contract_id)
  156. def test_sale_order_line_invoice_status(self):
  157. """Sale order line for contract product should have nothing to
  158. invoice as status"""
  159. self.order_line1.onchange_product()
  160. self.sale.action_confirm()
  161. self.assertEqual(self.order_line1.invoice_status, 'no')
  162. def test_sale_order_invoice_status(self):
  163. """Sale order with only contract product should have nothing to
  164. invoice status directtly"""
  165. self.sale.order_line.filtered(
  166. lambda line: not line.product_id.is_contract
  167. ).unlink()
  168. self.order_line1.onchange_product()
  169. self.sale.action_confirm()
  170. self.assertEqual(self.sale.invoice_status, 'no')
  171. def test_sale_order_create_invoice(self):
  172. """Should not invoice contract product on sale order create invoice"""
  173. self.product2.is_contract = False
  174. self.product2.invoice_policy = 'order'
  175. self.order_line1.onchange_product()
  176. self.sale.action_confirm()
  177. self.sale.action_invoice_create()
  178. self.assertEqual(len(self.sale.invoice_ids), 1)
  179. invoice_line = self.sale.invoice_ids.invoice_line_ids.filtered(
  180. lambda line: line.product_id.is_contract
  181. )
  182. self.assertEqual(len(invoice_line), 0)
  183. def test_link_contract_invoice_to_sale_order(self):
  184. """It should link contract invoice to sale order"""
  185. self.order_line1.onchange_product()
  186. self.sale.action_confirm()
  187. invoice = self.order_line1.contract_id.recurring_create_invoice()
  188. self.assertTrue(invoice in self.sale.invoice_ids)
  189. def test_contract_upsell(self):
  190. """Should stop contract line at sale order line start date"""
  191. self.order_line1.contract_id = self.contract
  192. self.order_line1.contract_line_id = self.contract_line
  193. self.contract_line.date_end = "2019-01-01"
  194. self.contract_line.is_auto_renew = "2019-01-01"
  195. self.order_line1.date_start = "2018-06-01"
  196. self.order_line1.onchange_product()
  197. self.sale.action_confirm()
  198. self.assertEqual(
  199. self.contract_line.date_end, Date.to_date("2018-06-01")
  200. )
  201. self.assertFalse(
  202. self.contract_line.is_auto_renew
  203. )